Library of Assembled Shared Sources
point_2dh.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#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_POINT_2DH_INL
44#define LASS_GUARDIAN_OF_INCLUSION_PRIM_POINT_2DH_INL
45
46#include "point_2dh.h"
47
48namespace lass
49{
50namespace prim
51{
52
53template <typename T> inline
54Point2DH<T>::Point2DH():
55 x(TNumTraits::zero),
56 y(TNumTraits::zero),
57 z(TNumTraits::zero)
58{
59 LASS_ASSERT(isZero());
60}
61
62
63
64template<typename T> inline
65Point2DH<T>::Point2DH(TParam iX, TParam iY, TParam iZ):
66 x(iX),
67 y(iY),
68 z(iZ)
69{
70}
71
72
73
74template<typename T> inline
75Point2DH<T>::Point2DH(const TPoint& iAffinePoint):
76 x(iAffinePoint.x),
77 y(iAffinePoint.y),
78 z(TNumTraits::one)
79{
80}
81
82
83
84template<typename T> inline
85Point2DH<T>::Point2DH(const TVector& iPositionVector):
86 x(iPositionVector.x),
87 y(iPositionVector.y),
88 z(iPositionVector.z)
89{
90}
91
92
93
94template <typename T> inline
95const typename Point2DH<T>::TVector
96Point2DH<T>::position() const
98 return TVector(x, y, z);
100
101
102
103template<typename T> inline
104typename Point2DH<T>::TConstReference
105Point2DH<T>::operator[](size_t iIndex) const
106{
107 LASS_ASSERT(iIndex < dimension);
108 return *(&x + iIndex);
113template<typename T> inline
114typename Point2DH<T>::TReference
115Point2DH<T>::operator[](size_t iIndex)
116{
117 LASS_ASSERT(iIndex < dimension);
118 return *(&x + iIndex);
119}
120
121
122
123/** Wrap index around range.
124 */
125template<typename T> inline
126typename Point2DH<T>::TConstReference
127Point2DH<T>::at(signed iIndex) const
128{
129 return *(&x + num::mod(iIndex, dimension));
130}
131
132
133
134/** Wrap index around range.
135 */
136template<typename T> inline
137typename Point2DH<T>::TReference
138Point2DH<T>::at(signed iIndex)
139{
140 return *(&x + num::mod(iIndex, dimension));
141}
142
143
144
145/** A weird way to get back the same object
146 */
147template<typename T> inline
148const Point2DH<T>& Point2DH<T>::operator+() const
149{
150 return *this;
151}
152
153
154
155template<typename T>
156const Point2DH<T> Point2DH<T>::operator-() const
157{
158 return Poin2DH(-x, -y, -z);
159}
160
161
162
163template<typename T>
164Point2DH<T>& Point2DH<T>::operator+=(const Point2DH<T>& iB)
165{
166 x += iB.x;
167 y += iB.y;
168 z += iB.z;
169 return *this;
170}
171
172
173
174template<typename T>
175Point2DH<T>& Point2DH<T>::operator-=(const Point2DH<T>& iB)
176{
177 x -= iB.x;
178 y -= iB.y;
179 z -= iB.z;
180 return *this;
181}
182
183
184
185template<typename T>
186Point2DH<T>& Point2DH<T>::operator*=(TParam iB)
187{
188 x *= iB;
189 y *= iB;
190 z *= iB;
191 return *this;
192}
193
194
195
196template<typename T>
197Point2DH<T>& Point2DH<T>::operator/=(TParam iB)
198{
199 const TValue invB = TNumTraits::one / iB;
200 x *= invB;
201 y *= invB;
202 z *= invB;
203 return *this;
204}
205
206
207
208/** Return true if point is origin (0, 0, z).
209 * z may be 0 but doesn't has to be.
210 */
211template<typename T>
213{
214 return x == TNumTraits::zero && y == TNumTraits::zero;
215}
216
217
218
219/** Return true if at least one of the components is NaN
220 */
221template<typename T> inline
223{
224 return num::isNaN(x) || num::isNaN(y) || num::isNaN(z);
225}
226
227
228
229/** Return true if point is at infinite distance of origin. test if z == 0.
230 */
231template<typename T>
233{
234 return z == TNumTraits::zero;
235}
236
237
238
239/** Return true if point is valid. test if point != (0, 0, 0)
240 */
241template<typename T>
243{
244 return x != TNumTraits::zero || y != TNumTraits::zero || z != TNumTraits::zero;
245}
246
247
248
249/** Return weight of point. weight = z.
250 */
251template<typename T> inline
252const typename Point2DH<T>::TValue Point2DH<T>::weight() const
253{
254 return z;
255}
256
257
258
259/** Return rescaled version of point with weight = 1.
260 * Does not influence original poitn.
261 */
262template<typename T> inline
263const Point2D<T> Point2DH<T>::affine() const
264{
265 Point2DH<T> result(*this);
266 result.homogenize();
267 return Point2D<T>(result.x, result.y);
268}
269
270
271
272/** Rescale point so that weight is 1.
273 */
274template<typename T>
276{
277 const TValue invZ = TNumTraits::one / z;
278 x *= invZ;
279 y *= invZ;
280 z = TNumTraits::one;
281}
282
283
284
285// --- FREE FUNCTIONS ---------------------------------------------------------------------------
286
287/** @relates lass::prim::Point2DH
288 */
289template<typename T>
290bool operator==(const Point2DH<T>& iA, const Point2DH<T>& iB)
291{
292 return iA.x == iB.x && iA.y == iB.y && iA.z == iB.z;
293}
294
295
296
297/** @relates lass::prim::Point2DH
298 */
299template<typename T> inline
300bool operator!=(const Point2DH<T>& iA, const Point2DH<T>& iB)
301{
302 return !(iA == iB);
303}
304
305
306
307/** @relates lass::prim::Point2DH
308 */
309template<typename T> inline
310Point2DH<T> operator+(const Point2DH<T>& iA, const Point2DH<T>& iB)
311{
312 Point2DH<T> result(iA);
313 result += iB;
314 return result;
315}
316
317
318
319/** @relates lass::prim::Point2DH
320 */
321template<typename T> inline
322Point2DH<T> operator+(const Point2DH<T>& iA, const Point2D<T>& iB)
323{
324 Point2DH<T> result(iA);
325 result += iB;
326 return result;
327}
328
329
330
331/** @relates lass::prim::Point2DH
332 */
333template<typename T> inline
334Point2DH<T> operator+(const Point2D<T>& iA, const Point2DH<T>& iB)
335{
336 Point2DH<T> result(iA);
337 result += iB;
338 return result;
339}
340
341
342
343/** @relates lass::prim::Point2DH
344 */
345template<typename T> inline
346Point2DH<T> operator+(const Point2D<T>& iA, const Point2D<T>& iB)
347{
348 Point2DH<T> result(iA);
349 result += iB;
350 return result;
351}
352
353
354
355/** @relates lass::prim::Point2DH
356 */
357template<typename T> inline
358Point2DH<T> operator-(const Point2DH<T>& iA, const Point2DH<T>& iB)
359{
360 Point2DH<T> result(iA.position());
361 result -= iB;
362 return result;
363}
364
365
366
367/** @relates lass::prim::Point2DH
368 */
369template<typename T> inline
370Point2DH<T> operator*(const Point2DH<T>& iA, typename Point2DH<T>::TParam iB)
371{
372 Point2DH<T> result(iA);
373 result *= iB;
374 return result;
375}
376
377
378
379/** @relates lass::prim::Point2DH
380 */
381template<typename T> inline
382Point2DH<T> operator/(const Point2DH<T>& iA, typename Point2DH<T>::TParam iB)
383{
384 Point2DH<T> result(iA);
385 result /= iB;
386 return result;
387}
388
389
390
391/** @relates lass::prim::Point2DH
392 */
393template<typename T> inline
394Point2DH<T> operator*(typename Point2DH<T>::TParam iA, const Point2DH<T>& iB)
395{
396 Point2DH<T> result(iB);
397 result *= iA;
398 return result;
399}
400
401
402
403/** @relates lass::prim::Point2DH
404 */
405template<typename T>
406std::ostream& operator<<(std::ostream& ioOStream, const Point2DH<T>& iB)
407{
408 LASS_ENFORCE_STREAM(ioOStream) << iB.position();
409 return ioOStream;
410}
411
412
413
414/** @relates lass::prim::Point2DH
415 */
416template<typename T>
417io::XmlOStream& operator<<(io::XmlOStream& oOStream, const Point2DH<T>& iB)
418{
419 LASS_ENFORCE_STREAM(oOStream)
420 << "<Point2DH>" << iB.x << " " << iB.y << " " << iB.z << "</Point2DH>\n";
421 return oOStream;
422}
423
424
425
426/** @relates lass::prim::Point2DH
427 */
428template<typename T>
429std::istream& operator>>(std::istream& ioIStream, Point2DH<T>& oB)
430{
431 Vector3D<T> temp;
432 LASS_ENFORCE(ioIStream) >> temp;
433 oB = Point2DH<T>(temp);
434 return ioIStream;
435}
436
437
438
439}
440
441}
442
443#endif
444
445// --- END OF FILE ------------------------------------------------------------------------------
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53
homogenous 2D Point
Definition point_2dh.h:63
void homogenize()
Rescale point so that weight is 1.
bool isValid() const
Return true if point is valid.
TConstReference at(signed iIndex) const
Wrap index around range.
bool isNaN() const
Return true if at least one of the components is NaN.
bool isZero() const
Return true if point is origin (0, 0, z).
bool isInfinite() const
Return true if point is at infinite distance of origin.
const Point2D< T > affine() const
Return rescaled version of point with weight = 1.
const Point2DH< T > & operator+() const
A weird way to get back the same object.
const TValue weight() const
Return weight of point.