maths.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* -----------------------------------------------------------------------------
  2. Copyright (c) 2006 Simon Brown si@sjbrown.co.uk
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  15. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  16. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  17. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  18. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. -------------------------------------------------------------------------- */
  20. #ifndef SQUISH_MATHS_H
  21. #define SQUISH_MATHS_H
  22. #include <cmath>
  23. #include <algorithm>
  24. #include "config.h"
  25. namespace squish {
  26. class Vec3
  27. {
  28. public:
  29. typedef Vec3 const& Arg;
  30. Vec3()
  31. {
  32. }
  33. explicit Vec3( float s )
  34. {
  35. m_x = s;
  36. m_y = s;
  37. m_z = s;
  38. }
  39. Vec3( float x, float y, float z )
  40. {
  41. m_x = x;
  42. m_y = y;
  43. m_z = z;
  44. }
  45. float X() const { return m_x; }
  46. float Y() const { return m_y; }
  47. float Z() const { return m_z; }
  48. Vec3 operator-() const
  49. {
  50. return Vec3( -m_x, -m_y, -m_z );
  51. }
  52. Vec3& operator+=( Arg v )
  53. {
  54. m_x += v.m_x;
  55. m_y += v.m_y;
  56. m_z += v.m_z;
  57. return *this;
  58. }
  59. Vec3& operator-=( Arg v )
  60. {
  61. m_x -= v.m_x;
  62. m_y -= v.m_y;
  63. m_z -= v.m_z;
  64. return *this;
  65. }
  66. Vec3& operator*=( Arg v )
  67. {
  68. m_x *= v.m_x;
  69. m_y *= v.m_y;
  70. m_z *= v.m_z;
  71. return *this;
  72. }
  73. Vec3& operator*=( float s )
  74. {
  75. m_x *= s;
  76. m_y *= s;
  77. m_z *= s;
  78. return *this;
  79. }
  80. Vec3& operator/=( Arg v )
  81. {
  82. m_x /= v.m_x;
  83. m_y /= v.m_y;
  84. m_z /= v.m_z;
  85. return *this;
  86. }
  87. Vec3& operator/=( float s )
  88. {
  89. float t = 1.0f/s;
  90. m_x *= t;
  91. m_y *= t;
  92. m_z *= t;
  93. return *this;
  94. }
  95. friend Vec3 operator+( Arg left, Arg right )
  96. {
  97. Vec3 copy( left );
  98. return copy += right;
  99. }
  100. friend Vec3 operator-( Arg left, Arg right )
  101. {
  102. Vec3 copy( left );
  103. return copy -= right;
  104. }
  105. friend Vec3 operator*( Arg left, Arg right )
  106. {
  107. Vec3 copy( left );
  108. return copy *= right;
  109. }
  110. friend Vec3 operator*( Arg left, float right )
  111. {
  112. Vec3 copy( left );
  113. return copy *= right;
  114. }
  115. friend Vec3 operator*( float left, Arg right )
  116. {
  117. Vec3 copy( right );
  118. return copy *= left;
  119. }
  120. friend Vec3 operator/( Arg left, Arg right )
  121. {
  122. Vec3 copy( left );
  123. return copy /= right;
  124. }
  125. friend Vec3 operator/( Arg left, float right )
  126. {
  127. Vec3 copy( left );
  128. return copy /= right;
  129. }
  130. friend float Dot( Arg left, Arg right )
  131. {
  132. return left.m_x*right.m_x + left.m_y*right.m_y + left.m_z*right.m_z;
  133. }
  134. friend Vec3 Min( Arg left, Arg right )
  135. {
  136. return Vec3(
  137. std::min( left.m_x, right.m_x ),
  138. std::min( left.m_y, right.m_y ),
  139. std::min( left.m_z, right.m_z )
  140. );
  141. }
  142. friend Vec3 Max( Arg left, Arg right )
  143. {
  144. return Vec3(
  145. std::max( left.m_x, right.m_x ),
  146. std::max( left.m_y, right.m_y ),
  147. std::max( left.m_z, right.m_z )
  148. );
  149. }
  150. friend Vec3 Truncate( Arg v )
  151. {
  152. return Vec3(
  153. v.m_x > 0.0f ? std::floor( v.m_x ) : std::ceil( v.m_x ),
  154. v.m_y > 0.0f ? std::floor( v.m_y ) : std::ceil( v.m_y ),
  155. v.m_z > 0.0f ? std::floor( v.m_z ) : std::ceil( v.m_z )
  156. );
  157. }
  158. private:
  159. float m_x;
  160. float m_y;
  161. float m_z;
  162. };
  163. inline float LengthSquared( Vec3::Arg v )
  164. {
  165. return Dot( v, v );
  166. }
  167. class Sym3x3
  168. {
  169. public:
  170. Sym3x3()
  171. {
  172. }
  173. Sym3x3( float s )
  174. {
  175. for( int i = 0; i < 6; ++i )
  176. m_x[i] = s;
  177. }
  178. float operator[]( int index ) const
  179. {
  180. return m_x[index];
  181. }
  182. float& operator[]( int index )
  183. {
  184. return m_x[index];
  185. }
  186. private:
  187. float m_x[6];
  188. };
  189. Sym3x3 ComputeWeightedCovariance( int n, Vec3 const* points, float const* weights );
  190. Vec3 ComputePrincipleComponent( Sym3x3 const& matrix );
  191. inline float SRGBToLinear(float color_srgb)
  192. {
  193. if (color_srgb <= 0.04045f)
  194. {
  195. return color_srgb * 0.0773993805f;
  196. }
  197. return powf((color_srgb + 0.055f) / 1.055f, 2.4f);
  198. }
  199. inline float LinearToSRGB(float color_linear)
  200. {
  201. if (color_linear <= 0.0031308f)
  202. {
  203. return color_linear * 12.92f;
  204. }
  205. return 1.055f * powf(color_linear, 0.416666657f) - 0.055f;
  206. }
  207. } // namespace squish
  208. #endif // ndef SQUISH_MATHS_H