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