]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkTransform.cxx
#3486 box vtkTransform nomalize the input vector
[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 #include "vtkMath.h"
31 namespace bbvtk
32 {
33   BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,Transform);
34   BBTK_BLACK_BOX_IMPLEMENTATION(Transform,bbtk::AtomicBlackBox);
35
36         // --------------------------------------------------------------       
37
38         void Transform::bbUserSetDefaultValues()
39         {
40                 bbSetInputIn(NULL);
41
42                 vecScale.push_back(1);  // scale x
43                 vecScale.push_back(1);  // scale y
44                 vecScale.push_back(1);  // scale z
45                 bbSetInputScale(vecScale);
46
47                 vecRotateWXYZ.push_back(0);  //angle 
48                 vecRotateWXYZ.push_back(1);  //vx
49                 vecRotateWXYZ.push_back(0);  //vy
50                 vecRotateWXYZ.push_back(0);  //vz
51                 bbSetInputRotateWXYZ(vecRotateWXYZ);
52
53                 vecTranslate.push_back(0);  //tx
54                 vecTranslate.push_back(0);  //ty
55                 vecTranslate.push_back(0);  //tz
56                 bbSetInputTranslate(vecTranslate);
57
58                 vecSpacing.push_back(1);  //spacing x
59                 vecSpacing.push_back(1);  //spacing y
60                 vecSpacing.push_back(1);  //spacing z
61                 bbSetInputSpacing(vecSpacing);
62         
63                 result = NULL;
64         }
65
66         // --------------------------------------------------------------       
67         void Transform::bbUserInitializeProcessing()
68         {
69                 bbUserFinalizeProcessing();
70                 result = vtkTransform::New();
71                 result->Update();
72                 resultInverse = vtkTransform::New();
73                 resultInverse->Update();
74         }
75
76         // --------------------------------------------------------------       
77         void Transform::bbUserFinalizeProcessing()
78         {
79                 if (result!=NULL)
80                 {
81                         result->Delete();
82                         result=NULL;
83                 }
84         }
85
86
87         // --------------------------------------------------------------       
88   void Transform::Process()
89   {
90           bbUserInitializeProcessing();
91           if (bbGetInputIn()!=NULL)
92       {
93                   result->Concatenate( bbGetInputIn()->GetMatrix() );
94       } // if In
95           if ((bbGetInputTranslate().size()>=3) && (bbGetInputSpacing().size()>=3))
96       {
97                   double tx = bbGetInputTranslate()[0] * bbGetInputSpacing()[0];
98                   double ty = bbGetInputTranslate()[1] * bbGetInputSpacing()[1];
99                   double tz = bbGetInputTranslate()[2] * bbGetInputSpacing()[2];
100                   result->Translate(tx,ty,tz);
101       }  // if translate spacing  size >=3
102           if (bbGetInputScale().size()>=3)
103       {
104                   result->Scale(bbGetInputScale()[0], bbGetInputScale()[1], bbGetInputScale()[2]);
105       }  // if scale size >=3
106           if (bbGetInputRotateWXYZ().size()>=4)
107       {
108                   result->RotateWXYZ(bbGetInputRotateWXYZ()[0],bbGetInputRotateWXYZ()[1], bbGetInputRotateWXYZ()[2], bbGetInputRotateWXYZ()[3]);
109       } // if rotation size >=4
110  
111       if (bbGetInputRotateToNormal().size()>=3)
112       {
113           double v1[3],v2[3],v3[3];
114           v1[0]             = 0;
115           v1[1]             = 0;
116           v1[2]             = 1;
117           v2[0]             = bbGetInputRotateToNormal()[0];
118           v2[1]             = bbGetInputRotateToNormal()[1];
119           v2[2]             = bbGetInputRotateToNormal()[2];
120           
121           double mag= sqrt( v2[0]*v2[0] + v2[1]*v2[1]+ v2[2]*v2[2] ) ;
122                   v2[0] = v2[0] / mag;
123                   v2[1] = v2[1] / mag;
124                   v2[2] = v2[2] / mag;
125           vtkMath *vtkmath  = vtkMath::New();
126           double ang        = acos ( vtkmath->Dot(v1,v2)  );
127           ang               = vtkmath->DegreesFromRadians( ang );
128           vtkmath->Cross( v1,v2,v3 );
129           vtkmath->Delete();
130           result->RotateWXYZ( ang  , v3[0], v3[1], v3[2] );
131       } // if rotation size >=4
132
133           if (bbGetInputInverse()==false)
134           {
135                 bbSetOutputOut(result);
136           } else {
137                 vtkMatrix4x4 *matrix;
138                 matrix=vtkMatrix4x4::New();
139                 result->GetInverse(matrix);
140                 resultInverse->SetMatrix( matrix );
141                 bbSetOutputOut(resultInverse);
142           }  // if Inverse
143   }
144   
145 }// EO namespace bbvtk