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