CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ExtVec.h
Go to the documentation of this file.
1 #ifndef DataFormat_Math_ExtVec_H
2 #define DataFormat_Math_ExtVec_H
3 
4 #include<type_traits>
5 
6 typedef float __attribute__( ( vector_size( 8 ) ) ) float32x2_t;
7 typedef float __attribute__( ( vector_size( 16 ) ) ) float32x4_t;
8 typedef float __attribute__( ( vector_size( 32 ) ) ) float32x8_t;
9 typedef double __attribute__( ( vector_size( 16 ) ) ) float64x2_t;
10 typedef double __attribute__( ( vector_size( 32 ) ) ) float64x4_t;
11 typedef double __attribute__( ( vector_size( 64 ) ) ) float64x8_t;
12 
13 
14 // template<typename T, int N> using ExtVec = T __attribute__( ( vector_size( N*sizeof(T) ) ) );
15 
16 template<typename T, int N>
17 struct ExtVecTraits {
18  typedef T __attribute__( ( vector_size( N*sizeof(T) ) ) ) type;
19 };
20 
21 template<typename T, int N> using ExtVec = typename ExtVecTraits<T,N>::type;
22 
23 template<typename T> using Vec4 = ExtVec<T,4>;
24 template<typename T> using Vec2 = ExtVec<T,2>;
25 
26 template<typename V>
27 inline
28 auto xy(V v)-> Vec2<typename std::remove_reference<decltype(v[0])>::type>
29 {
31  return Vec2<T>{v[0],v[1]};
32 }
33 
34 template<typename V>
35 inline
37 {
39  return Vec2<T>{v[2],v[3]};
40 }
41 
42 template<typename Vec, typename F>
43 inline
44 Vec apply(Vec v, F f) {
46  constexpr int N = sizeof(Vec)/sizeof(T);
47  Vec ret;
48  for (int i=0;i!=N;++i) ret[i] = f(v[i]);
49  return ret;
50 }
51 
52 
53 template<typename Vec>
54 inline
55 Vec cross3(Vec x, Vec y) {
56  // typedef Vec4<T> Vec;
57  // yz - zy, zx - xz, xy - yx, 0
58  Vec x1200{ x[1], x[2], x[0], x[0] };
59  Vec y2010{ y[2], y[0], y[1], y[0] };
60  Vec x2010{ x[2], x[0], x[1], x[0] };
61  Vec y1200{ y[1], y[2], y[0], y[0] };
62  return x1200 * y2010 - x2010 * y1200;
63 }
64 
65 template<typename V1,typename V2>
66 inline
68  return x[0]*y[1]-x[1]*y[0];
69 }
70 
71 
72 
73 /*
74 template<typename T>
75 T dot_product(Vec4<T> x, Vec4<T> y) {
76  auto res = x*y;
77  T ret=0;
78  for (int i=0;i!=4;++i) ret+=res[i];
79  return ret;
80 }
81 */
82 
83 /*
84 template<typename V, int K>
85 inline
86 V get1(V v) { return (V){v[K]}; }
87 */
88 
89 /*
90 template<typename T, int N>
91 inline
92 T dot(ExtVec<T,N> x, ExtVec<T,N> y) {
93  T ret=0;
94  for (int i=0;i!=N;++i) ret+=x[i]*y[i];
95  return ret;
96 }
97 */
98 
99 template<typename V>
100 inline
103  constexpr int N = sizeof(V)/sizeof(T);
104  T ret=0;
105  for (int i=0;i!=N;++i) ret+=x[i]*y[i];
106  return ret;
107 }
108 
109 template<typename V1,typename V2 >
110 inline
113  T ret=0;
114  for (int i=0;i!=2;++i) ret+=x[i]*y[i];
115  return ret;
116 }
117 
118 
119 
120 
127 
128 /*
129 template<typename T>
130 struct As3D {
131  Vec4<T> const & v;
132  As3D(Vec4<T> const &iv ) : v(iv){}
133 };
134 template<typename T>
135 inline As3D<T> as3D(Vec4<T> const &v ) { return v;}
136 */
137 
138 template<typename V>
139 struct As3D {
140  V const & v;
141  As3D(V const &iv ) : v(iv){}
142 };
143 template<typename V>
144 inline As3D<V> as3D(V const &v ) { return v;}
145 
146 
147 // rotations
148 
149 template<typename T>
150 struct Rot3 {
151  typedef Vec4<T> Vec;
152  Vec axis[3];
153 
155  axis{ (Vec){T(1),0,0,0},
156  (Vec){0,T(1),0,0},
157  (Vec){0,0,T(1),0}
158  }{}
159 
160  constexpr Rot3( Vec4<T> ix, Vec4<T> iy, Vec4<T> iz) :
161  axis{ix,iy,iz}{}
162 
163  constexpr Rot3( T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz) :
164  axis{ (Vec){xx,xy,xz,0},
165  (Vec){yx,yy,yz,0},
166  (Vec){zx,zy,zz,0}
167  }{}
168 
169  constexpr Rot3 transpose() const {
170  return Rot3( axis[0][0], axis[1][0], axis[2][0],
171  axis[0][1], axis[1][1], axis[2][1],
172  axis[0][2], axis[1][2], axis[2][2]
173  );
174  }
175 
176  constexpr Vec4<T> x() const { return axis[0];}
177  constexpr Vec4<T> y() const { return axis[1];}
178  constexpr Vec4<T> z() const { return axis[2];}
179 
180  // toLocal...
181  constexpr Vec4<T> rotate(Vec4<T> v) const {
182  return transpose().rotateBack(v);
183  }
184 
185 
186  // toGlobal...
187  constexpr Vec4<T> rotateBack(Vec4<T> v) const {
188  return v[0]*axis[0] + v[1]*axis[1] + v[2]*axis[2];
189  }
190 
191  Rot3 rotate(Rot3 const& r) const {
192  Rot3 tr = transpose();
193  return Rot3(tr.rotateBack(r.axis[0]),tr.rotateBack(r.axis[1]),tr.rotateBack(r.axis[2]));
194  }
195 
196  constexpr Rot3 rotateBack(Rot3 const& r) const {
197  return Rot3(rotateBack(r.axis[0]),rotateBack(r.axis[1]),rotateBack(r.axis[2]));
198  }
199 
200 
201  };
202 
204 
206 
207 template<typename T>
208 inline constexpr Rot3<T> operator *(Rot3<T> const & rh, Rot3<T> const & lh) {
209  return lh.rotateBack(rh);
210 }
211 
212 
213 template<typename T>
214 struct Rot2 {
215  typedef Vec2<T> Vec;
217 
219  axis{ (Vec){T(1),0},
220  (Vec){0,T(1)}
221  }{}
222 
223  constexpr Rot2( Vec2<T> ix, Vec2<T> iy) :
224  axis{ix, iy}{}
225 
226  constexpr Rot2( T xx, T xy, T yx, T yy) :
227  Rot2( (Vec){xx,xy},
228  (Vec){yx,yy}
229  ){}
230 
231  constexpr Rot2 transpose() const {
232  return Rot2( axis[0][0], axis[1][0],
233  axis[0][1], axis[1][1]
234  );
235  }
236 
237  constexpr Vec2<T> x() { return axis[0];}
238  constexpr Vec2<T> y() { return axis[1];}
239 
240  // toLocal...
241  constexpr Vec2<T> rotate(Vec2<T> v) const {
242  return transpose().rotateBack(v);
243  }
244 
245 
246  // toGlobal...
247  constexpr Vec2<T> rotateBack(Vec2<T> v) const {
248  return v[0]*axis[0] + v[1]*axis[1];
249  }
250 
251  Rot2 rotate(Rot2 const& r) const {
252  Rot2 tr = transpose();
253  return Rot2(tr.rotateBack(r.axis[0]),tr.rotateBack(r.axis[1]));
254  }
255 
256  constexpr Rot2 rotateBack(Rot2 const& r) const {
257  return Rot2(rotateBack(r.axis[0]),rotateBack(r.axis[1]));
258  }
259 
260 
261 };
262 
264 
266 
267 
268 
269 template<typename T>
270 inline constexpr Rot2<T> operator *(Rot2<T> const & rh, Rot2<T> const & lh) {
271  return lh.rotateBack(rh);
272 }
273 
274 
275 
276 #include <iosfwd>
277 std::ostream & operator<<(std::ostream & out, Vec2D const & v);
278 std::ostream & operator<<(std::ostream & out, Vec2F const & v);
279 std::ostream & operator<<(std::ostream & out, Vec4F const & v);
280 std::ostream & operator<<(std::ostream & out, Vec4D const & v);
281 
282 std::ostream & operator<<(std::ostream & out, As3D<Vec4F> const & v);
283 std::ostream & operator<<(std::ostream & out, As3D<Vec4D> const & v);
284 
285 std::ostream & operator<<(std::ostream & out, Rot3F const & v);
286 std::ostream & operator<<(std::ostream & out, Rot3D const & v);
287 std::ostream & operator<<(std::ostream & out, Rot2F const & v);
288 std::ostream & operator<<(std::ostream & out, Rot2D const & v);
289 
290 
291 #ifdef USE_INLINE_IO
292 #include <ostream>
293 std::ostream & operator<<(std::ostream & out, ::Vec4F const & v) {
294  return out << '(' << v[0] <<", " << v[1] <<", "<< v[2] <<", "<< v[3] <<')';
295 }
296 std::ostream & operator<<(std::ostream & out, ::Vec4D const & v) {
297  return out << '(' << v[0] <<", " << v[1] <<", "<< v[2] <<", "<< v[3] <<')';
298 }
299 std::ostream & operator<<(std::ostream & out, ::Vec2F const & v) {
300  return out << '(' << v[0] <<", " << v[1] <<')';
301 }
302 std::ostream & operator<<(std::ostream & out, ::Vec2D const & v) {
303  return out << '(' << v[0] <<", " << v[1] <<')';
304 }
305 
306 std::ostream & operator<<(std::ostream & out, ::As3D<Vec4F> const & v) {
307  return out << '(' << v.v[0] <<", " << v.v[1] <<", "<< v.v[2] <<')';
308 }
309 
310 std::ostream & operator<<(std::ostream & out, ::As3D<Vec4D> const & v) {
311  return out << '(' << v.v[0] <<", " << v.v[1] <<", "<< v.v[2] <<')';
312 }
313 
314 std::ostream & operator<<(std::ostream & out, ::Rot3F const & r){
315  return out << as3D(r.axis[0]) << '\n' << as3D(r.axis[1]) << '\n' << as3D(r.axis[2]);
316 }
317 
318 std::ostream & operator<<(std::ostream & out, ::Rot3D const & r){
319  return out << as3D(r.axis[0]) << '\n' << as3D(r.axis[1]) << '\n' << as3D(r.axis[2]);
320 }
321 
322 std::ostream & operator<<(std::ostream & out, ::Rot2F const & r){
323  return out << r.axis[0] << '\n' << r.axis[1];
324 }
325 
326 std::ostream & operator<<(std::ostream & out, ::Rot2D const & r){
327  return out << r.axis[0] << '\n' << r.axis[1];
328 }
329 #endif
330 
331 
332 #endif
type
Definition: HCALResponse.h:21
As3D(V const &iv)
Definition: ExtVec.h:141
int i
Definition: DBlmapReader.cc:9
Vec4< T > Vec
Definition: ExtVec.h:151
Vec2< T > Vec
Definition: ExtVec.h:215
auto cross2(V1 x, V2 y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:67
Rot2< double > Rot2D
Definition: ExtVec.h:265
ExtVec< T, 4 > Vec4
Definition: ExtVec.h:23
auto zw(V v) -> Vec2< typenamestd::remove_reference< decltype(v[0])>::type >
Definition: ExtVec.h:36
typename ExtVecTraits< T, N >::type ExtVec
Definition: ExtVec.h:21
Rot2< float > Rot2F
Definition: ExtVec.h:263
Rot3< float > Rot3F
Definition: ExtVec.h:203
constexpr Rot2()
Definition: ExtVec.h:218
Vec4< float > Vec3F
Definition: ExtVec.h:123
Vec cross3(Vec x, Vec y)
Definition: ExtVec.h:55
bool int lh
Definition: SIMDVec.h:19
std::ostream & operator<<(std::ostream &out, const ALILine &li)
Definition: ALILine.cc:187
Vec4< float > Vec4F
Definition: ExtVec.h:122
float float float z
Vec4< double > Vec4D
Definition: ExtVec.h:126
Vec2< T > axis[2]
Definition: ExtVec.h:216
T __attribute__((vector_size(N *sizeof(T)))) type
Definition: ExtVec.h:18
Vec2< float > Vec2F
Definition: ExtVec.h:121
auto dot2(V1 x, V2 y) -> typenamestd::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:111
V const & v
Definition: ExtVec.h:140
Definition: ExtVec.h:139
double f[11][100]
tuple out
Definition: dbtoconf.py:99
#define N
Definition: blowfish.cc:9
float __attribute__((vector_size(8))) float32x2_t
Definition: ExtVec.h:6
ExtVec< T, 2 > Vec2
Definition: ExtVec.h:24
Vec
Definition: ExtVec.h:156
Definition: ExtVec.h:150
def rotate
Definition: svgfig.py:704
Definition: ExtVec.h:214
T dot(const Basic3DVector &v) const
Scalar product, or &quot;dot&quot; product, with a vector of same type.
Vec2< double > Vec2D
Definition: ExtVec.h:124
Vec4< double > Vec3D
Definition: ExtVec.h:125
As3D< V > as3D(V const &v)
Definition: ExtVec.h:144
MatrixMeschach operator*(const MatrixMeschach &mat1, const MatrixMeschach &mat2)
Definition: DDAxes.h:10
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
Rot3< double > Rot3D
Definition: ExtVec.h:205
long double T
Vec axis[3]
Definition: ExtVec.h:152
constexpr Rot3()
Definition: ExtVec.h:154
#define constexpr
Vec apply(Vec v, F f)
Definition: ExtVec.h:44
def template
Definition: svgfig.py:520