manipulators_example_00006.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_manipulators.hpp"
26 #include "mimmo_iogeneric.hpp"
27 #include "mimmo_utils.hpp"
28 #include <random>
29 #if MIMMO_ENABLE_MPI
30 #include "mimmo_parallel.hpp"
31 #endif
32 
33 // =================================================================================== //
48 void test00006() {
49 
50  /*
51  Read a sphere from STL. Convert mode is to save the just read geometry in
52  another file with name manipulators_output_00006.0000.stl
53  */
55  mimmo0->setReadDir("geodata");
56  mimmo0->setReadFileType(FileType::STL);
57  mimmo0->setReadFilename("sphere2");
58  mimmo0->setWriteDir(".");
59  mimmo0->setWriteFileType(FileType::STL);
60  mimmo0->setWriteFilename("manipulators_output_00006.0000");
61 
62  /* write the deformed mesh to file */
64  mimmo1->setWriteDir(".");
65  mimmo1->setWriteFileType(FileType::STL);
66  mimmo1->setWriteFilename("manipulators_output_00006.0001");
67 
68 #if MIMMO_ENABLE_MPI
69  /*
70  Distribute mesh among processes
71  */
72  mimmo::Partition* partition= new mimmo::Partition();
73  partition->setPartitionMethod(mimmo::PartitionMethod::PARTGEOM);
74 #endif
75 
76  /*
77  Creation of a random distribution of 10 points with coordinates between [-0.5, 0.5]
78  */
79  int np = 10;
80  dvecarr3E rbfNodes(10);
81  std::minstd_rand rgen;
82  rgen.seed(160);
83  double dist = (rgen.max()-rgen.min());
84  for (int i=0; i<np; i++){
85  for (int j=0; j<3; j++)
86  rbfNodes[i][j] = 1.0*( double(rgen() - rgen.min() ) / dist ) - 0.5;
87  }
88 
89  /*
90  Set a Generic input block with the
91  nodes defined above.
92  */
94  inputn->setInput(rbfNodes);
95 
96  /*
97  Creation of a set of displacements of the control nodes of the radial basis functions.
98  Use a radial displacements from a center point placed in axes origin.
99  */
100  dvecarr3E displ(np, darray3E{0.0, 0.0, 0.0});
101  darray3E center({0.0, 0.0, 0.0});
102  for (int i=0; i<np; i++){
103  displ[i] = rbfNodes[i] - center;
104  displ[i] /= 2.0*norm2(displ[i]);
105  }
106 
107  /*
108  Set Generic input block with the
109  displacements defined above.
110  */
112  input->setInput(displ);
113 
114  /*
115  Create a Point Cloud that will use the outputs of previous generic inputs.
116  Nodes and displacements will be stored as a point cloud mesh with a vector
117  field attached
118  */
120 
121  /*
122  This block will project the rbf point cloud onto the target surface
123  */
125  proj->setWorkingOnTarget(true);
126  proj->setPlotInExecution(true);
127 
128  /*
129  RBF manipulator, will use RBF point cloud and its vectorfield of displacements
130  to build up itself.
131  It requires the definition of RBF function and support radius
132  */
134  mrbf->setFunction(bitpit::RBFBasisFunction::WENDLANDC2);
135  mrbf->setSupportRadiusReal(0.1);
136  mrbf->setPlotInExecution(true);
137 
138  /*
139  Create applier block.
140  It applies the deformation displacements to the original input geometry.
141  */
142  mimmo::Apply* applier = new mimmo::Apply();
143 
144  /* Setup pin connections.
145  */
146 #if MIMMO_ENABLE_MPI
147  mimmo::pin::addPin(mimmo0, partition, M_GEOM, M_GEOM);
148  mimmo::pin::addPin(partition, mrbf, M_GEOM, M_GEOM);
149 #else
150  mimmo::pin::addPin(mimmo0, mrbf, M_GEOM, M_GEOM);
151 #endif
152  mimmo::pin::addPin(mimmo0, proj, M_GEOM, M_GEOM);
153  mimmo::pin::addPin(mimmo0, applier, M_GEOM, M_GEOM);
154  mimmo::pin::addPin(inputn, rbfPC, M_COORDS, M_COORDS);
155  mimmo::pin::addPin(input, rbfPC, M_DISPLS, M_DISPLS);
156  mimmo::pin::addPin(rbfPC, proj, M_GEOM, M_GEOM2);
158  mimmo::pin::addPin(proj, mrbf, M_GEOM, M_GEOM2);
159  mimmo::pin::addPin(mrbf, applier, M_GDISPLS, M_GDISPLS);
160  mimmo::pin::addPin(applier, mimmo1, M_GEOM, M_GEOM);
161 
162  /*
163  Setup execution chain.
164  */
165  mimmo::Chain ch0;
166 #if MIMMO_ENABLE_MPI
167  ch0.addObject(partition);
168 #endif
169  ch0.addObject(input);
170  ch0.addObject(inputn);
171  ch0.addObject(rbfPC);
172  ch0.addObject(mimmo0);
173  ch0.addObject(proj);
174  ch0.addObject(applier);
175  ch0.addObject(mrbf);
176  ch0.addObject(mimmo1);
177 
178  /*
179  Execute the chain.
180  * Use debug flag true to print out the execution steps.
181  */
182  ch0.exec(true);
183 
184  /*
185  Clean up & exit;
186  */
187 #if MIMMO_ENABLE_MPI
188  delete partition;
189 #endif
190  delete mrbf;
191  delete proj;
192  delete applier;
193  delete input;
194  delete rbfPC;
195  delete mimmo0;
196  delete mimmo1;
197 
198  return;
199 }
200 
201 int main( int argc, char *argv[] ) {
202 
203  BITPIT_UNUSED(argc);
204  BITPIT_UNUSED(argv);
205 
206 #if MIMMO_ENABLE_MPI
207  MPI_Init(&argc, &argv);
208 #endif
209  try{
211  test00006() ;
212  }
213  catch(std::exception & e){
214  std::cout<<"manipulators_example_00006 exited with an error of type : "<<e.what()<<std::endl;
215  return 1;
216  }
217 #if MIMMO_ENABLE_MPI
218  MPI_Finalize();
219 #endif
220 
221  return 0;
222 }
void exec(bool debug=false)
Definition: Chain.cpp:284
#define M_GDISPLS
Chain is the class used to manage the chain execution of multiple executable blocks (manipulation obj...
Definition: Chain.hpp:48
#define M_DISPLS
#define M_GEOM
Apply is the class that applies the deformation resulting from a manipulation object to the geometry.
Definition: Apply.hpp:89
std::vector< darray3E > dvecarr3E
void setSupportRadiusReal(double suppR_)
Definition: MRBF.cpp:436
void setWriteFileType(FileType type)
void setWriteDir(std::string dir)
#define M_GEOM2
void setInput(T *data)
GENERICINPUT////////////////////////////////////////////////////////////////////////////.
void setWriteFilename(std::string filename)
#define M_VECTORFIELD
GenericInput is the class that set the initialization of a generic input data.
void setReadFilename(std::string filename)
bool addPin(BaseManipulation *objSend, BaseManipulation *objRec, PortID portS, PortID portR, bool forced)
MimmoGeometry is an executable block class wrapping(linking or internally instantiating) a Mimmo Obje...
Executable block class capable of projecting a surface patch, 3DCurve or PointCloud on a 3D surface,...
std::array< double, 3 > darray3E
Radial Basis Function evaluation from clouds of control points.
Definition: MRBF.hpp:146
void setReadFileType(FileType type)
CreatePointCloud manages cloud point data in raw format to create a MimmoObject Point Cloud container...
void setReadDir(std::string dir)
int addObject(BaseManipulation *obj, int id_=-1)
Definition: Chain.cpp:170
#define M_COORDS
void setFunction(const MRBFBasisFunction &funct, bool isCompact=false)
Definition: MRBF.cpp:594