2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------ */
28 #include "bbvtkTransform.h"
29 #include "bbvtkPackage.h"
32 #include "vtkMatrix4x4.h"
36 BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,Transform);
37 BBTK_BLACK_BOX_IMPLEMENTATION(Transform,bbtk::AtomicBlackBox);
39 // --------------------------------------------------------------
41 void Transform::bbUserSetDefaultValues()
44 bbSetInputInverse(false);
46 vecScale.push_back(1); // scale x
47 vecScale.push_back(1); // scale y
48 vecScale.push_back(1); // scale z
49 bbSetInputScale(vecScale);
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);
57 vecTranslate.push_back(0); //tx
58 vecTranslate.push_back(0); //ty
59 vecTranslate.push_back(0); //tz
60 bbSetInputTranslate(vecTranslate);
62 vecSpacing.push_back(1); //spacing x
63 vecSpacing.push_back(1); //spacing y
64 vecSpacing.push_back(1); //spacing z
65 bbSetInputSpacing(vecSpacing);
70 // --------------------------------------------------------------
71 void Transform::bbUserInitializeProcessing()
73 bbUserFinalizeProcessing();
74 result = vtkTransform::New();
76 resultInverse = vtkTransform::New();
77 resultInverse->Update();
80 // --------------------------------------------------------------
81 void Transform::bbUserFinalizeProcessing()
90 // --------------------------------------------------------------
91 void Transform::Process()
93 bbUserInitializeProcessing();
94 if (bbGetInputIn()!=NULL)
96 result->Concatenate( bbGetInputIn()->GetMatrix() );
98 if ((bbGetInputTranslate().size()>=3) && (bbGetInputSpacing().size()>=3))
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)
107 result->Scale(bbGetInputScale()[0], bbGetInputScale()[1], bbGetInputScale()[2]);
108 } // if scale size >=3
109 if (bbGetInputRotateWXYZ().size()>=4)
111 result->RotateWXYZ(bbGetInputRotateWXYZ()[0],bbGetInputRotateWXYZ()[1], bbGetInputRotateWXYZ()[2], bbGetInputRotateWXYZ()[3]);
112 } // if rotation size >=4
114 if (bbGetInputRotateToNormal().size()>=3)
116 double v1[3],v2[3],v3[3];
120 v2[0] = bbGetInputRotateToNormal()[0];
121 v2[1] = bbGetInputRotateToNormal()[1];
122 v2[2] = bbGetInputRotateToNormal()[2];
124 double mag= sqrt( v2[0]*v2[0] + v2[1]*v2[1]+ v2[2]*v2[2] ) ;
128 vtkMath *vtkmath = vtkMath::New();
129 double ang = acos ( vtkmath->Dot(v1,v2) );
130 ang = vtkmath->DegreesFromRadians( ang );
131 vtkmath->Cross( v1,v2,v3 );
133 result->RotateWXYZ( ang , v3[0], v3[1], v3[2] );
134 } // if rotation size >=4
136 if (bbGetInputManualMatrixIn().size()==16)
138 std::vector<double> manaulMatrixIn = bbGetInputManualMatrixIn();
139 vtkMatrix4x4* m = result->GetMatrix();
145 m->SetElement(i,j,manaulMatrixIn[ii]);
153 vtkTransform * finalTransform;
154 if (bbGetInputInverse()==false)
156 finalTransform = result;
158 vtkMatrix4x4 *matrix;
159 matrix=vtkMatrix4x4::New();
160 result->GetInverse(matrix);
161 resultInverse->SetMatrix( matrix );
162 finalTransform = resultInverse;
165 bbSetOutputOut(finalTransform);
167 std::vector<double> manualMatrixOut;
168 vtkMatrix4x4* m = finalTransform->GetMatrix();
174 manualMatrixOut.push_back( m->GetElement(i,j) );
177 bbSetOutputManualMatrixOut( manualMatrixOut );
180 }// EO namespace bbvtk