OpenWalnut  1.4.0
WMath.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
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.
14 //
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.
19 //
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/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WMATH_H
26 #define WMATH_H
27 
28 #include <cmath>
29 
30 #include <boost/math/constants/constants.hpp>
31 
32 #include "WLine.h"
33 #include "WPlane.h"
34 #include "linearAlgebra/WPosition.h"
35 
36 /**
37  * Classes and functions of math module of OpenWalnut.
38  */
39 
40 // Pi constants - we don't use the macro M_PI, because it is not part of the C++-standard.
41 // ref.: http://stackoverflow.com/questions/1727881/how-to-use-the-pi-constant-in-c
42 /**
43  * The pi constant in float format
44  */
45 const float piFloat = boost::math::constants::pi<float>();
46 
47 /**
48  * The pi constant in double format
49  */
50 const double piDouble = boost::math::constants::pi<double>();
51 
52 /**
53  * Checks if the triangle intersects with the given plane. If you are interested in the points of
54  * intersection if any \see intersection().
55  *
56  * \param p1 first point of the triangle
57  * \param p2 second point of the triangle
58  * \param p3 third point of the triangle
59  * \param p The plane to test with
60  *
61  * \return True if both intersects otherwise false.
62  */
63 bool testIntersectTriangle( const WPosition& p1, const WPosition& p2, const WPosition& p3, const WPlane& p );
64 
65 /**
66  * Checks if the given segment intersects with the plane or not. Even if
67  * just one endpoint intersects with the plane it should be returned as
68  * point of intersection. If the segment is totally inside of that plane
69  * the first endpoint (which was given: p1 ) should be returned in the
70  * cutPoint parameter.
71  *
72  * \param p The plane to test with intersection
73  * \param p1 The first endpoint of the line segment
74  * \param p2 The second endpoint of the line segment
75  * \param pointOfIntersection The point of intersection if any, otherwise 0,0,0
76  *
77  * \return True if an intersection was detected, false otherwise.
78  */
79 bool intersectPlaneSegment( const WPlane& p,
80  const WPosition& p1,
81  const WPosition& p2,
82  boost::shared_ptr< WPosition > pointOfIntersection );
83 
84 /**
85  * Checks a line (consecutive line segments) on intersection with a plane
86  * and selects (if there are more than one point of intersection) the
87  * closest to the base point of the plane.
88  *
89  * \param p The plane to test with intersection
90  * \param l The line segments
91  * \param cutPoint The return parameter for the point of intersection
92  *
93  * \return True if an intersection was detected, false otherwise.
94  */
95 bool intersectPlaneLineNearCP( const WPlane& p, const WLine& l, boost::shared_ptr< WPosition > cutPoint );
96 
97 /**
98  * Computes the signum for the given value.
99  *
100  * \tparam Type for which must support operator< 0, and operator> 0
101  * \param value To compute signum for
102  *
103  * \return The signum of the value so that signum( val ) * val == std::abs( val );
104  */
105 template< typename T > int signum( const T& value );
106 
107 /**
108  * Calculates the odd factorial. This means 1*3*5* ... * border if border is odd, or 1*3*5* ... * (border-1) if border is even.
109  * \param border the threshold for the factorial calculation.
110  */
111 inline unsigned int oddFactorial( unsigned int border )
112 {
113  unsigned int result = 1;
114  for( unsigned int i = 3; i <= border; i+=2 )
115  {
116  result *= i;
117  }
118  return result;
119 }
120 
121 /**
122  * 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.
123  * \param border the threshold for the factorial calculation.
124  */
125 inline unsigned int evenFactorial( unsigned int border )
126 {
127  unsigned int result = 1;
128  for( unsigned int i = 2; i <= border; i+=2 )
129  {
130  result *= i;
131  }
132  return result;
133 }
134 
135 /**
136  * Calculates the factorial i! for positive i.
137  *
138  * \note For i < 0, the result is undefined.
139  *
140  * \tparam The type of i.
141  * \param i The input.
142  * \return i!.
143  */
144 template< typename T >
145 T factorial( T i )
146 {
147  T res = static_cast< T >( 1 );
148  if( i < res )
149  {
150  return res;
151  }
152  for( T k = res; k <= i; ++k )
153  {
154  res *= k;
155  }
156  return res;
157 }
158 
159 /**
160  * Checks if two values are equal within a given delta.
161  *
162  * \tparam The floating point type.
163  * \param a The first value.
164  * \param b The second value.
165  * \param delta The tolerance parameter.
166  * \return True if both values are equal within the delta, otherwise false.
167  */
168 template< typename T >
169 T areEqual( T a, T b, T delta = T( 0 ) )
170 {
171  return ( std::fabs( a - b ) <= delta );
172 }
173 
174 template< typename T > inline int signum( const T& value )
175 {
176  if( value < 0 )
177  {
178  return -1;
179  }
180  else if( value > 0 )
181  {
182  return 1;
183  }
184  return 0;
185 }
186 
187 #endif // WMATH_H
A line is an ordered sequence of WPositions.
Definition: WLine.h:41
This only is a 3d double vector.