~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/core/common/math/WMath.h

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#if defined ( _MSC_VER )
 
29
    #include <float.h>
 
30
#endif
 
31
 
 
32
#include <cmath>
 
33
 
 
34
#include <boost/math/constants/constants.hpp>
 
35
 
 
36
#include "../WExportCommon.h"
 
37
#include "WLine.h"
 
38
#include "WPlane.h"
 
39
#include "linearAlgebra/WLinearAlgebra.h"
 
40
 
 
41
/**
 
42
 * Classes and functions of math module of OpenWalnut.
 
43
 */
 
44
 
 
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
 
47
/**
 
48
 * The pi constant in float format
 
49
 */
 
50
const float piFloat = boost::math::constants::pi<float>();
 
51
 
 
52
/**
 
53
 * The pi constant in double format
 
54
 */
 
55
const double piDouble = boost::math::constants::pi<double>();
 
56
 
 
57
/**
 
58
 * Tests whether the number stored in the parameter is finite.
 
59
 * \param number the number to be tested
 
60
 */
 
61
inline int myIsfinite( double number )
 
62
{
 
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);
 
69
#else
 
70
    WAssert( false, "isfinite not provided on this platform or platform not known." );
 
71
#endif
 
72
}
 
73
/**
 
74
 * Checks if the triangle intersects with the given plane. If you are interested in the points of
 
75
 * intersection if any \see intersection().
 
76
 *
 
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
 
81
 *
 
82
 * \return True if both intersects otherwise false.
 
83
 */
 
84
bool OWCOMMON_EXPORT testIntersectTriangle( const WPosition& p1, const WPosition& p2, const WPosition& p3, const WPlane& p );
 
85
 
 
86
/**
 
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
 
91
 * cutPoint parameter.
 
92
 *
 
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
 
97
 *
 
98
 * \return True if an intersection was detected, false otherwise.
 
99
 */
 
100
bool OWCOMMON_EXPORT intersectPlaneSegment( const WPlane& p,
 
101
        const WPosition& p1,
 
102
        const WPosition& p2,
 
103
        boost::shared_ptr< WPosition > pointOfIntersection );
 
104
 
 
105
/**
 
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.
 
109
 *
 
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
 
113
 *
 
114
 * \return True if an intersection was detected, false otherwise.
 
115
 */
 
116
bool OWCOMMON_EXPORT intersectPlaneLineNearCP( const WPlane& p, const WLine& l, boost::shared_ptr< WPosition > cutPoint );
 
117
 
 
118
/**
 
119
 * Computes the signum for the given value.
 
120
 *
 
121
 * \tparam Type for which must support operator< 0, and operator> 0
 
122
 * \param value To compute signum for
 
123
 *
 
124
 * \return The signum of the value so that signum( val ) * val == std::abs( val );
 
125
 */
 
126
template< typename T > int signum( const T& value );
 
127
 
 
128
/**
 
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.
 
131
 */
 
132
inline unsigned int OWCOMMON_EXPORT oddFactorial( unsigned int border )
 
133
{
 
134
    unsigned int result = 1;
 
135
    for( unsigned int i = 3; i <= border; i+=2 )
 
136
    {
 
137
        result *= i;
 
138
    }
 
139
    return result;
 
140
}
 
141
 
 
142
/**
 
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.
 
145
 */
 
146
inline unsigned int OWCOMMON_EXPORT evenFactorial( unsigned int border )
 
147
{
 
148
    unsigned int result = 1;
 
149
    for( unsigned int i = 2; i <= border; i+=2 )
 
150
    {
 
151
        result *= i;
 
152
    }
 
153
    return result;
 
154
}
 
155
 
 
156
/**
 
157
 * Calculates the factorial i! for positive i.
 
158
 *
 
159
 * \note For i < 0, the result is undefined.
 
160
 *
 
161
 * \tparam The type of i.
 
162
 * \param i The input.
 
163
 * \return i!.
 
164
 */
 
165
template< typename T >
 
166
T factorial( T i )
 
167
{
 
168
    T res = static_cast< T >( 1 );
 
169
    if( i < res )
 
170
    {
 
171
        return res;
 
172
    }
 
173
    for( T k = res; k <= i; ++k )
 
174
    {
 
175
        res *= k;
 
176
    }
 
177
    return res;
 
178
}
 
179
 
 
180
template< typename T > inline int signum( const T& value )
 
181
{
 
182
    if( value < 0 )
 
183
    {
 
184
        return -1;
 
185
    }
 
186
    else if( value > 0 )
 
187
    {
 
188
        return 1;
 
189
    }
 
190
    return 0;
 
191
}
 
192
 
 
193
#endif  // WMATH_H