37#include "../math/vec.h"
38#include "../math/Quaternion.h"
52 using scalar_t =
typename T::scalar_t;
62 inline __both__
LinearSpace2(
const vector_t& vx,
const vector_t& vy)
66 inline __both__
LinearSpace2(
const scalar_t& m00,
const scalar_t& m01,
67 const scalar_t& m10,
const scalar_t& m11)
68 : vx(m00,m10), vy(m01,m11) {}
71 inline __both__
const scalar_t
det()
const {
return vx.x*vy.y - vx.y*vy.x; }
83 inline const vector_t
row0()
const {
return vector_t(vx.x,vy.x); }
86 inline const vector_t
row1()
const {
return vector_t(vx.y,vy.y); }
92 inline __both__
LinearSpace2( ZeroTy ) : vx(ZeroTy()), vy(ZeroTy()) {}
93 inline __both__
LinearSpace2( OneTy ) : vx(OneTy(), ZeroTy()), vy(ZeroTy(), OneTy()) {}
103 scalar_t s = sin(r), c = cos(r);
113 scalar_t mirror{scalar_t(OneTy())};
114 if (m.
det() < scalar_t(ZeroTy())) {
120 for (
int i = 0; i < 99; i++) {
125 if (max(dot(d.
vx, d.
vx), dot(d.vy, d.vy)) < 1e-8)
145 template<
typename T> __both__
inline LinearSpace2<T> rcp (
const LinearSpace2<T>& a ) {
return a.inverse(); }
151 template<
typename T>
inline __both__ LinearSpace2<T> operator +(
const LinearSpace2<T>& a,
const LinearSpace2<T>& b ) {
return LinearSpace2<T>(a.vx+b.vx,a.vy+b.vy); }
152 template<
typename T>
inline __both__ LinearSpace2<T> operator -(
const LinearSpace2<T>& a,
const LinearSpace2<T>& b ) {
return LinearSpace2<T>(a.vx-b.vx,a.vy-b.vy); }
154 template<
typename T>
inline __both__ LinearSpace2<T> operator*(
const typename T::scalar_t & a,
const LinearSpace2<T>& b) {
return LinearSpace2<T>(a*b.vx, a*b.vy); }
155 template<
typename T>
inline __both__ T operator*(
const LinearSpace2<T>& a,
const T & b) {
return b.x*a.vx + b.y*a.vy; }
156 template<
typename T>
inline __both__ LinearSpace2<T> operator*(
const LinearSpace2<T>& a,
const LinearSpace2<T>& b) {
return LinearSpace2<T>(a*b.vx, a*b.vy); }
158 template<
typename T>
inline __both__ LinearSpace2<T> operator/(
const LinearSpace2<T>& a,
const typename T::scalar_t & b) {
return LinearSpace2<T>(a.vx/b, a.vy/b); }
159 template<
typename T>
inline __both__ LinearSpace2<T> operator/(
const LinearSpace2<T>& a,
const LinearSpace2<T>& b) {
return a * rcp(b); }
161 template<
typename T>
inline __both__ LinearSpace2<T>& operator *=( LinearSpace2<T>& a,
const LinearSpace2<T>& b ) {
return a = a * b; }
162 template<
typename T>
inline __both__ LinearSpace2<T>& operator /=( LinearSpace2<T>& a,
const LinearSpace2<T>& b ) {
return a = a / b; }
168 template<
typename T>
inline __both__
bool operator ==(
const LinearSpace2<T>& a,
const LinearSpace2<T>& b ) {
return a.vx == b.vx && a.vy == b.vy; }
169 template<
typename T>
inline __both__
bool operator !=(
const LinearSpace2<T>& a,
const LinearSpace2<T>& b ) {
return a.vx != b.vx || a.vy != b.vy; }
175 template<
typename T>
static std::ostream& operator<<(std::ostream& cout,
const LinearSpace2<T>& m) {
176 return cout <<
"{ vx = " << m.vx <<
", vy = " << m.vy <<
"}";
187 using scalar_t =
typename T::scalar_t;
194 : vx(OneTy(),ZeroTy(),ZeroTy()),
195 vy(ZeroTy(),OneTy(),ZeroTy()),
196 vz(ZeroTy(),ZeroTy(),OneTy())
201 inline __both__
LinearSpace3& operator=(
const LinearSpace3& other ) { vx = other.
vx; vy = other.vy; vz = other.vz;
return *
this; }
206 inline __both__
LinearSpace3(
const vector_t& vx,
const vector_t& vy,
const vector_t& vz)
207 : vx(vx), vy(vy), vz(vz) {}
211 : vx((q.r*q.r + q.i*q.i - q.j*q.j - q.k*q.k), 2.0f*(q.i*q.j + q.r*q.k), 2.0f*(q.i*q.k - q.r*q.j))
212 , vy(2.0f*(q.i*q.j - q.r*q.k), (q.r*q.r - q.i*q.i + q.j*q.j - q.k*q.k), 2.0f*(q.j*q.k + q.r*q.i))
213 , vz(2.0f*(q.i*q.k + q.r*q.j), 2.0f*(q.j*q.k - q.r*q.i), (q.r*q.r - q.i*q.i - q.j*q.j + q.k*q.k)) {}
216 inline __both__
LinearSpace3(
const scalar_t& m00,
const scalar_t& m01,
const scalar_t& m02,
217 const scalar_t& m10,
const scalar_t& m11,
const scalar_t& m12,
218 const scalar_t& m20,
const scalar_t& m21,
const scalar_t& m22)
219 : vx(m00,m10,m20), vy(m01,m11,m21), vz(m02,m12,m22) {}
222 inline __both__
const scalar_t
det()
const {
return dot(vx,cross(vy,vz)); }
234 inline __both__
const vector_t
row0()
const {
return vector_t(vx.x,vy.x,vz.x); }
237 inline __both__
const vector_t
row1()
const {
return vector_t(vx.y,vy.y,vz.y); }
240 inline __both__
const vector_t
row2()
const {
return vector_t(vx.z,vy.z,vz.z); }
248 : vx(ZeroTy()), vy(ZeroTy()), vz(ZeroTy())
251 : vx(OneTy(), ZeroTy(), ZeroTy()),
252 vy(ZeroTy(), OneTy(), ZeroTy()),
253 vz(ZeroTy(), ZeroTy(), OneTy())
269 vector_t u = normalize(_u);
270 scalar_t s = sin(r), c = cos(r);
271 return LinearSpace3(u.x*u.x+(1-u.x*u.x)*c, u.x*u.y*(1-c)-u.z*s, u.x*u.z*(1-c)+u.y*s,
272 u.x*u.y*(1-c)+u.z*s, u.y*u.y+(1-u.y*u.y)*c, u.y*u.z*(1-c)-u.x*s,
273 u.x*u.z*(1-c)-u.y*s, u.y*u.z*(1-c)+u.x*s, u.z*u.z+(1-u.z*u.z)*c);
278 scalar_t tr = a.
vx.x+a.vy.y+a.vz.z+1;
279 vector_t diag(a.
vx.x,a.vy.y,a.vz.z);
281 scalar_t s = owl::common::polymorphic::sqrt(tr) * 2;
286 }
else if (arg_max(diag) == 0) {
287 scalar_t s = owl::common::polymorphic::sqrt(1.f+diag.x-diag.y-diag.z)*2.f;
292 }
else if (arg_max(diag) == 1) {
293 scalar_t s = owl::common::polymorphic::sqrt(1.f+diag.y-diag.x-diag.z)*2.f;
299 scalar_t s = owl::common::polymorphic::sqrt(1.f+diag.z-diag.x-diag.y)*2.f;
319 template<
typename T>
inline __both__ LinearSpace3<T> rcp (
const LinearSpace3<T>& a ) {
return a.inverse(); }
323 inline __both__ LinearSpace3<T> frame(
const T &N)
326 const T dx0 = cross(T(OneTy(),ZeroTy(),ZeroTy()),N);
327 const T dx1 = cross(T(ZeroTy(),OneTy(),ZeroTy()),N);
332 const T dx = normalize(select(dot(dx0,dx0) > dot(dx1,dx1),dx0,dx1));
333 const T dy = normalize(cross(N,dx));
334 return LinearSpace3<T>(dx,dy,N);
338 template<
typename T>
inline __both__ LinearSpace3<T> frame(
const T& N,
const T& dxi)
340 if (abs(dot(dxi,N)) > 0.99f)
return frame(N);
341 const T dx = normalize(cross(dxi,N));
342 const T dy = normalize(cross(N,dx));
343 return LinearSpace3<T>(dx,dy,N);
347 template<
typename T>
inline __both__ LinearSpace3<T> clamp(
const LinearSpace3<T>& space) {
348 return LinearSpace3<T>(clamp(space.vx,T(-1.0f),T(1.0f)),
349 clamp(space.vy,T(-1.0f),T(1.0f)),
350 clamp(space.vz,T(-1.0f),T(1.0f)));
357 template<
typename T>
inline __both__ LinearSpace3<T> operator +(
const LinearSpace3<T>& a,
const LinearSpace3<T>& b ) {
return LinearSpace3<T>(a.vx+b.vx,a.vy+b.vy,a.vz+b.vz); }
358 template<
typename T>
inline __both__ LinearSpace3<T> operator -(
const LinearSpace3<T>& a,
const LinearSpace3<T>& b ) {
return LinearSpace3<T>(a.vx-b.vx,a.vy-b.vy,a.vz-b.vz); }
360 template<
typename T>
inline __both__ LinearSpace3<T> operator*(
const typename T::scalar_t & a,
const LinearSpace3<T>& b) {
return LinearSpace3<T>(a*b.vx, a*b.vy, a*b.vz); }
361 template<
typename T>
inline __both__ T operator*(
const LinearSpace3<T>& a,
const T & b) {
return b.x*a.vx + b.y*a.vy + b.z*a.vz; }
362 template<
typename T>
inline __both__ LinearSpace3<T> operator*(
const LinearSpace3<T>& a,
const LinearSpace3<T>& b) {
return LinearSpace3<T>(a*b.vx, a*b.vy, a*b.vz); }
364 template<
typename T>
inline __both__ LinearSpace3<T> operator/(
const LinearSpace3<T>& a,
const typename T::scalar_t & b) {
return LinearSpace3<T>(a.vx/b, a.vy/b, a.vz/b); }
366 template<
typename T>
inline __both__ LinearSpace3<T> operator/(
const LinearSpace3<T>& a,
const LinearSpace3<T>& b) {
return a * rcp(b); }
368 template<
typename T>
inline LinearSpace3<T>& operator *=( LinearSpace3<T>& a,
const LinearSpace3<T>& b ) {
return a = a * b; }
369 template<
typename T>
inline LinearSpace3<T>& operator /=( LinearSpace3<T>& a,
const LinearSpace3<T>& b ) {
return a = a / b; }
371 template<
typename T>
inline __both__ T xfmPoint (
const LinearSpace3<T>& s,
const T& a) {
return madd(T(a.x),s.vx,madd(T(a.y),s.vy,T(a.z*s.vz))); }
372 template<
typename T>
inline __both__ T xfmVector(
const LinearSpace3<T>& s,
const T& a) {
return madd(T(a.x),s.vx,madd(T(a.y),s.vy,T(a.z*s.vz))); }
373 template<
typename T>
inline __both__ T xfmNormal(
const LinearSpace3<T>& s,
const T& a) {
return xfmVector(s.inverse().transposed(),a); }
379 template<
typename T>
inline bool operator ==(
const LinearSpace3<T>& a,
const LinearSpace3<T>& b ) {
return a.vx == b.vx && a.vy == b.vy && a.vz == b.vz; }
380 template<
typename T>
inline bool operator !=(
const LinearSpace3<T>& a,
const LinearSpace3<T>& b ) {
return a.vx != b.vx || a.vy != b.vy || a.vz != b.vz; }
386 template<
typename T>
inline std::ostream& operator<<(std::ostream& cout,
const LinearSpace3<T>& m) {
387 return cout <<
"{ vx = " << m.vx <<
", vy = " << m.vy <<
", vz = " << m.vz <<
"}";
391 using LinearSpace2f = LinearSpace2<vec2f> ;
392 using LinearSpace3f = LinearSpace3<vec3f> ;
393 using LinearSpace3fa = LinearSpace3<vec3fa>;
395 using linear2f = LinearSpace2f;
396 using linear3f = LinearSpace3f;
2D Linear Transform (2x2 Matrix)
Definition: LinearSpace.h:48
static LinearSpace2 scale(const vector_t &s)
Definition: LinearSpace.h:96
__both__ const LinearSpace2 inverse() const
Definition: LinearSpace.h:77
static LinearSpace2 rotate(const scalar_t &r)
Definition: LinearSpace.h:102
__both__ LinearSpace2(const scalar_t &m00, const scalar_t &m01, const scalar_t &m10, const scalar_t &m11)
Definition: LinearSpace.h:66
__both__ const LinearSpace2 adjoint() const
Definition: LinearSpace.h:74
vector_t vx
Definition: LinearSpace.h:136
LinearSpace2 orthogonal() const
Definition: LinearSpace.h:109
__both__ LinearSpace2(ZeroTy)
Constants.
Definition: LinearSpace.h:92
__both__ LinearSpace2(const vector_t &vx, const vector_t &vy)
Definition: LinearSpace.h:62
const vector_t row1() const
Definition: LinearSpace.h:86
__both__ const LinearSpace2 transposed() const
Definition: LinearSpace.h:80
const vector_t row0() const
Definition: LinearSpace.h:83
__both__ const scalar_t det() const
Definition: LinearSpace.h:71
3D Linear Transform (3x3 Matrix)
Definition: LinearSpace.h:185
__both__ const LinearSpace3 inverse() const
Definition: LinearSpace.h:228
__both__ const vector_t row1() const
Definition: LinearSpace.h:237
__both__ const LinearSpace3 adjoint() const
Definition: LinearSpace.h:225
__both__ LinearSpace3(const vector_t &vx, const vector_t &vy, const vector_t &vz)
Definition: LinearSpace.h:206
__both__ LinearSpace3(const ZeroTy &)
Constants.
Definition: LinearSpace.h:247
__both__ const LinearSpace3 transposed() const
Definition: LinearSpace.h:231
T vx
Definition: LinearSpace.h:310
__both__ LinearSpace3(const QuaternionT< scalar_t > &q)
Definition: LinearSpace.h:210
__both__ const vector_t row2() const
Definition: LinearSpace.h:240
__both__ LinearSpace3()
Definition: LinearSpace.h:193
__both__ LinearSpace3(const scalar_t &m00, const scalar_t &m01, const scalar_t &m02, const scalar_t &m10, const scalar_t &m11, const scalar_t &m12, const scalar_t &m20, const scalar_t &m21, const scalar_t &m22)
Definition: LinearSpace.h:216
static __both__ LinearSpace3 rotate(const vector_t &_u, const scalar_t &r)
Definition: LinearSpace.h:268
static __both__ QuaternionT< scalar_t > rotation(const LinearSpace3 &a)
Definition: LinearSpace.h:277
__both__ const scalar_t det() const
Definition: LinearSpace.h:222
static __both__ LinearSpace3 scale(const vector_t &s)
Definition: LinearSpace.h:261
__both__ const vector_t row0() const
Definition: LinearSpace.h:234
Definition: Quaternion.h:48