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