6 #include "fwVtkIO/vtk.hpp" 8 #include <fwData/Image.hpp> 9 #include <fwData/ObjectLock.hpp> 11 #include <fwDataTools/helper/Image.hpp> 12 #include <fwDataTools/helper/ImageGetter.hpp> 14 #include <fwMath/MeshFunctions.hpp> 16 #include <boost/assign/list_of.hpp> 17 #include <boost/cast.hpp> 20 #include <vtkCellType.h> 21 #include <vtkDataArray.h> 22 #include <vtkDataSetAttributes.h> 23 #include <vtkImageData.h> 24 #include <vtkImageExport.h> 25 #include <vtkImageImport.h> 26 #include <vtkLookupTable.h> 27 #include <vtkMatrix4x4.h> 28 #include <vtkPointData.h> 29 #include <vtkPoints.h> 30 #include <vtkPolyData.h> 31 #include <vtkPolyDataNormals.h> 32 #include <vtkPolyDataWriter.h> 33 #include <vtkSetGet.h> 34 #include <vtkSmartPointer.h> 36 #include <vtkUnstructuredGrid.h> 48 TypeTranslator::fwToolsToVtkMap::mapped_type TypeTranslator::translate(
49 const TypeTranslator::fwToolsToVtkMap::key_type& key )
51 fwToolsToVtkMap::const_iterator it = s_toVtk.find( key );
52 FW_RAISE_IF(
"Unknown Type: " << key, it == s_toVtk.end() );
58 TypeTranslator::VtkTofwToolsMap::mapped_type TypeTranslator::translate(
59 const TypeTranslator::VtkTofwToolsMap::key_type& key )
61 VtkTofwToolsMap::const_iterator it = s_fromVtk.find( key );
62 FW_RAISE_IF(
"Unknown Type: " << key, it == s_fromVtk.end() );
66 const TypeTranslator::fwToolsToVtkMap TypeTranslator::s_toVtk
67 = boost::assign::map_list_of
70 ( fwTools::Type::create(
"int8" ), VTK_CHAR )
71 ( fwTools::Type::create(
"uint8" ), VTK_UNSIGNED_CHAR )
73 ( fwTools::Type::create(
"int16"), VTK_SHORT )
74 ( fwTools::Type::create(
"uint16"), VTK_UNSIGNED_SHORT )
76 ( fwTools::Type::create(
"int32"), VTK_INT )
77 ( fwTools::Type::create(
"uint32"), VTK_UNSIGNED_INT )
79 ( fwTools::Type::create(
"float" ), VTK_FLOAT )
80 ( fwTools::Type::create(
"double"), VTK_DOUBLE )
82 #
if ( INT_MAX < LONG_MAX )
83 ( fwTools::Type::create(
"int64"), VTK_LONG )
84 ( fwTools::Type::create(
"uint64"), VTK_UNSIGNED_LONG )
88 const TypeTranslator::VtkTofwToolsMap TypeTranslator::s_fromVtk
89 = boost::assign::map_list_of
93 ( VTK_SIGNED_CHAR, fwTools::Type::create(
"int8" ) )
94 ( VTK_CHAR, fwTools::Type::create(
"int8" ) )
95 ( VTK_UNSIGNED_CHAR, fwTools::Type::create(
"uint8" ) )
97 ( VTK_SHORT, fwTools::Type::create(
"int16") )
98 ( VTK_UNSIGNED_SHORT, fwTools::Type::create(
"uint16") )
100 ( VTK_INT, fwTools::Type::create(
"int32") )
101 ( VTK_UNSIGNED_INT, fwTools::Type::create(
"uint32") )
103 ( VTK_FLOAT, fwTools::Type::create(
"float" ) )
104 ( VTK_DOUBLE, fwTools::Type::create(
"double") )
106 #
if ( INT_MAX < LONG_MAX )
107 ( VTK_LONG, fwTools::Type::create(
"int64") )
108 ( VTK_UNSIGNED_LONG, fwTools::Type::create(
"uint64") )
110 ( VTK___INT64, fwTools::Type::create(
"int64") )
111 ( VTK_LONG_LONG, fwTools::Type::create(
"int64") )
113 ( VTK_UNSIGNED___INT64, fwTools::Type::create(
"uint64") )
114 ( VTK_UNSIGNED_LONG_LONG, fwTools::Type::create(
"uint64") )
116 ( VTK_LONG, fwTools::Type::create(
"int32") )
117 ( VTK_UNSIGNED_LONG, fwTools::Type::create(
"uint32") )
123 void toVTKImage( ::fwData::Image::csptr data, vtkImageData* dst)
125 vtkSmartPointer< vtkImageImport > importer = vtkSmartPointer< vtkImageImport >::New();
127 configureVTKImageImport( importer, data );
131 dst->ShallowCopy(importer->GetOutput());
136 template<
typename IMAGETYPE >
137 void* newBuffer(
size_t size)
139 IMAGETYPE* destBuffer;
142 destBuffer =
new IMAGETYPE[ size ];
144 catch (std::exception& e)
146 OSLM_ERROR(
"No enough memory to allocate an image of type " 147 << fwTools::makeDynamicType<IMAGETYPE>().
string()
148 <<
" and of size "<< size <<
"." << std::endl
157 template<
typename IMAGETYPE >
158 void fromRGBBuffer(
void* input,
size_t size,
void*& destBuffer)
160 if(destBuffer == NULL)
162 destBuffer = newBuffer<IMAGETYPE>(size);
165 IMAGETYPE* destBufferTyped = (IMAGETYPE*)destBuffer;
166 IMAGETYPE* inputTyped = (IMAGETYPE*)input;
167 IMAGETYPE* finalPtr = ((IMAGETYPE*)destBuffer) + size;
168 IMAGETYPE valR, valG, valB;
170 while (destBufferTyped < finalPtr)
172 valR = (IMAGETYPE)(
float((*(inputTyped++)) * 0.30));
173 valG = (IMAGETYPE)(
float((*(inputTyped++)) * 0.59));
174 valB = (IMAGETYPE)(
float((*(inputTyped++)) * 0.11));
175 (*destBufferTyped++) = valR + valG + valB;
181 template<
typename IMAGETYPE >
182 void fromRGBBufferColor(
void* input,
size_t size,
void*& destBuffer)
184 if(destBuffer == NULL)
186 destBuffer = newBuffer<IMAGETYPE>(size);
189 IMAGETYPE* destBufferTyped = (IMAGETYPE*)destBuffer;
190 IMAGETYPE* inputTyped = (IMAGETYPE*)input;
191 IMAGETYPE* finalPtr = ((IMAGETYPE*)destBuffer) + size;
193 while (destBufferTyped < finalPtr)
195 (*destBufferTyped++) = (*(inputTyped++));
201 void fromVTKImage( vtkImageData* source, ::fwData::Image::sptr destination )
203 SLM_ASSERT(
"vtkImageData source and/or ::fwData::Image destination are not correct", destination && source );
211 int dim = source->GetDataDimension();
212 OSLM_TRACE(
"source->GetDataDimension() : " << dim);
217 size[0] = source->GetDimensions()[0];
218 size[1] = source->GetDimensions()[1];
222 spacing[0] = source->GetSpacing()[0];
223 spacing[1] = source->GetSpacing()[1];
227 origin[0] = source->GetOrigin()[0];
228 origin[1] = source->GetOrigin()[1];
238 const int nbComponents = source->GetNumberOfScalarComponents();
240 std::accumulate(source->GetDimensions(), source->GetDimensions()+dim, std::max(3,
242 std::multiplies<size_t>() );
243 const void* input = source->GetScalarPointer();
248 const int nbBytePerPixel = source->GetScalarSize();
249 OSLM_TRACE(
"image size : " << size <<
" - nbBytePerPixel : " << nbBytePerPixel );
251 OSLM_TRACE(nbComponents <<
" components, " << TypeTranslator::translate( source->GetScalarType() ));
252 destination->setType( TypeTranslator::translate( source->GetScalarType() ) );
253 destination->setNumberOfComponents(nbComponents);
254 destination->allocate();
256 destBuffer = imageHelper.getBuffer();
257 const size_t sizeInBytes = destination->getSizeInBytes();
258 std::memcpy(destBuffer, input, sizeInBytes);
266 void configureVTKImageImport( ::vtkImageImport* _pImageImport, ::fwData::Image::csptr _pDataImage )
270 if(_pDataImage->getSize().size() == 2)
272 _pImageImport->SetDataSpacing( _pDataImage->getSpacing().at(0),
273 _pDataImage->getSpacing().at(1),
277 _pImageImport->SetDataOrigin( _pDataImage->getOrigin().at(0),
278 _pDataImage->getOrigin().at(1),
282 _pImageImport->SetWholeExtent( 0, _pDataImage->getSize().at(0) - 1,
283 0, _pDataImage->getSize().at(1) - 1,
289 _pImageImport->SetDataSpacing( _pDataImage->getSpacing().at(0),
290 _pDataImage->getSpacing().at(1),
291 _pDataImage->getSpacing().at(2)
294 _pImageImport->SetDataOrigin( _pDataImage->getOrigin().at(0),
295 _pDataImage->getOrigin().at(1),
296 _pDataImage->getOrigin().at(2)
299 _pImageImport->SetWholeExtent( 0, _pDataImage->getSize().at(0) - 1,
300 0, _pDataImage->getSize().at(1) - 1,
301 0, _pDataImage->getSize().at(2) - 1
305 _pImageImport->SetNumberOfScalarComponents(static_cast<int>( _pDataImage->getNumberOfComponents() ));
308 _pImageImport->SetDataExtentToWholeExtent();
310 _pImageImport->SetImportVoidPointer( imageHelper.getBuffer() );
312 _pImageImport->SetDataScalarType( TypeTranslator::translate(_pDataImage->getType()) );
317 vtkSmartPointer<vtkMatrix4x4> toVTKMatrix( ::fwData::TransformationMatrix3D::csptr _transfoMatrix )
319 auto matrix = vtkSmartPointer<vtkMatrix4x4>::New();
320 for(std::uint8_t l = 0; l < 4; l++)
322 for(std::uint8_t c = 0; c < 4; c++)
324 matrix->SetElement(l, c, _transfoMatrix->getCoefficient(l, c));
332 bool fromVTKMatrix( vtkMatrix4x4* _matrix, ::fwData::TransformationMatrix3D::sptr _transfoMatrix)
335 for(std::uint8_t l = 0; l < 4; l++)
337 for(std::uint8_t c = 0; c < 4; c++)
339 _transfoMatrix->setCoefficient(l, c, _matrix->GetElement(l, c));
#define OSLM_TRACE(message)
#define OSLM_ERROR(message)
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
std::vector< double > SpacingType
Image spacing type.
A simple helper to lock specific object, manages : Image, Mesh and Array.
::fwData::Array::SizeType SizeType
Image size type.
std::vector< double > OriginType
Image origin type.