1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkPlane.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15 /**
16 * @class vtkPlane
17 * @brief perform various plane computations
18 *
19 * vtkPlane provides methods for various plane computations. These include
20 * projecting points onto a plane, evaluating the plane equation, and
21 * returning plane normal. vtkPlane is a concrete implementation of the
22 * abstract class vtkImplicitFunction.
23 */
24
25 #ifndef vtkPlane_h
26 #define vtkPlane_h
27
28 #include "vtkCommonDataModelModule.h" // For export macro
29 #include "vtkImplicitFunction.h"
30
31 class vtkPoints; // forward declaration
32
33 class VTKCOMMONDATAMODEL_EXPORT vtkPlane : public vtkImplicitFunction
34 {
35 public:
36 /**
37 * Construct plane passing through origin and normal to z-axis.
38 */
39 static vtkPlane* New();
40
41 vtkTypeMacro(vtkPlane, vtkImplicitFunction);
42 void PrintSelf(ostream& os, vtkIndent indent) override;
43
44 ///@{
45 /**
46 * Evaluate plane equation for point x[3].
47 */
48 using vtkImplicitFunction::EvaluateFunction;
49 void EvaluateFunction(vtkDataArray* input, vtkDataArray* output) override;
50 double EvaluateFunction(double x[3]) override;
51 ///@}
52
53 /**
54 * Evaluate function gradient at point x[3].
55 */
56 void EvaluateGradient(double x[3], double g[3]) override;
57
58 ///@{
59 /**
60 * Set/get plane normal. Plane is defined by point and normal.
61 */
62 vtkSetVector3Macro(Normal, double);
63 vtkGetVectorMacro(Normal, double, 3);
64 ///@}
65
66 ///@{
67 /**
68 * Set/get point through which plane passes. Plane is defined by point
69 * and normal.
70 */
71 vtkSetVector3Macro(Origin, double);
72 vtkGetVectorMacro(Origin, double, 3);
73 ///@}
74
75 /**
76 * Translate the plane in the direction of the normal by the
77 * distance specified. Negative values move the plane in the
78 * opposite direction.
79 */
80 void Push(double distance);
81
82 ///@{
83 /**
84 * Project a point x onto plane defined by origin and normal. The
85 * projected point is returned in xproj. NOTE : normal assumed to
86 * have magnitude 1.
87 */
88 static void ProjectPoint(
89 const double x[3], const double origin[3], const double normal[3], double xproj[3]);
90 void ProjectPoint(const double x[3], double xproj[3]);
91 ///@}
92
93 ///@{
94 /**
95 * Project a vector v onto plane defined by origin and normal. The
96 * projected vector is returned in vproj.
97 */
98 static void ProjectVector(
99 const double v[3], const double origin[3], const double normal[3], double vproj[3]);
100 void ProjectVector(const double v[3], double vproj[3]);
101 ///@}
102
103 ///@{
104 /**
105 * Project a point x onto plane defined by origin and normal. The
106 * projected point is returned in xproj. NOTE : normal does NOT have to
107 * have magnitude 1.
108 */
109 static void GeneralizedProjectPoint(
110 const double x[3], const double origin[3], const double normal[3], double xproj[3]);
111 void GeneralizedProjectPoint(const double x[3], double xproj[3]);
112 ///@}
113
114 /**
115 * Quick evaluation of plane equation n(x-origin)=0.
116 */
117 static double Evaluate(double normal[3], double origin[3], double x[3]);
118
119 ///@{
120 /**
121 * Return the distance of a point x to a plane defined by n(x-p0) = 0. The
122 * normal n[3] must be magnitude=1.
123 */
124 static double DistanceToPlane(double x[3], double n[3], double p0[3]);
125 double DistanceToPlane(double x[3]);
126 ///@}
127
128 ///@{
129 /**
130 * Given a line defined by the two points p1,p2; and a plane defined by the
131 * normal n and point p0, compute an intersection. The parametric
132 * coordinate along the line is returned in t, and the coordinates of
133 * intersection are returned in x. A zero is returned if the plane and line
134 * do not intersect between (0<=t<=1). If the plane and line are parallel,
135 * zero is returned and t is set to VTK_LARGE_DOUBLE.
136 */
137 static int IntersectWithLine(
138 const double p1[3], const double p2[3], double n[3], double p0[3], double& t, double x[3]);
139 int IntersectWithLine(const double p1[3], const double p2[3], double& t, double x[3]);
140 ///@}
141
142 ///@{
143 /**
144 * Given two planes, one infinite and one finite, defined by the normal n
145 * and point o (infinite plane), and the second finite plane1 defined by
146 * the three points (pOrigin,px,py), compute a line of intersection (if
147 * any). The line of intersection is defined by the return values
148 * (x0,x1). If there is no intersection, then zero is returned; otherwise
149 * non-zero. There are two variants of this method. The static function
150 * operates on the supplied function parameters; the non-static operates on
151 * this instance of vtkPlane (and its associated origin and normal).
152 */
153 static int IntersectWithFinitePlane(double n[3], double o[3], double pOrigin[3], double px[3],
154 double py[3], double x0[3], double x1[3]);
155 int IntersectWithFinitePlane(
156 double pOrigin[3], double px[3], double py[3], double x0[3], double x1[3]);
157 ///@}
158
159 ///@{
160 /**
161 * Given a set of points calculate the best-fitting origin and normal for the plane.
162 * The origin will be the centroid of the points. The normal is determined
163 * by using the covariance matrix of the points relative to the centroid.
164 * Returns true if successful. If not successful the origin will still contain
165 * the centroid and the normal will point into z-direction.
166 */
167 static bool ComputeBestFittingPlane(vtkPoints* pts, double* origin, double* normal);
168 ///@}
169
170 protected:
171 vtkPlane();
172 ~vtkPlane() override = default;
173
174 double Normal[3];
175 double Origin[3];
176
177 private:
178 vtkPlane(const vtkPlane&) = delete;
179 void operator=(const vtkPlane&) = delete;
180 };
181
182 // Generally the normal should be normalized
Evaluate(double normal[3],double origin[3],double x[3])183 inline double vtkPlane::Evaluate(double normal[3], double origin[3], double x[3])
184 {
185 return normal[0] * (x[0] - origin[0]) + normal[1] * (x[1] - origin[1]) +
186 normal[2] * (x[2] - origin[2]);
187 }
188
189 // Assumes normal is normalized
DistanceToPlane(double x[3],double n[3],double p0[3])190 inline double vtkPlane::DistanceToPlane(double x[3], double n[3], double p0[3])
191 {
192 #define vtkPlaneAbs(x) ((x) < 0 ? -(x) : (x))
193 return (vtkPlaneAbs(n[0] * (x[0] - p0[0]) + n[1] * (x[1] - p0[1]) + n[2] * (x[2] - p0[2])));
194 }
195
196 #endif
197