]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx
2c17fb7be02d511263fc4fd5f4531dd4d0bc56a7
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkCreateMeshFromPoints.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbcreaVtkCreateMeshFromPoints.h"
5 #include "bbcreaVtkPackage.h"
6
7 #include "vtkTriangleStrip.h"
8 #include <vtkMath.h>
9
10 namespace bbcreaVtk
11 {
12
13 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,CreateMeshFromPoints)
14 BBTK_BLACK_BOX_IMPLEMENTATION(CreateMeshFromPoints,bbtk::AtomicBlackBox);
15 //===== 
16 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
17 //===== 
18 void CreateMeshFromPoints::Process()
19 {
20
21 // THE MAIN PROCESSING METHOD BODY
22 //   Here we simply set the input 'In' value to the output 'Out'
23 //   And print out the output value
24 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
25 //    void bbSet{Input|Output}NAME(const TYPE&)
26 //    const TYPE& bbGet{Input|Output}NAME() const 
27 //    Where :
28 //    * NAME is the name of the input/output
29 //      (the one provided in the attribute 'name' of the tag 'input')
30 //    * TYPE is the C++ type of the input/output
31 //      (the one provided in the attribute 'type' of the tag 'input')
32
33 //    bbSetOutputOut( bbGetInputIn() );
34 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
35
36                 std::vector<double> lstX                = bbGetInputLstX();
37                 std::vector<double> lstY                = bbGetInputLstY();
38                 std::vector<double> lstZ                = bbGetInputLstZ();
39                 std::vector<int> lstIndexs              = bbGetInputLstIndexs();
40                 if ( (lstIndexs.size()<=1) || (lstX.size()==0) || (lstX.size()!=lstY.size()) || (lstY.size()!=lstZ.size()) )
41                 {
42                         printf("PG CreateMeshFromPoints::Process: List of points X Y Z  and LstIndexes is not correct\n");
43                         bbSetOutputOut(NULL);
44                 } else  {
45                         int ii,sizeSegment1,sizeSegment2;
46                         int endSegment;
47 //                      vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
48                         if (points!=NULL) points->Delete();
49                         points = vtkPoints::New();
50                         int i,sizeLstX  =       lstX.size();
51                         for (i=0;i<sizeLstX;i++)
52                         {
53                                 points->InsertNextPoint(lstX[i],lstY[i],lstZ[i]);
54                         } // for i
55 //                      vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
56                         if (cells!=NULL) cells->Delete();
57                         cells = vtkCellArray::New();
58                         int maxElements;
59                         int maxSegment1,maxSegment2;
60                         int iSeg1,iSeg2;
61                         int iGeneral    =       0;
62                         int     sizeLstIdexes=lstIndexs.size();
63                         for (i=0; i<sizeLstIdexes-1; i++ )
64                         {
65                                 sizeSegment1 = lstIndexs[i];
66                                 sizeSegment2 = lstIndexs[i+1];
67                                 vtkSmartPointer<vtkTriangleStrip> triangleStrip = vtkSmartPointer<vtkTriangleStrip>::New();
68                                 triangleStrip->GetPointIds()->SetNumberOfIds(sizeSegment1+sizeSegment2);
69                                 maxElements=sizeSegment1;
70                                 if (maxElements<sizeSegment2) maxElements=sizeSegment2;
71                                 maxSegment1     = iGeneral+sizeSegment1;
72                                 maxSegment2     = iGeneral+sizeSegment1+sizeSegment2;
73                                 iSeg1           = iGeneral;
74                                 iSeg2           = iGeneral+sizeSegment1;
75                                 for (ii=0; ii<maxElements; ii++)
76                                 {
77                                         triangleStrip->GetPointIds()->SetId(ii*2  ,iSeg1);
78                                         triangleStrip->GetPointIds()->SetId(ii*2+1,iSeg2);
79                                         iSeg1++;
80                                         iSeg2++;
81                     if (iSeg1>=maxSegment1) { iSeg1=maxSegment1-1; }
82                     if (iSeg2>=maxSegment2) { iSeg2=maxSegment2-1; }
83                                 } // for ii 
84                                 iGeneral=iGeneral+sizeSegment1;
85                                 cells->InsertNextCell(triangleStrip);
86                         } //for  LstIndexs
87                         
88                         //false = Open
89                         //true = Close
90                         if(bbGetInputOpenClose()){                      
91                                 CloseContourSides(lstIndexs, true);
92                         }
93                         else{
94                                 CloseOpenContourSurface(lstIndexs);
95                         }
96                         
97 //                      vtkPolyData *polydata = vtkPolyData::New();
98                         if (polydata!=NULL) polydata->Delete();
99                         polydata = vtkPolyData::New();
100                         polydata->SetPoints(points);
101                         polydata->SetStrips(cells);
102 //                      vtkCleanPolyData *clean=vtkCleanPolyData::New();
103                         if (clean!=NULL) clean->Delete();
104                         clean = vtkCleanPolyData::New();
105                         clean->SetInputData(polydata);
106                         clean->Update();
107 //                      vtkTriangleFilter *triangle = vtkTriangleFilter::New();
108                         if (triangle!=NULL) triangle->Delete();
109                         triangle = vtkTriangleFilter::New();
110                         triangle->SetInputData( clean->GetOutput() );
111                         triangle->Update();
112                         bbSetOutputOut( clean->GetOutput() );
113                 }// if listXYZ size
114                 printf("CreateMeshFromPoints::Process: End\n");
115 }
116
117 void CreateMeshFromPoints::CloseContourSides(std::vector<int> lstIndexs, bool uPointOrder){
118         int     sizeLstIdexes = lstIndexs.size();
119         int sizePoints = bbGetInputLstX().size();
120
121         int firstIndex, end, centroidId, numPointsFace;
122         int increment = uPointOrder?1:sizeLstIdexes;
123         double centroid[3], currentPoint[3] = {};
124         
125         for(int facesIdx = 0; facesIdx < 2; facesIdx++){
126                 std::fill(std::begin(centroid), std::end(centroid), 0);
127                 if(facesIdx == 0){
128                         firstIndex = 0;
129                         numPointsFace = uPointOrder?lstIndexs[0]: sizeLstIdexes;
130                         end = uPointOrder?firstIndex + numPointsFace:sizePoints - lstIndexs[sizeLstIdexes - 1] + 1;
131                 }
132                 else{
133                         firstIndex = uPointOrder?sizePoints - lstIndexs[sizeLstIdexes-1]:lstIndexs[0] - 1;
134                         numPointsFace = uPointOrder?lstIndexs[sizeLstIdexes-1]:sizeLstIdexes;
135                         end = uPointOrder?firstIndex + numPointsFace:sizePoints;
136                 }
137                 if(numPointsFace > 1){
138                         double lastPoint[3] = {};
139                         CalcCentroid(centroid, firstIndex, end, increment, numPointsFace);
140                         centroidId = points->InsertNextPoint(centroid[0], centroid[1], centroid[2]);
141                         vtkSmartPointer<vtkTriangleStrip> triangleStrip1 = vtkSmartPointer<vtkTriangleStrip>::New();
142                         triangleStrip1->GetPointIds()->SetNumberOfIds(numPointsFace*2+1);
143                         if(facesIdx == 0){                              
144                                 int initial = firstIndex;
145                                 int triangleIndex = 0;
146                                 for(int index = initial; index < end; index+=increment){
147                                         triangleStrip1->GetPointIds()->SetId(triangleIndex,index);
148                                         if(index+increment >= end){
149                                                 triangleStrip1->GetPointIds()->SetId(triangleIndex+1,initial);
150                                                 triangleStrip1->GetPointIds()->SetId(triangleIndex+2,centroidId);
151                                         }
152                                         else{
153                                                 triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);
154                                         }
155                                         triangleIndex+=2;
156                                 }
157                                 cells->InsertNextCell(triangleStrip1);
158                         }
159                         else{
160                                 int initial = firstIndex-1;
161                                 int triangleIndex = 0;
162                                 int triangleStripStart = end-1;
163                                 for(int index = triangleStripStart; index > initial ; index-=increment){
164                                         triangleStrip1->GetPointIds()->SetId(triangleIndex,index);
165                                         if(index-increment <= initial){
166                                                 triangleStrip1->GetPointIds()->SetId(triangleIndex+1,triangleStripStart);
167                                                 triangleStrip1->GetPointIds()->SetId(triangleIndex+2,centroidId);
168                                         }
169                                         else{
170                                                 triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);
171                                         }
172                                         triangleIndex+=2;
173                                 }
174                                 cells->InsertNextCell(triangleStrip1);
175                         }
176                 }
177         }
178
179 }
180
181 void CreateMeshFromPoints::CloseOpenContourSurface(std::vector<int> lstIndexs){
182         int     sizeLstIdexes = lstIndexs.size();
183         int sizeLstX = bbGetInputLstX().size();
184         bool linePointOrder = false;
185
186         if(linePointOrder){
187                 CloseContourSides(lstIndexs, false);
188                 CloseContourBottom(false);
189         }
190         else{
191                 CloseContourSides(lstIndexs, true);
192                 CloseContourBottom(true);
193         }
194                 
195 }
196
197 void CreateMeshFromPoints::CalcCentroid(double(&centroid)[3], int start, int end, int increment, int numPoints){
198         double currPoint[3] = {};
199         for(int i = start; i < end; i+=increment){
200                 points->GetPoint(i, currPoint);
201                 centroid[0] += currPoint[0];
202                 centroid[1] += currPoint[1];
203                 centroid[2] += currPoint[2];
204         }
205         centroid[0] /= numPoints;
206         centroid[1] /= numPoints;
207         centroid[2] /= numPoints;
208 }
209
210 void CreateMeshFromPoints::CloseContourBottom(bool uPointOrder){
211         std::vector<int> lstIndexs = bbGetInputLstIndexs();
212         int sizeLstIdexes = lstIndexs.size();
213         int sizeLstX = bbGetInputLstX().size();
214         
215         vtkSmartPointer<vtkTriangleStrip> triangleStripBottom = vtkSmartPointer<vtkTriangleStrip>::New();
216         triangleStripBottom->GetPointIds()->SetNumberOfIds(sizeLstIdexes*2);
217         int triangleIndex = 0, currentId = 0, nextId = 0;
218         for(int splineIndex = 0; splineIndex < sizeLstIdexes;splineIndex++){
219                 triangleStripBottom->GetPointIds()->SetId(triangleIndex, currentId);
220                 nextId = uPointOrder?currentId + lstIndexs[splineIndex] - 1:sizeLstX - sizeLstIdexes + splineIndex;
221                 triangleStripBottom->GetPointIds()->SetId(triangleIndex+1, nextId);
222                 triangleIndex+=2;
223                 currentId = uPointOrder?nextId + 1: splineIndex+1;
224         }
225         cells->InsertNextCell(triangleStripBottom);
226 }
227
228 //===== 
229 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
230 //===== 
231 void CreateMeshFromPoints::bbUserSetDefaultValues()
232 {
233
234 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
235 //    Here we initialize the input 'In' to 0
236 //   bbSetInputIn(0);
237         bbSetInputOpenClose(false);
238         points          = NULL;
239         cells           = NULL;
240         polydata        = NULL;
241         clean           = NULL;
242         triangle        = NULL;
243 }
244 //===== 
245 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
246 //===== 
247 void CreateMeshFromPoints::bbUserInitializeProcessing()
248 {
249
250 //  THE INITIALIZATION METHOD BODY :
251 //    Here does nothing 
252 //    but this is where you should allocate the internal/output pointers 
253 //    if any 
254
255   
256 }
257 //===== 
258 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
259 //===== 
260 void CreateMeshFromPoints::bbUserFinalizeProcessing()
261 {
262
263 //  THE FINALIZATION METHOD BODY :
264 //    Here does nothing 
265 //    but this is where you should desallocate the internal/output pointers 
266 //    if any
267   
268 }
269 }
270 // EO namespace bbcreaVtk
271
272