Library of Assembled Shared Sources
ray_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-2025 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_RAY_3D_INL
46#define LASS_GUARDIAN_OF_INCLUSION_PRIM_RAY_3D_INL
47
48
49
50
51#include "ray_3d.h"
52
53
54
55namespace lass
56{
57
58namespace prim
59{
60
61template <typename T, class NP, class PP>
62Ray3D<T, NP, PP>::Ray3D():
63 support_(),
64 direction_()
65{
66 LASS_ASSERT(support_.isZero());
67 LASS_ASSERT(direction_.isZero());
68}
69
70
71
72template <typename T, class NP, class PP>
73Ray3D<T, NP, PP>::Ray3D(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, class PP>
83Ray3D<T, NP, PP>::Ray3D(const TPoint& iSupport, const TVector& iNormalizedDirection,
84 IsAlreadyNormalized):
85 support_(iSupport),
86 direction_(iNormalizedDirection)
87{
88}
89
90
91
92template <typename T, class NP, class PP>
93Ray3D<T, NP, PP>::Ray3D(const TPoint& iSupport, const TPoint& iLookAt):
94 support_(iSupport),
95 direction_(iLookAt - iSupport)
96{
97 NP::normalize(direction_);
98}
99
100
101
102template <typename T, class NP, class PP>
103template <class NP2, class PP2>
104Ray3D<T, NP, PP>::Ray3D(const Ray3D<T, NP2, PP2>& iOther):
105 support_(iOther.support()),
106 direction_(iOther.direction())
107{
108 NP::normalize(direction_);
109}
110
111
113/** return origin of ray.
114 * the origin is also the support point of the ray.
115 */
116template <typename T, class NP, class PP>
117const typename Ray3D<T, NP, PP>::TPoint&
120 return support_;
121}
125/** access origin of ray
126 * the origin is also the support point of the ray.
127 */
128template <typename T, class NP, class PP>
129typename Ray3D<T, NP, PP>::TPoint&
131{
132 return support_;
133}
134
135
136
137/** Return direction of ray.
138 */
139template <typename T, class NP, class PP>
140const typename Ray3D<T, NP, PP>::TVector&
142{
143 return direction_;
144}
145
146
147
148/** Set direction and normalize it if that's the policy
149 */
150template <typename T, class NP, class PP>
151void Ray3D<T, NP, PP>::setDirection(const TVector& iDirection)
152{
153 direction_ = iDirection;
154 NP::normalize(direction_);
155}
156
157
158
159/** Set direction from origin to look-at point.
160 */
161template <typename T, class NP, class PP>
162void Ray3D<T, NP, PP>::lookAt(const TPoint& iLookAt)
163{
164 direction_ = iLookAt - support_;
165 NP::normalize(direction_);
166}
167
168
169
170/** Return point on ray by it's parameter.
171 * @exception throw an error if parameter is out of range t >= 0, if Bounded is
172 * used as @c ParameterPolicy.
173 * @return origin + t * direction
174 */
175template <typename T, class NP, class PP> inline
176const typename Ray3D<T, NP, PP>::TPoint
178{
179 TParameterPolicy::enforceRange(t, TNumTraits::zero);
180 return TPoint(support_.x + t * direction_.x, support_.y + t * direction_.y, support_.z + t * direction_.z);
181}
182
183
184
185/** Return parameter of point on the ray that is the projection of the given point.
186 * @warning it can return a (invalid) negative number even if you've used a bounded parameter policy.
187 */
188template <typename T, class NP, class PP> inline
189const typename Ray3D<T, NP, PP>::TValue
190Ray3D<T, NP, PP>::t(const TPoint& point) const
191{
192 //return NP::divideBySquaredNorm(dot(point - support_, direction_), direction_);
193 return NP::divideBySquaredNorm(
194 (point.x - support_.x) * direction_.x + (point.y - support_.y) * direction_.y + (point.z - support_.z) * direction_.z,
195 direction_);
196}
197
198
199
200
201/** Project vector on the axis of the ray
202 */
203template <typename T, class NP, class PP>
204const typename Ray3D<T, NP, PP>::TVector
205Ray3D<T, NP, PP>::project(const TVector& iVector) const
206{
207 return direction_ * NP::divideBySquaredNorm(dot(iVector, direction_), direction_);
208}
209
210
211
212/** Reject vector against the axis of the ray
213 */
214template <typename T, class NP, class PP>
215const typename Ray3D<T, NP, PP>::TVector
216Ray3D<T, NP, PP>::reject(const TVector& iVector) const
217{
218 return iVector - project(iVector);
219}
220
221
222
223/** Reflect vector against the axis of the ray
224 */
225template <typename T, class NP, class PP>
226const typename Ray3D<T, NP, PP>::TVector
227Ray3D<T, NP, PP>::reflect(const TVector& iVector) const
228{
229 return 2 * project(iVector) - iVector;
230}
231
232
233
234
235/** Project point on the axis of the ray
236 */
237template <typename T, class NP, class PP>
238const typename Ray3D<T, NP, PP>::TPoint
239Ray3D<T, NP, PP>::project(const TPoint& iPoint) const
240{
241 return support_ + project(iPoint - support_);
242}
243
244
245
246/** Reject point against the axis of the ray
247 */
248template <typename T, class NP, class PP>
249const typename Ray3D<T, NP, PP>::TVector
250Ray3D<T, NP, PP>::reject(const TPoint& iPoint) const
251{
252 return reject(iPoint - support_);
253}
254
255
256
257/** Reject point against the axis of the ray
258 */
259template <typename T, class NP, class PP>
260const typename Ray3D<T, NP, PP>::TPoint
261Ray3D<T, NP, PP>::reflect(const TPoint& iPoint) const
262{
263 return support_ + reflect(iPoint - support_);
264}
265
266
267
268/** Return true if ray is valid (direction isn't a zero vector).
269 */
270template <typename T, class NP, class PP>
272{
273 return !direction_.isZero();
274}
275
276
277
278/** @relates lass::prim::Ray3D
279 */
280template<typename T, class NP>
281std::ostream& operator<<(std::ostream& ioOStream, const Ray3D<T, NP>& iRay)
282{
283 LASS_ENFORCE(ioOStream) << "{S=" << iRay.support() << ", D=" << iRay.direction() << "}";
284 return ioOStream;
285}
286
287
288
289/** @relates lass::prim::Ray3D
290 */
291template<typename T, class NP>
292io::XmlOStream& operator<<(io::XmlOStream& ioOStream, const Ray3D<T, NP>& iRay)
293{
294 LASS_ENFORCE_STREAM(ioOStream)
295 << "<Ray3D>\n"
296 << "<support>" << iRay.support() << "</support>\n"
297 << "<direction>" << iRay.direction() << "</direction>\n"
298 << "</Ray3D>\n";
299 return ioOStream;
300}
301
302
303
304}
305
306}
307
308#endif
309
310// --- END OF FILE ------------------------------------------------------------------------------
Output stream for writing a selection of geometric primitives to XML files.
const TVector & direction() const
Return direction of ray.
Definition ray_3d.inl:141
void setDirection(const TVector &iDirection)
Set direction and normalize it if that's the policy.
Definition ray_3d.inl:151
const TPoint & support() const
return origin of ray.
Definition ray_3d.inl:118
void lookAt(const TPoint &iLookAt)
Set direction from origin to look-at point.
Definition ray_3d.inl:162
const TVector reject(const TVector &iVector) const
Reject vector against the axis of the ray.
Definition ray_3d.inl:216
bool isValid() const
Return true if ray is valid (direction isn't a zero vector).
Definition ray_3d.inl:271
const TVector reflect(const TVector &iVector) const
Reflect vector against the axis of the ray.
Definition ray_3d.inl:227
const TValue t(const TPoint &iPoint) const
Return parameter of point on the ray that is the projection of the given point.
Definition ray_3d.inl:190
const TPoint point(TParam a_t) const
Return point on ray by it's parameter.
Definition ray_3d.inl:177
const TVector project(const TVector &iVector) const
Project vector on the axis of the ray.
Definition ray_3d.inl:205
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53