RotationGeometry.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 "RotationGeometry.hpp"
25 
26 namespace mimmo{
27 
28 
35  m_origin = origin;
36  m_direction = direction;
37  m_alpha= 0.0;
38  m_name = "mimmo.RotationGeometry";
39 };
40 
45 RotationGeometry::RotationGeometry(const bitpit::Config::Section & rootXML){
46 
47  m_origin.fill(0.0);
48  m_direction.fill(0.0);
49  m_alpha = 0.0;
50  m_name = "mimmo.RotationGeometry";
51 
52  std::string fallback_name = "ClassNONE";
53  std::string input = rootXML.get("ClassName", fallback_name);
54  input = bitpit::utils::string::trim(input);
55  if(input == "mimmo.RotationGeometry"){
56  absorbSectionXML(rootXML);
57  }else{
59  };
60 }
61 
65 
70  m_origin = other.m_origin;
71  m_direction = other.m_direction;
72  m_alpha = other.m_alpha;
73  m_filter = other.m_filter;
74 };
75 
80  swap(other);
81  return *this;
82 }
83 
89 {
90  std::swap(m_origin, x.m_origin);
91  std::swap(m_direction, x.m_direction);
92  std::swap(m_alpha, x.m_alpha);
93  m_filter.swap(x.m_filter);
94  m_displ.swap(x.m_displ);
96 }
97 
100 void
102  bool built = true;
103  built = (built && createPortIn<darray3E, RotationGeometry>(&m_origin, M_POINT));
104  built = (built && createPortIn<darray3E, RotationGeometry>(&m_direction, M_AXIS));
105  built = (built && createPortIn<double, RotationGeometry>(&m_alpha, M_VALUED));
106  built = (built && createPortIn<dmpvector1D*, RotationGeometry>(this, &mimmo::RotationGeometry::setFilter, M_FILTER));
107  built = (built && createPortIn<MimmoSharedPointer<MimmoObject>, RotationGeometry>(&m_geometry, M_GEOM, true));
108  built = (built && createPortOut<dmpvecarr3E*, RotationGeometry>(this, &mimmo::RotationGeometry::getDisplacements, M_GDISPLS));
109  built = (built && createPortOut<MimmoSharedPointer<MimmoObject>, RotationGeometry>(this, &BaseManipulation::getGeometry, M_GEOM));
110  m_arePortsBuilt = built;
111 };
112 
117 void
119  m_origin = origin;
120  m_direction = direction;
121 }
122 
126 void
128  m_origin = origin;
129 }
130 
134 void
136  m_direction = direction;
137  double L = norm2(m_direction);
138  for (int i=0; i<3; i++)
139  m_direction[i] /= L;
140 }
141 
145 void
147  m_alpha = alpha;
148 }
149 
154 void
156  if(!filter) return;
157  m_filter = *filter;
158 }
159 
166  return &m_displ;
167 };
168 
173 void
175 
176  if(getGeometry() == nullptr){
177  (*m_log)<<m_name + " : nullptr pointer to linked geometry found"<<std::endl;
178  throw std::runtime_error(m_name + "nullptr pointer to linked geometry found");
179  }
180 
181  checkFilter();
182 
183  m_displ.clear();
185  m_displ.reserve(getGeometry()->getNVertices());
186  m_displ.setGeometry(getGeometry());
187 
188 
189  //compute coefficients and constant vectors of rodriguez formula
190  double a = std::cos(m_alpha);
191  darray3E b = (1.0 - std::cos(m_alpha)) * m_direction;
192  double c = std::sin(m_alpha);
193 
194  darray3E point, rotated;
195  long ID;
196  darray3E value;
197  for (const auto & vertex : m_geometry->getVertices()){
198  point = vertex.getCoords();
199  ID = vertex.getId();
200 
201  point -= m_origin;
202  //rodrigues formula
203  rotated = a * point +
204  b * dotProduct(m_direction, point) +
205  c * crossProduct(m_direction, point);
206 
207  rotated += m_origin;
208  point += m_origin;
209 
210  value = (rotated-point)*m_filter[ID];
211  m_displ.insert(ID, value);
212 
213  }
214 };
215 
219 void
221  _apply(m_displ);
222 }
223 
228 void
230  bool check = m_filter.getDataLocation() == mimmo::MPVLocation::POINT;
231  check = check && m_filter.completeMissingData(0.0);
232  check = check && m_filter.getGeometry() == getGeometry();
233 
234  if (!check){
235  m_log->setPriority(bitpit::log::Verbosity::DEBUG);
236  (*m_log)<<"Not valid filter found in "<<m_name<<". Proceeding with default unitary field"<<std::endl;
237  m_log->setPriority(bitpit::log::Verbosity::NORMAL);
238 
239  m_filter.clear();
240  m_filter.setGeometry(m_geometry);
242  m_filter.reserve(getGeometry()->getNVertices());
243  for (const auto & vertex : getGeometry()->getVertices()){
244  m_filter.insert(vertex.getId(), 1.0);
245  }
246  }
247 }
248 
254 void
255 RotationGeometry::absorbSectionXML(const bitpit::Config::Section & slotXML, std::string name){
256 
257  BITPIT_UNUSED(name);
258 
259  BaseManipulation::absorbSectionXML(slotXML, name);
260 
261  if(slotXML.hasOption("Origin")){
262  std::string input = slotXML.get("Origin");
263  input = bitpit::utils::string::trim(input);
264  darray3E temp = {{0.0,0.0,0.0}};
265  if(!input.empty()){
266  std::stringstream ss(input);
267  for(auto &val : temp) ss>>val;
268  }
269  setOrigin(temp);
270  }
271 
272  if(slotXML.hasOption("Direction")){
273  std::string input = slotXML.get("Direction");
274  input = bitpit::utils::string::trim(input);
275  darray3E temp = {{0.0,0.0,0.0}};
276  if(!input.empty()){
277  std::stringstream ss(input);
278  for(auto &val : temp) ss>>val;
279  }
280  setDirection(temp);
281  }
282 
283  if(slotXML.hasOption("Rotation")){
284  std::string input = slotXML.get("Rotation");
285  input = bitpit::utils::string::trim(input);
286  double temp = 0.0;
287  if(!input.empty()){
288  std::stringstream ss(input);
289  ss>>temp;
290  }
291  setRotation(temp);
292  }
293 
294 };
295 
301 void
302 RotationGeometry::flushSectionXML(bitpit::Config::Section & slotXML, std::string name){
303 
304  BITPIT_UNUSED(name);
305 
306  BaseManipulation::flushSectionXML(slotXML, name);
307 
308  {
309  std::stringstream ss;
310  ss<<std::scientific<<m_origin[0]<<'\t'<<m_origin[1]<<'\t'<<m_origin[2];
311  slotXML.set("Origin", ss.str());
312  }
313 
314  {
315  std::stringstream ss;
316  ss<<std::scientific<<m_direction[0]<<'\t'<<m_direction[1]<<'\t'<<m_direction[2];
317  slotXML.set("Direction", ss.str());
318  }
319 
320  slotXML.set("Rotation", std::to_string(m_alpha));
321 
322 };
323 
324 }
#define M_GDISPLS
void setAxis(darray3E origin, darray3E direction)
void setFilter(dmpvector1D *filter)
#define M_GEOM
RotationGeometry is the class that applies a rotation to a given geometry patch.
bool completeMissingData(const mpv_t &defValue)
#define M_POINT
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
void warningXML(bitpit::Logger *log, std::string name)
BaseManipulation is the base class of any manipulation object of the library.
MimmoSharedPointer< MimmoObject > getGeometry() const
RotationGeometry(darray3E origin={ {0, 0, 0} }, darray3E direction={ {0, 0, 0} })
dmpvecarr3E * getDisplacements()
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
void setRotation(double alpha)
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
MimmoSharedPointer< MimmoObject > m_geometry
#define M_VALUED
void setGeometry(MimmoSharedPointer< MimmoObject > geo)
void swap(RotationGeometry &x) noexcept
void _apply(MimmoPiercedVector< darray3E > &displacements)
void setDataLocation(MPVLocation loc)
void setOrigin(darray3E origin)
std::array< double, 3 > darray3E
#define M_AXIS
MimmoSharedPointer< MimmoObject > getGeometry()
void swap(BaseManipulation &x) noexcept
void setDirection(darray3E direction)
#define M_FILTER
RotationGeometry & operator=(RotationGeometry other)