Library of Assembled Shared Sources
aabb_3d_simple_polygon_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_SIMPLE_POLYGON_3D_H
44#define LASS_GUARDIAN_OF_INCLUSION_PRIM_AABB_3D_SIMPLE_POLYGON_3D_H
45
46#include "prim_common.h"
47#include "aabb_3d.h"
48#include "simple_polygon_3d.h"
49
50namespace lass
51{
52namespace prim
53{
54
55/** determine axis aligned bounding box of a 3D simple polygon
56 * @relates Aabb3D
57 */
58template <typename T, class EP, class NP>
60{
61 Aabb3D<T> result;
62 const size_t n = polygon.size();
63 for (size_t i = 0; i < n; ++i)
64 {
65 result += polygon[i];
66 }
67 return result;
68}
69
70/** Clip a plane to an AABB and get a polygon.
71 * @relates lass::prim::Aabb3D
72 * @sa lass::prim::Plane3D
73 * @sa lass::prim::SimplePolygon3D
74 *
75 * @param box [in] the Aabb to clip to
76 * @param plane [in] the plane to be clipped
77 * @return the clipped polygon.
78 */
79template <typename T, class EP, class NP, class MMP>
81{
82 typedef Plane3D<T, EP, NP> TPlane;
83 typedef SimplePolygon3D<T, EP, NP> TPolygon;
84 typedef Point3D<T> TPoint;
85 typedef typename TPoint::TVector TVector;
86 typedef typename TPoint::TValue TValue;
87 TPolygon poly(plane);
88
89 const TPoint& min = box.min();
90 const TPoint& max = box.max();
91
92 TVector normal;
93 TValue d;
94 plane.getCartesian(normal, d);
95
96 TVector m;
97 switch (plane.majorAxis())
98 {
99 case 0: // x
100 {
101 TPoint p[4] =
102 {
103 TPoint(0.0, min.y, min.z), TPoint(0.0, max.y, min.z),
104 TPoint(0.0, max.y, max.z), TPoint(0.0, min.y, max.z)
105 };
106 if (plane.normal().x < 0)
107 {
108 std::reverse(p, p + 4);
109 }
110 for (size_t i = 0; i < 4; ++i)
111 {
112 p[i].x = -(dot(normal, p[i].position()) + d) / normal.x;
113 poly.add(p[i]);
114 }
115 m = TVector(1.0, 0.0, 0.0);
116 break;
117 }
118 case 1: // y
119 {
120 TPoint p[4] =
121 {
122 TPoint(min.x, 0.0, min.z), TPoint(min.x, 0.0, max.z),
123 TPoint(max.x, 0.0, max.z), TPoint(max.x, 0.0, min.z)
124 };
125 if (plane.normal().y < 0)
126 {
127 std::reverse(p, p + 4);
128 }
129 for (size_t i = 0; i < 4; ++i)
130 {
131 p[i].y = -(dot(normal, p[i].position()) + d) / normal.y;
132 poly.add(p[i]);
133 }
134 m = TVector(0.0, 1.0, 0.0);
135 break;
136 }
137 case 2: // z
138 {
139 TPoint p[4] =
140 {
141 TPoint(min.x, min.y, 0.0), TPoint(max.x, min.y, 0.0),
142 TPoint(max.x, max.y, 0.0), TPoint(min.x, max.y, 0.0)
143 };
144 if (plane.normal().z < 0)
145 {
146 std::reverse(p, p + 4);
147 }
148 for (size_t i = 0; i < 4; ++i)
149 {
150 p[i].z = -(dot(normal, p[i].position()) + d) / normal.z;
151 poly.add(p[i]);
152 }
153 m = TVector(0.0, 0.0, 1.0);
154 }
155 default:
156 LASS_ENFORCE_UNREACHABLE;
157 };
158
159 // clip against those 'm' borders
160 return clip(TPlane(m, min), clip(TPlane(-m, max), poly));
161}
162
163
164
165/** Clip a polygon to an AABB .
166 * @relates lass::prim::Aabb3D
167 * @sa lass::prim::SimplePolygon3D
168 *
169 * @param box [in] the Aabb to clip to
170 * @param polygon [in] the polygon to be clipped
171 * @return the clipped polygon.
172 */
173template <typename T, class EP, class NP, class MMP>
175{
176 typedef SimplePolygon3D<T, EP, NP> TPoly;
177 typedef typename TPoly::TPlane TPlane;
178 typedef typename TPoly::TPoint TPoint;
179 typedef typename TPoly::TVector TVector;
180
181 const TPoint& min = box.min();
182 const TPoint& max = box.max();
183
184 const TVector i(1, 0, 0);
185 TPoly poly = clip(TPlane(i, -min.x), clip(TPlane(-i, max.x), polygon));
186 const TVector j(0, 1, 0);
187 poly = clip(TPlane(j, -min.y), clip(TPlane(-j, max.y), poly));
188 const TVector k(0, 0, 1);
189 return clip(TPlane(k, -min.z), clip(TPlane(-k, max.z), poly));
190}
191
192
193
194}
195}
196
197#endif
198
199// EOF
SimplePolygon3D< T, EP, NP > clip(const Aabb3D< T, MMP > &box, const SimplePolygon3D< T, EP, NP > &polygon)
Clip a polygon to an AABB .
SimplePolygon3D< T, EP, NP > clip(const Aabb3D< T, MMP > &box, const Plane3D< T, EP, NP > &plane)
Clip a plane to an AABB and get a polygon.
Aabb3D< T > aabb(const SimplePolygon3D< T, EP, NP > &polygon)
determine axis aligned bounding box of a 3D simple polygon
A 3D hyper plane.
Definition plane_3d.h:361
const XYZ majorAxis() const
determines the major axis of the normal vector.
Definition plane_3d.inl:179
convex or concave polygon in 3D (not selfintersecting, no holes)
size_t size() const
return number of vertices
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53