Library of Assembled Shared Sources
aabb_3d_triangle_3d.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#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_AABB_3D_TRIANGLE_3D_H
44#define LASS_GUARDIAN_OF_INCLUSION_PRIM_AABB_3D_TRIANGLE_3D_H
45
46#include "prim_common.h"
47#include "aabb_3d.h"
48#include "triangle_3d.h"
49
50namespace lass
51{
52namespace prim
53{
54
55/** determine axis aligned bounding box of a 3D triangle
56 * @relates Aabb3D
57 */
58template <typename T>
60{
61 Aabb3D<T, AutoMinMax> result(triangle[0], triangle[1]);
62 result += triangle[2];
63 return result;
64}
65
66namespace impl
67{
68
69template <typename T>
70void inpminmax(T& a, T& b)
71{
72 if (a > b)
73 {
74 std::swap(a, b);
75 }
76}
77
78template <typename P>
79bool intersectsHelperTriangleAabb3D(const P& v0, const P& v1, const P& v2, const P& a, const P& b)
80{
81 typedef typename P::TVector TVector;
82 typedef typename P::TValue TValue;
83
84 // this should move somedday to some place more appropriate.
85 class Range
86 {
87 public:
88 Range(TValue min, TValue max): min_(min), max_(max)
89 {
90 inpminmax(min_, max_);
91 }
92 Range& operator+=(const Range& other)
93 {
94 min_ += other.min_;
95 max_ += other.max_;
96 return *this;
97 }
98 bool seperated(const Range& other) const
99 {
100 return other.max_ < min_ || other.min_ > max_;
101 }
102 private:
103 TValue min_;
104 TValue max_;
105 };
106
107 const TVector f = v1 - v0; // triangleEdge
108
109 {
110 // const V aabbEdge(1, 0, 0);
111 // const V axis = cross(aabbEdge, triangleEdge);
112 const Range triangleRange(
113 v0.z * v1.y - v0.y * v1.z, // == dot(v0, axis) == dot(v1, axis)
114 f.y * v2.z - f.z * v2.y // == dot(v2, axis)
115 );
116 Range boxRange(a.y * -f.z, b.y * -f.z);
117 boxRange += Range(a.z * f.y, b.z * f.y);
118 if (triangleRange.seperated(boxRange)) return false;
119 }
120
121 {
122 // const V aabbEdge(0, 1, 0);
123 const Range triangleRange(
124 v0.x * v1.z - v0.z * v1.x,
125 f.z * v2.x - f.x * v2.z
126 );
127 Range boxRange(a.z * -f.x, b.z * -f.x);
128 boxRange += Range(a.x * f.z, b.x * f.z);
129 if (triangleRange.seperated(boxRange)) return false;
130 }
131
132 {
133 // const V aabbEdge(0, 0, 1);
134 const Range triangleRange(
135 v0.y * v1.x - v0.x * v1.y,
136 f.x * v2.y - f.y * v2.x
137 );
138 Range boxRange(a.x * -f.y, b.x * -f.y);
139 boxRange += Range(a.y * f.x, b.y * f.x);
140 if (triangleRange.seperated(boxRange)) return false;
141 }
142
143 return true;
144}
145
146}
147
148/* @relates lass::prim::Triangle3D
149 * @sa lass::prim::Aabb3D
150 *
151 * Using the Tomas Akenine-Moller algorithm from "Fast 3D Triangle-Box Overlap Testing"
152 */
153template <typename T, typename MMP>
154bool intersects(const Triangle3D<T>& triangle, const Aabb3D<T, MMP>& box)
155{
156 // bullet 3
157 if (!impl::intersectsHelperTriangleAabb3D(triangle[0], triangle[1], triangle[2], box.min(), box.max())) return false;
158 if (!impl::intersectsHelperTriangleAabb3D(triangle[1], triangle[2], triangle[0], box.min(), box.max())) return false;
159 if (!impl::intersectsHelperTriangleAabb3D(triangle[2], triangle[0], triangle[1], box.min(), box.max())) return false;
160
161 // bullet 1
162 if (!box.intersects(aabb(triangle)))
163 {
164 return false;
165 }
166
167 // bullet 2
168 return intersects(triangle.plane(), box);
169}
170
171
172}
173}
174
175#endif
176
177// EOF
your momma's axis aligned bounding box.
Definition aabb_3d.h:89
bool intersects(const Aabb3D< T, MMP2 > &other) const
Check if two axis-aligned bounding boxes do intersect.
Definition aabb_3d.inl:392
const TPoint & min() const
return corner with smallest component values
Definition aabb_3d.inl:113
const TPoint & max() const
Return corner with largest component values.
Definition aabb_3d.inl:125
Aabb3D< T > aabb(const Triangle3D< T > &triangle)
determine axis aligned bounding box of a 3D triangle
A very simple 3D polygon :)
Definition triangle_3d.h:64
const TPlane plane() const
return support plane of polygon.
implementation details of lass::prim
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53