func_common.hpp 19 KB

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