# ----------------------------------
 # - BBTKGEditor v 1.5 BBG BlackBox Diagram file
-# - /Users/davila/Creatis/C23/creatools_source/creaMaracasVisu/bbtk/bbs/boxes/Mesh_tool_ApplySurface.bbg
+# - /home/garzon/Creatis/C23/creatools_source/creaMaracasVisu/bbtk/bbs/boxes/Mesh_tool_ApplySurface.bbg
 # ----------------------------------
 
 APP_START
 meshmanagermodel
 -150.374274:130.740991:-900.000000
 FIN_COMPLEX_PORT
-BOXES:70
+BOXES:75
 BOX
 creaMaracasVisu:ShowNPoints:showNpoints
 ISEXEC:FALSE
 PORT
 In0:"Tool - Inverse Normals"
 FIN_BOX
-CONNECTIONS:147
+BOX
+creaMaracasVisu:ShowNPoints_Tools:Box116
+ISEXEC:FALSE
+83.181891:-37.997829:-900.000000
+105.726891:-40.497829:-900.000000
+PORT
+BoxProcessMode:"Manual"
+PORT
+Params:" -1"
+PORT
+Type:"230"
+FIN_BOX
+BOX
+wx:CommandButton:Box117
+ISEXEC:FALSE
+100.839007:-14.508839:-900.000000
+123.224007:-17.008839:-900.000000
+PORT
+Label:"-"
+FIN_BOX
+BOX
+wx:LayoutLine:Box118
+ISEXEC:FALSE
+105.323248:-27.231844:-900.000000
+138.643248:-29.731844:-900.000000
+PORT
+Orientation:"H"
+FIN_BOX
+BOX
+wx:CommandButton:Box119
+ISEXEC:FALSE
+131.048147:-14.696561:-900.000000
+153.433147:-17.196561:-900.000000
+PORT
+Label:"+"
+FIN_BOX
+BOX
+creaMaracasVisu:ShowNPoints_Tools:Box120
+ISEXEC:FALSE
+118.818739:-37.869439:-900.000000
+141.363739:-40.369439:-900.000000
+PORT
+BoxProcessMode:"Manual"
+PORT
+Params:" 1"
+PORT
+Type:"230"
+FIN_BOX
+CONNECTIONS:158
 CONNECTION
 Box06:LstContourPointsX:Box04:LstX
 NumberOfControlPoints:0
 CONNECTION
 Box49:OutString:Box29:NbPoints
 NumberOfControlPoints:0
+CONNECTION
+showNpoints:WidgetShowNPoints:Box116:WidgetShowNPoints
+NumberOfControlPoints:0
+CONNECTION
+Box68:Spacing:Box116:Spacing
+NumberOfControlPoints:0
+CONNECTION
+Box117:BoxChange:Box116:BoxExecute
+NumberOfControlPoints:0
+CONNECTION
+Box117:Widget:Box118:Widget2
+NumberOfControlPoints:0
+CONNECTION
+Box118:Widget:Box50:Widget9
+NumberOfControlPoints:0
+CONNECTION
+Box119:Widget:Box118:Widget3
+NumberOfControlPoints:0
+CONNECTION
+Box119:BoxChange:Box120:BoxExecute
+NumberOfControlPoints:0
+CONNECTION
+showNpoints:WidgetShowNPoints:Box120:WidgetShowNPoints
+NumberOfControlPoints:0
+CONNECTION
+Box68:Spacing:Box120:Spacing
+NumberOfControlPoints:0
+CONNECTION
+Box105:Out:Box116:Mesh
+NumberOfControlPoints:0
+CONNECTION
+Box105:Out:Box120:Mesh
+NumberOfControlPoints:0
 APP_END
 
 # ----------------------------------
 # - BBTKGEditor v 1.5 BBS BlackBox Script (Complex Box)
-# - /Users/davila/Creatis/C23/creatools_source/creaMaracasVisu/bbtk/bbs/boxes/Mesh_tool_ApplySurface.bbs
+# - /home/garzon/Creatis/C23/creatools_source/creaMaracasVisu/bbtk/bbs/boxes/Mesh_tool_ApplySurface.bbs
 # ----------------------------------
 
 include std
 new std:AddStringToVector Box115
   set Box115.In0 "Tool - Inverse Normals"
 
+new creaMaracasVisu:ShowNPoints_Tools Box116
+  set Box116.BoxProcessMode "Manual"
+  set Box116.Params " -1"
+  set Box116.Type "230"
+
+new wx:CommandButton Box117
+  set Box117.Label "-"
+
+new wx:LayoutLine Box118
+  set Box118.Orientation "H"
+
+new wx:CommandButton Box119
+  set Box119.Label "+"
+
+new creaMaracasVisu:ShowNPoints_Tools Box120
+  set Box120.BoxProcessMode "Manual"
+  set Box120.Params " 1"
+  set Box120.Type "230"
+
 
 connect Box06.LstContourPointsX Box04.LstX
 connect showNpoints.lstIndexs Box06.LstIndexsIn
 connect Box115.Out Box57.In
 connect Box67.Out Box60.Mesh
 connect Box49.OutString Box29.NbPoints
+connect showNpoints.WidgetShowNPoints Box116.WidgetShowNPoints
+connect Box68.Spacing Box116.Spacing
+connect Box117.BoxChange Box116.BoxExecute
+connect Box117.Widget Box118.Widget2
+connect Box118.Widget Box50.Widget9
+connect Box119.Widget Box118.Widget3
+connect Box119.BoxChange Box120.BoxExecute
+connect showNpoints.WidgetShowNPoints Box120.WidgetShowNPoints
+connect Box68.Spacing Box120.Spacing
+connect Box105.Out Box116.Mesh
+connect Box105.Out Box120.Mesh
 
 # Complex input ports
 input point showNpoints.In " "
 
                double currentPoint[3], dV[3], mdfdPoint[3];
                for(int group = 0; group < 3; group++){
                        if(group == 1){
-                               cout << "collection: " << group << " i: " << "0" << endl;
                                wsp->GetCollectionPoint(group, 0, currentPoint);
                                dV[0] = currentPoint[0] - centroid[0];
                                dV[1] = currentPoint[1] - centroid[1];
                                vtkMath::Add(currentPoint, dV, mdfdPoint);
                                modPoint.insert(modPoint.begin(), std::begin(mdfdPoint), std::end(mdfdPoint));
                                wsp->SetCollectionPoint(group, 0, modPoint);
-                               cout << "collection: " << group << " i: " << "2" << endl;
                                wsp->GetCollectionPoint(group, 2, currentPoint);
                                dV[0] = currentPoint[0] - centroid[0];
                                dV[1] = currentPoint[1] - centroid[1];
                                        vtkMath::MultiplyScalar(dV, direction);
                                        vtkMath::Add(currentPoint, dV, mdfdPoint);
                                        modPoint.insert(modPoint.begin(), std::begin(mdfdPoint), std::end(mdfdPoint));
-                                       cout << "collection: " << group << " i: " << i << endl;
                                        wsp->SetCollectionPoint(group, i, modPoint);
                                }
                        }
         wsp->SetOutputBox();
         wsp->UndoRedo_SaveCollection();
 }
+
+void ShowNPoints_Tools::MovePointInNormal()
+{
+       //move the currently selected control point in the direction or opposite of the normal vector of the closest point in the input mesh
+
+       WidgetShowNPoints* wsp = bbGetInputWidgetShowNPoints();
+       wsp->StopAutoAddPoints();
+    wsp->StopTrackPoint();
+    if((wsp->GetLstModelShowNPointsSize()>=2)  //Check condition
+               && (wsp->GetModelShowNPoints()->GetLstPointsSize()>0)
+               && (bbGetInputParams().size() == 1)
+               && (bbGetInputMesh() != NULL)
+               && (bbGetInputSpacing().size() == 3))
+       {
+               if(bbGetInputParams()[0] != -1 && bbGetInputParams()[0] != 1){
+                       printf("PG ShowNPoints_Tools::MovePointInNormal No direction provided, must be 1 or -1");
+                       return;
+               }
+               double spc[3];
+        double x,y,z;
+        double p[3], normal[3];;        // point to modify and normal
+        std::vector<double> pN(3);    // new point
+
+        spc[0]                                                         = bbGetInputSpacing()[0];
+        spc[1]                                                         = bbGetInputSpacing()[1];
+        spc[2]                                                         = bbGetInputSpacing()[2];
+        
+        vtkPoints               *points         = bbGetInputMesh()->GetPoints();
+        vtkStaticPointLocator   *pointLocator   = vtkStaticPointLocator::New();
+        pointLocator->SetDataSet( bbGetInputMesh() );
+        pointLocator->BuildLocator();
+        
+        int idControlPoint = wsp->GetModelShowNPoints()->GetNearestPoint();
+        if(idControlPoint >= 0){
+                   wsp->GetModelShowNPoints()->GetIdPoint(idControlPoint, &x, &y, &z);
+                   p[0] = x;
+                   p[1] = y;
+                   p[2] = z;
+                   int idMeshPoint = pointLocator->FindClosestPoint(p);
+                   bbGetInputMesh()->GetPointData()->GetNormals()->GetTuple(idMeshPoint, normal);
+                       int direction = bbGetInputParams()[0];
+                   pN[0] = p[0] + direction*normal[0];
+                   pN[1] = p[1] + direction*normal[1];
+                   pN[2] = p[2] + direction*normal[2];
+                   
+                   wsp->GetModelShowNPoints()->SetPointById(idControlPoint, pN);
+                   wsp->GetViewShowNPoints()->RefreshPoint(idControlPoint);
+                   wsp->SetOutputBox();
+               wsp->UndoRedo_SaveCollection();
+        }
+        
+       }
+}
 //=====
 // 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)
 //===== 
         {
                MovePatchCenter();
         } // if Type
+        if(bbGetInputType()==230)
+        {
+               MovePointInNormal();
+        } // if Type
         if(bbGetInputType()==300)
         {
                CreateExpandedSurface();
 
     void ExpandSurfaceArea();
     void WidenSurface();
     void ExpandPatch();
+    void MovePointInNormal();
 
 //===== 
 // 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)
 
 ModelShowNPoints::ModelShowNPoints()
 {
        firsttime=true;
+       currentPoint = -1;
 }
 //----------------------------------------------------------------------
 
 void ModelShowNPoints::SetReferencePoint(std::vector<double> ppoint)
 {
        mReferencePoint = ppoint;
+       if(lstPointsX.size() > 0){
+               int idPoint = IdInsidePoint();
+               if(idPoint == -1 && currentPoint >= 0 && currentPoint < lstPointsX.size()){     
+                       currentPoint = currentPoint;
+               }
+               else{
+                       currentPoint = idPoint;
+               }
+       }
 }
 
 //------------------------------------------------------------------------
     } // for i
 }
 
-
+//----------------------------------------------------------------------
+int ModelShowNPoints::GetCurrentPoint()
+{
+       return currentPoint;
+}
 
          void                                                  SetFirstTime(bool value);
          bool                                                  GetFirstTime();
       void                             InversLstPoints();
+      int                                                      GetCurrentPoint();
       
        private:
          std::vector<double>           lstPointsX;
          double                                mradio;
          vtkImageData                          *mimage;
          bool                                                  firsttime;
+         int                                                   currentPoint;
 
   };