Points&Forces (core)
Software tools facilitating the task of surveying architecture
a_pos.cxx
Go to the documentation of this file.
1 /*
2 Copyright 2000-2019 Pierre SMARS (smars@yuntech.edu.tw)
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 2 of the License, or
6 (at your option) any later version.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 #include "a_pos.h"
17 #include <sstream>
18 //---------------------------------------------------------------------------
19 const std::string a_pos::help()
20 {
21  std::ostringstream o;
22  o << "*****" << std::endl;
23  o << "a_pos" << std::endl;
24  o << "*****" << std::endl;
25  o << "This is a pose class (position + orientation)" << std::endl;
26  o << "Commands:" << std::endl;
27  o << "--------" << std::endl;
28  o << "translate x y z" << std::endl;
29  o << "translate a_point" << std::endl;
30  o << "rotateX angle (degree)" << std::endl;
31  o << "rotateY angle (degree)" << std::endl;
32  o << "rotateZ angle (degree)" << std::endl;
33  o << "rotate a_quaternion" << std::endl;
34  o << "orient a_point (new z axis)" << std::endl;
35  return o.str();
36 }
37 //---------------------------------------------------------------------------
38 a_pos::a_pos(const a_mat& m) : a_mat(4,4)
39 {
40  for (int i = 0; i<4; i++)
41  {
42  for (int j = 0; j<4; j++)
43  (*this)(i,j)= m(i,j);
44  }
45 }
46 //---------------------------------------------------------------------------
47 a_pos & a_pos::translate(double x, double y, double z)
48 {
49  double v[] = {x,y,z};
50  for (int i = 0; i<3; i++)
51  (*this)(i,3) -= v[i];
52  return (*this);
53 }
54 //---------------------------------------------------------------------------
56 {
57  this->translate(t.x(),t.y(),t.z());
58  return (*this);
59 }
60 //---------------------------------------------------------------------------
61 a_pos & a_pos::rotateX(double v)
62 {
63  a_pos m;
64  m(1,1) = m(2,2) = cos(v);
65  m(2,1) = -sin(v);
66  m(1,2) = sin(v);
67  a_mat m2 = m*(*this);
68  (*this) = m2;
69  return (*this);
70 }
71 //---------------------------------------------------------------------------
72 a_pos & a_pos::rotateY(double v)
73 {
74  a_pos m;
75  m(0,0) = m(2,2) = cos(v);
76  m(2,0) = sin(v);
77  m(0,2) = -sin(v);
78  a_mat m2 = m*(*this);
79  (*this) = m2;
80  return (*this);
81 }
82 //---------------------------------------------------------------------------
83 a_pos & a_pos::rotateZ(double v)
84 {
85  a_pos m;
86  m(0,0) = m(1,1) = cos(v);
87  m(1,0) = -sin(v);
88  m(0,1) = sin(v);
89  a_mat m2 = m*(*this);
90  (*this) = m2;
91  return (*this);
92 }
93 //---------------------------------------------------------------------------
95 {
96  zn.normalise();
97  //new z in the direction of reference
98  a_point xn,yn;
99  a_point x(1,0,0);
100  a_point y(0,1,0);
101  a_point z(0,0,1);
102  if (zn.x()>zn.y()) //zn is closer to x than y
103  {
104  xn = cross(y,zn).normalise();
105  yn = cross(zn,xn).normalise();
106  }
107  else //zn is closer to y than x
108  {
109  yn = cross(zn,x).normalise();
110  xn = cross(yn,zn).normalise();
111  }
112  a_pos m;
113  m(0,0) = xn.x();
114  m(0,1) = xn.y();
115  m(0,2) = xn.z();
116  m(1,0) = yn.x();
117  m(1,1) = yn.y();
118  m(1,2) = yn.z();
119  m(2,0) = zn.x();
120  m(2,1) = zn.y();
121  m(2,2) = zn.z();
122  (*this) = m;
123  return (*this);
124 }
125 //---------------------------------------------------------------------------
127 {
128  double x = q.r();
129  double y = q.i();
130  double z = q.j();
131  double w = q.k();
132  a_pos m;
133  m(0,0) = 1.-2.*y*y-2.*z*z;
134  m(0,1) = 2.*x*y-2.*z*w;
135  m(0,2) = 2.*x*z+2.*y*w;
136  m(1,0) = 2.*x*y+2.*z*w;
137  m(1,1) = 1.-2.*x*x-2.*z*z;
138  m(1,2) = 2.*y*z-2.*x*w;
139  m(2,0) = 2.*x*z-2.*y*w;
140  m(2,1) = 2.*y*z+2.*x*w;
141  m(2,2) = 1.-2.*x*x-2.*y*y;
142  (*this) = m;
143  return (*this);
144 }
145 
a_point cross(const a_point &a, const a_point &b)
Definition: a_point.cxx:284
Definition: a_mat.h:42
std::valarray< double > & x() const
Definition: a_mat.h:53
a_point & normalise()
Definition: a_point.cxx:190
double z() const
Definition: a_point.h:56
double y() const
Definition: a_point.h:55
double x() const
Definition: a_point.h:54
position matrix
Definition: a_pos.h:31
a_pos & rotateY(double v)
Definition: a_pos.cxx:72
static const std::string help()
Definition: a_pos.cxx:19
a_pos & orient(a_point dir)
Definition: a_pos.cxx:94
a_pos & translate(double x, double y, double z)
Definition: a_pos.cxx:47
a_pos & rotateX(double v)
Definition: a_pos.cxx:61
a_pos()
Definition: a_pos.h:33
a_pos & rotate(const a_quaternion &q)
Definition: a_pos.cxx:126
a_pos & rotateZ(double v)
Definition: a_pos.cxx:83
double k() const
Definition: a_quaternion.h:30
double j() const
Definition: a_quaternion.h:29
double r() const
Definition: a_quaternion.h:27
double i() const
Definition: a_quaternion.h:28