1 // This is rpl/rsdl/rsdl_point.cxx
2 #include <iostream>
3 #include "rsdl_point.h"
4 //:
5 //  \file
6 
7 #include <cassert>
8 #ifdef _MSC_VER
9 #  include "vcl_msvc_warnings.h"
10 #endif
11 #include "vnl/vnl_vector.h"
12 
13 rsdl_point::rsdl_point()
14 
15     = default;
16 
rsdl_point(unsigned int Nc,unsigned int Na)17 rsdl_point::rsdl_point( unsigned int Nc, unsigned int Na )
18   : Nc_(Nc), Na_(Na), data_( new double[ Nc_ + Na_ ] )
19 {
20 }
21 
rsdl_point(const_iterator c_begin,const_iterator c_end,const_iterator a_begin,const_iterator a_end)22 rsdl_point::rsdl_point( const_iterator c_begin, const_iterator c_end,
23                         const_iterator a_begin, const_iterator a_end )
24   : Nc_( unsigned(c_end - c_begin) ),
25     Na_( unsigned(a_end - a_begin) ),
26     data_( new double[ Nc_ + Na_ ] )
27 {
28   for ( unsigned int i=0; i<Nc_; ++i ) data_[ i ] = * ( c_begin + i );
29   for ( unsigned int j=0; j<Na_; ++j ) data_[ Nc_ +j ]= * ( a_begin + j );
30 }
31 
32 
rsdl_point(const vnl_vector<double> & c,const vnl_vector<double> & a)33 rsdl_point::rsdl_point( const vnl_vector<double>& c, const vnl_vector<double>& a )
34   : Nc_( c.size() ), Na_( a.size() ),  data_( new double[ Nc_ + Na_ ] )
35 {
36   for ( unsigned int i=0; i<Nc_; ++i ) data_[ i ] = c[i];
37   for ( unsigned int j=0; j<Na_; ++j ) data_[ Nc_ + j ]= a[j];
38 }
39 
40 
rsdl_point(const rsdl_point & old)41 rsdl_point::rsdl_point( const rsdl_point& old )
42   : Nc_( old.Nc_ ), Na_( old.Na_ ), data_( new double[ Nc_ + Na_ ] )
43 {
44   for ( unsigned int i=0; i< Nc_ + Na_ ; ++i ) data_[i] = old.data_[i];
45 }
46 
47 
rsdl_point(const vnl_vector<double> & all,unsigned int Na)48 rsdl_point::rsdl_point( const vnl_vector<double>& all, unsigned int Na )
49   : Nc_( all.size() - Na ), Na_( Na )
50 {
51   assert( all.size() >= Na ); // Nc_ is always >= 0  :-)
52   data_ = new double[ Nc_ + Na_ ];
53   for ( unsigned int i=0; i< Nc_ + Na_; ++i )
54     data_[i] = all[i];
55 }
56 
~rsdl_point()57 rsdl_point::~rsdl_point()
58 {
59   delete [] data_;
60 }
61 
62 
63 void
set_cartesian(const vnl_vector<double> & c)64 rsdl_point::set_cartesian( const vnl_vector<double>& c )
65 {
66   assert ( c.size() == Nc_ );
67   for ( unsigned int i=0; i<Nc_; ++i )
68     data_[i] = c[i];
69 }
70 
71 
72 void
set_cartesian(const_iterator c)73 rsdl_point::set_cartesian( const_iterator c )
74 {
75   for ( unsigned int i=0; i<Nc_; ++i, ++c )
76     data_[i] = *c;
77 }
78 
79 
80 void
set_angular(const vnl_vector<double> & a)81 rsdl_point::set_angular( const vnl_vector<double>& a )
82 {
83   assert ( a.size() == Na_ );
84   for ( unsigned int i=0; i<Na_; ++i )
85     data_[ Nc_ + i ] = a[i];
86 }
87 
88 //: \brief  Establish the cartesian and angular values from a vnl_vector.  Size is checked.
89 void
set_all(const vnl_vector<double> & all)90 rsdl_point::set_all( const vnl_vector<double>& all )
91 {
92    assert ( all.size() == Nc_ + Na_ );
93    for ( unsigned int i=0; i < Nc_ + Na_; ++i )
94      data_[i] = all[i];
95 }
96 
97 
98 void
set_angular(const_iterator a)99 rsdl_point::set_angular( const_iterator a )
100 {
101   for ( unsigned int i=0; i<Na_; ++i, ++a )
102     data_[Nc_+i] = *a;
103 }
104 
105 
106 rsdl_point &
operator =(const rsdl_point & old)107 rsdl_point::operator= ( const rsdl_point& old )
108 {
109   if ( this != &old ) {
110     if ( Nc_ + Na_ != old.Nc_ + old.Na_ ) {
111       delete [] data_;
112       data_ = new double[ old.Nc_ + old.Na_ ];
113     }
114     Nc_ = old.Nc_;
115     Na_ = old.Na_;
116     for ( unsigned int i=0; i < Nc_ + Na_; ++i ) data_[i] = old.data_[i];
117   }
118   return *this;
119 }
120 
121 void
resize(unsigned int Nc,unsigned int Na)122 rsdl_point::resize( unsigned int Nc, unsigned int Na )
123 {
124   if ( Nc_ != Nc || Na_ != Na ) {
125     auto* buf = new double[ Nc + Na ];
126     unsigned int min_c = ( Nc < Nc_ ? Nc : Nc_ );
127     unsigned int min_a = ( Na < Na_ ? Na : Na_ );
128     for ( unsigned int i=0; i < min_c; ++i )  buf[i] = data_[i];
129     for ( unsigned int i=0; i < min_a; ++i )  buf[Nc+i] = data_[Nc_+i];
130     delete[] data_;
131     data_ = buf;
132     Nc_ = Nc;
133     Na_ = Na;
134   }
135 }
136 
operator <<(std::ostream & ostr,const rsdl_point & pt)137 std::ostream& operator<< ( std::ostream& ostr, const rsdl_point& pt )
138 {
139   int Nc = pt.num_cartesian();
140   int Na = pt.num_angular();
141   int i;
142   std::cout << "Cartesian: [";
143   for ( i=0; i<Nc; ++i ) {
144     std::cout << pt.cartesian(i);
145     if ( i<Nc-1 ) std::cout << ",";
146   }
147   std::cout << "];  Angular: [";
148   for ( i=0; i<Na; ++i ) {
149     std::cout << pt.angular(i);
150     if ( i<Na-1 ) std::cout << ",";
151   }
152   std::cout << "]";
153   return ostr;
154 }
155