Skip to content

Commit

Permalink
Added reading and writing linear object collections to/from file.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewsholden committed Jan 10, 2014
1 parent 59db0c5 commit c338015
Show file tree
Hide file tree
Showing 16 changed files with 548 additions and 81 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ set(MODULE_SRCS
qSlicer${MODULE_NAME}Module.h
qSlicer${MODULE_NAME}ModuleWidget.cxx
qSlicer${MODULE_NAME}ModuleWidget.h
qSlicer${MODULE_NAME}IO.cxx
qSlicer${MODULE_NAME}IO.h
)

set(MODULE_MOC_SRCS
qSlicer${MODULE_NAME}Module.h
qSlicer${MODULE_NAME}ModuleWidget.h
qSlicer${MODULE_NAME}IO.h
)

set(MODULE_UI_SRCS
Expand Down
2 changes: 2 additions & 0 deletions MRML/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ set(${KIT}_SRCS
vtkMRMLLORPlaneNode.h
vtkMRMLLORLinearObjectCollectionNode.cxx
vtkMRMLLORLinearObjectCollectionNode.h
vtkMRMLLORLinearObjectCollectionStorageNode.cxx
vtkMRMLLORLinearObjectCollectionStorageNode.h
vtkMRMLLORLinearObjectNode.cxx
vtkMRMLLORLinearObjectNode.h
vtkMRMLLORLineNode.cxx
Expand Down
43 changes: 35 additions & 8 deletions MRML/vtkMRMLLORLineNode.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,16 @@ ::ToXMLString()
{
std::stringstream xmlstring;

xmlstring << " <Line";
xmlstring << " Name=\"" << this->Name << "\"";
xmlstring << " BasePoint=\"" << vtkMRMLLORVectorMath::VectorToString( this->BasePoint ) << "\"";
xmlstring << " EndPoint=\"" << vtkMRMLLORVectorMath::VectorToString( this->EndPoint ) << "\"";
xmlstring << " />" << std::endl;
xmlstring << " <Line Name=\"" << this->Name << "\">" << std::endl;
xmlstring << " <BasePoint Value=\"" << vtkMRMLLORVectorMath::VectorToString( this->BasePoint ) << "\"/>" << std::endl;
xmlstring << " <EndPoint Value=\"" << vtkMRMLLORVectorMath::VectorToString( this->EndPoint ) << "\"/>" << std::endl;

if ( this->GetPositionBuffer() != NULL )
{
xmlstring << this->GetPositionBuffer()->ToXMLString();
}

xmlstring << " </Line>" << std::endl;

return xmlstring.str();
}
Expand All @@ -140,11 +145,33 @@ ::FromXMLElement( vtkSmartPointer< vtkXMLDataElement > element )

if ( strcmp( element->GetName(), "Line" ) != 0 )
{
return; // If it's not a "log" or is the wrong tool jump to the next.
return; // If it's not a "Line" jump to the next.
}

this->Name = std::string( element->GetAttribute( "Name" ) );
this->BasePoint = vtkMRMLLORVectorMath::StringToVector( std::string( element->GetAttribute( "BasePoint" ) ), 3 );
this->EndPoint = vtkMRMLLORVectorMath::StringToVector( std::string( element->GetAttribute( "EndPoint" ) ), 3 );

int numElements = element->GetNumberOfNestedElements();

for ( int i = 0; i < numElements; i++ )
{

vtkSmartPointer< vtkXMLDataElement > noteElement = element->GetNestedElement( i );

if ( strcmp( noteElement->GetName(), "BasePoint" ) == 0 )
{
this->BasePoint = vtkMRMLLORVectorMath::StringToVector( std::string( noteElement->GetAttribute( "Value" ) ), vtkMRMLLORLinearObjectNode::DIMENSION );
}
if ( strcmp( noteElement->GetName(), "EndPoint" ) == 0 )
{
this->EndPoint = vtkMRMLLORVectorMath::StringToVector( std::string( noteElement->GetAttribute( "Value" ) ), vtkMRMLLORLinearObjectNode::DIMENSION );
}
if ( strcmp( noteElement->GetName(), "Buffer" ) == 0 )
{
vtkMRMLLORPositionBufferNode* bufferNode = vtkMRMLLORPositionBufferNode::New();
bufferNode->FromXMLElement( noteElement );
this->SetPositionBuffer( bufferNode );
}

}

}
6 changes: 3 additions & 3 deletions MRML/vtkMRMLLORLinearObjectCollectionNode.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,12 @@ ::ToXMLString()
{
std::stringstream xmlstring;

xmlstring << "<Geometry>" << std::endl;
xmlstring << "<LinearObjectCollection>" << std::endl;
for ( int i = 0; i < this->Size(); i++ )
{
xmlstring << this->GetLinearObject(i)->ToXMLString();
xmlstring << this->GetLinearObject( i )->ToXMLString();
}
xmlstring << "</Geometry>";
xmlstring << "</LinearObjectCollection>";

return xmlstring.str();
}
Expand Down
13 changes: 10 additions & 3 deletions MRML/vtkMRMLLORLinearObjectCollectionNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
// VTK includes
#include "vtkSmartPointer.h"
#include "vtkObjectFactory.h"
#include "vtkXMLDataElement.h"
#include "vtkXMLDataParser.h"

#include "vtkMRMLNode.h"
#include "vtkMRMLStorableNode.h"

// VNL includes
#include "vnl/vnl_matrix.h"
#include "vnl/algo/vnl_matrix_inverse.h"

// LinearObjectRegistration includes
#include "vtkMRMLLORLinearObjectCollectionStorageNode.h"
#include "vtkMRMLLORLinearObjectNode.h"
#include "vtkMRMLLORReferenceNode.h"
#include "vtkMRMLLORPointNode.h"
Expand All @@ -29,10 +31,10 @@

// This class stores a vector of values and a string label
class VTK_SLICER_LINEAROBJECTREGISTRATION_MODULE_MRML_EXPORT
vtkMRMLLORLinearObjectCollectionNode : public vtkMRMLNode
vtkMRMLLORLinearObjectCollectionNode : public vtkMRMLStorableNode
{
public:
vtkTypeMacro( vtkMRMLLORLinearObjectCollectionNode, vtkMRMLNode );
vtkTypeMacro( vtkMRMLLORLinearObjectCollectionNode, vtkMRMLStorableNode );

// Standard MRML node methods
static vtkMRMLLORLinearObjectCollectionNode *New();
Expand All @@ -44,6 +46,11 @@ vtkMRMLLORLinearObjectCollectionNode : public vtkMRMLNode
virtual void WriteXML( ostream& of, int indent );
virtual void Copy( vtkMRMLNode *node );

// To use the storage node
virtual vtkMRMLStorageNode* CreateDefaultStorageNode() { return vtkMRMLLORLinearObjectCollectionStorageNode::New(); };
bool GetModifiedSinceRead() { return ( this->GetMTime() > this->GetStoredTime() ); };
virtual void UpdateScene( vtkMRMLScene *scene ) { Superclass::UpdateScene(scene); };

protected:

// Constructor/desctructor methods
Expand Down
149 changes: 149 additions & 0 deletions MRML/vtkMRMLLORLinearObjectCollectionStorageNode.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*=Auto=========================================================================
Portions (c) Copyright 2005 Brigham and Women's Hospital (BWH) All Rights Reserved.
See COPYRIGHT.txt
or http://www.slicer.org/copyright/copyright.txt for details.
Program: 3D Slicer
Module: $RCSfile: vtkMRMLTransformStorageNode.cxx,v $
Date: $Date: 2006/03/17 15:10:09 $
Version: $Revision: 1.2 $
=========================================================================auto=*/

#include "vtkMRMLLORLinearObjectCollectionStorageNode.h"
#include "vtkMRMLLORLinearObjectCollectionNode.h"


// Standard MRML Node Methods ------------------------------------------------------------

vtkMRMLLORLinearObjectCollectionStorageNode* vtkMRMLLORLinearObjectCollectionStorageNode
::New()
{
// First try to create the object from the vtkObjectFactory
vtkObject* ret = vtkObjectFactory::CreateInstance( "vtkMRMLLORLinearObjectCollectionStorageNode" );
if( ret )
{
return ( vtkMRMLLORLinearObjectCollectionStorageNode* )ret;
}
// If the factory was unable to create the object, then create it here.
return new vtkMRMLLORLinearObjectCollectionStorageNode();
}


vtkMRMLNode* vtkMRMLLORLinearObjectCollectionStorageNode
::CreateNodeInstance()
{
// First try to create the object from the vtkObjectFactory
vtkObject* ret = vtkObjectFactory::CreateInstance( "vtkMRMLLORLinearObjectCollectionStorageNode" );
if( ret )
{
return ( vtkMRMLLORLinearObjectCollectionStorageNode* )ret;
}
// If the factory was unable to create the object, then create it here.
return new vtkMRMLLORLinearObjectCollectionStorageNode();
}



void vtkMRMLLORLinearObjectCollectionStorageNode
::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}


// Constructors and Destructors --------------------------------------------------------------------

vtkMRMLLORLinearObjectCollectionStorageNode
::vtkMRMLLORLinearObjectCollectionStorageNode()
{
}


vtkMRMLLORLinearObjectCollectionStorageNode
::~vtkMRMLLORLinearObjectCollectionStorageNode()
{
}

// Storage node specific methods ----------------------------------------------------------------------------
bool vtkMRMLLORLinearObjectCollectionStorageNode
::CanReadInReferenceNode(vtkMRMLNode *refNode)
{
return refNode->IsA( "vtkMRMLLORLinearObjectCollectionNode" );
}


void vtkMRMLLORLinearObjectCollectionStorageNode
::InitializeSupportedWriteFileTypes()
{
this->SupportedWriteFileTypes->InsertNextValue("Linear Object Collection (.xml)");
}


const char* vtkMRMLLORLinearObjectCollectionStorageNode
::GetDefaultWriteFileExtension()
{
return "xml";
}



// Read and Write methods ----------------------------------------------------------------------------
int vtkMRMLLORLinearObjectCollectionStorageNode
::ReadDataInternal(vtkMRMLNode *refNode)
{
vtkMRMLLORLinearObjectCollectionNode* collectionNode = vtkMRMLLORLinearObjectCollectionNode::SafeDownCast( refNode );

std::string fullName = this->GetFullNameFromFileName();
if ( fullName == std::string( "" ) )
{
vtkErrorMacro("vtkMRMLLORLinearObjectCollectionNode: File name not specified");
return 0;
}

// Clear the current buffer prior to importing
collectionNode->Clear();

vtkXMLDataParser* parser = vtkXMLDataParser::New();
parser->SetFileName( fullName.c_str() );
parser->Parse();

collectionNode->FromXMLElement( parser->GetRootElement() );

// The buffer name should already be specified
// The scene should already be populated with the desired transforms

parser->Delete();

return 1;
}

//----------------------------------------------------------------------------
int vtkMRMLLORLinearObjectCollectionStorageNode
::WriteDataInternal(vtkMRMLNode *refNode)
{
vtkMRMLLORLinearObjectCollectionNode* collectionNode = vtkMRMLLORLinearObjectCollectionNode::SafeDownCast( refNode );

std::string fullName = this->GetFullNameFromFileName();
if ( fullName == std::string( "" ) )
{
vtkErrorMacro("vtkMRMLLORLinearObjectCollectionNode: File name not specified");
return 0;
}

std::ofstream output( fullName.c_str() );

if ( ! output.is_open() )
{
vtkErrorMacro( "Record file could not be opened!" );
return 0;
}

output << collectionNode->ToXMLString();

output.close();

return 1;
}
71 changes: 71 additions & 0 deletions MRML/vtkMRMLLORLinearObjectCollectionStorageNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*=auto=========================================================================
Portions (c) Copyright 2005 Brigham and Women's Hospital (BWH) All Rights Reserved.
See COPYRIGHT.txt
or http://www.slicer.org/copyright/copyright.txt for details.
Program: 3D Slicer
Module: $RCSfile: vtkMRMLLORLinearObjectCollectionStorageNode.h,v $
Date: $Date: 2006/03/19 17:12:29 $
Version: $Revision: 1.3 $
=========================================================================auto=*/

#ifndef __vtkMRMLLORLinearObjectCollectionStorageNode_h
#define __vtkMRMLLORLinearObjectCollectionStorageNode_h

// Standard includes
#include <ctime>
#include <iostream>
#include <sstream>
#include <utility>
#include <vector>

//VTK includes
#include "vtkMRMLStorageNode.h"
#include "vtkStringArray.h"

// TransformRecorder includes
#include "vtkSlicerLinearObjectRegistrationModuleMRMLExport.h"


/// Storage nodes has methods to read/write transform bufferss to/from disk.
class VTK_SLICER_LINEAROBJECTREGISTRATION_MODULE_MRML_EXPORT
vtkMRMLLORLinearObjectCollectionStorageNode : public vtkMRMLStorageNode
{
public:
vtkTypeMacro( vtkMRMLLORLinearObjectCollectionStorageNode, vtkMRMLStorageNode );

// Standard MRML node methods
static vtkMRMLLORLinearObjectCollectionStorageNode* New();
virtual vtkMRMLNode* CreateNodeInstance();
virtual const char* GetNodeTagName() { return "LORLinearObjectCollectionStorage"; };
void PrintSelf(ostream& os, vtkIndent indent);
// No need for special read/write/copy

// Initialize all the supported write file types
virtual void InitializeSupportedWriteFileTypes();
// Return a default file extension for writing
virtual const char* GetDefaultWriteFileExtension();

/// Support only linear object registration nodes
virtual bool CanReadInReferenceNode(vtkMRMLNode* refNode);

protected:
// Constructor/deconstructor
vtkMRMLLORLinearObjectCollectionStorageNode();
~vtkMRMLLORLinearObjectCollectionStorageNode();
vtkMRMLLORLinearObjectCollectionStorageNode(const vtkMRMLLORLinearObjectCollectionStorageNode&);
void operator=(const vtkMRMLLORLinearObjectCollectionStorageNode&);


/// Read data and set it in the referenced node
virtual int ReadDataInternal(vtkMRMLNode *refNode);

/// Write data from a referenced node
virtual int WriteDataInternal(vtkMRMLNode *refNode);

};

#endif
Loading

0 comments on commit c338015

Please sign in to comment.