func_geometric.inl 6.86 KB
Newer Older
1 2 3
///////////////////////////////////////////////////////////////////////////////////
/// OpenGL Mathematics (glm.g-truc.net)
///
Robert Menzel's avatar
Robert Menzel committed
4
/// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net)
5 6 7 8 9 10 11 12 13 14
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
/// 
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
/// 
Robert Menzel's avatar
Robert Menzel committed
15 16 17 18
/// Restrictions:
///		By making use of the Software for military purposes, you choose to make
///		a Bunny unhappy.
/// 
19 20 21 22 23 24 25 26 27
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
///
/// @ref core
Robert Menzel's avatar
Robert Menzel committed
28
/// @file glm/detail/func_geometric.inl
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/// @date 2008-08-03 / 2011-06-15
/// @author Christophe Riccio
///////////////////////////////////////////////////////////////////////////////////

#include "func_exponential.hpp"
#include "func_common.hpp"
#include "type_vec2.hpp"
#include "type_vec4.hpp"
#include "type_float.hpp"

namespace glm{
namespace detail
{
	template <template <class, precision> class vecType, typename T, precision P>
	struct compute_dot{};

	template <typename T, precision P>
Robert Menzel's avatar
Robert Menzel committed
46
	struct compute_dot<tvec1, T, P>
47
	{
Robert Menzel's avatar
Robert Menzel committed
48
		GLM_FUNC_QUALIFIER static T call(tvec1<T, P> const & a, tvec1<T, P> const & b)
49
		{
Robert Menzel's avatar
Robert Menzel committed
50
			return a.x * b.x;
51 52 53 54
		}
	};

	template <typename T, precision P>
Robert Menzel's avatar
Robert Menzel committed
55
	struct compute_dot<tvec2, T, P>
56
	{
Robert Menzel's avatar
Robert Menzel committed
57
		GLM_FUNC_QUALIFIER static T call(tvec2<T, P> const & x, tvec2<T, P> const & y)
58
		{
Robert Menzel's avatar
Robert Menzel committed
59
			tvec2<T, P> tmp(x * y);
60 61 62 63 64
			return tmp.x + tmp.y;
		}
	};

	template <typename T, precision P>
Robert Menzel's avatar
Robert Menzel committed
65
	struct compute_dot<tvec3, T, P>
66
	{
Robert Menzel's avatar
Robert Menzel committed
67
		GLM_FUNC_QUALIFIER static T call(tvec3<T, P> const & x, tvec3<T, P> const & y)
68
		{
Robert Menzel's avatar
Robert Menzel committed
69
			tvec3<T, P> tmp(x * y);
70 71 72 73 74
			return tmp.x + tmp.y + tmp.z;
		}
	};

	template <typename T, precision P>
Robert Menzel's avatar
Robert Menzel committed
75
	struct compute_dot<tvec4, T, P>
76
	{
Robert Menzel's avatar
Robert Menzel committed
77
		GLM_FUNC_QUALIFIER static T call(tvec4<T, P> const & x, tvec4<T, P> const & y)
78
		{
Robert Menzel's avatar
Robert Menzel committed
79
			tvec4<T, P> tmp(x * y);
80 81 82 83 84 85 86
			return (tmp.x + tmp.y) + (tmp.z + tmp.w);
		}
	};
}//namespace detail

	// length
	template <typename genType>
Robert Menzel's avatar
Robert Menzel committed
87
	GLM_FUNC_QUALIFIER genType length(genType x)
88 89 90
	{
		GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'length' only accept floating-point inputs");

Robert Menzel's avatar
Robert Menzel committed
91
		return abs(x);
92 93
	}

Robert Menzel's avatar
Robert Menzel committed
94 95
	template <typename T, precision P, template <typename, precision> class vecType>
	GLM_FUNC_QUALIFIER T length(vecType<T, P> const & v)
96 97 98
	{
		GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'length' only accept floating-point inputs");

Robert Menzel's avatar
Robert Menzel committed
99
		return sqrt(dot(v, v));
100 101 102 103
	}

	// distance
	template <typename genType>
Robert Menzel's avatar
Robert Menzel committed
104
	GLM_FUNC_QUALIFIER genType distance(genType const & p0, genType const & p1)
105 106 107 108 109 110
	{
		GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'distance' only accept floating-point inputs");

		return length(p1 - p0);
	}

Robert Menzel's avatar
Robert Menzel committed
111 112
	template <typename T, precision P, template <typename, precision> class vecType>
	GLM_FUNC_QUALIFIER T distance(vecType<T, P> const & p0, vecType<T, P> const & p1)
113 114 115 116 117 118
	{
		return length(p1 - p0);
	}

	// dot
	template <typename T>
Robert Menzel's avatar
Robert Menzel committed
119
	GLM_FUNC_QUALIFIER T dot(T x, T y)
120 121
	{
		GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'dot' only accept floating-point inputs");
Robert Menzel's avatar
Robert Menzel committed
122
		return x * y;
123 124 125
	}

	template <typename T, precision P, template <typename, precision> class vecType>
Robert Menzel's avatar
Robert Menzel committed
126
	GLM_FUNC_QUALIFIER T dot(vecType<T, P> const & x, vecType<T, P> const & y)
127 128 129 130 131 132 133
	{
		GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'dot' only accept floating-point inputs");
		return detail::compute_dot<vecType, T, P>::call(x, y);
	}

	// cross
	template <typename T, precision P>
Robert Menzel's avatar
Robert Menzel committed
134
	GLM_FUNC_QUALIFIER tvec3<T, P> cross(tvec3<T, P> const & x, tvec3<T, P> const & y)
135 136 137
	{
		GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'cross' only accept floating-point inputs");

Robert Menzel's avatar
Robert Menzel committed
138
		return tvec3<T, P>(
139 140 141 142 143 144 145
			x.y * y.z - y.y * x.z,
			x.z * y.x - y.z * x.x,
			x.x * y.y - y.x * x.y);
	}

	// normalize
	template <typename genType>
Robert Menzel's avatar
Robert Menzel committed
146
	GLM_FUNC_QUALIFIER genType normalize(genType const & x)
147 148 149 150 151 152
	{
		GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'normalize' only accept floating-point inputs");

		return x < genType(0) ? genType(-1) : genType(1);
	}

Robert Menzel's avatar
Robert Menzel committed
153 154
	template <typename T, precision P, template <typename, precision> class vecType>
	GLM_FUNC_QUALIFIER vecType<T, P> normalize(vecType<T, P> const & x)
155 156 157
	{
		GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'normalize' only accept floating-point inputs");

Robert Menzel's avatar
Robert Menzel committed
158
		return x * inversesqrt(dot(x, x));
159 160
	}

Robert Menzel's avatar
Robert Menzel committed
161 162 163
	// faceforward
	template <typename genType>
	GLM_FUNC_QUALIFIER genType faceforward(genType const & N, genType const & I, genType const & Nref)
164
	{
Robert Menzel's avatar
Robert Menzel committed
165
		return dot(Nref, I) < static_cast<genType>(0) ? N : -N;
166 167
	}

Robert Menzel's avatar
Robert Menzel committed
168 169
	template <typename T, precision P, template <typename, precision> class vecType>
	GLM_FUNC_QUALIFIER vecType<T, P> faceforward(vecType<T, P> const & N, vecType<T, P> const & I, vecType<T, P> const & Nref)
170
	{
Robert Menzel's avatar
Robert Menzel committed
171
		return dot(Nref, I) < static_cast<T>(0) ? N : -N;
172 173 174 175
	}

	// reflect
	template <typename genType>
Robert Menzel's avatar
Robert Menzel committed
176
	GLM_FUNC_QUALIFIER genType reflect(genType const & I, genType const & N)
177
	{
Robert Menzel's avatar
Robert Menzel committed
178
		return I - N * dot(N, I) * static_cast<genType>(2);
179 180 181 182
	}

	// refract
	template <typename genType>
Robert Menzel's avatar
Robert Menzel committed
183
	GLM_FUNC_QUALIFIER genType refract(genType const & I, genType const & N, genType const & eta)
184 185 186
	{
		GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'refract' only accept floating-point inputs");

Robert Menzel's avatar
Robert Menzel committed
187 188 189
		genType const dotValue(dot(N, I));
		genType const k(static_cast<genType>(1) - eta * eta * (static_cast<genType>(1) - dotValue * dotValue));
		return (eta * I - (eta * dotValue + sqrt(k)) * N) * static_cast<genType>(k >= static_cast<genType>(0));
190 191 192
	}

	template <typename T, precision P, template <typename, precision> class vecType>
Robert Menzel's avatar
Robert Menzel committed
193
	GLM_FUNC_QUALIFIER vecType<T, P> refract(vecType<T, P> const & I, vecType<T, P> const & N, T eta)
194 195 196
	{
		GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'refract' only accept floating-point inputs");

Robert Menzel's avatar
Robert Menzel committed
197 198 199
		T const dotValue(dot(N, I));
		T const k(static_cast<T>(1) - eta * eta * (static_cast<T>(1) - dotValue * dotValue));
		return (eta * I - (eta * dotValue + std::sqrt(k)) * N) * static_cast<T>(k >= static_cast<T>(0));
200 201
	}
}//namespace glm