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