OWL
Loading...
Searching...
No Matches
AffineSpace.h
1// ======================================================================== //
2// Copyright 2018-2019 Ingo Wald //
3// //
4// Licensed under the Apache License, Version 2.0 (the "License"); //
5// you may not use this file except in compliance with the License. //
6// You may obtain a copy of the License at //
7// //
8// http://www.apache.org/licenses/LICENSE-2.0 //
9// //
10// Unless required by applicable law or agreed to in writing, software //
11// distributed under the License is distributed on an "AS IS" BASIS, //
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
13// See the License for the specific language governing permissions and //
14// limitations under the License. //
15// ======================================================================== //
16
17/* originally taken (and adapted) from ospray, under following license */
18
19// ======================================================================== //
20// Copyright 2009-2018 Intel Corporation //
21// //
22// Licensed under the Apache License, Version 2.0 (the "License"); //
23// you may not use this file except in compliance with the License. //
24// You may obtain a copy of the License at //
25// //
26// http://www.apache.org/licenses/LICENSE-2.0 //
27// //
28// Unless required by applicable law or agreed to in writing, software //
29// distributed under the License is distributed on an "AS IS" BASIS, //
30// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
31// See the License for the specific language governing permissions and //
32// limitations under the License. //
33// ======================================================================== //
34
35#pragma once
36
37#include "../math/LinearSpace.h"
38#include "../math/box.h"
39
40namespace owl {
41 namespace common {
42
43#define VectorT typename L::vector_t
44#define ScalarT typename L::vector_t::scalar_t
45
47 // Affine Space
49
50 template<typename L>
51 struct OWL_INTERFACE AffineSpaceT
52 {
53 L l; /*< linear part of affine space */
54 VectorT p; /*< affine part of affine space */
55
57 // Constructors, Assignment, Cast, Copy Operations
59
60 // inline AffineSpaceT ( ) = default;
61// #ifdef __CUDA_ARCH__
62 inline __both__
63 AffineSpaceT ( )
64 : l(OneTy()),
65 p(ZeroTy())
66 {}
67// #else
68// inline __both__ AffineSpaceT ( ) : l(one), p(zero) {}
69// #endif
70
71 inline// __both__
72 AffineSpaceT ( const AffineSpaceT& other ) = default;
73 inline __both__ AffineSpaceT ( const L & other ) { l = other ; p = VectorT(ZeroTy()); }
74 inline __both__ AffineSpaceT& operator=( const AffineSpaceT& other ) { l = other.l; p = other.p; return *this; }
75
76 inline __both__ AffineSpaceT( const VectorT& vx, const VectorT& vy, const VectorT& vz, const VectorT& p ) : l(vx,vy,vz), p(p) {}
77 inline __both__ AffineSpaceT( const L& l, const VectorT& p ) : l(l), p(p) {}
78
79 template<typename L1> inline __both__ AffineSpaceT( const AffineSpaceT<L1>& s ) : l(s.l), p(s.p) {}
80
82 // Constants
84
85 inline AffineSpaceT( ZeroTy ) : l(ZeroTy()), p(ZeroTy()) {}
86 inline AffineSpaceT( OneTy ) : l(OneTy()), p(ZeroTy()) {}
87
89 static inline AffineSpaceT scale(const VectorT& s) { return L::scale(s); }
90
92 static inline AffineSpaceT translate(const VectorT& p) { return AffineSpaceT(OneTy(),p); }
93
95 static inline AffineSpaceT rotate(const ScalarT& r) { return L::rotate(r); }
96
98 static inline AffineSpaceT rotate(const VectorT& u, const ScalarT& r) { return L::rotate(u,r); }
99
101 static inline AffineSpaceT rotate(const VectorT& p, const VectorT& u, const ScalarT& r) { return translate(+p) * rotate(u,r) * translate(-p); }
102
104 static inline AffineSpaceT lookat(const VectorT& eye, const VectorT& point, const VectorT& up) {
105 VectorT Z = normalize(point-eye);
106 VectorT U = normalize(cross(Z,up));
107 VectorT V = cross(U,Z);
108 return AffineSpaceT(L(U,V,Z),eye);
109 }
110
111 };
112
114 // Unary Operators
116
117 template<typename L> inline AffineSpaceT<L> operator -( const AffineSpaceT<L>& a ) { return AffineSpaceT<L>(-a.l,-a.p); }
118 template<typename L> inline AffineSpaceT<L> operator +( const AffineSpaceT<L>& a ) { return AffineSpaceT<L>(+a.l,+a.p); }
119 template<typename L>
120 inline __both__
121 AffineSpaceT<L> rcp( const AffineSpaceT<L>& a ) {
122 L il = rcp(a.l);
123 return AffineSpaceT<L>(il,-(il*a.p));
124 }
125
127 // Binary Operators
129
130 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); }
131 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); }
132
133 template<typename L> inline __both__ AffineSpaceT<L> operator *( const ScalarT & a, const AffineSpaceT<L>& b ) { return AffineSpaceT<L>(a*b.l,a*b.p); }
134 template<typename L> inline __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); }
135 template<typename L> inline AffineSpaceT<L> operator /( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a * rcp(b); }
136 template<typename L> inline AffineSpaceT<L> operator /( const AffineSpaceT<L>& a, const ScalarT & b ) { return a * rcp(b); }
137
138 template<typename L> inline AffineSpaceT<L>& operator *=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a = a * b; }
139 template<typename L> inline AffineSpaceT<L>& operator *=( AffineSpaceT<L>& a, const ScalarT & b ) { return a = a * b; }
140 template<typename L> inline AffineSpaceT<L>& operator /=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a = a / b; }
141 template<typename L> inline AffineSpaceT<L>& operator /=( AffineSpaceT<L>& a, const ScalarT & b ) { return a = a / b; }
142
143 template<typename L> inline __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))); }
144 template<typename L> inline __both__ const VectorT xfmVector(const AffineSpaceT<L>& m, const VectorT& v) { return xfmVector(m.l,v); }
145 template<typename L> inline __both__ const VectorT xfmNormal(const AffineSpaceT<L>& m, const VectorT& n) { return xfmNormal(m.l,n); }
146
147
151
152 template<typename L> inline bool operator ==( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a.l == b.l && a.p == b.p; }
153 template<typename L> inline bool operator !=( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a.l != b.l || a.p != b.p; }
154
156 // Output Operators
158
159 template<typename L> inline std::ostream& operator<<(std::ostream& cout, const AffineSpaceT<L>& m) {
160 return cout << "{ l = " << m.l << ", p = " << m.p << " }";
161 }
162
164 // Type Aliases
166
167 using AffineSpace2f = AffineSpaceT<LinearSpace2f>;
168 using AffineSpace3f = AffineSpaceT<LinearSpace3f>;
169 using AffineSpace3fa = AffineSpaceT<LinearSpace3fa>;
170 using OrthonormalSpace3f = AffineSpaceT<Quaternion3f >;
171
172 using affine2f = AffineSpace2f;
173 using affine3f = AffineSpace3f;
174
176
177 template<> inline AffineSpace2f AffineSpace2f::rotate(const vec2f& p, const float& r)
178 { return translate(+p) * AffineSpace2f(LinearSpace2f::rotate(r)) * translate(-p); }
179
180#undef VectorT
181#undef ScalarT
182
183
184 inline __both__ box3f xfmBounds(const affine3f &xfm,
185 const box3f &box)
186 {
187 box3f dst;
188 const vec3f lo = box.lower;
189 const vec3f hi = box.upper;
190 dst.extend(xfmPoint(xfm,vec3f(lo.x,lo.y,lo.z)));
191 dst.extend(xfmPoint(xfm,vec3f(lo.x,lo.y,hi.z)));
192 dst.extend(xfmPoint(xfm,vec3f(lo.x,hi.y,lo.z)));
193 dst.extend(xfmPoint(xfm,vec3f(lo.x,hi.y,hi.z)));
194 dst.extend(xfmPoint(xfm,vec3f(hi.x,lo.y,lo.z)));
195 dst.extend(xfmPoint(xfm,vec3f(hi.x,lo.y,hi.z)));
196 dst.extend(xfmPoint(xfm,vec3f(hi.x,hi.y,lo.z)));
197 dst.extend(xfmPoint(xfm,vec3f(hi.x,hi.y,hi.z)));
198 return dst;
199 }
200
201 } // ::owl::common
202} // ::owl
Definition: AffineSpace.h:52
static AffineSpaceT lookat(const VectorT &eye, const VectorT &point, const VectorT &up)
Definition: AffineSpace.h:104
static AffineSpaceT rotate(const ScalarT &r)
Definition: AffineSpace.h:95
static AffineSpaceT scale(const VectorT &s)
Definition: AffineSpace.h:89
static AffineSpaceT rotate(const VectorT &p, const VectorT &u, const ScalarT &r)
Definition: AffineSpace.h:101
static AffineSpaceT translate(const VectorT &p)
Definition: AffineSpace.h:92
static AffineSpaceT rotate(const VectorT &u, const ScalarT &r)
Definition: AffineSpace.h:98
static LinearSpace2 rotate(const scalar_t &r)
Definition: LinearSpace.h:102