C:/cmcintos/defOrgs/examples/DefOrgViewerWithKW/Source/DefOrgViewerAdapterBaseTemplated.cxx

00001 #include "DefOrgViewerAdapterBaseTemplated.h"
00002 namespace mial{
00003 //explicit instantiations
00004 template class DefOrgViewerAdapterBaseTemplated<unsigned char, float>;
00005 
00006 template <class ITKPIXELTYPE, class MESHDATATYPE>
00007 void DefOrgViewerAdapterBaseTemplated<ITKPIXELTYPE,MESHDATATYPE>::PopulateVtkImageHelper(ImageTypePointer itkImage, vtkImageImport* vtkImporter){
00008         itkImageExportTypePointer itkExporter = itkImageExportType::New();
00009         m_itkImageExportPointerHolder.push_back(itkExporter);
00010         itkExporter->SetInput( itkImage );
00011         itkExporter->Update();
00012         ConnectPipelines( itkExporter, vtkImporter );
00013         vtkImporter->Update();
00014 }
00015 
00016 
00017 template <class ITKPIXELTYPE, class MESHDATATYPE>
00018 void DefOrgViewerAdapterBaseTemplated<ITKPIXELTYPE,MESHDATATYPE>::ConnectPipelines( typename itkImageExportType::Pointer exporter, 
00019                                    vtkImageImport* importer)
00020 {
00021         importer->SetUpdateInformationCallback(exporter->GetUpdateInformationCallback());
00022         importer->SetPipelineModifiedCallback(exporter->GetPipelineModifiedCallback());
00023         importer->SetWholeExtentCallback(exporter->GetWholeExtentCallback());
00024         importer->SetSpacingCallback(exporter->GetSpacingCallback());
00025         importer->SetOriginCallback(exporter->GetOriginCallback());
00026         importer->SetScalarTypeCallback(exporter->GetScalarTypeCallback());
00027         importer->SetNumberOfComponentsCallback(exporter->GetNumberOfComponentsCallback());
00028         importer->SetPropagateUpdateExtentCallback(exporter->GetPropagateUpdateExtentCallback());
00029         importer->SetUpdateDataCallback(exporter->GetUpdateDataCallback());
00030         importer->SetDataExtentCallback(exporter->GetDataExtentCallback());
00031         importer->SetBufferPointerCallback(exporter->GetBufferPointerCallback());
00032         importer->SetCallbackUserData(exporter->GetCallbackUserData());
00033 }
00034 
00035 template <class ITKPIXELTYPE, class MESHDATATYPE>
00036 void DefOrgViewerAdapterBaseTemplated<ITKPIXELTYPE,MESHDATATYPE>::MeshToUnstructuredGrid( MeshTypePointer mesh, vtkUnstructuredGrid* vgrid )
00037 {
00038         // Get the number of points in the mesh
00039         int numPoints = mesh->GetNumberOfPoints();
00040         if(numPoints == 0)
00041         {
00042                 mesh->Print(std::cerr);
00043                 std::cerr << "no points in Grid " << std::endl;
00044         }
00045 
00046         // Create the vtkPoints object and set the number of points
00047         vtkPoints* vpoints = vtkPoints::New();
00048         vpoints->SetNumberOfPoints(numPoints);
00049         // iterate over all the points in the itk mesh filling in
00050         // the vtkPoints object as we go
00051         MeshType::PointsContainer::Pointer points = mesh->GetPoints();
00052         for(MeshType::PointsContainer::Iterator i = points->Begin();
00053                 i != points->End(); ++i)
00054         {
00055                 // Get the point index from the point container iterator
00056                 int idx = i->Index();
00057                 // Set the vtk point at the index with the the coord array from itk
00058                 // itk returns a const pointer, but vtk is not const correct, so
00059                 // we have to use a const cast to get rid of the const
00060                 vpoints->SetPoint(idx, const_cast<DataType*>(i->Value().GetDataPointer()));
00061         }
00062         // Set the points on the vtk grid
00063         vgrid->SetPoints(vpoints);
00064 
00065         // Now create the cells using the MulitVisitor
00066         // 1. Create a MultiVisitor
00067         MeshType::CellType::MultiVisitor::Pointer mv =
00068                 MeshType::CellType::MultiVisitor::New();
00069         // 2. Create a triangle and quadrilateral visitor
00070         TriangleVisitor::Pointer tv = TriangleVisitor::New();
00071         QuadrilateralVisitor::Pointer qv =  QuadrilateralVisitor::New();
00072         // 3. Set up the visitors
00073         int vtkCellCount = 0; // running counter for current cell being inserted into vtk
00074         int numCells = mesh->GetNumberOfCells();
00075         int *types = new int[numCells]; // type array for vtk 
00076         // create vtk cells and estimate the size
00077         vtkCellArray* cells = vtkCellArray::New();
00078         cells->EstimateSize(numCells, 4);
00079         // Set the TypeArray CellCount and CellArray for both visitors
00080         tv->SetTypeArray(types);
00081         tv->SetCellCounter(&vtkCellCount);
00082         tv->SetCellArray(cells);
00083         qv->SetTypeArray(types);
00084         qv->SetCellCounter(&vtkCellCount);
00085         qv->SetCellArray(cells);
00086         // add the visitors to the multivisitor
00087         mv->AddVisitor(tv);
00088         mv->AddVisitor(qv);
00089         // Now ask the mesh to accept the multivisitor which
00090         // will Call Visit for each cell in the mesh that matches the
00091         // cell types of the visitors added to the MultiVisitor
00092         mesh->Accept(mv);
00093 
00094         // Now set the cells on the vtk grid with the type array and cell array
00095         vgrid->SetCells(types, cells);
00096 
00097         // Clean up vtk objects (no vtkSmartPointer ... )
00098         cells->Delete();
00099         vpoints->Delete();
00100 
00101 }
00102 }

Generated on Wed Jul 19 13:05:16 2006 for IDO by  doxygen 1.4.7