-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageTransformEngine.h
More file actions
140 lines (110 loc) · 5.07 KB
/
Copy pathImageTransformEngine.h
File metadata and controls
140 lines (110 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, development version *
* (c) 2006-2018 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_IMAGE_ImageTransformEngine_H
#define SOFA_IMAGE_ImageTransformEngine_H
#include <image/config.h>
#include "ImageTypes.h"
#include <sofa/core/DataEngine.h>
#include <sofa/core/objectmodel/BaseObject.h>
#include <sofa/helper/Quater.h>
namespace sofa
{
namespace component
{
namespace engine
{
/**
* Apply a transform to the data 'transform'
* in future: could be templated on ImageTransform type
* @author Benjamin GILLES
*/
class SOFA_IMAGE_API ImageTransformEngine : public core::DataEngine
{
public:
typedef core::DataEngine Inherited;
SOFA_CLASS(ImageTransformEngine,Inherited);
typedef SReal Real;
typedef defaulttype::ImageLPTransform<Real> TransformType;
typedef TransformType::Coord Coord;
typedef helper::WriteOnlyAccessor<Data< TransformType > > waTransform;
typedef helper::ReadAccessor<Data< TransformType > > raTransform;
Data< TransformType > inputTransform;
Data< TransformType > outputTransform;
Data<defaulttype::Vector3> translation; ///< translation
Data<defaulttype::Vector3> rotation; ///< rotation
Data<Real> scale; ///< scale
Data<bool> inverse; ///< true to apply inverse transformation
virtual std::string getTemplateName() const override { return templateName(this); }
static std::string templateName(const ImageTransformEngine* = NULL) { return std::string(); }
ImageTransformEngine() : Inherited()
, inputTransform(initData(&inputTransform,TransformType(),"inputTransform",""))
, outputTransform(initData(&outputTransform,TransformType(),"outputTransform",""))
, translation(initData(&translation, defaulttype::Vector3(0,0,0),"translation", "translation vector ") )
, rotation(initData(&rotation, defaulttype::Vector3(0,0,0), "rotation", "rotation vector ") )
, scale(initData(&scale, (Real)1.0,"scale", "scale factor") )
, inverse(initData(&inverse, false, "inverse", "true to apply inverse transformation"))
{
}
virtual ~ImageTransformEngine() {}
virtual void init() override
{
addInput(&translation);
addInput(&rotation);
addInput(&scale);
addInput(&inverse);
addInput(&inputTransform);
addOutput(&outputTransform);
setDirtyValue();
}
virtual void reinit() override { update(); }
protected:
virtual void update() override
{
raTransform inT(this->inputTransform);
waTransform outT(this->outputTransform);
Real s;
helper::Quater<Real> r;
Coord t;
if(this->inverse.getValue())
{
s=(Real)1./this->scale.getValue();
r=helper::Quater< Real >::createQuaterFromEuler(this->rotation.getValue()* (Real)M_PI / (Real)180.0 ).inverse();
t=-r.rotate(this->translation.getValue())*s;
}
else
{
s=this->scale.getValue();
t=this->translation.getValue();
r= helper::Quater< Real >::createQuaterFromEuler(this->rotation.getValue()* (Real)M_PI / (Real)180.0 );
}
outT->getScale() = inT->getScale() * s;
outT->getTranslation() = r.rotate(inT->getTranslation())*s + t;
helper::Quater<Real> q = r*inT->qrotation;
outT->getRotation()=q.toEulerVector() * (Real)180.0 / (Real)M_PI ;
outT->update(); // update internal data
cleanDirty();
}
};
} // namespace engine
} // namespace component
} // namespace sofa
#endif // SOFA_IMAGE_ImageTransformEngine_H