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