tcElevationOptimization::loadSnapShot: simplify reliability expression
[tecorrec.git] / maths / TemplateMaths.h
blob8cd3097ba6c7f1318264861d4654783c1a2ca13c
1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * Tecorrec is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * Tecorrec is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with Tecorrec. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file TemplateMaths.h
22 * @brief Template maths functions, some unecessary.
23 * @author James Hogan <james@albanarts.com>
24 * @note Copyright (C) 2007
26 * @version 1 : From Computer Graphics and Visualisation Open Assessment.
29 #ifndef _maths_TemplateMaths_h
30 #define _maths_TemplateMaths_h
32 #include <cmath>
34 // Defines template functions for merging fp functions with 1 argument
35 #define DEFINE_FP1_TEMPLATE_FUNCTIONS_SAFE(NAME, FLOAT, DOUBLE) \
36 template <typename T> \
37 inline T NAME(T arg) \
38 { \
39 return arg.NAME(); \
40 } \
41 template <> inline float NAME<float>(float arg) \
42 { \
43 return FLOAT(arg); \
44 } \
45 template <> inline double NAME<double>(double arg) \
46 { \
47 return DOUBLE(arg); \
48 } \
50 // Defines template functions for merging fp functions with 1 argument
51 #define DEFINE_FP1_TEMPLATE_FUNCTIONS(NAME, FLOAT, DOUBLE) \
52 template <typename T> \
53 inline T NAME(T arg); \
54 template <> inline float NAME<float>(float arg) \
55 { \
56 return FLOAT(arg); \
57 } \
58 template <> inline double NAME<double>(double arg) \
59 { \
60 return DOUBLE(arg); \
61 } \
63 // Defines template functions for merging fp functions with 2 arguments
64 #define DEFINE_FP2_TEMPLATE_FUNCTIONS(NAME, FLOAT, DOUBLE) \
65 template <typename T> \
66 inline T NAME(T arg1, T arg2); \
67 template <> inline float NAME<float>(float arg1, float arg2) \
68 { \
69 return FLOAT(arg1, arg2); \
70 } \
71 template <> inline double NAME<double>(double arg1, double arg2) \
72 { \
73 return DOUBLE(arg1, arg2); \
74 } \
76 namespace maths {
77 DEFINE_FP1_TEMPLATE_FUNCTIONS_SAFE(sqrt, std::sqrt, std::sqrt)
79 DEFINE_FP1_TEMPLATE_FUNCTIONS(sin, std::sin, std::sin)
80 DEFINE_FP1_TEMPLATE_FUNCTIONS(cos, std::cos, std::cos)
81 DEFINE_FP1_TEMPLATE_FUNCTIONS(tan, std::tan, std::tan)
83 DEFINE_FP1_TEMPLATE_FUNCTIONS(asin, std::asin, std::asin)
84 DEFINE_FP1_TEMPLATE_FUNCTIONS(acos, std::acos, std::acos)
85 DEFINE_FP1_TEMPLATE_FUNCTIONS(atan, std::atan, std::atan)
87 DEFINE_FP2_TEMPLATE_FUNCTIONS(atan2, std::atan2, std::atan2)
89 // Cleanup internal preprocessor definitions.
90 #undef DEFINE_FP1_TEMPLATE_FUNCTIONS
91 #undef DEFINE_FP2_TEMPLATE_FUNCTIONS
93 /// Write values into a 2 dimentional vector.
94 /**
95 * @param T Data type of vector components.
96 * @param vec Vector pointer to array of @a T.
97 * @param x X coordinate of vector.
98 * @param y Y coordinate of vector.
100 template <typename T>
101 inline T * setVector2(const T * vec, const T x, const T y)
103 vec[0] = x;
104 vec[1] = y;
105 return vec;
108 /// Write values into a 3 dimentional vector.
110 * @param T Data type of vector components.
111 * @param vec Vector pointer to array of @a T.
112 * @param x X coordinate of vector.
113 * @param y Y coordinate of vector.
114 * @param z Z coordinate of vector.
116 template <typename T>
117 inline T * setVector3(T * const vec, const T x, const T y, const T z)
119 vec[0] = x;
120 vec[1] = y;
121 vec[2] = z;
122 return vec;
125 /// Write values into a 3 dimentional vector.
127 * @param T Data type of vector components.
128 * @param vec Vector pointer to array of @a T.
129 * @param x X coordinate of vector.
130 * @param y Y coordinate of vector.
131 * @param z Z coordinate of vector.
133 template <typename T>
134 inline T * setVector4(T * const vec, const T x, const T y, const T z, const T w)
136 vec[0] = x;
137 vec[1] = y;
138 vec[2] = z;
139 vec[3] = w;
140 return vec;
144 /// Find the square of a number
146 * Useful when the expression is complex and a macro should be avoided
148 template <typename T>
149 inline T sqr(T value)
151 return value*value;
154 /// Zero in different types
157 template <typename T>
158 inline T zero()
160 return (T)0;
164 #endif // _maths_TemplateMaths_h