func_common.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2011 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. /// THE SOFTWARE.
  22. ///
  23. /// @ref core
  24. /// @file glm/core/func_common.hpp
  25. /// @date 2008-03-08 / 2010-01-26
  26. /// @author Christophe Riccio
  27. ///////////////////////////////////////////////////////////////////////////////////
  28. #ifndef glm_core_func_common
  29. #define glm_core_func_common
  30. #include "_fixes.hpp"
  31. namespace glm
  32. {
  33. /// @addtogroup core_funcs
  34. /// @{
  35. /// Returns x if x >= 0; otherwise, it returns -x.
  36. ///
  37. /// @see
  38. /// @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/abs.xml">GLSL abs man page</a>
  39. /// @li GLSL 1.30.08 specification, section 8.3
  40. template <typename genFIType>
  41. genFIType abs(genFIType const & x);
  42. /// Returns 1.0 if x > 0, 0.0 if x == 0, or -1.0 if x < 0.
  43. ///
  44. /// @see
  45. /// @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/sign.xml">GLSL sign man page</a>
  46. /// @li GLSL 1.30.08 specification, section 8.3
  47. template <typename genFIType>
  48. genFIType sign(genFIType const & x);
  49. //! Returns a value equal to the nearest integer that is less then or equal to x.
  50. //!
  51. /// @see
  52. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/floor.xml">GLSL floor man page</a>
  53. //! @li GLSL 1.30.08 specification, section 8.3
  54. template <typename genType>
  55. genType floor(genType const & x);
  56. //! Returns a value equal to the nearest integer to x
  57. //! whose absolute value is not larger than the absolute value of x.
  58. //!
  59. /// @see
  60. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/trunc.xml">GLSL trunc man page</a>
  61. //! @li GLSL 1.30.08 specification, section 8.3
  62. template <typename genType>
  63. genType trunc(genType const & x);
  64. //! Returns a value equal to the nearest integer to x.
  65. //! The fraction 0.5 will round in a direction chosen by the
  66. //! implementation, presumably the direction that is fastest.
  67. //! This includes the possibility that round(x) returns the
  68. //! same value as roundEven(x) for all values of x.
  69. //!
  70. /// @see
  71. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/round.xml">GLSL round man page</a>
  72. //! @li GLSL 1.30.08 specification, section 8.3
  73. template <typename genType>
  74. genType round(genType const & x);
  75. //! Returns a value equal to the nearest integer to x.
  76. //! A fractional part of 0.5 will round toward the nearest even
  77. //! integer. (Both 3.5 and 4.5 for x will return 4.0.)
  78. //!
  79. /// @see
  80. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/roundEven.xml">GLSL roundEven man page</a>
  81. //! @li GLSL 1.30.08 specification, section 8.3
  82. template <typename genType>
  83. genType roundEven(genType const & x);
  84. //! Returns a value equal to the nearest integer
  85. //! that is greater than or equal to x.
  86. //!
  87. /// @see
  88. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/ceil.xml">GLSL ceil man page</a>
  89. //! @li GLSL 1.30.08 specification, section 8.3
  90. template <typename genType>
  91. genType ceil(genType const & x);
  92. //! Return x - floor(x).
  93. //!
  94. /// @see
  95. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/fract.xml">GLSL fract man page</a>
  96. //! @li GLSL 1.30.08 specification, section 8.3
  97. template <typename genType>
  98. genType fract(genType const & x);
  99. //! Modulus. Returns x - y * floor(x / y)
  100. //! for each component in x using the floating point value y.
  101. //!
  102. /// @see
  103. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/mod.xml">GLSL mod man page</a>
  104. //! @li GLSL 1.30.08 specification, section 8.3
  105. template <typename genType>
  106. genType mod(
  107. genType const & x,
  108. genType const & y);
  109. //! Modulus. Returns x - y * floor(x / y)
  110. //! for each component in x using the floating point value y.
  111. //!
  112. /// @see
  113. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/mod.xml">GLSL mod man page</a>
  114. //! @li GLSL 1.30.08 specification, section 8.3
  115. template <typename genType>
  116. genType mod(
  117. genType const & x,
  118. typename genType::value_type const & y);
  119. //! Returns the fractional part of x and sets i to the integer
  120. //! part (as a whole number floating point value). Both the
  121. //! return value and the output parameter will have the same
  122. //! sign as x.
  123. //!
  124. /// @see
  125. /// @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/modf.xml">GLSL modf man page</a>
  126. /// @li GLSL 1.30.08 specification, section 8.3
  127. template <typename genType>
  128. genType modf(
  129. genType const & x,
  130. genType & i);
  131. /// Returns y if y < x; otherwise, it returns x.
  132. ///
  133. /// @see
  134. /// @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/min.xml">GLSL min man page</a>
  135. /// @li GLSL 1.30.08 specification, section 8.3
  136. template <typename genType>
  137. genType min(
  138. genType const & x,
  139. genType const & y);
  140. template <typename genType>
  141. genType min(
  142. genType const & x,
  143. typename genType::value_type const & y);
  144. /// Returns y if x < y; otherwise, it returns x.
  145. ///
  146. /// @see
  147. /// @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/max.xml">GLSL max man page</a>
  148. /// @li GLSL 1.30.08 specification, section 8.3
  149. template <typename genType>
  150. genType max(
  151. genType const & x,
  152. genType const & y);
  153. template <typename genType>
  154. genType max(
  155. genType const & x,
  156. typename genType::value_type const & y);
  157. //! Returns min(max(x, minVal), maxVal) for each component in x
  158. //! using the floating-point values minVal and maxVal.
  159. //!
  160. /// @see
  161. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/clamp.xml">GLSL clamp man page</a>
  162. //! @li GLSL 1.30.08 specification, section 8.3
  163. template <typename genType>
  164. genType clamp(
  165. genType const & x,
  166. genType const & minVal,
  167. genType const & maxVal);
  168. template <typename genType>
  169. genType clamp(
  170. genType const & x,
  171. typename genType::value_type const & minVal,
  172. typename genType::value_type const & maxVal);
  173. //! @return If genTypeU is a floating scalar or vector:
  174. //! Returns x * (1.0 - a) + y * a, i.e., the linear blend of
  175. //! x and y using the floating-point value a.
  176. //! The value for a is not restricted to the range [0, 1].
  177. //!
  178. //! @return If genTypeU is a boolean scalar or vector:
  179. //! Selects which vector each returned component comes
  180. //! from. For a component of a that is false, the
  181. //! corresponding component of x is returned. For a
  182. //! component of a that is true, the corresponding
  183. //! component of y is returned. Components of x and y that
  184. //! are not selected are allowed to be invalid floating point
  185. //! values and will have no effect on the results. Thus, this
  186. //! provides different functionality than
  187. //! genType mix(genType x, genType y, genType(a))
  188. //! where a is a Boolean vector.
  189. //!
  190. //! @see
  191. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/mix.xml">GLSL mix man page</a>
  192. //! @li GLSL 1.30.08 specification, section 8.3
  193. //!
  194. //! @param[in] x Floating point scalar or vector.
  195. //! @param[in] y Floating point scalar or vector.
  196. //! @param[in] a Floating point or boolean scalar or vector.
  197. //!
  198. //! @todo Test when 'a' is a boolean.
  199. template <typename genTypeT, typename genTypeU>
  200. genTypeT mix(genTypeT const & x, genTypeT const & y, genTypeU const & a);
  201. //! Returns 0.0 if x < edge, otherwise it returns 1.0.
  202. //!
  203. //! @see
  204. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/step.xml">GLSL step man page</a>
  205. //! @li GLSL 1.30.08 specification, section 8.3
  206. template <typename genType>
  207. genType step(
  208. genType const & edge,
  209. genType const & x);
  210. template <typename genType>
  211. genType step(
  212. typename genType::value_type const & edge,
  213. genType const & x);
  214. //! Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and
  215. //! performs smooth Hermite interpolation between 0 and 1
  216. //! when edge0 < x < edge1. This is useful in cases where
  217. //! you would want a threshold function with a smooth
  218. //! transition. This is equivalent to:
  219. //! genType t;
  220. //! t = clamp ((x – edge0) / (edge1 – edge0), 0, 1);
  221. //! return t * t * (3 – 2 * t);
  222. //! Results are undefined if edge0 >= edge1.
  223. //!
  224. //! @see
  225. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/smoothstep.xml">GLSL smoothstep man page</a>
  226. //! @li GLSL 1.30.08 specification, section 8.3
  227. template <typename genType>
  228. genType smoothstep(
  229. genType const & edge0,
  230. genType const & edge1,
  231. genType const & x);
  232. template <typename genType>
  233. genType smoothstep(
  234. typename genType::value_type const & edge0,
  235. typename genType::value_type const & edge1,
  236. genType const & x);
  237. //! Returns true if x holds a NaN (not a number)
  238. //! representation in the underlying implementation's set of
  239. //! floating point representations. Returns false otherwise,
  240. //! including for implementations with no NaN
  241. //! representations.
  242. //!
  243. //! @see
  244. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/isnan.xml">GLSL isnan man page</a>
  245. //! @li GLSL 1.30.08 specification, section 8.3
  246. template <typename genType>
  247. typename genType::bool_type isnan(genType const & x);
  248. //! Returns true if x holds a positive infinity or negative
  249. //! infinity representation in the underlying implementation's
  250. //! set of floating point representations. Returns false
  251. //! otherwise, including for implementations with no infinity
  252. //! representations.
  253. //!
  254. //! @see
  255. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/isinf.xml">GLSL isinf man page</a>
  256. //! @li GLSL 1.30.08 specification, section 8.3
  257. template <typename genType>
  258. typename genType::bool_type isinf(genType const & x);
  259. //! Returns a signed integer value representing
  260. //! the encoding of a floating-point value. The floatingpoint
  261. //! value's bit-level representation is preserved.
  262. //!
  263. //! @see
  264. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/floatBitsToInt.xml">GLSL floatBitsToInt man page</a>
  265. //! @li GLSL 4.00.08 specification, section 8.3
  266. template <typename genType, typename genIType>
  267. genIType floatBitsToInt(genType const & value);
  268. //! Returns a unsigned integer value representing
  269. //! the encoding of a floating-point value. The floatingpoint
  270. //! value's bit-level representation is preserved.
  271. //!
  272. //! @see
  273. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/floatBitsToUint.xml">GLSL floatBitsToUint man page</a>
  274. //! @li GLSL 4.00.08 specification, section 8.3
  275. template <typename genType, typename genUType>
  276. genUType floatBitsToUint(genType const & value);
  277. //! Returns a floating-point value corresponding to a signed
  278. //! integer encoding of a floating-point value.
  279. //! If an inf or NaN is passed in, it will not signal, and the
  280. //! resulting floating point value is unspecified. Otherwise,
  281. //! the bit-level representation is preserved.
  282. //!
  283. //! @see
  284. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/intBitsToFloat.xml">GLSL intBitsToFloat man page</a>
  285. //! @li GLSL 4.00.08 specification, section 8.3
  286. template <typename genType, typename genIType>
  287. genType intBitsToFloat(genIType const & value);
  288. //! Returns a floating-point value corresponding to a
  289. //! unsigned integer encoding of a floating-point value.
  290. //! If an inf or NaN is passed in, it will not signal, and the
  291. //! resulting floating point value is unspecified. Otherwise,
  292. //! the bit-level representation is preserved.
  293. //!
  294. //! @see
  295. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/uintBitsToFloat.xml">GLSL uintBitsToFloat man page</a>
  296. //! @li GLSL 4.00.08 specification, section 8.3
  297. template <typename genType, typename genUType>
  298. genType uintBitsToFloat(genUType const & value);
  299. //! Computes and returns a * b + c.
  300. //!
  301. //! @see
  302. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/fma.xml">GLSL fma man page</a>
  303. //! @li GLSL 4.00.08 specification, section 8.3
  304. template <typename genType>
  305. genType fma(genType const & a, genType const & b, genType const & c);
  306. //! Splits x into a floating-point significand in the range
  307. //! [0.5, 1.0) and an integral exponent of two, such that:
  308. //! x = significand * exp(2, exponent)
  309. //!
  310. //! The significand is returned by the function and the
  311. //! exponent is returned in the parameter exp. For a
  312. //! floating-point value of zero, the significant and exponent
  313. //! are both zero. For a floating-point value that is an
  314. //! infinity or is not a number, the results are undefined.
  315. //!
  316. //! @see
  317. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/frexp.xml">GLSL frexp man page</a>
  318. //! @li GLSL 4.00.08 specification, section 8.3
  319. template <typename genType, typename genIType>
  320. genType frexp(genType const & x, genIType & exp);
  321. //! Builds a floating-point number from x and the
  322. //! corresponding integral exponent of two in exp, returning:
  323. //! significand * exp(2, exponent)
  324. //!
  325. //! If this product is too large to be represented in the
  326. //! floating-point type, the result is undefined.
  327. //!
  328. //! @see
  329. //! @li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/ldexp.xml">GLSL ldexp man page</a>;
  330. //! @li GLSL 4.00.08 specification, section 8.3
  331. template <typename genType, typename genIType>
  332. genType ldexp(genType const & x, genIType const & exp);
  333. /// @}
  334. }//namespace glm
  335. #include "func_common.inl"
  336. #endif//glm_core_func_common