]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/Algorithms/PolyLineParametricPathWriter.hxx
yet another refactoring
[cpPlugins.git] / lib / cpExtensions / Algorithms / PolyLineParametricPathWriter.hxx
diff --git a/lib/cpExtensions/Algorithms/PolyLineParametricPathWriter.hxx b/lib/cpExtensions/Algorithms/PolyLineParametricPathWriter.hxx
new file mode 100644 (file)
index 0000000..8e9db8e
--- /dev/null
@@ -0,0 +1,128 @@
+// -------------------------------------------------------------------------
+// @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
+// -------------------------------------------------------------------------
+
+#ifndef __cpExtensions__Algorithms__PolyLineParametricPathWriter__hxx__
+#define __cpExtensions__Algorithms__PolyLineParametricPathWriter__hxx__
+
+#include <sstream>
+
+// -------------------------------------------------------------------------
+template< class _TPolyLine >
+void cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine >::
+SetInput( const _TPolyLine* input )
+{
+  this->itk::ProcessObject::SetNthInput(
+    0, const_cast< _TPolyLine* >( input )
+    );
+}
+
+// -------------------------------------------------------------------------
+template< class _TPolyLine >
+const _TPolyLine*
+cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine >::
+GetInput( )
+{
+  return(
+    dynamic_cast< const _TPolyLine* >(
+      this->itk::ProcessObject::GetInput( 0 )
+      )
+    );
+}
+
+// -------------------------------------------------------------------------
+template< class _TPolyLine >
+void cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine >::
+Update( )
+{
+  _TPolyLine* input = const_cast< _TPolyLine* >( this->GetInput( ) );
+  if( input != NULL )
+  {
+    input->UpdateOutputInformation( );
+    input->UpdateOutputData( );
+    this->GenerateData( );
+    this->ReleaseInputs( );
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+template< class _TPolyLine >
+cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine >::
+PolyLineParametricPathWriter( )
+  : Superclass( ),
+    m_FileName( "" ),
+    m_NumberOfPoints( 100 )
+{
+  this->SetNumberOfRequiredInputs( 1 );
+}
+
+// -------------------------------------------------------------------------
+template< class _TPolyLine >
+cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine >::
+~PolyLineParametricPathWriter( )
+{
+}
+
+// -------------------------------------------------------------------------
+template< class _TPolyLine >
+void cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine >::
+GenerateData( )
+{
+  typedef typename _TPolyLine::TContinuousIndex _TContinuousIndex;
+
+  // "Serialize" data
+  const _TPolyLine* input = this->GetInput( );
+  unsigned int dim = _TPolyLine::PathDimension;
+  std::stringstream buffer;
+  long step = input->GetSize( ) / this->m_NumberOfPoints;
+
+  for( unsigned long i = 0; i < input->GetSize( ); i += step )
+  {
+    _TContinuousIndex idx;
+    idx.Fill( 0 );
+    int c = 0;
+    for( long j = -step; j <= step; ++j )
+    {
+      long k = i + j;
+      if( k >= 0 && k < input->GetSize( ) )
+      {
+        _TContinuousIndex kdx = input->GetContinuousVertex( k );
+        for( unsigned int d = 0; d < dim; ++d )
+          idx[ d ] += kdx[ d ];
+        c++;
+
+      } // fi
+
+    } // rof
+    if( c != 0 )
+      for( unsigned int d = 0; d < dim; ++d )
+        idx[ d ] /= c;
+
+    buffer << idx[ 0 ];
+    for( unsigned int d = 1; d < dim; ++d )
+      buffer << " " << idx[ d ];
+    buffer << std::endl;
+
+  } // rof
+
+  // Real write
+  std::ofstream file_stream(
+    this->m_FileName.c_str( ), std::ofstream::binary
+    );
+  if( !file_stream )
+  {
+    itkExceptionMacro(
+      << "Could not open file \"" << this->m_FileName << "\" to write a "
+      << "cpExtensions::DataStructures::PolyLineParametricPath< " << dim
+      << " > object."
+      );
+    return;
+
+  } // fi
+  file_stream.write( buffer.str( ).c_str( ), buffer.str( ).size( ) );
+}
+
+#endif // __cpExtensions__Algorithms__PolyLineParametricPathWriter__hxx__
+
+// eof - $RCSfile$