Library of Assembled Shared Sources
line_2d_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_LINE_2D_PARAMETRIC_INL
46#define LASS_GUARDIAN_OF_INCLUSION_PRIM_IMPL_LINE_2D_PARAMETRIC_INL
47
48#include "../prim_common.h"
49#include "line_2d_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 direction_()
66{
67 LASS_ASSERT(!isValid());
68}
69
70
71
72/** Construct a line through three points.
73 * - support point S is given by the first point iSupport.
74 * - direction vector U is choosen from iSupport to iPointU and iPointV respectively
75 * (U = iPoint - iSupport).
76 */
77template<typename T, class NP>
78Line2DParametric<T, NP>::Line2DParametric(const TPoint& iSupport, const TPoint& iPoint):
79 support_(iSupport),
80 direction_(iPoint - iSupport)
81{
82 NP::normalize(direction_);
87/** construct a line through a support point and by two direction vectors..
88 * - support point S is given by the point iSupport.
89 * - direction vector U is given by iDirection.
90 */
91template<typename T, class NP>
92Line2DParametric<T, NP>::Line2DParametric(const TPoint& iSupport, const TVector& iDirection):
93 support_(iSupport),
94 direction_(iDirection)
96 NP::normalize(direction_);
97}
101/** Construct a line through a support point and by a normal vector.
102 * - support point S is given by the point iSupport.
103 * - direction vector U is automatically generated so that iNormal is perp of U.
104 */
105template<typename T, class NP>
106Line2DParametric<T, NP>::Line2DParametric(const TVector& iNormal, const TPoint& iSupport):
107 support_(iSupport),
108 direction_(-iNormal.perp())
110 NP::normalize(direction_);
111}
112
113
114
115/** Construct a line by a cartesian equation N.P + d == 0.
116 * - support point S automatically generated so that iNormal.S + iD == 0.
117 * - direction vector U is automatically generated so that iNormal is perp of U.
118 */
119template<typename T, class NP>
120Line2DParametric<T, NP>::Line2DParametric(const TVector& iNormal, TParam iD):
121 support_(iNormal * (-iD / iNormal.squaredNorm())),
122 direction_(-iNormal.perp())
123{
124 NP::normalize(direction_);
125}
126
127
128
129/** return support point.
130 */
131template<typename T, class NP>
132const typename Line2DParametric<T, NP>::TPoint& Line2DParametric<T, NP>::support() const
133{
134 return support_;
135}
136
137
138
139/** return direction vector.
140 */
141template<typename T, class NP>
142const typename Line2DParametric<T, NP>::TVector& Line2DParametric<T, NP>::direction() const
143{
144 return direction_;
145}
146
147
148
149template<typename T, class NP>
150void Line2DParametric<T, NP>::getCartesian(TVector& oNormal, TReference oD) const
151{
152 oNormal = direction_.perp();
153 oD = -dot(oNormal, support_.position());
154}
155
156
157
158template<typename T, class NP>
159const typename Line2DParametric<T, NP>::TVector Line2DParametric<T, NP>::normal() const
160{
161 TVector normal;
162 TValue d;
163 getCartesian(normal, d);
164 return normal;
165}
166
167
168
169template<typename T, class NP>
170const typename Line2DParametric<T, NP>::TValue Line2DParametric<T, NP>::d() const
171{
172 TVector normal;
173 TValue d;
174 getCartesian(normal, d);
175 return d;
176}
177
178
179
180/** Return value of point in equation.
181 */
182template<typename T, class NP>
183const typename Line2DParametric<T, NP>::TValue
184Line2DParametric<T, NP>::equation(const TPoint& iPoint) const
185{
186 TVector normal;
187 TValue d;
188 getCartesian(normal, d);
189 return dot(iPoint.position(), normal) + d;
190}
191
192
193
194/** Return value of point in equation.
195 */
196template<typename T, class NP>
197const typename Line2DParametric<T, NP>::TValue
198Line2DParametric<T, NP>::equation(const TPoint& iPoint, TParam iRelativeTolerance) const
199{
200 TVector normal;
201 TValue d;
202 getCartesian(normal, d);
203 const TValue pn = dot(iPoint.position(), normal);
204 return num::almostEqual(pn, -d, iRelativeTolerance) ? TNumTraits::zero : (pn + d);
205}
206
207
208
209/** return the vector that, if added to the PROJECTION of iPoint, you get iPoint again.
210 * iPoint == (almost) project(iPoint) + reject(iPoint)
211 */
212template<typename T, class NP>
213const typename Line2DParametric<T, NP>::TVector
214Line2DParametric<T, NP>::reject(const TPoint& iPoint) const
215{
216 return reject(iPoint - support_);
217}
218
219
220
221/** return the part of iVector that is orthogonal to the line.
222 * it's the vector that, if added to the PROJECTION of iVector, you get iVector again.
223 * iVector == (almost) project(iVector) + reject(iVector).
224 */
225template<typename T, class NP>
226const typename Line2DParametric<T, NP>::TVector
227Line2DParametric<T, NP>::reject(const TVector& iVector) const
228{
229
230 return iVector - project(iVector);
231}
232
233
234
235/** project a point orthogonally onto the line
236 */
237template<typename T, class NP>
238const typename Line2DParametric<T, NP>::TPoint
239Line2DParametric<T, NP>::project(const TPoint& iPoint) const
240{
241 return support_ + project(iPoint - support_);
242}
243
244
245
246/** project a vector orthogonally onto the line
247 */
248template<typename T, class NP>
249const typename Line2DParametric<T, NP>::TVector
250Line2DParametric<T, NP>::project(const TVector& iVector) const
251{
252 return direction_ * NP::divideBySquaredNorm(dot(iVector, direction_), direction_);
253}
254
255
256
257/** reflect a point orthogonally into the line.
258 */
259template<typename T, class NP>
260const typename Line2DParametric<T, NP>::TPoint
261Line2DParametric<T, NP>::reflect(const TPoint& iPoint) const
262{
263 return support_ + reflect(iPoint - support_);
264}
265
266
267
268/** reflect a vector orthogonally to the line
269 */
270template<typename T, class NP>
271const typename Line2DParametric<T, NP>::TVector
272Line2DParametric<T, NP>::reflect(const TVector& iVector) const
273{
274 return T(2) * project(iVector) - iVector;
275}
276
277
278
279/** return point by filling in the parametric equation: P(t) = S + t * U
280 */
281template<typename T, class NP>
282const typename Line2DParametric<T, NP>::TPoint
284{
285 return support_ + iT * direction_;
286}
287
288
289
290/** return UV pair of parameters
291 */
292template<typename T, class NP>
293const typename Line2DParametric<T, NP>::TValue
294Line2DParametric<T, NP>::t(const TPoint& iPoint) const
295{
296 return NP::divideBySquaredNorm(dot(iPoint - support_, direction_), direction_);
297}
298
299
300
301template <typename T, class NP>
302void Line2DParametric<T, NP>::flip()
303{
304 direction_ = -direction_;
305}
306
307
308
309/** return true if line is a valid line (no normal or direction vectors that are zero).
310 */
311template<typename T, class NP>
313{
314 return !direction_.isZero();
315}
316
317
318
319
320// --- protected -----------------------------------------------------------------------------------
321
322
323
324// --- private -------------------------------------------------------------------------------------
325
326
327
328// --- free ----------------------------------------------------------------------------------------
329
330template<typename T, class NP>
331std::ostream& operator<<(std::ostream& ioOStream, const Line2DParametric<T, NP>& iLine)
332{
333 LASS_ENFORCE(ioOStream) << "{S=" << iLine.support() << ", D=" << iLine.direction() << "}";
334 return ioOStream;
335}
336
337
338
339}
340
341}
342
343}
344
345#endif
346
347// EOF
LineSegment3D< T, PP > reflect(const Plane3D< T, EP, NP > &plane, const LineSegment3D< T, PP > &lineSegment)
reflect a linesegment in a plane.
LineSegment3D< T, PP > project(const Plane3D< T, EP, NP > &plane, const LineSegment3D< T, PP > &lineSegment)
project a linesegment on a plane.
implementation of 2d line with parametric equation.
const TVector reject(const TPoint &iPoint) const
return the vector that, if added to the PROJECTION of iPoint, you get iPoint again.
const TValue equation(const TPoint &iPoint) const
Return value of point in equation.
const TValue t(const TPoint &iPoint) const
return UV pair of parameters
const TPoint & support() const
return support point.
const TPoint project(const TPoint &iPoint) const
project a point orthogonally onto the line
Line2DParametric()
initializes to an invalid state.
const TVector & direction() const
return direction vector.
const TPoint point(TParam iT) const
return point by filling in the parametric equation: P(t) = S + t * U
bool isValid() const
return true if line is a valid line (no normal or direction vectors that are zero).
const TPoint reflect(const TPoint &iPoint) const
reflect a point orthogonally into the line.
implementation details of lass::prim
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53
const Vector2D< T > perp() const
return the vector perpendicular to this one, 90° CCW (to the left).