Skip to content

Commit ea31c28

Browse files
committed
cleanups to affinespace, to make transforms work in double
1 parent 94da41c commit ea31c28

3 files changed

Lines changed: 172 additions & 83 deletions

File tree

cuBQL/math/affine.h

Lines changed: 155 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,75 +7,102 @@
77

88
namespace cuBQL {
99

10-
#define VectorT typename L::vector_t
11-
#define ScalarT typename L::vector_t::scalar_t
12-
1310
////////////////////////////////////////////////////////////////////////////////
1411
// Affine Space
1512
////////////////////////////////////////////////////////////////////////////////
1613

1714
template<typename L>
1815
struct AffineSpaceT
19-
{
20-
L l; /*< linear part of affine space */
21-
VectorT p; /*< affine part of affine space */
22-
23-
////////////////////////////////////////////////////////////////////////////////
24-
// Constructors, Assignment, Cast, Copy Operations
25-
////////////////////////////////////////////////////////////////////////////////
26-
27-
// inline AffineSpaceT ( ) = default;
28-
// #ifdef __CUDA_ARCH__
29-
inline __cubql_both
30-
AffineSpaceT ( )
31-
: l(OneTy()),
32-
p(ZeroTy())
33-
{}
34-
// #else
35-
// inline __cubql_both AffineSpaceT ( ) : l(one), p(zero) {}
36-
// #endif
37-
38-
inline// __cubql_both
39-
AffineSpaceT ( const AffineSpaceT& other ) = default;
40-
inline __cubql_both AffineSpaceT ( const L & other ) { l = other ; p = VectorT(ZeroTy()); }
41-
inline __cubql_both AffineSpaceT& operator=( const AffineSpaceT& other ) { l = other.l; p = other.p; return *this; }
42-
43-
inline __cubql_both AffineSpaceT( const VectorT& vx, const VectorT& vy, const VectorT& vz, const VectorT& p ) : l(vx,vy,vz), p(p) {}
44-
inline __cubql_both AffineSpaceT( const L& l, const VectorT& p ) : l(l), p(p) {}
45-
46-
template<typename L1> inline __cubql_both AffineSpaceT( const AffineSpaceT<L1>& s ) : l(s.l), p(s.p) {}
47-
48-
////////////////////////////////////////////////////////////////////////////////
49-
// Constants
50-
////////////////////////////////////////////////////////////////////////////////
51-
52-
inline AffineSpaceT( ZeroTy ) : l(ZeroTy()), p(ZeroTy()) {}
53-
inline AffineSpaceT( OneTy ) : l(OneTy()), p(ZeroTy()) {}
54-
55-
/*! return matrix for scaling */
56-
static inline AffineSpaceT scale(const VectorT& s) { return L::scale(s); }
57-
58-
/*! return matrix for translation */
59-
static inline AffineSpaceT translate(const VectorT& p) { return AffineSpaceT(OneTy(),p); }
16+
{
17+
using vector_t = typename L::vector_t;
18+
using linear_t = L;
19+
using scalar_t = typename vector_t::scalar_t;
6020

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

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

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

70-
/*! return matrix for looking at given point, only in 3D; right-handed coordinate system */
71-
static inline AffineSpaceT lookat(const VectorT& eye, const VectorT& point, const VectorT& up) {
72-
VectorT Z = normalize(point-eye);
73-
VectorT U = normalize(cross(Z,up));
74-
VectorT V = cross(U,Z);
75-
return AffineSpaceT(L(U,V,Z),eye);
76-
}
33+
inline __cubql_both AffineSpaceT(const AffineSpaceT &other) = default;
34+
35+
inline __cubql_both AffineSpaceT(const L &other)
36+
{
37+
l = other ;
38+
p = vector_t(ZeroTy());
39+
}
40+
41+
inline __cubql_both AffineSpaceT& operator=(const AffineSpaceT& other)
42+
{
43+
l = other.l;
44+
p = other.p;
45+
return *this;
46+
}
47+
48+
inline __cubql_both AffineSpaceT(const vector_t& vx,
49+
const vector_t& vy,
50+
const vector_t& vz,
51+
const vector_t& p)
52+
: l(vx,vy,vz),
53+
p(p)
54+
{}
55+
56+
inline __cubql_both AffineSpaceT(const L& l,
57+
const vector_t& p)
58+
: l(l),
59+
p(p)
60+
{}
61+
62+
template<typename L1> inline __cubql_both AffineSpaceT( const AffineSpaceT<L1>& s )
63+
: l(s.l),
64+
p(s.p)
65+
{}
66+
67+
////////////////////////////////////////////////////////////////////////////////
68+
// Constants
69+
////////////////////////////////////////////////////////////////////////////////
70+
71+
inline AffineSpaceT( ZeroTy ) : l(ZeroTy()), p(ZeroTy()) {}
72+
inline AffineSpaceT( OneTy ) : l(OneTy()), p(ZeroTy()) {}
73+
74+
/*! return matrix for scaling */
75+
static inline AffineSpaceT scale(const vector_t& s) { return L::scale(s); }
76+
77+
/*! return matrix for translation */
78+
static inline AffineSpaceT translate(const vector_t& p) { return AffineSpaceT(OneTy(),p); }
79+
80+
/*! return matrix for rotation, only in 2D */
81+
static inline AffineSpaceT rotate(const scalar_t &r) { return L::rotate(r); }
82+
83+
/*! return matrix for rotation around arbitrary point (2D) or axis (3D) */
84+
static inline AffineSpaceT rotate(const vector_t &u,
85+
const scalar_t &r)
86+
{ return L::rotate(u,r);}
87+
88+
/*! return matrix for rotation around arbitrary axis and point, only in 3D */
89+
static inline AffineSpaceT rotate(const vector_t &p,
90+
const vector_t &u,
91+
const scalar_t &r)
92+
{ return translate(+p) * rotate(u,r) * translate(-p); }
93+
94+
/*! return matrix for looking at given point, only in 3D; right-handed coordinate system */
95+
static inline AffineSpaceT lookat(const vector_t& eye,
96+
const vector_t& point,
97+
const vector_t& up)
98+
{
99+
vector_t Z = normalize(point-eye);
100+
vector_t U = normalize(cross(Z,up));
101+
vector_t V = cross(U,Z);
102+
return AffineSpaceT(L(U,V,Z),eye);
103+
}
77104

78-
};
105+
};
79106

80107
////////////////////////////////////////////////////////////////////////////////
81108
// Unary Operators
@@ -97,27 +124,74 @@ namespace cuBQL {
97124
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); }
98125
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); }
99126

100-
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); }
101-
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); }
102-
template<typename L> inline AffineSpaceT<L> operator /( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a * rcp(b); }
103-
template<typename L> inline AffineSpaceT<L> operator /( const AffineSpaceT<L>& a, const ScalarT & b ) { return a * rcp(b); }
104-
105-
template<typename L> inline AffineSpaceT<L>& operator *=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a = a * b; }
106-
template<typename L> inline AffineSpaceT<L>& operator *=( AffineSpaceT<L>& a, const ScalarT & b ) { return a = a * b; }
107-
template<typename L> inline AffineSpaceT<L>& operator /=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a = a / b; }
108-
template<typename L> inline AffineSpaceT<L>& operator /=( AffineSpaceT<L>& a, const ScalarT & b ) { return a = a / b; }
109-
110-
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))); }
111-
template<typename L> inline __cubql_both const VectorT xfmVector(const AffineSpaceT<L>& m, const VectorT& v) { return xfmVector(m.l,v); }
112-
template<typename L> inline __cubql_both const VectorT xfmNormal(const AffineSpaceT<L>& m, const VectorT& n) { return xfmNormal(m.l,n); }
127+
template<typename L> inline __cubql_both
128+
AffineSpaceT<L> operator *(const typename AffineSpaceT<L>::scalar_t &a,
129+
const AffineSpaceT<L> &b )
130+
{ return AffineSpaceT<L>(a*b.l,a*b.p); }
131+
132+
template<typename L> inline __cubql_both
133+
AffineSpaceT<L> operator *( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b )
134+
{ return AffineSpaceT<L>(a.l*b.l,a.l*b.p+a.p); }
135+
136+
template<typename L> inline
137+
AffineSpaceT<L> operator /( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b )
138+
{ return a * rcp(b); }
139+
140+
template<typename L> inline
141+
AffineSpaceT<L> operator/(const AffineSpaceT<L> &a,
142+
const typename AffineSpaceT<L>::scalar_t &b)
143+
{ return a * rcp(b); }
144+
145+
template<typename L> inline
146+
AffineSpaceT<L>& operator *=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b )
147+
{ return a = a * b; }
148+
149+
template<typename L> inline
150+
AffineSpaceT<L> &operator*=(AffineSpaceT<L> &a,
151+
const typename AffineSpaceT<L>::scalar_t &b)
152+
{ return a = a * b; }
153+
154+
template<typename L> inline
155+
AffineSpaceT<L>& operator /=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b )
156+
{ return a = a / b; }
157+
158+
template<typename L> inline
159+
AffineSpaceT<L> &operator/=(AffineSpaceT<L> &a,
160+
const typename AffineSpaceT<L>::scalar_t &b)
161+
{ return a = a / b; }
162+
163+
template<typename L> inline __cubql_both
164+
typename AffineSpaceT<L>::vector_t xfmPoint(const AffineSpaceT<L>& m,
165+
const typename AffineSpaceT<L>::vector_t &p)
166+
{
167+
return madd(vector_t(p.x),m.l.vx,
168+
madd(vector_t(p.y),m.l.vy,
169+
madd(vector_t(p.z),m.l.vz,
170+
m.p)));
171+
}
172+
173+
template<typename L> inline __cubql_both
174+
typename AffineSpaceT<L>::vector_t xfmVector(const AffineSpaceT<L>& m,
175+
const typename AffineSpaceT<L>::vector_t& v)
176+
{ return xfmVector(m.l,v); }
177+
178+
template<typename L> inline __cubql_both
179+
typename AffineSpaceT<L>::vector_t xfmNormal(const AffineSpaceT<L>& m,
180+
const typename AffineSpaceT<L>::vector_t& n)
181+
{ return xfmNormal(m.l,n); }
113182

114183

115184
////////////////////////////////////////////////////////////////////////////////
116185
/// Comparison Operators
117186
////////////////////////////////////////////////////////////////////////////////
118187

119-
template<typename L> inline bool operator ==( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a.l == b.l && a.p == b.p; }
120-
template<typename L> inline bool operator !=( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a.l != b.l || a.p != b.p; }
188+
template<typename L> inline
189+
bool operator ==( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b )
190+
{ return a.l == b.l && a.p == b.p; }
191+
192+
template<typename L> inline
193+
bool operator !=( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b )
194+
{ return a.l != b.l || a.p != b.p; }
121195

122196
////////////////////////////////////////////////////////////////////////////////
123197
// Output Operators
@@ -131,18 +205,19 @@ namespace cuBQL {
131205
// Type Aliases
132206
////////////////////////////////////////////////////////////////////////////////
133207

134-
using AffineSpace2f = AffineSpaceT<LinearSpace2f>;
135-
using AffineSpace3f = AffineSpaceT<LinearSpace3f>;
136-
137-
using affine2f = AffineSpace2f;
138-
using affine3f = AffineSpace3f;
208+
using AffineSpace2f = AffineSpaceT<linear2f>;
209+
using AffineSpace3f = AffineSpaceT<linear3f>;
210+
using AffineSpace2d = AffineSpaceT<linear2d>;
211+
using AffineSpace3d = AffineSpaceT<linear3d>;
139212

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

145-
#undef VectorT
146-
#undef ScalarT
147218

219+
using affine2f = AffineSpace2f;
220+
using affine3f = AffineSpace3f;
221+
using affine2d = AffineSpace2d;
222+
using affine3d = AffineSpace3d;
148223
} // ::cuBQL

cuBQL/math/linear.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,13 @@ namespace cuBQL {
321321
/*! Shortcuts for common linear spaces. */
322322
using LinearSpace2f = LinearSpace2<vec2f> ;
323323
using LinearSpace3f = LinearSpace3<vec3f> ;
324+
using LinearSpace2d = LinearSpace2<vec2d> ;
325+
using LinearSpace3d = LinearSpace3<vec3d> ;
324326
// using LinearSpace3fa = LinearSpace3<vec3fa>;
325327

326328
using linear2f = LinearSpace2f;
327329
using linear3f = LinearSpace3f;
330+
using linear2d = LinearSpace2d;
331+
using linear3d = LinearSpace3d;
328332

329333
} // ::cuBQL

cuBQL/math/vec.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,24 @@ namespace cuBQL {
228228
using vec3d = vec_t<double,3>;
229229
using vec4d = vec_t<double,4>;
230230

231-
using vec2i = vec_t<int,2>;
232-
using vec3i = vec_t<int,3>;
233-
using vec4i = vec_t<int,4>;
231+
using vec2i = vec_t<int32_t,2>;
232+
using vec3i = vec_t<int32_t,3>;
233+
using vec4i = vec_t<int32_t,4>;
234234

235235
using vec2ui = vec_t<uint32_t,2>;
236236
using vec3ui = vec_t<uint32_t,3>;
237237
using vec4ui = vec_t<uint32_t,4>;
238238

239+
240+
using vec2l = vec_t<int64_t,2>;
241+
using vec3l = vec_t<int64_t,3>;
242+
using vec4l = vec_t<int64_t,4>;
243+
244+
using vec2ul = vec_t<uint64_t,2>;
245+
using vec3ul = vec_t<uint64_t,3>;
246+
using vec4ul = vec_t<uint64_t,4>;
247+
248+
239249
template<typename T, int D>
240250
inline __cubql_both
241251
vec_t<T,D> madd(vec_t<T,D> a, vec_t<T,D> b, vec_t<T,D> c)

0 commit comments

Comments
 (0)