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)
4 #include "bbcreaVtkCreateMeshFromPoints.h"
5 #include "bbcreaVtkPackage.h"
7 #include "vtkTriangleStrip.h"
8 #include "vtkTriangle.h"
14 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,CreateMeshFromPoints)
15 BBTK_BLACK_BOX_IMPLEMENTATION(CreateMeshFromPoints,bbtk::AtomicBlackBox);
17 // 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)
19 void CreateMeshFromPoints::Process()
22 // THE MAIN PROCESSING METHOD BODY
23 // Here we simply set the input 'In' value to the output 'Out'
24 // And print out the output value
25 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
26 // void bbSet{Input|Output}NAME(const TYPE&)
27 // const TYPE& bbGet{Input|Output}NAME() const
29 // * NAME is the name of the input/output
30 // (the one provided in the attribute 'name' of the tag 'input')
31 // * TYPE is the C++ type of the input/output
32 // (the one provided in the attribute 'type' of the tag 'input')
34 // bbSetOutputOut( bbGetInputIn() );
35 // std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
37 std::vector<double> lstX = bbGetInputLstX();
38 std::vector<double> lstY = bbGetInputLstY();
39 std::vector<double> lstZ = bbGetInputLstZ();
40 std::vector<int> lstIndexs = bbGetInputLstIndexs();
41 if ( (lstIndexs.size()<1) || (lstX.size()==0) || (lstX.size()!=lstY.size()) || (lstY.size()!=lstZ.size()) )
43 printf("Warning! CreateMeshFromPoints::Process: List of points X Y Z and LstIndexes is not correct\n");
46 int ii,sizeSegment1,sizeSegment2;
48 // vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
49 if (points!=NULL) points->Delete();
50 points = vtkPoints::New();
51 int i,sizeLstX = lstX.size();
52 for (i=0;i<sizeLstX;i++)
54 points->InsertNextPoint(lstX[i],lstY[i],lstZ[i]);
56 // vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
57 if (cells!=NULL) cells->Delete();
58 cells = vtkCellArray::New();
60 int maxSegment1,maxSegment2;
63 int sizeLstIdexes=lstIndexs.size();
64 for (i=0; i<sizeLstIdexes-1; i++ )
66 sizeSegment1 = lstIndexs[i];
67 sizeSegment2 = lstIndexs[i+1];
68 vtkSmartPointer<vtkTriangleStrip> triangleStrip = vtkSmartPointer<vtkTriangleStrip>::New();
69 triangleStrip->GetPointIds()->SetNumberOfIds(sizeSegment1+sizeSegment2);
70 maxElements=sizeSegment1;
71 if (maxElements<sizeSegment2) maxElements=sizeSegment2;
72 maxSegment1 = iGeneral+sizeSegment1;
73 maxSegment2 = iGeneral+sizeSegment1+sizeSegment2;
75 iSeg2 = iGeneral+sizeSegment1;
76 for (ii=0; ii<maxElements; ii++)
78 triangleStrip->GetPointIds()->SetId(ii*2 ,iSeg1);
79 triangleStrip->GetPointIds()->SetId(ii*2+1,iSeg2);
82 if (iSeg1>=maxSegment1) { iSeg1=maxSegment1-1; }
83 if (iSeg2>=maxSegment2) { iSeg2=maxSegment2-1; }
85 iGeneral=iGeneral+sizeSegment1;
86 cells->InsertNextCell(triangleStrip);
89 if(bbGetInputCloseSurface())
91 int lastId1 = lstIndexs[0]-1;
92 int lastId2 = sizeLstX - 1;
93 int firstId2 = sizeLstX - lstIndexs[sizeLstIdexes - 1];
94 bool face1open = std::fabs(lstX[0] - lstX[lastId1]) > 0.0001 && std::fabs(lstY[0] - lstY[lastId1]) > 0.0001 && std::fabs(lstZ[0] - lstZ[lastId1]) > 0.0001;
95 bool face2open = std::fabs(lstX[firstId2] - lstX[lastId2]) > 0.0001 && std::fabs(lstY[firstId2] - lstY[lastId2]) > 0.0001 && std::fabs(lstZ[firstId2] - lstZ[lastId2]) > 0.0001;
97 bool altFace1open = std::fabs(lstX[0] - lstX[firstId2]) > 0.0001 && std::fabs(lstY[0] - lstY[firstId2]) > 0.0001 && std::fabs(lstZ[0] - lstZ[firstId2]) > 0.0001;
98 bool altFace2open = std::fabs(lstX[lastId1] - lstX[lastId2]) > 0.0001 && std::fabs(lstY[lastId1] - lstY[lastId2]) > 0.0001 && std::fabs(lstZ[lastId1] - lstZ[lastId2]) > 0.0001;
100 //false = Open Contour
101 //true = Closed Contour
102 if(!face1open && !face2open)
104 // isClosedCont = true;
105 CloseContourSides(lstIndexs, true, true);
107 else if(!altFace1open && !altFace2open)
109 // isClosedCont = true;
110 CloseContourSides(lstIndexs, false, true);
113 CloseOpenContourSurface(lstIndexs);
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);
127 // vtkTriangleFilter *triangle = vtkTriangleFilter::New();
128 if (triangle!=NULL) triangle->Delete();
129 triangle = vtkTriangleFilter::New();
130 triangle->SetInputData( clean->GetOutput() );
132 bbSetOutputOut( triangle->GetOutput() );
133 // bbSetOutputOut( clean->GetOutput() );
135 //printf("PG CreateMeshFromPoints::Process: End\n");
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
143 void CreateMeshFromPoints::CloseContourSides(std::vector<int> lstIndexs, bool uPointOrder, bool isClosedCont){
144 int sizeLstIdexes = lstIndexs.size();
145 int sizePoints = bbGetInputLstX().size();
147 int firstIndex, end, centroidId, numPointsFace, contraryId;
148 int increment = uPointOrder?1:sizeLstIdexes;
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);
156 numPointsFace = uPointOrder?lstIndexs[0]: sizeLstIdexes;
157 end = uPointOrder?firstIndex + numPointsFace:sizePoints - lstIndexs[sizeLstIdexes - 1] + 1;
158 contraryId = sizePoints-1;
160 firstIndex = uPointOrder?sizePoints - lstIndexs[sizeLstIdexes-1]:lstIndexs[0]-1;
161 numPointsFace = uPointOrder?lstIndexs[sizeLstIdexes-1]:sizeLstIdexes;
162 end = uPointOrder?firstIndex + numPointsFace:sizePoints;
165 if(numPointsFace > 1)
167 bool validCentroid = CalcValidCentroid(centroid, firstIndex, end, increment, numPointsFace);
170 bool normalOrder = isPointingCorrectly(firstIndex, firstIndex+increment, centroid, contraryId);
171 centroidId = points->InsertNextPoint(centroid[0], centroid[1], centroid[2]);
174 int initial = firstIndex;
175 for(int index = initial; index < end; index+=increment){
176 if(index+increment >= end && !isClosedCont){
177 vtkNew<vtkTriangle> triangle;
178 triangle->GetPointIds()->SetId(0, index);
179 triangle->GetPointIds()->SetId(1, initial);
180 triangle->GetPointIds()->SetId(2, centroidId);
181 cells->InsertNextCell(triangle);
182 }else if(index+increment < end){
183 vtkNew<vtkTriangle> triangle;
184 triangle->GetPointIds()->SetId(0, index);
185 triangle->GetPointIds()->SetId(1, index+increment);
186 triangle->GetPointIds()->SetId(2, centroidId);
187 cells->InsertNextCell(triangle);
191 int initial = firstIndex-1;
192 int triangleStripStart = end-1;
193 for(int index = triangleStripStart; index > initial; index-=increment){
194 if(index-increment <= initial && !isClosedCont){
195 vtkNew<vtkTriangle> triangle;
196 triangle->GetPointIds()->SetId(0, index);
197 triangle->GetPointIds()->SetId(1, triangleStripStart);
198 triangle->GetPointIds()->SetId(2, centroidId);
199 cells->InsertNextCell(triangle);
200 }else if(index-increment > initial){
201 vtkNew<vtkTriangle> triangle;
202 triangle->GetPointIds()->SetId(0, index);
203 triangle->GetPointIds()->SetId(1, index-increment);
204 triangle->GetPointIds()->SetId(2, centroidId);
205 cells->InsertNextCell(triangle);
216 * Checks if the normal from firstPointId, secPointId and centroid points away
217 * from the vector centroid to contrPointId.
218 * Used to check that the order used to create the new polygons is correct.
220 bool CreateMeshFromPoints::isPointingCorrectly( int firstPointId, int secPointId, double(¢roid)[3], int contrPointId) {
222 double firstPoint[3], secPoint[3], contrPoint[3];
223 points->GetPoint(firstPointId, firstPoint);
224 points->GetPoint(secPointId, secPoint);
226 double firstVect[3], secVect[3], normal[3], contrVect[3];
228 vtkMath::Subtract(firstPoint, centroid, firstVect);
229 vtkMath::Subtract(secPoint, centroid, secVect);
231 points->GetPoint(contrPointId, contrPoint);
232 vtkMath::Subtract(contrPoint, centroid, contrVect);
234 vtkMath::Cross(firstVect, secVect, normal);
236 dotCalc = vtkMath::Dot(normal, contrVect);
242 * Checks if points on each side of the shapes represent a curve.
244 bool CreateMeshFromPoints::CheckLinePointOrder(){
245 int sizePoints = bbGetInputLstX().size();
246 std::vector<int> lstIndexs = bbGetInputLstIndexs();
247 double point1[3], point2[3], point3[3];
249 double firstRadiusSum = 0;
250 double secondRadiusSum = 0;
251 for(int i = 0; i < lstIndexs[0] && lstIndexs[0] > 9; i+=5){
252 if(i+10 < lstIndexs[0]){
253 points->GetPoint(i, point1);
254 points->GetPoint(i+5, point2);
255 points->GetPoint(i+10, point3);
256 firstRadiusSum += vtkMath::Solve3PointCircle(point1, point2, point3, center);
260 for(int i = 0; i < sizePoints && lstIndexs.size() > 9; i+=(lstIndexs.size()*5)){
261 if(i+(10*lstIndexs.size()) < sizePoints){
262 points->GetPoint(i, point1);
263 points->GetPoint(i+(5*lstIndexs.size()), point2);
264 points->GetPoint(i+(10*lstIndexs.size()), point3);
265 secondRadiusSum += vtkMath::Solve3PointCircle(point1, point2, point3, center);
269 return firstRadiusSum > secondRadiusSum;
273 * Closes an open contour
274 * lstIndexs: number of points on each spline
276 void CreateMeshFromPoints::CloseOpenContourSurface(std::vector<int> lstIndexs){
277 bool linePointOrder = CheckLinePointOrder();
278 CloseContourSides(lstIndexs, !linePointOrder, false);
279 CloseContourBottom(!linePointOrder);
283 * Calculates centroid and checks if points are collinear.
284 * centroid: array to store calculation
285 * start: start index of points to use
286 * end: end index of points to use
287 * increment: increment to be used in point iteration
288 * numPoints: number of points used to calculate the centroid.
289 * Returns a bool indicating the validity of the centroid calculated.
290 * False = invalid centroid = all points are the same.
292 bool CreateMeshFromPoints::CalcValidCentroid(double(¢roid)[3], int start, int end, int increment, int numPoints){
293 double currPoint[3] = {}, prevPoint[3] = {}, middlePoint[3] = {}, firstPoint[3] = {};
294 double vector1[3], vector2[3];
295 bool samePoint = true;
296 int splineMidPoint = numPoints/2;
297 bool collinear = true;
299 points->GetPoint(start, firstPoint);
300 points->GetPoint(splineMidPoint, middlePoint);
301 vtkMath::Subtract(middlePoint, firstPoint, vector1);
303 for(int i = start; i < end; i+=increment){
304 points->GetPoint(i, currPoint);
305 if(samePoint && i > start && (currPoint[0] != prevPoint[0] || currPoint[1] != prevPoint[1] || currPoint[2] != prevPoint[2])){
309 vtkMath::Subtract(currPoint, firstPoint, vector2);
310 double angle = vtkMath::AngleBetweenVectors(vector1, vector2);
311 if(angle > 0.0001 && collinear){
315 centroid[0] += currPoint[0];
316 centroid[1] += currPoint[1];
317 centroid[2] += currPoint[2];
318 std::copy(std::begin(currPoint), std::end(currPoint), prevPoint);
321 centroid[0] /= numPoints;
322 centroid[1] /= numPoints;
323 centroid[2] /= numPoints;
325 return !samePoint && !collinear;
329 * Closes the bottom of the given countour.
330 * Should only be used when its an open contour.
331 * uPointOrder: points are ordered in U shape
333 void CreateMeshFromPoints::CloseContourBottom(bool uPointOrder){
334 std::vector<int> lstIndexs = bbGetInputLstIndexs();
335 int sizeLstIdexes = lstIndexs.size();
336 int sizeLstX = bbGetInputLstX().size();
338 vtkSmartPointer<vtkTriangleStrip> triangleStripBottom = vtkSmartPointer<vtkTriangleStrip>::New();
339 triangleStripBottom->GetPointIds()->SetNumberOfIds(sizeLstIdexes*2);
340 int triangleIndex = 0, currentId = 0, nextId = 0;
341 for(int splineIndex = 0; splineIndex < sizeLstIdexes;splineIndex++){
342 triangleStripBottom->GetPointIds()->SetId(triangleIndex, currentId);
343 nextId = uPointOrder?currentId + lstIndexs[splineIndex] - 1:sizeLstX - sizeLstIdexes + splineIndex;
344 triangleStripBottom->GetPointIds()->SetId(triangleIndex+1, nextId);
346 currentId = uPointOrder?nextId + 1: splineIndex+1;
348 cells->InsertNextCell(triangleStripBottom);
352 // 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)
354 void CreateMeshFromPoints::bbUserSetDefaultValues()
357 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
358 // Here we initialize the input 'In' to 0
360 bbSetInputCloseSurface(false);
368 // 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)
370 void CreateMeshFromPoints::bbUserInitializeProcessing()
373 // THE INITIALIZATION METHOD BODY :
375 // but this is where you should allocate the internal/output pointers
381 // 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)
383 void CreateMeshFromPoints::bbUserFinalizeProcessing()
386 // THE FINALIZATION METHOD BODY :
388 // but this is where you should desallocate the internal/output pointers
393 // EO namespace bbcreaVtk