-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransferFunction.h
More file actions
196 lines (154 loc) · 6.65 KB
/
Copy pathTransferFunction.h
File metadata and controls
196 lines (154 loc) · 6.65 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/******************************************************************************
* 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_TRANSFERFUNCTION_H
#define SOFA_IMAGE_TRANSFERFUNCTION_H
#include <image/config.h>
#include "ImageTypes.h"
#include <sofa/core/DataEngine.h>
#include <sofa/core/objectmodel/BaseObject.h>
#include <sofa/defaulttype/Vec.h>
#include <sofa/helper/rmath.h>
#include <sofa/helper/OptionsGroup.h>
#include <map>
#define LINEAR 0
namespace sofa
{
namespace component
{
namespace engine
{
/**
* This class transforms pixel intensities
*/
/// Default implementation does not compile
template <class InImageType, class OutImageType>
struct TransferFunctionSpecialization
{
};
/// forward declaration
template <class InImageType, class OutImageType> class TransferFunction;
/// Specialization for regular Image
template <class Ti, class To>
struct TransferFunctionSpecialization<defaulttype::Image<Ti>,defaulttype::Image<To>>
{
typedef TransferFunction<defaulttype::Image<Ti>,defaulttype::Image<To>> TransferFunctionT;
static void update(TransferFunctionT& This)
{
typename TransferFunctionT::raParam p(This.param);
typename TransferFunctionT::raImagei in(This.inputImage);
if(in->isEmpty()) return;
const cimg_library::CImgList<Ti>& inimg = in->getCImgList();
typename TransferFunctionT::waImageo out(This.outputImage);
typename TransferFunctionT::imCoord dim=in->getDimensions();
out->setDimensions(dim);
cimg_library::CImgList<To>& img = out->getCImgList();
switch(This.filter.getValue().getSelectedId())
{
case LINEAR:
{
typename TransferFunctionT::iomap mp; for(unsigned int i=0; i<p.size(); i+=2) mp[(Ti)p[i]]=(To)p[i+1];
cimglist_for(inimg,l) cimg_forXYZC(inimg(l),x,y,z,c) img(l)(x,y,z,c)=This.Linear_TransferFunction(inimg(l)(x,y,z,c),mp);
}
break;
default:
img.assign(in->getCImgList()); // copy
break;
}
}
};
/**
* \todo adjust type of ParamTypes according to InImageTypes and OutImageTypes
*/
template <class _InImageTypes,class _OutImageTypes>
class TransferFunction : public core::DataEngine
{
friend struct TransferFunctionSpecialization<_InImageTypes,_OutImageTypes>;
public:
typedef core::DataEngine Inherited;
SOFA_CLASS(SOFA_TEMPLATE2(TransferFunction,_InImageTypes,_OutImageTypes),Inherited);
typedef _InImageTypes InImageTypes;
typedef typename InImageTypes::T Ti;
typedef typename InImageTypes::imCoord imCoord;
typedef helper::ReadAccessor<Data< InImageTypes > > raImagei;
typedef _OutImageTypes OutImageTypes;
typedef typename OutImageTypes::T To;
typedef helper::WriteOnlyAccessor<Data< OutImageTypes > > waImageo;
typedef std::map<Ti,To> iomap;
typedef typename iomap::const_iterator iomapit;
typedef helper::vector<double> ParamTypes;
typedef helper::WriteOnlyAccessor<Data< ParamTypes > > waParam;
typedef helper::ReadAccessor<Data< ParamTypes > > raParam;
Data<helper::OptionsGroup> filter; ///< Filter
Data< ParamTypes > param; ///< Parameters
Data< InImageTypes > inputImage;
Data< OutImageTypes > outputImage;
virtual std::string getTemplateName() const override { return templateName(this); }
static std::string templateName(const TransferFunction<InImageTypes,OutImageTypes>* = NULL) { return InImageTypes::Name()+std::string(",")+OutImageTypes::Name(); }
TransferFunction() : Inherited()
, filter ( initData ( &filter,"filter","Filter" ) )
, param ( initData ( ¶m,"param","Parameters" ) )
, inputImage(initData(&inputImage,InImageTypes(),"inputImage",""))
, outputImage(initData(&outputImage,OutImageTypes(),"outputImage",""))
{
inputImage.setReadOnly(true);
outputImage.setReadOnly(true);
helper::OptionsGroup filterOptions(1 ,"0 - Piecewise Linear ( i1, o1, i2, o2 ...)"
);
filterOptions.setSelectedItem(LINEAR);
filter.setValue(filterOptions);
}
virtual ~TransferFunction() {}
virtual void init() override
{
addInput(&inputImage);
addOutput(&outputImage);
setDirtyValue();
}
virtual void reinit() override { update(); }
protected:
virtual void update() override
{
TransferFunctionSpecialization<InImageTypes,OutImageTypes>::update( *this );
cleanDirty();
}
inline To Linear_TransferFunction(const Ti& vi, const iomap & mp) const
{
To vo=mp.begin()->second;
iomapit mit;
for (iomapit it=mp.begin(); it!=mp.end(); it++)
{
if (it->first>vi && it!=mp.begin())
{
double alpha=(((double)it->first-(double)vi)/((double)it->first-(double)mit->first));
double v= alpha*(double)mit->second + (1.-alpha)*(double)it->second;
return (To)v;
}
else vo=it->second;
mit=it;
}
return vo;
}
};
} // namespace engine
} // namespace component
} // namespace sofa
#endif // SOFA_IMAGE_TRANSFERFUNCTION_H