]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx
69aab0853b9182bfbc92af097f6a8ca2fe5caf96
[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                         
89                         if(bbGetInputCloseSurface()){                   
90                                 //false = Open Contour
91                                 //true = Closed Contour
92                                 if(bbGetInputOpenClose()){                      
93                                         CloseContourSides(lstIndexs, true);
94                                 }
95                                 else{
96                                         CloseOpenContourSurface(lstIndexs);
97                                 }
98                         }
99                     
100 //                      vtkPolyData *polydata = vtkPolyData::New();
101                         if (polydata!=NULL) polydata->Delete();
102                         polydata = vtkPolyData::New();
103                         polydata->SetPoints(points);
104                         polydata->SetStrips(cells);
105 //                      vtkCleanPolyData *clean=vtkCleanPolyData::New();
106                         if (clean!=NULL) clean->Delete();
107                         clean = vtkCleanPolyData::New();
108                         clean->SetInputData(polydata);
109                         clean->Update();
110 //                      vtkTriangleFilter *triangle = vtkTriangleFilter::New();
111                         if (triangle!=NULL) triangle->Delete();
112                         triangle = vtkTriangleFilter::New();
113                         triangle->SetInputData( clean->GetOutput() );
114                         triangle->Update();
115             bbSetOutputOut( triangle->GetOutput() );
116             //            bbSetOutputOut( clean->GetOutput() );
117                 }// if listXYZ size
118                 //printf("PG CreateMeshFromPoints::Process: End\n");
119 }
120
121 /**
122 * Closes the sides of the contour
123 * iterates in one way or the other, depending on the order of the points and calculated vectors.
124 * uPointOrder: Points are order in a U shape
125 * lstIndexs: number of points on each spline
126 */
127 void CreateMeshFromPoints::CloseContourSides(std::vector<int> lstIndexs, bool uPointOrder){
128         int     sizeLstIdexes = lstIndexs.size();
129         int sizePoints = bbGetInputLstX().size();
130
131         int firstIndex, end, centroidId, numPointsFace, contraryId;
132         int increment = uPointOrder?1:sizeLstIdexes;
133         double centroid[3];
134         
135         for(int facesIdx = 0; facesIdx < 2; facesIdx++){
136                 std::fill(std::begin(centroid), std::end(centroid), 0);
137                 if(facesIdx == 0){
138                         firstIndex = 0;
139                         numPointsFace = uPointOrder?lstIndexs[0]: sizeLstIdexes;
140                         end = uPointOrder?firstIndex + numPointsFace:sizePoints - lstIndexs[sizeLstIdexes - 1] + 1;
141                         contraryId = sizePoints-1;
142                 }
143                 else{
144                         firstIndex = uPointOrder?sizePoints - lstIndexs[sizeLstIdexes-1]:lstIndexs[0]-1;
145                         numPointsFace = uPointOrder?lstIndexs[sizeLstIdexes-1]:sizeLstIdexes;
146                         end = uPointOrder?firstIndex + numPointsFace:sizePoints;
147                         contraryId = 0;
148                 }
149                 if(numPointsFace > 1){
150                         double lastPoint[3] = {};
151                         bool validCentroid = CalcValidCentroid(centroid, firstIndex, end, increment, numPointsFace);
152                         if(validCentroid){
153                                 bool normalOrder    = isPointingCorrectly(firstIndex, firstIndex+increment, centroid, contraryId);
154                                 centroidId          = points->InsertNextPoint(centroid[0], centroid[1], centroid[2]);
155                                 vtkSmartPointer<vtkTriangleStrip> triangleStrip1 = vtkSmartPointer<vtkTriangleStrip>::New();
156                                 triangleStrip1->GetPointIds()->SetNumberOfIds(numPointsFace*2+1);
157                                 if( !normalOrder )
158                 {       //(facesIdx == 0 && uPointOrder) || (facesIdx == 1 && !uPointOrder)){
159                                         int initial = firstIndex;
160                                         int triangleIndex = 0;
161                                         for(int index = initial; index < end; index+=increment){
162                                                 triangleStrip1->GetPointIds()->SetId(triangleIndex,index);
163                                                 if(index+increment >= end){
164                                                         triangleStrip1->GetPointIds()->SetId(triangleIndex+1,initial);
165                                                         triangleStrip1->GetPointIds()->SetId(triangleIndex+2,centroidId);
166                                                 }
167                                                 else{
168                                                         triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);
169                                                 }
170                                                 triangleIndex+=2;
171                                         }
172                                         cells->InsertNextCell(triangleStrip1);
173                                 } else {
174                                         int initial = firstIndex-1;
175                                         int triangleIndex = 0;
176                                         int triangleStripStart = end-1;
177                                         for(int index = triangleStripStart; index > initial; index-=increment){
178                                                 triangleStrip1->GetPointIds()->SetId(triangleIndex,index);
179                                                 if(index-increment <= initial){
180                                                         triangleStrip1->GetPointIds()->SetId(triangleIndex+1,triangleStripStart);
181                                                         triangleStrip1->GetPointIds()->SetId(triangleIndex+2,centroidId);
182                                                 }
183                                                 else{
184                                                         triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);
185                                                 }
186                                                 triangleIndex+=2;
187                                         }
188                                         cells->InsertNextCell(triangleStrip1);
189                                 }
190                         }//if validCentroid
191                 }//if numPointsFace
192         }//for facesIdx
193
194 }
195
196 /**
197 * Checks if the normal from firstPointId, secPointId and centroid points away 
198 * from the vector centroid to contrPointId.
199 * Used to check that the order used to create the new polygons is correct.
200 */
201 bool CreateMeshFromPoints::isPointingCorrectly( int firstPointId, int secPointId, double(&centroid)[3], int contrPointId) {
202
203         double firstPoint[3], secPoint[3], contrPoint[3];
204         points->GetPoint(firstPointId, firstPoint);
205         points->GetPoint(secPointId, secPoint);
206         
207         double firstVect[3], secVect[3], normal[3], contrVect[3];
208         
209         vtkMath::Subtract(firstPoint, centroid, firstVect);
210         vtkMath::Subtract(secPoint, centroid, secVect);
211         
212         points->GetPoint(contrPointId, contrPoint);
213         vtkMath::Subtract(contrPoint, centroid, contrVect);
214         
215         vtkMath::Cross(firstVect, secVect, normal);
216         double dotCalc;
217         dotCalc = vtkMath::Dot(normal, contrVect);
218         
219         return dotCalc<0;
220 }
221
222 /**
223 * Checks if points on each side of the shapes represent a curve.
224 */
225 bool CreateMeshFromPoints::CheckLinePointOrder(){
226         int sizePoints = bbGetInputLstX().size();
227         std::vector<int> lstIndexs = bbGetInputLstIndexs();
228         double point1[3], point2[3], point3[3];
229         double center[3];
230         double firstRadiusSum = 0;
231         double secondRadiusSum = 0;
232         for(int i = 0; i < lstIndexs[0] && lstIndexs[0] > 3; i+=3){
233                 if(i+3 <= lstIndexs[0]){
234                         points->GetPoint(i, point1);
235                         points->GetPoint(i+1, point2);
236                         points->GetPoint(i+2, point3);
237                         firstRadiusSum += vtkMath::Solve3PointCircle(point1, point2, point3, center);
238                 }
239         }
240         
241         for(int i = 0; i < sizePoints && lstIndexs.size() > 3; i+=(lstIndexs[0]*3)){
242                 if(i+(3*lstIndexs[0]) <= sizePoints){
243                         points->GetPoint(i, point1);
244                         points->GetPoint(i+lstIndexs[0], point2);
245                         points->GetPoint(i+(2*lstIndexs[0]), point3);
246                         secondRadiusSum += vtkMath::Solve3PointCircle(point1, point2, point3, center);
247                 }
248         }
249         
250         return firstRadiusSum > secondRadiusSum;
251 }
252
253 /**
254 * Closes an open contour
255 * lstIndexs: number of points on each spline
256 */
257 void CreateMeshFromPoints::CloseOpenContourSurface(std::vector<int> lstIndexs){
258         int     sizeLstIdexes = lstIndexs.size();
259         int sizeLstX = bbGetInputLstX().size();
260         bool linePointOrder = CheckLinePointOrder();
261         CloseContourSides(lstIndexs, !linePointOrder);
262         CloseContourBottom(!linePointOrder);
263 }
264
265 /**
266 * Calculates centroid.
267 * centroid: array to store calculation
268 * start: start index of points to use
269 * end: end index of points to use
270 * increment: increment to be used in point iteration
271 * numPoints: number of points used to calculate the centroid.
272 * Returns a bool indicating the validity of the centroid calculated.
273 * False = invalid centroid = all points are the same.
274 */
275 bool CreateMeshFromPoints::CalcValidCentroid(double(&centroid)[3], int start, int end, int increment, int numPoints){
276         double currPoint[3] = {}, prevPoint[3] = {};
277         bool samePoint = true;
278         for(int i = start; i < end; i+=increment){
279                 points->GetPoint(i, currPoint);
280                 if(samePoint && (currPoint[0] != prevPoint[0] || currPoint[1] != prevPoint[1] || currPoint[2] != prevPoint[2])){
281                         samePoint = false;
282                 }
283                 centroid[0] += currPoint[0];
284                 centroid[1] += currPoint[1];
285                 centroid[2] += currPoint[2];
286                 std::copy(std::begin(currPoint), std::end(currPoint), prevPoint);
287         }
288         centroid[0] /= numPoints;
289         centroid[1] /= numPoints;
290         centroid[2] /= numPoints;
291         
292         return !samePoint;
293 }
294
295 /**
296 * Closes the bottom of the given countour.
297 * Should only be used when its an open contour.
298 * uPointOrder: points are ordered in U shape
299 */
300 void CreateMeshFromPoints::CloseContourBottom(bool uPointOrder){
301         std::vector<int> lstIndexs = bbGetInputLstIndexs();
302         int sizeLstIdexes = lstIndexs.size();
303         int sizeLstX = bbGetInputLstX().size();
304         
305         vtkSmartPointer<vtkTriangleStrip> triangleStripBottom = vtkSmartPointer<vtkTriangleStrip>::New();
306         triangleStripBottom->GetPointIds()->SetNumberOfIds(sizeLstIdexes*2);
307         int triangleIndex = 0, currentId = 0, nextId = 0;
308         for(int splineIndex = 0; splineIndex < sizeLstIdexes;splineIndex++){
309                 triangleStripBottom->GetPointIds()->SetId(triangleIndex, currentId);
310                 nextId = uPointOrder?currentId + lstIndexs[splineIndex] - 1:sizeLstX - sizeLstIdexes + splineIndex;
311                 triangleStripBottom->GetPointIds()->SetId(triangleIndex+1, nextId);
312                 triangleIndex+=2;
313                 currentId = uPointOrder?nextId + 1: splineIndex+1;
314         }
315         cells->InsertNextCell(triangleStripBottom);
316 }
317
318 //===== 
319 // 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)
320 //===== 
321 void CreateMeshFromPoints::bbUserSetDefaultValues()
322 {
323
324 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
325 //    Here we initialize the input 'In' to 0
326 //   bbSetInputIn(0);
327         bbSetInputCloseSurface(false);
328         bbSetInputOpenClose(false);
329         points          = NULL;
330         cells           = NULL;
331         polydata        = NULL;
332         clean           = NULL;
333         triangle        = NULL;
334 }
335 //===== 
336 // 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)
337 //===== 
338 void CreateMeshFromPoints::bbUserInitializeProcessing()
339 {
340
341 //  THE INITIALIZATION METHOD BODY :
342 //    Here does nothing 
343 //    but this is where you should allocate the internal/output pointers 
344 //    if any 
345
346   
347 }
348 //===== 
349 // 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)
350 //===== 
351 void CreateMeshFromPoints::bbUserFinalizeProcessing()
352 {
353
354 //  THE FINALIZATION METHOD BODY :
355 //    Here does nothing 
356 //    but this is where you should desallocate the internal/output pointers 
357 //    if any
358   
359 }
360 }
361 // EO namespace bbcreaVtk
362
363