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

00001 #include "DefOrgViewerAdapter.h"
00002 
00003 DefOrgViewerAdapter::DefOrgViewerAdapter(){
00004         geomLayerPointer = new GeometricType();
00005 }
00006 
00007 void DefOrgViewerAdapter::PopulateItkScene(itkScenePointer itkScene /*in/out*/){
00008         geomLayerPointer->readTopologyFromFile( m_MeshFileName );
00009         itkScene->AddSpatialObject(geomLayerPointer->theMeshSpatialObject);  
00010 }
00011 
00012 void DefOrgViewerAdapter::UpdateOrganism(itkScenePointer itkScene /*in/out*/){
00013         testOrg->run();
00014         itkScene->AddSpatialObject(geomLayerPointer->theMeshSpatialObject);  
00015 }
00016 
00017 void DefOrgViewerAdapter::PopulateVtkImage(vtkImageImport* vtkImporter){
00018         m_InputImageReader = ImageFileReader::New();
00019         m_InputImageReader->SetFileName(m_ImageFileName.c_str());
00020         
00021         m_InputImageReader->Update();
00022         ImageType::Pointer image = m_InputImageReader->GetOutput();     
00023         m_ItkExporter = itkImageExportType::New();
00024         m_ItkExporter->SetInput( image );
00025         m_ItkExporter->Update();
00026         ConnectPipelines( m_ItkExporter, vtkImporter );
00027         vtkImporter->Update();
00028 }
00029 
00030 void DefOrgViewerAdapter::ConnectPipelines( itkImageExportType::Pointer exporter, 
00031                                    vtkImageImport* importer)
00032 {
00033         importer->SetUpdateInformationCallback(exporter->GetUpdateInformationCallback());
00034         importer->SetPipelineModifiedCallback(exporter->GetPipelineModifiedCallback());
00035         importer->SetWholeExtentCallback(exporter->GetWholeExtentCallback());
00036         importer->SetSpacingCallback(exporter->GetSpacingCallback());
00037         importer->SetOriginCallback(exporter->GetOriginCallback());
00038         importer->SetScalarTypeCallback(exporter->GetScalarTypeCallback());
00039         importer->SetNumberOfComponentsCallback(exporter->GetNumberOfComponentsCallback());
00040         importer->SetPropagateUpdateExtentCallback(exporter->GetPropagateUpdateExtentCallback());
00041         importer->SetUpdateDataCallback(exporter->GetUpdateDataCallback());
00042         importer->SetDataExtentCallback(exporter->GetDataExtentCallback());
00043         importer->SetBufferPointerCallback(exporter->GetBufferPointerCallback());
00044         importer->SetCallbackUserData(exporter->GetCallbackUserData());
00045 }
00046 
00047 bool DefOrgViewerAdapter::IsAllInputFilesSet(){
00048         return 
00049         (vtksys::SystemTools::FileExists(m_ImageFileName.c_str()) &&
00050         vtksys::SystemTools::FileExists(m_ScheduleFileName.c_str()) &&
00051         vtksys::SystemTools::FileExists(m_MeshFileName.c_str()) 
00052         );
00053 }
00054 
00055 
00056 void DefOrgViewerAdapter::SetupOrganism()
00057 {
00058         //TODO:: make this robust (i.e. flag errors if image not set, geom not set etc).
00059         
00060         // Instantiate the organism:
00061         testOrg = OrganismType::New();
00062         std::cout << "Organism created..." << std::endl;
00063 
00064         //Setup the sensor
00065         GradientSensorType::sensorIn input;
00066                 
00067         input.sigma = 1.0;
00068         this->m_InputImageReader->Update();
00069         input.imageIn = this->m_InputImageReader->GetOutput();
00070         gradientSensor.run((void *)&input);
00071         std::cout << "Gradient sensor setup complete..." << std::endl;
00072         GradientSensorType::sensorOut * output = (GradientSensorType::sensorOut *) gradientSensor.getOuput();
00073 
00074 
00075         //Instantiate geomtery and physics layers
00076         eulerPhysLayerPointer = new EulerPhysicsType(0,0,10);   
00077                         
00078         //physLayer.setInput(reader->GetOutput());
00079         eulerPhysLayerPointer->setExternalForces((void *) &(output->imageOut));
00080         eulerPhysLayerPointer->setGeometry(geomLayerPointer);
00081 
00082         testOrg->setPhysicsLayer(eulerPhysLayerPointer);
00083         testOrg->setGeometricLayer(geomLayerPointer);
00084         std::cout << "Physics layer added..." << std::endl;
00085 
00086         //TEST BEHAVIOR
00087         //TODO figure out how to make sure it gets deleted.
00088         //TODO should we attach things by value instead by ref?
00089         cognitiveLayerPointer = new CognitiveType(5);
00090         beh1 = new Beh_TranslateAllType();
00091         def1 = new Def_TranslateAllType();
00092         cognitiveLayerPointer->setSchedule(m_ScheduleFileName);
00093         //Beh_TranslateAll<float, 3> beh1;
00094         //Def_Translation<float, 3> def1;                                                               
00095         this->testOrg->setCognitiveLayer(cognitiveLayerPointer);
00096         this->testOrg->addBehaviour(beh1);
00097         this->testOrg->addDeformation(def1);
00098 
00099         //Pass the output of the reader to the filter, then to the writer.
00100         testOrg->SetInput(m_InputImageReader->GetOutput());
00101 }
00102 
00103 void DefOrgViewerAdapter::PopulateVtkUnstructuredGrid(vtkUnstructuredGrid* vtkGrid /*in/out*/){
00104         geomLayerPointer->readTopologyFromFile( m_MeshFileName );
00105         MeshToUnstructuredGrid( geomLayerPointer->theMesh, vtkGrid/*in/out*/ );
00106 }
00107 
00108 
00109 void DefOrgViewerAdapter::MeshToUnstructuredGrid( MeshTypePointer mesh, vtkUnstructuredGrid* vgrid )
00110 {
00111         // Get the number of points in the mesh
00112         int numPoints = mesh->GetNumberOfPoints();
00113         if(numPoints == 0)
00114         {
00115                 mesh->Print(std::cerr);
00116                 std::cerr << "no points in Grid " << std::endl;
00117         }
00118 
00119         // Create the vtkPoints object and set the number of points
00120         vtkPoints* vpoints = vtkPoints::New();
00121         vpoints->SetNumberOfPoints(numPoints);
00122         // iterate over all the points in the itk mesh filling in
00123         // the vtkPoints object as we go
00124         MeshType::PointsContainer::Pointer points = mesh->GetPoints();
00125         for(MeshType::PointsContainer::Iterator i = points->Begin();
00126                 i != points->End(); ++i)
00127         {
00128                 // Get the point index from the point container iterator
00129                 int idx = i->Index();
00130                 // Set the vtk point at the index with the the coord array from itk
00131                 // itk returns a const pointer, but vtk is not const correct, so
00132                 // we have to use a const cast to get rid of the const
00133                 vpoints->SetPoint(idx, const_cast<DataType*>(i->Value().GetDataPointer()));
00134         }
00135         // Set the points on the vtk grid
00136         vgrid->SetPoints(vpoints);
00137 
00138         // Now create the cells using the MulitVisitor
00139         // 1. Create a MultiVisitor
00140         MeshType::CellType::MultiVisitor::Pointer mv =
00141                 MeshType::CellType::MultiVisitor::New();
00142         // 2. Create a triangle and quadrilateral visitor
00143         TriangleVisitor::Pointer tv = TriangleVisitor::New();
00144         QuadrilateralVisitor::Pointer qv =  QuadrilateralVisitor::New();
00145         // 3. Set up the visitors
00146         int vtkCellCount = 0; // running counter for current cell being inserted into vtk
00147         int numCells = mesh->GetNumberOfCells();
00148         int *types = new int[numCells]; // type array for vtk 
00149         // create vtk cells and estimate the size
00150         vtkCellArray* cells = vtkCellArray::New();
00151         cells->EstimateSize(numCells, 4);
00152         // Set the TypeArray CellCount and CellArray for both visitors
00153         tv->SetTypeArray(types);
00154         tv->SetCellCounter(&vtkCellCount);
00155         tv->SetCellArray(cells);
00156         qv->SetTypeArray(types);
00157         qv->SetCellCounter(&vtkCellCount);
00158         qv->SetCellArray(cells);
00159         // add the visitors to the multivisitor
00160         mv->AddVisitor(tv);
00161         mv->AddVisitor(qv);
00162         // Now ask the mesh to accept the multivisitor which
00163         // will Call Visit for each cell in the mesh that matches the
00164         // cell types of the visitors added to the MultiVisitor
00165         mesh->Accept(mv);
00166 
00167         // Now set the cells on the vtk grid with the type array and cell array
00168         vgrid->SetCells(types, cells);
00169 
00170         // Clean up vtk objects (no vtkSmartPointer ... )
00171         cells->Delete();
00172         vpoints->Delete();
00173 
00174 }

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