RotationAxes.cpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo is free software: you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License v3 (LGPL)
13  * as published by the Free Software Foundation.
14  *
15  * mimmo is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23 \*---------------------------------------------------------------------------*/
24 #include "RotationAxes.hpp"
25 
26 namespace mimmo{
27 
28 
33  setOrigin(origin);
34  setDirection(direction);
35 
36  for(auto & val: m_axes)val.fill(0.0);
37  m_axes[0][0] = 1.0;
38  m_axes[1][1] = 1.0;
39  m_axes[1][1] = 1.0;
40  m_axes_origin = {{0,0,0}};
41 
42  for(auto & val: m_rotax)val.fill(0.0);
43  m_rotax[0][0] = 1.0;
44  m_rotax[1][1] = 1.0;
45  m_rotax[1][1] = 1.0;
46  m_rotax_origin = {{0,0,0}};
47 
48  m_alpha=0.0;
49 
50  m_name = "mimmo.RotationAxes";
51 };
52 
57 RotationAxes::RotationAxes(const bitpit::Config::Section & rootXML){
58 
59  m_origin.fill(0.0);
60  m_direction.fill(0.0);
61 
62  for(auto & val: m_axes)
63  val.fill(0.0);
64  m_axes[0][0] = 1.0;
65  m_axes[1][1] = 1.0;
66  m_axes[1][1] = 1.0;
67  m_axes_origin = {{0,0,0}};
68 
69  for(auto & val: m_rotax)
70  val.fill(0.0);
71  m_rotax[0][0] = 1.0;
72  m_rotax[1][1] = 1.0;
73  m_rotax[1][1] = 1.0;
74  m_rotax_origin = {{0,0,0}};
75 
76  m_alpha=0.0;
77 
78  m_name = "mimmo.RotationAxes";
79 
80  std::string fallback_name = "ClassNONE";
81  std::string input = rootXML.get("ClassName", fallback_name);
82  input = bitpit::utils::string::trim(input);
83  if(input == "mimmo.RotationAxes"){
84  absorbSectionXML(rootXML);
85  }else{
87  };
88 }
89 
93 
97  m_axes = other.m_axes;
99  m_alpha=other.m_alpha;
100  m_origin = other.m_origin;
101  m_direction = other.m_direction;
102  for(auto & val: m_rotax)
103  val.fill(0.0);
104  m_rotax[0][0] = 1.0;
105  m_rotax[1][1] = 1.0;
106  m_rotax[1][1] = 1.0;
107  m_rotax_origin = {{0,0,0}};
108 };
109 
113  swap(other);
114  return *this;
115 };
116 
117 
123 {
124  std::swap(m_axes, x.m_axes);
125  std::swap(m_axes_origin, x.m_axes_origin);
126  std::swap(m_alpha, x.m_alpha);
127  std::swap(m_origin, x.m_origin);
128  std::swap(m_direction, x.m_direction);
129  std::swap(m_rotax, x.m_rotax);
130  std::swap(m_rotax_origin, x.m_rotax_origin);
132 }
135 void
137  bool built = true;
138  built = (built && createPortIn<darray3E, RotationAxes>(this, &mimmo::RotationAxes::setOrigin, M_POINT));
139  built = (built && createPortIn<darray3E, RotationAxes>(this, &mimmo::RotationAxes::setDirection, M_AXIS));
140  built = (built && createPortIn<double, RotationAxes>(this, &mimmo::RotationAxes::setRotation, M_VALUED));
141  built = (built && createPortIn<darray3E, RotationAxes>(this, &mimmo::RotationAxes::setAxesOrigin, M_POINT2));
142  built = (built && createPortIn<dmatrix33E, RotationAxes>(this, &mimmo::RotationAxes::setAxes, M_AXES));
143  built = (built && createPortOut<darray3E, RotationAxes>(this, &mimmo::RotationAxes::getRotatedOrigin, M_POINT));
144  built = (built && createPortOut<dmatrix33E, RotationAxes>(this, &mimmo::RotationAxes::getRotatedAxes, M_AXES));
145  m_arePortsBuilt = built;
146 };
147 
152 void
154  setOrigin(origin);
155  setDirection(direction);
156 }
157 
161 void
163  m_origin = origin;
164 }
165 
169 void
171  m_direction = direction;
172  double norm = norm2(m_direction);
173  if(norm > std::numeric_limits<double>::min()){
174  m_direction /= norm;
175  }
176 }
177 
181 void
183  m_alpha = alpha;
184 }
185 
189 void
191  m_axes = axes;
192 }
193 
197 void
199  m_axes_origin = axes_origin;
200 }
201 
207  return(m_rotax);
208 }
209 
213 darray3E
215  return(m_rotax_origin);
216 }
217 
222 void
224 
225  //Rotation of origin
226  m_rotax_origin = {{0,0,0}};
227  m_axes_origin += (-1.0)*m_origin;
228  //rodrigues formula
229  m_rotax_origin = m_axes_origin * std::cos(m_alpha) +
230  dotProduct(m_direction, m_axes_origin) * (1.0 - std::cos(m_alpha)) * m_direction +
231  crossProduct(m_direction, m_axes_origin) * std::sin(m_alpha);
232 
235 
236  //rotation of axes
237  m_rotax.fill(darray3E{{0,0,0}});
238  //rodrigues formula
239  for (int i=0; i<3; i++){
240  m_rotax[i] = m_axes[i] * std::cos(m_alpha) +
241  dotProduct(m_direction, m_axes[i]) * (1.0 - std::cos(m_alpha)) * m_direction +
242  crossProduct(m_direction, m_axes[i]) * std::sin(m_alpha);
243  }
244 
245 };
246 
252 void
253 RotationAxes::absorbSectionXML(const bitpit::Config::Section & slotXML, std::string name){
254 
255  BITPIT_UNUSED(name);
256 
257  BaseManipulation::absorbSectionXML(slotXML, name);
258 
259  if(slotXML.hasOption("Origin")){
260  std::string input = slotXML.get("Origin");
261  input = bitpit::utils::string::trim(input);
262  darray3E temp = {{0.0,0.0,0.0}};
263  if(!input.empty()){
264  std::stringstream ss(input);
265  for(auto &val : temp) ss>>val;
266  }
267  setOrigin(temp);
268  }
269 
270  if(slotXML.hasOption("Direction")){
271  std::string input = slotXML.get("Direction");
272  input = bitpit::utils::string::trim(input);
273  darray3E temp = {{0.0,0.0,0.0}};
274  if(!input.empty()){
275  std::stringstream ss(input);
276  for(auto &val : temp) ss>>val;
277  }
278  setDirection(temp);
279  }
280 
281  if(slotXML.hasOption("Rotation")){
282  std::string input = slotXML.get("Rotation");
283  input = bitpit::utils::string::trim(input);
284  double temp = 0.0;
285  if(!input.empty()){
286  std::stringstream ss(input);
287  ss>>temp;
288  }
289  setRotation(temp);
290  }
291 
292  if(slotXML.hasSection("RefSystem")){
293  const bitpit::Config::Section & rfXML = slotXML.getSection("RefSystem");
294  std::string rootAxis = "axis";
295  std::string axis;
296  dmatrix33E temp;
297  temp[0].fill(0.0); temp[0][0] = 1.0;
298  temp[1].fill(0.0); temp[1][1] = 1.0;
299  temp[2].fill(0.0); temp[2][2] = 1.0;
300  for(int i=0; i<3; ++i){
301  axis = rootAxis + std::to_string(i);
302  std::string input = rfXML.get(axis);
303  input = bitpit::utils::string::trim(input);
304  if(!input.empty()){
305  std::stringstream ss(input);
306  for(auto &val : temp[i]) ss>>val;
307  }
308  }
309  setAxes(temp);
310  }
311 
312  if(slotXML.hasOption("OriginRS")){
313  std::string input = slotXML.get("OriginRS");
314  input = bitpit::utils::string::trim(input);
315  darray3E temp = {{0.0,0.0,0.0}};
316  if(!input.empty()){
317  std::stringstream ss(input);
318  for(auto &val : temp) ss>>val;
319  }
320  setAxesOrigin(temp);
321  }
322 };
323 
329 void
330 RotationAxes::flushSectionXML(bitpit::Config::Section & slotXML, std::string name){
331 
332  BITPIT_UNUSED(name);
333 
334  BaseManipulation::flushSectionXML(slotXML, name);
335 
336  {
337  std::stringstream ss;
338  ss<<std::scientific<<m_origin[0]<<'\t'<<m_origin[1]<<'\t'<<m_origin[2];
339  slotXML.set("Origin", ss.str());
340  }
341 
342  {
343  std::stringstream ss;
344  ss<<std::scientific<<m_direction[0]<<'\t'<<m_direction[1]<<'\t'<<m_direction[2];
345  slotXML.set("Direction", ss.str());
346  }
347 
348  slotXML.set("Rotation", std::to_string(m_alpha));
349 
350  {
351  bitpit::Config::Section & rsXML = slotXML.addSection("RefSystem");
352  std::string rootAxis = "axis";
353  std::string localAxis;
354  int counter=0;
355  for(auto &axis : m_axes){
356  localAxis = rootAxis+std::to_string(counter);
357  std::stringstream ss;
358  ss<<std::scientific<<axis[0]<<'\t'<<axis[1]<<'\t'<<axis[2];
359  rsXML.set(localAxis, ss.str());
360  ++counter;
361  }
362  }
363 
364  {
365  std::stringstream ss;
366  ss<<std::scientific<<m_axes_origin[0]<<'\t'<<m_axes_origin[1]<<'\t'<<m_axes_origin[2];
367  slotXML.set("OriginRS", ss.str());
368  }
369 
370 };
371 
372 }
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
#define M_POINT2
void setOrigin(darray3E origin)
void setAxes(dmatrix33E axes)
void setAxesOrigin(darray3E axes_origin)
void setRotation(double alpha)
#define M_POINT
void warningXML(bitpit::Logger *log, std::string name)
RotationAxes is the class that applies a rotation to a given reference system.
BaseManipulation is the base class of any manipulation object of the library.
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
dmatrix33E getRotatedAxes()
std::array< darray3E, 3 > dmatrix33E
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
RotationAxes & operator=(RotationAxes other)
#define M_AXES
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
void setDirection(darray3E direction)
darray3E getRotatedOrigin()
#define M_VALUED
RotationAxes(darray3E origin={ {0, 0, 0} }, darray3E direction={ {0, 0, 0} })
std::array< double, 3 > darray3E
#define M_AXIS
void swap(RotationAxes &x) noexcept
void setAxis(darray3E origin, darray3E direction)
void swap(BaseManipulation &x) noexcept