vmmlib  1.10.0
Templatized C++ vector and matrix math library
util.hpp
1 /*
2  * Copyright (c) 2006-2016, 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 <cstdlib>
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 > bool equals( T arg0, U arg1)
53 {
54  return std::abs( arg0 - arg1 ) < std::numeric_limits< float >::epsilon();
55 }
56 
57 
58 // matrix convenience functions
59 
60 
61 template< size_t M, size_t N, typename T >
62 inline matrix< M, N, T >
63 create_translation( const vector< M - 1, T > &arg )
64 {
65  matrix< M, N, T > ret;
66  identity( ret );
67  ret.set_translation( arg );
68 
69  return ret;
70 }
71 
72 template< typename T >
73 inline matrix< 4, 4, T >
74 create_translation( const vector< 3, T > &arg )
75 {
76  return create_translation< 4, 4 >(arg);
77 }
78 
79 template< size_t M, size_t N, typename T >
80 inline void
81 apply_translation( matrix< M, N, T > &m, T t0, T t1, T t2 )
82 {
83  m *= create_translation< M, N, T >( vector< M - 1, T >( t0, t1, t2 ) );
84 }
85 
86 template< size_t M, size_t N, typename T >
87 inline matrix< M, N, T >
88 create_rotation( T angle, const vector< M - 1, T > &axis )
89 {
90  matrix< M, N, T > ret;
91  identity( ret );
92  ret.rotate( angle, axis );
93 
94  return ret;
95 }
96 
97 template< typename T >
98 inline matrix< 4, 4, T >
99 create_rotation( T angle, const vector< 3, T > &axis )
100 {
101  return create_rotation< 4, 4 >( angle, axis );
102 }
103 
104 template< size_t M, size_t N, typename T >
105 inline void
106 apply_rotation( matrix< M, N, T > &m, T angle, T t0, T t1, T t2 )
107 {
108  m *= create_rotation< M, N, T >( angle, vector< M - 1, T >( t0, t1, t2 ) );
109 }
110 
111 template< size_t M, size_t N, typename T >
112 inline matrix< M, N, T >
113 create_scaling( const vector< N - 1, T > &arg )
114 {
115  matrix< M, N, T > ret;
116  identity(ret);
117  ret.scale(arg);
118 
119  return ret;
120 }
121 
122 template< typename T >
123 inline matrix< 4, 4, T >
124 const
125 create_scaling( const vector< 3, T > &arg )
126 {
127  return create_scaling< 4, 4 >(arg);
128 }
129 
130 template< typename T >
131 inline matrix< 4, 4, T >
132 create_scaling( T arg )
133 {
134  return create_scaling< 4, 4 >( vector< 3, T >( arg ) );
135 }
136 
137 
138 // vector convenience functions
139 
140 
141 template< size_t M, typename T >
142 inline void zero( vector< M, T > &arg )
143 {
144  std::fill( arg.begin(), arg.end(), 0 );
145 }
146 
147 template< size_t M, typename T >
148 vector< M, T > min( const vector< M, T > &arg0, const vector< M, T > &arg1 )
149 {
150  vector< M, T > ret;
151 
152  for( size_t i = 0; i < M; ++i )
153  ret[i] = std::min( arg0[i], arg1[i] );
154 
155  return ret;
156 }
157 
158 template< size_t M, typename T >
159 vector< M, T > max( const vector< M, T > &arg0, const vector< M, T > &arg1 )
160 {
161  vector< M, T > ret;
162 
163  for( size_t i = 0; i < M; ++i )
164  ret[i] = std::max( arg0[i], arg1[i] );
165 
166  return ret;
167 }
168 
169 template< size_t M, typename T >
170 T
171 manhattan( const vector< M, T > &arg )
172 {
173  T ret = 0;
174  for( size_t i = 0; i < M; ++i )
175  {
176  ret += std::abs(arg[i]);
177  }
178 
179  return ret;
180 }
181 
182 } // namespace vmml
183 
184 #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:40