]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkMeshDeformation.cxx
47bf806d454819ddf306ee3a5264ffe72a0d9fcb
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkMeshDeformation.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 "bbcreaVtkMeshDeformation.h"
5 #include "bbcreaVtkPackage.h"
6 namespace bbcreaVtk
7 {
8
9 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,MeshDeformation)
10 BBTK_BLACK_BOX_IMPLEMENTATION(MeshDeformation,bbtk::AtomicBlackBox);
11 //===== 
12 // 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)
13 //===== 
14 void MeshDeformation::Process()
15 {
16
17 // THE MAIN PROCESSING METHOD BODY
18 //   Here we simply set the input 'In' value to the output 'Out'
19 //   And print out the output value
20 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
21 //    void bbSet{Input|Output}NAME(const TYPE&)
22 //    const TYPE& bbGet{Input|Output}NAME() const 
23 //    Where :
24 //    * NAME is the name of the input/output
25 //      (the one provided in the attribute 'name' of the tag 'input')
26 //    * TYPE is the C++ type of the input/output
27 //      (the one provided in the attribute 'type' of the tag 'input')
28 //    bbSetOutputOut( bbGetInputIn() );
29 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
30
31     std::vector<double> lstCenter = bbGetInputCenter();
32
33     double  s   = bbGetInputS();
34     bool    ok  = false;
35     if (bbGetInputTypeIn()==0) // direction
36     {
37         if (bbGetInputDirection().size()==3)
38         {
39             ok = !( (bbGetInputDirection()[0]==0) && (bbGetInputDirection()[1]==0) && (bbGetInputDirection()[2]==0) );
40         }
41     } // if TypeIn 0
42     
43     if (bbGetInputTypeIn()==1)  // center
44     {
45         ok = ( lstCenter.size()==3 );
46     } // if TypeIn 1
47
48     if ( (bbGetInputIn()!=NULL) && (ok==true) && (bbGetInputEdgeId()>=0) && (bbGetInputActive()==true) )
49     {
50         vtkPoints* points=bbGetInputIn()->GetPoints();
51         long    i,size=points->GetNumberOfPoints();
52         double  p[3];  // point
53         double  pb[3]; // point base
54         double  np[3]; // new point
55         double  sx,sy,sz;
56         sx = s*2;
57         sy = sx;
58         sz = sy;
59
60         double displcement_x = 0;
61         double displcement_y = 0;
62         double displcement_z = 0;
63         
64         if (bbGetInputTypeIn()==0) // Direction
65         {
66             displcement_x = bbGetInputDirection()[0];
67             displcement_y = bbGetInputDirection()[1];
68             displcement_z = bbGetInputDirection()[2];
69         } // if TypeIn 0 Direction
70         if (bbGetInputTypeIn()==1) // Center
71         {
72             if (EdgeIdBack==bbGetInputEdgeId() )
73             {
74                 displcement_x = (lstCenter[0]-backLstCenter[0])/1.0;
75                 displcement_y = (lstCenter[1]-backLstCenter[1])/1.0;
76                 displcement_z = (lstCenter[2]-backLstCenter[2])/1.0;
77             } // if EdgeIdBack!=bbGetInputEdgeId()
78             backLstCenter[0] = lstCenter[0];
79             backLstCenter[1] = lstCenter[1];
80             backLstCenter[2] = lstCenter[2];
81         } // if TypeIn 1 Center
82         points->GetPoint( bbGetInputEdgeId() , pb );
83         if (EdgeIdBack!=bbGetInputEdgeId() )
84         {
85             EdgeIdBack = bbGetInputEdgeId();
86             displcement_x = 0;
87             displcement_y = 0;
88             displcement_z = 0;
89             voiIdPoints.clear();
90             for ( i=0 ; i<size ; i++)
91             {
92                 points->GetPoint(i,p);
93                 if ( (p[0]>(pb[0]-sx)) && (p[0]<(pb[0]+sx)) &&
94                      (p[1]>(pb[1]-sy)) && (p[1]<(pb[1]+sy)) &&
95                      (p[2]>(pb[2]-sz)) && (p[2]<(pb[2]+sz)) )
96                 {
97                     voiIdPoints.push_back( i );
98                 } // if
99             } // for i
100         } // back
101
102         if ( !((displcement_x==0) &&(displcement_y==0) && (displcement_z==0)) )
103         {
104             size=voiIdPoints.size();
105             for (i=0;i<size;i++)
106             {
107                 points->GetPoint( voiIdPoints[i] , p );
108                 double x = p[0] - pb[0]; //distance between a point and seed point
109                 double y = p[1] - pb[1];
110                 double z = p[2] - pb[2];
111                 double distance = std::sqrt(std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2));
112
113     //        https://en.wikipedia.org/wiki/Generalized_normal_distribution
114     // with Gama function
115                 
116                 double weight = exp(-distance * 1. / s);
117                 if (weight > 0.0001)
118                 {
119                     np[0] = p[0] + weight*displcement_x;
120                     np[1] = p[1] + weight*displcement_y;
121                     np[2] = p[2] + weight*displcement_z;
122                     points->SetPoint( voiIdPoints[i] , np );
123                 } // if
124             } // for
125             points->Modified();
126             bbGetInputIn()->Modified();
127         } // if distance_x y z  != 0
128     } // In != NULL    ok    active
129 }
130 //===== 
131 // 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)
132 //===== 
133 void MeshDeformation::bbUserSetDefaultValues()
134 {
135
136 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
137 //    Here we initialize the input 'In' to 0
138     bbSetInputActive(true);
139     bbSetInputTypeIn(0);
140     bbSetInputIn(NULL);
141     std::vector<double> direction;
142     direction.push_back(1);
143     direction.push_back(0);
144     direction.push_back(0);
145     bbSetInputDirection(direction);
146     EdgeIdBack=-1;
147     bbSetInputEdgeId(EdgeIdBack);
148     bbSetInputS(10);
149     
150     backLstCenter.push_back(0);
151     backLstCenter.push_back(0);
152     backLstCenter.push_back(0);
153 }
154 //===== 
155 // 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)
156 //===== 
157 void MeshDeformation::bbUserInitializeProcessing()
158 {
159
160 //  THE INITIALIZATION METHOD BODY :
161 //    Here does nothing 
162 //    but this is where you should allocate the internal/output pointers 
163 //    if any 
164
165   
166 }
167 //===== 
168 // 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)
169 //===== 
170 void MeshDeformation::bbUserFinalizeProcessing()
171 {
172
173 //  THE FINALIZATION METHOD BODY :
174 //    Here does nothing 
175 //    but this is where you should desallocate the internal/output pointers 
176 //    if any
177   
178 }
179 }
180 // EO namespace bbcreaVtk
181
182