1 // This is gel/vsol/vsol_point_2d.cxx
2 #include "vsol_point_2d.h"
3 //:
4 // \file
5 #include "vgl/vgl_distance.h"
6
7 //***************************************************************************
8 // Initialization
9 //***************************************************************************
10
11 //---------------------------------------------------------------------------
12 // Destructor
13 //---------------------------------------------------------------------------
14 vsol_point_2d::~vsol_point_2d() = default;
15
16 //---------------------------------------------------------------------------
17 //: Clone `this': creation of a new object and initialization
18 // See Prototype pattern
19 //---------------------------------------------------------------------------
clone() const20 vsol_spatial_object_2d *vsol_point_2d::clone() const {
21 return new vsol_point_2d(*this);
22 }
23
24 //***************************************************************************
25 // Comparison
26 //***************************************************************************
27
28 //---------------------------------------------------------------------------
29 //: Has `this' the same coordinates than `other' ?
30 //---------------------------------------------------------------------------
operator ==(const vsol_point_2d & other) const31 bool vsol_point_2d::operator==(const vsol_point_2d &other) const
32 {
33 return this==&other || p_==other.p_;
34 }
35
36 //: spatial object equality
37
operator ==(const vsol_spatial_object_2d & obj) const38 bool vsol_point_2d::operator==(const vsol_spatial_object_2d& obj) const
39 {
40 return obj.cast_to_point() && *this == *obj.cast_to_point();
41 }
42
43 //***************************************************************************
44 // Status report
45 //***************************************************************************
46
47 //---------------------------------------------------------------------------
48 //: Return the real type of a point. It is a POINT
49 //---------------------------------------------------------------------------
50 vsol_spatial_object_2d::vsol_spatial_object_2d_type
spatial_type() const51 vsol_point_2d::spatial_type() const {
52 return POINT;
53 }
54
55 //---------------------------------------------------------------------------
56 //: Compute the bounding box of `this'
57 //---------------------------------------------------------------------------
compute_bounding_box() const58 void vsol_point_2d::compute_bounding_box() const {
59 set_bounding_box(p_.x(),p_.y());
60 }
61
62 //***************************************************************************
63 // Status setting
64 //***************************************************************************
65
66 //---------------------------------------------------------------------------
67 //: Set the abscissa
68 //---------------------------------------------------------------------------
set_x(const double new_x)69 void vsol_point_2d::set_x(const double new_x)
70 {
71 p_.set(new_x, p_.y());
72 }
73
74 //---------------------------------------------------------------------------
75 //: Set the ordinate
76 //---------------------------------------------------------------------------
set_y(const double new_y)77 void vsol_point_2d::set_y(const double new_y)
78 {
79 p_.set(p_.x(), new_y);
80 }
81
82 //***************************************************************************
83 // Basic operations
84 //***************************************************************************
85
86 //---------------------------------------------------------------------------
87 //: Return the distance (N2) between `this' and `other'
88 //---------------------------------------------------------------------------
distance(const vsol_point_2d & other) const89 double vsol_point_2d::distance(const vsol_point_2d &other) const
90 {
91 return vgl_distance(p_,other.p_);
92 }
93
distance(vsol_point_2d_sptr other) const94 double vsol_point_2d::distance(vsol_point_2d_sptr other) const
95 {
96 return vgl_distance(p_,other->p_);
97 }
98
99 //---------------------------------------------------------------------------
100 //: Return the middle point between `this' and `other'
101 //---------------------------------------------------------------------------
middle(const vsol_point_2d & other) const102 vsol_point_2d_sptr vsol_point_2d::middle(const vsol_point_2d &other) const
103 {
104 return new vsol_point_2d(midpoint(p_,other.p_));
105 }
106
107 //---------------------------------------------------------------------------
108 //: Add `v' to `this'
109 //---------------------------------------------------------------------------
add_vector(vgl_vector_2d<double> const & v)110 void vsol_point_2d::add_vector(vgl_vector_2d<double> const& v)
111 {
112 p_ += v;
113 }
114
115 //---------------------------------------------------------------------------
116 //: Add `v' and `this'
117 //---------------------------------------------------------------------------
118 vsol_point_2d_sptr
plus_vector(vgl_vector_2d<double> const & v) const119 vsol_point_2d::plus_vector(vgl_vector_2d<double> const& v) const
120 {
121 return new vsol_point_2d(p_ + v);
122 }
123
124 //---------------------------------------------------------------------------
125 //: Return the vector `this',`other'.
126 //---------------------------------------------------------------------------
127 vgl_vector_2d<double>
to_vector(const vsol_point_2d & other) const128 vsol_point_2d::to_vector(const vsol_point_2d &other) const
129 {
130 return {other.x() - x(), other.y() - y()};
131 }
132
133 //----------------------------------------------------------------
134 // ================ Binary I/O Methods ========================
135 //----------------------------------------------------------------
136
137 //: Binary save self to stream.
b_write(vsl_b_ostream & os) const138 void vsol_point_2d::b_write(vsl_b_ostream &os) const
139 {
140 vsl_b_write(os, version());
141 vsol_spatial_object_2d::b_write(os);
142 vsl_b_write(os, p_.x());
143 vsl_b_write(os, p_.y());
144 }
145
146 //: Binary load self from stream (not typically used)
b_read(vsl_b_istream & is)147 void vsol_point_2d::b_read(vsl_b_istream &is)
148 {
149 if (!is)
150 return;
151 short ver;
152 vsl_b_read(is, ver);
153 switch (ver)
154 {
155 case 1:
156 vsol_spatial_object_2d::b_read(is);
157 { double x=0, y=0;
158 vsl_b_read(is, x);
159 vsl_b_read(is, y);
160 this->p_.set(x, y);
161 }
162 break;
163
164 default:
165 std::cerr << "I/O ERROR: vsol_point_2d::b_read(vsl_b_istream&)\n"
166 << " Unknown version number "<< ver << '\n';
167 is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
168 return;
169 }
170 }
171
172 //: Return IO version number;
version() const173 short vsol_point_2d::version() const
174 {
175 return 1;
176 }
177
178 //: Print an ascii summary to the stream
print_summary(std::ostream & os) const179 void vsol_point_2d::print_summary(std::ostream &os) const
180 {
181 os << *this;
182 }
183
184 //: Binary save vsol_point_2d to stream.
185 void
vsl_b_write(vsl_b_ostream & os,const vsol_point_2d * p)186 vsl_b_write(vsl_b_ostream &os, const vsol_point_2d* p)
187 {
188 if (p==nullptr) {
189 vsl_b_write(os, false); // Indicate null pointer stored
190 }
191 else{
192 vsl_b_write(os,true); // Indicate non-null pointer stored
193 p->b_write(os);
194 }
195 }
196
197
198 //: Binary load vsol_point_2d from stream.
199 void
vsl_b_read(vsl_b_istream & is,vsol_point_2d * & p)200 vsl_b_read(vsl_b_istream &is, vsol_point_2d* &p)
201 {
202 delete p;
203 bool not_null_ptr;
204 vsl_b_read(is, not_null_ptr);
205 if (not_null_ptr) {
206 p = new vsol_point_2d();
207 p->b_read(is);
208 }
209 else
210 p = nullptr;
211 }
212