Library of Assembled Shared Sources
point_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_POINT_3D_INL
46#define LASS_GUARDIAN_OF_INCLUSION_PRIM_POINT_3D_INL
47
48
49
50
51#include "point_3d.h"
52
53
54
55namespace lass
56{
57
58namespace prim
59{
60
61template<typename T> inline
62Point3D<T>::Point3D() :
63 x(TNumTraits::zero),
64 y(TNumTraits::zero),
65 z(TNumTraits::zero)
66{
67 LASS_ASSERT(isZero());
68}
69
70
71
72template<typename T> inline
73Point3D<T>::Point3D(TParam x, TParam y, TParam z) :
74 x(x),
75 y(y),
76 z(z)
77{
78}
79
80
81
82template<typename T>
83template<typename U> inline
84Point3D<T>::Point3D(const Point3D<U>& other):
85 x(static_cast<TValue>(other.x)),
86 y(static_cast<TValue>(other.y)),
87 z(static_cast<TValue>(other.z))
88{
89}
90
91
92
93template<typename T>
94template<typename U> inline
95Point3D<T>::Point3D(const Vector3D<U>& position):
96 x(static_cast<TValue>(position.x)),
97 y(static_cast<TValue>(position.y)),
98 z(static_cast<TValue>(position.z))
99{
100}
101
102
103
104template<typename T>
105template<typename U> inline
106Point3D<T>::Point3D(const U& x, const U& y, const U& z):
107 x(static_cast<TValue>(x)),
108 y(static_cast<TValue>(y)),
109 z(static_cast<TValue>(z))
110{
112
113
114
115template <typename T> inline
116const typename Point3D<T>::TVector
117Point3D<T>::position() const
118{
119 return TVector(x, y, z);
120}
121
122
123
124template<typename T> inline
125typename Point3D<T>::TConstReference
126Point3D<T>::operator[](size_t index) const
127{
128 LASS_ASSERT(index < dimension);
129 return *(&x + index);
130}
131
132
133
134template<typename T> inline
135typename Point3D<T>::TReference
136Point3D<T>::operator[](size_t index)
137{
138 LASS_ASSERT(index < dimension);
139 return *(&x + index);
140}
141
142
143
144/** Wrap index around range.
145 */
146template<typename T> inline
147typename Point3D<T>::TConstReference
148Point3D<T>::at(signed index) const
149{
150 return *(&x + num::mod(index, dimension));
151}
152
153
154
155/** Wrap index around range.
156 */
157template<typename T> inline
158typename Point3D<T>::TReference
159Point3D<T>::at(signed index)
160{
161 return *(&x + num::mod(index, dimension));
162}
163
164
165
166
167template<typename T> inline
168Point3D<T>& Point3D<T>::operator+=(const TVector& offset)
169{
170 x += offset.x;
171 y += offset.y;
172 z += offset.z;
173 return *this;
174}
175
176
177
178template<typename T> inline
179Point3D<T>& Point3D<T>::operator-=(const TVector& offset)
180{
181 x -= offset.x;
182 y -= offset.y;
183 z -= offset.z;
184 return *this;
185}
186
187
188
189template<typename T> inline
190bool Point3D<T>::isZero() const
191{
192 return x == TNumTraits::zero && y == TNumTraits::zero && z == TNumTraits::zero;
193}
194
195
196
197/** Return true if at least one of the components is NaN
198 */
199template<typename T> inline
201{
202 return num::isNaN(x) || num::isNaN(y) || num::isNaN(z);
203}
204
205
206
207// --- FREE FUNCTIONS ---------------------------------------------------------------------------
208
209/** @relates lass::prim::Point3D
210 */
211template<typename T> inline
212bool operator==(const Point3D<T>& a, const Point3D<T>& b)
213{
214 return a.x == b.x && a.y == b.y && a.z == b.z;
215}
216
217
218
219/** @relates lass::prim::Point3D
220 */
221template<typename T> inline
222bool operator!=(const Point3D<T>& a, const Point3D<T>& b)
223{
224 return !(a == b);
225}
226
227
228
229/** @relates lass::prim::Point3D
230 */
231template<typename T> inline
232Point3D<T> operator+(const Point3D<T>& a, const Vector3D<T>& b)
233{
234 Point3D<T> result(a);
235 result += b;
236 return result;
237}
238
239
240
241/** @relates lass::prim::Point3D
242 */
243template<typename T> inline
244Point3D<T> operator-(const Point3D<T>& a, const Vector3D<T>& b)
245{
246 Point3D<T> result(a);
247 result -= b;
248 return result;
249}
250
251
252
253/** @relates lass::prim::Point3D
254 */
255template<typename T> inline
256Point3D<T> operator+(const Vector3D<T>& a, const Point3D<T>& b)
257{
258 Point3D<T> result(b);
259 result += a;
260 return result;
261}
262
263
264
265/** @relates lass::prim::Point3D
266 */
267template<typename T> inline
268Vector3D<T> operator-(const Point3D<T>& a, const Point3D<T>& b)
269{
270 return Vector3D<T>(a.x - b.x, a.y - b.y, a.z - b.z);
271}
272
273
274
275/** return the distance between two points
276 * @relates lass::prim::Point3D
277 */
278template<typename T> inline
279typename Point3D<T>::TValue distance(const Point3D<T>& a, const Point3D<T>& b)
280{
281 return (a - b).norm();
282}
283
284/** @relates lass::prim::Point2D
285 */
286template<typename T> inline
287typename Point3D<T>::TValue squaredDistance(const Point3D<T>& a, const Point3D<T>& b)
288{
289 return (a - b).squaredNorm();
290}
291
292
293
294/** return a point with, for each coordinate, the minimum value of a and b
295 * @relates lass::prim::Point3D
296 */
297template<typename T>
298Point3D<T> pointwiseMin(const Point3D<T>& a, const Point3D<T>& b)
299{
300 return Point3D<T>(std::min(a.x, b.x), std::min(a.y, b.y), std::min(a.z, b.z));
301}
302
303
304
305/** interpolate linearly between two points: a + t * (b - a)
306 * @relates lass::prim::Point3D
307 */
308template<typename T> inline
309Point3D<T> lerp(const Point3D<T>& a, const Point3D<T>& b, typename Point3D<T>::TParam t)
310{
311 return Point3D<T>(lerp(a.position(), b.position(), t));
312}
313
314
315
316/** return a point with, for each coordinate, the maximum value of a and b
317 * @relates lass::prim::Point3D
318 */
319template<typename T>
320Point3D<T> pointwiseMax(const Point3D<T>& a, const Point3D<T>& b)
321{
322 return Point3D<T>(std::max(a.x, b.x), std::max(a.y, b.y), std::max(a.z, b.z));
323}
324
325
326
327/** @relates lass::prim::Point3D
328 */
329template<typename T>
330std::ostream& operator<<(std::ostream& stream, const Point3D<T>& b)
331{
332 LASS_ENFORCE_STREAM(stream) << b.position();
333 return stream;
334}
335
336
337
338/** @relates lass::prim::Point3D
339 */
340template<typename T>
341io::XmlOStream& operator<<(io::XmlOStream& stream, const Point3D<T>& b)
342{
343 LASS_ENFORCE_STREAM(stream)
344 << "<Point3D>" << b.x << " " << b.y << " " << b.z << "</Point3D>\n";
345 return stream;
346}
347
348
349/** @relates lass::prim::Point3D
350 */
351template<typename T>
352std::istream& operator>>(std::istream& stream, Point3D<T>& b)
353{
354 Vector3D<T> temp;
355 LASS_ENFORCE_STREAM(stream) >> temp;
356 b = Point3D<T>(temp);
357 return stream;
358}
359
360
361
362}
363
364}
365
366#endif
Output stream for writing a selection of geometric primitives to XML files.
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53
bool isNaN() const
Return true if at least one of the components is NaN.
Definition point_3d.inl:200
TConstReference at(signed index) const
Wrap index around range.
Definition point_3d.inl:148
Point3D< T > pointwiseMax(const Point3D< T > &a, const Point3D< T > &b)
return a point with, for each coordinate, the maximum value of a and b
Definition point_3d.inl:320
Point3D< T > lerp(const Point3D< T > &a, const Point3D< T > &b, typename Point3D< T >::TParam t)
interpolate linearly between two points: a + t * (b - a)
Definition point_3d.inl:309
Point3D< T > pointwiseMin(const Point3D< T > &a, const Point3D< T > &b)
return a point with, for each coordinate, the minimum value of a and b
Definition point_3d.inl:298
Point3D< T >::TValue distance(const Point3D< T > &a, const Point3D< T > &b)
return the distance between two points
Definition point_3d.inl:279