fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Image / Squish / maths.h
blob9d7939dfda8c2e04e3e310c391040734b7a09cd7
1 /* -----------------------------------------------------------------------------
3 Copyright (c) 2006 Simon Brown si@sjbrown.co.uk
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 -------------------------------------------------------------------------- */
26 #ifndef SQUISH_MATHS_H
27 #define SQUISH_MATHS_H
29 #include <cmath>
30 #include <algorithm>
31 #include "config.h"
33 namespace osgsquish {
35 class Vec3
37 public:
38 typedef Vec3 const& Arg;
40 Vec3() : m_x(0.f), m_y(0.f), m_z(0.f)
44 explicit Vec3( float s ) : m_x(s), m_y(s), m_z(s)
48 Vec3( float x, float y, float z ) : m_x(x), m_y(y), m_z(z)
52 float X() const { return m_x; }
53 float Y() const { return m_y; }
54 float Z() const { return m_z; }
56 Vec3 operator-() const
58 return Vec3( -m_x, -m_y, -m_z );
61 Vec3& operator+=( Arg v )
63 m_x += v.m_x;
64 m_y += v.m_y;
65 m_z += v.m_z;
66 return *this;
69 Vec3& operator-=( Arg v )
71 m_x -= v.m_x;
72 m_y -= v.m_y;
73 m_z -= v.m_z;
74 return *this;
77 Vec3& operator*=( Arg v )
79 m_x *= v.m_x;
80 m_y *= v.m_y;
81 m_z *= v.m_z;
82 return *this;
85 Vec3& operator*=( float s )
87 m_x *= s;
88 m_y *= s;
89 m_z *= s;
90 return *this;
93 Vec3& operator/=( Arg v )
95 m_x /= v.m_x;
96 m_y /= v.m_y;
97 m_z /= v.m_z;
98 return *this;
101 Vec3& operator/=( float s )
103 float t = 1.0f/s;
104 m_x *= t;
105 m_y *= t;
106 m_z *= t;
107 return *this;
110 friend Vec3 operator+( Arg left, Arg right )
112 Vec3 copy( left );
113 return copy += right;
116 friend Vec3 operator-( Arg left, Arg right )
118 Vec3 copy( left );
119 return copy -= right;
122 friend Vec3 operator*( Arg left, Arg right )
124 Vec3 copy( left );
125 return copy *= right;
128 friend Vec3 operator*( Arg left, float right )
130 Vec3 copy( left );
131 return copy *= right;
134 friend Vec3 operator*( float left, Arg right )
136 Vec3 copy( right );
137 return copy *= left;
140 friend Vec3 operator/( Arg left, Arg right )
142 Vec3 copy( left );
143 return copy /= right;
146 friend Vec3 operator/( Arg left, float right )
148 Vec3 copy( left );
149 return copy /= right;
152 friend float Dot( Arg left, Arg right )
154 return left.m_x*right.m_x + left.m_y*right.m_y + left.m_z*right.m_z;
157 friend Vec3 Min( Arg left, Arg right )
159 return Vec3(
160 std::min( left.m_x, right.m_x ),
161 std::min( left.m_y, right.m_y ),
162 std::min( left.m_z, right.m_z )
166 friend Vec3 Max( Arg left, Arg right )
168 return Vec3(
169 std::max( left.m_x, right.m_x ),
170 std::max( left.m_y, right.m_y ),
171 std::max( left.m_z, right.m_z )
175 friend Vec3 Truncate( Arg v )
177 return Vec3(
178 v.m_x > 0.0f ? std::floor( v.m_x ) : std::ceil( v.m_x ),
179 v.m_y > 0.0f ? std::floor( v.m_y ) : std::ceil( v.m_y ),
180 v.m_z > 0.0f ? std::floor( v.m_z ) : std::ceil( v.m_z )
184 private:
185 float m_x;
186 float m_y;
187 float m_z;
190 inline float LengthSquared( Vec3::Arg v )
192 return Dot( v, v );
195 class Sym3x3
197 public:
198 Sym3x3()
202 Sym3x3( float s )
204 for( int i = 0; i < 6; ++i )
205 m_x[i] = s;
208 float operator[]( int index ) const
210 return m_x[index];
213 float& operator[]( int index )
215 return m_x[index];
218 private:
219 float m_x[6];
222 Sym3x3 ComputeWeightedCovariance( int n, Vec3 const* points, float const* weights );
223 Vec3 ComputePrincipleComponent( Sym3x3 const& matrix );
225 } // namespace squish
227 #endif // ndef SQUISH_MATHS_H