vmmlib  1.9.0
Templatized C++ vector and matrix math library
util.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 
33 #ifndef __VMML__UTIL__HPP__
34 #define __VMML__UTIL__HPP__
35 
36 #include "vector.hpp"
37 #include "matrix.hpp"
38 
39 #include <algorithm>
40 #include <limits>
41 #include <cmath>
42 
43 namespace vmml
44 {
45 
46 #define M_PI_F 3.14159265358979323846f
47 #define M_PI_2_F 1.57079632679489661923f
48 #define M_PI_3_F 1.04719755119659774615f
49 #define M_PI_4_F 0.78539816339744830962f
50 
51 
52 template< typename T, typename U >
53 bool
54 equals( T arg0, U arg1)
55 {
56  return fabs( arg0 - arg1 ) < std::numeric_limits< float >::epsilon();
57 }
58 
59 
60 // matrix convenience functions
61 
62 
63 template< size_t M, size_t N, typename T >
64 inline matrix< M, N, T >
65 create_translation( const vector< M - 1, T > &arg )
66 {
67  matrix< M, N, T > ret;
68  identity( ret );
69  ret.set_translation( arg );
70 
71  return ret;
72 }
73 
74 template< typename T >
75 inline matrix< 4, 4, T >
76 create_translation( const vector< 3, T > &arg )
77 {
78  return create_translation< 4, 4 >(arg);
79 }
80 
81 template< size_t M, size_t N, typename T >
82 inline void
83 apply_translation( matrix< M, N, T > &m, T t0, T t1, T t2 )
84 {
85  m *= create_translation< M, N, T >( vector< M - 1, T >( t0, t1, t2 ) );
86 }
87 
88 template< size_t M, size_t N, typename T >
89 inline matrix< M, N, T >
90 create_rotation( T angle, const vector< M - 1, T > &axis )
91 {
92  matrix< M, N, T > ret;
93  identity( ret );
94  ret.rotate( angle, axis );
95 
96  return ret;
97 }
98 
99 template< typename T >
100 inline matrix< 4, 4, T >
101 create_rotation( T angle, const vector< 3, T > &axis )
102 {
103  return create_rotation< 4, 4 >( angle, axis );
104 }
105 
106 template< size_t M, size_t N, typename T >
107 inline void
108 apply_rotation( matrix< M, N, T > &m, T angle, T t0, T t1, T t2 )
109 {
110  m *= create_rotation< M, N, T >( angle, vector< M - 1, T >( t0, t1, t2 ) );
111 }
112 
113 template< size_t M, size_t N, typename T >
114 inline matrix< M, N, T >
115 create_scaling( const vector< N - 1, T > &arg )
116 {
117  matrix< M, N, T > ret;
118  identity(ret);
119  ret.scale(arg);
120 
121  return ret;
122 }
123 
124 template< typename T >
125 inline matrix< 4, 4, T >
126 const
127 create_scaling( const vector< 3, T > &arg )
128 {
129  return create_scaling< 4, 4 >(arg);
130 }
131 
132 template< typename T >
133 inline matrix< 4, 4, T >
134 create_scaling( T arg )
135 {
136  return create_scaling< 4, 4 >( vector< 3, T >( arg ) );
137 }
138 
139 
140 // vector convenience functions
141 
142 
143 template< size_t M, typename T >
144 inline void zero( vector< M, T > &arg )
145 {
146  std::fill( arg.begin(), arg.end(), 0 );
147 }
148 
149 template< size_t M, typename T >
150 vector< M, T > min( const vector< M, T > &arg0, const vector< M, T > &arg1 )
151 {
152  vector< M, T > ret;
153 
154  for( size_t i = 0; i < M; ++i )
155  ret[i] = std::min( arg0[i], arg1[i] );
156 
157  return ret;
158 }
159 
160 template< size_t M, typename T >
161 vector< M, T > max( const vector< M, T > &arg0, const vector< M, T > &arg1 )
162 {
163  vector< M, T > ret;
164 
165  for( size_t i = 0; i < M; ++i )
166  ret[i] = std::max( arg0[i], arg1[i] );
167 
168  return ret;
169 }
170 
171 template< size_t M, typename T >
172 T
173 manhattan( const vector< M, T > &arg )
174 {
175  T ret = 0;
176  for( size_t i = 0; i < M; ++i )
177  {
178  ret += std::abs(arg[i]);
179  }
180 
181  return ret;
182 }
183 
184 } // namespace vmml
185 
186 #endif
heavily inspired by boost::enable_if http://www.boost.org, file: boost/utility/enable_if.hpp, Copyright 2003 Jaakko Järvi, Jeremiah Willcock, Andrew Lumsdaine
Definition: aabb.hpp:38