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