ImageIO no longer depends on VTK

* The non-documented WITH_VTK is no longer supported.
* The function CGAL::Image_3::read_vtk_image_data
  has been replaced by a global function.
* Added Mesh_3/examples/Mesh_3/mesh_3D_gray_vtk_image.cpp
This commit is contained in:
Andreas Fabri 2016-01-13 15:50:54 +01:00
parent 4afa8a7813
commit 57ad151cd4
10 changed files with 224 additions and 46 deletions

View File

@ -64,6 +64,7 @@ public:
class CGAL_IMAGEIO_EXPORT Image_3
{
struct Image_deleter {
void operator()(_image* image)
{
@ -164,7 +165,7 @@ public:
vx,vy,vz,offset));
}
#ifdef CGAL_USE_VTK
#if 0
bool read_vtk_image_data(vtkImageData*);
#endif // CGAL_USE_VTK

View File

@ -157,7 +157,7 @@ void Image_3::gl_draw_bbox(const float line_width,
} // end namespace CGAL
#ifdef CGAL_USE_VTK
#if 0
#include <vtkImageData.h>
#include <CGAL/Image_3_vtk_interface.h>

View File

@ -23,7 +23,6 @@
#include <CGAL/config.h>
#ifdef CGAL_USE_VTK
#include <CGAL/Image_3.h>
#include <vtkImageData.h>
#include <vtkPointData.h>
@ -126,7 +125,5 @@ struct VTK_type_generator<boost::uint32_t> {
} //end namespace CGAL
#endif // CGAL_USE_VTK
#endif // CGAL_IMAGE_3_VTK_INTERFACE_H

View File

@ -0,0 +1,95 @@
// Copyright (c) 2005-2008 INRIA Sophia-Antipolis (France).
// 2008 GeometryFactory
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org); 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 3 of the License,
// or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL$
// $Id$
//
//
// Author(s) : Laurent Rineau, Pierre Alliez
#ifndef CGAL_READ_VTK_IMAGE_DATA_H
#define CGAL_READ_VTK_IMAGE_DATA_H
#include <CGAL/Image_3.h>
#include <CGAL/Image_3_vtk_interface.h>
namespace CGAL {
namespace {
struct VTK_to_ImageIO_type_mapper {
WORD_KIND wordKind;
SIGN sign;
unsigned int wdim;
};
static const VTK_to_ImageIO_type_mapper VTK_to_ImageIO_type[VTK_ID_TYPE] =
{ { WK_UNKNOWN, SGN_UNKNOWN, 0}, // 0=VTK_VOID
{ WK_UNKNOWN, SGN_UNKNOWN, 0}, // 1=VTK_BIT
{ WK_FIXED, SGN_SIGNED, 1}, // 2=VTK_CHAR
{ WK_FIXED, SGN_UNSIGNED, 1}, // 3=VTK_UNSIGNED_CHAR
{ WK_FIXED, SGN_SIGNED, 2}, // 4=VTK_SHORT
{ WK_FIXED, SGN_UNSIGNED, 2}, // 5=VTK_UNSIGNED_SHORT
{ WK_FIXED, SGN_SIGNED, 4}, // 6=VTK_INT
{ WK_FIXED, SGN_UNSIGNED, 4}, // 7=VTK_UNSIGNED_INT
{ WK_FIXED, SGN_SIGNED, 8}, // 8=VTK_LONG
{ WK_FIXED, SGN_UNSIGNED, 8}, // 9=VTK_UNSIGNED_LONG
{ WK_FLOAT, SGN_SIGNED, 4}, // 10=VTK_FLOAT
{ WK_FIXED, SGN_SIGNED, 8} // 11=VTK_DOUBLE
};
} //end anonymous namespace
inline
Image_3
read_vtk_image_data(vtkImageData* vtk_image)
{
if(!vtk_image)
return Image_3();
_image* image = ::_initImage();
const int* dims = vtk_image->GetDimensions();
const double* spacing = vtk_image->GetSpacing();
image->vectMode = VM_SCALAR;
image->xdim = dims[0];
image->ydim = dims[1];
image->zdim = dims[2];
image->vdim = 1;
image->vx = spacing[0];
image->vy = spacing[1];
image->vz = spacing[2];
image->endianness = ::_getEndianness();
int vtk_type = vtk_image->GetScalarType();
if(vtk_type == VTK_SIGNED_CHAR) vtk_type = VTK_CHAR;
if(vtk_type < 0 || vtk_type > VTK_DOUBLE)
vtk_type = VTK_DOUBLE;
const VTK_to_ImageIO_type_mapper& imageio_type =
VTK_to_ImageIO_type[vtk_type];
image->wdim = imageio_type.wdim;
image->wordKind = imageio_type.wordKind;
image->sign = imageio_type.sign;
image->data = ::ImageIO_alloc(dims[0]*dims[1]*dims[2]*image->wdim);
std::cerr << "GetNumberOfTuples()=" << vtk_image->GetPointData()->GetScalars()->GetNumberOfTuples()
<< "\nimage->size()=" << dims[0]*dims[1]*dims[2]
<< "\nwdim=" << image->wdim << '\n';
CGAL_assertion(vtk_image->GetPointData()->GetScalars()->GetNumberOfTuples() == dims[0]*dims[1]*dims[2]);
vtk_image->GetPointData()->GetScalars()->ExportToVoidPointer(image->data);
return Image_3(image);
}
} // namespace CGAL
#endif // CGAL_READ_VTK_IMAGE_DATA_H

View File

@ -3,11 +3,6 @@ message("Configuring libCGAL_ImageIO")
find_package( OpenGL )
find_package( ZLIB )
#option(WITH_VTK "Add VTK support to libCGAL_ImageIO." OFF)
if(WITH_VTK)
find_package(VTK COMPONENTS vtkImagingCore vtkIOImage vtkFiltersImaging NO_MODULE REQUIRED)
endif(WITH_VTK)
if(OPENGL_FOUND)
message( STATUS "OpenGL include: ${OPENGL_INCLUDE_DIR}" )
@ -26,20 +21,6 @@ if(OPENGL_FOUND)
set( CGAL_ImageIO_BASENAME CGAL_ImageIO)
if(WITH_VTK)
if( VTK_FOUND )
message(STATUS "VTK-${VTK_MAJOR_VERSION}.${VTK_MINOR_VERSION}.${VTK_BUILD_VERSION} found. Build VTK support in ${CGAL_ImageIO_BASENAME}.")
include( ${VTK_USE_FILE} )
cache_set(CGAL_ImageIO_3RD_PARTY_DEFINITIONS ${CGAL_ImageIO_3RD_PARTY_DEFINITIONS} -DCGAL_USE_VTK)
cache_set(CGAL_ImageIO_3RD_PARTY_INCLUDE_DIRS ${CGAL_ImageIO_3RD_PARTY_INCLUDE_DIRS} ${VTK_INCLUDE_DIRS} )
cache_set(CGAL_ImageIO_3RD_PARTY_LIBRARIES_DIRS ${CGAL_ImageIO_3RD_PARTY_LIBRARIES_DIRS} ${vtkImagingCore_RUNTIME_LIBRARY_DIRS} ${vtkImagingCore_LIBRARY_DIRS} ${vtkIOImage_RUNTIME_LIBRARY_DIRS} ${vtkIOImage_LIBRARY_DIRS} ${vtkFiltersImaging_RUNTIME_LIBRARY_DIRS} ${vtkFiltersImaging_LIBRARY_DIRS})
cache_set(CGAL_ImageIO_3RD_PARTY_LIBRARIES ${CGAL_ImageIO_3RD_PARTY_LIBRARIES} ${VTK_LIBRARIES} )
else()
message(STATUS "VTK not found.")
endif()
endif()
if(COMMAND add_config_flag)
set( CGAL_HAS_IMAGEIO TRUE )
add_config_flag( CGAL_HAS_IMAGEIO )

View File

@ -8,9 +8,6 @@ cmake_minimum_required(VERSION 2.8.11)
include_directories(../../include)
include_directories(../../../Triangulation_3/include)
include_directories(../../../STL_Extension/include)
include_directories(../../../AABB_tree/include)
add_definitions(-DCGAL_MESH_3_NO_DEPRECATED_SURFACE_INDEX
-DCGAL_MESH_3_NO_DEPRECATED_C3T3_ITERATORS)
@ -48,6 +45,21 @@ if ( CGAL_FOUND )
endif()
set(VTK_LIBS "")
find_package(VTK QUIET COMPONENTS vtkImagingGeneral vtkIOImage NO_MODULE)
if(VTK_FOUND)
include(${VTK_USE_FILE})
if ("${VTK_VERSION_MAJOR}" GREATER "5")
message(STATUS "VTK found")
set( VTK_LIBS vtkImagingGeneral vtkIOImage )
else()
message(STATUS "VTK version 6.0 or greater is required")
endif()
else()
message(STATUS "VTK was not found")
endif()
if ( Boost_FOUND AND Boost_VERSION GREATER 103400 )
include( CGAL_CreateSingleSourceCGALProgram )
@ -62,6 +74,11 @@ if ( CGAL_FOUND )
create_single_source_cgal_program( "mesh_polyhedral_domain.cpp" )
create_single_source_cgal_program( "mesh_polyhedral_domain_with_features.cpp" )
if( WITH_CGAL_ImageIO )
if( VTK_FOUND )
add_executable ( mesh_3D_gray_vtk_image mesh_3D_gray_vtk_image.cpp )
target_link_libraries( mesh_3D_gray_vtk_image ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} ${VTK_LIBS})
endif()
if( CGAL_ImageIO_USE_ZLIB )
create_single_source_cgal_program( "mesh_optimization_example.cpp" )
create_single_source_cgal_program( "mesh_optimization_lloyd_example.cpp" )

View File

@ -0,0 +1,79 @@
#include <vtkImageData.h>
#include <vtkDICOMImageReader.h>
#include <vtkImageReader.h>
#include <vtkImageGaussianSmooth.h>
#include <vtkDemandDrivenPipeline.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Mesh_triangulation_3.h>
#include <CGAL/Mesh_complex_3_in_triangulation_3.h>
#include <CGAL/Mesh_criteria_3.h>
#include <CGAL/Gray_image_mesh_domain_3.h>
#include <CGAL/make_mesh_3.h>
#include <CGAL/Image_3.h>
#include <CGAL/read_vtk_image_data.h>
typedef short Image_word_type;
// Domain
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Gray_image_mesh_domain_3<CGAL::Image_3,K,
Image_word_type,
std::binder1st< std::less<Image_word_type> > > Mesh_domain;
// Triangulation
typedef CGAL::Mesh_triangulation_3<Mesh_domain>::type Tr;
typedef CGAL::Mesh_complex_3_in_triangulation_3<Tr> C3t3;
// Criteria
typedef CGAL::Mesh_criteria_3<Tr> Mesh_criteria;
// To avoid verbose function and named parameters call
using namespace CGAL::parameters;
int main(int argc, char* argv[])
{
// Loads image
vtkDICOMImageReader*dicom_reader = vtkDICOMImageReader::New();
dicom_reader->SetDirectoryName(argv[1]);
vtkDemandDrivenPipeline*executive =
vtkDemandDrivenPipeline::SafeDownCast(dicom_reader->GetExecutive());
if (executive)
{
executive->SetReleaseDataFlag(0, 0); // where 0 is the port index
}
vtkImageGaussianSmooth* smoother = vtkImageGaussianSmooth::New();
smoother->SetStandardDeviations(1., 1., 1.);
smoother->SetInputConnection(dicom_reader->GetOutputPort());
smoother->Update();
vtkImageData* vtk_image = smoother->GetOutput();
vtk_image->Print(std::cerr);
CGAL::Image_3 image = CGAL::read_vtk_image_data(vtk_image);
if(image.image() == 0){
std::cerr << "could not create a CGAL::Image_3 from the vtk image\n";
return 0;
}
// Domain
Mesh_domain domain(image, std::bind1st(std::less<Image_word_type>(), -555), -1000);
// Mesh criteria
Mesh_criteria criteria(facet_angle=30, facet_size=6, facet_distance=1,
cell_radius_edge_ratio=3, cell_size=8);
// Meshing
C3t3 c3t3 = CGAL::make_mesh_3<C3t3>(domain, criteria);
// Output
std::ofstream medit_file("out.mesh");
c3t3.output_to_medit(medit_file);
return 0;
}

View File

@ -97,20 +97,18 @@ if ( CGAL_FOUND AND CGAL_Qt5_FOUND AND CGAL_ImageIO_FOUND)
add_to_cached_list( CGAL_EXECUTABLE_TARGETS ${prj} )
set(VTK_LIBS "")
if (WITH_VTK)
message(STATUS "VTK support ON, CGAL ImageIO must have been compiled with VTK support ON (WITH_VTK=ON at the cmake level)")
find_package(VTK QUIET COMPONENTS vtkImagingCore)
if(VTK_FOUND)
include(${VTK_USE_FILE})
if ("${VTK_VERSION_MAJOR}" GREATER "5")
add_definitions(-DCGAL_USE_VTK)
set(VTK_LIBS vtkImagingCore)
else()
message(STATUS "VTK support switched OFF, VTK version 6.0 or greater is required")
endif()
find_package(VTK QUIET COMPONENTS vtkImagingGeneral vtkIOImage NO_MODULE)
if(VTK_FOUND)
include(${VTK_USE_FILE})
if ("${VTK_VERSION_MAJOR}" GREATER "5")
message(STATUS "VTK found")
add_definitions(-DCGAL_USE_VTK)
set(VTK_LIBS vtkImagingGeneral vtkIOImage)
else()
message(STATUS "VTK support switched OFF, VTK was not found")
message(STATUS "Vtk must be at least Rel 6")
endif()
else()
message(STATUS "For reading Dicom files install VTK first")
endif()
# Link the executable to CGAL and third-party libraries

View File

@ -44,10 +44,17 @@ public:
typedef FT_ FT;
public:
CBinary_image_3() : Image_3(), interpolate_(true)
CBinary_image_3()
: Image_3(), interpolate_(true)
{
}
CBinary_image_3(const Image_3& bi)
: Image_3(bi), interpolate_(true)
{
}
CBinary_image_3(const CBinary_image_3& bi)
: Image_3(bi), interpolate_(bi.interpolate_),labellized_(bi.labellized_)
{
@ -56,9 +63,6 @@ public:
max_value = bi.max_value;
}
~CBinary_image_3()
{
}
void finish_open() {
CGAL_IMAGE_IO_CASE(image_ptr.get(),

View File

@ -325,6 +325,9 @@ void Volume::only_in()
}
#ifdef CGAL_USE_VTK
#include <CGAL/read_vtk_image_data.h>
#include <vtkImageData.h>
#include <vtkDICOMImageReader.h>
#include <vtkImageReader.h>
@ -368,7 +371,8 @@ bool Volume::opendir(const QString& dirname)
smoother->Update();
vtk_image = smoother->GetOutput();
vtk_image->Print(std::cerr);
if(!m_image.read_vtk_image_data(vtk_image))
m_image = CGAL::read_vtk_image_data(vtk_image);
if(m_image.image() == 0)
{
QMessageBox::warning(mw, mw->windowTitle(),
tr("Error with file <tt>%1/</tt>:\nunknown file format!").arg(dirname));
@ -418,7 +422,8 @@ bool Volume::open_vtk(const QString& filename)
vtk_reader->Print(std::cerr);
vtk_image = vtk_reader->GetOutput();
vtk_image->Print(std::cerr);
if(!m_image.read_vtk_image_data(vtk_image))
m_image = CGAL::read_vtk_image_data(vtk_image);
if(m_image.image() == NULL)
{
QMessageBox::warning(mw, mw->windowTitle(),
tr("Error with file <tt>%1</tt>:\nunknown file format!").arg(filename));
@ -485,7 +490,8 @@ bool Volume::open_xt(const QString& filename)
vtk_reader->Print(std::cerr);
vtkImageData* vtk_image = vtk_reader->GetOutput();
vtk_image->Print(std::cerr);
if(!m_image.read_vtk_image_data(vtk_image))
m_image = CGAL::read_vtk_image_data(vtk_image);
if(m_image.image() != NULL)
{
QMessageBox::warning(mw, mw->windowTitle(),
tr("Error with file <tt>%1</tt>:\nunknown file format!").arg(filename));