]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx
#3270 creaVtk Feature New Normal - PolyDataToImageData Box
[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 "vtkPoints.h"
8 #include "vtkTriangleStrip.h"
9 #include "vtkCellArray.h"
10
11 #include "vtkCleanPolyData.h"
12 #include "vtkTriangleFilter.h"
13
14 namespace bbcreaVtk
15 {
16
17 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,CreateMeshFromPoints)
18 BBTK_BLACK_BOX_IMPLEMENTATION(CreateMeshFromPoints,bbtk::AtomicBlackBox);
19 //===== 
20 // 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)
21 //===== 
22 void CreateMeshFromPoints::Process()
23 {
24
25 // THE MAIN PROCESSING METHOD BODY
26 //   Here we simply set the input 'In' value to the output 'Out'
27 //   And print out the output value
28 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
29 //    void bbSet{Input|Output}NAME(const TYPE&)
30 //    const TYPE& bbGet{Input|Output}NAME() const 
31 //    Where :
32 //    * NAME is the name of the input/output
33 //      (the one provided in the attribute 'name' of the tag 'input')
34 //    * TYPE is the C++ type of the input/output
35 //      (the one provided in the attribute 'type' of the tag 'input')
36
37 //    bbSetOutputOut( bbGetInputIn() );
38 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
39
40
41 printf("EED CreateMeshFromPoints::Process Start\n");
42
43   
44                 std::vector<double> lstX                = bbGetInputLstX();
45                 std::vector<double> lstY                = bbGetInputLstY();
46                 std::vector<double> lstZ                = bbGetInputLstZ();
47                 std::vector<int> lstIndexs              = bbGetInputLstIndexs();
48
49
50                 if ( (lstIndexs.size()<=1) || (lstX.size()==0) || (lstX.size()!=lstY.size()) || (lstY.size()!=lstZ.size()) )
51                 {
52                         printf("Warnning! CreateMeshFromPoints::Process: List of points X Y Z  and LstIndexes is not correct\n");
53                 } else  {
54
55                         int ii,sizeSegment1,sizeSegment2;
56                         int endSegment;
57
58                         vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
59                         int i,sizeLstX  =       lstX.size();
60                         for (i=0;i<sizeLstX;i++)
61                         {
62                                 points->InsertNextPoint(lstX[i],lstY[i],lstZ[i]);
63                         } // for i
64
65                         vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
66                         int maxElements;
67                         int maxSegment1,maxSegment2;
68                         int iSeg1,iSeg2;
69
70                         int iGeneral    =       0;
71                         int     sizeLstIdexes=lstIndexs.size();
72                         for (i=0; i<sizeLstIdexes-1;i++)
73                         {
74                                 sizeSegment1=lstIndexs[i];
75                                 sizeSegment2=lstIndexs[i+1];
76
77                                 vtkSmartPointer<vtkTriangleStrip> triangleStrip = vtkSmartPointer<vtkTriangleStrip>::New();
78                                 triangleStrip->GetPointIds()->SetNumberOfIds(sizeSegment1+sizeSegment2);
79
80                                 maxElements=sizeSegment1;
81                                 if (maxElements<sizeSegment2) maxElements=sizeSegment2;
82                                 maxSegment1=iGeneral+sizeSegment1;
83                                 maxSegment2=iGeneral+sizeSegment1+sizeSegment2;
84                                 iSeg1=iGeneral;
85                                 iSeg2=iGeneral+sizeSegment1;
86                                 for (ii=0; ii<maxElements; ii++)
87                                 {
88                                         triangleStrip->GetPointIds()->SetId(ii*2  ,iSeg1);
89                                         triangleStrip->GetPointIds()->SetId(ii*2+1,iSeg2);
90                                         iSeg1++;
91                                         iSeg2++;
92                                         if (iSeg1>=maxSegment1) iSeg1=maxSegment1-1;
93                                         if (iSeg2>=maxSegment2) iSeg2=maxSegment2-1;
94                                 } // for ii 
95                                 iGeneral=iGeneral+sizeSegment1;
96                                 cells->InsertNextCell(triangleStrip);
97
98                         } //for  LstIndexs
99
100                         vtkPolyData *polydata = vtkPolyData::New();
101                         polydata->SetPoints(points);
102                         polydata->SetStrips(cells);
103
104                         vtkCleanPolyData *clean=vtkCleanPolyData::New();
105                         clean->SetInputData(polydata);
106                         clean->Update();
107
108                         vtkTriangleFilter *triangle = vtkTriangleFilter::New();
109                         triangle->SetInputData( clean->GetOutput() );
110                         triangle->Update();
111
112                         bbSetOutputOut( clean->GetOutput() );
113
114                 }// if listXYZ size
115
116 printf("EED CreateMeshFromPoints::Process End\n");
117
118
119 }
120 //===== 
121 // 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)
122 //===== 
123 void CreateMeshFromPoints::bbUserSetDefaultValues()
124 {
125
126 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
127 //    Here we initialize the input 'In' to 0
128 //   bbSetInputIn(0);
129   
130 }
131 //===== 
132 // 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)
133 //===== 
134 void CreateMeshFromPoints::bbUserInitializeProcessing()
135 {
136
137 //  THE INITIALIZATION METHOD BODY :
138 //    Here does nothing 
139 //    but this is where you should allocate the internal/output pointers 
140 //    if any 
141
142   
143 }
144 //===== 
145 // 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)
146 //===== 
147 void CreateMeshFromPoints::bbUserFinalizeProcessing()
148 {
149
150 //  THE FINALIZATION METHOD BODY :
151 //    Here does nothing 
152 //    but this is where you should desallocate the internal/output pointers 
153 //    if any
154   
155 }
156 }
157 // EO namespace bbcreaVtk
158
159