Library of Assembled Shared Sources
aabb_2d_triangle_2d.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_2D_TRIANGLE_2D_H
44#define LASS_GUARDIAN_OF_INCLUSION_PRIM_AABB_2D_TRIANGLE_2D_H
45
46#include "prim_common.h"
47#include "aabb_2d.h"
48#include "triangle_2d.h"
49
50namespace lass
51{
52namespace prim
53{
54
55/** determine axis aligned bounding box of a 2D triangle
56 * @relates lass::prim::Triangle2D
57 * @sa lass::prim::Aabb2D
58 */
59template <typename T>
61{
62 Aabb2D<T, AutoMinMax> result(triangle[0], triangle[1]);
63 result += triangle[2];
64 return result;
65}
66
67
68
69/* @relates lass::prim::Triangle2D
70 * @sa lass::prim::Aabb2D
71 */
72template <typename T, typename MMP>
73bool intersects(const Triangle2D<T>& triangle, const Aabb2D<T, MMP>& box)
74{
75 if (!box.intersects(aabb(triangle)))
76 {
77 return false;
78 }
79
80 typedef typename Triangle2D<T>::TPoint TPoint;
81 typedef typename Triangle2D<T>::TVector TVector;
82 typedef typename Triangle2D<T>::TValue TValue;
83 size_t k1 = 1, k2 = 2;
84 for (size_t k0 = 0; k0 < 3; ++k0)
85 {
86 const TPoint& tail = triangle[k0];
87 const TVector edge = triangle[k1] - tail;
88 const TVector min = box.min() - tail;
89 const TVector max = box.max() - tail;
90
91 const TValue t[4] = {
92 perpDot(edge, min),
93 perpDot(edge, max),
94 perpDot(edge, TVector(min.x, max.y)),
95 perpDot(edge, TVector(max.x, min.y))
96 };
97
98 if (t[0] < 0 && t[1] < 0 && t[2] < 0 && t[3] < 0)
99 {
100 return false;
101 }
102
103 const TValue tMax = perpDot(edge, triangle[k2] - tail);
104 LASS_ASSERT(tMax >= 0);
105 if (t[0] > tMax && t[1] > tMax && t[2] > tMax && t[3] > tMax)
106 {
107 return false;
108 }
109
110 k1 = k2;
111 k2 = k0;
112 }
113
114 return true;
115}
116
117
118
119/* @relates lass::prim::Aabb2D
120 * @sa lass::prim::Triangle2D
121 */
122template <typename T, typename MMP>
123bool intersects(const Aabb2D<T, MMP>& box, const Triangle2D<T>& triangle)
124{
125 return intersects(triangle, box);
126}
127
128
129
130/* @relates lass::prim::Triangle2D
131 * @sa lass::prim::Aabb2D
132 */
133template <typename T, typename MMP>
134bool collides(const Triangle2D<T>& triangle, const Aabb2D<T, MMP>& box)
135{
136 if (!box.collides(aabb(triangle)))
137 {
138 return false;
139 }
140
141 typedef typename Triangle2D<T>::TPoint TPoint;
142 typedef typename Triangle2D<T>::TVector TVector;
143 typedef typename Triangle2D<T>::TValue TValue;
144 size_t k1 = 1, k2 = 2;
145 for (size_t k0 = 0; k0 < 3; ++k0)
146 {
147 const TPoint& tail = triangle[k0];
148 const TVector edge = triangle[k1] - tail;
149 const TVector min = box.min() - tail;
150 const TVector max = box.max() - tail;
151
152 const TValue t[4] = {
153 perpDot(edge, min),
154 perpDot(edge, max),
155 perpDot(edge, TVector(min.x, max.y)),
156 perpDot(edge, TVector(max.x, min.y))
157 };
158
159 if (t[0] <= 0 && t[1] <= 0 && t[2] <= 0 && t[3] <= 0)
160 {
161 return false;
162 }
163
164 const TValue tMax = perpDot(edge, triangle[k2] - tail);
165 LASS_ASSERT(tMax >= 0);
166 if (t[0] >= tMax && t[1] >= tMax && t[2] >= tMax && t[3] >= tMax)
167 {
168 return false;
169 }
170
171 k1 = k2;
172 k2 = k0;
173 }
174
175 return true;
176}
177
178
179
180/* @relates lass::prim::Aabb2D
181 * @sa lass::prim::Triangle2D
182 */
183template <typename T, typename MMP>
184bool collides(const Aabb2D<T, MMP>& box, const Triangle2D<T>& triangle)
185{
186 return collides(triangle, box);
187}
188
189}
190}
191
192#endif
193
194// EOF
your momma's axis aligned bounding box.
Definition aabb_2d.h:89
const TPoint & min() const
return corner with smallest component values
Definition aabb_2d.inl:114
bool intersects(const Aabb2D< T, MMP2 > &other) const
Check if two axis-aligned bounding boxes do intersect.
Definition aabb_2d.inl:384
const TPoint & max() const
Return corner with largest component values.
Definition aabb_2d.inl:126
A very simple 2D polygon :)
Definition triangle_2d.h:65
Aabb2D< T > aabb(const Triangle2D< T > &triangle)
determine axis aligned bounding box of a 2D triangle
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53