]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuShowNPoints_Tools.cxx
#3517 ShowNPoints actual Point
[creaMaracasVisu.git] / bbtk / src / bbcreaMaracasVisuShowNPoints_Tools.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 "bbcreaMaracasVisuShowNPoints_Tools.h"
5 #include "bbcreaMaracasVisuPackage.h"
6
7 #include <vtkPointData.h>
8 #include <vtkDataArray.h>
9 #include <vtkMath.h>
10
11
12 namespace bbcreaMaracasVisu
13 {
14
15 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints_Tools)
16 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints_Tools,bbtk::AtomicBlackBox);
17
18
19
20 void ShowNPoints_Tools::NearestPointToMesh( vtkPoints *points,vtkStaticPointLocator *pointLocator,double *spc,double *p,double *pM)
21 {
22     p[0]  = p[0] * spc[0];
23     p[1]  = p[1] * spc[1];
24     p[2]  = p[2] * spc[2];
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];
29 }
30
31 void ShowNPoints_Tools::CreatePatch_3points()
32 {
33         //printf("PG ShowNPoints_Tools::CreatePatch_3points  Entered patch 3 points !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
34         WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
35         
36         std::vector<double> lstX = wsp->GetModelShowNPoints()->GetLstPointsX();
37         std::vector<double> lstY = wsp->GetModelShowNPoints()->GetLstPointsY();
38         std::vector<double> lstZ = wsp->GetModelShowNPoints()->GetLstPointsZ();
39         
40         double pA[3];
41     pA[0] = lstX[0];
42     pA[1] = lstY[0];
43     pA[2] = lstZ[0];
44     double pB[3];
45     pB[0] = lstX[1];
46     pB[1] = lstY[1];
47     pB[2] = lstZ[1];
48     double pC[3];
49     pC[0] = lstX[2];
50     pC[1] = lstY[2];
51     pC[2] = lstZ[2];
52
53         double v[3];
54         double u[3];
55         vtkMath::Subtract(pC,pA,v);
56         vtkMath::Subtract(pB,pA,u);
57         
58         /**point in AC closest to B.
59         *formula t=(V)dot(U)/(V)dot(V)
60         */
61         double t = vtkMath::Dot(v,u)/vtkMath::Dot(v,v);
62         /**nP found point
63         *formula A+t(C-A) -> A+tV
64         */
65         vtkMath::MultiplyScalar(v, t);
66         double nP[3];
67         vtkMath::Add(pA, v, nP);
68         
69         //calculate direction vector from found point to B
70         double dirVector[3];
71         vtkMath::Subtract(pB, nP, dirVector);
72         
73         double pointAdd[3];
74         double pointSub[3];
75         
76         //Add and subtract direction vector to A and C to find the 4 points to create the patch,
77         
78         std::vector<double> resListX;
79         std::vector<double> resListY;
80         std::vector<double> resListZ;
81         
82         vtkMath::Add(pA, dirVector, pointAdd);
83         vtkMath::Subtract(pA, dirVector, pointSub);
84         
85         resListX.push_back(pointSub[0]);
86         resListY.push_back(pointSub[1]);
87         resListZ.push_back(pointSub[2]);
88
89         resListX.push_back(pointAdd[0]);
90         resListY.push_back(pointAdd[1]);
91         resListZ.push_back(pointAdd[2]);
92         
93         
94         vtkMath::Add(pC, dirVector, pointAdd);
95         vtkMath::Subtract(pC, dirVector, pointSub);
96         
97         resListX.push_back(pointAdd[0]);
98         resListY.push_back(pointAdd[1]);
99         resListZ.push_back(pointAdd[2]);
100         
101         resListX.push_back(pointSub[0]);
102         resListY.push_back(pointSub[1]);
103         resListZ.push_back(pointSub[2]);
104
105         //Create patch given the previously calculated points (4 points)
106         CreatePatch_Points(resListX, resListY, resListZ);
107 }
108
109 void ShowNPoints_Tools::CreatePatch_4points()
110 {
111         WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
112              
113         std::vector<double> lstX = wsp->GetModelShowNPoints()->GetLstPointsX();
114         std::vector<double> lstY = wsp->GetModelShowNPoints()->GetLstPointsY();
115         std::vector<double> lstZ = wsp->GetModelShowNPoints()->GetLstPointsZ();
116         CreatePatch_Points(lstX, lstY, lstZ);
117 }
118
119 void ShowNPoints_Tools::InitCreatePatch_Points()
120 {
121         WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
122         wsp->StopAutoAddPoints();
123     wsp->StopTrackPoint();
124         if((wsp->GetLstModelShowNPointsSize()==1) && (bbGetInputMesh()!=NULL ))
125     {
126                 if(wsp->GetModelShowNPoints()->GetLstPointsSize()==4){
127                         CreatePatch_4points();
128                 }
129                 else if(wsp->GetModelShowNPoints()->GetLstPointsSize()==3){
130                         CreatePatch_3points();
131                 }
132                 else{
133                         printf("PG ShowNPoints_Tools::CreatePatch_Npoints  Warning patch not apply. Number of points is invalid. Group should have 3 or 4 points\n");
134                 }
135         }
136         else{
137                 printf("PG ShowNPoints_Tools::CreatePatch_Npoints  Warning patch not apply. groups or mesh invalid. Need 1 group of 3 or 4 points, and a mesh\n");
138     } // if wsp && mesh
139     
140     // --- Finish ---
141     wsp->RefreshCollectionText();
142     wsp->RefreshColourCollection();
143     wsp->SetOutputBox();
144     wsp->UndoRedo_SaveCollection();
145
146 }
147
148 void ShowNPoints_Tools::InitCreateVolumeSurface_Points()
149 {
150     InitCreatePatch_Points();   //  1 group -> 3 groups
151     
152     // 3 groups to 4 groups
153     WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
154     wsp->StopAutoAddPoints();
155     wsp->StopTrackPoint();
156     if( wsp->GetLstModelShowNPointsSize()==3 )
157     {
158         double spc[3];
159         double pC1[3];    // fist point
160         double dx,dy,dz;
161         double pC2A[3];  // first middle point
162         double pC2B[3];  // second midle point
163         double pC3[3];   // last point
164         double pN[3];    // new over the surface
165
166         vtkPoints               *points         = bbGetInputMesh()->GetPoints();
167         vtkStaticPointLocator   *pointLocator   = vtkStaticPointLocator::New();
168         pointLocator->SetDataSet( bbGetInputMesh() );
169         pointLocator->BuildLocator();
170         spc[0]          = bbGetInputSpacing()[0];
171         spc[1]          = bbGetInputSpacing()[1];
172         spc[2]          = bbGetInputSpacing()[2];
173         wsp->InsertCollectionAfter_();
174         wsp->GetCollectionPoint(1,0, pC1);
175         wsp->GetCollectionPoint(1,2, pC3);
176         dx=pC3[0]-pC1[0];
177         dy=pC3[1]-pC1[1];
178         dz=pC3[2]-pC1[2];
179         pC2A[0]=pC1[0]+dx*0.33333;   pC2A[1]=pC1[1]+dy*0.3333;   pC2A[2]=pC1[2]+dz*0.33333;
180         pC2B[0]=pC1[0]+dx*0.66666;   pC2B[1]=pC1[1]+dy*0.6666;   pC2B[2]=pC1[2]+dz*0.66666;
181         
182         wsp->InsertPoint(pC1[0] ,pC1[1], pC1[2],"");
183         
184         NearestPointToMesh(points, pointLocator, spc, pC2A,pN);
185         wsp->InsertPoint(pN[0] ,pN[1], pN[2],"");
186         
187         NearestPointToMesh(points, pointLocator, spc, pC2B,pN);
188         wsp->InsertPoint(pN[0] ,pN[1], pN[2],"");
189         
190         wsp->InsertPoint(pC3[0] ,pC3[1], pC3[2],"");
191                 
192         // Add points to the other Groups
193         std::vector<double> pointStart;
194         pointStart.push_back( pC1[0] );
195         pointStart.push_back( pC1[1] );
196         pointStart.push_back( pC1[2] );
197         std::vector<double> pointEnd;
198         pointEnd.push_back( pC3[0] );
199         pointEnd.push_back( pC3[1] );
200         pointEnd.push_back( pC3[2] );
201
202         wsp->SetActualCollection(0);
203         wsp->SetReferencePoint(pointStart);
204         wsp->OnInsertPoint_();
205         wsp->SetReferencePoint(pointEnd);
206         wsp->OnInsertPoint_();
207
208         wsp->SetActualCollection(2);
209         wsp->SetReferencePoint(pointStart);
210         wsp->OnInsertPoint_();
211         wsp->SetReferencePoint(pointEnd);
212         wsp->OnInsertPoint_();
213
214         // --- Finish ---
215         wsp->RefreshCollectionText();
216         wsp->RefreshColourCollection();
217         wsp->SetOutputBox();
218         wsp->UndoRedo_SaveCollection();
219
220         pointLocator->Delete();
221
222     } // if wsp && mesh
223 }
224
225
226 void ShowNPoints_Tools::CreatePatch_Points(std::vector<double> lstX, std::vector<double> lstY, std::vector<double> lstZ)
227 {
228     WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
229     std::vector<long int>   lstIdNormalSurface;
230     double                  spc[3];
231                             spc[0]          = bbGetInputSpacing()[0];
232                             spc[1]          = bbGetInputSpacing()[1];
233                             spc[2]          = bbGetInputSpacing()[2];
234     vtkPoints               *points         = bbGetInputMesh()->GetPoints();
235     vtkStaticPointLocator   *pointLocator   = vtkStaticPointLocator::New();
236     pointLocator->SetDataSet( bbGetInputMesh() );
237     pointLocator->BuildLocator();
238     wsp->StopTrackPoint();
239     double p[3],pM[3];
240     double dx,dy,dz;
241     
242     wsp->DeleteAllPoints_();
243     //wsp->ErasePoint( 0 );
244     //wsp->ErasePoint( 0 );
245     //wsp->ErasePoint( 0 );
246     //wsp->ErasePoint( 0 );
247     // --- Group 0 ---
248     dx = lstX[1]-lstX[0];
249     dy = lstY[1]-lstY[0];
250     dz = lstZ[1]-lstZ[0];
251     //
252     //
253     double part = 0.25;
254     for(int sect = 0; sect < 5; sect++)
255     {
256         p[0] = lstX[0] + dx*(sect*part);
257                 p[1] = lstY[0] + dy*(sect*part);
258                 p[2] = lstZ[0] + dz*(sect*part);
259                 if(sect == 4){
260                         p[0] = p[0] + 0.5*(((lstX[1]+lstX[2])/2)-p[0]);
261                         p[1] = p[1] + 0.5*(((lstY[1]+lstY[2])/2)-p[1]);
262                         p[2] = p[2] + 0.5*(((lstZ[1]+lstZ[2])/2)-p[2]);
263                 }
264                 if(sect == 0){
265                         p[0] = p[0] + 0.5*(((lstX[0]+lstX[3])/2)-p[0]);
266                         p[1] = p[1] + 0.5*(((lstY[0]+lstY[3])/2)-p[1]);
267                         p[2] = p[2] + 0.5*(((lstZ[0]+lstZ[3])/2)-p[2]);
268                 }
269                 NearestPointToMesh(points, pointLocator, spc, p,pM);
270                 wsp->InsertPoint(pM[0] ,pM[1], pM[2],"");
271                 if(sect == 0 || sect == 4) lstIdNormalSurface.push_back( pointLocator->FindClosestPoint(pM) );
272     }
273         
274     // --- Group 1 ---
275 //EED    double centroid[3];
276     
277     wsp->InsertCollectionAfter_();
278       p[0] = (lstX[0]+lstX[3])/2;
279       p[1] = (lstY[0]+lstY[3])/2;
280       p[2] = (lstZ[0]+lstZ[3])/2;
281       NearestPointToMesh(points, pointLocator, spc, p,pM);
282       wsp->InsertPoint(pM[0] ,pM[1], pM[2],"");
283       p[0] = (lstX[1]+lstX[2])/2;
284       p[1] = (lstY[1]+lstY[2])/2;
285       p[2] = (lstZ[1]+lstZ[2])/2;
286       NearestPointToMesh(points, pointLocator, spc, p,pM);
287       wsp->InsertPoint(pM[0] ,pM[1], pM[2],"");
288       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;
289       wsp->InsertPoint(p[0] ,p[1], p[2],"");
290 //EED     std::copy(std::begin(p), std::end(p), std::begin(centroid));
291     // --- Group 2 ---
292     wsp->InsertCollectionAfter_();
293     dx = lstX[2]-lstX[3];
294     dy = lstY[2]-lstY[3];
295     dz = lstZ[2]-lstZ[3];  
296     for(int sect = 0; sect < 5; sect++)
297     {
298         p[0] = lstX[3] + dx*(sect*part);
299                 p[1] = lstY[3] + dy*(sect*part);
300                 p[2] = lstZ[3] + dz*(sect*part);
301                 if(sect == 4){
302                         p[0] = p[0] + 0.5*(((lstX[1]+lstX[2])/2)-p[0]);
303                         p[1] = p[1] + 0.5*(((lstY[1]+lstY[2])/2)-p[1]);
304                         p[2] = p[2] + 0.5*(((lstZ[1]+lstZ[2])/2)-p[2]);
305                 }
306                 if(sect == 0){
307                         p[0] = p[0] + 0.5*(((lstX[0]+lstX[3])/2)-p[0]);
308                         p[1] = p[1] + 0.5*(((lstY[0]+lstY[3])/2)-p[1]);
309                         p[2] = p[2] + 0.5*(((lstZ[0]+lstZ[3])/2)-p[2]);
310                 }
311                 NearestPointToMesh(points, pointLocator, spc, p,pM);
312                 wsp->InsertPoint(pM[0] ,pM[1], pM[2],"");
313                 if(sect == 0 || sect == 4) lstIdNormalSurface.push_back( pointLocator->FindClosestPoint(pM) );
314     }
315      pointLocator->Delete();
316     // Check normals
317 //        1. Recorrer las normales de lstIdNormals y calcular el promedio  -> V1
318     double          *nValue;
319         double                  n1[3];
320     vtkPointData    *pointdata  =  bbGetInputMesh()->GetPointData();
321     vtkDataArray    *dataarray  = pointdata->GetNormals();
322         int i,size = lstIdNormalSurface.size();
323         n1[0]=0;
324         n1[1]=0;
325         n1[2]=0;
326         for (i=0; i<size; i++)
327         {
328             nValue        = dataarray->GetTuple3( lstIdNormalSurface[i] );
329                 n1[0] = n1[0] + nValue[0];
330                 n1[1] = n1[1] + nValue[1];
331                 n1[2] = n1[2] + nValue[2];
332         } // for i
333         n1[0] = n1[0]/size;
334         n1[1] = n1[1]/size;
335         n1[2] = n1[2]/size;
336
337 //        2. Calcular el promedio de 4 normales de la nueva superficie     -> V2
338     double pC[3];
339     double pM1[3];
340     double pM2[3];
341     double n2[3];
342     n2[0] = 0;
343     n2[1] = 0;
344     n2[2] = 0;
345     // Collection 0 with 4 points
346     // Collection 1 with 3 points
347     // Collection 2 with 4 points
348     wsp->GetCollectionPoint(1,1, pC);
349     
350     wsp->GetCollectionPoint(0,0, pM);
351     vtkMath::Subtract(pM,pC,pM1);
352     wsp->GetCollectionPoint(0,3, pM);
353     vtkMath::Subtract(pM,pC,pM2);
354     vtkMath::Cross(pM1,pM2,pM);
355     n2[0] = pM[0];
356     n2[1] = pM[1];
357     n2[2] = pM[2];
358
359     wsp->GetCollectionPoint(0,3, pM);
360     vtkMath::Subtract(pM,pC,pM1);
361     wsp->GetCollectionPoint(2,3, pM);
362     vtkMath::Subtract(pM,pC,pM2);
363     vtkMath::Cross(pM1,pM2,pM);
364     n2[0] = n2[0] + pM[0];
365     n2[1] = n2[1] + pM[1];
366     n2[2] = n2[2] + pM[2];
367
368     wsp->GetCollectionPoint(2,3, pM);
369     vtkMath::Subtract(pM,pC,pM1);
370     wsp->GetCollectionPoint(2,0, pM);
371     vtkMath::Subtract(pM,pC,pM2);
372     vtkMath::Cross(pM1,pM2,pM);
373     n2[0] = n2[0] + pM[0];
374     n2[1] = n2[1] + pM[1];
375     n2[2] = n2[2] + pM[2];
376     
377     wsp->GetCollectionPoint(2,0, pM);
378     vtkMath::Subtract(pM,pC,pM1);
379     wsp->GetCollectionPoint(0,0, pM);
380     vtkMath::Subtract(pM,pC,pM2);
381     vtkMath::Cross(pM1,pM2,pM);
382     n2[0] = n2[0] + pM[0];
383     n2[1] = n2[1] + pM[1];
384     n2[2] = n2[2] + pM[2];
385     
386     n2[0] = n2[0] / 4;
387     n2[1] = n2[1] / 4;
388     n2[2] = n2[2] / 4;
389     
390 //        3. Calcular el angulo entre V1 y V2
391     double angle = vtkMath::AngleBetweenVectors(n1,n2) * 180 / vtkMath::Pi();
392 //        4. Si el angulo es major de 90 Invertir las normales de la superficie actual
393     if (angle<90)
394     {
395         vtkMath::MultiplyScalar(n2, -1);
396         wsp->InvertLstPoints_();
397     } // if angle
398     
399 //EED 2023 07 12
400     //EED    std::vector<double> normalOut(n2, n2 + 3);
401     //EED    double norm = vtkMath::Norm(n2);
402     //EED    normalOut[0] = normalOut[0]/norm;
403     //EED       normalOut[1] = normalOut[1]/norm;
404     //EED       normalOut[2] = normalOut[2]/norm;
405         
406     //EED       std::vector<double> outputData;
407     //EED       outputData.insert(outputData.end(), &centroid[0], &centroid[3]);
408     //EED       outputData.insert(outputData.end(), &normalOut[0], &normalOut[3]);
409         
410 //EED   bbSetOutputOut(outputData);
411     //bbSetOutputOut(normalOut);
412     
413     // --- Finish ---
414     wsp->SetOutputBox();
415     wsp->UndoRedo_SaveCollection();
416 }
417
418 void ShowNPoints_Tools::MovePatchCenter()
419 {
420     
421 /* EED 2023 07 12
422         std::vector<double> params = bbGetInputParams();
423         if(params.size() == 4)
424         {
425                 if(params[3] != 1 && params[3] != -1)
426                 {
427                         printf("PG ShowNPoints_Tools::MovePatchCenter()  Warning params are wrong. direction of movement should be 1 or -1\n");
428                         return;
429                 }
430                 
431                 double centerPoint[3];
432                 std::vector<double> normal, modPoint;
433                 normal.push_back(params[0]);
434                 normal.push_back(params[1]);
435                 normal.push_back(params[2]);
436                 int direction = -1 * params[3];
437                 WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
438                 wsp->GetCollectionPoint(1,1, centerPoint);
439                 modPoint.push_back(centerPoint[0] + direction * normal[0]);
440                 modPoint.push_back(centerPoint[1] + direction * normal[1]);
441                 modPoint.push_back(centerPoint[2] + direction * normal[2]);
442                 wsp->SetCollectionPoint(1, 1, modPoint);
443     
444     
445         // --- Finish ---
446         wsp->SetOutputBox();
447         wsp->UndoRedo_SaveCollection();
448         }else{
449                 printf("PG ShowNPoints_Tools::MovePatchCenter()  Warning params are wrong. Need 4: normal x y z and direction of movement (1 or -1)\n");
450         }
451  
452  */
453
454     double              step                = bbGetInputParams()[0];
455     WidgetShowNPoints   *wsp                = bbGetInputWidgetShowNPoints();
456     wsp->SetActualCollection( wsp->GetLstModelShowNPointsSize()/2 );
457     ModelShowNPoints    *modelSNP           = wsp->GetModelShowNPoints( );
458     modelSNP->SetIdCurrentPoint( modelSNP->GetLstPointsSize()/2 );
459     wsp->MovePoint_( step );
460 }
461
462 /**
463 *       Creates a cutting surface with 1 group of points. This surface can have its area adjusted 
464 *       (increase or decrease the distance from the points to the center)
465 *       The centroid is set as the output, to be used by the expanding tool.
466 */
467 void ShowNPoints_Tools::CreateExpandedSurface()
468 {
469         //
470         //Set Input to 0 as this function should only be executed manually but still update its children.
471         bbSetInputType(0);
472         //
473         //
474         WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
475         wsp->StopAutoAddPoints();
476     wsp->StopTrackPoint();
477         if((wsp->GetLstModelShowNPointsSize()==1) && (bbGetInputMesh()!=NULL ) && (wsp->GetModelShowNPoints()->GetLstPointsSize()>0))
478         {       
479                 //Set Input to 0 as this function should only be executed manually but still update its children.
480                 //bbSetInputType(0);
481                 //
482                 std::vector<double> lstX = wsp->GetModelShowNPoints()->GetLstPointsX();
483                 std::vector<double> lstY = wsp->GetModelShowNPoints()->GetLstPointsY();
484                 std::vector<double> lstZ = wsp->GetModelShowNPoints()->GetLstPointsZ();
485                 
486                 wsp->DeleteAllPoints_();
487                 double centroid[3];
488                 
489                 for(int i = 0; i < (int)lstX.size(); i++){
490                         centroid[0] += lstX[i];
491                         centroid[1] += lstY[i];
492                         centroid[2] += lstZ[i];
493                 }
494                 centroid[0] /= lstX.size();
495                 centroid[1] /= lstX.size();
496                 centroid[2] /= lstX.size();
497                 
498                 double p[3];
499                 double dV[3];
500                 
501                 for(int i = 0; i < (int)lstX.size(); i++){
502                         dV[0] = lstX[i] - centroid[0];
503                         dV[1] = lstY[i] - centroid[1];
504                         dV[2] = lstZ[i] - centroid[2];
505                         vtkMath::Normalize(dV);
506                         
507                         p[0] = lstX[i] + 4*dV[0];
508                         p[1] = lstY[i] + 4*dV[1];
509                         p[2] = lstZ[i] + 4*dV[2];
510                         wsp->AddPoint(p[0] ,p[1], p[2],"");
511                 }
512                 std::vector<double> outData = {centroid[0], centroid[1], centroid[2]};
513                 bbSetOutputOut(outData);
514         }
515         else{
516                 printf("PG ShowNPoints_Tools::CreateExpandedSurface  Warning surface not apply. groups, mesh or points invalid. need 1 group of points\n");
517         }
518         // --- Finish ---
519         wsp->SetOutputBox();
520         wsp->UndoRedo_SaveCollection();
521 }
522
523 /**
524 *       Creates a cutting surface with 3 groups of points. This surface can have its area adjusted
525 *       (Increase or decrease distance from points to centroid of spline or distance between splines)
526 *       The centroid and normal is set as the Output, to be used by the expanding tools.
527 */
528 void ShowNPoints_Tools::CreateWideExpandedSurface()
529 {
530         //
531         //Set Input to 0 as this function is only used by the popup menu
532         //bbSetInputType(0);
533         //
534         //
535         WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
536         wsp->StopAutoAddPoints();
537     wsp->StopTrackPoint();
538     
539         if((wsp->GetLstModelShowNPointsSize()==1) && (bbGetInputMesh()!=NULL ) && (wsp->GetModelShowNPoints()->GetLstPointsSize()>0))
540         {       
541                 std::vector<double> lstX = wsp->GetModelShowNPoints()->GetLstPointsX();
542                 std::vector<double> lstY = wsp->GetModelShowNPoints()->GetLstPointsY();
543                 std::vector<double> lstZ = wsp->GetModelShowNPoints()->GetLstPointsZ();
544                 
545                 wsp->DeleteAllPoints_();
546                 
547                 double normal[3];
548                 double centroid[3];
549                         
550                 for(int i = 0; i < (int)lstX.size(); i++){
551                         centroid[0] += lstX[i];
552                         centroid[1] += lstY[i];
553                         centroid[2] += lstZ[i];
554                 }
555                 centroid[0] /= lstX.size();
556                 centroid[1] /= lstX.size();
557                 centroid[2] /= lstX.size();
558                 
559                 double v1[3], v2[3];
560                 double currNormal[3];
561                 
562                 for(int i = 0; i < ((int)lstX.size())-1; i++){
563                         v1[0] = lstX[i] - centroid[0];
564                         v1[1] = lstY[i] - centroid[1];
565                         v1[2] = lstZ[i] - centroid[2];          
566                         v2[0] = lstX[i+1] - centroid[0];
567                         v2[1] = lstY[i+1] - centroid[1];
568                         v2[2] = lstZ[i+1] - centroid[2];
569                         vtkMath::Cross(v1, v2, currNormal);
570                         normal[0] += currNormal[0];
571                         normal[1] += currNormal[1];
572                         normal[2] += currNormal[2];
573                 }
574                 
575                 normal[0] /= (lstX.size()-1);
576                 normal[1] /= (lstX.size()-1);
577                 normal[2] /= (lstX.size()-1);
578                 
579                 vtkMath::Normalize(normal);
580                 
581                 double np[3], dV[3];
582                 int     addNormal = 1;
583                 //Add new groups on both sides from the original spline
584                 for(int group = 0; group < 2; group++){
585                         for(int i = 0; i < (int) lstX.size(); i++){
586                                 dV[0] = lstX[i] - centroid[0];
587                                 dV[1] = lstY[i] - centroid[1];
588                                 dV[2] = lstZ[i] - centroid[2];
589                                 vtkMath::Normalize(dV);
590                                 
591                                 np[0] = lstX[i] + dV[0]*4 + ((normal[0]*2)*addNormal);
592                                 np[1] = lstY[i] + dV[1]*4 + ((normal[1]*2)*addNormal);
593                                 np[2] = lstZ[i] + dV[2]*4 + ((normal[2]*2)*addNormal);
594                                 wsp->AddPoint(np[0] ,np[1], np[2],"");
595                         }
596                         addNormal = -1;
597                         if(group < 1)wsp->InsertCollectionAfter_();
598                 }
599                 
600                 std::vector<double> outData = {centroid[0], centroid[1], centroid[2], normal[0], normal[1], normal[2]};
601                 bbSetOutputOut(outData);
602                 
603                 // --- Finish ---
604         wsp->SetOutputBox();
605         wsp->UndoRedo_SaveCollection();
606         }
607         else{
608                 printf("PG ShowNPoints_Tools::CreateWideExpandedSurface  Warning surface not apply. groups, mesh or points invalid. need 1 group of points\n");
609         }
610 }
611
612 /**
613 *       Given a cutting surface, expand its area by moving the points outwards of the main centroid.
614 */
615 void ShowNPoints_Tools::ExpandSurfaceArea()
616 {
617         WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
618         wsp->StopAutoAddPoints();
619     wsp->StopTrackPoint();
620     
621         if((wsp->GetLstModelShowNPointsSize()==1 || wsp->GetLstModelShowNPointsSize() == 2)  
622                 && (wsp->GetModelShowNPoints()->GetLstPointsSize()>0)
623                 && (bbGetInputParams().size() == 4))
624         {
625                 std::vector<double> params = bbGetInputParams();
626                 double direction = params[3];
627                 double centroid[3] = {params[0], params[1], params[2]};
628                 
629                 double dV[3], currPoint[3], mdfdPoint[3];
630                 
631                 std::vector<double> modPoint;
632                 int pointsPerSpline = wsp->GetModelShowNPoints()->GetLstPointsSize();
633                 for(int i = 0; i < pointsPerSpline; i++){
634                         wsp->GetCollectionPoint(0, i, currPoint);
635                         dV[0] = currPoint[0] - centroid[0];
636                         dV[1] = currPoint[1] - centroid[1];
637                         dV[2] = currPoint[2] - centroid[2];
638                         vtkMath::Normalize(dV);
639                         
640                         vtkMath::MultiplyScalar(dV, direction);
641                         vtkMath::Add(currPoint, dV, mdfdPoint);
642                         modPoint.insert(modPoint.begin(), std::begin(mdfdPoint), std::end(mdfdPoint));
643                         wsp->SetCollectionPoint(0, i, modPoint);
644                         
645                         if(wsp->GetLstModelShowNPointsSize() == 2){
646                                 wsp->GetCollectionPoint(1, i, currPoint);
647                                 vtkMath::Add(currPoint, dV, mdfdPoint);
648                                 modPoint.insert(modPoint.begin(), std::begin(mdfdPoint), std::end(mdfdPoint));
649                                 wsp->SetCollectionPoint(1, i, modPoint);
650                         }
651                 }
652         }
653         else{
654                 printf("PG ShowNPoints_Tools::ExpandSurface  Warning surface not apply. groups, points or params invalid. need 1 group of points, need 4 params(centroid and direction)\n");
655         }
656         // --- Finish ---
657         wsp->SetOutputBox();
658         wsp->UndoRedo_SaveCollection();
659 }
660
661 void ShowNPoints_Tools::ExpandPatch()
662 {
663         WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
664         wsp->StopAutoAddPoints();
665     wsp->StopTrackPoint();
666         if((wsp->GetLstModelShowNPointsSize() == 3)  
667                 && (wsp->GetLstPointsX().size() == 13)
668                 && (bbGetInputParams().size() == 4))
669         {
670                 std::vector<double> params = bbGetInputParams();
671                 double direction = params[3];
672                 double centroid[3] = {params[0], params[1], params[2]};
673
674                 std::vector<double> modPoint(3);
675                 double currentPoint[3], dV[3], mdfdPoint[3];
676                 for(int group = 0; group < 3; group++){
677                         if(group == 1){
678                                 wsp->GetCollectionPoint(group, 0, currentPoint);
679                                 dV[0] = currentPoint[0] - centroid[0];
680                                 dV[1] = currentPoint[1] - centroid[1];
681                                 dV[2] = currentPoint[2] - centroid[2];
682                                 vtkMath::Normalize(dV);
683                                 vtkMath::MultiplyScalar(dV, direction);
684                                 vtkMath::Add(currentPoint, dV, mdfdPoint);
685                                 modPoint.insert(modPoint.begin(), std::begin(mdfdPoint), std::end(mdfdPoint));
686                                 wsp->SetCollectionPoint(group, 0, modPoint);
687                                 wsp->GetCollectionPoint(group, 2, currentPoint);
688                                 dV[0] = currentPoint[0] - centroid[0];
689                                 dV[1] = currentPoint[1] - centroid[1];
690                                 dV[2] = currentPoint[2] - centroid[2];
691                                 vtkMath::Normalize(dV);
692                                 vtkMath::MultiplyScalar(dV, direction);
693                                 vtkMath::Add(currentPoint, dV, mdfdPoint);
694                                 modPoint.insert(modPoint.begin(), std::begin(mdfdPoint), std::end(mdfdPoint));
695                                 wsp->SetCollectionPoint(group, 2, modPoint);
696                         }else{
697                                 for(int i = 0; i < 5; i++){
698                                         wsp->GetCollectionPoint(group,i, currentPoint);
699                                         dV[0] = currentPoint[0] - centroid[0];
700                                         dV[1] = currentPoint[1] - centroid[1];
701                                         dV[2] = currentPoint[2] - centroid[2];
702                                         vtkMath::Normalize(dV);
703                                         vtkMath::MultiplyScalar(dV, direction);
704                                         vtkMath::Add(currentPoint, dV, mdfdPoint);
705                                         modPoint.insert(modPoint.begin(), std::begin(mdfdPoint), std::end(mdfdPoint));
706                                         wsp->SetCollectionPoint(group, i, modPoint);
707                                 }
708                         }
709                 }
710         }else{
711                 printf("PG ShowNPoints_Tools::ExpandPatch  Warning groups, points or params invalid. need 3 groups of points, need 4 params(centroid, and direction 1 or -1)\n");
712         }
713         // --- Finish ---
714         wsp->SetOutputBox();
715         wsp->UndoRedo_SaveCollection();
716 }
717
718 /**
719 *       Given a cutting surface, expand the distance between the edge splines and the middle spline. making it "wider" or "thicker".
720 */
721 void ShowNPoints_Tools::WidenSurface()
722 {
723         WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
724         wsp->StopAutoAddPoints();
725     wsp->StopTrackPoint();
726
727         if((wsp->GetLstModelShowNPointsSize()==2)  
728                 && (wsp->GetModelShowNPoints()->GetLstPointsSize()>0)
729                 && (bbGetInputParams().size() == 4))
730         {
731                 double direction = bbGetInputParams()[3];
732                 double normal[3] = {bbGetInputParams()[0], bbGetInputParams()[1], bbGetInputParams()[2]};
733                 vtkMath::MultiplyScalar(normal, direction);
734                 
735                 int pointsPerSpline = wsp->GetModelShowNPoints()->GetLstPointsSize();
736                 double pointSp1[3], pointSp2[3];
737                 std::vector<double> modifiedPoint;
738                 for(int i = 0; i < pointsPerSpline; i++){
739                         wsp->GetCollectionPoint(0, i, pointSp1);
740                         wsp->GetCollectionPoint(1, i, pointSp2);
741                         vtkMath::Add(pointSp1, normal, pointSp1);
742                         vtkMath::Subtract(pointSp2, normal, pointSp2);
743                         modifiedPoint.insert(modifiedPoint.begin(), std::begin(pointSp1), std::end(pointSp1));
744                         wsp->SetCollectionPoint(0, i, modifiedPoint);
745                         modifiedPoint.insert(modifiedPoint.begin(), std::begin(pointSp2), std::end(pointSp2));
746                         wsp->SetCollectionPoint(1, i, modifiedPoint);
747                 }
748         
749         }else{
750                 printf("PG ShowNPoints_Tools::WidenSurface  Warning surface not apply. groups, points or params invalid. need 3 group of points, need 4 params(normal and direction)\n");
751         }
752         // --- Finish ---
753         wsp->SetOutputBox();
754         wsp->UndoRedo_SaveCollection();
755 }
756
757 void ShowNPoints_Tools::MovePointInNormal()
758 {
759         //move the currently selected control point in the direction or opposite of the normal vector of the closest point in the input mesh
760
761         WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
762         wsp->StopAutoAddPoints();
763     wsp->StopTrackPoint();
764     if((wsp->GetLstModelShowNPointsSize()>=2)  //Check condition
765                 && (wsp->GetModelShowNPoints()->GetLstPointsSize()>0)
766                 && (bbGetInputParams().size() == 1)
767                 && (bbGetInputMesh() != NULL)
768                 && (bbGetInputSpacing().size() == 3))
769         {
770                 if(bbGetInputParams()[0] != -1 && bbGetInputParams()[0] != 1){
771                         printf("PG ShowNPoints_Tools::MovePointInNormal No direction provided, must be 1 or -1");
772                         return;
773                 }
774                 double spc[3];
775         double x,y,z;
776         double p[3], normal[3];;         // point to modify and normal
777         std::vector<double> pN(3);    // new point
778
779         spc[0]                                                          = bbGetInputSpacing()[0];
780         spc[1]                                                          = bbGetInputSpacing()[1];
781         spc[2]                                                          = bbGetInputSpacing()[2];
782         
783         vtkStaticPointLocator   *pointLocator   = vtkStaticPointLocator::New();
784         pointLocator->SetDataSet( bbGetInputMesh() );
785         pointLocator->BuildLocator();
786         
787 //        int idControlPoint = wsp->GetModelShowNPoints()->GetNearestPoint();
788         int idControlPoint = wsp->GetModelShowNPoints()->GetIdCurrentPoint();
789         
790         if(idControlPoint >= 0){
791                     wsp->GetModelShowNPoints()->GetIdPoint(idControlPoint, &x, &y, &z);
792                     p[0] = x * spc[0];
793                     p[1] = y * spc[1];
794                     p[2] = z * spc[2];
795                     int idMeshPoint = pointLocator->FindClosestPoint(p);
796                     bbGetInputMesh()->GetPointData()->GetNormals()->GetTuple(idMeshPoint, normal);
797                         int direction = bbGetInputParams()[0];
798                     pN[0] = p[0] / spc[0] + direction*normal[0];
799                     pN[1] = p[1] / spc[1] + direction*normal[1];
800                     pN[2] = p[2] / spc[2] + direction*normal[2];
801                     
802                     wsp->GetModelShowNPoints()->SetPointById(idControlPoint, pN);
803 //                  wsp->GetViewShowNPoints()->RefreshPoint(idControlPoint);
804             wsp->RefreshCollectionText();
805             wsp->RefreshColourCollection();
806                     wsp->SetOutputBox();
807                 wsp->UndoRedo_SaveCollection();
808         } // if idControlPoint
809         } // if wsp->GetLstModelShowNPointsSiz->size()>=2
810 }
811
812 void ShowNPoints_Tools::ChangeCurrentPoint()
813 {
814     if(bbGetInputParams()[0] != -1 && bbGetInputParams()[0] != 1){
815         printf("PG ShowNPoints_Tools::MovePointInNormal No direction provided, must be 1 or -1");
816         return;
817     } // if Params
818     int step=bbGetInputParams()[0];
819     WidgetShowNPoints   *wsp            = bbGetInputWidgetShowNPoints();
820     int                 idControlPoint  = wsp->GetModelShowNPoints()->GetIdCurrentPoint() + step;
821     int size=wsp->GetModelShowNPoints()->GetLstPointsSize();
822     if (idControlPoint<0)       { idControlPoint = size-1;  }
823     if (idControlPoint>=size )  { idControlPoint = 0;       }
824     wsp->GetModelShowNPoints()->SetIdCurrentPoint( idControlPoint );
825     wsp->RefreshCollectionText();
826     wsp->RefreshColourCollection();
827 }
828
829 void ShowNPoints_Tools::JoinPoints()
830 {
831         WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
832         wsp->StopAutoAddPoints();
833     wsp->StopTrackPoint();
834     if((wsp->GetLstModelShowNPointsSize()>=2)
835                 && (wsp->GetModelShowNPoints()->GetLstPointsSize()>0))
836         {
837                 int numberOfSplines = wsp->GetLstModelShowNPointsSize();
838                 double x, y, z;
839                 int sizeCurrentSpline, i;
840                 wsp->GetModelShowNPoints()->GetIdPoint(0, &x, &y, &z);
841                 std::vector<double> pointStart = {x, y, z};
842                 sizeCurrentSpline = wsp->GetModelShowNPoints()->GetLstPointsX().size();
843                 wsp->GetModelShowNPoints()->GetIdPoint(sizeCurrentSpline-1, &x, &y, &z);
844                 std::vector<double> pointEnd = {x, y, z};
845                 for(i = 0; i < numberOfSplines; i++){
846                         sizeCurrentSpline = wsp->GetModelShowNPoints(i)->GetLstPointsX().size();
847                         wsp->SetCollectionPoint(i, 0, pointStart);
848                         wsp->SetCollectionPoint(i, sizeCurrentSpline-1, pointEnd);
849                 }
850                 wsp->SetOutputBox();
851         wsp->UndoRedo_SaveCollection();
852         } // if Size
853 }
854
855 void ShowNPoints_Tools::SetMesh()
856 {
857     WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
858     wsp->SetAuxMesh(bbGetInputMesh(), bbGetInputSpacing(), bbGetInputParams() );
859 }
860
861 //=====
862 // 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)
863 //===== 
864 void ShowNPoints_Tools::Process()
865 {
866
867 // THE MAIN PROCESSING METHOD BODY
868 //   Here we simply set the input 'In' value to the output 'Out'
869 //   And print out the output value
870 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
871 //    void bbSet{Input|Output}NAME(const TYPE&)
872 //    const TYPE& bbGet{Input|Output}NAME() const 
873 //    Where :
874 //    * NAME is the name of the input/output
875 //      (the one provided in the attribute 'name' of the tag 'input')
876 //    * TYPE is the C++ type of the input/output
877 //      (the one provided in the attribute 'type' of the tag 'input')
878
879 //    bbSetOutputOut( bbGetInputIn() );
880 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
881
882     if (bbGetInputWidgetShowNPoints()!=NULL)
883     {
884         if (bbGetInputType()==1)
885         {
886             bbGetInputWidgetShowNPoints()->OnAutoAddPoints_tool();
887         } // if Type
888
889         if (bbGetInputType()==5)
890         {
891             bbGetInputWidgetShowNPoints()->OnAddPoint_();
892         } // if Type
893         if (bbGetInputType()==10)
894         {
895             bbGetInputWidgetShowNPoints()->OnInsertPoint_();
896         } // if Type
897         if (bbGetInputType()==20)
898         {
899             bbGetInputWidgetShowNPoints()->OnTrackPoint_tool();
900         } // if Type
901         if (bbGetInputType()==30)
902         {
903             bbGetInputWidgetShowNPoints()->OnSetPoint_();
904         } // if Type
905         if (bbGetInputType()==40)
906         {
907             bbGetInputWidgetShowNPoints()->OnErasePoint_();
908         } // if Type
909         if (bbGetInputType()==50)
910         {
911             bbGetInputWidgetShowNPoints()->OnDeleteAllPoints_();
912         } // if Type
913         if (bbGetInputType()==100)
914         {
915             bbGetInputWidgetShowNPoints()->OnInsertCollectionAfter_();
916         } // if Type
917         if (bbGetInputType()==110)
918         {
919             bbGetInputWidgetShowNPoints()->OnDeleteCollection_();
920         } // if Type
921         if (bbGetInputType()==120)
922         {
923             bbGetInputWidgetShowNPoints()->OnResetCollections_();
924         } // if Type
925         if (bbGetInputType()==200)   // Create patch surface
926         {
927             InitCreatePatch_Points();
928         } // if Type
929         if (bbGetInputType()==205)  // Create volume surface from points
930         {
931             InitCreateVolumeSurface_Points();
932         } // if Type
933         if (bbGetInputType()==210)
934         {
935             bbGetInputWidgetShowNPoints()->OnInvertLstPoints_();
936         } // if Type
937         if(bbGetInputType()==220)
938         {
939                 MovePatchCenter();
940         } // if Type
941         if(bbGetInputType()==230)
942         {
943                 MovePointInNormal();
944         } // if Type
945         if(bbGetInputType()==235)
946         {
947             ChangeCurrentPoint();
948         } // if Type
949         if(bbGetInputType()==240)
950         {
951                 JoinPoints();
952         } // if Type
953         if(bbGetInputType()==250)
954         {
955             SetMesh();
956         } // if Type
957         if(bbGetInputType()==300)
958         {
959                 CreateExpandedSurface();
960         } // if Type
961         if(bbGetInputType()==310)
962         {
963                 CreateWideExpandedSurface();
964         } // if Type
965         if(bbGetInputType()==320)
966         {
967                 ExpandSurfaceArea();
968         } // if Type
969         if(bbGetInputType()==330)
970         {
971                 WidenSurface();
972         } // if Type
973         if(bbGetInputType()==340)
974         {
975                 ExpandPatch();
976         } // if Type
977     } // if bbGetInputWidgetShowNPoints
978 }
979 //===== 
980 // 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)
981 //===== 
982 void ShowNPoints_Tools::bbUserSetDefaultValues()
983 {
984 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
985 //    Here we initialize the input 'In' to 0
986    bbSetInputType(0);
987    bbSetInputMesh(NULL);
988    bbSetInputWidgetShowNPoints(NULL);
989     
990     std::vector<double> spc;
991     spc.push_back(1);
992     spc.push_back(1);
993     spc.push_back(1);
994     bbSetInputSpacing(spc);
995 }
996 //===== 
997 // 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)
998 //===== 
999 void ShowNPoints_Tools::bbUserInitializeProcessing()
1000 {
1001 //  THE INITIALIZATION METHOD BODY :
1002 //    Here does nothing 
1003 //    but this is where you should allocate the internal/output pointers 
1004 //    if any
1005 }
1006 //===== 
1007 // 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)
1008 //===== 
1009 void ShowNPoints_Tools::bbUserFinalizeProcessing()
1010 {
1011 //  THE FINALIZATION METHOD BODY :
1012 //    Here does nothing 
1013 //    but this is where you should desallocate the internal/output pointers 
1014 //    if any
1015 }
1016
1017 } // EO namespace bbcreaMaracasVisu
1018
1019