Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 155 additions & 80 deletions cuBQL/math/affine.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,75 +7,102 @@

namespace cuBQL {

#define VectorT typename L::vector_t
#define ScalarT typename L::vector_t::scalar_t

////////////////////////////////////////////////////////////////////////////////
// Affine Space
////////////////////////////////////////////////////////////////////////////////

template<typename L>
struct AffineSpaceT
{
L l; /*< linear part of affine space */
VectorT p; /*< affine part of affine space */

////////////////////////////////////////////////////////////////////////////////
// Constructors, Assignment, Cast, Copy Operations
////////////////////////////////////////////////////////////////////////////////

// inline AffineSpaceT ( ) = default;
// #ifdef __CUDA_ARCH__
inline __cubql_both
AffineSpaceT ( )
: l(OneTy()),
p(ZeroTy())
{}
// #else
// inline __cubql_both AffineSpaceT ( ) : l(one), p(zero) {}
// #endif

inline// __cubql_both
AffineSpaceT ( const AffineSpaceT& other ) = default;
inline __cubql_both AffineSpaceT ( const L & other ) { l = other ; p = VectorT(ZeroTy()); }
inline __cubql_both AffineSpaceT& operator=( const AffineSpaceT& other ) { l = other.l; p = other.p; return *this; }

inline __cubql_both AffineSpaceT( const VectorT& vx, const VectorT& vy, const VectorT& vz, const VectorT& p ) : l(vx,vy,vz), p(p) {}
inline __cubql_both AffineSpaceT( const L& l, const VectorT& p ) : l(l), p(p) {}

template<typename L1> inline __cubql_both AffineSpaceT( const AffineSpaceT<L1>& s ) : l(s.l), p(s.p) {}

////////////////////////////////////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////////////////////////////////////

inline AffineSpaceT( ZeroTy ) : l(ZeroTy()), p(ZeroTy()) {}
inline AffineSpaceT( OneTy ) : l(OneTy()), p(ZeroTy()) {}

/*! return matrix for scaling */
static inline AffineSpaceT scale(const VectorT& s) { return L::scale(s); }

/*! return matrix for translation */
static inline AffineSpaceT translate(const VectorT& p) { return AffineSpaceT(OneTy(),p); }
{
using vector_t = typename L::vector_t;
using linear_t = L;
using scalar_t = typename vector_t::scalar_t;

/*! return matrix for rotation, only in 2D */
static inline AffineSpaceT rotate(const ScalarT& r) { return L::rotate(r); }
linear_t l; /*< linear part of affine space */
vector_t p; /*< affine part of affine space */

/*! return matrix for rotation around arbitrary point (2D) or axis (3D) */
static inline AffineSpaceT rotate(const VectorT& u, const ScalarT& r) { return L::rotate(u,r); }
////////////////////////////////////////////////////////////////////////////////
// Constructors, Assignment, Cast, Copy Operations
////////////////////////////////////////////////////////////////////////////////

/*! return matrix for rotation around arbitrary axis and point, only in 3D */
static inline AffineSpaceT rotate(const VectorT& p, const VectorT& u, const ScalarT& r) { return translate(+p) * rotate(u,r) * translate(-p); }
inline __cubql_both AffineSpaceT()
: l(OneTy()),
p(ZeroTy())
{}

/*! return matrix for looking at given point, only in 3D; right-handed coordinate system */
static inline AffineSpaceT lookat(const VectorT& eye, const VectorT& point, const VectorT& up) {
VectorT Z = normalize(point-eye);
VectorT U = normalize(cross(Z,up));
VectorT V = cross(U,Z);
return AffineSpaceT(L(U,V,Z),eye);
}
inline __cubql_both AffineSpaceT(const AffineSpaceT &other) = default;

inline __cubql_both AffineSpaceT(const L &other)
{
l = other ;
p = vector_t(ZeroTy());
}

inline __cubql_both AffineSpaceT& operator=(const AffineSpaceT& other)
{
l = other.l;
p = other.p;
return *this;
}

inline __cubql_both AffineSpaceT(const vector_t& vx,
const vector_t& vy,
const vector_t& vz,
const vector_t& p)
: l(vx,vy,vz),
p(p)
{}

inline __cubql_both AffineSpaceT(const L& l,
const vector_t& p)
: l(l),
p(p)
{}

template<typename L1> inline __cubql_both AffineSpaceT( const AffineSpaceT<L1>& s )
: l(s.l),
p(s.p)
{}

////////////////////////////////////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////////////////////////////////////

inline AffineSpaceT( ZeroTy ) : l(ZeroTy()), p(ZeroTy()) {}
inline AffineSpaceT( OneTy ) : l(OneTy()), p(ZeroTy()) {}

/*! return matrix for scaling */
static inline AffineSpaceT scale(const vector_t& s) { return L::scale(s); }

/*! return matrix for translation */
static inline AffineSpaceT translate(const vector_t& p) { return AffineSpaceT(OneTy(),p); }

/*! return matrix for rotation, only in 2D */
static inline AffineSpaceT rotate(const scalar_t &r) { return L::rotate(r); }

/*! return matrix for rotation around arbitrary point (2D) or axis (3D) */
static inline AffineSpaceT rotate(const vector_t &u,
const scalar_t &r)
{ return L::rotate(u,r);}

/*! return matrix for rotation around arbitrary axis and point, only in 3D */
static inline AffineSpaceT rotate(const vector_t &p,
const vector_t &u,
const scalar_t &r)
{ return translate(+p) * rotate(u,r) * translate(-p); }

/*! return matrix for looking at given point, only in 3D; right-handed coordinate system */
static inline AffineSpaceT lookat(const vector_t& eye,
const vector_t& point,
const vector_t& up)
{
vector_t Z = normalize(point-eye);
vector_t U = normalize(cross(Z,up));
vector_t V = cross(U,Z);
return AffineSpaceT(L(U,V,Z),eye);
}

};
};

////////////////////////////////////////////////////////////////////////////////
// Unary Operators
Expand All @@ -97,27 +124,74 @@ namespace cuBQL {
template<typename L> inline AffineSpaceT<L> operator +( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return AffineSpaceT<L>(a.l+b.l,a.p+b.p); }
template<typename L> inline AffineSpaceT<L> operator -( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return AffineSpaceT<L>(a.l-b.l,a.p-b.p); }

template<typename L> inline __cubql_both AffineSpaceT<L> operator *( const ScalarT & a, const AffineSpaceT<L>& b ) { return AffineSpaceT<L>(a*b.l,a*b.p); }
template<typename L> inline __cubql_both AffineSpaceT<L> operator *( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return AffineSpaceT<L>(a.l*b.l,a.l*b.p+a.p); }
template<typename L> inline AffineSpaceT<L> operator /( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a * rcp(b); }
template<typename L> inline AffineSpaceT<L> operator /( const AffineSpaceT<L>& a, const ScalarT & b ) { return a * rcp(b); }

template<typename L> inline AffineSpaceT<L>& operator *=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a = a * b; }
template<typename L> inline AffineSpaceT<L>& operator *=( AffineSpaceT<L>& a, const ScalarT & b ) { return a = a * b; }
template<typename L> inline AffineSpaceT<L>& operator /=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a = a / b; }
template<typename L> inline AffineSpaceT<L>& operator /=( AffineSpaceT<L>& a, const ScalarT & b ) { return a = a / b; }

template<typename L> inline __cubql_both const VectorT xfmPoint (const AffineSpaceT<L>& m, const VectorT& p) { return madd(VectorT(p.x),m.l.vx,madd(VectorT(p.y),m.l.vy,madd(VectorT(p.z),m.l.vz,m.p))); }
template<typename L> inline __cubql_both const VectorT xfmVector(const AffineSpaceT<L>& m, const VectorT& v) { return xfmVector(m.l,v); }
template<typename L> inline __cubql_both const VectorT xfmNormal(const AffineSpaceT<L>& m, const VectorT& n) { return xfmNormal(m.l,n); }
template<typename L> inline __cubql_both
AffineSpaceT<L> operator *(const typename AffineSpaceT<L>::scalar_t &a,
const AffineSpaceT<L> &b )
{ return AffineSpaceT<L>(a*b.l,a*b.p); }

template<typename L> inline __cubql_both
AffineSpaceT<L> operator *( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b )
{ return AffineSpaceT<L>(a.l*b.l,a.l*b.p+a.p); }

template<typename L> inline
AffineSpaceT<L> operator /( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b )
{ return a * rcp(b); }

template<typename L> inline
AffineSpaceT<L> operator/(const AffineSpaceT<L> &a,
const typename AffineSpaceT<L>::scalar_t &b)
{ return a * rcp(b); }

template<typename L> inline
AffineSpaceT<L>& operator *=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b )
{ return a = a * b; }

template<typename L> inline
AffineSpaceT<L> &operator*=(AffineSpaceT<L> &a,
const typename AffineSpaceT<L>::scalar_t &b)
{ return a = a * b; }

template<typename L> inline
AffineSpaceT<L>& operator /=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b )
{ return a = a / b; }

template<typename L> inline
AffineSpaceT<L> &operator/=(AffineSpaceT<L> &a,
const typename AffineSpaceT<L>::scalar_t &b)
{ return a = a / b; }

template<typename L> inline __cubql_both
typename AffineSpaceT<L>::vector_t xfmPoint(const AffineSpaceT<L>& m,
const typename AffineSpaceT<L>::vector_t &p)
{
return madd(vector_t(p.x),m.l.vx,
madd(vector_t(p.y),m.l.vy,
madd(vector_t(p.z),m.l.vz,
m.p)));
}

template<typename L> inline __cubql_both
typename AffineSpaceT<L>::vector_t xfmVector(const AffineSpaceT<L>& m,
const typename AffineSpaceT<L>::vector_t& v)
{ return xfmVector(m.l,v); }

template<typename L> inline __cubql_both
typename AffineSpaceT<L>::vector_t xfmNormal(const AffineSpaceT<L>& m,
const typename AffineSpaceT<L>::vector_t& n)
{ return xfmNormal(m.l,n); }


////////////////////////////////////////////////////////////////////////////////
/// Comparison Operators
////////////////////////////////////////////////////////////////////////////////

template<typename L> inline bool operator ==( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a.l == b.l && a.p == b.p; }
template<typename L> inline bool operator !=( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a.l != b.l || a.p != b.p; }
template<typename L> inline
bool operator ==( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b )
{ return a.l == b.l && a.p == b.p; }

template<typename L> inline
bool operator !=( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b )
{ return a.l != b.l || a.p != b.p; }

////////////////////////////////////////////////////////////////////////////////
// Output Operators
Expand All @@ -131,18 +205,19 @@ namespace cuBQL {
// Type Aliases
////////////////////////////////////////////////////////////////////////////////

using AffineSpace2f = AffineSpaceT<LinearSpace2f>;
using AffineSpace3f = AffineSpaceT<LinearSpace3f>;

using affine2f = AffineSpace2f;
using affine3f = AffineSpace3f;
using AffineSpace2f = AffineSpaceT<linear2f>;
using AffineSpace3f = AffineSpaceT<linear3f>;
using AffineSpace2d = AffineSpaceT<linear2d>;
using AffineSpace3d = AffineSpaceT<linear3d>;

////////////////////////////////////////////////////////////////////////////////
/*! Template Specialization for 2D: return matrix for rotation around point (rotation around arbitrarty vector is not meaningful in 2D) */
template<> inline AffineSpace2f AffineSpace2f::rotate(const vec2f& p, const float& r)
{ return translate(+p) * AffineSpace2f(LinearSpace2f::rotate(r)) * translate(-p); }

#undef VectorT
#undef ScalarT

using affine2f = AffineSpace2f;
using affine3f = AffineSpace3f;
using affine2d = AffineSpace2d;
using affine3d = AffineSpace3d;
} // ::cuBQL
4 changes: 4 additions & 0 deletions cuBQL/math/linear.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,13 @@ namespace cuBQL {
/*! Shortcuts for common linear spaces. */
using LinearSpace2f = LinearSpace2<vec2f> ;
using LinearSpace3f = LinearSpace3<vec3f> ;
using LinearSpace2d = LinearSpace2<vec2d> ;
using LinearSpace3d = LinearSpace3<vec3d> ;
// using LinearSpace3fa = LinearSpace3<vec3fa>;

using linear2f = LinearSpace2f;
using linear3f = LinearSpace3f;
using linear2d = LinearSpace2d;
using linear3d = LinearSpace3d;

} // ::cuBQL
16 changes: 13 additions & 3 deletions cuBQL/math/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,24 @@ namespace cuBQL {
using vec3d = vec_t<double,3>;
using vec4d = vec_t<double,4>;

using vec2i = vec_t<int,2>;
using vec3i = vec_t<int,3>;
using vec4i = vec_t<int,4>;
using vec2i = vec_t<int32_t,2>;
using vec3i = vec_t<int32_t,3>;
using vec4i = vec_t<int32_t,4>;

using vec2ui = vec_t<uint32_t,2>;
using vec3ui = vec_t<uint32_t,3>;
using vec4ui = vec_t<uint32_t,4>;


using vec2l = vec_t<int64_t,2>;
using vec3l = vec_t<int64_t,3>;
using vec4l = vec_t<int64_t,4>;

using vec2ul = vec_t<uint64_t,2>;
using vec3ul = vec_t<uint64_t,3>;
using vec4ul = vec_t<uint64_t,4>;


template<typename T, int D>
inline __cubql_both
vec_t<T,D> madd(vec_t<T,D> a, vec_t<T,D> b, vec_t<T,D> c)
Expand Down