Library of Assembled Shared Sources
default_object_traits.h
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/** @class lass::spat::DefaultObjectTraits
46 * @brief default traits for objects to be stored in spatial subdivision trees
47 * @author Bram de Greve [BdG]
48 */
49
50#ifndef LASS_GUARDIAN_OF_INCLUSION_SPAT_DEFAULT_OBJECT_TRAITS_H
51#define LASS_GUARDIAN_OF_INCLUSION_SPAT_DEFAULT_OBJECT_TRAITS_H
52
53#include "spat_common.h"
54#include "../prim/result.h"
55#include "../meta/int.h"
56
57namespace lass
58{
59namespace spat
60{
61
62template
63<
64 typename AabbType,
65 typename RayType
66>
67struct DefaultAabbRayTraits
68{
69 typedef AabbType TAabb; /**< an nD AABB */
70 typedef RayType TRay; /**< an nD Ray */
71
72 typedef typename TAabb::TPoint TPoint; /**< an nD point */
73 typedef typename TAabb::TVector TVector; /**< an nD vector */
74 typedef typename TAabb::TValue TValue; /**< numerical type used in TPoint and TVector */
75 typedef typename TAabb::TParam TParam; /**< best type for function parameters of TValue */
76 typedef typename TAabb::TReference TReference; /**< reference to TValue */
77 typedef typename TAabb::TConstReference TConstReference; /**< const reference to TValue */
78
79 enum { dimension = TAabb::dimension }; /**< nD = number of dimensions of TPoint */
80
81 // AABB
82
83 /** return empty AABB
84 */
85 static const TAabb aabbEmpty()
86 {
87 return TAabb();
88 }
89
90 static const TAabb aabbMake(const TPoint& min, const TPoint& max)
91 {
92 return TAabb(min, max);
93 }
94
95 /** return true if AABB contains a point, return false otherwise
96 */
97 static bool aabbContains(const TAabb& aabb, const TPoint& point)
98 {
99 return aabb.contains(point);
100 }
101
102 static bool aabbContains(const TAabb& aabb, const TAabb& other)
103 {
104 return aabb.contains(other);
105 }
106
107 static bool aabbIntersects(const TAabb& aabb, const TAabb& other)
108 {
109 return aabb.intersects(other);
110 }
111
112 /** return true if AABB is intersected by ray
113 */
114 static bool aabbIntersect(const TAabb& aabb, const TRay& ray, TReference t, const TParam tMin)
115 {
116 return intersect(aabb, ray, t, tMin) != prim::rNone;
117 }
118
119 /** return true if AABB is intersected by ray, with the reciprocal ray direction already supplied.
120 */
121 static bool aabbIntersect(const TAabb& aabb, const TRay& ray, const TVector& invDirection, TReference t, const TParam tMin)
122 {
123 return intersect(aabb, ray, invDirection, t, tMin) != prim::rNone;
124 }
125
126 /** join two AABBs and return the result
127 */
128 static const TAabb aabbJoin(const TAabb& a, const TAabb& b)
129 {
130 return a + b;
131 }
132
133 /** return the minimum corner of the AABB
134 */
135 static const TPoint aabbMin(const TAabb& aabb)
136 {
137 return aabb.min();
138 }
139
140 /** return the maximum corner of the AABB
141 */
142 static const TPoint aabbMax(const TAabb& aabb)
143 {
144 return aabb.max();
145 }
146
147 /** return surface area of the AABB
148 */
149 static TValue aabbSurfaceArea(const TAabb& aabb)
150 {
151 return aabbSurfaceArea(aabb, meta::Int<dimension>());
152 }
153
154
155 // RAY
156
157 /** return the support point of the ray
158 */
159 static const TPoint raySupport(const TRay& ray)
160 {
161 return ray.support();
162 }
163
164 /** return the direction vector of the ray
165 */
166 static const TVector rayDirection(const TRay& ray)
167 {
168 return ray.direction();
169 }
170
171 /** return a point along the ray
172 */
173 static const TPoint rayPoint(const TRay& ray, TParam t)
174 {
175 return ray.point(t);
176 }
177
178
179 // POINTS AND VECTORS
180
181 /** return the @a axis coordinate value of @a point.
182 */
183 static const TValue coord(const TPoint& point, size_t axis)
184 {
185 return point[axis];
186 }
187
188 /** set the @a axis coordinate value of @a point.
189 */
190 static void coord(TPoint& point, size_t axis, TParam value)
191 {
192 point[axis] = value;
193 }
194
195 /** return the @a axis component value of @a vector.
196 */
197 static const TValue coord(const TVector& vector, size_t axis)
198 {
199 return vector[axis];
200 }
201
202 /** set the @a axis component value of @a vector.
203 */
204 static void coord(TVector& vector, size_t axis, TParam value)
205 {
206 vector[axis] = value;
207 }
208
209 /** return the reciprocal vector of @a vector
210 */
211 static const TVector vectorReciprocal(const TVector& vector)
212 {
213 return vector.reciprocal();
214 }
215
216private:
217
218 static TValue aabbSurfaceArea(const TAabb& aabb, meta::Int<2>)
219 {
220 return aabb.perimeter();
221 }
222 static TValue aabbSurfaceArea(const TAabb& aabb, meta::Int<3>)
223 {
224 return aabb.area();
225 }
226};
227
228
229
230template
231<
232 typename ObjectType,
233 typename AabbType = typename ObjectType::TAabb,
234 typename RayType = typename ObjectType::TRay,
235 typename ObjectIterator = const ObjectType*
236>
237struct DefaultObjectTraits: public DefaultAabbRayTraits<AabbType, RayType>
238{
239 typedef ObjectType TObject; /**< type of nD object */
240 typedef ObjectIterator TObjectIterator; /**< iterator to object */
241 typedef const TObject& TObjectReference; /**< const reference to object */
242 typedef void TInfo; /**< extra info for contain/intersect operations */
243
244 typedef typename DefaultAabbRayTraits<AabbType, RayType>::TAabb TAabb;
245 typedef typename DefaultAabbRayTraits<AabbType, RayType>::TRay TRay;
246 typedef typename DefaultAabbRayTraits<AabbType, RayType>::TPoint TPoint;
247 typedef typename DefaultAabbRayTraits<AabbType, RayType>::TVector TVector;
248 typedef typename DefaultAabbRayTraits<AabbType, RayType>::TValue TValue;
249 typedef typename DefaultAabbRayTraits<AabbType, RayType>::TParam TParam;
250 typedef typename DefaultAabbRayTraits<AabbType, RayType>::TReference TReference;
251 typedef typename DefaultAabbRayTraits<AabbType, RayType>::TConstReference TConstReference;
252
253 /** return reference to object
254 */
256 {
257 return *it;
258 }
259
260 /** return the AABB of an object
261 */
262 static const TAabb objectAabb(TObjectIterator it)
263 {
264 return aabb(object(it));
265 }
266
267 /** return true if object contains a point, return false otherwise
268 */
269 static bool objectContains(TObjectIterator it, const TPoint& point, const TInfo* /*iInfo*/)
270 {
271 return object(it).contains(point);
272 }
273
274 /** return true if object is intersected by ray
275 */
276 static bool objectIntersect(TObjectIterator it, const TRay& ray, TReference t, TParam tMin, const TInfo* /*iInfo*/)
277 {
278 return intersect(object(it), ray, t, tMin) != prim::rNone;
279 }
280
281 /** return true if object is intersected by ray
282 */
283 static bool objectIntersects(TObjectIterator it, const TRay& ray, TParam tMin, TParam tMax, const TInfo* /*iInfo*/)
284 {
285 TValue t;
286 return intersect(object(it), ray, t, tMin) != prim::rNone && t < tMax;
287 }
288
289 /** return true if part of the object is inside the bounding box
290 */
291 static bool objectIntersects(TObjectIterator it, const TAabb& aabb, const TInfo* /*iInfo*/)
292 {
293 return intersects(object(it), aabb);
294 }
295
296 /** return squared distance between object and point)
297 */
298 static const TValue objectSquaredDistance(TObjectIterator it, const TPoint& point, const TInfo* /* info */)
299 {
300 return squaredDistance(object(it), point);
301 }
302
303 /** return surface area of object
304 */
306 {
307 return object(it).area();
308 }
309};
310
311
312
313}
314
315}
316
317#endif
318
319// EOF
@ rNone
operation has no answer, output arguments are meaningless
Definition result.h:76
spatial subdivisions, quadtrees, octrees, meshes in 2D and 3D, triangulators, ...
Definition aabb8_tree.h:80
Library for Assembled Shared Sources.
Definition config.h:53
default traits for objects to be stored in spatial subdivision trees
static const TValue objectSquaredDistance(TObjectIterator it, const TPoint &point, const TInfo *)
return squared distance between object and point)
ObjectIterator TObjectIterator
iterator to object
static bool objectIntersect(TObjectIterator it, const TRay &ray, TReference t, TParam tMin, const TInfo *)
return true if object is intersected by ray
static const TAabb objectAabb(TObjectIterator it)
return the AABB of an object
static bool objectIntersects(TObjectIterator it, const TAabb &aabb, const TInfo *)
return true if part of the object is inside the bounding box
void TInfo
extra info for contain/intersect operations
static TValue objectSurfaceArea(TObjectIterator it)
return surface area of object
const TObject & TObjectReference
const reference to object
static bool objectIntersects(TObjectIterator it, const TRay &ray, TParam tMin, TParam tMax, const TInfo *)
return true if object is intersected by ray
static bool objectContains(TObjectIterator it, const TPoint &point, const TInfo *)
return true if object contains a point, return false otherwise
ObjectType TObject
type of nD object
static TObjectReference object(TObjectIterator it)
return reference to object