opennurbs_line.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 #if !defined(ON_LINE_INC_)
18 #define ON_LINE_INC_
19 
20 class ON_CLASS ON_Line
21 {
22 public:
23 
24  static const ON_Line ZeroLine; // (ON_3dPoint::Origin, ON_3dPoint::Origin)
25  static const ON_Line UnsetLine; // (ON_3dPoint::UnsetPoint, ON_3dPoint::UnsetPoint)
26  static const ON_Line NanLine; // (ON_3dPoint::NanPoint, ON_3dPoint::NanPoint)
27 
28  // Default constructor sets from = to = ON_3dPoint::Origin
29  ON_Line();
30  ~ON_Line();
31 
32  explicit ON_Line(
33  ON_3dPoint start,
34  ON_3dPoint end
35  );
36 
37  explicit ON_Line(
38  ON_2dPoint start,
39  ON_2dPoint end
40  );
41 
42  /*
43  Returns:
44  True if from != to.
45  */
46  bool IsValid() const;
47 
48  // line[0] = start point line[1] = end point
49  ON_3dPoint& operator[](int);
50  const ON_3dPoint& operator[](int) const;
51 
52 
53  // Description:
54  // Create a line from two points.
55  // Parameters:
56  // start - [in] point at start of line segment
57  // end - [in] point at end of line segment
58  // Returns:
59  // true if start and end are distinct points.
60  bool Create(
61  const ON_3dPoint start,
62  const ON_3dPoint end
63  );
64  bool Create(
65  const ON_2dPoint start,
66  const ON_2dPoint end
67  );
68 
69  /*
70  Description:
71  Get line's 3d axis aligned bounding box.
72  Returns:
73  3d bounding box.
74  */
75  ON_BoundingBox BoundingBox() const;
76 
77  /*
78  Description:
79  Get line's 3d axis aligned bounding box or the
80  union of the input box with the object's bounding box.
81  Parameters:
82  bbox - [in/out] 3d axis aligned bounding box
83  bGrowBox - [in] (default=false)
84  If true, then the union of the input bbox and the
85  object's bounding box is returned in bbox.
86  If false, the object's bounding box is returned in bbox.
87  Returns:
88  true if object has bounding box and calculation was successful.
89  */
90  bool GetBoundingBox(
91  ON_BoundingBox& bbox,
92  int bGrowBox = false
93  ) const;
94 
95  /*
96  Description:
97  Get tight bounding box.
98  Parameters:
99  tight_bbox - [in/out] tight bounding box
100  bGrowBox -[in] (default=false)
101  If true and the input tight_bbox is valid, then returned
102  tight_bbox is the union of the input tight_bbox and the
103  line's tight bounding box.
104  xform -[in] (default=nullptr)
105  If not nullptr, the tight bounding box of the transformed
106  line is calculated. The line is not modified.
107  Returns:
108  True if a valid tight_bbox is returned.
109  */
110  bool GetTightBoundingBox(
111  ON_BoundingBox& tight_bbox,
112  bool bGrowBox = false,
113  const ON_Xform* xform = nullptr
114  ) const;
115 
116  /*
117  Description:
118  Get a plane that contains the line.
119  Parameters:
120  plane - [out] a plane that contains the line. The orgin
121  of the plane is at the start of the line. The distance
122  from the end of the line to the plane is <= tolerance.
123  If possible a plane parallel to the world xy, yz or zx
124  plane is returned.
125  tolerance - [in]
126  Returns:
127  true if a coordinate of the line's direction vector is
128  larger than tolerance.
129  */
130  bool InPlane( ON_Plane& plane, double tolerance = 0.0 ) const;
131 
132  // Returns:
133  // Length of line
134  double Length() const;
135 
136  // Returns:
137  // direction vector = line.to - line.from
138  // See Also:
139  // ON_Line::Tangent
140  ON_3dVector Direction() const;
141 
142  // Returns:
143  // Unit tangent vector.
144  // See Also:
145  // ON_Line::Direction
146  ON_3dVector Tangent() const;
147 
148  /*
149  Description:
150  Evaluate point on (infinite) line.
151  Parameters:
152  t - [in] evaluation parameter. t=0 returns line.from
153  and t=1 returns line.to.
154  Returns:
155  (1-t)*line.from + t*line.to.
156  See Also:
157  ON_Line::Direction
158  ON_Line::Tangent
159  */
160  ON_3dPoint PointAt(
161  double t
162  ) const;
163 
164  /*
165  Description:
166  Find the point on the (infinite) line that is
167  closest to the test_point.
168  Parameters:
169  test_point - [in]
170  t - [out] line.PointAt(*t) is the point on the line
171  that is closest to test_point.
172  Returns:
173  true if successful.
174  */
175  bool ClosestPointTo(
176  const ON_3dPoint& test_point,
177  double* t
178  ) const;
179 
180  /*
181  Description:
182  Find the point on the (infinite) line that is
183  closest to the test_point.
184  Parameters:
185  test_point - [in]
186  Returns:
187  The point on the line that is closest to test_point.
188  */
189  ON_3dPoint ClosestPointTo(
190  const ON_3dPoint& test_point
191  ) const;
192 
193  /*
194  Description:
195  Find the point on the (infinite) line that is
196  closest to the test_point.
197  Parameters:
198  test_point - [in]
199  Returns:
200  distance from the point on the line that is closest
201  to test_point.
202  See Also:
203  ON_3dPoint::DistanceTo
204  ON_Line::ClosestPointTo
205  */
206  double DistanceTo( ON_3dPoint test_point ) const;
207 
208 
209  /*
210  Description:
211  Finds the shortest distance between the line as a finite
212  chord and the other object.
213  Parameters:
214  P - [in]
215  L - [in] (another finite chord)
216  Returns:
217  A value d such that if Q is any point on
218  this line and P is any point on the other object,
219  then d <= Q.DistanceTo(P).
220  */
221  double MinimumDistanceTo( const ON_3dPoint& P ) const;
222  double MinimumDistanceTo( const ON_Line& L ) const;
223 
224  /*
225  Description:
226  Finds the longest distance between the line as a finite
227  chord and the other object.
228  Parameters:
229  P - [in]
230  L - [in] (another finite chord)
231  Returns:
232  A value d such that if Q is any point on this line and P is any
233  point on the other object, then d >= Q.DistanceTo(P).
234  */
235  double MaximumDistanceTo( const ON_3dPoint& P ) const;
236  double MaximumDistanceTo( const ON_Line& other ) const;
237 
238 
239  /*
240  Description:
241  Quickly determine if the shortest distance from
242  this line to the other object is greater than d.
243  Parameters:
244  d - [in] distance (> 0.0)
245  P - [in]
246  L - [in]
247  Returns:
248  True if if the shortest distance from this line
249  to the other object is greater than d.
250  */
251  bool IsFartherThan( double d, const ON_3dPoint& P ) const;
252  bool IsFartherThan( double d, const ON_Line& L ) const;
253 
254 
255  // For intersections see ON_Intersect();
256 
257  // Description:
258  // Reverse line by swapping from and to.
259  void Reverse();
260 
261  bool Transform(
262  const ON_Xform& xform
263  );
264 
265  // rotate line about a point and axis
266  bool Rotate(
267  double sin_angle,
268  double cos_angle,
269  const ON_3dVector& axis_of_rotation,
270  const ON_3dPoint& center_of_rotation
271  );
272 
273  bool Rotate(
274  double angle_in_radians,
275  const ON_3dVector& axis_of_rotation,
276  const ON_3dPoint& center_of_rotation
277  );
278 
279  bool Translate(
280  const ON_3dVector& delta
281  );
282 
283 
284 public:
285  ON_3dPoint from; // start point
286  ON_3dPoint to; // end point
287 };
288 
289 #if defined(ON_DLL_TEMPLATE)
290 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<ON_Line>;
291 #endif
292 
293 /*
294 Returns:
295  True if a and be are identical and no coordinate is a nan.
296 */
297 ON_DECL
298 bool operator==(const ON_Line& a, const ON_Line& b);
299 
300 /*
301 Returns:
302  True if a and be are not identical.
303 Remarks:
304  If a nan is involved in every coordinate compare,
305  the result will be false.
306 */
307 ON_DECL
308 bool operator!=(const ON_Line& a, const ON_Line& b);
309 
310 
311 
312 class ON_CLASS ON_Triangle
313 {
314 public:
315 
316  static const ON_Triangle ZeroTriangle; // {ON_3dPoint::Origin, ON_3dPoint::Origin, ON_3dPoint::Origin}
317  static const ON_Triangle UnsetTriangle; // {ON_3dPoint::UnsetPoint, ON_3dPoint::UnsetPoint, ON_3dPoint::UnsetPoint}
318  static const ON_Triangle NanTriangle; // {ON_3dPoint::NanPoint, ON_3dPoint::NanPoint, ON_3dPoint::NanPoint}
319 
320  ON_Triangle() = default; // Default constructor is uninitialized
321  ON_Triangle(const ON_3dPoint vertices[3]);
322  ON_Triangle(const ON_3dPoint& a, const ON_3dPoint& b, const ON_3dPoint& c);
323  ON_Triangle(double x); // Allows Triangle(0.0) ZeroTriangle
324 
325  ON_Triangle(const ON_Triangle& tri) = default;
326  ON_Triangle& operator=(const ON_Triangle& tri) = default;
327  ~ON_Triangle() = default;
328 
329  operator ON_3dPoint*();
330  operator const ON_3dPoint*() const;
331 
332  /*
333  Returns:
334  True if m_V[i].IsValid() for all i
335  */
336  bool IsValid() const;
337 
338  // Triangle[i] = Triangle.m_V[i]
339  ON_3dPoint& operator[](int);
340  const ON_3dPoint& operator[](int) const;
341 
342 
343  // Description:
344  // Create a Triangle from three points.
345  // Parameters:
346  // vertices - [in] vertices
347  void Create(const ON_3dPoint vertices[3]);
348 
349  // Description:
350  // Create a Triangle from three points.
351  // Parameters:
352  // a,b,c - [in] vertices
353  void Create(const ON_3dPoint& a, const ON_3dPoint& b, const ON_3dPoint& c);
354 
355  /*
356  Description:
357  Get Triangles 3d axis aligned bounding box.
358  Returns:
359  3d bounding box.
360  */
361  ON_BoundingBox BoundingBox() const;
362 
363  /*
364  Description:
365  Get line's 3d axis aligned bounding box or the
366  union of the input box with the object's bounding box.
367  Parameters:
368  bbox - [in/out] 3d axis aligned bounding box
369  bGrowBox - [in] (default=false)
370  If true, then the union of the input bbox and the
371  object's bounding box is returned in bbox.
372  If false, the object's bounding box is returned in bbox.
373  Returns:
374  true if object has bounding box and calculation was successful.
375  */
376  bool GetBoundingBox(
377  ON_BoundingBox& bbox,
378  int bGrowBox = false
379  ) const;
380 
381  /*
382  Description:
383  Get tight bounding box with respect to a given frame
384  Parameters:
385  tight_bbox - [in/out] tight bounding box
386  bGrowBox -[in] (default=false)
387  If true and the input tight_bbox is valid, then returned
388  tight_bbox is the union of the input tight_bbox and the
389  line's tight bounding box.
390  xform -[in] (default=nullptr)
391  If not nullptr, the tight bounding box of the transformed
392  triangle is calculated. The triangle is not modified.
393  Returns:
394  True if a valid tight_bbox is returned.
395  */
396  bool GetTightBoundingBox(
397  ON_BoundingBox& tight_bbox,
398  bool bGrowBox = false,
399  const ON_Xform* xform = nullptr
400  ) const;
401 
402  // Returns:
403  // Edge opposite m_V[i]
404  // Specifically,
405  // ON_Line( m_V[(i+1)%3 ], m_V[(i+2)%3 ] )
406  ON_Line Edge(int i) const;
407 
408  // Returns:
409  // true if Area()< tol
410  // Note:
411  // Recall Area = .5* base * height. So this degeneracy tests for
412  // a combination long enough and high enough.
413  // See Also:
414  // ON_Triangle::Area()
415  bool IsDegenerate(double tol = ON_ZERO_TOLERANCE) const;
416 
417  // Returns:
418  // Area of triangle
419  double Area() const;
420 
421 
422  // Returns:
423  // N = ( b-a) X ( c-a)
424  // where a,b,c are the verticies
425  // See Also:
426  // ON_Triangle UnitNormal()
427  ON_3dVector Normal() const;
428 
429  // Returns:
430  // Normal().Unitize()
431  // Notes:
432  // Ensure !IsDegenerate() to gaurentee that UnitNormal().Length()==1
433  // and the result is not just a bunch of noise. Can return zero vector
434  // in some degenerate cases.
435  ON_3dVector UnitNormal() const;
436 
437  // Returns:
438  // Plane containing Triangle with normal given by UnitNormal().
439  // Notes:
440  // Ensure !IsDegenerate() to gaurentee meaningful result
441  ON_PlaneEquation PlaneEquation() const;
442 
443 
444  /*
445  Description:
446  Evaluate point on triangle.
447  Parameters:
448  s1, s2 - [in] evaluation parameter.
449  Returns:
450  (1-s1-s2)* m_V[0] + s1*m_V[1] + s2*m_V[2]
451  Notes:
452  Point is in the triangle iff s1>=0, s2>=0 and s1 + s2<=1.
453  Other values produce points on the plane of the triangle.
454  */
455  ON_3dPoint PointAt(
456  double s1, double s2
457  ) const;
458 
459  /*
460  Description:
461  Find the point on the triangle that is
462  closest to the test_point.
463  Parameters:
464  test_point - [in]
465  s1, s2 - [out] PointAt( *s1, *s2) is the point on the
466  triangle closest to test_point.
467  Returns:
468  true if successful.
469  */
470  bool ClosestPointTo(
471  const ON_3dPoint& test_point,
472  double* s1, double *s2
473  ) const;
474 
475  /*
476  Description:
477  Find the point on the triangle that is
478  closest to the test_point.
479  Parameters:
480  test_point - [in]
481  Returns:
482  The point on the line that is closest to test_point.
483  */
484  ON_3dPoint ClosestPointTo(
485  const ON_3dPoint& test_point
486  ) const;
487 
488  /*
489  Description:
490  Find the point on the triangle that is
491  closest to the test_point.
492  Parameters:
493  test_point -[in]
494  Returns:
495  distance from the point on triangle that is closest
496  to test_point.
497  See Also:
498  ON_3dPoint::DistanceTo
499  ON_Line::ClosestPointTo
500  */
501  double DistanceTo(const ON_3dPoint& test_point) const;
502 
503 
504  // Description:
505  // Reverse endpoints of Edge[i].
506  void Reverse(int i);
507 
508  bool Transform(
509  const ON_Xform& xform
510  );
511 
512  // rotate line about a point and axis
513  bool Rotate(
514  double sin_angle,
515  double cos_angle,
516  const ON_3dVector& axis_of_rotation,
517  const ON_3dPoint& center_of_rotation
518  );
519 
520  bool Rotate(
521  double angle_in_radians,
522  const ON_3dVector& axis_of_rotation,
523  const ON_3dPoint& center_of_rotation
524  );
525 
526  bool Translate(
527  const ON_3dVector& delta
528  );
529 
530 
531 public:
532  ON_3dPoint m_V[3]; // verticies
533 };
534 
535 /*
536 Returns:
537 True if a and be are identical and no coordinate is a nan.
538 */
539 ON_DECL
540 bool operator==(const ON_Triangle& a, const ON_Triangle& b);
541 
542 /*
543 Returns:
544 True if a and be are not identical.
545 Remarks:
546 If a nan is involved in every coordinate compare,
547 the result will be false.
548 */
549 ON_DECL
550 bool operator!=(const ON_Triangle& a, const ON_Triangle& b);
551 
552 
553 
554 
555 #endif
static const ON_Line NanLine
Definition: opennurbs_line.h:26
Definition: opennurbs_array.h:36
Definition: opennurbs_point.h:277
Definition: opennurbs_bounding_box.h:25
Definition: opennurbs_xform.h:28
Definition: opennurbs_line.h:20
Definition: opennurbs_line.h:301
static const ON_Line ZeroLine
Definition: opennurbs_line.h:24
static const ON_Line UnsetLine
Definition: opennurbs_line.h:25
Definition: opennurbs_point.h:460
Definition: opennurbs_plane.h:20
Typically the vector portion is a unit vector and m_d = -(x*P.x + y*P.y + z*P.z) for a point P on the...
Definition: opennurbs_point.h:1433
Definition: opennurbs_point.h:1152