You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

430 lines
20 KiB

4 years ago
  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 "_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. GLM_FUNC_DECL 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. GLM_FUNC_DECL 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. GLM_FUNC_DECL 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. GLM_FUNC_DECL 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. GLM_FUNC_DECL 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. GLM_FUNC_DECL 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. GLM_FUNC_DECL 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. GLM_FUNC_DECL 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. GLM_FUNC_DECL 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. GLM_FUNC_DECL 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. GLM_FUNC_DECL 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. GLM_FUNC_DECL genType min(
  158. genType const & x,
  159. genType const & y);
  160. template <typename genType>
  161. GLM_FUNC_DECL 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. GLM_FUNC_DECL genType max(
  172. genType const & x,
  173. genType const & y);
  174. template <typename genType>
  175. GLM_FUNC_DECL 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. GLM_FUNC_DECL genType clamp(
  187. genType const & x,
  188. genType const & minVal,
  189. genType const & maxVal);
  190. template <typename genType>
  191. GLM_FUNC_DECL genType clamp(
  192. genType const & x,
  193. typename genType::value_type const & minVal,
  194. typename genType::value_type const & maxVal);
  195. /// 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. /// 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. GLM_FUNC_DECL 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. GLM_FUNC_DECL genType step(
  245. genType const & edge,
  246. genType const & x);
  247. template <typename genType>
  248. GLM_FUNC_DECL 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. GLM_FUNC_DECL genType smoothstep(
  267. genType const & edge0,
  268. genType const & edge1,
  269. genType const & x);
  270. template <typename genType>
  271. GLM_FUNC_DECL 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. /// /!\ When using compiler fast math, this function may fail.
  282. ///
  283. /// @tparam genType Floating-point scalar or vector types.
  284. ///
  285. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/isnan.xml">GLSL isnan man page</a>
  286. /// @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>
  287. template <typename genType>
  288. GLM_FUNC_DECL typename genType::bool_type isnan(genType const & x);
  289. /// Returns true if x holds a positive infinity or negative
  290. /// infinity representation in the underlying implementation's
  291. /// set of floating point representations. Returns false
  292. /// otherwise, including for implementations with no infinity
  293. /// representations.
  294. ///
  295. /// @tparam genType Floating-point scalar or vector types.
  296. ///
  297. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/isinf.xml">GLSL isinf man page</a>
  298. /// @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>
  299. template <typename genType>
  300. GLM_FUNC_DECL typename genType::bool_type isinf(genType const & x);
  301. /// Returns a signed integer value representing
  302. /// the encoding of a floating-point value. The floatingpoint
  303. /// value's bit-level representation is preserved.
  304. ///
  305. /// @tparam genType Single-precision floating-point scalar or vector types.
  306. /// @tparam genIType Signed integer scalar or vector types.
  307. ///
  308. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/floatBitsToInt.xml">GLSL floatBitsToInt man page</a>
  309. /// @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>
  310. template <typename genType, typename genIType>
  311. GLM_FUNC_DECL genIType floatBitsToInt(genType const & value);
  312. /// Returns a unsigned integer value representing
  313. /// the encoding of a floating-point value. The floatingpoint
  314. /// value's bit-level representation is preserved.
  315. ///
  316. /// @tparam genType Single-precision floating-point scalar or vector types.
  317. /// @tparam genUType Unsigned integer scalar or vector types.
  318. ///
  319. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/floatBitsToUint.xml">GLSL floatBitsToUint man page</a>
  320. /// @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>
  321. template <typename genType, typename genUType>
  322. GLM_FUNC_DECL genUType floatBitsToUint(genType const & value);
  323. /// Returns a floating-point value corresponding to a signed
  324. /// integer encoding of a floating-point value.
  325. /// If an inf or NaN is passed in, it will not signal, and the
  326. /// resulting floating point value is unspecified. Otherwise,
  327. /// the bit-level representation is preserved.
  328. ///
  329. /// @tparam genType Single-precision floating-point scalar or vector types.
  330. /// @tparam genIType Signed integer scalar or vector types.
  331. ///
  332. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/intBitsToFloat.xml">GLSL intBitsToFloat man page</a>
  333. /// @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>
  334. ///
  335. /// @todo Clarify this declaration, we don't need to actually specify the return type
  336. template <typename genType, typename genIType>
  337. GLM_FUNC_DECL genType intBitsToFloat(genIType const & value);
  338. /// Returns a floating-point value corresponding to a
  339. /// unsigned integer encoding of a floating-point value.
  340. /// If an inf or NaN is passed in, it will not signal, and the
  341. /// resulting floating point value is unspecified. Otherwise,
  342. /// the bit-level representation is preserved.
  343. ///
  344. /// @tparam genType Single-precision floating-point scalar or vector types.
  345. /// @tparam genUType Unsigned integer scalar or vector types.
  346. ///
  347. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/uintBitsToFloat.xml">GLSL uintBitsToFloat man page</a>
  348. /// @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>
  349. ///
  350. /// @todo Clarify this declaration, we don't need to actually specify the return type
  351. template <typename genType, typename genUType>
  352. GLM_FUNC_DECL genType uintBitsToFloat(genUType const & value);
  353. /// Computes and returns a * b + c.
  354. ///
  355. /// @tparam genType Floating-point scalar or vector types.
  356. ///
  357. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/fma.xml">GLSL fma man page</a>
  358. /// @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>
  359. template <typename genType>
  360. GLM_FUNC_DECL genType fma(genType const & a, genType const & b, genType const & c);
  361. /// Splits x into a floating-point significand in the range
  362. /// [0.5, 1.0) and an integral exponent of two, such that:
  363. /// x = significand * exp(2, exponent)
  364. ///
  365. /// The significand is returned by the function and the
  366. /// exponent is returned in the parameter exp. For a
  367. /// floating-point value of zero, the significant and exponent
  368. /// are both zero. For a floating-point value that is an
  369. /// infinity or is not a number, the results are undefined.
  370. ///
  371. /// @tparam genType Floating-point scalar or vector types.
  372. ///
  373. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/frexp.xml">GLSL frexp man page</a>
  374. /// @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>
  375. template <typename genType, typename genIType>
  376. GLM_FUNC_DECL genType frexp(genType const & x, genIType & exp);
  377. /// Builds a floating-point number from x and the
  378. /// corresponding integral exponent of two in exp, returning:
  379. /// significand * exp(2, exponent)
  380. ///
  381. /// If this product is too large to be represented in the
  382. /// floating-point type, the result is undefined.
  383. ///
  384. /// @tparam genType Floating-point scalar or vector types.
  385. ///
  386. /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/ldexp.xml">GLSL ldexp man page</a>;
  387. /// @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>
  388. template <typename genType, typename genIType>
  389. GLM_FUNC_DECL genType ldexp(genType const & x, genIType const & exp);
  390. /// @}
  391. }//namespace glm
  392. #include "func_common.inl"
  393. #endif//GLM_CORE_func_common