Library of Assembled Shared Sources
plane_3d_parametric.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_PARAMETRIC_INL
46#define LASS_GUARDIAN_OF_INCLUSION_PRIM_IMPL_PLANE_3D_PARAMETRIC_INL
47
48#include "../prim_common.h"
49#include "plane_3d_parametric.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{
68 LASS_ASSERT(!isValid());
69}
70
71
72
73/** Construct a plane through three points.
74 * - support point S is given by the first point iSupport.
75 * - direction vectors U and V are choosen from iSupport to iPointU and iPointV respectively
76 * (U = iPointU - iSupport, V = iPointV - iSupport).
77 */
78template<typename T, class NP>
80 const TPoint& iPointU,
81 const TPoint& iPointV):
82 support_(iSupport),
83 directionU_(iPointU - iSupport),
84 directionV_(iPointV - iSupport)
85{
86 NP::normalize(directionU_);
87 NP::normalize(directionV_);
88}
89
92/** construct a plane through a support point and by two direction vectors..
93 * - support point S is given by the point iSupport.
94 * - direction vectors U and V are given by iDirectionU and iDirectionV.
95 */
96template<typename T, class NP>
98 const TVector& iDirectionU,
99 const TVector& iDirectionV):
100 support_(iSupport),
101 directionU_(iDirectionU),
102 directionV_(iDirectionV)
104 NP::normalize(directionU_);
105 NP::normalize(directionV_);
106}
107
108
110/** Construct a plane through a support point and by a normal vector.
111 * - support point S is given by the point iSupport.
112 * - direction vectors U and V are automatically generated so that N == U x V.
113 */
114template<typename T, class NP>
115Plane3DParametric<T, NP>::Plane3DParametric(const TVector& iNormal, const TPoint& iSupport):
116 support_(iSupport)
118 Plane3DImplDetail::generateDirections(iNormal, directionU_, directionV_);
119 NP::normalize(directionU_);
120 NP::normalize(directionV_);
122
123
125/** Construct a plane by a cartesian equation N.P + d == 0.
126 * - support point S automatically generated so that N.S + d == 0.
127 * - direction vectors U and V are automatically generated so that N == U x V.
128 */
129template<typename T, class NP>
130Plane3DParametric<T, NP>::Plane3DParametric(const TVector& iNormal, TParam iD):
131 support_(iNormal * (-iD / iNormal.squaredNorm()))
132{
133 Plane3DImplDetail::generateDirections(iNormal, directionU_, directionV_);
134 NP::normalize(directionU_);
135 NP::normalize(directionV_);
136}
137
138
139
140/** return support point.
141 */
142template<typename T, class NP>
143const typename Plane3DParametric<T, NP>::TPoint& Plane3DParametric<T, NP>::support() const
144{
145 return support_;
146}
147
148
149
150/** return U and V direction vectors
151 */
152template<typename T, class NP>
153void Plane3DParametric<T, NP>::getDirections(TVector& oDirectionU, TVector& oDirectionV) const
154{
155 oDirectionU = directionU_;
156 oDirectionV = directionV_;
157}
158
159
160
161/** return U direction vector.
162 */
163template<typename T, class NP>
164const typename Plane3DParametric<T, NP>::TVector& Plane3DParametric<T, NP>::directionU() const
165{
166 return directionU_;
167}
168
169
170
171/** return V direction vector.
172 */
173template<typename T, class NP>
174const typename Plane3DParametric<T, NP>::TVector& Plane3DParametric<T, NP>::directionV() const
175{
176 return directionV_;
177}
178
179
180
181/** return reciprocal vectors for U and V direction vectors
182 */
183template<typename T, class NP>
185 TVector& oReciprocalV) const
186{
187 Plane3DImplDetail::generateReciprocal(directionU_, directionV_, oReciprocalU, oReciprocalV);
188}
189
190
191
192/** return reciprocal for U direction vector.
193 */
194template<typename T, class NP>
195const typename Plane3DParametric<T, NP>::TVector Plane3DParametric<T, NP>::reciprocalU() const
196{
197 TVector reciprocalU;
198 TVector reciprocalV;
200 return reciprocalU;
201}
202
203
204
205/** return reciprocal for V direction vector.
206 */
207template<typename T, class NP>
208const typename Plane3DParametric<T, NP>::TVector Plane3DParametric<T, NP>::reciprocalV() const
209{
210 TVector reciprocalU;
211 TVector reciprocalV;
213 return reciprocalV;
214}
215
216
217
218template<typename T, class NP>
219void Plane3DParametric<T, NP>::getCartesian(TVector& oNormal, TReference oD) const
220{
221 Plane3DImplDetail::generateCartesian(support_, directionU_, directionV_, oNormal, oD);
222 NP::normalizeAndScale(oNormal, oD);
223}
224
225
226
227template<typename T, class NP>
228const typename Plane3DParametric<T, NP>::TVector Plane3DParametric<T, NP>::normal() const
229{
230 TVector normal;
231 TValue d;
232 getCartesian(normal, d);
233 return normal;
234}
235
236
237
238template<typename T, class NP>
239const typename Plane3DParametric<T, NP>::TValue Plane3DParametric<T, NP>::d() const
240{
241 TVector normal;
242 TValue d;
243 getCartesian(normal, d);
244 return d;
245}
246
247
248
249/** Return value of point in equation.
250 */
251template<typename T, class NP>
252const typename Plane3DParametric<T, NP>::TValue
253Plane3DParametric<T, NP>::equation(const TPoint& iPoint) const
254{
255 TVector normal;
256 TValue d;
257 getCartesian(normal, d);
258 return dot(iPoint.position(), normal) + d;
259}
260
261
262
263/** Return value of point in equation.
264 */
265template<typename T, class NP>
266const typename Plane3DParametric<T, NP>::TValue
267Plane3DParametric<T, NP>::equation(const TPoint& iPoint, TParam iRelativeTolerance) const
268{
269 TVector normal;
270 TValue d;
271 getCartesian(normal, d);
272 const TValue a = dot(iPoint.position(), normal);
273 return almostEqual(a, -d, iRelativeTolerance) ? TNumTraits::zero : (a + d);
274}
275
276
277
278/** return the vector that, if added to the PROJECTION of iPoint, you get iPoint again.
279 * iPoint == (almost) project(iPoint) + reject(iPoint)
280 */
281template<typename T, class NP>
282const typename Plane3DParametric<T, NP>::TVector
283Plane3DParametric<T, NP>::reject(const TPoint& iPoint) const
284{
285 TVector normal;
286 TValue d;
287 getCartesian(normal, d);
288 return normal * NP::divideBySquaredNorm(dot(iPoint.position(), normal) + d, normal);
289}
290
291
292
293/** return the part of iVector that is orthogonal to the plane.
294 * it's the vector that, if added to the PROJECTION of iVector, you get iVector again.
295 * iVector == (almost) project(iVector) + reject(iVector).
296 */
297template<typename T, class NP>
298const typename Plane3DParametric<T, NP>::TVector
299Plane3DParametric<T, NP>::reject(const TVector& iVector) const
300{
301 TVector normal;
302 TValue d;
303 getCartesian(normal, d);
304 return normal * NP::divideBySquaredNorm(dot(iVector, normal), normal);
305}
306
307
308
309/** project a point orthogonally onto the plane
310 */
311template<typename T, class NP>
312const typename Plane3DParametric<T, NP>::TPoint
313Plane3DParametric<T, NP>::project(const TPoint& iPoint) const
314{
315 return iPoint - reject(iPoint);
316}
317
318
319
320/** project a vector orthogonally onto the plane
321 */
322template<typename T, class NP>
323const typename Plane3DParametric<T, NP>::TVector
324Plane3DParametric<T, NP>::project(const TVector& iVector) const
325{
326 return iVector - reject(iVector);
327}
328
329
330
331/** reflect a point orthogonally into the plane.
332 */
333template<typename T, class NP>
334const typename Plane3DParametric<T, NP>::TPoint
335Plane3DParametric<T, NP>::reflect(const TPoint& iPoint) const
336{
337 return iPoint - 2 * reject(iPoint);
338}
339
340
341
342/** reflect a vector orthogonally into the plane
343 */
344template<typename T, class NP>
345const typename Plane3DParametric<T, NP>::TVector
346Plane3DParametric<T, NP>::reflect(const TVector& iVector) const
347{
348 return iVector - 2 * reject(iVector);
349}
350
351
352
353/** return point by filling in the parametric equation: P(u, v) = S + u * U + v * V
354 */
355template<typename T, class NP>
356const typename Plane3DParametric<T, NP>::TPoint
357Plane3DParametric<T, NP>::point(TParam iU, TParam iV) const
358{
359 return point(TIndex(iU, iV));
360}
361
362
363
364/** return point by filling in the parametric equation: P(u, v) = S + u * U + v * V
365 */
366template<typename T, class NP>
367const typename Plane3DParametric<T, NP>::TPoint
369{
370 return support_ + iUV.x * directionU_ + iUV.y * directionV_;
371}
372
373
374
375/** return UV pair of parameters
376 */
377template<typename T, class NP>
378const typename Plane3DParametric<T, NP>::TUV
379Plane3DParametric<T, NP>::uv(const TPoint& iPoint) const
380{
381 TVector reciprocalU;
382 TVector reciprocalV;
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 Plane3DParametric<T, NP>::flip()
392{
393 directionV_ = -directionV_;
394}
395
396
397
398/** return true if plane is a valid plane (no direction vector is zero and they're not
399 * colinear.
400 */
401template<typename T, class NP>
403{
404 return !cross(directionU_, directionV_).isZero(); // zero vectors will give zero cross product too.
405}
406
407
408
409
410// --- protected -----------------------------------------------------------------------------------
411
412
413
414// --- private -------------------------------------------------------------------------------------
415
416
417
418// --- free ----------------------------------------------------------------------------------------
419
420template<typename T, class NP>
421std::ostream& operator<<(std::ostream& ioOStream, const Plane3DParametric<T, NP>& iPlane)
422{
423 LASS_ENFORCE(ioOStream) << "{S=" << iPlane.support() << ", U=" << iPlane.directionU() << ", V="
424 << iPlane.directionV() << "}";
425 return ioOStream;
426}
427
428
429
430}
431
432}
433
434}
435
436#endif
437
438// EOF
implementation of lass::prim::Plane3D with a parametric equation
const TPoint point(TParam iU, TParam iV) const
return point by filling in the parametric equation: P(u, v) = S + u * U + v * V
void getDirections(TVector &oDirectionU, TVector &oDirectionV) const
return U and V direction vectors
void getReciprocals(TVector &oReciprocalU, TVector &oReciprocalV) const
return reciprocal vectors for U and V direction vectors
const TPoint project(const TPoint &iPoint) const
project a point orthogonally onto the plane
const TVector reciprocalU() const
return reciprocal for U direction vector.
const TPoint reflect(const TPoint &iPoint) const
reflect a point orthogonally into the plane.
const TUV uv(const TPoint &iPoint) const
return UV pair of parameters
const TValue equation(const TPoint &iPoint) const
Return value of point in equation.
const TVector & directionU() const
return U direction vector.
const TPoint & support() const
return support point.
const TVector & directionV() const
return V direction vector.
Plane3DParametric()
initializes to an invalid state.
const TVector reciprocalV() const
return reciprocal for V direction vector.
bool isValid() const
return true if plane is a valid plane (no direction vector is zero and they're not colinear.
const TVector reject(const TPoint &iPoint) const
return the vector that, if added to the PROJECTION of iPoint, you get iPoint again.
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.