opennurbs_plane.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_PLANE_INC_)
18 #define ON_PLANE_INC_
19 
20 class ON_CLASS ON_Plane
21 {
22 public:
23 
24  /*
25  Description:
26  The default constructor creates a plane
27  with orgin=(0,0,0), xaxis=(1,0,0), yaxis=(0,1,0)
28  zaxis=(0,0,1), and equation=(0,0,1,0).
29  */
30  ON_Plane();
31 
32  /*
33  Description:
34  Construct a plane from a point and normal vector.
35  Parameters:
36  origin - [in] point on the plane
37  normal - [in] non-zero normal to the plane
38  Remarks:
39  origin = point, zaxis = unitized normal, xaxis
40  xaxis set with xaxis.PerpindicularTo(zaxis).
41  See Also:
42  ON_Plane::CreateFromNormal
43  */
44  ON_Plane(
45  const ON_3dPoint& origin,
46  const ON_3dVector& normal
47  );
48 
49  /*
50  Description:
51  Construct a plane from a point, and two vectors in
52  the plane.
53  Parameters:
54  origin - [in] point on the plane
55  x_dir - [in] non-zero vector in the plane that
56  determines the xaxis direction.
57  y_dir - [in] non-zero vector not parallel to x_dir
58  that is used to determine the yaxis direction.
59  y_dir does not have to be perpendicular to x_dir.
60  */
61  ON_Plane(
62  const ON_3dPoint& origin,
63  const ON_3dVector& x_dir,
64  const ON_3dVector& y_dir
65  );
66 
67  /*
68  Description:
69  Construct a plane from three non-collinear points.
70  Parameters:
71  origin - [in] point on the plane
72  x_point - [in] second point in the plane.
73  The xaxis will be parallel to x_point-origin.
74  y_point - [in] third point on the plane that is
75  not collinear with the first two points.
76  yaxis*(y_point-origin) will be > 0.
77  */
78  ON_Plane(
79  const ON_3dPoint& origin,
80  const ON_3dPoint& x_point,
81  const ON_3dPoint& y_point
82  );
83 
84  /*
85  Description:
86  Construct a plane from an equation.
87  Parameters:
88  equation - [in] an array of 4 doubles with
89  one of equation[0], equation[1], or equation[2]
90  being non-zero.
91  */
92  ON_Plane(
93  const double equation[4]
94  );
95 
96  ON_Plane(
97  const ON_PlaneEquation& plane_equation
98  );
99 
100  ~ON_Plane();
101 
102  bool operator==(const ON_Plane&) const;
103  bool operator!=(const ON_Plane&) const;
104 
105  /*
106  Description:
107  Create a plane from a point and normal vector.
108  Parameters:
109  origin - [in] point on the plane
110  normal - [in] non-zero normal to the plane
111  Remarks:
112  origin = point, zaxis = unitized normal, xaxis
113  xaxis set with xaxis.PerpindicularTo(zaxis).
114  Returns:
115  true if valid plane is created.
116  */
117  bool CreateFromNormal(
118  const ON_3dPoint& origin,
119  const ON_3dVector& normal
120  );
121 
122  /*
123  Description:
124  Construct a plane from a point, and two vectors in
125  the plane.
126  Parameters:
127  origin - [in] point on the plane
128  x_dir - [in] non-zero vector in the plane that
129  determines the xaxis direction.
130  y_dir - [in] non-zero vector not parallel to x_dir
131  that is used to determine the yaxis direction.
132  y_dir does not have to be perpendicular to x_dir.
133  Returns:
134  true if valid plane is created.
135  */
136  bool CreateFromFrame(
137  const ON_3dPoint& origin,
138  const ON_3dVector& x_dir,
139  const ON_3dVector& y_dir
140  );
141 
142  /*
143  Description:
144  Construct a plane from three non-collinear points.
145  Parameters:
146  origin - [in] point on the plane
147  point_on_x - [in] second point in the plane.
148  The xaxis will be parallel to x_point-origin.
149  point_on - [in] third point on the plane that is
150  not collinear with the first two points.
151  yaxis*(y_point-origin) will be > 0.
152  Returns:
153  true if valid plane is created.
154  */
155  bool CreateFromPoints(
156  const ON_3dPoint& origin,
157  const ON_3dPoint& point_on_x,
158  const ON_3dPoint& point_on
159  );
160 
161  /*
162  Description:
163  Construct a plane from an equation.
164  Parameters:
165  equation - [in] an array of 4 doubles with
166  one of equation[0], equation[1], or equation[2]
167  being non-zero.
168  Remarks:
169  points on the plane will satisfy
170  x*equation[0] +y*equation[1] + z*equation[2] + equation[3] = 0
171  Returns:
172  true if valid plane is created.
173  */
174  bool CreateFromEquation(
175  const double equation[4]
176  );
177 
178  bool CreateFromEquation(
179  const class ON_PlaneEquation& plane_equation
180  );
181 
182  /*
183  Returns:
184  ON_Plane::UnsetPlane if input is not valid.
185  */
186  static ON_Plane FromPointList(
187  size_t point_list_count,
188  const ON_3dPoint* point_list
189  );
190 
191  /*
192  Returns:
193  ON_Plane::UnsetPlane if input is not valid.
194  */
195  static ON_Plane FromPointList(
196  const ON_SimpleArray< ON_3dPoint >& point_list
197  );
198 
199 
200  /*
201  Returns:
202  ON_Plane::UnsetPlane if input is not valid.
203  */
204  static ON_Plane FromPointList(
205  size_t point_list_count,
206  const ON_3fPoint* point_list
207  );
208 
209  /*
210  Returns:
211  ON_Plane::UnsetPlane if input is not valid.
212  */
213  static ON_Plane FromPointList(
214  const ON_SimpleArray< ON_3fPoint >& point_list
215  );
216 
217  /*
218  Returns:
219  ON_Plane::UnsetPlane if input is not valid.
220  */
221  static ON_Plane FromPointList(
222  const class ON_3dPointListRef& point_list
223  );
224 
225  /*
226  Returns:
227  ON_Plane::UnsetPlane if input is not valid.
228  */
229  static ON_Plane FromPointList(
230  size_t point_index_count,
231  const unsigned int* point_index_list,
232  const class ON_3dPointListRef& point_list
233  );
234 
235  static ON_Plane FromPointList(
236  size_t point_index_count,
237  size_t point_index_stride,
238  const unsigned int* point_index_list,
239  const class ON_3dPointListRef& point_list
240  );
241 
242  /*
243  Description:
244  Test plane to see if it is valid.
245  Returns:
246  true if all fields contain reasonable
247  information and equation jibes with point and zaxis.
248  */
249  bool IsValid() const;
250 
251  /*
252  Returns:
253  Plane origin.
254  */
255  const ON_3dPoint& Origin() const;
256 
257  /*
258  Returns:
259  Plane unit x-axis.
260  */
261  const ON_3dVector& Xaxis() const;
262 
263  /*
264  Returns:
265  Plane unit y-axis.
266  */
267  const ON_3dVector& Yaxis() const;
268 
269  /*
270  Returns:
271  Plane unit normal.
272  */
273  const ON_3dVector& Normal() const;
274 
275 
276  /*
277  Description:
278  Set the origin and update the plane equation
279  Parameters:
280  origin - [in] the new origin
281  */
282  void SetOrigin( const ON_3dPoint& origin );
283 
284  /*
285  Description:
286  Evaluate a point on the plane
287  Parameters:
288  u - [in]
289  v - [in] evaulation parameters
290  Returns:
291  plane.origin + u*plane.xaxis + v*plane.yaxis
292  */
293  ON_3dPoint PointAt(
294  double u,
295  double v
296  ) const;
297 
298  /*
299  Description:
300  Evaluate a point on the plane
301  Parameters:
302  u - [in]
303  v - [in] evaluation parameters
304  w - [in] elevation parameter
305  Returns:
306  plane.origin + u*plane.xaxis + v*plane.yaxis + z*plane.zaxis
307  */
308  ON_3dPoint PointAt(
309  double u,
310  double v,
311  double w
312  ) const;
313 
314  /*
315  Description:
316  Get an isoparameteric line on the plane.
317  Parameters:
318  dir - [in] direction of iso-parametric line
319  0: first parameter varies and second parameter is constant
320  e.g., line(t) = plane(t,c)
321  1: first parameter is constant and second parameter varies
322  e.g., line(t) = plane(c,t)
323  c - [in] value of constant parameter
324  Returns:
325  iso-parametric line
326  */
327  ON_Line IsoLine(
328  int dir,
329  double c
330  ) const;
331 
332  /*
333  Description:
334  Get signed distance from the plane to a point.
335  Parameters:
336  point - [in]
337  Returns:
338  Signed distance from a point to a plane.
339  Remarks:
340  If the point is on the plane, the distance is 0.
341  If the point is above the plane, the distance is > 0.
342  If the point is below the plane the distance is < 0.
343  The zaxis determines the plane's orientation.
344  */
345  double DistanceTo(
346  const ON_3dPoint& point
347  ) const;
348 
349 
350  bool GetDistanceToBoundingBox(
351  //returns false if plane has zero length normal
352  const ON_BoundingBox&, // Box
353 
354  //output
355  double* min, // min signed dist from plane to box
356  double* max //max signed dist from plane to box
357  ) const;
358 
359  /*
360  Description:
361  Update the plane equation based on the current values
362  of the origin and zaxis.
363  Returns:
364  true if successful. false if zaxis is zero.
365  Remarks:
366  If you modify a plane's origin or zaxis, call UpdateEquation()
367  to set equation[].
368  */
369  bool UpdateEquation();
370 
371  /*
372  Description:
373  Get point on plane that is closest to a given point.
374  Parameters:
375  world_point - [in] 3d point
376  u - [out]
377  v - [out] The point ON_Plane::PointAt(*u,*v) is the point
378  on the plane that is closest to world_point.
379  Returns:
380  true if successful.
381  */
382  bool ClosestPointTo(
383  ON_3dPoint world_point,
384  double* u,
385  double* v
386  ) const;
387 
388  /*
389  Description:
390  Get point on plane that is closest to a given point.
391  Parameters:
392  point - [in]
393  Returns:
394  A 3d point on the plane that is closest to world_point.
395  */
396  ON_3dPoint ClosestPointTo(
397  ON_3dPoint point
398  ) const;
399 
400  // For intersections see ON_Intersect();
401 
402  /*
403  Description:
404  Transform plane.
405  Parameters:
406  xform - [in] transformation to apply to plane
407  Returns:
408  true if successful
409  */
410  bool Transform(
411  const ON_Xform& xform
412  );
413 
414 
415  /*
416  Description:
417  Transform a plane by swapping coordinates.
418  Parameters:
419  i - [in]
420  j - [in] indices of coordinates to swap.
421  0 = x coordinate, 1 = y coordinate, 2 = z coordinate.
422  Returns:
423  true if successful.
424  */
425  bool SwapCoordinates(
426  int i,
427  int j
428  );
429 
430  /*
431  Description:
432  Rotate a plane about its origin.
433  Parameters:
434  sin_angle - [in] sine of rotation angle
435  cos_angle - [in] cosine of rotation angle
436  axis - [in] axis of rotation
437  Returns:
438  true if successful
439  */
440  bool Rotate(
441  double sin_angle,
442  double cos_angle,
443  const ON_3dVector& axis
444  );
445 
446  /*
447  Description:
448  Rotate a plane about its origin.
449  Parameters:
450  angle - [in] rotation angle in radians
451  axis - [in] axis of rotation
452  Returns:
453  true if successful
454  */
455  bool Rotate(
456  double angle,
457  const ON_3dVector& axis
458  );
459 
460  /*
461  Description:
462  Rotate a plane about a point.
463  Parameters:
464  sin_angle - [in] sine of rotation angle
465  cos_angle - [in] cosine of rotation angle
466  axis - [in] axis of rotation
467  center - [in] center of rotation
468  Returns:
469  true if successful
470  */
471  bool Rotate(
472  double sin_angle,
473  double cos_angle,
474  const ON_3dVector& axis,
475  const ON_3dPoint& center
476  );
477 
478  /*
479  Description:
480  Rotate a plane about a point.
481  Parameters:
482  angle - [in] rotation angle in radians
483  axis - [in] axis of rotation
484  center - [in] center of rotation
485  Returns:
486  true if successful
487  */
488  bool Rotate(
489  double angle,
490  const ON_3dVector& axis,
491  const ON_3dPoint& center
492  );
493 
494  /*
495  Description:
496  Translate a plane.
497  Parameters:
498  delta - [in] translation vector
499  Returns:
500  true if successful
501  */
502  bool Translate(
503  const ON_3dVector& delta
504  );
505 
506  /*
507  Description:
508  Flip plane orientation by swapping x and y axes,
509  reversing the zaxis, and updating the equation.
510  Returns:
511  true if successful
512  */
513  bool Flip();
514 
515 // world plane coordinate system ON_Plane(ON_3dPoint::Origin, ON_3dVector::XAxis, ON_3dVector::YAxis);
516  const static
517  ON_Plane World_xy;
518 
519  // All values are ON_UNSET_VALUE.
520  const static
521  ON_Plane UnsetPlane;
522 
523 public:
524  // origin of plane
525  ON_3dPoint origin;
526 
527  // unit X axis of plane
528  ON_3dVector xaxis;
529 
530  // unit Y axis of plane
531  ON_3dVector yaxis;
533  // unit Z axis of plane
534  ON_3dVector zaxis;
536  // equation of plane
537  ON_PlaneEquation plane_equation;
538  //double equation[4];
539 };
540 
541 class ON_CLASS ON_ClippingPlaneInfo
542 {
543 public:
544  // C++ defaults for construction, destruction, copy construction
545  // and operator= work fine.
546 
547  // A point is visible if m_plane_equation.ValueAt(point) <= 0.
548  // (This is the opposite convention from what OpenGL uses.)
549  ON_PlaneEquation m_plane_equation;
550  ON_UUID m_plane_id;
551  bool m_bEnabled;
552 
553  void Default();
554  bool Write( ON_BinaryArchive& ) const;
555  bool Read( ON_BinaryArchive& );
556 };
557 
558 class ON_CLASS ON_ClippingPlane
559 {
560 public:
561  ON_ClippingPlane();
562  ~ON_ClippingPlane();
563 
564  void Default();
565 
566  ON_Plane m_plane;
567  ON_UuidList m_viewport_ids; //ids of viewports that this clipping plane "clips"
568  ON_UUID m_plane_id;
569  bool m_bEnabled; // true if this clipping plane is active
570 
571  ON_ClippingPlaneInfo ClippingPlaneInfo() const;
572 
573  bool Read( class ON_BinaryArchive& );
574  bool Write( class ON_BinaryArchive& ) const;
575 };
576 
577 
578 #if defined(ON_DLL_TEMPLATE)
579 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<ON_Plane>;
580 ON_DLL_TEMPLATE template class ON_CLASS ON_ClassArray<ON_ClippingPlane>;
581 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<ON_ClippingPlaneInfo>;
582 #endif
583 
584 extern ON_EXTERN_DECL const ON_Plane ON_xy_plane;
585 extern ON_EXTERN_DECL const ON_Plane ON_yz_plane;
586 extern ON_EXTERN_DECL const ON_Plane ON_zx_plane;
587 
588 /*
589 Description:
590  Get a convex hull of a set of 3d points.
591 Parameters:
592  points - [in]
593  List of points. This function can handle tens of points
594  but is too slow for hundreds of points.
595  hull -[out]
596  Equations of the sides of the convex hull are appended to
597  this list.
598  A point P is inside the hull if hull[i].ValueAt(P) <= 0 for
599  every plane equation.
600 Returns:
601  Number of equations appended to hull[] array.
602  If 0, then the points are coincident or collinear.
603  If 2, then the points are coplanar and the returned
604  planes are parallel.
605  If >= 4, then the points are in a 3d convex hull.
606 */
607 ON_DECL
608 int ON_Get3dConvexHull(
609  const ON_SimpleArray<ON_3dPoint> & points,
611  );
612 
613 #endif
Definition: opennurbs_point.h:2374
Definition: opennurbs_plane.h:507
ON_UUID is a 16 byte universally unique identifier.
Definition: opennurbs_uuid.h:32
The ON_UuidList class provides a tool to efficiently maintain a list of uuids and determine if a uuid...
Definition: opennurbs_array.h:803
Definition: opennurbs_fpoint.h:211
Definition: opennurbs_bounding_box.h:25
Definition: opennurbs_xform.h:28
Definition: opennurbs_line.h:20
Definition: opennurbs_array.h:409
Definition: opennurbs_archive.h:1783
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