func_common.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2012 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. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  29. ///
  30. /// @defgroup core_func_common Common functions
  31. /// @ingroup core
  32. ///
  33. /// These all operate component-wise. The description is per component.
  34. ///////////////////////////////////////////////////////////////////////////////////
  35. #ifndef GLM_CORE_func_common
  36. #define GLM_CORE_func_common GLM_VERSION
  37. #include "_fixes.hpp"
  38. namespace glm
  39. {
  40. /// @addtogroup core_func_common
  41. /// @{
  42. /// Returns x if x >= 0; otherwise, it returns -x.
  43. ///
  44. /// @tparam genType floating-point or signed integer; scalar or vector types.
  45. ///
  46. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/abs.xml">GLSL abs man page</a>
  47. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  48. template <typename genType>
  49. genType abs(genType const & x);
  50. /// Returns 1.0 if x > 0, 0.0 if x == 0, or -1.0 if x < 0.
  51. ///
  52. /// @tparam genType Floating-point or signed integer; scalar or vector types.
  53. ///
  54. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/sign.xml">GLSL sign man page</a>
  55. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  56. template <typename genType>
  57. genType sign(genType const & x);
  58. /// Returns a value equal to the nearest integer that is less then or equal to x.
  59. ///
  60. /// @tparam genType Floating-point scalar or vector types.
  61. ///
  62. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/floor.xml">GLSL floor man page</a>
  63. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  64. template <typename genType>
  65. genType floor(genType const & x);
  66. /// Returns a value equal to the nearest integer to x
  67. /// whose absolute value is not larger than the absolute value of x.
  68. ///
  69. /// @tparam genType Floating-point scalar or vector types.
  70. ///
  71. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/trunc.xml">GLSL trunc man page</a>
  72. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  73. template <typename genType>
  74. genType trunc(genType const & x);
  75. /// Returns a value equal to the nearest integer to x.
  76. /// The fraction 0.5 will round in a direction chosen by the
  77. /// implementation, presumably the direction that is fastest.
  78. /// This includes the possibility that round(x) returns the
  79. /// same value as roundEven(x) for all values of x.
  80. ///
  81. /// @tparam genType Floating-point scalar or vector types.
  82. ///
  83. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/round.xml">GLSL round man page</a>
  84. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  85. template <typename genType>
  86. genType round(genType const & x);
  87. /// Returns a value equal to the nearest integer to x.
  88. /// A fractional part of 0.5 will round toward the nearest even
  89. /// integer. (Both 3.5 and 4.5 for x will return 4.0.)
  90. ///
  91. /// @tparam genType Floating-point scalar or vector types.
  92. ///
  93. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/roundEven.xml">GLSL roundEven man page</a>
  94. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  95. /// @see <a href="http://developer.amd.com/documentation/articles/pages/New-Round-to-Even-Technique.aspx">New round to even technique</a>
  96. template <typename genType>
  97. genType roundEven(genType const & x);
  98. /// Returns a value equal to the nearest integer
  99. /// that is greater than or equal to x.
  100. ///
  101. /// @tparam genType Floating-point scalar or vector types.
  102. ///
  103. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/ceil.xml">GLSL ceil man page</a>
  104. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  105. template <typename genType>
  106. genType ceil(genType const & x);
  107. /// Return x - floor(x).
  108. ///
  109. /// @tparam genType Floating-point scalar or vector types.
  110. ///
  111. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/fract.xml">GLSL fract man page</a>
  112. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  113. template <typename genType>
  114. genType fract(genType const & x);
  115. /// Modulus. Returns x - y * floor(x / y)
  116. /// for each component in x using the floating point value y.
  117. ///
  118. /// @tparam genType Floating-point scalar or vector types.
  119. ///
  120. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/mod.xml">GLSL mod man page</a>
  121. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  122. template <typename genType>
  123. genType mod(
  124. genType const & x,
  125. genType const & y);
  126. /// Modulus. Returns x - y * floor(x / y)
  127. /// for each component in x using the floating point value y.
  128. ///
  129. /// @tparam genType Floating-point scalar or vector types.
  130. ///
  131. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/mod.xml">GLSL mod man page</a>
  132. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  133. template <typename genType>
  134. genType mod(
  135. genType const & x,
  136. typename genType::value_type const & y);
  137. /// Returns the fractional part of x and sets i to the integer
  138. /// part (as a whole number floating point value). Both the
  139. /// return value and the output parameter will have the same
  140. /// sign as x.
  141. ///
  142. /// @tparam genType Floating-point scalar or vector types.
  143. ///
  144. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/modf.xml">GLSL modf man page</a>
  145. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  146. template <typename genType>
  147. genType modf(
  148. genType const & x,
  149. genType & i);
  150. /// Returns y if y < x; otherwise, it returns x.
  151. ///
  152. /// @tparam genType Floating-point or integer; scalar or vector types.
  153. ///
  154. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/min.xml">GLSL min man page</a>
  155. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  156. template <typename genType>
  157. genType min(
  158. genType const & x,
  159. genType const & y);
  160. template <typename genType>
  161. genType min(
  162. genType const & x,
  163. typename genType::value_type const & y);
  164. /// Returns y if x < y; otherwise, it returns x.
  165. ///
  166. /// @tparam genType Floating-point or integer; scalar or vector types.
  167. ///
  168. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/max.xml">GLSL max man page</a>
  169. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  170. template <typename genType>
  171. genType max(
  172. genType const & x,
  173. genType const & y);
  174. template <typename genType>
  175. genType max(
  176. genType const & x,
  177. typename genType::value_type const & y);
  178. /// Returns min(max(x, minVal), maxVal) for each component in x
  179. /// using the floating-point values minVal and maxVal.
  180. ///
  181. /// @tparam genType Floating-point or integer; scalar or vector types.
  182. ///
  183. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/clamp.xml">GLSL clamp man page</a>
  184. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  185. template <typename genType>
  186. genType clamp(
  187. genType const & x,
  188. genType const & minVal,
  189. genType const & maxVal);
  190. template <typename genType>
  191. genType clamp(
  192. genType const & x,
  193. typename genType::value_type const & minVal,
  194. typename genType::value_type const & maxVal);
  195. //! @return If genTypeU is a floating scalar or vector:
  196. //! Returns x * (1.0 - a) + y * a, i.e., the linear blend of
  197. //! x and y using the floating-point value a.
  198. //! The value for a is not restricted to the range [0, 1].
  199. //!
  200. //! @return If genTypeU is a boolean scalar or vector:
  201. //! Selects which vector each returned component comes
  202. //! from. For a component of a that is false, the
  203. //! corresponding component of x is returned. For a
  204. //! component of a that is true, the corresponding
  205. //! component of y is returned. Components of x and y that
  206. //! are not selected are allowed to be invalid floating point
  207. //! values and will have no effect on the results. Thus, this
  208. //! provides different functionality than
  209. //! genType mix(genType x, genType y, genType(a))
  210. //! where a is a Boolean vector.
  211. ///
  212. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/mix.xml">GLSL mix man page</a>
  213. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  214. ///
  215. /// @param[in] x Value to interpolate.
  216. /// @param[in] y Value to interpolate.
  217. /// @param[in] a Interpolant.
  218. ///
  219. /// @tparam genTypeT Floating point scalar or vector.
  220. /// @tparam genTypeU Floating point or boolean scalar or vector. It can't be a vector if it is the length of genTypeT.
  221. ///
  222. /// @code
  223. /// #include <glm/glm.hpp>
  224. /// ...
  225. /// float a;
  226. /// bool b;
  227. /// glm::dvec3 e;
  228. /// glm::dvec3 f;
  229. /// glm::vec4 g;
  230. /// glm::vec4 h;
  231. /// ...
  232. /// glm::vec4 r = glm::mix(g, h, a); // Interpolate with a floating-point scalar two vectors.
  233. /// glm::vec4 s = glm::mix(g, h, b); // Teturns g or h;
  234. /// glm::dvec3 t = glm::mix(e, f, a); // Types of the third parameter is not required to match with the first and the second.
  235. /// glm::vec4 u = glm::mix(g, h, r); // Interpolations can be perform per component with a vector for the last parameter.
  236. /// @endcode
  237. template <typename genTypeT, typename genTypeU>
  238. genTypeT mix(genTypeT const & x, genTypeT const & y, genTypeU const & a);
  239. //! Returns 0.0 if x < edge, otherwise it returns 1.0.
  240. //!
  241. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/step.xml">GLSL step man page</a>
  242. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  243. template <typename genType>
  244. genType step(
  245. genType const & edge,
  246. genType const & x);
  247. template <typename genType>
  248. genType step(
  249. typename genType::value_type const & edge,
  250. genType const & x);
  251. /// Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and
  252. /// performs smooth Hermite interpolation between 0 and 1
  253. /// when edge0 < x < edge1. This is useful in cases where
  254. /// you would want a threshold function with a smooth
  255. /// transition. This is equivalent to:
  256. /// genType t;
  257. /// t = clamp ((x – edge0) / (edge1 – edge0), 0, 1);
  258. /// return t * t * (3 – 2 * t);
  259. /// Results are undefined if edge0 >= edge1.
  260. ///
  261. /// @tparam genType Floating-point scalar or vector types.
  262. ///
  263. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/smoothstep.xml">GLSL smoothstep man page</a>
  264. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  265. template <typename genType>
  266. genType smoothstep(
  267. genType const & edge0,
  268. genType const & edge1,
  269. genType const & x);
  270. template <typename genType>
  271. genType smoothstep(
  272. typename genType::value_type const & edge0,
  273. typename genType::value_type const & edge1,
  274. genType const & x);
  275. /// Returns true if x holds a NaN (not a number)
  276. /// representation in the underlying implementation's set of
  277. /// floating point representations. Returns false otherwise,
  278. /// including for implementations with no NaN
  279. /// representations.
  280. ///
  281. /// @tparam genType Floating-point scalar or vector types.
  282. ///
  283. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/isnan.xml">GLSL isnan man page</a>
  284. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  285. template <typename genType>
  286. typename genType::bool_type isnan(genType const & x);
  287. /// Returns true if x holds a positive infinity or negative
  288. /// infinity representation in the underlying implementation's
  289. /// set of floating point representations. Returns false
  290. /// otherwise, including for implementations with no infinity
  291. /// representations.
  292. ///
  293. /// @tparam genType Floating-point scalar or vector types.
  294. ///
  295. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/isinf.xml">GLSL isinf man page</a>
  296. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  297. template <typename genType>
  298. typename genType::bool_type isinf(genType const & x);
  299. /// Returns a signed integer value representing
  300. /// the encoding of a floating-point value. The floatingpoint
  301. /// value's bit-level representation is preserved.
  302. ///
  303. /// @tparam genType Single-precision floating-point scalar or vector types.
  304. /// @tparam genIType Signed integer scalar or vector types.
  305. ///
  306. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/floatBitsToInt.xml">GLSL floatBitsToInt man page</a>
  307. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  308. template <typename genType, typename genIType>
  309. genIType floatBitsToInt(genType const & value);
  310. /// Returns a unsigned integer value representing
  311. /// the encoding of a floating-point value. The floatingpoint
  312. /// value's bit-level representation is preserved.
  313. ///
  314. /// @tparam genType Single-precision floating-point scalar or vector types.
  315. /// @tparam genUType Unsigned integer scalar or vector types.
  316. ///
  317. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/floatBitsToUint.xml">GLSL floatBitsToUint man page</a>
  318. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  319. template <typename genType, typename genUType>
  320. genUType floatBitsToUint(genType const & value);
  321. /// Returns a floating-point value corresponding to a signed
  322. /// integer encoding of a floating-point value.
  323. /// If an inf or NaN is passed in, it will not signal, and the
  324. /// resulting floating point value is unspecified. Otherwise,
  325. /// the bit-level representation is preserved.
  326. ///
  327. /// @tparam genType Single-precision floating-point scalar or vector types.
  328. /// @tparam genIType Signed integer scalar or vector types.
  329. ///
  330. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/intBitsToFloat.xml">GLSL intBitsToFloat man page</a>
  331. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  332. ///
  333. /// @todo Clarify this declaration, we don't need to actually specify the return type
  334. template <typename genType, typename genIType>
  335. genType intBitsToFloat(genIType const & value);
  336. /// Returns a floating-point value corresponding to a
  337. /// unsigned integer encoding of a floating-point value.
  338. /// If an inf or NaN is passed in, it will not signal, and the
  339. /// resulting floating point value is unspecified. Otherwise,
  340. /// the bit-level representation is preserved.
  341. ///
  342. /// @tparam genType Single-precision floating-point scalar or vector types.
  343. /// @tparam genUType Unsigned integer scalar or vector types.
  344. ///
  345. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/uintBitsToFloat.xml">GLSL uintBitsToFloat man page</a>
  346. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  347. ///
  348. /// @todo Clarify this declaration, we don't need to actually specify the return type
  349. template <typename genType, typename genUType>
  350. genType uintBitsToFloat(genUType const & value);
  351. /// Computes and returns a * b + c.
  352. ///
  353. /// @tparam genType Floating-point scalar or vector types.
  354. ///
  355. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/fma.xml">GLSL fma man page</a>
  356. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  357. template <typename genType>
  358. genType fma(genType const & a, genType const & b, genType const & c);
  359. /// Splits x into a floating-point significand in the range
  360. /// [0.5, 1.0) and an integral exponent of two, such that:
  361. /// x = significand * exp(2, exponent)
  362. ///
  363. /// The significand is returned by the function and the
  364. /// exponent is returned in the parameter exp. For a
  365. /// floating-point value of zero, the significant and exponent
  366. /// are both zero. For a floating-point value that is an
  367. /// infinity or is not a number, the results are undefined.
  368. ///
  369. /// @tparam genType Floating-point scalar or vector types.
  370. ///
  371. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/frexp.xml">GLSL frexp man page</a>
  372. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  373. template <typename genType, typename genIType>
  374. genType frexp(genType const & x, genIType & exp);
  375. /// Builds a floating-point number from x and the
  376. /// corresponding integral exponent of two in exp, returning:
  377. /// significand * exp(2, exponent)
  378. ///
  379. /// If this product is too large to be represented in the
  380. /// floating-point type, the result is undefined.
  381. ///
  382. /// @tparam genType Floating-point scalar or vector types.
  383. ///
  384. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/ldexp.xml">GLSL ldexp man page</a>;
  385. /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.3 Common Functions</a>
  386. template <typename genType, typename genIType>
  387. genType ldexp(genType const & x, genIType const & exp);
  388. /// @}
  389. }//namespace glm
  390. #include "func_common.inl"
  391. #endif//GLM_CORE_func_common