utils_example_00003.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 
25 #include "mimmo_utils.hpp"
26 #include "MeshSelection.hpp"
27 #include "ReconstructFields.hpp"
28 #include "MRBF.hpp"
29 #include "Apply.hpp"
30 #if MIMMO_ENABLE_MPI
31  #include "Partition.hpp"
32 #endif
33 // =================================================================================== //
50 void test00003() {
51 
52  mimmo::setExpertMode(true); // avoid control of unlinked ports.
53 
54  /*
55  Read a target geometry from file. CONVERT option will let the block to write
56  the just read file in another file, immediately after the reading.
57  */
59  mimmo0->setName("converterGeometry");
60  mimmo0->setReadDir("geodata");
61  mimmo0->setReadFileType(FileType::STL);
62  mimmo0->setReadFilename("sphere2");
63  mimmo0->setWriteDir(".");
64  mimmo0->setWriteFileType(FileType::SURFVTU);
65  mimmo0->setWriteFilename("utils_mesh_00003.0000");
66 
67 #if MIMMO_ENABLE_MPI
68  /*
69  Distribute target mesh among processes.
70  */
71  mimmo::Partition* partitioner = new mimmo::Partition();
72  partitioner->setPartitionMethod(mimmo::PartitionMethod::PARTGEOM);
73  partitioner->setName("mimmo.Partitioner");
74  partitioner->setPlotInExecution(true);
75 #endif
76 
77  //declare a support Radius for RBF cloud
78  double suppR = 0.2;
79 
80  //create manually a RBF cloud with a set of displacements attached.
83 
84 #if MIMMO_ENABLE_MPI
85  if(rbfPointCloud->getRank() == 0)
86  {
87 #endif
88 
89  //create 4 points on a y-plane at {{0.0,-0.6,0.0}}
90  rbfPointCloud->addVertex({{0.0,-0.6,0.0}}, 0);
91  rbfPointCloud->addVertex({{-0.1,-0.6,0.0}}, 1);
92  rbfPointCloud->addVertex({{0.1,-0.6,0.12}}, 2);
93  rbfPointCloud->addVertex({{-0.03,-0.6,-0.1}}, 3);
94 
95  rbfPointCloud->addConnectedCell(std::vector<long>(1,0), bitpit::ElementType::VERTEX, 0, 10, 0);
96  rbfPointCloud->addConnectedCell(std::vector<long>(1,1), bitpit::ElementType::VERTEX, 0, 11, 0);
97  rbfPointCloud->addConnectedCell(std::vector<long>(1,2), bitpit::ElementType::VERTEX, 0, 12, 0);
98  rbfPointCloud->addConnectedCell(std::vector<long>(1,3), bitpit::ElementType::VERTEX, 0, 13, 0);
99 
100  // insert displacements for POINTS
101  rbfDispls.insert(0, {{0.0,-0.2,0.0}});
102  rbfDispls.insert(1, {{-0.6,-0.2,0.0}});
103  rbfDispls.insert(2, {{0.16,-0.2,0.4}});
104  rbfDispls.insert(3, {{-0.4,-0.2,-0.18}});
105 
106 #if MIMMO_ENABLE_MPI
107  }
108  rbfPointCloud->cleanPatchInfo();
109  rbfPointCloud->updateAdjacencies();
110  rbfPointCloud->update();
111 #endif
112 
113  /*
114  Project onto surface and mirror these rbf points with their displacements attached
115  w.r.t. the y plane crossing the origin
116  */
118  spec->setName("MirroringRBF");
119  spec->setPointCloud(rbfPointCloud);
120  spec->setVectorData(&rbfDispls);
121  spec->setOrigin({{0.0,0.0,0.0}});
122  spec->setNormal({{0.0,1.0,0.0}});
123  spec->setInsideOut(true);
124  spec->setForce(false);
125  spec->setPlotInExecution(true);
126 
127 
128  /*
129  Calculate AABB of the Mirrored RBF set accounting for the current support
130  radius chosen. Mirrored points are passed through port connection.
131  */
132  mimmo::RBFBox * rbfbox = new mimmo::RBFBox();
133  rbfbox->setName("AABB_RBFBox");
134  rbfbox->setSupportRadius(suppR);
135  rbfbox->setPlotInExecution(true);
136  /*
137  Use RBFBox to provide a sub-selection of the original geometry.
138  Origin and span of RBFBox AABB are provided through ports.
139  */
141  sel->setName("RBFBoxSelection");
142  sel->setDual(false);
143  sel->setPlotInExecution(true);
144 
145  /*
146  Get deformation on the box selected surface with the current RBF mirrored set,
147  but not apply it yet.
148  */
149  mimmo::MRBF * manip = new mimmo::MRBF();
150  manip->setName("RBFManipulator");
151  manip->setSupportRadiusReal(suppR);
152 
153  /*
154  Reconstruct the deformation field on the whole body
155  */
157  recon->setName("ReconstructDeformation");
158  recon->setPlotInExecution(true);
159 
160  /*
161  Verify actual geometry deformation vs the original geometry
162  level-set isolevel at d=0.14. Get the max penetration value on file.
163  Original geometry and its to-check deformation are passed through ports
164  */
166  isolevelCheck->setName("IsoLevelCheckCollision");
167  isolevelCheck->setLimitDistance(0.5);
168  isolevelCheck->setPlotInExecution(true);
169 
170  /*
171  Apply deformation to the original geometry
172  */
173  mimmo::Apply * applier = new mimmo::Apply();
174 
175 #if MIMMO_ENABLE_MPI
176  /*
177  Serialize the final deformed mesh
178  */
179  mimmo::Partition* serialize = new mimmo::Partition();
180  serialize->setName("mimmo.Serialization");
181  serialize->setPlotInExecution(false);
182  serialize->setPartitionMethod(mimmo::PartitionMethod::SERIALIZE);
183 #endif
184 
185 
186  /* Setup pin connections.
187  */
188  // original geometry passed to spec
189 #if MIMMO_ENABLE_MPI
190  //read target geometry, partition it and distribute it to the other subblocks
191  mimmo::pin::addPin(mimmo0, partitioner, M_GEOM, M_GEOM);
192  mimmo::pin::addPin(partitioner, spec, M_GEOM, M_GEOM);
193  mimmo::pin::addPin(partitioner, sel, M_GEOM, M_GEOM);
194  mimmo::pin::addPin(partitioner, recon, M_GEOM, M_GEOM);
195  mimmo::pin::addPin(partitioner, isolevelCheck, M_GEOM, M_GEOM);
196  mimmo::pin::addPin(partitioner, applier, M_GEOM, M_GEOM);
197 
198 #else
199  //read target geometry and distribute it to the other subblocks
200  mimmo::pin::addPin(mimmo0, spec, M_GEOM, M_GEOM);
201  mimmo::pin::addPin(mimmo0, sel, M_GEOM, M_GEOM);
202  mimmo::pin::addPin(mimmo0, recon, M_GEOM, M_GEOM);
203  mimmo::pin::addPin(mimmo0, isolevelCheck, M_GEOM, M_GEOM);
204  mimmo::pin::addPin(mimmo0, applier, M_GEOM, M_GEOM);
205 
206 #endif
207  //passing mirrored pc to rbfbox
208  mimmo::pin::addPin(spec, rbfbox, M_GEOM, M_GEOM);
209 
210  //passing box dimension from rbfbox to box selection
211  mimmo::pin::addPin(rbfbox, sel, M_POINT, M_POINT);
212  mimmo::pin::addPin(rbfbox, sel, M_SPAN, M_SPAN);
213 
214  // manip get as working geometry the selected patch of sel.
215  // rbf point cloud and displacements are provided by spec
216  mimmo::pin::addPin(sel, manip, M_GEOM, M_GEOM);
217  mimmo::pin::addPin(spec, manip, M_GEOM, M_GEOM2);
219 
220  //pass the deformation field of manip to reconstructor
222 
223  //pass the reconstructed deformation field to isolevelCheck
224  mimmo::pin::addPin(recon, isolevelCheck, M_VECTORFIELD, M_GDISPLS);
225 
226  // pass the reconstructed deformation to applier
227  mimmo::pin::addPin(recon, applier, M_VECTORFIELD, M_GDISPLS);
228 
229 #if MIMMO_ENABLE_MPI
230  //add serialization
231  mimmo::pin::addPin(applier, serialize, M_GEOM, M_GEOM);
232 #endif
233  /* Setup execution chain.
234  */
235  mimmo::Chain ch0;
236  ch0.addObject(mimmo0);
237  ch0.addObject(manip);
238  ch0.addObject(spec);
239  ch0.addObject(rbfbox);
240  ch0.addObject(sel);
241  ch0.addObject(recon);
242  ch0.addObject(isolevelCheck);
243  ch0.addObject(applier);
244 #if MIMMO_ENABLE_MPI
245  ch0.addObject(partitioner);
246  ch0.addObject(serialize);
247 #endif
248 
249  /* Execute the chain.
250  * Use debug flag false to avoid to print out the execution steps on console.
251  */
252  ch0.exec(true);
253 
254  //Last step write deformed original geometry in vtu
255 #if MIMMO_ENABLE_MPI
256  serialize->getGeometry()->getPatch()->write("utils_mesh_00003.0001");
257 #else
258  mimmo0->getGeometry()->getPatch()->write("utils_mesh_00003.0001");
259 #endif
260 
261  /* Clean up & exit;
262  */
263  delete mimmo0;
264  delete spec;
265  delete rbfbox;
266  delete sel;
267  delete manip;
268  delete recon;
269  delete isolevelCheck;
270  delete applier;
271 #if MIMMO_ENABLE_MPI
272  delete partitioner;
273  delete serialize;
274 #endif
275  return;
276 
277 }
278 
279 
280 int main(int argc, char *argv[]) {
281 
282  BITPIT_UNUSED(argc);
283  BITPIT_UNUSED(argv);
284 
285 #if MIMMO_ENABLE_MPI==1
286  MPI_Init(&argc, &argv);
287 
288  {
289 #endif
290 
291  mimmo::setLogger("mimmo");
292 
294  try{
295  test00003() ;
296  }
297  catch(std::exception & e){
298  std::cout<<"utils_example_00003 exited with an error of type : "<<e.what()<<std::endl;
299  return 1;
300  }
301 
302 #if MIMMO_ENABLE_MPI==1
303  }
304 
305  MPI_Finalize();
306 #endif
307 
308  return 0;
309 }
void setInsideOut(bool flag)
void exec(bool debug=false)
Definition: Chain.cpp:284
void setLogger(std::string log)
#define M_GDISPLS
Chain is the class used to manage the chain execution of multiple executable blocks (manipulation obj...
Definition: Chain.hpp:48
MimmoObject is the basic geometry container for mimmo library.
#define M_GEOM
Apply is the class that applies the deformation resulting from a manipulation object to the geometry.
Definition: Apply.hpp:89
void setSupportRadiusReal(double suppR_)
Definition: MRBF.cpp:436
void setWriteFileType(FileType type)
void setName(std::string name)
void setSupportRadius(double suppR_)
Definition: RBFBox.cpp:153
void setWriteDir(std::string dir)
#define M_GEOM2
#define M_POINT
void setWriteFilename(std::string filename)
Radial Basis Functions Bounding Box calculator.
Definition: RBFBox.hpp:74
#define M_VECTORFIELD
SpecularPoints is a class that mirrors a point cloud w.r.t. a reference plane, on a target surface ge...
ControlDeformMaxDistance is a class that check a deformation field associated to a MimmoObject surfac...
void setReadFilename(std::string filename)
#define M_SPAN
bool addPin(BaseManipulation *objSend, BaseManipulation *objRec, PortID portS, PortID portR, bool forced)
void setOrigin(darray3E origin)
Selection through volume box primitive.
MimmoGeometry is an executable block class wrapping(linking or internally instantiating) a Mimmo Obje...
Reconstruct a vector field from daughter mesh to mother mesh.
void setPointCloud(MimmoSharedPointer< MimmoObject > targetpatch)
void setVectorData(dmpvecarr3E *vdata)
void setDual(bool flag=false)
void setExpertMode(bool flag)
Radial Basis Function evaluation from clouds of control points.
Definition: MRBF.hpp:146
void setForce(bool flag)
void setReadFileType(FileType type)
MimmoSharedPointer< MimmoObject > getGeometry()
void setReadDir(std::string dir)
int addObject(BaseManipulation *obj, int id_=-1)
Definition: Chain.cpp:170
void setNormal(darray3E normal)