manipulators_example_00007.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 
30 // =================================================================================== //
46 void test00007() {
47 
48  /*
49  Read a sphere from STL. Convert mode is to save the just read geometry in
50  another file with name manipulators_output_00007.0000.stl
51  */
53  mimmo0->setReadDir("geodata");
54  mimmo0->setReadFileType(FileType::STL);
55  mimmo0->setReadFilename("sphere2");
56  mimmo0->setWriteDir(".");
57  mimmo0->setWriteFileType(FileType::STL);
58  mimmo0->setWriteFilename("manipulators_output_00007.0000");
59 
60  /* write the deformed mesh to file */
62  mimmo1->setWriteDir(".");
63  mimmo1->setWriteFileType(FileType::STL);
64  mimmo1->setWriteFilename("manipulators_output_00007.0001");
65 
66  /*
67  Creation of a random distribution of 10 points with coordinates between [-0.5, 0.5]
68  and a random distribution of 10 support radii between [0.05, 0.35]
69  */
70  int np = 10;
71  dvecarr3E rbfNodes(10);
72  dvector1D supportRadii(10);
73  std::minstd_rand rgen, sgen;
74  rgen.seed(160);
75  sgen.seed(333);
76  double dist = (rgen.max()-rgen.min());
77  double dist2 = (sgen.max()-sgen.min());
78 
79  for (int i=0; i<np; i++){
80  for (int j=0; j<3; j++){
81  rbfNodes[i][j] = 1.0*( double(rgen() - rgen.min() ) / dist ) - 0.5;
82  }
83  supportRadii[i] = 0.1 * ( double(sgen() - sgen.min() ) / dist2 ) + 0.05;
84  }
85 
86  /*
87  Creation of a set of displacements of the control nodes of the radial basis functions.
88  * Use a radial displacements from a center point placed in axes origin.
89  */
90  dvecarr3E displ(np, darray3E{0.0, 0.0, 0.0});
91  darray3E center({0.0, 0.0, 0.0});
92  for (int i=0; i<np; i++){
93  displ[i] = rbfNodes[i] - center;
94  displ[i] /= -20.0*norm2(displ[i]);
95  }
96 
97 
98  /*
99  Set Generic input block with the
100  nodes defined above.
101  */
103  inputn->setInput(rbfNodes);
104 
105  /* Set Generic input block with the
106  * supportRadii defined above.
107  */
108  mimmo::GenericInput* inputSR = new mimmo::GenericInput();
109  inputSR->setInput(supportRadii);
110 
111  /* Set Generic input block with the
112  * displacements defined above.
113  */
115  input->setInput(displ);
116 
117  /*
118  Create a Point Cloud that will use the outputs of previous generic inputs.
119  Nodes, displacements and supportRadii will be stored as a point cloud mesh with a vector
120  field/scalar field attached, respectively.
121  */
123 
124  /*
125  This block will project the rbf point cloud onto the target surface
126  */
128  proj->setWorkingOnTarget(true);
129 
130  /*
131  RBF manipulator, will use RBF point cloud, its vectorfield of displacements
132  and its scalarfield of variable supportRadii to build up itself.
133  It requires the definition of RBF function.
134  */
137  mrbf->setPlotInExecution(true);
138 
139  /*
140  Create applier block.
141  It applies the deformation displacements to the original input geometry.
142  */
143  mimmo::Apply* applier = new mimmo::Apply();
144 
145  /*
146  Setup pin connections.
147  */
148  mimmo::pin::addPin(mimmo0, mrbf, M_GEOM, M_GEOM);
149  mimmo::pin::addPin(mimmo0, proj, M_GEOM, M_GEOM);
150  mimmo::pin::addPin(mimmo0, applier, M_GEOM, M_GEOM);
151  mimmo::pin::addPin(inputn, pointcloud, M_COORDS, M_COORDS);
152  mimmo::pin::addPin(input, pointcloud, M_DISPLS, M_DISPLS);
153  mimmo::pin::addPin(inputSR, pointcloud, M_DATAFIELD, M_DATAFIELD);
154  mimmo::pin::addPin(pointcloud, proj, M_GEOM, M_GEOM2);
155  mimmo::pin::addPin(pointcloud, mrbf, M_SCALARFIELD, M_SCALARFIELD2);
156  mimmo::pin::addPin(pointcloud, mrbf, M_VECTORFIELD, M_VECTORFIELD);
157  mimmo::pin::addPin(proj, mrbf, M_GEOM, M_GEOM2);
158  mimmo::pin::addPin(mrbf, applier, M_GDISPLS, M_GDISPLS);
159  mimmo::pin::addPin(applier, mimmo1, M_GEOM, M_GEOM);
160 
161  /*
162  Setup execution chain.
163  */
164  mimmo::Chain ch0;
165  ch0.addObject(input);
166  ch0.addObject(inputn);
167  ch0.addObject(inputSR);
168  ch0.addObject(mimmo0);
169  ch0.addObject(pointcloud);
170  ch0.addObject(proj);
171  ch0.addObject(applier);
172  ch0.addObject(mrbf);
173  ch0.addObject(mimmo1);
174 
175  /*
176  Execute the chain.
177  Use debug flag true to print out the execution steps.
178  */
179  ch0.exec(true);
180 
181  /*
182  Clean up & exit;
183  */
184  delete mrbf;
185  delete proj;
186  delete pointcloud;
187  delete applier;
188  delete input;
189  delete inputSR;
190  delete mimmo0;
191  delete mimmo1;
192 
193  return;
194 }
195 
196 int main( int argc, char *argv[] ) {
197 
198  BITPIT_UNUSED(argc);
199  BITPIT_UNUSED(argv);
200 
201 #if MIMMO_ENABLE_MPI
202  MPI_Init(&argc, &argv);
203 #endif
204  try{
206  test00007() ;
207  }
208  catch(std::exception & e){
209  std::cout<<"manipulators_example_00007 exited with an error of type : "<<e.what()<<std::endl;
210  return 1;
211  }
212 #if MIMMO_ENABLE_MPI
213  MPI_Finalize();
214 #endif
215 
216  return 0;
217 }
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_DATAFIELD
#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 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)
std::vector< double > dvector1D
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,...
#define M_SCALARFIELD
std::array< double, 3 > darray3E
Radial Basis Function evaluation from clouds of control points.
Definition: MRBF.hpp:146
#define M_SCALARFIELD2
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