Library of Assembled Shared Sources
plane_3d_combined.inl
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_IMPL_PLANE_3D_COMBINED_INL
46#define LASS_GUARDIAN_OF_INCLUSION_PRIM_IMPL_PLANE_3D_COMBINED_INL
47
48#include "../prim_common.h"
49#include "plane_3d_combined.h"
50
51namespace lass
52{
53namespace prim
54{
55namespace impl
56{
57
58// --- public --------------------------------------------------------------------------------------
59
60/** initializes to an invalid state.
61 */
62template<typename T, class NP>
64 support_(),
65 directionU_(),
66 directionV_(),
67 reciprocalU_(),
68 reciprocalV_(),
69 normal_(),
70 d_(TNumTraits::zero)
71{
72 LASS_ASSERT(!isValid());
73}
74
75
76
77/** Construct a plane through three points.
78 * - support point S is given by the first point iSupport.
79 * - direction vectors U and V are choosen from iSupport to iPointU and iPointV respectively
80 * (U = iPointU - iSupport, V = iPointV - iSupport).
81 * - normal vector N is given by the cross product N = U x V.
82 * - value d is choosen so that the support point is indeed a point of the plane.
83 */
84template<typename T, class NP>
86 const TPoint& iPointU,
87 const TPoint& iPointV):
88 support_(iSupport),
89 directionU_(iPointU - iSupport),
90 directionV_(iPointV - iSupport)
92 Plane3DImplDetail::generateCartesian(support_, directionU_, directionV_, normal_, d_);
93 NP::normalize(directionU_);
94 NP::normalize(directionV_);
95 NP::normalizeAndScale(normal_, d_);
96 Plane3DImplDetail::generateReciprocal(directionU_, directionV_, reciprocalU_, reciprocalV_);
99
100
101/** construct a plane through a support point and by two direction vectors..
102 * - support point S is given by the point iSupport.
103 * - direction vectors U and V are given by iDirectionU and iDirectionV.
104 * - normal vector N is given by the cross product N = U x V.
105 * - value d is choosen so that the support point is indeed a point of the plane.
106 */
107template<typename T, class NP>
109 const TVector& iDirectionU,
110 const TVector& iDirectionV):
111 support_(iSupport),
112 directionU_(iDirectionU),
113 directionV_(iDirectionV)
115 Plane3DImplDetail::generateCartesian(support_, directionU_, directionV_, normal_, d_);
116 NP::normalize(directionU_);
117 NP::normalize(directionV_);
118 NP::normalizeAndScale(normal_, d_);
119 Plane3DImplDetail::generateReciprocal(directionU_, directionV_, reciprocalU_, reciprocalV_);
120}
121
122
123
124/** Construct a plane through a support point and by a normal vector.
125 * - support point S is given by the point iSupport.
126 * - normal vector N is given by the vector iNormal.
127 * - direction vectors U and V are automatically generated so that N == U x V.
128 * - value d is choosen so that the support point is indeed a point of the plane.
129 */
130template<typename T, class NP>
131Plane3DCombined<T, NP>::Plane3DCombined(const TVector& iNormal, const TPoint& iSupport):
132 support_(iSupport),
133 normal_(iNormal),
134 d_(-dot(iNormal, iSupport.position()))
135{
136 Plane3DImplDetail::generateDirections(normal_, directionU_, directionV_);
137 NP::normalize(directionU_);
138 NP::normalize(directionV_);
139 NP::normalizeAndScale(normal_, d_);
140 Plane3DImplDetail::generateReciprocal(directionU_, directionV_, reciprocalU_, reciprocalV_);
141}
142
143
144
145/** Construct a plane by a cartesian equation N.P + d == 0.
146 * - normal vector N is given by the vector iNormal.
147 * - value d is given by the value iD.
148 * - support point S automatically generated so that N.S + d == 0.
149 * - direction vectors U and V are automatically generated so that N == U x V.
150 */
151template<typename T, class NP>
152Plane3DCombined<T, NP>::Plane3DCombined(const TVector& iNormal, TParam iD):
153 support_(iNormal * (-iD / iNormal.squaredNorm())),
154 normal_(iNormal),
155 d_(iD)
156{
157 Plane3DImplDetail::generateDirections(normal_, directionU_, directionV_);
158 NP::normalize(directionU_);
159 NP::normalize(directionV_);
160 NP::normalizeAndScale(normal_, d_);
161 Plane3DImplDetail::generateReciprocal(directionU_, directionV_, reciprocalU_, reciprocalV_);
162}
163
164
165
166/** return support point.
167 */
168template<typename T, class NP>
169const typename Plane3DCombined<T, NP>::TPoint& Plane3DCombined<T, NP>::support() const
170{
171 return support_;
172}
173
174
175
176/** return U and V direction vectors
177 */
178template<typename T, class NP>
179void Plane3DCombined<T, NP>::getDirections(TVector& oDirectionU, TVector& oDirectionV) const
180{
181 oDirectionU = directionU_;
182 oDirectionV = directionV_;
183}
184
185
186
187/** return U direction vector.
188 */
189template<typename T, class NP>
190const typename Plane3DCombined<T, NP>::TVector& Plane3DCombined<T, NP>::directionU() const
191{
192 return directionU_;
193}
194
195
196
197/** return V direction vector.
198 */
199template<typename T, class NP>
200const typename Plane3DCombined<T, NP>::TVector& Plane3DCombined<T, NP>::directionV() const
201{
202 return directionV_;
203}
204
205
206
207/** return reciprocal vectors for U and V direction vectors
208 */
209template<typename T, class NP>
210void Plane3DCombined<T, NP>::getReciprocals(TVector& oReciprocalU,
211 TVector& oReciprocalV) const
212{
213 oReciprocalU = reciprocalU_;
214 oReciprocalV = reciprocalV_;
215}
216
217
218
219/** return reciprocal for U direction vector.
220 */
221template<typename T, class NP>
222const typename Plane3DCombined<T, NP>::TVector& Plane3DCombined<T, NP>::reciprocalU() const
223{
224 return reciprocalU_;
225}
226
227
228
229/** return reciprocal for V direction vector.
230 */
231template<typename T, class NP>
232const typename Plane3DCombined<T, NP>::TVector& Plane3DCombined<T, NP>::reciprocalV() const
233{
234 return reciprocalV_;
235}
236
237
238
239template<typename T, class NP>
240void Plane3DCombined<T, NP>::getCartesian(TVector& oNormal, TReference oD) const
241{
242 oNormal = normal_;
243 oD = d_;
244}
245
246
247
248template<typename T, class NP>
249const typename Plane3DCombined<T, NP>::TVector& Plane3DCombined<T, NP>::normal() const
250{
251 return normal_;
252}
253
254
255
256template<typename T, class NP>
257const typename Plane3DCombined<T, NP>::TParam Plane3DCombined<T, NP>::d() const
258{
259 return d_;
260}
261
262
263
264/** Return value of point in equation.
265 */
266template<typename T, class NP>
267const typename Plane3DCombined<T, NP>::TValue
268Plane3DCombined<T, NP>::equation(const TPoint& iPoint) const
269{
270 return dot(iPoint.position(), normal_) + d_;
271}
272
273
274
275/** Return value of point in equation.
276 */
277template<typename T, class NP>
278const typename Plane3DCombined<T, NP>::TValue
279Plane3DCombined<T, NP>::equation(const TPoint& iPoint, TParam iRelativeTolerance) const
280{
281 const TValue a = dot(iPoint.position(), normal_);
282 return almostEqual(a, -d_, iRelativeTolerance) ? TNumTraits::zero : (a + d_);
283}
284
285
286
287/** return the vector that, if added to the PROJECTION of iPoint, you get iPoint again.
288 * iPoint == (almost) project(iPoint) + reject(iPoint)
289 */
290template<typename T, class NP>
291const typename Plane3DCombined<T, NP>::TVector
292Plane3DCombined<T, NP>::reject(const TPoint& iPoint) const
293{
294 return normal_ * NP::divideBySquaredNorm(equation(iPoint), normal_);
295}
296
297
298
299/** return the part of iVector that is orthogonal to the plane.
300 * it's the vector that, if added to the PROJECTION of iVector, you get iVector again.
301 * iVector == (almost) project(iVector) + reject(iVector).
302 */
303template<typename T, class NP>
304const typename Plane3DCombined<T, NP>::TVector
305Plane3DCombined<T, NP>::reject(const TVector& iVector) const
306{
307 return normal_ * NP::divideBySquaredNorm(dot(normal_, iVector), normal_);
308}
309
310
311
312/** project a point orthogonally onto the plane
313 */
314template<typename T, class NP>
315const typename Plane3DCombined<T, NP>::TPoint
316Plane3DCombined<T, NP>::project(const TPoint& iPoint) const
317{
318 return iPoint - reject(iPoint);
319}
320
321
322
323/** project a vector orthogonally onto the plane
324 */
325template<typename T, class NP>
326const typename Plane3DCombined<T, NP>::TVector
327Plane3DCombined<T, NP>::project(const TVector& iVector) const
328{
329 return iVector - reject(iVector);
330}
331
332
333
334/** reflect a point orthogonally into the plane.
335 */
336template<typename T, class NP>
337const typename Plane3DCombined<T, NP>::TPoint
338Plane3DCombined<T, NP>::reflect(const TPoint& iPoint) const
339{
340 return iPoint - 2 * reject(iPoint);
341}
342
343
344
345/** reflect a vector orthogonally into the plane
346 */
347template<typename T, class NP>
348const typename Plane3DCombined<T, NP>::TVector
349Plane3DCombined<T, NP>::reflect(const TVector& iVector) const
350{
351 return iVector - 2 * reject(iVector);
352}
353
354
355
356/** return point by filling in the parametric equation: P(u, v) = S + u * U + v * V
357 */
358template<typename T, class NP>
359const typename Plane3DCombined<T, NP>::TPoint
360Plane3DCombined<T, NP>::point(TParam iU, TParam iV) const
361{
362 return point(TIndex(iU, iV));
363}
364
365
366
367/** return point by filling in the parametric equation: P(u, v) = S + u * U + v * V
368 */
369template<typename T, class NP>
370const typename Plane3DCombined<T, NP>::TPoint
371Plane3DCombined<T, NP>::point(const TUV& iUV) const
372{
373 return support_ + iUV.x * directionU_ + iUV.y * directionV_;
374}
375
376
377
378/** return UV pair of parameters
379 */
380template<typename T, class NP>
381const typename Plane3DCombined<T, NP>::TUV
382Plane3DCombined<T, NP>::uv(const TPoint& iPoint) const
383{
384 const TVector relative = iPoint - support_;
385 return TUV(dot(relative, reciprocalU_), dot(relative, reciprocalV_));
386}
387
388
389
390template <typename T, class NP>
391void Plane3DCombined<T, NP>::flip()
392{
393 directionV_ = -directionV_;
394 normal_ = -normal_;
395 d_ = -d_;
396 Plane3DImplDetail::generateReciprocal(directionU_, directionV_, reciprocalU_, reciprocalV_);
397}
398
399
400
401/** return true if plane is a valid plane (no normal or direction vectors that are zero).
402 */
403template<typename T, class NP>
405{
406 return !normal_.isZero() && !cross(directionU_, directionV_).isZero();
407}
408
409
410
411
412// --- protected -----------------------------------------------------------------------------------
413
414
415
416// --- private -------------------------------------------------------------------------------------
417
418
419
420// --- free ----------------------------------------------------------------------------------------
421
422template<typename T, class NP>
423std::ostream& operator<<(std::ostream& ioOStream, const Plane3DCombined<T, NP>& iPlane)
424{
425 LASS_ENFORCE(ioOStream) << "{S=" << iPlane.support() << ", U=" << iPlane.directionU() << ", V="
426 << iPlane.directionV() << ", N=" << iPlane.normal() << ", d=" << iPlane.d() << "}";
427 return ioOStream;
428}
429
430
431
432}
433
434}
435
436}
437
438#endif
439
440// EOF
implementation of plane 3d with both cartesian and parametric equation, and plus extra reciprocal vec...
void getDirections(TVector &oDirectionU, TVector &oDirectionV) const
return U and V direction vectors
const TPoint point(TParam iU, TParam iV) const
return point by filling in the parametric equation: P(u, v) = S + u * U + v * V
const TVector reject(const TPoint &iPoint) const
return the vector that, if added to the PROJECTION of iPoint, you get iPoint again.
const TVector & directionV() const
return V direction vector.
const TVector & directionU() const
return U direction vector.
const TValue equation(const TPoint &iPoint) const
Return value of point in equation.
const TPoint project(const TPoint &iPoint) const
project a point orthogonally onto the plane
const TVector & reciprocalU() const
return reciprocal for U direction vector.
void getReciprocals(TVector &oReciprocalU, TVector &oReciprocalV) const
return reciprocal vectors for U and V direction vectors
Plane3DCombined()
initializes to an invalid state.
const TPoint & support() const
return support point.
bool isValid() const
return true if plane is a valid plane (no normal or direction vectors that are zero).
const TUV uv(const TPoint &iPoint) const
return UV pair of parameters
const TPoint reflect(const TPoint &iPoint) const
reflect a point orthogonally into the plane.
const TVector & reciprocalV() const
return reciprocal for V direction vector.
implementation details of lass::prim
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53
static void generateReciprocal(const Vector3D< T > &iDirU, const Vector3D< T > &iDirV, Vector3D< T > &oReciprocalDirU, Vector3D< T > &oReciprocalDirV)
generate reciprocal direction vectors Ur and Vr given directions U and V.
static void generateDirections(const Vector3D< T > &iNormal, Vector3D< T > &oDirU, Vector3D< T > &oDirV)
Generate directions vectors U and V of parametric equation P = S + x*U + y*V, based on the normal vec...
static void generateCartesian(const Point3D< T > &iSupport, const Vector3D< T > &iDirU, const Vector3D< T > &iDirV, Vector3D< T > &oNormal, T &oD)
Generate cartesian equation out of parametric equation.