Library of Assembled Shared Sources
line_3d.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_LINE_3D_INL
46#define LASS_GUARDIAN_OF_INCLUSION_PRIM_LINE_3D_INL
47
48
49
50
51#include "line_3d.h"
52
53
54
55namespace lass
56{
57
58namespace prim
59{
60
61template <typename T, class NP>
62Line3D<T, NP>::Line3D():
63 support_(),
64 direction_()
65{
66 LASS_ASSERT(support_.isZero());
67 LASS_ASSERT(direction_.isZero());
68}
69
70
71
72template <typename T, class NP>
73Line3D<T, NP>::Line3D(const TPoint& iSupport, const TVector& iDirection):
74 support_(iSupport),
75 direction_(iDirection)
76{
77 NP::normalize(direction_);
78}
79
80
81
82template <typename T, class NP>
83Line3D<T, NP>::Line3D(const TPoint& iSupport, const TPoint& iLookAt):
84 support_(iSupport),
85 direction_(iLookAt - iSupport)
86{
87 NP::normalize(direction_);
88}
89
90
91
92template <typename T, class NP> inline
93const typename Line3D<T, NP>::TPoint&
94Line3D<T, NP>::support() const
95{
96 return support_;
97}
98
99
100
101template <typename T, class NP> inline
102typename Line3D<T, NP>::TPoint&
103Line3D<T, NP>::support()
104{
105 return support_;
108
110/** Return direction of line.
111 */
112template <typename T, class NP> inline
113const typename Line3D<T, NP>::TVector&
115{
116 return direction_;
118
119
121/** Set direction and normalize it if that's the policy
122 */
123template <typename T, class NP> inline
124void Line3D<T, NP>::setDirection(const TVector& iDirection)
125{
126 direction_ = iDirection;
127 NP::normalize(direction_);
128}
129
130
131
132/** Set direction from support to look-at point.
133 */
134template <typename T, class NP>
135void Line3D<T, NP>::lookAt(const TPoint& iLookAt)
136{
137 direction_ = iLookAt - support_;
138 NP::normalize(direction_);
139}
140
141
142
143/** return the vector that, if added to the @e projection of @a iPoint, you get @a iPoint again.
144 * <tt>iPoint == (almost) project(iPoint) + reject(iPoint)</tt>
145 */
146template<typename T, class NP>
147const typename Line3D<T, NP>::TVector
148Line3D<T, NP>::reject(const TPoint& iPoint) const
149{
150 return reject(iPoint - support_);
151}
152
153
154
155/** return the part of @a iVector that is orthogonal to the line.
156 * it's the vector that, if added to the @e projection of @a iVector, you get @a iVector again.
157 * <tt>iVector == (almost) project(iVector) + reject(iVector)</tt>.
158 */
159template<typename T, class NP>
160const typename Line3D<T, NP>::TVector
161Line3D<T, NP>::reject(const TVector& iVector) const
162{
163
164 return iVector - project(iVector);
165}
166
167
168
169/** project a point orthogonally onto the line
170 */
171template<typename T, class NP>
172const typename Line3D<T, NP>::TPoint
173Line3D<T, NP>::project(const TPoint& iPoint) const
174{
175 return support_ + project(iPoint - support_);
176}
177
178
179
180/** project a vector orthogonally onto the line
181 */
182template<typename T, class NP>
183const typename Line3D<T, NP>::TVector
184Line3D<T, NP>::project(const TVector& iVector) const
185{
186 return direction_ * NP::divideBySquaredNorm(dot(iVector, direction_), direction_);
187}
188
189
190
191/** reflect a point orthogonally into the line.
192 */
193template<typename T, class NP>
194const typename Line3D<T, NP>::TPoint
195Line3D<T, NP>::reflect(const TPoint& iPoint) const
196{
197 return support_ + reflect(iPoint - support_);
198}
199
200
201
202/** reflect a vector orthogonally to the line
203 */
204template<typename T, class NP>
205const typename Line3D<T, NP>::TVector
206Line3D<T, NP>::reflect(const TVector& iVector) const
207{
208 return T(2) * project(iVector) - iVector;
209}
210
211
212
213/** Return point on line by it's parameter.
214 * @return support + t * direction
215 */
216template <typename T, class NP>
217const typename Line3D<T, NP>::TPoint
218Line3D<T, NP>::point(TParam iT) const
219{
220 return support_ + iT * direction_;
221}
222
223
224
225/** Return parameter of point on the line that is the projection of the given point.
226 */
227template <typename T, class NP>
228const typename Line3D<T, NP>::TValue
229Line3D<T, NP>::t(const TPoint& iPoint) const
230{
231 return NP::divideBySquaredNorm(dot(iPoint - support_, direction_), direction_);
232}
233
234
235
236/** Return true if line is valid (direction isn't a zero vector).
237 */
238template <typename T, class NP>
240{
241 return !direction_.isZero();
242}
243
244
245
246/** @relates lass::prim::Line3D
247 */
248template<typename T, class NP>
249std::ostream& operator<<(std::ostream& ioOStream, const Line3D<T, NP>& iLine)
250{
251 LASS_ENFORCE(ioOStream) << "{S=" << iLine.support() << ", U=" << iLine.direction() << "}";
252 return ioOStream;
253}
254
255
256
257/** @relates lass::prim::Line3D
258 */
259template<typename T, class NP>
260io::XmlOStream& operator<<(io::XmlOStream& ioOStream, const Line3D<T, NP>& iLine)
261{
262 LASS_ENFORCE_STREAM(ioOStream)
263 << "<Line3D>\n"
264 << "<support>" << iLine.support() << "</support>\n"
265 << "<direction>" << iLine.direction() << "</direction>\n"
266 << "</Line3D>\n";
267
268 return ioOStream;
269}
270
271
272
273}
274
275}
276
277#endif
278
279// EOF
Output stream for writing a selection of geometric primitives to XML files.
const TPoint reflect(const TPoint &iPoint) const
reflect a point orthogonally into the line.
Definition line_3d.inl:195
const TPoint point(TParam iT) const
Return point on line by it's parameter.
Definition line_3d.inl:218
const TVector & direction() const
Return direction of line.
Definition line_3d.inl:114
const TValue t(const TPoint &iPoint) const
Return parameter of point on the line that is the projection of the given point.
Definition line_3d.inl:229
void lookAt(const TPoint &iLookAt)
Set direction from support to look-at point.
Definition line_3d.inl:135
void setDirection(const TVector &iDirection)
Set direction and normalize it if that's the policy.
Definition line_3d.inl:124
const TPoint project(const TPoint &iPoint) const
project a point orthogonally onto the line
Definition line_3d.inl:173
const TVector reject(const TPoint &iPoint) const
return the vector that, if added to the projection of iPoint, you get iPoint again.
Definition line_3d.inl:148
bool isValid() const
Return true if line is valid (direction isn't a zero vector).
Definition line_3d.inl:239
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53