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