common.h (5022B)
1 // File : common.h 2 // Date : Tue Nov 22 15:51:55 2016 3 // Author : Fabian Wermelinger 4 // Description: common stuff 5 // Copyright 2016 ETH Zurich. All Rights Reserved. 6 #ifndef COMMON_H_ZH0IVQBA 7 #define COMMON_H_ZH0IVQBA 8 9 #include <cassert> 10 11 #ifdef _FLOAT_PRECISION_ 12 typedef float Real; 13 #else 14 typedef double Real; 15 #endif 16 17 #ifdef _USE_HDF_ 18 #ifdef _FLOAT_PRECISION_ 19 #define HDF_PRECISION H5T_NATIVE_FLOAT 20 #else 21 #define HDF_PRECISION H5T_NATIVE_DOUBLE 22 #endif 23 #endif /* _USE_HDF_ */ 24 25 #ifndef _ALIGN_ 26 #define _ALIGN_ 16 27 #endif /* _ALIGN_ */ 28 29 #include "Matrix3D.h" 30 31 32 template <typename T, size_t P> 33 struct Face 34 { 35 T* data; 36 int _N; 37 int _M; 38 int _Nelem; 39 static constexpr int _P = P; 40 Face() : data(nullptr) {} 41 Face(const size_t N, const size_t M) : 42 data(new T[N*M*P]), 43 _N(N), _M(M), _Nelem(N*M*P) 44 {} 45 ~Face() { delete [] data; } 46 47 void alloc(const size_t N, const size_t M) 48 { 49 if (data == nullptr) 50 { 51 _N = N; 52 _M = M; 53 _Nelem = N*M*P; 54 data = new T[_Nelem]; 55 } 56 } 57 58 inline T& operator()(const size_t ix, const size_t iy, const size_t iz) 59 { 60 assert(ix < _N); 61 assert(iy < _M); 62 assert(iz < P); 63 return data[ix + iy*_N + iz*_N*_M]; 64 } 65 66 inline T operator()(const size_t ix, const size_t iy, const size_t iz) const 67 { 68 assert(ix < _N); 69 assert(iy < _M); 70 assert(iz < P); 71 return data[ix + iy*_N + iz*_N*_M]; 72 } 73 }; 74 75 76 template <typename T, size_t P, size_t Q> 77 struct Edge 78 { 79 T* data; 80 int _N; 81 int _Nelem; 82 static constexpr int _P = P; 83 static constexpr int _Q = Q; 84 Edge() : data(nullptr) {} 85 Edge(const size_t N) : 86 data(new T[N*P*Q]), 87 _N(N), _Nelem(N*P*Q) 88 {} 89 ~Edge() { delete [] data; } 90 91 void alloc(const size_t N) 92 { 93 if (data == nullptr) 94 { 95 _N = N; 96 _Nelem = N*P*Q; 97 data = new T[_Nelem]; 98 } 99 } 100 101 inline T& operator()(const size_t ix, const size_t iy, const size_t iz) 102 { 103 assert(ix < _N); 104 assert(iy < P); 105 assert(iz < Q); 106 return data[ix + iy*_N + iz*_N*P]; 107 } 108 109 inline T operator()(const size_t ix, const size_t iy, const size_t iz) const 110 { 111 assert(ix < _N); 112 assert(iy < P); 113 assert(iz < Q); 114 return data[ix + iy*_N + iz*_N*P]; 115 } 116 }; 117 118 119 template <typename T, size_t P, size_t Q, size_t R> 120 struct Corner 121 { 122 T* data; 123 const int _Nelem; 124 static constexpr int _P = P; 125 static constexpr int _Q = Q; 126 static constexpr int _R = R; 127 Corner() : data(new T[P*Q*R]), _Nelem(P*Q*R) {} 128 ~Corner() { delete [] data; } 129 130 inline T& operator()(const size_t ix, const size_t iy, const size_t iz) 131 { 132 assert(ix < P); 133 assert(iy < Q); 134 assert(iz < R); 135 return data[ix + iy*P + iz*P*Q]; 136 } 137 138 inline T operator()(const size_t ix, const size_t iy, const size_t iz) const 139 { 140 assert(ix < P); 141 assert(iy < Q); 142 assert(iz < R); 143 return data[ix + iy*P + iz*P*Q]; 144 } 145 }; 146 147 148 typedef Matrix3D<Real,-3,3,-3,3,-3,3> Matrix_t; 149 typedef Face<Real,3> Face_t; 150 typedef Edge<Real,3,3> Edge_t; 151 typedef Corner<Real,3,3,3> Corner_t; 152 153 154 struct Faces 155 { 156 // indices 0 and 1 correspond to the near and far faces relative to the 157 // origin. 158 Face_t x0, x1, y0, y1, z0, z1; 159 Faces() {} 160 Faces(const size_t XN, const size_t XM, 161 const size_t YN, const size_t YM, 162 const size_t ZN, const size_t ZM) : 163 x0(XN,XM), x1(XN,XM), y0(YN,YM), y1(YN,YM), z0(ZN,ZM), z1(ZN,ZM) 164 {} 165 void alloc(const size_t XN, const size_t XM, 166 const size_t YN, const size_t YM, 167 const size_t ZN, const size_t ZM) 168 { 169 x0.alloc(XN,XM); 170 x1.alloc(XN,XM); 171 y0.alloc(YN,YM); 172 y1.alloc(YN,YM); 173 z0.alloc(ZN,ZM); 174 z1.alloc(ZN,ZM); 175 } 176 }; 177 178 struct Edges 179 { 180 // edges are along the given coordinate direction. The counting is done 181 // using right hand rule for the indicated direction. The 0-edge is the 182 // one that goes through the origin. 183 Edge_t x0, x1, x2, x3, y0, y1, y2, y3, z0, z1, z2, z3; 184 Edges() {} 185 Edges(const size_t XN, const size_t YN, const size_t ZN) : 186 x0(XN), x1(XN), x2(XN), x3(XN), 187 y0(YN), y1(YN), y2(YN), y3(YN), 188 z0(ZN), z1(ZN), z2(ZN), z3(ZN) 189 {} 190 void alloc(const size_t XN, const size_t YN, const size_t ZN) 191 { 192 x0.alloc(XN); x1.alloc(XN); x2.alloc(XN); x3.alloc(XN); 193 y0.alloc(YN); y1.alloc(YN); y2.alloc(YN); y3.alloc(YN); 194 z0.alloc(ZN); z1.alloc(ZN); z2.alloc(ZN); z3.alloc(ZN); 195 } 196 197 }; 198 199 struct Corners 200 { 201 // corners are identified by x0 or x1 (near far). The second index is 202 // again given by the right hand rule along the x-axis. The x{0,1}0 are 203 // the ones along the axis that goes through the origin. 204 Corner_t x00, x01, x02, x03, x10, x11, x12, x13; 205 }; 206 207 #endif /* COMMON_H_ZH0IVQBA */