[Insight-users] Reading MeshSpatialObjects from file

Gavin Baker gavinb+xtk at cs.mu.OZ.AU
Tue Aug 2 22:44:36 EDT 2005


Hello Richard,

On Tue, Aug 02, 2005 at 01:16:21PM +0200, Richard van der Put wrote:

> I am trying to read the MeshSpatialObject that I have put in a metafile 
> using the itk::SpatialObjectWriter. When I use:
> 
> reader->GetScene();
> 
> it returns a std::list (in my case it has size 1) of pointers to 
> itk::SpatialObject objects. My question is how to turn this into an 
> object of type itk::MeshSpatialObject so that I can have access to the 
> mesh? When I print the object it tells me it is a MeshSpatialObject, but 
> I do not know how to cast it to this type. I have looked at the guide 
> and tutorials but have not been able to find an answer.

There's a simple example of iterating through child objects in section 5.2
of the software guide (which is the same concept), but not an entire scene.

One way is to use RTTI and dynamically cast the pointer, assuming you need
to take some different action depending on the type.

I've put together a simple example of this which I've attached.  It loads up
an arbitrary meta file, and prints out the number of cells in each mesh
object it finds.  You can easily add other cases to handle other types.

Hope this is useful -

  :: Gavin

-- 
Gavin Baker                                      Complex Systems Group
http://www.cs.mu.oz.au/~gavinb             The University of Melbourne
-------------- next part --------------

#include <itkMesh.h>
#include <itkSpatialObject.h>
#include <itkSpatialObjectReader.h>
#include <itkSceneSpatialObject.h>

typedef itk::Mesh<float>                        MeshType;

typedef itk::SceneSpatialObject<3>              SceneType;
typedef SceneType::SpatialObjectType            SpatialObjectType;
typedef itk::SpatialObjectReader<3, float>      SceneReaderType;
typedef itk::MeshSpatialObject<MeshType>        MeshObjectType;

int main( int argc, char* argv[] )
{
    if ( argc < 2 )
    {
        std::cerr << "Usage: soiter <filename.meta>" << std::endl;
        return 1;
    }

    const char* fileName = argv[1];

    SceneReaderType::Pointer reader = SceneReaderType::New();
    reader->SetFileName( fileName );
    reader->Update();

    SceneType::Pointer scene = reader->GetScene();

    SceneType::ObjectListType* objects = scene->GetObjects();

    std::cout
        << "Loaded a scene with "
        << scene->GetNumberOfObjects()
        << " objects from "
        << fileName
        << std::endl;

    SceneType::ObjectListType::iterator iter = objects->begin();
    SceneType::ObjectListType::iterator end  = objects->end();

    while ( iter != end )
    {
        SpatialObjectType::Pointer obj = *iter;

        if ( typeid( *obj ) == typeid( MeshObjectType ) )
        {
            MeshObjectType* mesh =
                dynamic_cast<MeshObjectType*>(obj.GetPointer());
                
            std::cout << "Mesh: "
                      << "Cells = " << mesh->GetMesh()->GetNumberOfCells()
                      << std::endl;
        }
        else
        {
            std::cout << "Unknown: " << obj << std::endl;
        }

        ++ iter;
    }

    return 0;
}
-------------- next part --------------

PROJECT(soiter)

FIND_PACKAGE(ITK)
IF(ITK_FOUND)
   INCLUDE(${ITK_USE_FILE})
ELSE(ITK_FOUND)
   MESSAGE(FATAL_ERROR "Cannot build without ITK.  Please set ITK_DIR.")
ENDIF(ITK_FOUND)

ADD_EXECUTABLE(soiter soiter.cxx)

TARGET_LINK_LIBRARIES(soiter ITKCommon ITKIO)


More information about the Insight-users mailing list