[Insight-users] Cone spatial object ?
Luis Ibanez
luis.ibanez at kitware.com
Fri Apr 9 18:24:08 EDT 2010
Hi Somi,
The problem is that a Mesh Spatial object assumes
that "inside" means:
"in the 2D manifold of its surface"
So, the points of space that are "inside" your mesh
are the ones "close enough to the mesh surface".
If you look at the source code implementation of this
class, the test is done by checking whether a given
point is inside any of the triangles of the Mesh.
--
You probably should use the itkEllipseSpatialObject
along with the SpatialObjectToImageFilter
as illustrated in the example:
Insight/Examples/Filtering/
SpatialObjectToImage1.cxx
This example shows you how to generate a rasterized
Sphere. (you probably would like to remove the cylinder
that is also used in that source code example).
Regards,
Luis
-------------------------------------------------------------------------
On Thu, Apr 8, 2010 at 6:32 PM, somi <seesomi at gmail.com> wrote:
> Hi Luis,
> Thanks for your reply. itkTubeSpatialObject worked for me, I feed stupid
> not to think of this before :)
> I took the following approach:
> vtkConeSource->VtkPolyData -> ITKMesh ->
> ITKMeshSpatialObject->ITKSpatialObjectToImage
>
> While working on this I got an unexpected behavior as described below:
> I used the follow pipeline:
>
> vtkShrereSource->VtkPolyData -> ITKMesh ->
> ITKMeshSpatialObject->ITKSpatialObjectToImage
>
>
> I expected to get a solid spherical object in the final image, but I got a
> hollow sphere (see attached screenshot).
> Is this expected behavior ?
>
> I used the following code:
> ///////
> //Code Starts
> ////
> #include <iostream>
> #include <iomanip>
> #include <itkBinaryMask3DMeshSource.h>
> #include <itkMesh.h>
> #include <itkMeshSpatialObject.h>
> #include <vtkPolyData.h>
> #include <vtkCellArray.h>
> #include <vtkSphereSource.h>
> #include <vtkCellLinks.h>
> #include <itkImage.h>
>
> #include <iostream>
> #include <iomanip>
> #include <vtkSmartPointer.h>
> #include <itkSpatialObjectToImageFilter.h>
> #include <itkImageFileWriter.h>
>
> #if defined(_MSC_VER)
> #pragma warning ( disable : 4786 )
> #endif
>
> #ifdef __BORLANDC__
> #define ITK_LEAN_AND_MEAN
> #endif
>
>
>
>
> int main( int argc, char *argv[] )
> {
>
> // Generate a Sphere in VTK
> vtkSmartPointer<vtkSphereSource> polyData =
> vtkSmartPointer<vtkSphereSource>::New();
> polyData->SetRadius(10);
> polyData->SetThetaResolution(10);
> polyData->SetPhiResolution(10);
> polyData->SetCenter(80,50,50);
> polyData->Update();
>
> #ifndef vtkFloatingPointType
> #define vtkFloatingPointType float
> #endif
>
>
>
> //
> // Convert VTK polydata to ITK Mesh
> //
>
> const unsigned int PointDimension = 3;
> const unsigned int MaxCellDimension = 2;
>
> typedef itk::DefaultStaticMeshTraits<
> vtkFloatingPointType,
> PointDimension,
> MaxCellDimension,
> vtkFloatingPointType,
> vtkFloatingPointType > MeshTraits;
>
>
> typedef itk::Mesh<
> vtkFloatingPointType,
> PointDimension,
> MeshTraits > MeshType;
>
>
> MeshType::Pointer mesh = MeshType::New();
>
> //
> // Transfer the points from the vtkPolyData into the itk::Mesh
> //
> const unsigned int numberOfPoints =
> polyData->GetOutput()->GetNumberOfPoints();
>
> vtkPoints * vtkpoints = polyData->GetOutput()->GetPoints();
>
> mesh->GetPoints()->Reserve( numberOfPoints );
>
> for(unsigned int p =0; p < numberOfPoints; p++)
> {
>
> vtkFloatingPointType * apoint = vtkpoints->GetPoint( p );
>
> mesh->SetPoint( p, MeshType::PointType( apoint ));
>
> }
>
>
> //
> // Transfer the cells from the vtkPolyData into the itk::Mesh
> //
> vtkCellArray * triangleStrips = polyData->GetOutput()->GetStrips();
>
>
> vtkIdType * cellPoints;
> vtkIdType numberOfCellPoints;
>
> //
> // First count the total number of triangles from all the triangle
> strips.
> //
> unsigned int numberOfTriangles = 0;
>
> triangleStrips->InitTraversal();
>
> while( triangleStrips->GetNextCell( numberOfCellPoints, cellPoints ) )
> {
> numberOfTriangles += numberOfCellPoints-2;
> }
>
>
> vtkCellArray * polygons = polyData->GetOutput()->GetPolys();
>
> polygons->InitTraversal();
>
> while( polygons->GetNextCell( numberOfCellPoints, cellPoints ) )
> {
> if( numberOfCellPoints == 3 )
> {
> numberOfTriangles ++;
> }
> }
>
>
>
> //
> // Reserve memory in the itk::Mesh for all those triangles
> //
> mesh->GetCells()->Reserve( numberOfTriangles );
>
>
> //
> // Copy the triangles from vtkPolyData into the itk::Mesh
> //
> //
>
> typedef MeshType::CellType CellType;
>
> typedef itk::TriangleCell< CellType > TriangleCellType;
>
> int cellId = 0;
>
> // first copy the triangle strips
> triangleStrips->InitTraversal();
> while( triangleStrips->GetNextCell( numberOfCellPoints, cellPoints ) )
> {
>
> unsigned int numberOfTrianglesInStrip = numberOfCellPoints - 2;
>
> unsigned long pointIds[3];
> pointIds[0] = cellPoints[0];
> pointIds[1] = cellPoints[1];
> pointIds[2] = cellPoints[2];
>
> for( unsigned int t=0; t < numberOfTrianglesInStrip; t++ )
> {
> MeshType::CellAutoPointer c;
> TriangleCellType * tcell = new TriangleCellType;
> tcell->SetPointIds( pointIds );
> c.TakeOwnership( tcell );
> mesh->SetCell( cellId, c );
> cellId++;
> pointIds[0] = pointIds[1];
> pointIds[1] = pointIds[2];
> pointIds[2] = cellPoints[t+3];
> }
>
>
> }
>
>
> // then copy the normal triangles
> polygons->InitTraversal();
> while( polygons->GetNextCell( numberOfCellPoints, cellPoints ) )
> {
> if( numberOfCellPoints !=3 ) // skip any non-triangle.
> {
> continue;
> }
> MeshType::CellAutoPointer c;
> TriangleCellType * t = new TriangleCellType;
> t->SetPointIds( (unsigned long*)cellPoints );
> c.TakeOwnership( t );
> mesh->SetCell( cellId, c );
> cellId++;
> }
>
>
>
> std::cout << "Mesh " << std::endl;
> std::cout << "Number of Points = " << mesh->GetNumberOfPoints() <<
> std::endl;
> std::cout << "Number of Cells = " << mesh->GetNumberOfCells() <<
> std::endl;
>
>
>
>
> //
> // Convert ITK Mesh to ITK Mesh spatial object
> //
>
> typedef itk::MeshSpatialObject< MeshType > SpatialObjectType;
> SpatialObjectType::Pointer meshSpa = SpatialObjectType::New();
> meshSpa->SetMesh( mesh);
> meshSpa->Update();
>
>
>
> typedef unsigned char PixelType;
> const unsigned int Dimension = 3;
> typedef itk::Image< PixelType, Dimension > ImageType;
>
> typedef itk::SpatialObjectToImageFilter<
> SpatialObjectType, ImageType >
> SpatialObjectToImageFilterType;
>
> SpatialObjectToImageFilterType::Pointer imageFilter =
> SpatialObjectToImageFilterType::New();
> ImageType::SizeType size;
> size[ 0 ] = 100;
> size[ 1 ] = 100;
> size[ 2 ] = 100;
> imageFilter->SetSize( size );
> ImageType::SpacingType spacing;
> spacing[0] = 100.0 / size[0];
> spacing[1] = 100.0 / size[1];
> spacing[2] = 100.0 / size[2];
>
> imageFilter->SetSpacing( spacing );
> imageFilter->SetInput( meshSpa );
>
> ImageType::PointType origin;
> origin[0]=0;origin[1]=0;origin[2]=0;
> imageFilter->SetOrigin(origin);
> const PixelType outValue = 0;
> const PixelType inValue = 1;
> meshSpa->SetDefaultInsideValue( inValue );
> meshSpa->SetDefaultOutsideValue( outValue );
>
> imageFilter->SetUseObjectValue( true );
> imageFilter->SetOutsideValue( airHounsfieldUnits );
>
> // Write the image
> typedef itk::ImageFileWriter< ImageType > WriterType;
> WriterType::Pointer writer = WriterType::New();
>
> writer->SetFileName("Output.nii");
> writer->SetInput( imageFilter->GetOutput() );
> try
> {
> imageFilter->Update();
> writer->Update();
> }
> catch( itk::ExceptionObject & excp )
> {
> std::cerr << excp << std::endl;
> return EXIT_FAILURE;
> }
>
>
>
> return EXIT_SUCCESS;
> }
> /////////
> ///code ends
> /////
>
> Thanks,
>
>
> On Thu, Apr 8, 2010 at 2:51 PM, Luis Ibanez <luis.ibanez at kitware.com>wrote:
>
>> Hi Somi,
>>
>>
>> Nope, there is no ConeSpatialObject.
>>
>>
>> However, you could easily create this new class by reusing the
>> code of the CylinderSpatialObject.
>>
>> or
>>
>> you could fake one by using the
>>
>> itkTubeSpatialObject
>>
>> with only two points, one of them having Radius == 0.
>>
>>
>> Regards,
>>
>>
>> Luis
>>
>>
>> -------------------------------------------------------------
>> On Mon, Apr 5, 2010 at 1:56 PM, somi <seesomi at gmail.com> wrote:
>> > Hi,
>> > Does a cone spatial object exist in ITK ?
>> > In my application a user interactively places a cone over an ITK image
>> > volume.
>> >
>> > I then have to voxelize the cone. However volexiling in VTK takes a lot
>> of
>> > time (I tried vtkImplicitModeller,vtkSelectEnclosedPoints they were too
>> > slow).
>> > So is it possible to pass the parameters of the cone to itk and generate
>> it
>> > using some spatial object ?
>> >
>> > Regards,
>> > Somesh
>> >
>> >
>> > _____________________________________
>> > Powered by www.kitware.com
>> >
>> > Visit other Kitware open-source projects at
>> > http://www.kitware.com/opensource/opensource.html
>> >
>> > Kitware offers ITK Training Courses, for more information visit:
>> > http://www.kitware.com/products/protraining.html
>> >
>> > Please keep messages on-topic and check the ITK FAQ at:
>> > http://www.itk.org/Wiki/ITK_FAQ
>> >
>> > Follow this link to subscribe/unsubscribe:
>> > http://www.itk.org/mailman/listinfo/insight-users
>> >
>> >
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20100409/71c2f581/attachment-0001.htm>
More information about the Insight-users
mailing list