#include "DefOrgViewerAdapter.h" DefOrgViewerAdapter::DefOrgViewerAdapter(){ geomLayerPointer = new GeometricType(); } void DefOrgViewerAdapter::PopulateItkScene(itkScenePointer itkScene /*in/out*/){ geomLayerPointer->readTopologyFromFile( m_MeshFileName ); itkScene->AddSpatialObject(geomLayerPointer->theMeshSpatialObject); } void DefOrgViewerAdapter::UpdateOrganism(itkScenePointer itkScene /*in/out*/){ testOrg->run(); itkScene->AddSpatialObject(geomLayerPointer->theMeshSpatialObject); } void DefOrgViewerAdapter::PopulateVtkImage(vtkImageImport* vtkImporter){ m_InputImageReader = ImageFileReader::New(); m_InputImageReader->SetFileName(m_ImageFileName.c_str()); m_InputImageReader->Update(); ImageType::Pointer image = m_InputImageReader->GetOutput(); m_ItkExporter = itkImageExportType::New(); m_ItkExporter->SetInput( image ); m_ItkExporter->Update(); ConnectPipelines( m_ItkExporter, vtkImporter ); vtkImporter->Update(); } void DefOrgViewerAdapter::ConnectPipelines( itkImageExportType::Pointer exporter, vtkImageImport* importer) { importer->SetUpdateInformationCallback(exporter->GetUpdateInformationCallback()); importer->SetPipelineModifiedCallback(exporter->GetPipelineModifiedCallback()); importer->SetWholeExtentCallback(exporter->GetWholeExtentCallback()); importer->SetSpacingCallback(exporter->GetSpacingCallback()); importer->SetOriginCallback(exporter->GetOriginCallback()); importer->SetScalarTypeCallback(exporter->GetScalarTypeCallback()); importer->SetNumberOfComponentsCallback(exporter->GetNumberOfComponentsCallback()); importer->SetPropagateUpdateExtentCallback(exporter->GetPropagateUpdateExtentCallback()); importer->SetUpdateDataCallback(exporter->GetUpdateDataCallback()); importer->SetDataExtentCallback(exporter->GetDataExtentCallback()); importer->SetBufferPointerCallback(exporter->GetBufferPointerCallback()); importer->SetCallbackUserData(exporter->GetCallbackUserData()); } bool DefOrgViewerAdapter::IsAllInputFilesSet(){ return (vtksys::SystemTools::FileExists(m_ImageFileName.c_str()) && vtksys::SystemTools::FileExists(m_ScheduleFileName.c_str()) && vtksys::SystemTools::FileExists(m_MeshFileName.c_str()) ); } void DefOrgViewerAdapter::SetupOrganism() { //TODO:: make this robust (i.e. flag errors if image not set, geom not set etc). // Instantiate the organism: testOrg = OrganismType::New(); std::cout << "Organism created..." << std::endl; //Setup the sensor GradientSensorType::sensorIn input; input.sigma = 1.0; this->m_InputImageReader->Update(); input.imageIn = this->m_InputImageReader->GetOutput(); gradientSensor.run((void *)&input); std::cout << "Gradient sensor setup complete..." << std::endl; GradientSensorType::sensorOut * output = (GradientSensorType::sensorOut *) gradientSensor.getOuput(); //Instantiate geomtery and physics layers eulerPhysLayerPointer = new EulerPhysicsType(0,0,10); //physLayer.setInput(reader->GetOutput()); eulerPhysLayerPointer->setExternalForces((void *) &(output->imageOut)); eulerPhysLayerPointer->setGeometry(geomLayerPointer); testOrg->setPhysicsLayer(eulerPhysLayerPointer); testOrg->setGeometricLayer(geomLayerPointer); std::cout << "Physics layer added..." << std::endl; //TEST BEHAVIOR //TODO figure out how to make sure it gets deleted. //TODO should we attach things by value instead by ref? cognitiveLayerPointer = new CognitiveType(5); beh1 = new Beh_TranslateAllType(); def1 = new Def_TranslateAllType(); cognitiveLayerPointer->setSchedule(m_ScheduleFileName); //Beh_TranslateAll beh1; //Def_Translation def1; this->testOrg->setCognitiveLayer(cognitiveLayerPointer); this->testOrg->addBehaviour(beh1); this->testOrg->addDeformation(def1); //Pass the output of the reader to the filter, then to the writer. testOrg->SetInput(m_InputImageReader->GetOutput()); } void DefOrgViewerAdapter::PopulateVtkUnstructuredGrid(vtkUnstructuredGrid* vtkGrid /*in/out*/){ geomLayerPointer->readTopologyFromFile( m_MeshFileName ); MeshToUnstructuredGrid( geomLayerPointer->theMesh, vtkGrid/*in/out*/ ); } void DefOrgViewerAdapter::MeshToUnstructuredGrid( MeshTypePointer mesh, vtkUnstructuredGrid* vgrid ) { // Get the number of points in the mesh int numPoints = mesh->GetNumberOfPoints(); if(numPoints == 0) { mesh->Print(std::cerr); std::cerr << "no points in Grid " << std::endl; } // Create the vtkPoints object and set the number of points vtkPoints* vpoints = vtkPoints::New(); vpoints->SetNumberOfPoints(numPoints); // iterate over all the points in the itk mesh filling in // the vtkPoints object as we go MeshType::PointsContainer::Pointer points = mesh->GetPoints(); for(MeshType::PointsContainer::Iterator i = points->Begin(); i != points->End(); ++i) { // Get the point index from the point container iterator int idx = i->Index(); // Set the vtk point at the index with the the coord array from itk // itk returns a const pointer, but vtk is not const correct, so // we have to use a const cast to get rid of the const vpoints->SetPoint(idx, const_cast(i->Value().GetDataPointer())); } // Set the points on the vtk grid vgrid->SetPoints(vpoints); // Now create the cells using the MulitVisitor // 1. Create a MultiVisitor MeshType::CellType::MultiVisitor::Pointer mv = MeshType::CellType::MultiVisitor::New(); // 2. Create a triangle and quadrilateral visitor TriangleVisitor::Pointer tv = TriangleVisitor::New(); QuadrilateralVisitor::Pointer qv = QuadrilateralVisitor::New(); // 3. Set up the visitors int vtkCellCount = 0; // running counter for current cell being inserted into vtk int numCells = mesh->GetNumberOfCells(); int *types = new int[numCells]; // type array for vtk // create vtk cells and estimate the size vtkCellArray* cells = vtkCellArray::New(); cells->EstimateSize(numCells, 4); // Set the TypeArray CellCount and CellArray for both visitors tv->SetTypeArray(types); tv->SetCellCounter(&vtkCellCount); tv->SetCellArray(cells); qv->SetTypeArray(types); qv->SetCellCounter(&vtkCellCount); qv->SetCellArray(cells); // add the visitors to the multivisitor mv->AddVisitor(tv); mv->AddVisitor(qv); // Now ask the mesh to accept the multivisitor which // will Call Visit for each cell in the mesh that matches the // cell types of the visitors added to the MultiVisitor mesh->Accept(mv); // Now set the cells on the vtk grid with the type array and cell array vgrid->SetCells(types, cells); // Clean up vtk objects (no vtkSmartPointer ... ) cells->Delete(); vpoints->Delete(); }