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