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