]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkTransform.cxx
#3518 Python descrition
[bbtk.git] / packages / vtk / src / bbvtkTransform.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 #include "bbvtkTransform.h"
29 #include "bbvtkPackage.h"
30
31 #include "vtkMath.h"
32 #include "vtkMatrix4x4.h"
33
34 namespace bbvtk
35 {
36   BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,Transform);
37   BBTK_BLACK_BOX_IMPLEMENTATION(Transform,bbtk::AtomicBlackBox);
38
39         // --------------------------------------------------------------       
40
41         void Transform::bbUserSetDefaultValues()
42         {
43         bbSetInputIn(NULL);
44                 bbSetInputInverse(false);
45
46                 vecScale.push_back(1);  // scale x
47                 vecScale.push_back(1);  // scale y
48                 vecScale.push_back(1);  // scale z
49                 bbSetInputScale(vecScale);
50
51                 vecRotateWXYZ.push_back(0);  //angle 
52                 vecRotateWXYZ.push_back(1);  //vx
53                 vecRotateWXYZ.push_back(0);  //vy
54                 vecRotateWXYZ.push_back(0);  //vz
55                 bbSetInputRotateWXYZ(vecRotateWXYZ);
56
57                 vecTranslate.push_back(0);  //tx
58                 vecTranslate.push_back(0);  //ty
59                 vecTranslate.push_back(0);  //tz
60                 bbSetInputTranslate(vecTranslate);
61
62                 vecSpacing.push_back(1);  //spacing x
63                 vecSpacing.push_back(1);  //spacing y
64                 vecSpacing.push_back(1);  //spacing z
65                 bbSetInputSpacing(vecSpacing);
66         
67                 result = NULL;
68         }
69
70         // --------------------------------------------------------------       
71         void Transform::bbUserInitializeProcessing()
72         {
73                 bbUserFinalizeProcessing();
74                 result = vtkTransform::New();
75                 result->Update();
76                 resultInverse = vtkTransform::New();
77                 resultInverse->Update();
78         }
79
80         // --------------------------------------------------------------       
81         void Transform::bbUserFinalizeProcessing()
82         {
83                 if (result!=NULL)
84                 {
85                         result->Delete();
86                         result=NULL;
87                 }
88         }
89
90         // --------------------------------------------------------------       
91   void Transform::Process()
92   {
93           bbUserInitializeProcessing();
94           if (bbGetInputIn()!=NULL)
95       {
96                   result->Concatenate( bbGetInputIn()->GetMatrix() );
97       } // if In
98           if ((bbGetInputTranslate().size()>=3) && (bbGetInputSpacing().size()>=3))
99       {
100                   double tx = bbGetInputTranslate()[0] * bbGetInputSpacing()[0];
101                   double ty = bbGetInputTranslate()[1] * bbGetInputSpacing()[1];
102                   double tz = bbGetInputTranslate()[2] * bbGetInputSpacing()[2];
103                   result->Translate(tx,ty,tz);
104       }  // if translate spacing  size >=3
105           if (bbGetInputScale().size()>=3)
106       {
107                   result->Scale(bbGetInputScale()[0], bbGetInputScale()[1], bbGetInputScale()[2]);
108       }  // if scale size >=3
109           if (bbGetInputRotateWXYZ().size()>=4)
110       {
111                   result->RotateWXYZ(bbGetInputRotateWXYZ()[0],bbGetInputRotateWXYZ()[1], bbGetInputRotateWXYZ()[2], bbGetInputRotateWXYZ()[3]);
112       } // if rotation size >=4
113  
114       if (bbGetInputRotateToNormal().size()>=3)
115       {
116           double v1[3],v2[3],v3[3];
117           v1[0]             = 0;
118           v1[1]             = 0;
119           v1[2]             = 1;
120           v2[0]             = bbGetInputRotateToNormal()[0];
121           v2[1]             = bbGetInputRotateToNormal()[1];
122           v2[2]             = bbGetInputRotateToNormal()[2];
123           
124           double mag= sqrt( v2[0]*v2[0] + v2[1]*v2[1]+ v2[2]*v2[2] ) ;
125                   v2[0] = v2[0] / mag;
126                   v2[1] = v2[1] / mag;
127                   v2[2] = v2[2] / mag;
128           vtkMath *vtkmath  = vtkMath::New();
129           double ang        = acos ( vtkmath->Dot(v1,v2)  );
130           ang               = vtkmath->DegreesFromRadians( ang );
131           vtkmath->Cross( v1,v2,v3 );
132           vtkmath->Delete();
133           result->RotateWXYZ( ang  , v3[0], v3[1], v3[2] );
134       } // if rotation size >=4
135
136       if (bbGetInputManualMatrixIn().size()==16)
137       {
138           std::vector<double> manaulMatrixIn = bbGetInputManualMatrixIn();
139           vtkMatrix4x4* m = result->GetMatrix();
140           int i,j,ii=0;
141           for (j=0;j<4;j++)
142           {
143               for (i=0;i<4;i++)
144               {
145                   m->SetElement(i,j,manaulMatrixIn[ii]);
146                   ii++;
147               }// for i
148           } // for j
149           m->Modified();
150           result->Update();
151       } // ManualMatrix
152       
153       vtkTransform * finalTransform;
154           if (bbGetInputInverse()==false)
155           {
156         finalTransform = result;
157           } else {
158                 vtkMatrix4x4 *matrix;
159                 matrix=vtkMatrix4x4::New();
160                 result->GetInverse(matrix);
161                 resultInverse->SetMatrix( matrix );
162                 finalTransform = resultInverse;
163           }  // if Inverse
164       
165       bbSetOutputOut(finalTransform);
166
167       std::vector<double> manualMatrixOut;
168       vtkMatrix4x4* m = finalTransform->GetMatrix();
169       int i,j,ii=0;
170       for (j=0;j<4;j++)
171       {
172           for (i=0;i<4;i++)
173           {
174               manualMatrixOut.push_back( m->GetElement(i,j) );
175           }// for i
176       } // for j
177       bbSetOutputManualMatrixOut( manualMatrixOut );
178   }
179   
180 }// EO namespace bbvtk