1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
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. *
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. *
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 ***************************************************************************/
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
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) \
41 template <> inline float NAME<float>(float arg) \
45 template <> inline double NAME<double>(double arg) \
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) \
58 template <> inline double NAME<double>(double arg) \
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) \
69 return FLOAT(arg1, arg2); \
71 template <> inline double NAME<double>(double arg1, double arg2) \
73 return DOUBLE(arg1, arg2); \
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.
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
)
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
)
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
)
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
)
154 /// Zero in different types
157 template <typename T
>
164 #endif // _maths_TemplateMaths_h