library of assembled shared sources

http://lass.cocamware.com

degenerate_policy.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 
00044 
00045 /** @defgroup DegeneratePolicy DegeneratePolicy
00046  *  @brief policies to specify the rules for degenerate polygons and others..
00047  *  @author Bram de Greve
00048  *  @date 2003
00049  *
00050  *  Polygons and triangles can be degenerate.  e.g. when they have less than
00051  *  3 vertices, or some vertices are coincident, or colinear.  We provide a few
00052  *  policies to deal with this.
00053  */
00054 
00055 #ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_DEGENERATE_POLICY_H
00056 #define LASS_GUARDIAN_OF_INCLUSION_PRIM_DEGENERATE_POLICY_H
00057 
00058 
00059 #include "prim_common.h"
00060 #include "../util/enforcer.h"
00061 
00062 namespace lass
00063 {
00064 namespace prim
00065 {
00066 
00067 /** @ingroup DegeneratePolicy
00068  */
00069 class DegenerationError: public util::Exception
00070 {
00071 public:
00072     DegenerationError(const std::string& msg, const std::string& loc): util::Exception(msg, loc) {}
00073 private:
00074     LASS_UTIL_EXCEPTION_PRIVATE_IMPL(DegenerationError)
00075 };
00076 
00077 
00078 
00079 /** @ingroup DegeneratePolicy
00080  *
00081  *  This is the default policy
00082  *
00083  *  NoDegenerate will not prevent a primitive to become degenerate, but it will check some
00084  *  basic conditions when you try to do "unsafe" operations and throws
00085  *  an exception on failure.  e.g. CheckDegenerate will not allow you to get the @c edge(0)
00086  *  or @c vector(0) of a polygon with only one vertex.
00087  *
00088  *  However, it will not check all posible conditions.  It will avoid expensive predicates.
00089  *  e.g. it will avoid to test if a polygon is simple, because that's too expensive.  If you
00090  *  want all conditions to be checked, you should use StrictNoDegenerate
00091  */
00092 struct NoDegenerate
00093 {
00094     template <class Primitive>
00095     static void enforceEdge(const Primitive& iPrimitive, int iIndexOfTailVertex)
00096     {
00097         if (iPrimitive.at(iIndexOfTailVertex) == iPrimitive.at(iIndexOfTailVertex + 1))
00098         {
00099             LASS_THROW_EX(DegenerationError, "Degenerate edge detected at tail vertex '" << iIndexOfTailVertex << "'.");
00100         }
00101     }
00102 
00103     template <class Primitive>
00104     static void enforceSimple(const Primitive& /*iPrimitive*/)
00105     {
00106     }
00107 
00108     template <class Primitive>
00109     static const typename Primitive::TValue enforceNonZeroSignedArea(const Primitive& iPrimitive)
00110     {
00111         const typename Primitive::TValue result = iPrimitive.signedArea();
00112         if (result == Primitive::TNumTraits::zero)
00113         {
00114             LASS_THROW_EX(DegenerationError, "Area of primitive is zero.");
00115         }
00116         return result;
00117     }
00118 };
00119 
00120 
00121 
00122 /** @ingroup DegeneratePolicy
00123  *
00124  *  This is the default policy
00125  *
00126  *  StrictNoDegenerate is similar to NoDegenerate, but will check more conditions.  However, this
00127  *  might go wild on cpu usage (read: be very slow).  Use this if you really need to be on the safe
00128  *  side.
00129  */
00130 struct StrictNoDegenerate
00131 {
00132     template <class Primitive>
00133     static void enforceEdge(const Primitive& iPrimitive, int iIndexOfTailVertex)
00134     {
00135         if (iPrimitive.at(iIndexOfTailVertex) == iPrimitive.at(iIndexOfTailVertex + 1))
00136         {
00137             LASS_THROW_EX(DegenerationError, "Degenerate edge detected at tail vertex '" << iIndexOfTailVertex << "'.");
00138         }
00139     }
00140 
00141     template <class Primitive>
00142     static void enforceSimple(const Primitive& iPrimitive)
00143     {
00144         if (!iPrimitive.isSimple())
00145         {
00146             LASS_THROW_EX(DegenerationError, "polygon is not simple.");
00147         }
00148     }
00149 
00150     template <class Primitive>
00151     static const typename Primitive::TValue enforceNonZeroSignedArea(const Primitive& iPrimitive)
00152     {
00153         const typename Primitive::TValue result = iPrimitive.signedArea();
00154         if (result == Primitive::TNumTraits::zero)
00155         {
00156             LASS_THROW_EX(DegenerationError, "Area of primitive is zero.");
00157         }
00158         return result;
00159     }
00160 };
00161 
00162 
00163 
00164 /** @ingroup DegeneratePolicy
00165  *
00166  *  AllowDegenerate puts the responsibility on the user.  It will assume all conditions are fullfilled
00167  *  and continue like nothing is wrong.  This is the "best guess" thing.
00168  */
00169 struct AllowDegenerate
00170 {
00171     template <class Primitive>
00172     static void enforceEdge(const Primitive& /*iPrimitive*/, int /*iIndexOfTailVertex*/)
00173     {
00174     }
00175 
00176     template <class Primitive>
00177     static void enforceSimple(const Primitive& /*iPrimitive*/)
00178     {
00179     }
00180 
00181     template <class Primitive>
00182     static const typename Primitive::TValue enforceNonZeroSignedArea(const Primitive& iPrimitive)
00183     {
00184         return iPrimitive.signedArea();
00185     }
00186 };
00187 
00188 
00189 }
00190 
00191 }
00192 
00193 // --- implementation ------------------------------------------------------------------------------
00194 
00195 namespace lass
00196 {
00197 namespace prim
00198 {
00199 namespace impl
00200 {
00201 
00202 /** raises exception in case primitive is degenerate and we have restricting
00203  *  @relates lass::prim::Bounded
00204  */
00205 /*
00206 struct DegenerateRaiser
00207 {
00208     template <class T>
00209     static void raise(const T&, const std::string& iMessage, const char* iLocus)
00210     {
00211         LASS_THROW_EX(DegenerationError, iMessage << '\n' << iLocus);
00212     }
00213 };
00214 */
00215 
00216 }
00217 
00218 }
00219 
00220 }
00221 
00222 
00223 
00224 #endif
00225 
00226 // EOF

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