Commit f030ae55 authored by Julien Michel's avatar Julien Michel
Browse files

Maj DXF.

parent 26597bcb
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __otbSpatialObjectToImageDrawingFilter_h
#define __otbSpatialObjectToImageDrawingFilter_h
#include "itkImageSource.h"
#include "itkConceptChecking.h"
#include "itkSpatialObject.h"
namespace otb
{
/** \class SpatialObjectToImageDrawingFilter
* \brief
*
* \brief Base class for filters that take a SpatialObject
* as input and produce an image as output.
* By default, if the user does not specify the size of the output image,
* the maximum size of the object's bounding box is used.
* The spacing of the image is given by the spacing of the input
* Spatial object.
*/
template <class TInputSpatialObject, class TOutputImage>
class ITK_EXPORT SpatialObjectToImageDrawingFilter : public itk::ImageSource<TOutputImage>
......@@ -34,37 +39,30 @@ class ITK_EXPORT SpatialObjectToImageDrawingFilter : public itk::ImageSource<TOu
public:
/** Standard class typedefs. */
typedef SpatialObjectToImageDrawingFilter Self;
typedef itk::ImageSource<TOutputImage> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef itk::ImageSource<TOutputImage> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef typename TOutputImage::SizeType SizeType;
typedef typename TOutputImage::PointType PointType;
typedef TOutputImage OutputImageType;
typedef typename OutputImageType::Pointer OutputImagePointer;
typedef typename OutputImageType::ValueType ValueType;
typedef typename OutputImageType::SpacingType SpacingType;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(SpatialObjectToImageDrawingFilter,itk::ImageSource);
/** Template parameters typedef */
typedef TOutputImage OutputImageType;
typedef typename OutputImageType::Pointer OutputImagePointer;
typedef typename OutputImageType::ValueType ValueType;
typedef typename OutputImageType::SpacingType SpacingType;
typedef typename OutputImageType::SizeType SizeType;
typedef typename OutputImageType::PointType PointType;
/** Superclass typedefs. */
typedef typename Superclass::OutputImageRegionType OutputImageRegionType;
/** Some convenient typedefs. */
typedef TInputSpatialObject InputSpatialObjectType;
typedef typename InputSpatialObjectType::Pointer InputSpatialObjectPointer;
typedef typename InputSpatialObjectType::ConstPointer InputSpatialObjectConstPointer;
typedef typename TInputSpatialObject::ChildrenListType ChildrenListType;
/** Spatial objects related typedefs */
typedef itk::SpatialObject<InputSpatialObjectType::ObjectDimension> SpatialObjectType;
typedef typename SpatialObjectType::ChildrenListType* ChildrenType;
typedef typename SpatialObjectType::ChildrenListType::iterator IteratorType;
/** Superclass typedefs. */
typedef typename Superclass::OutputImageRegionType OutputImageRegionType;
typedef typename TInputSpatialObject::ChildrenListType ChildrenListType;
/** ImageDimension constants */
itkStaticConstMacro(ObjectDimension, unsigned int,
......@@ -74,7 +72,7 @@ public:
/** Set/Get the image input of this process object. */
virtual void SetInput( const InputSpatialObjectType *object);
virtual void SetInput( unsigned int index, const InputSpatialObjectType * object);
virtual void SetInput( unsigned int, const InputSpatialObjectType * object);
const InputSpatialObjectType * GetInput(void);
const InputSpatialObjectType * GetInput(unsigned int idx);
......@@ -82,10 +80,10 @@ public:
* spacing is the geometric distance between image samples.
* It is stored internally as double, but may be set from
* float. \sa GetSpacing() */
itkGetConstReferenceMacro(Spacing,SpacingType);
virtual void SetSpacing( const SpacingType& spacing);
virtual void SetSpacing( const double* spacing);
virtual void SetSpacing( const float* spacing);
virtual const double* GetSpacing() const;
/** Set/Get the value for pixels inside the spatial object.
* By default, this filter will return an image
......@@ -112,8 +110,7 @@ public:
virtual void SetOrigin( const PointType& origin);
virtual void SetOrigin( const double* origin);
virtual void SetOrigin( const float* origin);
itkGetConstReferenceMacro(Origin,PointType);
virtual const double * GetOrigin() const;
/** The spatial object being transformed can be part of a hierarchy.
* How deep in the hierarchy should we descend in generating the
......@@ -123,43 +120,47 @@ public:
itkGetMacro(ChildrenDepth, unsigned int);
/** Set/Get Size */
virtual void SetSize(const SizeType& size);
itkSetMacro(Size,SizeType);
itkGetMacro(Size,SizeType);
/** If UseObjectValue is set to true, then the filter uses
* the ValueAt() function instead of IsInside() */
itkSetMacro(UseObjectValue,bool);
itkGetMacro(UseObjectValue,bool);
typedef itk::SpatialObject<InputSpatialObjectType::ObjectDimension> SpatialObjectType;
typedef typename SpatialObjectType::ChildrenListType* ChildrenType;
typedef typename SpatialObjectType::ChildrenListType::iterator IteratorType;
protected:
/** Constructor */
SpatialObjectToImageDrawingFilter();
/** Destructor */
~SpatialObjectToImageDrawingFilter();
/** Main computation method */
virtual void GenerateOutputInformation(){}; // do nothing
virtual void GenerateData();
/** Printself method */
virtual void PrintSelf(std::ostream& os, itk::Indent indent) const;
/** Dummy GenerateOuptutInformation */
virtual void GenerateOutputInformation(void){}
private:
SpatialObjectToImageDrawingFilter(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
SizeType m_Size;
SpacingType m_Spacing;
PointType m_Origin;
double m_Spacing[OutputImageDimension];
double m_Origin[OutputImageDimension];
unsigned int m_ChildrenDepth;
ValueType m_InsideValue;
ValueType m_OutsideValue;
bool m_UseObjectValue;
bool m_OriginSpecified;
bool m_SpacingSpecified;
bool m_SizeSpecified;
virtual void PrintSelf(std::ostream& os, itk::Indent indent) const;
private:
SpatialObjectToImageDrawingFilter(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
};
} // end namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#ifndef ITK_MANUAL_INSTANTIATION
#include "otbSpatialObjectToImageDrawingFilter.txx"
#endif
......
......@@ -73,6 +73,13 @@ class ITK_EXPORT SpatialObjectDXFReader
private:
SpatialObjectDXFReader(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
/** Test whether the given filename exist and it is readable,
this is intended to be called before attempting to use
ImageIO classes for actually reading the file. If the file
doesn't exist or it is not readable, and exception with an
approriate message will be thrown. */
void TestFileExistanceAndReadability();
std::string m_FileName;
std::string m_LayerName;
......
......@@ -22,7 +22,10 @@ PURPOSE. See the above copyright notices for more information.
#include "otbDXFToSpatialObjectGroupFilter.h"
#include "dl_dxf.h"
#include "dl_creationadapter.h"
#include "itksys/SystemTools.hxx"
#include "otbMacro.h"
#include "otbSystem.h"
#include "otbImageFileReader.h"
namespace otb
{
......@@ -55,6 +58,51 @@ SpatialObjectDXFReader<TSpatialObject>
m_FileName = filename;
this->Modified();
}
/** Test whether the given filename exist and it is readable,
this is intended to be called before attempting to use
ImageIO classes for actually reading the file. If the file
doesn't exist or it is not readable, and exception with an
approriate message will be thrown. */
template <class TSpatialObject>
void
SpatialObjectDXFReader<TSpatialObject>
::TestFileExistanceAndReadability()
{
// Test if the file exists.
if( ! itksys::SystemTools::FileExists( this->m_FileName.c_str() ) )
{
itk::ImageFileReaderException e(__FILE__, __LINE__);
itk::OStringStream msg;
msg <<"The file doesn't exists. "
<< std::endl << "Filename = " << this->m_FileName
<< std::endl;
e.SetDescription(msg.str().c_str());
throw e;
return;
}
// Test if the file can be open for reading access.
//Only if m_FileName speciy a filname (not a dirname)
if( System::IsAFileName( this->m_FileName ) == true )
{
std::ifstream readTester;
readTester.open( this->m_FileName.c_str() );
if( readTester.fail() )
{
readTester.close();
itk::OStringStream msg;
msg <<"The file couldn't be opened for reading. "
<< std::endl << "Filename: " << this->m_FileName
<< std::endl;
itk::ImageFileReaderException e(__FILE__, __LINE__,msg.str().c_str(),ITK_LOCATION);
throw e;
return;
}
readTester.close();
}
}
/**
* Main computation method
*/
......@@ -63,7 +111,7 @@ void
SpatialObjectDXFReader<TSpatialObject>
::GenerateData()
{
std::cout<<"Entering GenerateData()"<<std::endl;
TestFileExistanceAndReadability();
GroupSpatialObjectType * ptr = this->GetOutput();
typedef otb::DXFToSpatialObjectGroupFilter<TSpatialObject> CreationFilter;
typename CreationFilter::Pointer creationClass = CreationFilter::New();
......@@ -75,7 +123,6 @@ SpatialObjectDXFReader<TSpatialObject>
dxf.in(m_FileName, creationClass);
GroupSpatialObjectType * group = creationClass->GetOutput();
group->Update();
otbMsgDebugMacro(<<" from inside class: "<<group->GetNumberOfChildren());
this->SetNthOutput(0,group);
}
......
......@@ -962,8 +962,12 @@ ADD_TEST(ioTvSpatialObjectDXFReader ${IO_TESTS}
${BASELINE}/ioTvSpatialObjectDXFReaderOutput.png
${TEMP}/ioTvSpatialObjectDXFReaderOutput.png
otbSpatialObjectDXFReader
${INPUTDATA}/test.dxf
${TEMP}/ioTvSpatialObjectDXFReaderOutput.png)
${INPUTDATA}/Lardenne.dxf
${TEMP}/ioTvSpatialObjectDXFReaderOutput.png
TOIT
50
)
#----------------------------------------------------------------------------------
# Exemples de tests sur les grosses images
......
......@@ -20,6 +20,7 @@
#include "otbImageFileWriter.h"
#include "otbSpatialObjectDXFReader.h"
#include "otbSpatialObjectToImageDrawingFilter.h"
#include "itkRescaleIntensityImageFilter.h"
int otbSpatialObjectDXFReader(int argc, char * argv[])
{
......@@ -28,21 +29,26 @@ int otbSpatialObjectDXFReader(int argc, char * argv[])
const unsigned int Dimension = 2;
char * inputFilename = argv[1];
char * outputFilename = argv[2];
const unsigned int outputSize = atoi(argv[4]);
typedef unsigned char PixelType;
typedef double PixelType;
typedef unsigned char OutputPixelType;
typedef itk::GroupSpatialObject<Dimension> GroupType;
typedef otb::Image<PixelType,Dimension> ImageType;
typedef otb::ImageFileWriter<ImageType> WriterType;
typedef otb::Image<OutputPixelType,Dimension> OutputImageType;
typedef otb::ImageFileWriter<OutputImageType> WriterType;
typedef otb::SpatialObjectDXFReader<GroupType> SpatialObjectDXFReaderType;
typedef otb::SpatialObjectToImageDrawingFilter<GroupType,ImageType> SpatialObjectToImageDrawingFilterType;
typedef itk::RescaleIntensityImageFilter< ImageType, OutputImageType > CastFilterType;
typedef itk::SpatialObject<Dimension> SpatialObjectType;
// Instantiating object
SpatialObjectDXFReaderType::Pointer reader = SpatialObjectDXFReaderType::New();
SpatialObjectToImageDrawingFilterType::Pointer imageGenerator = SpatialObjectToImageDrawingFilterType::New();
WriterType::Pointer writer = WriterType::New();
CastFilterType::Pointer castFilter = CastFilterType::New();
reader->SetFileName(inputFilename);
//reader->SetLayerName("TOIT");
reader->SetLayerName(argv[3]);
reader->Update();
......@@ -50,57 +56,64 @@ int otbSpatialObjectDXFReader(int argc, char * argv[])
if(group->GetNumberOfChildren() != 0)
{
GroupType::ChildrenListType* children=group->GetChildren(0);
GroupType::ChildrenListType::iterator it = children->begin();
GroupType::ChildrenListType::iterator end = children->end();
double maximum[Dimension],minimum[Dimension];
(*it)->ComputeBoundingBox();
minimum[0]=(*it)->GetBoundingBox()->GetMinimum()[0];
minimum[1]=(*it)->GetBoundingBox()->GetMinimum()[1];
while(it != end)
{
(*it)->ComputeBoundingBox();
if ((*it)->GetBoundingBox()->GetMinimum()[0] < minimum[0])
{
minimum[0]=(*it)->GetBoundingBox()->GetMinimum()[0];
}
if ((*it)->GetBoundingBox()->GetMinimum()[1] < minimum[1])
{
minimum[1]=(*it)->GetBoundingBox()->GetMinimum()[1];
}
it++;
}
ImageType::SizeType size;
size[0]=100;
size[1]=100;
imageGenerator->SetSize(size);
ImageType::PointType origin;
origin[0]= minimum[0];
origin[1]= minimum[1];
imageGenerator->SetOrigin(origin);
group->ComputeBoundingBox();
maximum[0]=group->GetBoundingBox()->GetMaximum()[0];
maximum[1]=group->GetBoundingBox()->GetMaximum()[1];
ImageType::SpacingType spacing;
spacing[0]=(maximum[0]-origin[0])/size[0];
spacing[1]=(maximum[1]-origin[1])/size[1];
imageGenerator->SetSpacing(spacing);
std::cout<<"Minimum: "<<minimum[0]<<", "<<minimum[1]<<std::endl;
std::cout<<"Maximum: "<<maximum[0]<<", "<<maximum[1]<<std::endl;
std::cout<<"Origin: "<<origin<<std::endl;
std::cout<<"Spacing: "<<spacing << std::endl;
imageGenerator->SetInput(group);
writer->SetInput(imageGenerator->GetOutput());
writer->SetFileName(outputFilename);
writer->Update();
/** Writing image **/
SpatialObjectType::ChildrenListType* children=group->GetChildren(0);
SpatialObjectType::ChildrenListType::iterator it = children->begin();
SpatialObjectType::ChildrenListType::iterator end = children->end();
double maximum[Dimension],minimum[Dimension];
(*it)->ComputeBoundingBox();
minimum[0]=(*it)->GetBoundingBox()->GetMinimum()[0];
minimum[1]=(*it)->GetBoundingBox()->GetMinimum()[1];
while(it != end)
{
(*it)->ComputeBoundingBox();
if ((*it)->GetBoundingBox()->GetMinimum()[0] < minimum[0])
{
minimum[0]=(*it)->GetBoundingBox()->GetMinimum()[0];
}
if ((*it)->GetBoundingBox()->GetMinimum()[1] < minimum[1])
{
minimum[1]=(*it)->GetBoundingBox()->GetMinimum()[1];
}
it++;
}
ImageType::SizeType size;
size[0]=outputSize;
size[1]=outputSize;
imageGenerator->SetSize(size);
ImageType::PointType origin;
origin[0]=(int) minimum[0];
origin[1]=(int) minimum[1];
imageGenerator->SetOrigin(origin);
group->ComputeBoundingBox();
maximum[0]=group->GetBoundingBox()->GetMaximum()[0];
maximum[1]=group->GetBoundingBox()->GetMaximum()[1];
ImageType::SpacingType spacing;
spacing[0]=(maximum[0]-origin[0])/size[0];
spacing[1]=(maximum[1]-origin[1])/size[1];
imageGenerator->SetSpacing(spacing);
imageGenerator->SetInput(group);
imageGenerator->Update();
castFilter->SetOutputMinimum( 0 );
castFilter->SetOutputMaximum( 255 );
castFilter->SetInput( imageGenerator->GetOutput() );
writer->SetInput(castFilter->GetOutput());
writer->SetFileName(outputFilename);
writer->Update();
}
else
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment