MSpin-JCoupling  2.1
coordinate.h
1 /***************************************************************************
2  coordinate.h - description
3  -------------------
4  author : Armando Navarro-Vazquez
5  email : armando.deus@gmail.com
6  ***************************************************************************/
7 
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 
18 #ifndef MAGNES_COORDINATE_H
19 #define MAGNES_COORDINATE_H
20 
21 #include <iostream>
22 #include <cmath>
23 #include "mathtools.h"
24 #include "coreexport.h"
25 
26 namespace magnes
27 {
28 
33 class MAGNES_CORE_API Coordinate
34 {
35 public:
37  Coordinate();
39  Coordinate(float cx,float cy,float cz);
41  Coordinate operator^(const Coordinate& b) const;
43  float operator*(const Coordinate& b) const;
45  void operator-=(const Coordinate& b);
47  void operator +=(float f);
49  void operator +=(const Coordinate& b);
51  void operator *=(float f);
53  void operator /=(float f);
55  Coordinate operator *(float f) const;
57  Coordinate operator /(float f) const;
59  Coordinate operator -(const Coordinate& b) const;
61  Coordinate operator+(const Coordinate& b) const;
63  float& x() { return m_xyz(0); }
65  const float& x() const { return m_xyz(0); }
67  float& y() { return m_xyz(1); }
69  const float& y() const { return m_xyz(1); }
71  float& z() { return m_xyz(2); }
73  const float& z() const { return m_xyz(2); }
75  operator const D1StaticArray<float,3>& () const { return m_xyz; }
77  operator D1StaticArray<float,3>& () { return m_xyz; }
79  D1Array<double> DoublePrecisionCoords() const;
81  ~Coordinate();
83  static float Distance(const Coordinate& a, const Coordinate& b);
85  float Norm() const;
87  void Normalize();
89  static float Angle(const Coordinate& a, const Coordinate& b);
91  static float Dihedral(const Coordinate& a, const Coordinate& b, const Coordinate& c, const Coordinate& d);
93  static Coordinate MiddlePoint(const Coordinate& a, const Coordinate& b);
94 
96  static Coordinate RotAroundAxis(const Coordinate& c, const Coordinate& axisorigin, const Coordinate& axisend,float angle);
97 private:
99 };
100 
102 inline Coordinate operator*(float f,Coordinate a)
103 {
104  return a*f;
105 }
106 
107 
113  class MAGNES_CORE_API Quaternion
114  {
115  public:
117  Quaternion() { _w=_v.x()=_v.y()=_v.z()=0; }
119  Quaternion(double scalar,const Coordinate& c) { _w=scalar; _v=c; }
121  Coordinate& V() { return _v; }
123  const Coordinate& V() const { return _v; }
125  float& W() { return _w; }
127  const float& W() const { return _w; }
129  float Norm() const
130  {
131  return std::sqrt(_w*_w+_v.x()*_v.x()+_v.y()*_v.y()+_v.z()*_v.z());
132  }
135  {
136  Quaternion q1;
137 
138  q1._w=this->_w;
139  q1._v.x()=-(this->_v.x());
140  q1._v.y()=-(this->_v.y());
141  q1._v.z()=-(this->_v.z());
142  return q1;
143  }
146  {
147  Quaternion q1;
148  q1._w= this->_w*q._w - this->_v.x()*q._v.x() - this->_v.y()*q._v.y() -this->_v.z()*q._v.z();
149  q1._v.x()=this->_w*q._v.x()+this->_v.x()*q._w+this->_v.y()*q._v.z()-this->_v.z()*q._v.y();
150  q1._v.y()=this->_w*q._v.y()+this->_v.y()*q._w+this->_v.z()*q._v.x()-this->_v.x()*q._v.z();
151  q1._v.z()=this->_w*q._v.z()+this->_v.z()*q._w+this->_v.x()*q._v.y()-this->_v.y()*q._v.x();
152 
153  return q1;
154  }
155 
158  {
159  Quaternion c1(0,c);
160  return (*this)^c1;
161  }
162 
163  void Normalize()
164  {
165  float norm=this->Norm();
166  _v.x()/=norm;
167  _v.y()/=norm;
168  _v.z()/=norm;
169  _w/=norm;
170  }
171  static Quaternion FromRotationAxis(float angle,const Coordinate& rv)
172  {
173  angle *= 0.5f;
174  Coordinate nrv=rv;
175  nrv.Normalize();
176 
177  float seno=sin(angle);
178  Quaternion q;
179  q.V().x() = (nrv.x() * seno);
180  q.V().y() = (nrv.y() * seno);
181  q.V().z() = (nrv.z() * seno);
182  q.W() = cos(angle);
183 
184  return q;
185 
186  }
188  void GLMatrix(D2Array<float>& glmatrix)
189  {
190 
191  float xx = V().x()*V().x();
192  float xy = V().x() * V().y();
193  float xz = V().x()*V().z();
194  float xw = V().x()*W();
195  float yy = V().y()*V().y();
196  float yz = V().y() * V().z();
197  float yw = V().y()*W();
198  float zz = V().z()*V().z();
199  float zw = V().z() * W();
200  glmatrix[0] = 1 - 2 * ( yy + zz );
201  glmatrix[1] = 2 * ( xy - zw );
202  glmatrix[2] = 2 * ( xz + yw );
203  glmatrix[4] = 2 * ( xy + zw );
204  glmatrix[5] = 1 - 2 * ( xx + zz );
205  glmatrix[6] = 2 * ( yz - xw );
206  glmatrix[8] = 2 * ( xz - yw );
207  glmatrix[9] = 2 * ( yz + xw );
208  glmatrix[10] = 1 - 2 * ( xx + yy );
209  glmatrix[3] =glmatrix[7] = glmatrix[11] = glmatrix[12] = glmatrix[13] = glmatrix[14] = 0;
210  glmatrix[15] = 1;
211 
212  }
215  {
216 
217  float xx = V().x()*V().x();
218  float xy = V().x() * V().y();
219  float xz = V().x()*V().z();
220  float xw = V().x()*W();
221  float yy = V().y()*V().y();
222  float yz = V().y() * V().z();
223  float yw = V().y()*W();
224  float zz = V().z()*V().z();
225  float zw = V().z() * W();
226  rotmatrix(0,0) = 1 - 2 * ( yy + zz );
227  rotmatrix(0,1) = 2 * ( xy - zw );
228  rotmatrix(0,2) = 2 * ( xz + yw );
229  rotmatrix(1,0) = 2 * ( xy + zw );
230  rotmatrix(1,1) = 1 - 2 * ( xx + zz );
231  rotmatrix(1,2) = 2 * ( yz - xw );
232  rotmatrix(2,0) = 2 * ( xz - yw );
233  rotmatrix(2,1) = 2 * ( yz + xw );
234  rotmatrix(2,2) = 1 - 2 * ( xx + yy );
235 
236  }
237 
238  void RotationMatrix(D2Array<double>& rotmatrix)
239  {
240 
241  double xx = V().x()*V().x();
242  double xy = V().x() * V().y();
243  double xz = V().x()*V().z();
244  double xw = V().x()*W();
245  double yy = V().y()*V().y();
246  double yz = V().y() * V().z();
247  double yw = V().y()*W();
248  double zz = V().z()*V().z();
249  double zw = V().z() * W();
250  rotmatrix(0,0) = 1 - 2 * ( yy + zz );
251  rotmatrix(0,1) = 2 * ( xy - zw );
252  rotmatrix(0,2) = 2 * ( xz + yw );
253  rotmatrix(1,0) = 2 * ( xy + zw );
254  rotmatrix(1,1) = 1 - 2 * ( xx + zz );
255  rotmatrix(1,2) = 2 * ( yz - xw );
256  rotmatrix(2,0) = 2 * ( xz - yw );
257  rotmatrix(2,1) = 2 * ( yz + xw );
258  rotmatrix(2,2) = 1 - 2 * ( xx + yy );
259 
260  }
261 
262 private:
263  float _w;
264  Coordinate _v;
265 
266  };
269 inline MAGNES_CORE_API std::ostream& operator << (std::ostream& s,const Coordinate& c)
270 {
271  s << "(" << c.x() << "," << c.y() << "," << c.z() << ")";
272 
273  return s;
274 }
275 
276 inline MAGNES_CORE_API std::ostream& operator << ( std::ostream& s, const Quaternion& c)
277 {
278  s << "[" << ( c.W() ) << "," << ( c.V() ) << "]" << std::endl;
279  return s;
280 }
281 }
282 #endif // MAGNES_COORDINATE_H
const float & z() const
Definition: coordinate.h:73
Quaternion Invert() const
Definition: coordinate.h:134
const float & x() const
Definition: coordinate.h:65
float & x()
Definition: coordinate.h:63
float & W()
Definition: coordinate.h:125
float & z()
Definition: coordinate.h:71
MAGNES_CORE_API std::ostream & operator<<(std::ostream &s, const Coordinate &c)
Definition: coordinate.h:269
the global magnes namespace
Definition: ccchequation.h:38
const Coordinate & V() const
Definition: coordinate.h:123
void GLMatrix(D2Array< float > &glmatrix)
Definition: coordinate.h:188
Quaternion operator^(const Coordinate &c)
Definition: coordinate.h:157
Quaternion(double scalar, const Coordinate &c)
Definition: coordinate.h:119
float & y()
Definition: coordinate.h:67
void RotationMatrix(D2Array< float > &rotmatrix)
Definition: coordinate.h:214
monodimensional array
Definition: mathtools.h:242
float Norm() const
Definition: coordinate.h:129
const float & y() const
Definition: coordinate.h:69
const float & W() const
Definition: coordinate.h:127
representaion of a 3D point
Definition: coordinate.h:33
Quaternion()
Definition: coordinate.h:117
Coordinate & V()
Definition: coordinate.h:121
quaternion representation
Definition: coordinate.h:113
Coordinate operator*(float f, Coordinate a)
Definition: coordinate.h:102
Quaternion operator^(const Quaternion &q)
Definition: coordinate.h:145