g4tools  5.4.0
mat3
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_mat3
5 #define tools_mat3
6 
7 #include "mat"
8 
9 //#include <cmath>
10 
11 namespace tools {
12 
13 template <class T>
14 class mat3 : public mat<T,3> {
15  typedef mat<T,3> parent;
16  typedef mat<T,3> pr;
17 public:
18 #ifdef TOOLS_MEM
19  mat3(bool a_inc = true):parent(a_inc) {}
20 #else
21  mat3():parent() {}
22 #endif
23  mat3(const mat<T,3>& a_from):parent(a_from){}
24  virtual ~mat3() {}
25 public:
26  mat3(const mat3& a_from):parent(a_from){}
27  mat3& operator=(const mat3& a_from){
28  parent::operator=(a_from);
29  return *this;
30  }
31 public:
32  mat3(const T& a_00,const T& a_01,const T& a_02, //first row
33  const T& a_10,const T& a_11,const T& a_12, //second row
34  const T& a_20,const T& a_21,const T& a_22) //third row
35  {
36  set_matrix(a_00,a_01,a_02,
37  a_10,a_11,a_12,
38  a_20,a_21,a_22);
39  }
40 public:
41  void set_matrix(const mat3<T>& a_m){parent::set_matrix(a_m);}
42  void set_matrix(const T& a_00,const T& a_01,const T& a_02, //1 row
43  const T& a_10,const T& a_11,const T& a_12, //2 row
44  const T& a_20,const T& a_21,const T& a_22) { //3 row
45  //a_<R><C>
46  //pr::m_vec[R + C * 3];
47  pr::m_vec[0] = a_00;pr::m_vec[3] = a_01;pr::m_vec[6] = a_02;
48  pr::m_vec[1] = a_10;pr::m_vec[4] = a_11;pr::m_vec[7] = a_12;
49  pr::m_vec[2] = a_20;pr::m_vec[5] = a_21;pr::m_vec[8] = a_22;
50  }
51 
52  void set_scale(const T& a_s) {_set_scale(a_s,a_s,a_s,pr::m_vec);}
53  void set_scale(const T& a_1,const T& a_2,const T& a_3) {_set_scale(a_1,a_2,a_3,pr::m_vec);}
54  void set_rotate(const T& a_x,const T& a_y,const T& a_z,const T& a_angle,T(*a_sin)(T),T(*a_cos)(T)) {
55  _set_rotate(a_x,a_y,a_z,a_angle,pr::m_vec,a_sin,a_cos);
56  }
57 
58  bool get_rotate(T& a_x,T& a_y,T& a_z,T& a_angle,T(*a_acos)(T),T(*a_sin)(T),T(*a_sqrt)(T)) { //warning : acos and not cos.
59  // used in tests/check_pauli. Same code as matTs/get_rotate.
60  // we assume that we have a rotation matrix.
61  // get (a_angle,a_n(x,y,z)) such that matrix is exp(a_angle*a_n*Rs) (passive(=coord) rotation) and then :
62  // exp(a_angle*n*Rs) = cos_angle*I3+(1-cos_angle)*n*n+sin_theta*n*Rs
63  // trace = 3*cos_angle+(1-cos_angle)*n2
64  T cos_angle = T(0.5)*(pr::m_vec[0]+pr::m_vec[4]+pr::m_vec[8]-T(1));
65  if((cos_angle<T(-1))||(T(1)<cos_angle)) { //matrix is not a rotation :
66  a_angle = T(0);
67  a_x = 0;
68  a_y = 0;
69  a_z = 0;
70  return false;
71  }
72  if(cos_angle==T(1)) {
73  a_angle = T(0);
74  a_x = 0;
75  a_y = 0;
76  a_z = 1;
77  return true; //vector is undefined but we return true anyway.
78  }
79  T one_minus_cos_angle = T(1)-cos_angle;
80  T x2 = (pr::m_vec[0]-cos_angle)/one_minus_cos_angle;
81  //T y2 = (pr::m_vec[4]-cos_angle)/one_minus_cos_angle;
82  //T z2 = (pr::m_vec[8]-cos_angle)/one_minus_cos_angle;
83  /*
84  if((x2<T(0))||(y2<T(0))||(z2<T(0))||(x2+y2+z2)!=T(1)) { //not a rotation :
85  a_angle = T(0);
86  a_x = 0;
87  a_y = 0;
88  a_z = 0;
89  return false;
90  }
91  */
92  a_angle = a_acos(cos_angle); //in ]0,pi].
93  T sin_angle = a_sin(a_angle); //in [0,1].
94  if(sin_angle==T(0)) { //angle is pi. //sym part can determine vector, but up to a sign :
95  // sym part is : cos_angle*I+(1-cos_angle)*n*n
96  // cos_angle = -1
97  T xy = T(0.5)*(pr::m_vec[3]+pr::m_vec[1])/one_minus_cos_angle;
98  T xz = T(0.5)*(pr::m_vec[6]+pr::m_vec[2])/one_minus_cos_angle;
99  //T yz = T(0.5)*(pr::m_vec[7]+pr::m_vec[5])/one_minus_cos_angle;
100  a_x = a_sqrt(x2); //up to a sign.
101  a_y = xy/a_x;
102  a_z = xz/a_x;
103  return true; //vector is defined up to a sign.
104  }
105  // antisym part is : sin(theta)*n.Rs
106  a_z = T(0.5)*(pr::m_vec[3]-pr::m_vec[1])/sin_angle;
107  a_y = T(-0.5)*(pr::m_vec[6]-pr::m_vec[2])/sin_angle; //warning : -1.
108  a_x = T(0.5)*(pr::m_vec[7]-pr::m_vec[5])/sin_angle;
109  return true;
110  }
111 
112 public:
113  void mul_3(T& a_x,T& a_y,T& a_z) const {
114  // a_[x,y,z] = this * a_[x,y,z]
115  //pr::m_vec[R + C * 3];
116  T x = pr::m_vec[0]*a_x+pr::m_vec[3]*a_y+pr::m_vec[6]*a_z;
117  T y = pr::m_vec[1]*a_x+pr::m_vec[4]*a_y+pr::m_vec[7]*a_z;
118  T z = pr::m_vec[2]*a_x+pr::m_vec[5]*a_y+pr::m_vec[8]*a_z;
119  a_x = x;
120  a_y = y;
121  a_z = z;
122  }
123 
124  void mul_rotate(const T& a_x,const T& a_y,const T& a_z,const T& a_angle,T(*a_sin)(T),T(*a_cos)(T)) {
125  T rot[9];
126  _set_rotate(a_x,a_y,a_z,a_angle,rot,a_sin,a_cos);
127  parent::_mul_mtx(rot);
128  }
129 
130  void left_mul_rotate(const T& a_x,const T& a_y,const T& a_z,const T& a_angle,T(*a_sin)(T),T(*a_cos)(T)) {
131  T _m[9];
132  _set_rotate(a_x,a_y,a_z,a_angle,_m,a_sin,a_cos);
133  parent::_left_mul_mtx(_m);
134  }
135 
136  void v00(const T& a_value){pr::m_vec[0+0*3] = a_value;}
137  void v10(const T& a_value){pr::m_vec[1+0*3] = a_value;}
138  void v20(const T& a_value){pr::m_vec[2+0*3] = a_value;}
139 
140  void v01(const T& a_value){pr::m_vec[0+1*3] = a_value;}
141  void v11(const T& a_value){pr::m_vec[1+1*3] = a_value;}
142  void v21(const T& a_value){pr::m_vec[2+1*3] = a_value;}
143 
144  void v02(const T& a_value){pr::m_vec[0+2*3] = a_value;}
145  void v12(const T& a_value){pr::m_vec[1+2*3] = a_value;}
146  void v22(const T& a_value){pr::m_vec[2+2*3] = a_value;}
147 
148  const T& v00() const {return pr::m_vec[0+0*3];}
149  const T& v10() const {return pr::m_vec[1+0*3];}
150  const T& v20() const {return pr::m_vec[2+0*3];}
151 
152  const T& v01() const {return pr::m_vec[0+1*3];}
153  const T& v11() const {return pr::m_vec[1+1*3];}
154  const T& v21() const {return pr::m_vec[2+1*3];}
155 
156  const T& v02() const {return pr::m_vec[0+2*3];}
157  const T& v12() const {return pr::m_vec[1+2*3];}
158  const T& v22() const {return pr::m_vec[2+2*3];}
159 
160 protected:
161  static void _set_scale(const T& a_1,const T& a_2,const T& a_3,T v[]) {
162  v[0] = a_1;v[3] = 0;v[6] = 0;
163  v[1] = 0;v[4] = a_2;v[7] = 0;
164  v[2] = 0;v[5] = 0;v[8] = a_3;
165  }
166 
167  static void _set_rotate(const T& a_x,const T& a_y,const T& a_z,const T& a_angle,T v[],T(*a_sin)(T),T(*a_cos)(T)) {
168  //WARNING : it models the rotation of an object (active rotation) = exp(-a_angle*n(a_x,a_y,a_z)*Rs).
169  //WARNING : (a_x,a_y,a_z) must be a normalized vector.
170  T si = a_sin(a_angle);
171  T co = a_cos(a_angle);
172  T x = a_x;
173  T y = a_y;
174  T z = a_z;
175  T x2 = x*x;
176  T y2 = y*y;
177  T z2 = z*z;
178  T xy = x*y;
179  T xz = x*z;
180  T yz = y*z;
181  v[0] = x2+(1-x2)*co;v[3] = xy*(1-co)-z*si;v[6] = xz*(1-co)+y*si;
182  v[1] = xy*(1-co)+z*si;v[4] = y2+(1-y2)*co;v[7] = yz*(1-co)-x*si;
183  v[2] = xz*(1-co)-y*si;v[5] = yz*(1-co)+x*si;v[8] = z2+(1-z2)*co;
184 
185  // If :
186  // n =(x,y,z)
187  // n2 = x2+y2+z2 = 1
188  // n.E = x*E1+y*E2+z*E3
189  // with :
190  // E1 E2 E3
191  // 0 0 0 0 0 -1 0 1 0
192  // 0 0 1 0 0 0 -1 0 0
193  // 0 -1 0 1 0 0 0 0 0
194  //
195  // R(r,c) = cos(theta)*Id(r,c)+(1-cos(theta))*nr*nc-sin(theta)*(n.E)(r,c)
196  //
197  // R = exp(-theta*(n.E))
198 
199  }
200 
201 public:
202 private:static void check_instantiation() {mat3<float> dummy;}
203 };
204 
208 
209 template <class T>
210 inline const mat3<T>& mat3_zero() {
211  static const mat3<T> s_v(false); //inc mem count = false
212  return s_v;
213 }
214 
215 //for sf, mf :
216 //template <class T>
217 //inline const T* get_data(const mat3<T>& a_v) {return a_v.data();}
218 
219 }
220 
221 #include <ostream>
222 
223 namespace tools {
224 
225 template <class T>
226 inline std::ostream& operator<<(std::ostream& a_out,const mat3<T>& a_mtx){
227  const T* v = a_mtx.data();
228  a_out << v[0] << "," << v[3] << "," << v[6] << std::endl
229  << v[1] << "," << v[4] << "," << v[7] << std::endl
230  << v[2] << "," << v[5] << "," << v[8] << std::endl;
231  return a_out;
232 }
233 
234 }
235 
236 #endif
tools::mat3::set_matrix
void set_matrix(const T &a_00, const T &a_01, const T &a_02, const T &a_10, const T &a_11, const T &a_12, const T &a_20, const T &a_21, const T &a_22)
Definition: mat3:42
tools::mat3::operator=
mat3 & operator=(const mat3 &a_from)
Definition: mat3:27
tools::mat3::get_rotate
bool get_rotate(T &a_x, T &a_y, T &a_z, T &a_angle, T(*a_acos)(T), T(*a_sin)(T), T(*a_sqrt)(T))
Definition: mat3:58
tools::mat3::v01
void v01(const T &a_value)
Definition: mat3:140
tools::mat3::v00
const T & v00() const
Definition: mat3:148
tools::mat3::v02
void v02(const T &a_value)
Definition: mat3:144
tools::mat< T, 3 >::m_vec
T m_vec[D *D]
Definition: mat:85
tools::mat3::~mat3
virtual ~mat3()
Definition: mat3:24
tools::mat3::v20
void v20(const T &a_value)
Definition: mat3:138
tools::mat3::v12
const T & v12() const
Definition: mat3:157
tools::mat3::v00
void v00(const T &a_value)
Definition: mat3:136
tools::mat3::set_scale
void set_scale(const T &a_1, const T &a_2, const T &a_3)
Definition: mat3:53
tools::mat3::_set_rotate
static void _set_rotate(const T &a_x, const T &a_y, const T &a_z, const T &a_angle, T v[], T(*a_sin)(T), T(*a_cos)(T))
Definition: mat3:167
tools::mat3::v10
void v10(const T &a_value)
Definition: mat3:137
tools::mat3::mat3
mat3(const T &a_00, const T &a_01, const T &a_02, const T &a_10, const T &a_11, const T &a_12, const T &a_20, const T &a_21, const T &a_22)
Definition: mat3:32
tools::mat3::v12
void v12(const T &a_value)
Definition: mat3:145
tools::mat3::set_scale
void set_scale(const T &a_s)
Definition: mat3:52
tools::mat3::v01
const T & v01() const
Definition: mat3:152
tools::mat3::left_mul_rotate
void left_mul_rotate(const T &a_x, const T &a_y, const T &a_z, const T &a_angle, T(*a_sin)(T), T(*a_cos)(T))
Definition: mat3:130
tools::mat3::mat3
mat3(const mat< T, 3 > &a_from)
Definition: mat3:23
tools::mat3::v21
const T & v21() const
Definition: mat3:154
tools::mat3::mat3
mat3(const mat3 &a_from)
Definition: mat3:26
tools::mat3::v11
const T & v11() const
Definition: mat3:153
tools::mat3::v10
const T & v10() const
Definition: mat3:149
tools::set_matrix
void set_matrix(MATRIX &a_matrix, const std::string &a_fmt)
Definition: sprintf:100
tools::mat3::mat3
mat3()
Definition: mat3:21
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::mat3::set_rotate
void set_rotate(const T &a_x, const T &a_y, const T &a_z, const T &a_angle, T(*a_sin)(T), T(*a_cos)(T))
Definition: mat3:54
tools::mat3::v22
void v22(const T &a_value)
Definition: mat3:146
mat
tools::mat
Definition: mat:19
tools::mat3::_set_scale
static void _set_scale(const T &a_1, const T &a_2, const T &a_3, T v[])
Definition: mat3:161
tools::mat3::v20
const T & v20() const
Definition: mat3:150
tools::mat3::mul_3
void mul_3(T &a_x, T &a_y, T &a_z) const
Definition: mat3:113
tools::mat3_zero
const mat3< T > & mat3_zero()
common matrices : //////////////////////////
Definition: mat3:210
tools::mat3
Definition: mat3:14
tools::operator<<
std::ostream & operator<<(std::ostream &a_out, const mat3< T > &a_mtx)
Definition: mat3:226
tools::mat3::set_matrix
void set_matrix(const mat3< T > &a_m)
Definition: mat3:41
tools::mat3::v02
const T & v02() const
Definition: mat3:156
tools::mat3::v21
void v21(const T &a_value)
Definition: mat3:142
tools::mat< T, 3 >::operator=
mat & operator=(const mat &a_from)
Definition: mat:64
tools::mat3::mul_rotate
void mul_rotate(const T &a_x, const T &a_y, const T &a_z, const T &a_angle, T(*a_sin)(T), T(*a_cos)(T))
Definition: mat3:124
tools::mat3::v22
const T & v22() const
Definition: mat3:158
tools::mat3::v11
void v11(const T &a_value)
Definition: mat3:141