ApplyFilter.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 "ApplyFilter.hpp"
25 
26 namespace mimmo{
27 
32  m_name = "mimmo.ApplyFilter";
33  m_factor = std::numeric_limits<double>::max();
34 };
35 
40 ApplyFilter::ApplyFilter(const bitpit::Config::Section & rootXML){
41 
42  m_name = "mimmo.ApplyFilter";
43  m_factor = 1.;
44 
45  std::string fallback_name = "ClassNONE";
46  std::string input = rootXML.get("ClassName", fallback_name);
47  input = bitpit::utils::string::trim(input);
48  if(input == "mimmo.ApplyFilter"){
49  absorbSectionXML(rootXML);
50  }else{
52  };
53 }
54 
58 
63 };
64 
69 void ApplyFilter::swap(ApplyFilter & x) noexcept
70 {
71  Apply::swap(x);
72 }
73 
76 void
78  bool built = true;
79  built = (built && createPortIn<dmpvecarr3E*, ApplyFilter>(this, &ApplyFilter::setInput, M_GDISPLS, true, 1));
80  built = (built && createPortIn<dmpvector1D*, ApplyFilter>(this, &ApplyFilter::setScalarInput, M_SCALARFIELD, true, 1));
81  built = (built && createPortIn<dmpvector1D*, ApplyFilter>(this, &ApplyFilter::setFilter, M_FILTER));
82  built = (built && createPortIn<MimmoSharedPointer<MimmoObject>, Apply>(this, &BaseManipulation::setGeometry, M_GEOM));
83 
84  built = (built && createPortOut<dmpvecarr3E*, ApplyFilter>(this, &ApplyFilter::getOutput, M_GDISPLS));
85 
86  m_arePortsBuilt = built;
87 };
88 
89 
95 void
97 
98  checkInput();
99 
100  m_output = m_input;
101 
102  if (m_factor != std::numeric_limits<double>::max()){
103  for (auto & val : m_output){
104  val *= m_factor;
105  }
106  }
107 
108 };
109 
110 
116 void
117 ApplyFilter::absorbSectionXML(const bitpit::Config::Section & slotXML, std::string name){
118 
119  BITPIT_UNUSED(name);
120  BaseManipulation::absorbSectionXML(slotXML, name);
121 
122  if(slotXML.hasOption("Scaling")){
123  std::string input = slotXML.get("Scaling");
124  input = bitpit::utils::string::trim(input);
125  double val = 1.;
126  if(!input.empty()){
127  std::stringstream ss(input);
128  ss>>val;
129  }
130  setScaling(val);
131  }
132 
133 };
134 
140 void
141 ApplyFilter::flushSectionXML(bitpit::Config::Section & slotXML, std::string name){
142 
143  BITPIT_UNUSED(name);
144  BaseManipulation::flushSectionXML(slotXML, name);
145 
146  slotXML.set("Scaling", std::to_string(m_factor));
147 
148 };
149 
155 void
157 
158  bool check = false;
159  MimmoSharedPointer<MimmoObject> linked_geometry = nullptr;
160  if (m_geometry) linked_geometry = m_geometry;
161  if (m_input.size()){
162  if (!linked_geometry){
163  linked_geometry = m_input.getGeometry();
164  }
165  else{
166  m_input.setGeometry(linked_geometry);
167  }
169  check = check && m_input.completeMissingData({{0.0,0.0,0.0}});
170  }
171  if (m_scalarinput.size()){
172  if (!linked_geometry){
173  linked_geometry = m_scalarinput.getGeometry();
174  }
175  else{
176  m_scalarinput.setGeometry(linked_geometry);
177  }
179  check = check && m_scalarinput.completeMissingData(0.0);
180  if (check){
181  bitpit::SurfaceKernel* skernel = static_cast<bitpit::SurfaceKernel*>(m_scalarinput.getGeometry()->getPatch());
182  bitpit::PiercedVector<darray3E> vNormals;
183  bitpit::ConstProxyVector<long> verts;
184  std::size_t size;
185  long idN;
186  for(const auto & cell: m_scalarinput.getGeometry()->getCells()){
187  verts = cell.getVertexIds();
188  size = verts.size();
189  for(std::size_t i=0; i<size; ++i){
190  idN = verts[i];
191  if(!vNormals.exists(idN)){
192  vNormals.insert(idN, skernel->evalVertexNormal(cell.getId(), i));
193  }
194  }
195  }
196  m_input.clear();
197  for(const auto & vertex: m_scalarinput.getGeometry()->getVertices()){
200  long id = vertex.getId();
201  m_input.insert(id, darray3E(m_scalarinput[id]*vNormals[id]));
202  }
203  }
204  }
205  if (m_filter.size()){
207  // Force filter to link the geometry linked by deformation field
208  bool geometry_check = m_filter.getGeometry() == linked_geometry;
209  if (!geometry_check){
210  m_filter.setGeometry(linked_geometry);
211  }
212  // Complete missing data force filter to 0.
213  check = check && m_filter.completeMissingData(0.0);
214  if (check){
215  // ApplyFilter filter to input deformation field
216  for (const auto & vertex : linked_geometry->getVertices()){
217  long ID = vertex.getId();
218  m_input[ID] *= m_filter[ID];
219  }
220  }
221  }
222  if (!check){
223  m_log->setPriority(bitpit::log::Verbosity::NORMAL);
224  (*m_log) << m_name + " : not valid inputs found" << std::endl;
225  throw std::runtime_error (m_name + " : not valid inputs found");
226  }
227 };
228 
229 }
#define M_GDISPLS
ApplyFilter is a class that applies a filter field to a deformation field defined on a geometry.
Definition: ApplyFilter.hpp:73
dmpvecarr3E m_input
Definition: Apply.hpp:124
#define M_GEOM
Apply is the class that applies the deformation resulting from a manipulation object to the geometry.
Definition: Apply.hpp:89
void setScaling(double alpha)
Definition: Apply.cpp:158
dmpvecarr3E m_output
Definition: Apply.hpp:128
bool completeMissingData(const mpv_t &defValue)
void warningXML(bitpit::Logger *log, std::string name)
MimmoSharedPointer< MimmoObject > getGeometry() const
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
void setInput(dmpvecarr3E *input)
Definition: Apply.cpp:128
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
double m_factor
Definition: Apply.hpp:127
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
MimmoSharedPointer< MimmoObject > m_geometry
void setGeometry(MimmoSharedPointer< MimmoObject > geo)
dmpvector1D m_filter
Definition: Apply.hpp:126
void setDataLocation(MPVLocation loc)
#define M_SCALARFIELD
void setScalarInput(dmpvector1D *input)
Definition: Apply.cpp:138
void setGeometry(MimmoSharedPointer< MimmoObject > geometry)
void swap(ApplyFilter &x) noexcept
Definition: ApplyFilter.cpp:69
std::array< double, 3 > darray3E
void swap(Apply &x) noexcept
Definition: Apply.cpp:88
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
MimmoSharedPointer is a custom implementation of shared pointer.
dmpvecarr3E * getOutput()
Definition: Apply.cpp:216
void setFilter(dmpvector1D *input)
Definition: Apply.cpp:148
dmpvector1D m_scalarinput
Definition: Apply.hpp:125
#define M_FILTER