opennurbs_fpoint.h
1 /* $NoKeywords: $ */
2 /*
3 //
4 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
5 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
6 // McNeel & Associates.
7 //
8 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
9 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
10 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
11 //
12 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
13 //
14 ////////////////////////////////////////////////////////////////
15 */
16 
17 ////////////////////////////////////////////////////////////////
18 //
19 // defines float precision point, vector, and array classes
20 //
21 ////////////////////////////////////////////////////////////////
22 #if !defined(ON_FPOINT_INC_)
23 #define ON_FPOINT_INC_
24 
25 class ON_Xform;
26 
27 class ON_2fPoint;
28 class ON_3fPoint;
29 class ON_4fPoint;
30 
31 class ON_2fVector;
32 class ON_3fVector;
33 
34 ////////////////////////////////////////////////////////////////
35 //
36 // ON_2fPoint
37 //
38 class ON_CLASS ON_2fPoint
39 {
40 public:
41  float x, y;
42 
43 public:
44  // x,y not initialized
45  ON_2fPoint() = default;
46  ~ON_2fPoint() = default;
47  ON_2fPoint(const ON_2fPoint&) = default;
48  ON_2fPoint& operator=(const ON_2fPoint&) = default;
49 
50 public:
51  static const ON_2fPoint Origin; // (0.0f,0.0f)
52  static const ON_2fPoint NanPoint; // (ON_FLT_QNAN,ON_FLT_QNAN)
53 
54 public:
55  explicit ON_2fPoint(float x,float y);
56 
57  /*
58  Description:
59  A well ordered dictionary compare function that is nan aware and can
60  be used for robust sorting.
61  */
62  static int Compare(
63  const ON_2fPoint& lhs,
64  const ON_2fPoint& rhs
65  );
66 
67  explicit ON_2fPoint(const ON_3fPoint& ); // from 3f point
68  explicit ON_2fPoint(const ON_4fPoint& ); // from 4f point
69  explicit ON_2fPoint(const ON_2fVector& ); // from 2f vector
70  explicit ON_2fPoint(const ON_3fVector& ); // from 3f vector
71  explicit ON_2fPoint(const float*); // from float[2] array
72 
73  explicit ON_2fPoint(const ON_2dPoint& ); // from 2d point
74  explicit ON_2fPoint(const ON_3dPoint& ); // from 3d point
75  explicit ON_2fPoint(const ON_4dPoint& ); // from 4d point
76  explicit ON_2fPoint(const ON_2dVector& ); // from 2d vector
77  explicit ON_2fPoint(const ON_3dVector& ); // from 3d vector
78  explicit ON_2fPoint(const double*); // from double[2] array
79 
80  // (float*) conversion operators
81  operator float*();
82  operator const float*() const;
83 
84  // use implicit operator=(const ON_2fPoint&)
85  ON_2fPoint& operator=(const ON_3fPoint&);
86  ON_2fPoint& operator=(const ON_4fPoint&);
87  ON_2fPoint& operator=(const ON_2fVector&);
88  ON_2fPoint& operator=(const ON_3fVector&);
89  ON_2fPoint& operator=(const float*); // point = float[2] support
90 
91  ON_2fPoint& operator=(const ON_2dPoint&);
92  ON_2fPoint& operator=(const ON_3dPoint&);
93  ON_2fPoint& operator=(const ON_4dPoint&);
94  ON_2fPoint& operator=(const ON_2dVector&);
95  ON_2fPoint& operator=(const ON_3dVector&);
96  ON_2fPoint& operator=(const double*); // point = double[2] support
97 
98  ON_2fPoint& operator*=(float);
99  ON_2fPoint& operator/=(float);
100  ON_2fPoint& operator+=(const ON_2fVector&);
101  ON_2fPoint& operator-=(const ON_2fVector&);
102 
103  ON_2fPoint operator*(int) const;
104  ON_2fPoint operator/(int) const;
105  ON_2fPoint operator*(float) const;
106  ON_2fPoint operator/(float) const;
107  ON_2dPoint operator*(double) const;
108  ON_2dPoint operator/(double) const;
109 
110  ON_2fPoint operator+(const ON_2fPoint&) const;
111  ON_2fPoint operator+(const ON_2fVector&) const;
112  ON_2fVector operator-(const ON_2fPoint&) const;
113  ON_2fPoint operator-(const ON_2fVector&) const;
114  ON_3fPoint operator+(const ON_3fPoint&) const;
115  ON_3fPoint operator+(const ON_3fVector&) const;
116  ON_3fVector operator-(const ON_3fPoint&) const;
117  ON_3fPoint operator-(const ON_3fVector&) const;
118 
119  ON_2dPoint operator+(const ON_2dPoint&) const;
120  ON_2dPoint operator+(const ON_2dVector&) const;
121  ON_2dVector operator-(const ON_2dPoint&) const;
122  ON_2dPoint operator-(const ON_2dVector&) const;
123  ON_3dPoint operator+(const ON_3dPoint&) const;
124  ON_3dPoint operator+(const ON_3dVector&) const;
125  ON_3dVector operator-(const ON_3dPoint&) const;
126  ON_3dPoint operator-(const ON_3dVector&) const;
127 
128  float operator*(const ON_2fPoint&) const; // for points acting as vectors
129  float operator*(const ON_2fVector&) const; // for points acting as vectors
130 
131  bool operator==(const ON_2fPoint&) const;
132  bool operator!=(const ON_2fPoint&) const;
133 
134  // dictionary order comparisons
135  bool operator<=(const ON_2fPoint&) const;
136  bool operator>=(const ON_2fPoint&) const;
137  bool operator<(const ON_2fPoint&) const;
138  bool operator>(const ON_2fPoint&) const;
139 
140  // index operators mimic float[2] behavior
141  float& operator[](int);
142  float operator[](int) const;
143  float& operator[](unsigned int);
144  float operator[](unsigned int) const;
145 
146  /*
147  Returns:
148  False if any coordinate is ON_UNSET_FLOAT, ON_UNSET_POSITIVE_FLOAT, nan, or infinite.
149  True, otherwise.
150  */
151  bool IsValid() const;
152 
153  /*
154  Returns:
155  True if any coordinate is ON_UNSET_FLOAT or ON_UNSET_POSITIVE_FLOAT
156  */
157  bool IsUnset() const;
158 
159  // set 2d point value
160  void Set(float,float);
161 
162  double DistanceTo( const ON_2fPoint& ) const;
163 
164  int MaximumCoordinateIndex() const;
165  double MaximumCoordinate() const; // absolute value of maximum coordinate
166 
167  ON_DEPRECATED_MSG("Use p = ON_2fPoint::Origin;")
168  void Zero(); // set all coordinates to zero;
169 
170  /*
171  Returns:
172  true if all coordinates are not zero and no coordinates are nans.
173  false otherwise.
174  */
175  bool IsZero() const;
176 
177  /*
178  Returns:
179  true if at least one coordinate is not zero and no coordinates are nans.
180  */
181  bool IsNotZero() const;
182 
183  // These transform the point in place. The transformation matrix acts on
184  // the left of the point; i.e., result = transformation*point
185  void Transform(
186  const ON_Xform&
187  );
188 
189  void Rotate( // rotatation in XY plane
190  double, // angle in radians
191  const ON_2fPoint& // center of rotation
192  );
193 
194  void Rotate( // rotatation in XY plane
195  double, // sin(angle)
196  double, // cos(angle)
197  const ON_2fPoint& // center of rotation
198  );
199 
200  ON__UINT32 DataCRC(ON__UINT32 current_remainder) const;
201 };
202 
203 ON_DECL
204 ON_2fPoint operator*(int, const ON_2fPoint&);
205 
206 ON_DECL
207 ON_2fPoint operator*(float, const ON_2fPoint&);
208 
209 ON_DECL
210 ON_2dPoint operator*(double, const ON_2fPoint&);
212 ////////////////////////////////////////////////////////////////
213 //
214 // ON_3fPoint
215 //
216 class ON_CLASS ON_3fPoint
217 {
218 public:
219  float x, y, z;
220 
221 public:
222  // x,y,z not initialized
223  ON_3fPoint() = default;
224  ~ON_3fPoint() = default;
225  ON_3fPoint(const ON_3fPoint&) = default;
226  ON_3fPoint& operator=(const ON_3fPoint&) = default;
227 
228 public:
229  static const ON_3fPoint Origin; // (0.0f,0.0f,0.0f)
230  static const ON_3fPoint NanPoint; // (ON_FLT_QNAN,ON_FLT_QNAN,ON_FLT_QNAN)
231 
232  /*
233  Description:
234  A well ordered dictionary compare function that is nan aware and can
235  be used for robust sorting.
236  */
237  static int Compare(
238  const ON_3fPoint& lhs,
239  const ON_3fPoint& rhs
240  );
241 
242  explicit ON_3fPoint(float x,float y,float z);
243  explicit ON_3fPoint(const ON_2fPoint& ); // from 2f point
244  explicit ON_3fPoint(const ON_4fPoint& ); // from 4f point
245  explicit ON_3fPoint(const ON_2fVector& ); // from 2f vector
246  explicit ON_3fPoint(const ON_3fVector& ); // from 3f vector
247  explicit ON_3fPoint(const float*); // from float[3] array
248 
249  explicit ON_3fPoint(const ON_2dPoint& ); // from 2d point
250  explicit ON_3fPoint(const ON_3dPoint& ); // from 3d point
251  explicit ON_3fPoint(const ON_4dPoint& ); // from 4d point
252  explicit ON_3fPoint(const ON_2dVector& ); // from 2d vector
253  explicit ON_3fPoint(const ON_3dVector& ); // from 3d vector
254  explicit ON_3fPoint(const double*); // from double[3] array
255 
256  // (float*) conversion operators
257  operator float*();
258  operator const float*() const;
259 
260  // use implicit operator=(const ON_3fPoint&)
261  ON_3fPoint& operator=(const ON_2fPoint&);
262  ON_3fPoint& operator=(const ON_4fPoint&);
263  ON_3fPoint& operator=(const ON_2fVector&);
264  ON_3fPoint& operator=(const ON_3fVector&);
265  ON_3fPoint& operator=(const float*); // point = float[3] support
266 
267  ON_3fPoint& operator=(const ON_2dPoint&);
268  ON_3fPoint& operator=(const ON_3dPoint&);
269  ON_3fPoint& operator=(const ON_4dPoint&);
270  ON_3fPoint& operator=(const ON_2dVector&);
271  ON_3fPoint& operator=(const ON_3dVector&);
272  ON_3fPoint& operator=(const double*); // point = double[3] support
273 
274  ON_3fPoint& operator*=(float);
275  ON_3fPoint& operator/=(float);
276  ON_3fPoint& operator+=(const ON_3fVector&);
277  ON_3fPoint& operator-=(const ON_3fVector&);
278 
279  ON_3fPoint operator*(int) const;
280  ON_3fPoint operator/(int) const;
281  ON_3fPoint operator*(float) const;
282  ON_3fPoint operator/(float) const;
283  ON_3dPoint operator*(double) const;
284  ON_3dPoint operator/(double) const;
285 
286  ON_3fPoint operator+(const ON_3fPoint&) const;
287  ON_3fPoint operator+(const ON_3fVector&) const;
288  ON_3fVector operator-(const ON_3fPoint&) const;
289  ON_3fPoint operator-(const ON_3fVector&) const;
290  ON_3fPoint operator+(const ON_2fPoint&) const;
291  ON_3fPoint operator+(const ON_2fVector&) const;
292  ON_3fVector operator-(const ON_2fPoint&) const;
293  ON_3fPoint operator-(const ON_2fVector&) const;
294 
295  ON_3dPoint operator+(const ON_3dPoint&) const;
296  ON_3dPoint operator+(const ON_3dVector&) const;
297  ON_3dVector operator-(const ON_3dPoint&) const;
298  ON_3dPoint operator-(const ON_3dVector&) const;
299  ON_3dPoint operator+(const ON_2dPoint&) const;
300  ON_3dPoint operator+(const ON_2dVector&) const;
301  ON_3dVector operator-(const ON_2dPoint&) const;
302  ON_3dPoint operator-(const ON_2dVector&) const;
303 
304  float operator*(const ON_3fPoint&) const; // for points acting as vectors
305  float operator*(const ON_3fVector&) const; // for points acting as vectors
306 
307  bool operator==(const ON_3fPoint&) const;
308  bool operator!=(const ON_3fPoint&) const;
309 
310  // dictionary order comparisons
311  bool operator<=(const ON_3fPoint&) const;
312  bool operator>=(const ON_3fPoint&) const;
313  bool operator<(const ON_3fPoint&) const;
314  bool operator>(const ON_3fPoint&) const;
315 
316  // index operators mimic float[3] behavior
317  float& operator[](int);
318  float operator[](int) const;
319  float& operator[](unsigned int);
320  float operator[](unsigned int) const;
321 
322  /*
323  Returns:
324  False if any coordinate is ON_UNSET_FLOAT, ON_UNSET_POSITIVE_FLOAT, nan, or infinite.
325  True, otherwise.
326  */
327  bool IsValid() const;
328 
329  /*
330  Returns:
331  True if any coordinate is ON_UNSET_FLOAT or ON_UNSET_POSITIVE_FLOAT
332  */
333  bool IsUnset() const;
334 
335  // set 3d point value
336  void Set(float,float,float);
337 
338  double DistanceTo( const ON_3fPoint& ) const;
339 
340  int MaximumCoordinateIndex() const;
341  double MaximumCoordinate() const; // absolute value of maximum coordinate
342  double Fuzz( double = ON_ZERO_TOLERANCE ) const; // tolerance to use when comparing 3d points
343 
344  ON_DEPRECATED_MSG("Use p = ON_3fPoint::Origin;")
345  void Zero(); // set all coordinates to zero;
346 
347  /*
348  Returns:
349  true if all coordinates are not zero and no coordinates are nans.
350  false otherwise.
351  */
352  bool IsZero() const;
353 
354  /*
355  Returns:
356  true if at lease one coordinate is not zero and no coordinates are nans.
357  */
358  bool IsNotZero() const;
359 
360  // These transform the point in place. The transformation matrix acts on
361  // the left of the point; i.e., result = transformation*point
362  void Transform(
363  const ON_Xform&
364  );
365 
366  void Rotate(
367  double, // angle in radians
368  const ON_3fVector&, // axis of rotation
369  const ON_3fPoint& // center of rotation
370  );
371 
372  void Rotate(
373  double, // sin(angle)
374  double, // cos(angle)
375  const ON_3fVector&, // axis of rotation
376  const ON_3fPoint& // center of rotation
377  );
378 
379  ON__UINT32 DataCRC(ON__UINT32 current_remainder) const;
380 };
381 
382 ON_DECL
383 ON_3fPoint operator*(int, const ON_3fPoint&);
384 
385 ON_DECL
386 ON_3fPoint operator*(float, const ON_3fPoint&);
387 
388 ON_DECL
389 ON_3dPoint operator*(double, const ON_3fPoint&);
390 
391 ////////////////////////////////////////////////////////////////
392 //
393 // ON_4fPoint (homogeneous coordinates)
394 //
395 class ON_CLASS ON_4fPoint
396 {
397 public:
398  float x, y, z, w;
399 
400  /*
401  Returns:
402  ON_UNSET_VALUE, if x or w is ON_UNSET_VALUE or ON_UNSET_POSITIVE_VALUE
403  and neither x nor w is a nan.
404  x/w, otherwise
405  Remarks:
406  If w is 0.0 or nan, the result will be a nan.
407  */
408  float EuclideanX() const;
409 
410  /*
411  Returns:
412  ON_UNSET_VALUE, if y or w is ON_UNSET_VALUE or ON_UNSET_POSITIVE_VALUE
413  and neither y nor w is a nan.
414  y/w, otherwise
415  Remarks:
416  If w is 0.0 or nan, the result will be a nan.
417  */
418  float EuclideanY() const;
419 
420  /*
421  Returns:
422  ON_UNSET_VALUE, if z or w is ON_UNSET_VALUE or ON_UNSET_POSITIVE_VALUE
423  and neither z nor w is a nan.
424  z/w, otherwise
425  Remarks:
426  If w is 0.0 or nan, the result will be a nan.
427  */
428  float EuclideanZ() const;
430 public:
431  // x,y,z,w not initialized
432  ON_4fPoint() = default;
433  ~ON_4fPoint() = default;
434  ON_4fPoint(const ON_4fPoint&) = default;
435  ON_4fPoint& operator=(const ON_4fPoint&) = default;
436 
437 public:
438  static const ON_4fPoint Zero; // (0,0,0,0)
439  static const ON_4fPoint Nan; // (ON_FLT_QNAN,ON_FLT_QNAN,ON_FLT_QNAN,ON_FLT_QNAN)
440 
441  /*
442  Description:
443  A well ordered projective compare function that is nan aware and can
444  be used for robust sorting.
445  Remarks:
446  float c = non-nan value.
447  ON_4fPoint h0 = ...;
448  ON_4fPoint h1(c*h0.x,c*h0.x,c*h0.x,c*h0.x);
449  0 == ON_4fPoint::ProjectiveCompare(h0,ha);
450  */
451  static int ProjectiveCompare(
452  const ON_4fPoint& lhs,
453  const ON_4fPoint& rhs
454  );
455 
456  /*
457  Description:
458  A well ordered dictionary compare function that is nan aware and can
459  be used for robust sorting.
460  */
461  static int DictionaryCompare(
462  const ON_4fPoint& lhs,
463  const ON_4fPoint& rhs
464  );
465 
466  /*
467  Returns:
468  True if (lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.w == rhs.w).
469  */
470  bool operator==(const ON_4fPoint& rhs) const;
471 
472  /*
473  Returns:
474  True if lhs.* != rhs.* for some coordinate and no values are nans.
475  */
476  bool operator!=(const ON_4fPoint& rhs) const;
477 
478  explicit ON_4fPoint(float x,float y,float z,float w);
479 
480  ON_4fPoint(const ON_2fPoint& ); // from 2f point
481  ON_4fPoint(const ON_3fPoint& ); // from 3f point
482  ON_4fPoint(const ON_2fVector& ); // from 2f vector
483  ON_4fPoint(const ON_3fVector& ); // from 3f vector
484 
485  // Require explicit construction when dev must insure array has length >= 4.
486  explicit ON_4fPoint(const float*); // from float[4] array
487 
488  // Require explicit construction when loosing precision
489  explicit ON_4fPoint(const ON_2dPoint& ); // from 2d point
490  explicit ON_4fPoint(const ON_3dPoint& ); // from 3d point
491  explicit ON_4fPoint(const ON_4dPoint& ); // from 4d point
492  explicit ON_4fPoint(const ON_2dVector& ); // from 2d vector
493  explicit ON_4fPoint(const ON_3dVector& ); // from 3d vector
494  explicit ON_4fPoint(const double*); // from double[4] array
495 
496  // (float*) conversion operators
497  operator float*();
498  operator const float*() const;
499 
500  // use implicit operator=(const ON_4fPoint&)
501  ON_4fPoint& operator=(const ON_2fPoint&);
502  ON_4fPoint& operator=(const ON_3fPoint&);
503  ON_4fPoint& operator=(const ON_2fVector&);
504  ON_4fPoint& operator=(const ON_3fVector&);
505  ON_4fPoint& operator=(const float*); // point = float[4] support
506 
507  ON_4fPoint& operator=(const ON_2dPoint&);
508  ON_4fPoint& operator=(const ON_3dPoint&);
509  ON_4fPoint& operator=(const ON_4dPoint&);
510  ON_4fPoint& operator=(const ON_2dVector&);
511  ON_4fPoint& operator=(const ON_3dVector&);
512  ON_4fPoint& operator=(const double*); // point = double[4] support
513 
514  ON_4fPoint& operator*=(float);
515  ON_4fPoint& operator/=(float);
516  ON_4fPoint& operator+=(const ON_4fPoint&);
517  ON_4fPoint& operator-=(const ON_4fPoint&);
518 
519  ON_4fPoint operator*(float) const;
520  ON_4fPoint operator/(float) const;
521  ON_4fPoint operator+(const ON_4fPoint&) const; // sum w = sqrt(w1*w2)
522  ON_4fPoint operator-(const ON_4fPoint&) const; // difference w = sqrt(w1*w2)
523 
524 public:
525  // index operators mimic float[4] behavior
526  float& operator[](int);
527  float operator[](int) const;
528  float& operator[](unsigned int);
529  float operator[](unsigned int) const;
530 
531  /*
532  Returns:
533  False if any coordinate is ON_UNSET_FLOAT, ON_UNSET_POSITIVE_FLOAT, nan, or infinite.
534  True, otherwise.
535  */
536  bool IsValid() const;
537 
538  /*
539  Returns:
540  True if any coordinate is ON_UNSET_FLOAT or ON_UNSET_POSITIVE_FLOAT
541  */
542  bool IsUnset() const;
543 
544  // set 4d point value
545  void Set(float,float,float,float);
546 
547  int MaximumCoordinateIndex() const;
548  double MaximumCoordinate() const; // absolute value of maximum coordinate
549 
550  bool Normalize(); // set so x^2 + y^2 + z^2 + w^2 = 1
551 
552  // These transform the point in place. The transformation matrix acts on
553  // the left of the point; i.e., result = transformation*point
554  void Transform(
555  const ON_Xform&
556  );
557 
558  ON__UINT32 DataCRC(ON__UINT32 current_remainder) const;
559 };
560 
561 ON_DECL
562 ON_4fPoint operator*(float, const ON_4fPoint&);
563 
564 ON_DECL
565 ON_4dPoint operator*(double, const ON_4fPoint&);
566 
567 ////////////////////////////////////////////////////////////////
568 //
569 // ON_2fVector
570 //
571 class ON_CLASS ON_2fVector
572 {
573 public:
574  float x, y;
575 
576 public:
577  // x,y not initialized
578  ON_2fVector() = default;
579  ~ON_2fVector() = default;
580  ON_2fVector(const ON_2fVector&) = default;
581  ON_2fVector& operator=(const ON_2fVector&) = default;
582 
583 public:
584  static const ON_2fVector NanVector; // (ON_FLT_QNAN,ON_FLT_QNAN)
585  static const ON_2fVector ZeroVector; // (0.0f,0.0f)
586  static const ON_2fVector XAxis; // (1.0f,0.0f)
587  static const ON_2fVector YAxis; // (0.0f,1.0f)
588 
589  /*
590  Description:
591  A well ordered dictionary compare function that is nan aware and can
592  be used for robust sorting.
593  */
594  static int Compare(
595  const ON_2fVector& lhs,
596  const ON_2fVector& rhs
597  );
598 
599  // Description:
600  // A index driven function to get unit axis vectors.
601  // Parameters:
602  // index - [in] 0 returns (1,0), 1 returns (0,1)
603  // Returns:
604  // Unit 3d vector with vector[i] = (i==index)?1:0;
605  static const ON_2fVector& UnitVector(
606  int // index
607  );
608 
609  explicit ON_2fVector(float x,float y);
610  explicit ON_2fVector(const ON_2fPoint& ); // from 2f point
611  explicit ON_2fVector(const ON_3fPoint& ); // from 3f point
612  explicit ON_2fVector(const ON_3fVector& ); // from 3f vector
613  explicit ON_2fVector(const float*); // from float[2] array
614 
615  explicit ON_2fVector(const ON_2dPoint& ); // from 2d point
616  explicit ON_2fVector(const ON_3dPoint& ); // from 3d point
617  explicit ON_2fVector(const ON_2dVector& ); // from 2d vector
618  explicit ON_2fVector(const ON_3dVector& ); // from 3d vector
619  explicit ON_2fVector(const double*); // from double[2] array
620 
621  // (float*) conversion operators
622  operator float*();
623  operator const float*() const;
624 
625  // use implicit operator=(const ON_2fVector&)
626  ON_2fVector& operator=(const ON_2fPoint&);
627  ON_2fVector& operator=(const ON_3fPoint&);
628  ON_2fVector& operator=(const ON_3fVector&);
629  ON_2fVector& operator=(const float*); // point = float[2] support
630 
631  ON_2fVector& operator=(const ON_2dPoint&);
632  ON_2fVector& operator=(const ON_3dPoint&);
633  ON_2fVector& operator=(const ON_2dVector&);
634  ON_2fVector& operator=(const ON_3dVector&);
635  ON_2fVector& operator=(const double*); // point = double[2] support
636 
637  ON_2fVector operator-() const;
638 
639  ON_2fVector& operator*=(float);
640  ON_2fVector& operator/=(float);
641  ON_2fVector& operator+=(const ON_2fVector&);
642  ON_2fVector& operator-=(const ON_2fVector&);
643 
644  float operator*(const ON_2fVector&) const; // inner (dot) product
645  float operator*(const ON_2fPoint&) const; // inner (dot) product point acting as a vector
646  double operator*(const ON_2dVector&) const; // inner (dot) product
647 
648  ON_2fVector operator*(int) const;
649  ON_2fVector operator/(int) const;
650  ON_2fVector operator*(float) const;
651  ON_2fVector operator/(float) const;
652  ON_2dVector operator*(double) const;
653  ON_2dVector operator/(double) const;
654 
655  ON_2fVector operator+(const ON_2fVector&) const;
656  ON_2fPoint operator+(const ON_2fPoint&) const;
657  ON_2fVector operator-(const ON_2fVector&) const;
658  ON_2fPoint operator-(const ON_2fPoint&) const;
659  ON_3fVector operator+(const ON_3fVector&) const;
660  ON_3fPoint operator+(const ON_3fPoint&) const;
661  ON_3fVector operator-(const ON_3fVector&) const;
662  ON_3fPoint operator-(const ON_3fPoint&) const;
663 
664  ON_2dVector operator+(const ON_2dVector&) const;
665  ON_2dPoint operator+(const ON_2dPoint&) const;
666  ON_2dVector operator-(const ON_2dVector&) const;
667  ON_2dPoint operator-(const ON_2dPoint&) const;
668  ON_3dVector operator+(const ON_3dVector&) const;
669  ON_3dPoint operator+(const ON_3dPoint&) const;
670  ON_3dVector operator-(const ON_3dVector&) const;
671  ON_3dPoint operator-(const ON_3dPoint&) const;
672 
673  bool operator==(const ON_2fVector&) const;
674  bool operator!=(const ON_2fVector&) const;
675 
676  // dictionary order comparisons
677  bool operator<=(const ON_2fVector&) const;
678  bool operator>=(const ON_2fVector&) const;
679  bool operator<(const ON_2fVector&) const;
680  bool operator>(const ON_2fVector&) const;
681 
682  // index operators mimic float[2] behavior
683  float& operator[](int);
684  float operator[](int) const;
685  float& operator[](unsigned int);
686  float operator[](unsigned int) const;
687 
688  /*
689  Returns:
690  False if any coordinate is ON_UNSET_FLOAT, ON_UNSET_POSITIVE_FLOAT, nan, or infinite.
691  True, otherwise.
692  */
693  bool IsValid() const;
694 
695  /*
696  Returns:
697  True if any coordinate is ON_UNSET_FLOAT or ON_UNSET_POSITIVE_FLOAT
698  */
699  bool IsUnset() const;
700 
701  // set 2d vector value
702  void Set(float,float);
703 
704  int MaximumCoordinateIndex() const;
705  double MaximumCoordinate() const; // absolute value of maximum coordinate
706 
707  double LengthSquared() const;
708  double Length() const;
709 
710  bool Decompose( // Computes a, b such that this vector = a*X + b*Y
711  // Returns false if unable to solve for a,b. This happens
712  // when X,Y is not really a basis.
713  //
714  // If X,Y is known to be an orthonormal frame,
715  // then a = V*X, b = V*Y will compute
716  // the same result more quickly.
717  const ON_2fVector&, // X
718  const ON_2fVector&, // Y
719  double*, // a
720  double* // b
721  ) const;
722 
723  int IsParallelTo(
724  // returns 1: this and other vectors are parallel
725  // -1: this and other vectors are anti-parallel
726  // 0: this and other vectors are not parallel
727  // or at least one of the vectors is zero
728  const ON_2fVector&, // other vector
729  double = ON_DEFAULT_ANGLE_TOLERANCE // optional angle tolerance (radians)
730  ) const;
731 
732  bool IsPerpendicularTo(
733  // returns true: this and other vectors are perpendicular
734  // false: this and other vectors are not perpendicular
735  // or at least one of the vectors is zero
736  const ON_2fVector&, // other vector
737  double = ON_DEFAULT_ANGLE_TOLERANCE // optional angle tolerance (radians)
738  ) const;
739 
740  ON_DEPRECATED_MSG("Use p = ON_2fVector::ZeroVector;")
741  void Zero(); // set all coordinates to zero;
742 
743  ON_DEPRECATED_MSG("Use v = -v;")
744  void Reverse(); // negate all coordinates
745 
746  bool Unitize(); // returns false if vector has zero length
747 
748  bool IsUnitVector() const;
749 
750  /*
751  Returns:
752  If this is a valid non-zero vector, a unit vector parallel to this is returned.
753  Otherwise the zero vector is returned.
754  */
755  ON_2fVector UnitVector() const;
756 
757  // Description:
758  // Test a vector to see if it is very short
759  //
760  // Parameters:
761  // tiny_tol - [in] (default = ON_ZERO_TOLERANCE) a nonzero
762  // value used as the coordinate zero tolerance.
763  //
764  // Returns:
765  // ( fabs(x) <= tiny_tol && fabs(y) <= tiny_tol )
766  //
767  bool IsTiny(
768  double = ON_ZERO_TOLERANCE // tiny_tol
769  ) const;
770 
771  // Returns:
772  // true if vector is the zero vector.
773  bool IsZero() const;
774 
775  /*
776  Returns:
777  true if at lease one coordinate is not zero and no coordinates are nans.
778  */
779  bool IsNotZero() const;
780 
781  // set this vector to be perpendicular to another vector
782  bool PerpendicularTo( // Result is not unitized.
783  // returns false if input vector is zero
784  const ON_2fVector&
785  );
786 
787  // set this vector to be perpendicular to a line defined by 2 points
788  bool PerpendicularTo(
789  const ON_2fPoint&,
790  const ON_2fPoint&
791  );
792 
793  ON__UINT32 DataCRC(ON__UINT32 current_remainder) const;
794 };
795 
796 ON_DECL
797 ON_2fVector operator*(int, const ON_2fVector&);
798 
799 ON_DECL
800 ON_2fVector operator*(float, const ON_2fVector&);
801 
802 ON_DECL
803 ON_2dVector operator*(double, const ON_2fVector&);
804 
805 ///////////////////////////////////////////////////////////////
806 //
807 // ON_2fVector utilities
808 //
809 
810 ON_DECL
811 float
812 ON_DotProduct(
813  const ON_2fVector&,
814  const ON_2fVector&
815  );
816 
817 ON_DECL
819 ON_CrossProduct(
820  const ON_2fVector&,
821  const ON_2fVector&
822  );
823 
824 ON_DECL
825 bool
826 ON_IsOrthogonalFrame( // true if X, Y are nonzero and mutually perpendicular
827  const ON_2fVector&, // X
828  const ON_2fVector& // Y
829  );
830 
831 ON_DECL
832 bool
833 ON_IsOrthonormalFrame( // true if X, Y are orthogonal and unit length
834  const ON_2fVector&, // X
835  const ON_2fVector& // Y
836  );
837 
838 ON_DECL
839 bool
840 ON_IsRightHandFrame( // true if X, Y are orthonormal and right handed
841  const ON_2fVector&, // X
842  const ON_2fVector& // Y
843  );
844 
845 ////////////////////////////////////////////////////////////////
846 //
847 // ON_3fVector
848 //
849 class ON_CLASS ON_3fVector
850 {
851 public:
852  float x, y, z;
853 
854 public:
855  // x,y,z not initialized
856  ON_3fVector() = default;
857  ~ON_3fVector() = default;
858  ON_3fVector(const ON_3fVector&) = default;
859  ON_3fVector& operator=(const ON_3fVector&) = default;
860 
861 public:
862  static const ON_3fVector NanVector; // (ON_FLT_QNAN,ON_FLT_QNAN,ON_FLT_QNAN)
863  static const ON_3fVector ZeroVector; // (0.0f,0.0f,0.0f)
864  static const ON_3fVector XAxis; // (1.0f,0.0f,0.0f)
865  static const ON_3fVector YAxis; // (0.0f,1.0f,0.0f)
866  static const ON_3fVector ZAxis; // (0.0f,0.0f,1.0f)
867 
868  /*
869  Description:
870  A well ordered dictionary compare function that is nan aware and can
871  be used for robust sorting.
872  */
873  static int Compare(
874  const ON_3fVector& lhs,
875  const ON_3fVector& rhs
876  );
877 
878  // Description:
879  // A index driven function to get unit axis vectors.
880  // Parameters:
881  // index - [in] 0 returns (1,0,0), 1 returns (0,1,0)
882  // 2 returns (0,0,1)
883  // Returns:
884  // Unit 3d vector with vector[i] = (i==index)?1:0;
885  static const ON_3fVector& UnitVector(
886  int // index
887  );
888 
889  explicit ON_3fVector(float x,float y,float z);
890 
891  explicit ON_3fVector(const ON_2fPoint& ); // from 2f point
892  explicit ON_3fVector(const ON_3fPoint& ); // from 3f point
893  explicit ON_3fVector(const ON_2fVector& ); // from 2f vector
894  explicit ON_3fVector(const float*); // from float[3] array
895 
896  explicit ON_3fVector(const ON_2dPoint& ); // from 2d point
897  explicit ON_3fVector(const ON_3dPoint& ); // from 3d point
898  explicit ON_3fVector(const ON_2dVector& ); // from 2d vector
899  explicit ON_3fVector(const ON_3dVector& ); // from 3d vector
900  explicit ON_3fVector(const double*); // from double[3] array
901 
902  // (float*) conversion operators
903  operator float*();
904  operator const float*() const;
905 
906  // use implicit operator=(const ON_3fVector&)
907  ON_3fVector& operator=(const ON_2fPoint&);
908  ON_3fVector& operator=(const ON_3fPoint&);
909  ON_3fVector& operator=(const ON_2fVector&);
910  ON_3fVector& operator=(const float*); // point = float[3] support
911 
912  ON_3fVector& operator=(const ON_2dPoint&);
913  ON_3fVector& operator=(const ON_3dPoint&);
914  ON_3fVector& operator=(const ON_2dVector&);
915  ON_3fVector& operator=(const ON_3dVector&);
916  ON_3fVector& operator=(const double*); // point = double[3] support
917 
918  ON_3fVector operator-() const;
919 
920  ON_3fVector& operator*=(float);
921  ON_3fVector& operator/=(float);
922  ON_3fVector& operator+=(const ON_3fVector&);
923  ON_3fVector& operator-=(const ON_3fVector&);
924 
925  float operator*(const ON_3fVector&) const; // inner (dot) product
926  float operator*(const ON_3fPoint&) const; // inner (dot) product (point acting as a vector)
927  double operator*(const ON_3dVector&) const; // inner (dot) product
928 
929  ON_3fVector operator*(int) const;
930  ON_3fVector operator/(int) const;
931  ON_3fVector operator*(float) const;
932  ON_3fVector operator/(float) const;
933  ON_3dVector operator*(double) const;
934  ON_3dVector operator/(double) const;
935 
936  ON_3fVector operator+(const ON_3fVector&) const;
937  ON_3fPoint operator+(const ON_3fPoint&) const;
938  ON_3fVector operator-(const ON_3fVector&) const;
939  ON_3fPoint operator-(const ON_3fPoint&) const;
940  ON_3fVector operator+(const ON_2fVector&) const;
941  ON_3fPoint operator+(const ON_2fPoint&) const;
942  ON_3fVector operator-(const ON_2fVector&) const;
943  ON_3fPoint operator-(const ON_2fPoint&) const;
944 
945  ON_3dVector operator+(const ON_3dVector&) const;
946  ON_3dPoint operator+(const ON_3dPoint&) const;
947  ON_3dVector operator-(const ON_3dVector&) const;
948  ON_3dPoint operator-(const ON_3dPoint&) const;
949  ON_3dVector operator+(const ON_2dVector&) const;
950  ON_3dPoint operator+(const ON_2dPoint&) const;
951  ON_3dVector operator-(const ON_2dVector&) const;
952  ON_3dPoint operator-(const ON_2dPoint&) const;
953 
954  bool operator==(const ON_3fVector&) const;
955  bool operator!=(const ON_3fVector&) const;
956 
957  // dictionary order comparisons
958  bool operator<=(const ON_3fVector&) const;
959  bool operator>=(const ON_3fVector&) const;
960  bool operator<(const ON_3fVector&) const;
961  bool operator>(const ON_3fVector&) const;
962 
963  // index operators mimic float[3] behavior
964  float& operator[](int);
965  float operator[](int) const;
966  float& operator[](unsigned int);
967  float operator[](unsigned int) const;
968 
969  /*
970  Returns:
971  False if any coordinate is ON_UNSET_FLOAT, ON_UNSET_POSITIVE_FLOAT, nan, or infinite.
972  True, otherwise.
973  */
974  bool IsValid() const;
975 
976  /*
977  Returns:
978  True if any coordinate is ON_UNSET_FLOAT or ON_UNSET_POSITIVE_FLOAT
979  */
980  bool IsUnset() const;
981 
982  // set 3d vector value
983  void Set(float,float,float);
984 
985  int MaximumCoordinateIndex() const;
986  double MaximumCoordinate() const; // absolute value of maximum coordinate
987 
988  double LengthSquared() const;
989  double Length() const;
990 
991  bool IsPerpendicularTo(
992  // returns true: this and other vectors are perpendicular
993  // false: this and other vectors are not perpendicular
994  // or at least one of the vectors is zero
995  const ON_3fVector&, // other vector
996  double = ON_DEFAULT_ANGLE_TOLERANCE // optional angle tolerance (radians)
997  ) const;
998 
999  double Fuzz( double = ON_ZERO_TOLERANCE ) const; // tolerance to use when comparing 3d vectors
1000 
1001  ON_DEPRECATED_MSG("Use p = ON_3fVector::ZeroVector;")
1002  void Zero(); // set all coordinates to zero
1003 
1004  ON_DEPRECATED_MSG("Use v = -v;")
1005  void Reverse(); // negate all coordinates
1006 
1007  bool Unitize(); // returns false if vector has zero length
1008 
1009  bool IsUnitVector() const;
1010 
1011  /*
1012  Returns:
1013  If this is a valid non-zero vector, a unit vector parallel to this is returned.
1014  Otherwise the zero vector is returned.
1015  */
1016  ON_3fVector UnitVector() const;
1017 
1018 
1019  // Description:
1020  // Test a vector to see if it is very short
1021  //
1022  // Parameters:
1023  // tiny_tol - [in] (default = ON_ZERO_TOLERANCE) a nonzero
1024  // value used as the coordinate zero tolerance.
1025  //
1026  // Returns:
1027  // ( fabs(x) <= tiny_tol && fabs(y) <= tiny_tol && fabs(z) <= tiny_tol )
1028  //
1029  bool IsTiny(
1030  double = ON_ZERO_TOLERANCE // tiny_tol
1031  ) const;
1032 
1033  // Returns:
1034  // true if vector is the zero vector.
1035  bool IsZero() const;
1036 
1037  /*
1038  Returns:
1039  true if at lease one coordinate is not zero and no coordinates are nans.
1040  */
1041  bool IsNotZero() const;
1042 
1043  // set this vector to be perpendicular to another vector
1044  bool PerpendicularTo( // Result is not unitized.
1045  // returns false if input vector is zero
1046  const ON_3fVector&
1047  );
1048 
1049  // These transform the vector in place. The transformation matrix acts on
1050  // the left of the vector; i.e., result = transformation*vector
1051  void Transform(
1052  const ON_Xform& // can use ON_Xform here
1053  );
1054 
1055  void Rotate(
1056  double, // angle in radians
1057  const ON_3fVector& // axis of rotation
1058  );
1059 
1060  void Rotate(
1061  double, // sin(angle)
1062  double, // cos(angle)
1063  const ON_3fVector& // axis of rotation
1064  );
1065 
1066  ON__UINT32 DataCRC(ON__UINT32 current_remainder) const;
1067 };
1068 
1069 ON_DECL
1070 ON_3fVector operator*(int, const ON_3fVector&);
1071 
1072 ON_DECL
1073 ON_3fVector operator*(float, const ON_3fVector&);
1074 
1075 ON_DECL
1076 ON_3dVector operator*(double, const ON_3fVector&);
1077 
1078 ///////////////////////////////////////////////////////////////
1079 //
1080 // ON_3fVector utilities
1081 //
1082 
1083 ON_DECL
1084 float
1085 ON_DotProduct(
1086  const ON_3fVector&,
1087  const ON_3fVector&
1088  );
1089 
1090 
1091 ON_DECL
1092 ON_3fVector
1093 ON_CrossProduct(
1094  const ON_3fVector&,
1095  const ON_3fVector&
1096  );
1097 
1098 ON_DECL
1099 ON_3fVector
1100 ON_CrossProduct( // 3d cross product for old fashioned arrays
1101  const float*, // array of 3d floats
1102  const float* // array of 3d floats
1103  );
1104 
1105 ON_DECL
1106 float
1107 ON_TripleProduct(
1108  const ON_3fVector&,
1109  const ON_3fVector&,
1110  const ON_3fVector&
1111  );
1112 
1113 ON_DECL
1114 float
1115 ON_TripleProduct( // 3d triple product for old fashioned arrays
1116  const float*, // array of 3d floats
1117  const float*, // array of 3d floats
1118  const float* // array of 3d floats
1119  );
1120 
1121 ON_DECL
1122 bool
1123 ON_IsOrthogonalFrame( // true if X, Y, Z are nonzero and mutually perpendicular
1124  const ON_3fVector&, // X
1125  const ON_3fVector&, // Y
1126  const ON_3fVector& // Z
1127  );
1128 
1129 ON_DECL
1130 bool
1131 ON_IsOrthonormalFrame( // true if X, Y, Z are orthogonal and unit length
1132  const ON_3fVector&, // X
1133  const ON_3fVector&, // Y
1134  const ON_3fVector& // Z
1135  );
1136 
1137 ON_DECL
1138 bool
1139 ON_IsRightHandFrame( // true if X, Y, Z are orthonormal and right handed
1140  const ON_3fVector&, // X
1141  const ON_3fVector&, // Y
1142  const ON_3fVector& // Z
1143  );
1144 
1145 #endif
Definition: opennurbs_fpoint.h:556
Definition: opennurbs_fpoint.h:832
static const ON_2fPoint Origin
Definition: opennurbs_fpoint.h:51
Definition: opennurbs_fpoint.h:211
Definition: opennurbs_point.h:277
float y
Definition: opennurbs_fpoint.h:41
static const ON_2fPoint NanPoint
Definition: opennurbs_fpoint.h:52
Definition: opennurbs_point.h:648
Definition: opennurbs_xform.h:28
Definition: opennurbs_fpoint.h:38
Definition: opennurbs_point.h:460
Definition: opennurbs_fpoint.h:385
Definition: opennurbs_point.h:839
Definition: opennurbs_point.h:1152