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 "bbcreaMaracasVisuShowNPoints_Tools.h"
5 #include "bbcreaMaracasVisuPackage.h"
7 #include <vtkPointData.h>
8 #include <vtkDataArray.h>
12 namespace bbcreaMaracasVisu
15 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints_Tools)
16 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints_Tools,bbtk::AtomicBlackBox);
20 void ShowNPoints_Tools::NearestPointToMesh( vtkPoints *points,vtkStaticPointLocator *pointLocator,double *spc,double *p,double *pM)
25 points->GetPoint( pointLocator->FindClosestPoint(p) , pM );
26 pM[0] = pM[0] / spc[0];
27 pM[1] = pM[1] / spc[1];
28 pM[2] = pM[2] / spc[2];
31 void ShowNPoints_Tools::CreatePatch_3points()
33 printf("PG ShowNPoints_Tools::CreatePatch_3points Entered patch 3 points !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
34 WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
35 wsp->StopAutoAddPoints();
36 wsp->StopTrackPoint();
37 if ( (wsp->GetLstModelShowNPointsSize()==1) &&
38 (wsp->GetModelShowNPoints()->GetLstPointsSize()==3) &&
39 (bbGetInputMesh()!=NULL ) )
41 std::vector<int> lstX = wsp->GetModelShowNPoints()->GetLstPointsX();
42 std::vector<int> lstY = wsp->GetModelShowNPoints()->GetLstPointsY();
43 std::vector<int> lstZ = wsp->GetModelShowNPoints()->GetLstPointsZ();
60 vtkMath::Subtract(pC,pA,v);
61 vtkMath::Subtract(pB,pA,u);
63 /**point in AC closest to B.
64 *formula t=(V)dot(U)/(V)dot(V)
66 double t = vtkMath::Dot(v,u)/vtkMath::Dot(v,v);
68 *formula A+t(C-A) -> A+tV
70 vtkMath::MultiplyScalar(v, t);
72 vtkMath::Add(pA, v, nP);
74 //calculate direction vector from found point to B
76 vtkMath::Subtract(pB, nP, dirVector);
81 //Add and subtract direction vector to A and C to find the 4 points to create the patch,
83 std::vector<int> resListX;
84 std::vector<int> resListY;
85 std::vector<int> resListZ;
87 vtkMath::Add(pA, dirVector, pointAdd);
88 vtkMath::Subtract(pA, dirVector, pointSub);
90 resListX.push_back(pointSub[0]);
91 resListY.push_back(pointSub[1]);
92 resListZ.push_back(pointSub[2]);
94 resListX.push_back(pointAdd[0]);
95 resListY.push_back(pointAdd[1]);
96 resListZ.push_back(pointAdd[2]);
99 vtkMath::Add(pC, dirVector, pointAdd);
100 vtkMath::Subtract(pC, dirVector, pointSub);
102 resListX.push_back(pointAdd[0]);
103 resListY.push_back(pointAdd[1]);
104 resListZ.push_back(pointAdd[2]);
106 resListX.push_back(pointSub[0]);
107 resListY.push_back(pointSub[1]);
108 resListZ.push_back(pointSub[2]);
109 printf("PG ShowNPoints_Tools::CreatePatch_3points Calls CreatePatch_Points !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
110 //Create patch given the previously calculated points (4 points)
111 CreatePatch_Points(resListX, resListY, resListZ);
113 printf("PG ShowNPoints_Tools::CreatePatch_3points Warning patch not apply. Need only one group of 3 points\n");
117 void ShowNPoints_Tools::CreatePatch_4points()
119 WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
120 wsp->StopAutoAddPoints();
121 wsp->StopTrackPoint();
122 if ( (wsp->GetLstModelShowNPointsSize()==1) &&
123 (wsp->GetModelShowNPoints()->GetLstPointsSize()==4) &&
124 (bbGetInputMesh()!=NULL ) )
126 std::vector<int> lstX = wsp->GetModelShowNPoints()->GetLstPointsX();
127 std::vector<int> lstY = wsp->GetModelShowNPoints()->GetLstPointsY();
128 std::vector<int> lstZ = wsp->GetModelShowNPoints()->GetLstPointsZ();
129 CreatePatch_Points(lstX, lstY, lstZ);
131 printf("PG ShowNPoints_Tools::CreatePatch_4points Warning patch not apply. Need only one group of 4 points\n");
135 void ShowNPoints_Tools::CreatePatch_Points(std::vector<int> lstX, std::vector<int> lstY, std::vector<int> lstZ)
137 WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
138 std::vector<long int> lstIdNormalSurface;
140 spc[0] = bbGetInputSpacing()[0];
141 spc[1] = bbGetInputSpacing()[1];
142 spc[2] = bbGetInputSpacing()[2];
143 vtkPoints *points = bbGetInputMesh()->GetPoints();
144 vtkStaticPointLocator *pointLocator = vtkStaticPointLocator::New();
145 pointLocator->SetDataSet( bbGetInputMesh() );
146 pointLocator->BuildLocator();
147 wsp->StopTrackPoint();
151 wsp->DeleteAllPoints_();
152 //wsp->ErasePoint( 0 );
153 //wsp->ErasePoint( 0 );
154 //wsp->ErasePoint( 0 );
155 //wsp->ErasePoint( 0 );
157 dx = lstX[1]-lstX[0];
158 dy = lstY[1]-lstY[0];
159 dz = lstZ[1]-lstZ[0];
163 for(int sect = 0; sect < 5; sect++)
165 p[0] = lstX[0] + dx*(sect*part);
166 p[1] = lstY[0] + dy*(sect*part);
167 p[2] = lstZ[0] + dz*(sect*part);
169 p[0] = p[0] + 0.5*(((lstX[1]+lstX[2])/2)-p[0]);
170 p[1] = p[1] + 0.5*(((lstY[1]+lstY[2])/2)-p[1]);
171 p[2] = p[2] + 0.5*(((lstZ[1]+lstZ[2])/2)-p[2]);
174 p[0] = p[0] + 0.5*(((lstX[0]+lstX[3])/2)-p[0]);
175 p[1] = p[1] + 0.5*(((lstY[0]+lstY[3])/2)-p[1]);
176 p[2] = p[2] + 0.5*(((lstZ[0]+lstZ[3])/2)-p[2]);
178 NearestPointToMesh(points, pointLocator, spc, p,pM);
179 wsp->InsertPoint(pM[0] ,pM[1], pM[2],"");
181 if(sect == 0 || sect == 4) lstIdNormalSurface.push_back( pointLocator->FindClosestPoint(pM) );
185 wsp->InsertCollectionAfter_();
186 p[0] = (lstX[0]+lstX[3])/2;
187 p[1] = (lstY[0]+lstY[3])/2;
188 p[2] = (lstZ[0]+lstZ[3])/2;
189 NearestPointToMesh(points, pointLocator, spc, p,pM);
190 wsp->InsertPoint(pM[0] ,pM[1], pM[2],"");
191 p[0] = (lstX[1]+lstX[2])/2;
192 p[1] = (lstY[1]+lstY[2])/2;
193 p[2] = (lstZ[1]+lstZ[2])/2;
194 NearestPointToMesh(points, pointLocator, spc, p,pM);
195 wsp->InsertPoint(pM[0] ,pM[1], pM[2],"");
196 p[0] = (lstX[0]+lstX[1]+lstX[2]+lstX[3])/4; p[1] = (lstY[0]+lstY[1]+lstY[2]+lstY[3])/4; p[2] = (lstZ[0]+lstZ[1]+lstZ[2]+lstZ[3])/4;
197 wsp->InsertPoint(p[0] ,p[1], p[2],"");
201 wsp->InsertCollectionAfter_();
203 dx = lstX[2]-lstX[3];
204 dy = lstY[2]-lstY[3];
205 dz = lstZ[2]-lstZ[3];
206 for(int sect = 0; sect < 5; sect++)
208 p[0] = lstX[3] + dx*(sect*part);
209 p[1] = lstY[3] + dy*(sect*part);
210 p[2] = lstZ[3] + dz*(sect*part);
212 p[0] = p[0] + 0.5*(((lstX[1]+lstX[2])/2)-p[0]);
213 p[1] = p[1] + 0.5*(((lstY[1]+lstY[2])/2)-p[1]);
214 p[2] = p[2] + 0.5*(((lstZ[1]+lstZ[2])/2)-p[2]);
217 p[0] = p[0] + 0.5*(((lstX[0]+lstX[3])/2)-p[0]);
218 p[1] = p[1] + 0.5*(((lstY[0]+lstY[3])/2)-p[1]);
219 p[2] = p[2] + 0.5*(((lstZ[0]+lstZ[3])/2)-p[2]);
221 NearestPointToMesh(points, pointLocator, spc, p,pM);
222 wsp->InsertPoint(pM[0] ,pM[1], pM[2],"");
224 if(sect == 0 || sect == 4) lstIdNormalSurface.push_back( pointLocator->FindClosestPoint(pM) );
227 pointLocator->Delete();
230 // 1. Recorrer las normales de lstIdNormals y calcular el promedio -> V1
233 vtkPointData *pointdata = bbGetInputMesh()->GetPointData();
234 vtkDataArray *dataarray = pointdata->GetNormals();
235 int i,size = lstIdNormalSurface.size();
239 for (i=0; i<size; i++)
241 nValue = dataarray->GetTuple3( lstIdNormalSurface[i] );
242 n1[0] = n1[0] + nValue[0];
243 n1[1] = n1[1] + nValue[1];
244 n1[2] = n1[2] + nValue[2];
250 // 2. Calcular el promedio de 4 normales de la nueva superficie -> V2
258 // Collection 0 with 4 points
259 // Collection 1 with 3 points
260 // Collection 2 with 4 points
261 wsp->GetCollectionPoint(1,1, pC);
263 wsp->GetCollectionPoint(0,0, pM);
264 vtkMath::Subtract(pM,pC,pM1);
265 wsp->GetCollectionPoint(0,3, pM);
266 vtkMath::Subtract(pM,pC,pM2);
267 vtkMath::Cross(pM1,pM2,pM);
272 wsp->GetCollectionPoint(0,3, pM);
273 vtkMath::Subtract(pM,pC,pM1);
274 wsp->GetCollectionPoint(2,3, pM);
275 vtkMath::Subtract(pM,pC,pM2);
276 vtkMath::Cross(pM1,pM2,pM);
277 n2[0] = n2[0] + pM[0];
278 n2[1] = n2[1] + pM[1];
279 n2[2] = n2[2] + pM[2];
281 wsp->GetCollectionPoint(2,3, pM);
282 vtkMath::Subtract(pM,pC,pM1);
283 wsp->GetCollectionPoint(2,0, pM);
284 vtkMath::Subtract(pM,pC,pM2);
285 vtkMath::Cross(pM1,pM2,pM);
286 n2[0] = n2[0] + pM[0];
287 n2[1] = n2[1] + pM[1];
288 n2[2] = n2[2] + pM[2];
290 wsp->GetCollectionPoint(2,0, pM);
291 vtkMath::Subtract(pM,pC,pM1);
292 wsp->GetCollectionPoint(0,0, pM);
293 vtkMath::Subtract(pM,pC,pM2);
294 vtkMath::Cross(pM1,pM2,pM);
295 n2[0] = n2[0] + pM[0];
296 n2[1] = n2[1] + pM[1];
297 n2[2] = n2[2] + pM[2];
303 // 3. Calcular el angulo entre V1 y V2
304 double angle = vtkMath::AngleBetweenVectors(n1,n2) * 180 / vtkMath::Pi();
305 // 4. Si el angulo es major de 90 Invertir las normales de la superficie actual
308 wsp->InvertLstPoints_();
313 wsp->UndoRedo_SaveCollection();
319 // 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)
321 void ShowNPoints_Tools::Process()
324 // THE MAIN PROCESSING METHOD BODY
325 // Here we simply set the input 'In' value to the output 'Out'
326 // And print out the output value
327 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
328 // void bbSet{Input|Output}NAME(const TYPE&)
329 // const TYPE& bbGet{Input|Output}NAME() const
331 // * NAME is the name of the input/output
332 // (the one provided in the attribute 'name' of the tag 'input')
333 // * TYPE is the C++ type of the input/output
334 // (the one provided in the attribute 'type' of the tag 'input')
336 // bbSetOutputOut( bbGetInputIn() );
337 // std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
339 if (bbGetInputWidgetShowNPoints()!=NULL)
341 if (bbGetInputType()==1)
343 bbGetInputWidgetShowNPoints()->OnAutoAddPoints_tool();
346 if (bbGetInputType()==5)
348 bbGetInputWidgetShowNPoints()->OnAddPoint_();
350 if (bbGetInputType()==10)
352 bbGetInputWidgetShowNPoints()->OnInsertPoint_();
354 if (bbGetInputType()==20)
356 bbGetInputWidgetShowNPoints()->OnTrackPoint_tool();
358 if (bbGetInputType()==30)
360 bbGetInputWidgetShowNPoints()->OnSetPoint_();
362 if (bbGetInputType()==40)
364 bbGetInputWidgetShowNPoints()->OnErasePoint_();
366 if (bbGetInputType()==50)
368 bbGetInputWidgetShowNPoints()->OnDeleteAllPoints_();
370 if (bbGetInputType()==100)
372 bbGetInputWidgetShowNPoints()->OnInsertCollectionAfter_();
374 if (bbGetInputType()==110)
376 bbGetInputWidgetShowNPoints()->OnDeleteCollection_();
378 if (bbGetInputType()==120)
380 bbGetInputWidgetShowNPoints()->OnResetCollections_();
382 if (bbGetInputType()==190)
384 CreatePatch_3points();
386 if (bbGetInputType()==200)
388 CreatePatch_4points();
390 if (bbGetInputType()==210)
392 bbGetInputWidgetShowNPoints()->OnInvertLstPoints_();
394 } // if bbGetInputWidgetShowNPoints
397 // 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)
399 void ShowNPoints_Tools::bbUserSetDefaultValues()
401 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
402 // Here we initialize the input 'In' to 0
404 bbSetInputMesh(NULL);
405 bbSetInputWidgetShowNPoints(NULL);
407 std::vector<double> spc;
411 bbSetInputSpacing(spc);
414 // 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)
416 void ShowNPoints_Tools::bbUserInitializeProcessing()
418 // THE INITIALIZATION METHOD BODY :
420 // but this is where you should allocate the internal/output pointers
424 // 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)
426 void ShowNPoints_Tools::bbUserFinalizeProcessing()
428 // THE FINALIZATION METHOD BODY :
430 // but this is where you should desallocate the internal/output pointers
434 } // EO namespace bbcreaMaracasVisu