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