OpenWalnut 1.2.5
WMath.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WMATH_H
00026 #define WMATH_H
00027 
00028 #if defined ( _MSC_VER )
00029     #include <float.h>
00030 #endif
00031 
00032 #include <cmath>
00033 
00034 #include <boost/math/constants/constants.hpp>
00035 
00036 #include "../WExportCommon.h"
00037 #include "WLine.h"
00038 #include "WPlane.h"
00039 #include "linearAlgebra/WLinearAlgebra.h"
00040 
00041 /**
00042  * Classes and functions of math module of OpenWalnut.
00043  */
00044 
00045 // Pi constants - we don't use the macro M_PI, because it is not part of the C++-standard.
00046 // ref.: http://stackoverflow.com/questions/1727881/how-to-use-the-pi-constant-in-c
00047 /**
00048  * The pi constant in float format
00049  */
00050 const float piFloat = boost::math::constants::pi<float>();
00051 
00052 /**
00053  * The pi constant in double format
00054  */
00055 const double piDouble = boost::math::constants::pi<double>();
00056 
00057 /**
00058  * Tests whether the number stored in the parameter is finite.
00059  * \param number the number to be tested
00060  */
00061 inline int myIsfinite( double number )
00062 {
00063 #if defined( __linux__ ) || defined( __APPLE__ )
00064     // C99 defines isfinite() as a macro.
00065     return std::isfinite(number);
00066 #elif defined( _WIN32 )
00067     // Microsoft Visual C++ and Borland C++ Builder use _finite().
00068     return _finite(number);
00069 #else
00070     WAssert( false, "isfinite not provided on this platform or platform not known." );
00071 #endif
00072 }
00073 /**
00074  * Checks if the triangle intersects with the given plane. If you are interested in the points of
00075  * intersection if any \see intersection().
00076  *
00077  * \param p1 first point of the triangle
00078  * \param p2 second point of the triangle
00079  * \param p3 third point of the triangle
00080  * \param p The plane to test with
00081  *
00082  * \return True if both intersects otherwise false.
00083  */
00084 bool OWCOMMON_EXPORT testIntersectTriangle( const WPosition& p1, const WPosition& p2, const WPosition& p3, const WPlane& p );
00085 
00086 /**
00087  * Checks if the given segment intersects with the plane or not. Even if
00088  * just one endpoint intersects with the plane it should be returned as
00089  * point of intersection. If the segment is totally inside of that plane
00090  * the first endpoint (which was given: p1 ) should be returned in the
00091  * cutPoint parameter.
00092  *
00093  * \param p The plane to test with intersection
00094  * \param p1 The first endpoint of the line segment
00095  * \param p2 The second endpoint of the line segment
00096  * \param pointOfIntersection The point of intersection if any, otherwise 0,0,0
00097  *
00098  * \return True if an intersection was detected, false otherwise.
00099  */
00100 bool OWCOMMON_EXPORT intersectPlaneSegment( const WPlane& p,
00101         const WPosition& p1,
00102         const WPosition& p2,
00103         boost::shared_ptr< WPosition > pointOfIntersection );
00104 
00105 /**
00106  * Checks a line (consecutive line segments) on intersection with a plane
00107  * and selects (if there are more than one point of intersection) the
00108  * closest to the base point of the plane.
00109  *
00110  * \param p The plane to test with intersection
00111  * \param l The line segments
00112  * \param cutPoint The return parameter for the point of intersection
00113  *
00114  * \return True if an intersection was detected, false otherwise.
00115  */
00116 bool OWCOMMON_EXPORT intersectPlaneLineNearCP( const WPlane& p, const WLine& l, boost::shared_ptr< WPosition > cutPoint );
00117 
00118 /**
00119  * Computes the signum for the given value.
00120  *
00121  * \tparam Type for which must support operator< 0, and operator> 0
00122  * \param value To compute signum for
00123  *
00124  * \return The signum of the value so that signum( val ) * val == std::abs( val );
00125  */
00126 template< typename T > int signum( const T& value );
00127 
00128 /**
00129  * Calculates the odd factorial. This means 1*3*5* ... * border if border is odd, or 1*3*5* ... * (border-1) if border is even.
00130  * \param border the threshold for the factorial calculation.
00131  */
00132 inline unsigned int OWCOMMON_EXPORT oddFactorial( unsigned int border )
00133 {
00134     unsigned int result = 1;
00135     for( unsigned int i = 3; i <= border; i+=2 )
00136     {
00137         result *= i;
00138     }
00139     return result;
00140 }
00141 
00142 /**
00143  * Calculates the even factorial. This means 2*4*6 ... * \param border if border is even, or 2*4*6* ... * ( \param border - 1 ) if border is odd.
00144  * \param border the threshold for the factorial calculation.
00145  */
00146 inline unsigned int OWCOMMON_EXPORT evenFactorial( unsigned int border )
00147 {
00148     unsigned int result = 1;
00149     for( unsigned int i = 2; i <= border; i+=2 )
00150     {
00151         result *= i;
00152     }
00153     return result;
00154 }
00155 
00156 /**
00157  * Calculates the factorial i! for positive i.
00158  *
00159  * \note For i < 0, the result is undefined.
00160  *
00161  * \tparam The type of i.
00162  * \param i The input.
00163  * \return i!.
00164  */
00165 template< typename T >
00166 T factorial( T i )
00167 {
00168     T res = static_cast< T >( 1 );
00169     if( i < res )
00170     {
00171         return res;
00172     }
00173     for( T k = res; k <= i; ++k )
00174     {
00175         res *= k;
00176     }
00177     return res;
00178 }
00179 
00180 template< typename T > inline int signum( const T& value )
00181 {
00182     if( value < 0 )
00183     {
00184         return -1;
00185     }
00186     else if( value > 0 )
00187     {
00188         return 1;
00189     }
00190     return 0;
00191 }
00192 
00193 #endif  // WMATH_H
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends