Graphics Reference
In-Depth Information
17
18
19
20
21
22
23
24
25
26
27
28
29
/
**
Returns the distance to the intersection, or inf if
there is none before maxDistance
*
/
public float
findFirstRayIntersection(
Ray
ray,
float
maxDistance);
}
public class
SomeStructure<
Value
>{
...
void
insert(
Value
value) {
Point3
key =
new
Point3
();
value.getPosition(key);
...
}
}
Listing 37.4: One possible implementation of a triangle under an
inheritance-based scheme. The
getBoxBounds
implementation computes the
bounds as needed; an alternative is to precompute and store them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class
Triangle
implements Primitive {
private
Point3
_vertex[3];
public
Point3
vertex(
int
i) {
return
_vertex[i];
}
public void
getBoxBounds(AABox bounds) {
bounds.set(
Point3
::min(vertex[0], vertex[1], vertex[2]),
Point3
::max(vertex[0], vertex[1], vertex[2]));
}
...
}
Inheritance is usually well understood by programmers working in an object-
oriented language. It also keeps the implementation of program features related
to a
Value
within that
Value
's class. This makes it a very attractive choice for
extracting keys. The simplicity comes at a cost in flexibility, however. Using an
inheritance approach, one cannot associate two different key extraction methods
with the same class, and the needs of a spatial data structure impose on the design
of the
Value
class, forcing them to be designed concurrently.
37.2.2.2 Traits
A C++ implementation might use a
trait
data structure in the style of the design
of the C++ Standard Template Library (STL). In this design pattern, a templated
trait class defines a set of method prototypes, and then specialized templates give
implementations of those methods for particular
Value
classes. Listing 37.5 shows
an example of one such interface named
PrimitiveKeyTrait
that supports box,
ball, and point keys. Below that definition is a specialization of the template for a
Triangle
, and an example of how a spatial data structure would use the trait class
to obtain a position key from a value.