1 |
francois |
1061 |
// nUtil - An utility Library for gnurbs |
2 |
|
|
// Copyright (C) 2008-2019 Eric Bechet |
3 |
|
|
// |
4 |
|
|
// See the LICENSE file for contributions and license information. |
5 |
|
|
// Please report all bugs and problems to <bechet@cadxfem.org>. |
6 |
|
|
// |
7 |
|
|
|
8 |
|
|
#ifndef __NPOINT_H |
9 |
|
|
#define __NPOINT_H |
10 |
|
|
|
11 |
|
|
#include <cmath> |
12 |
|
|
#include <iostream> |
13 |
|
|
|
14 |
|
|
#ifndef n_pi |
15 |
|
|
#define n_pi (3.1415926535897932384626433832795) |
16 |
|
|
#endif |
17 |
|
|
|
18 |
|
|
class npoint2; |
19 |
|
|
class npoint3; |
20 |
|
|
|
21 |
|
|
/// 3D point (generic class). |
22 |
|
|
class npoint3D |
23 |
|
|
{ |
24 |
|
|
public: |
25 |
|
|
/// \brief Destructor. |
26 |
|
|
virtual ~npoint3D() {} |
27 |
|
|
|
28 |
|
|
/// \brief Returns x coordinate (constant version). |
29 |
|
|
/// \return Coordinate. |
30 |
|
|
virtual double x() const=0; |
31 |
|
|
|
32 |
|
|
/// \brief Returns y coordinate (constant version). |
33 |
|
|
/// \return Coordinate. |
34 |
|
|
virtual double y() const=0; |
35 |
|
|
|
36 |
|
|
/// \brief Returns z coordinate (constant version). |
37 |
|
|
/// \return Coordinate. |
38 |
|
|
virtual double z() const=0; |
39 |
|
|
|
40 |
|
|
/// \brief Returns homogeneous coordinate (constant version). |
41 |
|
|
/// \return Coordinate. |
42 |
|
|
virtual double w() const=0; |
43 |
|
|
|
44 |
|
|
/// \brief Returns the ith coordinate (from 0 to 3). |
45 |
|
|
/// \return Coordinate. |
46 |
|
|
virtual double operator()(const int i) const=0; // always able to return 4 items (3D x y z + weight) |
47 |
|
|
}; |
48 |
|
|
|
49 |
|
|
/// 4D point. |
50 |
|
|
class npoint : public npoint3D |
51 |
|
|
{ |
52 |
|
|
double coord[4]; //!< Homogeneous coordinates. |
53 |
|
|
public : |
54 |
|
|
|
55 |
|
|
/// \brief Destructor. |
56 |
|
|
virtual ~npoint() {} |
57 |
|
|
|
58 |
|
|
/// \brief Default constructor. |
59 |
|
|
npoint() |
60 |
|
|
{ |
61 |
|
|
coord[0]=coord[1]=coord[2]=0.0; |
62 |
|
|
coord[3]=1.0; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
/// \brief Constructor a npoint from a npoint3. |
66 |
|
|
/// \param[in] pt 3D point. |
67 |
|
|
/// \param[in] w weight. |
68 |
|
|
npoint(const npoint3 pt, const double w=1) ; |
69 |
|
|
|
70 |
|
|
/// \brief Constructor a npoint from a npoint2. |
71 |
|
|
/// \param[in] pt 2D point. |
72 |
|
|
/// \param[in] w weight. |
73 |
|
|
npoint(const npoint2 pt, const double w=1) ; |
74 |
|
|
|
75 |
|
|
/// \brief Constructor a npoint from a value. |
76 |
|
|
/// \param[in] pt 1D point. |
77 |
|
|
/// \param[in] w weight. |
78 |
|
|
npoint(const double pt, const double w) ; |
79 |
|
|
|
80 |
|
|
/// \brief Constructor a npoint from three scalars. |
81 |
|
|
/// \param[in] x x-coordinate. |
82 |
|
|
/// \param[in] y y-coordinate. |
83 |
|
|
/// \param[in] z z-coordinate. |
84 |
|
|
npoint(double x,double y,double z) |
85 |
|
|
{ |
86 |
|
|
coord[0]=x; |
87 |
|
|
coord[1]=y; |
88 |
|
|
coord[2]=z; |
89 |
|
|
coord[3]=1.; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
/// \brief Constructor a npoint from four scalars. |
93 |
|
|
/// \param[in] x x-coordinate. |
94 |
|
|
/// \param[in] y y-coordinate. |
95 |
|
|
/// \param[in] z z-coordinate. |
96 |
|
|
/// \param[in] w weight. |
97 |
|
|
npoint(double x,double y,double z,double w) |
98 |
|
|
{ |
99 |
|
|
coord[0]=x; |
100 |
|
|
coord[1]=y; |
101 |
|
|
coord[2]=z; |
102 |
|
|
coord[3]=w; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
/// \brief Returns the ith homogeneous coordinate (from 0 to 3) - mutable version. |
106 |
|
|
/// \param[in] i wanted coordinate number. |
107 |
|
|
/// \return Coordinate. |
108 |
|
|
double & operator[](const int i) |
109 |
|
|
{ |
110 |
|
|
return coord[i]; // renvoie la ieme coordonnee (modifiable) |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
/// \brief Returns the ith homogeneous coordinate (from 0 to 3) - constant version. |
114 |
|
|
/// \param[in] i wanted coordinate number. |
115 |
|
|
/// \return Coordinate. |
116 |
|
|
double operator[](const int i) const |
117 |
|
|
{ |
118 |
|
|
return coord[i]; // renvoie la ieme coordonnee (constante) |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
/// \brief Returns the ith coordinate (from 0 to 3). |
122 |
|
|
/// \warning If \f$ i \in [0, 2] \f$ a perspective division is performed on the returned coordinate. |
123 |
|
|
/// \param[in] i wanted coordinate number. |
124 |
|
|
/// \return Coordinate. |
125 |
|
|
virtual double operator()(const int i) const |
126 |
|
|
{ |
127 |
|
|
if (i<3) |
128 |
|
|
if (coord[3]!=0.0) return coord[i]/coord[3]; else return coord[i]; |
129 |
|
|
else return coord[3]; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
/// \brief Returns the homogeneous x coordinate (mutable version). |
133 |
|
|
/// \return Coordinate. |
134 |
|
|
double & wx() |
135 |
|
|
{ |
136 |
|
|
return coord[0]; // renvoie w*x |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
/// \brief Returns the homogeneous x coordinate (constant version). |
140 |
|
|
/// \return Coordinate. |
141 |
|
|
double wx() const |
142 |
|
|
{ |
143 |
|
|
return coord[0]; |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
/// \brief Returns the x coordinate. |
147 |
|
|
/// \warning A perspective division is performed. |
148 |
|
|
/// \return Coordinate. |
149 |
|
|
virtual double x() const |
150 |
|
|
{ |
151 |
|
|
if (coord[3]!=0.0) |
152 |
|
|
return coord[0]/coord[3]; |
153 |
|
|
else |
154 |
|
|
return coord[0]; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
/// \brief Returns the homogeneous y coordinate (mutable version). |
158 |
|
|
/// \return Coordinate. |
159 |
|
|
double & wy() |
160 |
|
|
{ |
161 |
|
|
return coord[1]; // "" w*y |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
/// \brief Returns the homogeneous y coordinate (constant version). |
165 |
|
|
/// \return Coordinate. |
166 |
|
|
double wy() const |
167 |
|
|
{ |
168 |
|
|
return coord[1]; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
/// \brief Returns the y coordinate. |
172 |
|
|
/// \warning A perspective division is performed. |
173 |
|
|
/// \return Coordinate. |
174 |
|
|
virtual double y() const |
175 |
|
|
{ |
176 |
|
|
if (coord[3]!=0.0) |
177 |
|
|
return coord[1]/coord[3]; |
178 |
|
|
else |
179 |
|
|
return coord[1]; |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
/// \brief Returns the homogeneous z coordinate (mutable version). |
183 |
|
|
/// \return Coordinate. |
184 |
|
|
double & wz() |
185 |
|
|
{ |
186 |
|
|
return coord[2]; // "" w*z |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
/// \brief Returns the homogeneous z coordinate (constant version). |
190 |
|
|
/// \return Coordinate. |
191 |
|
|
double wz() const |
192 |
|
|
{ |
193 |
|
|
return coord[2]; |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
/// \brief Returns the z coordinate. |
197 |
|
|
/// \warning A perspective division is performed. |
198 |
|
|
/// \return Coordinate. |
199 |
|
|
virtual double z() const |
200 |
|
|
{ |
201 |
|
|
if (coord[3]!=0.0) |
202 |
|
|
return coord[2]/coord[3]; |
203 |
|
|
else |
204 |
|
|
return coord[2]; |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
/// \brief Returns the weight (mutable version). |
208 |
|
|
/// \return Coordinate. |
209 |
|
|
double & w() |
210 |
|
|
{ |
211 |
|
|
return coord[3]; // "" w |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
/// \brief Returns the weight (constant version). |
215 |
|
|
/// \return Coordinate. |
216 |
|
|
virtual double w() const |
217 |
|
|
{ |
218 |
|
|
return coord[3]; |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
/// \brief Returns array on data (mutable version). |
222 |
|
|
/// \return Array. |
223 |
|
|
double * array() |
224 |
|
|
{ |
225 |
|
|
return coord; |
226 |
|
|
}; // "" adresse de la premiere coordonee |
227 |
|
|
|
228 |
|
|
/// \brief Returns array on data (constant version). |
229 |
|
|
/// \return Array. |
230 |
|
|
const double * array() const |
231 |
|
|
{ |
232 |
|
|
return coord; // "" adresse de la premiere coordonee |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
/// \brief Substract this point \f$ P \f$ with another point: \f$ P_{out} = P - P_{other} \f$. |
236 |
|
|
/// \param[in] other \f$ P_{other} \f$. |
237 |
|
|
/// \return \f$ P_{out} \f$. |
238 |
|
|
npoint operator- (npoint other) const // renvoie la soustraction des coords. de deux points |
239 |
|
|
{ |
240 |
|
|
for (int i=0;i<4;++i) other.coord[i]=coord[i]-other.coord[i]; |
241 |
|
|
return other; |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
/// \brief Adds this point \f$ P \f$ with another point: \f$ P_{out} = P + P_{other} \f$. |
245 |
|
|
/// \param[in] other \f$ P_{other} \f$. |
246 |
|
|
/// \return \f$ P_{out} \f$. |
247 |
|
|
npoint operator+ (npoint other) const // "" addition "" |
248 |
|
|
{ |
249 |
|
|
for (int i=0;i<4;++i) other.coord[i]+=coord[i]; |
250 |
|
|
return other; |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
/// \brief Divides this point \f$ P \f$ with a scalar: \f$ P_{out} = P / a \f$. |
254 |
|
|
/// \param[in] fact \f$ a \f$. |
255 |
|
|
/// \return \f$ P_{out} \f$. |
256 |
|
|
npoint operator/ (const double fact) const // "" division par une constante |
257 |
|
|
{ |
258 |
|
|
npoint buf=*this; |
259 |
|
|
for (int i=0;i<4;++i) buf[i]/=fact; |
260 |
|
|
return buf; |
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
/// \brief Right-multiply this point \f$ P \f$ with a scalar: \f$ P_{out} = P\ a \f$. |
264 |
|
|
/// \param[in] fact \f$ a \f$. |
265 |
|
|
/// \return \f$ P_{out} \f$. |
266 |
|
|
npoint operator*(const double fact) const // "" multiplication (a droite) par un scalaire |
267 |
|
|
{ |
268 |
|
|
npoint buf=*this; |
269 |
|
|
for (int i=0;i<4;++i) buf[i]*=fact; |
270 |
|
|
return buf; |
271 |
|
|
} |
272 |
|
|
|
273 |
|
|
/// \brief Adds this point with another, this point is modified. |
274 |
|
|
/// param[in] other other point. |
275 |
|
|
/// \return Modified point. |
276 |
|
|
npoint& operator+= (const npoint other) // ajout "en place" |
277 |
|
|
{ |
278 |
|
|
for (int i=0;i<4;++i) coord[i]+=other.coord[i]; |
279 |
|
|
return *this; |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
/// \brief Substract this point with another, this point is modified. |
283 |
|
|
/// param[in] other other point. |
284 |
|
|
/// \return Modified point. |
285 |
|
|
npoint& operator-= (const npoint other) // soustraction "en place" |
286 |
|
|
{ |
287 |
|
|
for (int i=0;i<4;++i) coord[i]-=other.coord[i]; |
288 |
|
|
return *this; |
289 |
|
|
} |
290 |
|
|
|
291 |
|
|
|
292 |
|
|
friend npoint operator * (const double fact,const npoint other); // "" multiplication (a gauche) par un scalaire |
293 |
|
|
|
294 |
|
|
/// Euclidean norm in 4D/homogeneous space. |
295 |
|
|
/// \return Norm. |
296 |
|
|
double norm() const |
297 |
|
|
{ |
298 |
|
|
return sqrt(coord[0]*coord[0]+coord[1]*coord[1]+coord[2]*coord[2]+coord[3]*coord[3]); |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
/// Euclidean norm in 3D space. |
302 |
|
|
/// \return Norm. |
303 |
|
|
double norm3D() const |
304 |
|
|
{ |
305 |
|
|
if (w()!=1.0) |
306 |
|
|
{ |
307 |
|
|
if (w()!=0.0) |
308 |
|
|
return sqrt((coord[0]*coord[0]+coord[1]*coord[1]+coord[2]*coord[2])/(coord[3]*coord[3])); |
309 |
|
|
else |
310 |
|
|
return sqrt(coord[0]*coord[0]+coord[1]*coord[1]+coord[2]*coord[2]); |
311 |
|
|
} |
312 |
|
|
else return sqrt(coord[0]*coord[0]+coord[1]*coord[1]+coord[2]*coord[2]); |
313 |
|
|
} |
314 |
|
|
|
315 |
|
|
/// \brief Compute the perspective division of this point. |
316 |
|
|
/// \return True if point in 3D space, false if vector. |
317 |
|
|
bool perspective_divide() // returns true if point in 3D space, false if vector |
318 |
|
|
{ |
319 |
|
|
double w=coord[3]; |
320 |
|
|
if (w!=1.0) |
321 |
|
|
{ |
322 |
|
|
if (w!=0.0) |
323 |
|
|
{ |
324 |
|
|
for (int i=0;i<4;++i) coord[i]/=w; |
325 |
|
|
return true; |
326 |
|
|
} |
327 |
|
|
else return false; |
328 |
|
|
} |
329 |
|
|
else return true; |
330 |
|
|
} |
331 |
|
|
|
332 |
|
|
/// \brief Print this point to output stream. |
333 |
|
|
/// \param[in,out] os output stream. |
334 |
|
|
/// \param[in] prec printing precision. |
335 |
|
|
void print(std::ostream &os,int prec=5) const ; // sort les coordonees dans le flux os |
336 |
|
|
}; |
337 |
|
|
|
338 |
|
|
npoint operator * (const double fact,const npoint other); |
339 |
|
|
class npoint2; |
340 |
|
|
|
341 |
|
|
/// 3D point. |
342 |
|
|
class npoint3 : public npoint3D |
343 |
|
|
{ |
344 |
|
|
double coord[3]; //!< Array of coordinates. |
345 |
|
|
public : |
346 |
|
|
|
347 |
|
|
/// \brief Destructor. |
348 |
|
|
virtual ~npoint3() {} |
349 |
|
|
|
350 |
|
|
/// \brief Default constructor. |
351 |
|
|
npoint3() |
352 |
|
|
{ |
353 |
|
|
coord[0]=coord[1]=coord[2]=0.; |
354 |
|
|
} |
355 |
|
|
|
356 |
|
|
/// \brief Initializes point from 3D array. |
357 |
|
|
/// \param[in] xyz 3D array. |
358 |
|
|
npoint3(const double *xyz) |
359 |
|
|
{ |
360 |
|
|
coord[0]=xyz[0]; |
361 |
|
|
coord[1]=xyz[1]; // initialisation |
362 |
|
|
coord[2]=xyz[2]; |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
/// \brief Initializes point from 3 scalars. |
366 |
|
|
/// \param x first coordinate. |
367 |
|
|
/// \param y second coordinate. |
368 |
|
|
/// \param z third coordinate. |
369 |
|
|
npoint3(double x,double y,double z) |
370 |
|
|
{ |
371 |
|
|
coord[0]=x; |
372 |
|
|
coord[1]=y; // initialisation |
373 |
|
|
coord[2]=z; |
374 |
|
|
} |
375 |
|
|
|
376 |
|
|
/// \brief Initializes 3D point from a 4D point. |
377 |
|
|
/// \warning A perspective division is performed. |
378 |
|
|
/// \param[in] p 4D point. |
379 |
|
|
npoint3(npoint p) |
380 |
|
|
{ |
381 |
|
|
p.perspective_divide(); |
382 |
|
|
coord[0]=p[0]; |
383 |
|
|
coord[1]=p[1]; |
384 |
|
|
coord[2]=p[2]; |
385 |
|
|
} |
386 |
|
|
|
387 |
|
|
npoint3(const npoint2 p); |
388 |
|
|
|
389 |
|
|
/// \brief Initialization from a scalar. |
390 |
|
|
/// Coordinate x = pt. Other coordinates are set to zero. |
391 |
|
|
/// \param[in] pt scalar. |
392 |
|
|
npoint3(const double pt) |
393 |
|
|
{ |
394 |
|
|
coord[0]=pt; |
395 |
|
|
coord[1]=0.; |
396 |
|
|
coord[2]=0.; |
397 |
|
|
} |
398 |
|
|
|
399 |
|
|
/// \brief Gets the ith coordinate (mutable version). |
400 |
|
|
/// \param[in] i coordinate number. |
401 |
|
|
/// \return Coordinate. |
402 |
|
|
double & operator[](const int i) |
403 |
|
|
{ |
404 |
|
|
return coord[i]; // renvoie la ieme coordonnee (modifiable) |
405 |
|
|
} |
406 |
|
|
|
407 |
|
|
/// \brief Gets the ith coordinate (constant version). |
408 |
|
|
/// \param[in] i coordinate number. |
409 |
|
|
/// \return Coordinate. |
410 |
|
|
double operator[](const int i) const |
411 |
|
|
{ |
412 |
|
|
return coord[i]; // renvoie la ieme coordonnee (constante) |
413 |
|
|
} |
414 |
|
|
|
415 |
|
|
virtual double operator()(const int i) const |
416 |
|
|
{ |
417 |
|
|
if (i<3) |
418 |
|
|
return coord[i]; |
419 |
|
|
else return 1.0; |
420 |
|
|
} |
421 |
|
|
|
422 |
|
|
/// \brief Returns the x coordinate (mutable version). |
423 |
|
|
/// \return Coordinate. |
424 |
|
|
double & x() |
425 |
|
|
{ |
426 |
|
|
return coord[0]; // renvoie x |
427 |
|
|
} |
428 |
|
|
virtual double x() const |
429 |
|
|
{ |
430 |
|
|
return coord[0]; |
431 |
|
|
} |
432 |
|
|
|
433 |
|
|
/// \brief Returns the y coordinate (mutable version). |
434 |
|
|
/// \return Coordinate. |
435 |
|
|
double & y() |
436 |
|
|
{ |
437 |
|
|
return coord[1]; // "" y |
438 |
|
|
} |
439 |
|
|
virtual double y() const |
440 |
|
|
{ |
441 |
|
|
return coord[1]; |
442 |
|
|
} |
443 |
|
|
|
444 |
|
|
/// \brief Returns the z coordinate (mutable version). |
445 |
|
|
/// \return Coordinate. |
446 |
|
|
double & z() |
447 |
|
|
{ |
448 |
|
|
return coord[2]; // "" z |
449 |
|
|
} |
450 |
|
|
virtual double z() const |
451 |
|
|
{ |
452 |
|
|
return coord[2]; |
453 |
|
|
} |
454 |
|
|
virtual double w() const |
455 |
|
|
{ |
456 |
|
|
return 1.; |
457 |
|
|
} |
458 |
|
|
|
459 |
|
|
/// \brief Returns the data array of coordinates (mutable version). |
460 |
|
|
/// \return Data array. |
461 |
|
|
double * array() |
462 |
|
|
{ |
463 |
|
|
return coord; // "" adresse de la premiere coordonee |
464 |
|
|
} |
465 |
|
|
|
466 |
|
|
/// \brief Returns the data array of coordinates (constant version). |
467 |
|
|
/// \return Data array. |
468 |
|
|
const double * array() const |
469 |
|
|
{ |
470 |
|
|
return coord; // "" adresse de la premiere coordonee |
471 |
|
|
} |
472 |
|
|
|
473 |
|
|
/// Computes the cross product between two points \f$ this = v_1 \wedge v_2 \f$ |
474 |
|
|
/// and stores the result into this point. |
475 |
|
|
/// \param[in] V1 left point. |
476 |
|
|
/// \param[in] V2 right point. |
477 |
|
|
/// \return Cross product. |
478 |
|
|
npoint3& crossprod(const npoint3 V1,const npoint3 V2) |
479 |
|
|
{ |
480 |
|
|
coord[0]=V1[1]* V2[2]-V1[2]* V2[1]; |
481 |
|
|
coord[1]=V1[2]* V2[0]-V1[0]* V2[2]; |
482 |
|
|
coord[2]=V1[0]* V2[1]-V1[1]* V2[0]; |
483 |
|
|
return *this; |
484 |
|
|
} |
485 |
|
|
|
486 |
|
|
/// \brief Computes the dot product with another point. |
487 |
|
|
/// \param[in] other other point. |
488 |
|
|
/// \return Dot product. |
489 |
|
|
double dotprod(const npoint3 other) const |
490 |
|
|
{ |
491 |
|
|
return coord[0]*other.coord[0]+coord[1]*other.coord[1]+coord[2]*other.coord[2]; |
492 |
|
|
} |
493 |
|
|
|
494 |
|
|
/// \brief Computes the squared euclidean norm. |
495 |
|
|
/// \return Squared norm. |
496 |
|
|
double norm2() const |
497 |
|
|
{ |
498 |
|
|
return dotprod(*this); |
499 |
|
|
} |
500 |
|
|
|
501 |
|
|
/// \brief Computes the euclidean norm. |
502 |
|
|
/// \return Norm. |
503 |
|
|
double norm() const |
504 |
|
|
{ |
505 |
|
|
return sqrt(dotprod(*this)); |
506 |
|
|
} |
507 |
|
|
|
508 |
|
|
/// \brief Normalizes this point (w.r.t the euclidean norm). |
509 |
|
|
/// \return Euclidean norm. |
510 |
|
|
double normalize() |
511 |
|
|
{ |
512 |
|
|
double n=norm(); |
513 |
|
|
coord[0]/=n; |
514 |
|
|
coord[1]/=n; |
515 |
|
|
coord[2]/=n; |
516 |
|
|
return n; |
517 |
|
|
} |
518 |
|
|
|
519 |
|
|
/// \brief Substracts this point with another. This point is not modified. |
520 |
|
|
/// \param[in] other other point. |
521 |
|
|
/// \return Result. |
522 |
|
|
npoint3 operator- (npoint3 other) const // renvoie la soustraction des coords. de deux points |
523 |
|
|
{ |
524 |
|
|
for (int i=0;i<3;++i) other[i]=coord[i]-other.coord[i]; |
525 |
|
|
return other; |
526 |
|
|
} |
527 |
|
|
|
528 |
|
|
/// \brief Adds this point with another. This point is not modified. |
529 |
|
|
/// \param[in] other other point. |
530 |
|
|
/// \return Result. |
531 |
|
|
npoint3 operator+ (npoint3 other) const // "" addition "" |
532 |
|
|
{ |
533 |
|
|
for (int i=0;i<3;++i) other[i]+=coord[i]; |
534 |
|
|
return other; |
535 |
|
|
} |
536 |
|
|
|
537 |
|
|
/// \brief Divides this point by a factor. This point is not modified. |
538 |
|
|
/// \param[in] fact factor. |
539 |
|
|
/// \return Result. |
540 |
|
|
npoint3 operator/ (const double fact) const // "" division par une constante |
541 |
|
|
{ |
542 |
|
|
npoint3 buf=*this; |
543 |
|
|
for (int i=0;i<3;++i) buf[i]/=fact; |
544 |
|
|
return buf; |
545 |
|
|
} |
546 |
|
|
|
547 |
|
|
/// \brief Multiplies this point by a factor. This point is not modified. |
548 |
|
|
/// \param[in] fact factor. |
549 |
|
|
/// \return Result. |
550 |
|
|
npoint3 operator*(const double fact) const // "" multiplication (a droite) par un scalaire |
551 |
|
|
{ |
552 |
|
|
npoint3 buf=*this; |
553 |
|
|
for (int i=0;i<3;++i) buf[i]*=fact; |
554 |
|
|
return buf; |
555 |
|
|
} |
556 |
|
|
|
557 |
|
|
/// \brief Adds this point with another. This point is modified. |
558 |
|
|
/// \param[in] other other point. |
559 |
|
|
/// \return Result. |
560 |
|
|
npoint3& operator+= (const npoint3 other) // ajout "en place" |
561 |
|
|
{ |
562 |
|
|
for (int i=0;i<3;++i) coord[i]+=other.coord[i]; |
563 |
|
|
return *this; |
564 |
|
|
} |
565 |
|
|
|
566 |
|
|
/// \brief Substracts this point with another. This point is modified. |
567 |
|
|
/// \param[in] other other point. |
568 |
|
|
/// \return Result. |
569 |
|
|
npoint3& operator-= (const npoint3 other) // soustraction "en place" |
570 |
|
|
{ |
571 |
|
|
for (int i=0;i<3;++i) coord[i]-=other.coord[i]; |
572 |
|
|
return *this; |
573 |
|
|
} |
574 |
|
|
|
575 |
|
|
/// \brief Multiplies this point by a factor. This point is modified. |
576 |
|
|
/// \param[in] fact factor. |
577 |
|
|
/// \return Result. |
578 |
|
|
npoint3& operator*= (double fact) // soustraction "en place" |
579 |
|
|
{ |
580 |
|
|
for (int i=0;i<3;++i) coord[i]*=fact; |
581 |
|
|
return *this; |
582 |
|
|
} |
583 |
|
|
|
584 |
|
|
/// \brief Divides this point by a factor. This point is modified. |
585 |
|
|
/// \param[in] fact factor. |
586 |
|
|
/// \return Result. |
587 |
|
|
npoint3& operator/= (double fact) // soustraction "en place" |
588 |
|
|
{ |
589 |
|
|
for (int i=0;i<3;++i) coord[i]/=fact; |
590 |
|
|
return *this; |
591 |
|
|
} |
592 |
|
|
friend npoint3 operator * (const double fact,const npoint3 other); // "" multiplication (a gauche) par un scalaire |
593 |
|
|
|
594 |
|
|
/// \brief Print this point to output stream. |
595 |
|
|
/// \param[in,out] os output stream. |
596 |
|
|
/// \param[in] prec printing precision. |
597 |
|
|
void print(std::ostream &os,int prec=5) const; // sort les coordonees dans le flux os |
598 |
|
|
}; |
599 |
|
|
|
600 |
|
|
std::ostream& operator<<(std::ostream& stream, |
601 |
|
|
const npoint& pt); |
602 |
|
|
|
603 |
|
|
std::ostream& operator<<(std::ostream& stream, |
604 |
|
|
const npoint3& pt); |
605 |
|
|
|
606 |
|
|
npoint3 operator * (const double fact,const npoint3 other); |
607 |
|
|
|
608 |
|
|
double operator * (const npoint3 p1, const npoint3 p2); |
609 |
|
|
|
610 |
|
|
npoint3 crossprod(const npoint3 V1,const npoint3 V2); |
611 |
|
|
|
612 |
|
|
/// 2D point. |
613 |
|
|
class npoint2 : public npoint3D |
614 |
|
|
{ |
615 |
|
|
double coord[2]; //!< Array of coordinates. |
616 |
|
|
public : |
617 |
|
|
|
618 |
|
|
/// \brief Destructor. |
619 |
|
|
virtual ~npoint2() {} |
620 |
|
|
|
621 |
|
|
/// \brief default constructor. |
622 |
|
|
npoint2() |
623 |
|
|
{ |
624 |
|
|
coord[0]=coord[1]=0.; |
625 |
|
|
} |
626 |
|
|
|
627 |
|
|
/// Initializes the first coordinate by a scalar. |
628 |
|
|
/// The second coordinate is set to zero. |
629 |
|
|
/// \param[in] pt scalar. |
630 |
|
|
npoint2(double pt) |
631 |
|
|
{ |
632 |
|
|
coord[0]=pt;coord[1]=0.; // initialisation |
633 |
|
|
} |
634 |
|
|
|
635 |
|
|
/// \brief Initializes this point from a 3D point. |
636 |
|
|
/// The z coordinate is discarded. |
637 |
|
|
/// \param[in] pt 3D point. |
638 |
|
|
npoint2(const npoint3 pt) |
639 |
|
|
{ |
640 |
|
|
coord[0]=pt[0];coord[1]=pt[1]; // discard z |
641 |
|
|
} |
642 |
|
|
|
643 |
|
|
/// \brief Initializes this point from a 3D point. |
644 |
|
|
/// The z coordinate is discarded. |
645 |
|
|
/// \warning A perspective division is performed. |
646 |
|
|
/// \param[in] pt 4D point. |
647 |
|
|
npoint2(npoint pt) |
648 |
|
|
{ |
649 |
|
|
pt.perspective_divide(); |
650 |
|
|
coord[0]=pt[0];coord[1]=pt[1]; // discard z |
651 |
|
|
} |
652 |
|
|
|
653 |
|
|
/// \brief Initialization from two scalars. |
654 |
|
|
/// \param[in] u x coordinate. |
655 |
|
|
/// \param[in] v y coordinate. |
656 |
|
|
npoint2(double u,double v) |
657 |
|
|
{ |
658 |
|
|
coord[0]=u;coord[1]=v; // initialisation |
659 |
|
|
} |
660 |
|
|
|
661 |
|
|
/// \brief Gets the ith coordinate (mutable version). |
662 |
|
|
/// \param[in] i coordinate number. |
663 |
|
|
/// \return Coordinate. |
664 |
|
|
double & operator[](const int i) |
665 |
|
|
{ |
666 |
|
|
return coord[i]; // renvoie la ieme coordonnee (modifiable) |
667 |
|
|
} |
668 |
|
|
|
669 |
|
|
/// \brief Gets the ith coordinate (constant version). |
670 |
|
|
/// \param[in] i coordinate number. |
671 |
|
|
/// \return Coordinate. |
672 |
|
|
double operator[](const int i) const |
673 |
|
|
{ |
674 |
|
|
return coord[i]; // renvoie la ieme coordonnee (constante) |
675 |
|
|
} |
676 |
|
|
|
677 |
|
|
virtual double operator()(const int i) const |
678 |
|
|
{ |
679 |
|
|
if (i<2) |
680 |
|
|
return coord[i]; |
681 |
|
|
else return 1.0; |
682 |
|
|
} |
683 |
|
|
|
684 |
|
|
/// \brief Returns the u (x) coordinate (mutable version). |
685 |
|
|
/// \return Coordinate. |
686 |
|
|
double & u() |
687 |
|
|
{ |
688 |
|
|
return coord[0]; |
689 |
|
|
} |
690 |
|
|
|
691 |
|
|
/// \brief Returns the u (x) coordinate (constant version). |
692 |
|
|
/// \return Coordinate. |
693 |
|
|
double u() const |
694 |
|
|
{ |
695 |
|
|
return coord[0]; |
696 |
|
|
} |
697 |
|
|
|
698 |
|
|
/// \brief Returns the v (y) coordinate (mutable version). |
699 |
|
|
/// \return Coordinate. |
700 |
|
|
double & v() |
701 |
|
|
{ |
702 |
|
|
return coord[1]; |
703 |
|
|
} |
704 |
|
|
|
705 |
|
|
/// \brief Returns the v (y) coordinate (constant version). |
706 |
|
|
/// \return Coordinate. |
707 |
|
|
double v() const |
708 |
|
|
{ |
709 |
|
|
return coord[1]; |
710 |
|
|
} |
711 |
|
|
|
712 |
|
|
/// \brief Returns the x coordinate (constant version). |
713 |
|
|
/// \return Coordinate. |
714 |
|
|
virtual double x() const |
715 |
|
|
{ |
716 |
|
|
return coord[0]; |
717 |
|
|
} |
718 |
|
|
|
719 |
|
|
/// \brief Returns the y coordinate (constant version). |
720 |
|
|
/// \return Coordinate. |
721 |
|
|
virtual double y() const |
722 |
|
|
{ |
723 |
|
|
return coord[1]; |
724 |
|
|
} |
725 |
|
|
|
726 |
|
|
/// \brief Returns the z coordinate (constant version). |
727 |
|
|
/// \return Coordinate. |
728 |
|
|
virtual double z() const |
729 |
|
|
{ |
730 |
|
|
return 0.; |
731 |
|
|
} |
732 |
|
|
|
733 |
|
|
/// \brief Returns the weigh (constant version). |
734 |
|
|
/// For 3D points, this is always 1.0. |
735 |
|
|
/// \return Coordinate. |
736 |
|
|
virtual double w() const |
737 |
|
|
{ |
738 |
|
|
return 1.; |
739 |
|
|
} |
740 |
|
|
|
741 |
|
|
/// \brief Returns the data array of coordinates (mutable version). |
742 |
|
|
/// \return Data array. |
743 |
|
|
double * array() |
744 |
|
|
{ |
745 |
|
|
return coord; |
746 |
|
|
}; |
747 |
|
|
|
748 |
|
|
/// \brief Substracts this point with another. This point is not modified. |
749 |
|
|
/// \param[in] other other point. |
750 |
|
|
/// \return Result. |
751 |
|
|
npoint2 operator- (npoint2 other) const |
752 |
|
|
{ |
753 |
|
|
for (int i=0;i<2;++i) other.coord[i]=coord[i]-other.coord[i]; |
754 |
|
|
return other; |
755 |
|
|
} |
756 |
|
|
|
757 |
|
|
/// \brief Adds this point with another. This point is not modified. |
758 |
|
|
/// \param[in] other other point. |
759 |
|
|
/// \return Result. |
760 |
|
|
npoint2 operator+ (npoint2 other) const |
761 |
|
|
{ |
762 |
|
|
for (int i=0;i<2;++i) other.coord[i]+=coord[i]; |
763 |
|
|
return other; |
764 |
|
|
} |
765 |
|
|
|
766 |
|
|
/// \brief Divides this point by a factor. This point is not modified. |
767 |
|
|
/// \param[in] fact factor. |
768 |
|
|
/// \return Result. |
769 |
|
|
npoint2 operator/ (const double fact) const |
770 |
|
|
{ |
771 |
|
|
npoint2 buf=*this; |
772 |
|
|
for (int i=0;i<2;++i) buf[i]/=fact; |
773 |
|
|
return buf; |
774 |
|
|
} |
775 |
|
|
|
776 |
|
|
/// \brief Multiplies this point by a factor. This point is not modified. |
777 |
|
|
/// \param[in] fact factor. |
778 |
|
|
/// \return Result. |
779 |
|
|
npoint2 operator*(const double fact) const |
780 |
|
|
{ |
781 |
|
|
npoint2 buf=*this; |
782 |
|
|
for (int i=0;i<2;++i) buf[i]*=fact; |
783 |
|
|
return buf; |
784 |
|
|
} |
785 |
|
|
|
786 |
|
|
/// \brief Adds this point with another. This point is modified. |
787 |
|
|
/// \param[in] other other point. |
788 |
|
|
/// \return Result. |
789 |
|
|
npoint2& operator+= (const npoint2 other) |
790 |
|
|
{ |
791 |
|
|
for (int i=0;i<2;++i) coord[i]+=other.coord[i]; |
792 |
|
|
return *this; |
793 |
|
|
} |
794 |
|
|
|
795 |
|
|
/// \brief Substracts this point with another. This point is modified. |
796 |
|
|
/// \param[in] other other point. |
797 |
|
|
/// \return Result. |
798 |
|
|
npoint2& operator-= (const npoint2 other) |
799 |
|
|
{ |
800 |
|
|
for (int i=0;i<2;++i) coord[i]-=other.coord[i]; |
801 |
|
|
return *this; |
802 |
|
|
} |
803 |
|
|
|
804 |
|
|
/// \brief Multiplies this point by a factor. This point is modified. |
805 |
|
|
/// \param[in] fact factor. |
806 |
|
|
/// \return Result. |
807 |
|
|
npoint2& operator*= (double fact) |
808 |
|
|
{ |
809 |
|
|
for (int i=0;i<2;++i) coord[i]*=fact; |
810 |
|
|
return *this; |
811 |
|
|
} |
812 |
|
|
|
813 |
|
|
/// \brief Divides this point by a factor. This point is modified. |
814 |
|
|
/// \param[in] fact factor. |
815 |
|
|
/// \return Result. |
816 |
|
|
npoint2& operator/= (double fact) |
817 |
|
|
{ |
818 |
|
|
for (int i=0;i<2;++i) coord[i]/=fact; |
819 |
|
|
return *this; |
820 |
|
|
} |
821 |
|
|
|
822 |
|
|
/// \brief Computes the dot product with another point. |
823 |
|
|
/// \param[in] other other point. |
824 |
|
|
/// \return Dot product. |
825 |
|
|
double dotprod(const npoint2 other) const |
826 |
|
|
{ |
827 |
|
|
return coord[0]*other.coord[0]+coord[1]*other.coord[1]; |
828 |
|
|
} |
829 |
|
|
|
830 |
|
|
/// \brief Computes the squared euclidean norm. |
831 |
|
|
/// \return Squared norm. |
832 |
|
|
double norm2() const |
833 |
|
|
{ |
834 |
|
|
return dotprod(*this); |
835 |
|
|
} |
836 |
|
|
|
837 |
|
|
/// \brief Computes the euclidean norm. |
838 |
|
|
/// \return Norm. |
839 |
|
|
double norm() const |
840 |
|
|
{ |
841 |
|
|
return sqrt(dotprod(*this)); |
842 |
|
|
} |
843 |
|
|
|
844 |
|
|
/// \brief Normalizes this point (w.r.t the euclidean norm). |
845 |
|
|
/// \return Euclidean norm. |
846 |
|
|
double normalize() |
847 |
|
|
{ |
848 |
|
|
double n=norm(); |
849 |
|
|
coord[0]/=n; |
850 |
|
|
coord[1]/=n; |
851 |
|
|
return n; |
852 |
|
|
} |
853 |
|
|
}; |
854 |
|
|
|
855 |
|
|
npoint2 operator * (const double fact,const npoint2 other); |
856 |
|
|
double operator * (const npoint2 p1, const npoint2 p2); |
857 |
|
|
double crossprod(const npoint2 V1,const npoint2 V2); |
858 |
|
|
|
859 |
|
|
|
860 |
|
|
#endif // __NPOINT_H |