1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
10
// OpenWalnut is free software: you can redistribute it and/or modify
11
// it under the terms of the GNU Lesser General Public License as published by
12
// the Free Software Foundation, either version 3 of the License, or
13
// (at your option) any later version.
15
// OpenWalnut is distributed in the hope that it will be useful,
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
// GNU Lesser General Public License for more details.
20
// You should have received a copy of the GNU Lesser General Public License
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
23
//---------------------------------------------------------------------------
28
#if defined ( _MSC_VER )
34
#include <boost/math/constants/constants.hpp>
36
#include "../WExportCommon.h"
39
#include "linearAlgebra/WLinearAlgebra.h"
42
* Classes and functions of math module of OpenWalnut.
45
// Pi constants - we don't use the macro M_PI, because it is not part of the C++-standard.
46
// ref.: http://stackoverflow.com/questions/1727881/how-to-use-the-pi-constant-in-c
48
* The pi constant in float format
50
const float piFloat = boost::math::constants::pi<float>();
53
* The pi constant in double format
55
const double piDouble = boost::math::constants::pi<double>();
58
* Tests whether the number stored in the parameter is finite.
59
* \param number the number to be tested
61
inline int myIsfinite( double number )
63
#if defined( __linux__ ) || defined( __APPLE__ )
64
// C99 defines isfinite() as a macro.
65
return std::isfinite(number);
66
#elif defined( _WIN32 )
67
// Microsoft Visual C++ and Borland C++ Builder use _finite().
68
return _finite(number);
70
WAssert( false, "isfinite not provided on this platform or platform not known." );
74
* Checks if the triangle intersects with the given plane. If you are interested in the points of
75
* intersection if any \see intersection().
77
* \param p1 first point of the triangle
78
* \param p2 second point of the triangle
79
* \param p3 third point of the triangle
80
* \param p The plane to test with
82
* \return True if both intersects otherwise false.
84
bool OWCOMMON_EXPORT testIntersectTriangle( const WPosition& p1, const WPosition& p2, const WPosition& p3, const WPlane& p );
87
* Checks if the given segment intersects with the plane or not. Even if
88
* just one endpoint intersects with the plane it should be returned as
89
* point of intersection. If the segment is totally inside of that plane
90
* the first endpoint (which was given: p1 ) should be returned in the
93
* \param p The plane to test with intersection
94
* \param p1 The first endpoint of the line segment
95
* \param p2 The second endpoint of the line segment
96
* \param pointOfIntersection The point of intersection if any, otherwise 0,0,0
98
* \return True if an intersection was detected, false otherwise.
100
bool OWCOMMON_EXPORT intersectPlaneSegment( const WPlane& p,
103
boost::shared_ptr< WPosition > pointOfIntersection );
106
* Checks a line (consecutive line segments) on intersection with a plane
107
* and selects (if there are more than one point of intersection) the
108
* closest to the base point of the plane.
110
* \param p The plane to test with intersection
111
* \param l The line segments
112
* \param cutPoint The return parameter for the point of intersection
114
* \return True if an intersection was detected, false otherwise.
116
bool OWCOMMON_EXPORT intersectPlaneLineNearCP( const WPlane& p, const WLine& l, boost::shared_ptr< WPosition > cutPoint );
119
* Computes the signum for the given value.
121
* \tparam Type for which must support operator< 0, and operator> 0
122
* \param value To compute signum for
124
* \return The signum of the value so that signum( val ) * val == std::abs( val );
126
template< typename T > int signum( const T& value );
129
* Calculates the odd factorial. This means 1*3*5* ... * border if border is odd, or 1*3*5* ... * (border-1) if border is even.
130
* \param border the threshold for the factorial calculation.
132
inline unsigned int OWCOMMON_EXPORT oddFactorial( unsigned int border )
134
unsigned int result = 1;
135
for( unsigned int i = 3; i <= border; i+=2 )
143
* 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.
144
* \param border the threshold for the factorial calculation.
146
inline unsigned int OWCOMMON_EXPORT evenFactorial( unsigned int border )
148
unsigned int result = 1;
149
for( unsigned int i = 2; i <= border; i+=2 )
157
* Calculates the factorial i! for positive i.
159
* \note For i < 0, the result is undefined.
161
* \tparam The type of i.
162
* \param i The input.
165
template< typename T >
168
T res = static_cast< T >( 1 );
173
for( T k = res; k <= i; ++k )
180
template< typename T > inline int signum( const T& value )