7 #include "fwData/TransferFunction.hpp" 9 #include <fwData/Exception.hpp> 10 #include <fwData/registry/macros.hpp> 12 #include <fwCom/Signal.hxx> 14 #include <fwTools/Type.hpp> 40 void TransferFunction::initTF()
47 m_interpolationMode = TransferFunction::LINEAR;
55 fwData::TransferFunction::sptr TransferFunction::createDefaultTF()
57 TransferFunction::sptr tf = TransferFunction::New();
59 tf->setName(TransferFunction::s_DEFAULT_TF_NAME);
61 tf->addTFColor(1.0,
TFColor(1.0, 1.0, 1.0, 1.0));
62 tf->setIsClamped(
false);
63 tf->setWindow( 500. );
78 TFValueVectorType values;
79 values.reserve(m_tfData.size());
80 std::transform( m_tfData.begin(), m_tfData.end(),
81 std::back_inserter(values),
82 std::bind(&TFDataType::value_type::first, std::placeholders::_1) );
90 TFValueVectorType values;
91 values.reserve(m_tfData.size());
95 const double scale = m_window / (minMax.second - minMax.first);
97 for(
const TFDataType::value_type& data : m_tfData)
99 const double value = (data.first - minMax.first) * scale + windowMinMax.first;
100 values.push_back(value);
108 TransferFunction::TFValuePairType
111 SLM_ASSERT(
"It must have at least one value.", m_tfData.size() >= 1);
112 TFValuePairType minMax;
113 minMax.first = m_tfData.begin()->first;
114 minMax.second = (m_tfData.rbegin())->first;
120 TransferFunction::TFValuePairType
123 TFValuePairType minMax;
124 const double halfWindow = m_window/2.;
125 minMax.first = m_level - halfWindow;
126 minMax.second = m_level + halfWindow;
134 m_window = minMax.second - minMax.first;
135 const double halfWindow = m_window/2.;
136 m_level = halfWindow + minMax.first;
143 OSLM_ASSERT(
"It must have at least one value.", m_tfData.size() >= 1);
144 const std::pair<double, double> minMax = ::fwTools::Type::s_DOUBLE.minMax<
double>();
145 double previousValue = minMax.first;
146 double nextValue = minMax.second;
149 for(
const TFDataType::value_type& data : m_tfData)
151 if(value < data.first )
153 nextValue = data.first;
158 previousValue = data.first;
161 if(previousValue == minMax.first)
165 else if(nextValue == minMax.second)
171 if((value - previousValue) < (nextValue - value))
201 m_tfData[value] = color;
207 m_tfData.erase(value);
219 TFColorVectorType colors;
220 std::transform( m_tfData.begin(), m_tfData.end(),
221 std::back_inserter(colors),
222 std::bind(&TFDataType::value_type::second, std::placeholders::_1) );
230 OSLM_ASSERT(
"It must have at least one value.", m_tfData.size() >= 1);
232 const double min = std::numeric_limits<double>::min();
233 const double max = std::numeric_limits<double>::max();
235 double previousValue = min;
236 double nextValue = max;
238 const TFColor blackColor(0.0, 0.0, 0.0, 0.0);
240 TFColor previousColor = blackColor;
241 TFColor nextColor = blackColor;
243 for(
const TFDataType::value_type& data : m_tfData)
245 if(value < data.first )
247 nextValue = data.first;
248 nextColor = data.second;
253 previousValue = data.first;
254 previousColor = data.second;
257 if(previousValue == min)
268 else if(nextValue == max)
270 if(m_isClamped && (value != previousValue))
276 color = previousColor;
281 if((value - previousValue) < (nextValue - value))
283 color = previousColor;
298 OSLM_ASSERT(
"It must have at least one value.", m_tfData.size() >= 1);
300 const double min = std::numeric_limits<double>::min();
301 const double max = std::numeric_limits<double>::max();
302 double previousValue = min;
303 double nextValue = max;
305 const TFColor blackColor(0.0, 0.0, 0.0, 0.0);
307 TFColor previousColor = blackColor;
308 TFColor nextColor = blackColor;
310 for(
const TFDataType::value_type& data : m_tfData)
312 if(value < data.first )
314 nextValue = data.first;
315 nextColor = data.second;
320 previousValue = data.first;
321 previousColor = data.second;
324 if(previousValue == min)
335 else if(nextValue == max)
337 if(m_isClamped && (value != previousValue))
343 color = previousColor;
349 const double distanceToNextValue = nextValue - value;
350 const double distanceToPreviousValue = value - previousValue;
351 const double distance = 1.0 / (nextValue - previousValue);
352 const double coefPrevious = 1.0 - (distanceToPreviousValue * distance);
353 const double coefNext = 1.0 - (distanceToNextValue * distance);
355 color.
r = coefPrevious*previousColor.
r + coefNext*nextColor.
r;
356 color.
g = coefPrevious*previousColor.
g + coefNext*nextColor.
g;
357 color.
b = coefPrevious*previousColor.
b + coefNext*nextColor.
b;
358 color.
a = coefPrevious*previousColor.
a + coefNext*nextColor.
a;
367 TFDataType::const_iterator itr = m_tfData.find(value);
368 OSLM_ASSERT(
"The value "<< value <<
" is not defined in the transfer function.", itr != m_tfData.end());
375 TransferFunction::csptr other = TransferFunction::dynamicConstCast(_source);
377 "Unable to copy" + (_source ? _source->getClassname() : std::string(
"<NULL>"))
380 this->m_level = other->m_level;
381 this->m_window = other->m_window;
382 this->m_name = other->m_name;
383 this->m_backgroundColor = other->m_backgroundColor;
384 this->m_tfData = other->m_tfData;
385 this->m_interpolationMode = other->m_interpolationMode;
386 this->m_isClamped = other->m_isClamped;
393 TransferFunction::csptr other = TransferFunction::dynamicConstCast(_source);
395 "Unable to copy" + (_source ? _source->getClassname() : std::string(
"<NULL>"))
398 this->m_level = other->m_level;
399 this->m_window = other->m_window;
400 this->m_name = other->m_name;
401 this->m_backgroundColor = other->m_backgroundColor;
402 this->m_tfData = other->m_tfData;
403 this->m_interpolationMode = other->m_interpolationMode;
404 this->m_isClamped = other->m_isClamped;
FWDATA_API void setWLMinMax(const TFValuePairType &minMax)
Set the min/max of the window level.
FWDATA_API void shallowCopy(const Object::csptr &_source) override
Shallow copy method.
FWDATA_API TFColor getNearestColor(TFValueType value) const
Get the nearest color of a value.
ColorType r
red color (value [0,1])
FWDATA_API TFValueVectorType getScaledValues() const
Get all the point values of the TF scaled and shifted with the window level.
static FWDATA_API const std::string s_DEFAULT_TF_NAME
Default transfer function name.
#define OSLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
FWDATA_API void addTFColor(TFValueType value, const TFColor &color)
Add a new TF color point.
FWDATA_API const TFDataType & getTFData() const
Get all the TF data .
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
Implements data exception class.
virtual FWDATA_API ~TransferFunction()
Destructor.
FWDATA_API void clear()
Clear all the TF data.
ColorType b
blue color (value [0,1])
ColorType a
alpha (value [0,1])
FWDATA_API void cachedDeepCopy(const Object::csptr &_source, DeepCopyCacheType &cache) override
Deep copy method.
virtual const std::string & getClassname() const override
return full object's classname with its namespace, i.e. fwCore::BaseObject
FWDATA_API void fieldDeepCopy(const ::fwData::Object::csptr &source)
A deep copy of fields (objects in m_children)
FWDATA_API TFValuePairType getMinMaxTFValues() const
Get the first and last point values of the tf data.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
FWDATA_API void eraseTFValue(TFValueType value)
Erase a TF color point.
FWDATA_API void setTFData(const TFDataType &tfData)
Set all the TF data (clear previous tf data).
This class defines a transfer function.
ColorType g
green color (value [0,1])
FWDATA_API TFColorVectorType getTFColors() const
Get all the colors of the TF.
Contains the representation of the data objects used in the framework.
FWDATA_API void fieldShallowCopy(const ::fwData::Object::csptr &source)
A shallow copy of fields (objects in m_children)
Macro for deep and shallow copies.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_WINDOWING_MODIFIED_SIG
Type of signal when points are modified.
FWDATA_API TFColor getLinearColor(TFValueType value) const
Get the color for a value (the color is computed with a linear interpolation).
FWDATA_API TFValueType getNearestValue(TFValueType value) const
Return the nearest point value of a value.
FWDATA_API TFValueVectorType getTFValues() const
Get all the point values of the TF (keys of the map m_tfData)
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_POINTS_MODIFIED_SIG
Type of signal when points are modified.
FWDATA_API TransferFunction(::fwData::Object::Key key)
Constructor.
FWDATA_API TFValuePairType getWLMinMax() const
Get the min/max of the window level.