library of assembled shared sources

http://lass.cocamware.com

aabb_3d_simple_polygon_3d.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_AABB_3D_SIMPLE_POLYGON_3D_H
00044 #define LASS_GUARDIAN_OF_INCLUSION_PRIM_AABB_3D_SIMPLE_POLYGON_3D_H
00045 
00046 #include "prim_common.h"
00047 #include "aabb_3d.h"
00048 #include "simple_polygon_3d.h"
00049 
00050 namespace lass
00051 {
00052 namespace prim
00053 {
00054 
00055 /** determine axis aligned bounding box of a 3D simple polygon
00056  *  @relates Aabb3D
00057  */
00058 template <typename T, class EP, class NP> 
00059 Aabb3D<T> aabb(const SimplePolygon3D<T, EP, NP>& polygon)
00060 {
00061     Aabb3D<T> result;
00062     const size_t n = polygon.size();
00063     for (size_t i = 0; i < n; ++i)
00064     {
00065         result += polygon[i];
00066     }
00067     return result;
00068 }
00069 
00070 /** Clip a plane to an AABB and get a polygon.
00071  *  @relates lass::prim::Aabb3D
00072  *  @relates lass::prim::Plane3D
00073  *  @relates lass::prim::SimplePolygon3D
00074  *
00075  *  @param iAabb [in] the Aabb to clip to
00076  *  @param iPlane [in] the plane to be clipped
00077  *  @return the clipped polygon.
00078  */
00079 template <typename T, class EP, class NP, class MMP>
00080 SimplePolygon3D<T, EP, NP> clip(const Aabb3D<T, MMP>& iAabb, 
00081         const Plane3D<T, EP, NP>& iPlane)
00082 {
00083     typedef Plane3D<T, EP, NP> TPlane;
00084     typedef SimplePolygon3D<T, EP, NP> TPolygon;
00085     typedef Point3D<T> TPoint;
00086     typedef typename TPoint::TVector TVector;
00087     typedef typename TPoint::TValue TValue;
00088     TPolygon poly(iPlane);
00089 
00090     const TPoint& min = iAabb.min();
00091     const TPoint& max = iAabb.max();
00092     
00093     TVector normal;
00094     TValue d;
00095     iPlane.getCartesian(normal, d);
00096 
00097     TVector m;
00098     switch (iPlane.majorAxis())
00099     {
00100         case 0: // x
00101         {
00102                 TPoint p[4] = 
00103             {
00104                 TPoint(0.0, min.y, min.z), TPoint(0.0, max.y, min.z),
00105                 TPoint(0.0, max.y, max.z), TPoint(0.0, min.y, max.z)
00106             };
00107             if (iPlane.normal().x < 0)
00108             {
00109                 std::reverse(p, p + 4);
00110             }
00111             for (size_t i = 0; i < 4; ++i)
00112             {
00113                 p[i].x = -(dot(normal, p[i].position()) + d) / normal.x;
00114                 poly.add(p[i]);
00115             }
00116             m = TVector(1.0, 0.0, 0.0);
00117             break;
00118         }
00119         case 1: // y
00120         {
00121             TPoint p[4] = 
00122             {
00123                 TPoint(min.x, 0.0, min.z), TPoint(min.x, 0.0, max.z),
00124                 TPoint(max.x, 0.0, max.z), TPoint(max.x, 0.0, min.z)
00125             };
00126             if (iPlane.normal().y < 0)
00127             {
00128                 std::reverse(p, p + 4);
00129             }
00130             for (size_t i = 0; i < 4; ++i)
00131             {
00132                 p[i].y = -(dot(normal, p[i].position()) + d) / normal.y;
00133                 poly.add(p[i]);
00134             }
00135             m = TVector(0.0, 1.0, 0.0);
00136             break;
00137         }
00138         case 2: // z
00139         {
00140             TPoint p[4] = 
00141             {
00142                 TPoint(min.x, min.y, 0.0), TPoint(max.x, min.y, 0.0),
00143                 TPoint(max.x, max.y, 0.0), TPoint(min.x, max.y, 0.0)
00144             };
00145             if (iPlane.normal().z < 0)
00146             {
00147                 std::reverse(p, p + 4);
00148             }
00149             for (size_t i = 0; i < 4; ++i)
00150             {
00151                 p[i].z = -(dot(normal, p[i].position()) + d) / normal.z;
00152                 poly.add(p[i]);
00153             }
00154             m = TVector(0.0, 0.0, 1.0);
00155         }
00156         default:
00157             LASS_ENFORCE_UNREACHABLE;
00158     };
00159 
00160     // clip against those 'm' borders
00161     return clip(TPlane(m, min), clip(TPlane(-m, max), poly));
00162 }       
00163 
00164 }
00165 }
00166 
00167 #endif
00168 
00169 // EOF

Generated on Mon Nov 10 14:19:59 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo