vmmlib  1.8.0
Templatized C++ vector and matrix math library
 All Classes Namespaces Files Functions Variables Typedefs Pages
math.hpp
1 /*
2  * Copyright (c) 2006-2014, Visualization and Multimedia Lab,
3  * University of Zurich <http://vmml.ifi.uzh.ch>,
4  * Eyescale Software GmbH,
5  * Blue Brain Project, EPFL
6  *
7  * This file is part of VMMLib <https://github.com/VMML/vmmlib/>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * Redistributions of source code must retain the above copyright notice, this
13  * list of conditions and the following disclaimer. Redistributions in binary
14  * form must reproduce the above copyright notice, this list of conditions and
15  * the following disclaimer in the documentation and/or other materials provided
16  * with the distribution. Neither the name of the Visualization and Multimedia
17  * Lab, University of Zurich nor the names of its contributors may be used to
18  * endorse or promote products derived from this software without specific prior
19  * written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #ifndef __VMML__MATH__HPP__
33 #define __VMML__MATH__HPP__
34 
35 #include <cmath>
36 
37 #ifndef M_PI
38 # define M_PI 3.14159265358979323846
39 #endif
40 
41 namespace vmml
42 {
43 
44 namespace math
45 {
46 // helpers for certain cmath functions
47 
48 
49 template< class T >
50 inline T squared( const T a )
51 {
52  return ( a == 0.0 ) ? 0.0 : a * a;
53 }
54 
55 
56 
57 /*
58  * Computes (a2 + b2)1/2 without destructive underflow or overflow.
59  */
60 template< class T >
61 inline T pythag( T a, T b )
62 {
63  a = std::abs(a);
64  b = std::abs(b);
65  if ( a > b )
66  return a * std::sqrt( 1.0 + squared( b / a ) );
67  else
68  return ( b == 0.0 ) ? 0.0 : b * sqrt( 1.0 + squared( a / b ) );
69 }
70 
71 
72 
73 template< class T >
74 inline T sign( T a, T b )
75 {
76  return ( b >= 0.0 ) ? std::abs( a ) : -std::abs( a );
77 }
78 
79 
80 
81 template< typename T >
82 struct abs_less
83 {
84  T operator()( const T& a, const T& b )
85  {
86  return std::abs(a) < std::abs( b );
87  }
88 };
89 
90 
91 template< typename T >
93 {
94  T operator()( const T& a, const T& b )
95  {
96  return std::abs(a) > std::abs( b );
97  }
98 };
99 
100 
101 } // namespace math
102 
103 } // namespace vmml
104 
105 #endif