diff --git a/Modules/Simulation/files.cmake b/Modules/Simulation/files.cmake index 1701fd9b05..92064e233b 100644 --- a/Modules/Simulation/files.cmake +++ b/Modules/Simulation/files.cmake @@ -1,12 +1,10 @@ set(CPP_FILES mitkSimulation.cpp mitkSimulationDrawTool.cpp mitkSimulationPropAssemblyVisitor.cpp mitkSimulationIOFactory.cpp mitkSimulationMapper3D.cpp mitkSimulationReader.cpp mitkSimulationObjectFactory.cpp - mitkSimulationLight.cpp - mitkSimulationLightManager.cpp mitkSimulationModel.cpp ) diff --git a/Modules/Simulation/mitkSimulationLight.cpp b/Modules/Simulation/mitkSimulationLight.cpp deleted file mode 100644 index 83c40696d1..0000000000 --- a/Modules/Simulation/mitkSimulationLight.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/*=================================================================== - -The Medical Imaging Interaction Toolkit (MITK) - -Copyright (c) German Cancer Research Center, -Division of Medical and Biological Informatics. -All rights reserved. - -This software is distributed WITHOUT ANY WARRANTY; without -even the implied warranty of MERCHANTABILITY or FITNESS FOR -A PARTICULAR PURPOSE. - -See LICENSE.txt or http://www.mitk.org for details. - -===================================================================*/ - -#include "mitkSimulationLight.h" - -mitk::SimulationLight::SimulationLight() -{ -} - -mitk::SimulationLight::~SimulationLight() -{ -} diff --git a/Modules/Simulation/mitkSimulationLight.h b/Modules/Simulation/mitkSimulationLight.h deleted file mode 100644 index fce94c7e30..0000000000 --- a/Modules/Simulation/mitkSimulationLight.h +++ /dev/null @@ -1,39 +0,0 @@ -/*=================================================================== - -The Medical Imaging Interaction Toolkit (MITK) - -Copyright (c) German Cancer Research Center, -Division of Medical and Biological Informatics. -All rights reserved. - -This software is distributed WITHOUT ANY WARRANTY; without -even the implied warranty of MERCHANTABILITY or FITNESS FOR -A PARTICULAR PURPOSE. - -See LICENSE.txt or http://www.mitk.org for details. - -===================================================================*/ - -#ifndef mitkSimulationLight_h -#define mitkSimulationLight_h - -#include -#include - -namespace mitk -{ - class Simulation_EXPORT SimulationLight : public sofa::core::visual::VisualModel - { - public: - SOFA_CLASS(SimulationLight, sofa::core::visual::VisualModel); - - private: - SimulationLight(); - ~SimulationLight(); - - SimulationLight(const MyType&); - MyType& operator=(const MyType&); - }; -} - -#endif diff --git a/Modules/Simulation/mitkSimulationLightManager.cpp b/Modules/Simulation/mitkSimulationLightManager.cpp deleted file mode 100644 index c2835e629f..0000000000 --- a/Modules/Simulation/mitkSimulationLightManager.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/*=================================================================== - -The Medical Imaging Interaction Toolkit (MITK) - -Copyright (c) German Cancer Research Center, -Division of Medical and Biological Informatics. -All rights reserved. - -This software is distributed WITHOUT ANY WARRANTY; without -even the implied warranty of MERCHANTABILITY or FITNESS FOR -A PARTICULAR PURPOSE. - -See LICENSE.txt or http://www.mitk.org for details. - -===================================================================*/ - -#include "mitkSimulationLightManager.h" - -mitk::SimulationLightManager::SimulationLightManager() -{ -} - -mitk::SimulationLightManager::~SimulationLightManager() -{ -} diff --git a/Modules/Simulation/mitkSimulationLightManager.h b/Modules/Simulation/mitkSimulationLightManager.h deleted file mode 100644 index f4c60d7a1a..0000000000 --- a/Modules/Simulation/mitkSimulationLightManager.h +++ /dev/null @@ -1,39 +0,0 @@ -/*=================================================================== - -The Medical Imaging Interaction Toolkit (MITK) - -Copyright (c) German Cancer Research Center, -Division of Medical and Biological Informatics. -All rights reserved. - -This software is distributed WITHOUT ANY WARRANTY; without -even the implied warranty of MERCHANTABILITY or FITNESS FOR -A PARTICULAR PURPOSE. - -See LICENSE.txt or http://www.mitk.org for details. - -===================================================================*/ - -#ifndef mitkSimulationLightManager_h -#define mitkSimulationLightManager_h - -#include -#include - -namespace mitk -{ - class Simulation_EXPORT SimulationLightManager : public sofa::core::visual::VisualManager - { - public: - SOFA_CLASS(SimulationLightManager, sofa::core::visual::VisualManager); - - private: - SimulationLightManager(); - ~SimulationLightManager(); - - SimulationLightManager(const MyType&); - MyType& operator=(const MyType&); - }; -} - -#endif diff --git a/Modules/Simulation/mitkSimulationModel.cpp b/Modules/Simulation/mitkSimulationModel.cpp index f7230ccc08..6dc14fe952 100644 --- a/Modules/Simulation/mitkSimulationModel.cpp +++ b/Modules/Simulation/mitkSimulationModel.cpp @@ -1,243 +1,324 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkSimulation.h" #include "mitkSimulationModel.h" #include #include #include #include +#include +#include #include #include #include #include +#include mitk::SimulationModel::SimulationModel() : m_LastTime(-1.0), m_LastShowNormals(false), m_LastShowVisualModels(true), m_LastShowWireFrame(false) { } mitk::SimulationModel::~SimulationModel() { + this->DeleteVtkTextures(); this->DeleteVtkObjects(); } void mitk::SimulationModel::DeleteVtkObjects() { for (std::vector::const_iterator object = m_VtkObjects.begin(); object != m_VtkObjects.end(); ++object) (*object)->Delete(); m_VtkObjects.clear(); for (std::vector::const_iterator actor = m_Actors.begin(); actor != m_Actors.end(); ++actor) (*actor)->Delete(); m_Actors.clear(); } +void mitk::SimulationModel::DeleteVtkTextures() +{ + for (std::map::const_iterator texture = m_Textures.begin(); texture != m_Textures.end(); ++texture) + texture->second->Delete(); + + m_Textures.clear(); +} + void mitk::SimulationModel::DrawGroup(int ig, const sofa::core::visual::VisualParams* vparams, bool transparent) { const sofa::defaulttype::ResizableExtVector& triangles = this->getTriangles(); const sofa::defaulttype::ResizableExtVector& quads = this->getQuads(); FaceGroup g; if (ig < 0) { g.materialId = -1; g.tri0 = 0; g.nbt = triangles.size(); g.quad0 = 0; g.nbq = quads.size(); } else { g = groups.getValue()[ig]; } const sofa::defaulttype::ResizableExtVector& vertices = this->getVertices(); unsigned int numVertices = vertices.size(); vtkPoints* points = vtkPoints::New(); points->SetNumberOfPoints(numVertices); for (unsigned int i = 0; i < numVertices; ++i) points->SetPoint(i, vertices[i].elems); vtkPolyData* polyData = vtkPolyData::New(); polyData->SetPoints(points); points->Delete(); vtkCellArray* polys = vtkCellArray::New(); int numTriangles = g.tri0 + g.nbt; int numQuads = g.quad0 + g.nbq; for (int i = g.tri0; i < numTriangles; ++i) polys->InsertNextCell(3, reinterpret_cast(triangles[i].elems)); for (int i = g.quad0; i < numQuads; ++i) polys->InsertNextCell(4, reinterpret_cast(quads[i].elems)); polyData->SetPolys(polys); polys->Delete(); const sofa::defaulttype::ResizableExtVector& normals = this->getVnormals(); unsigned int numNormals = normals.size(); vtkFloatArray* vtkNormals = vtkFloatArray::New(); vtkNormals->SetNumberOfComponents(3); - vtkNormals->SetName("Normals"); for (unsigned int i = 0; i < numNormals; ++i) vtkNormals->InsertNextTuple(normals[i].elems); polyData->GetPointData()->SetNormals(vtkNormals); vtkNormals->Delete(); + sofa::core::loader::Material m = g.materialId >= 0 + ? materials.getValue()[g.materialId] + : material.getValue(); + + if (m.useTexture && m.activated) + { + const sofa::defaulttype::ResizableExtVector& texCoords = this->getVtexcoords(); + unsigned int numTexCoords = texCoords.size(); + + vtkFloatArray* vtkTexCoords = vtkFloatArray::New(); + vtkTexCoords->SetNumberOfComponents(2); + + for (unsigned int i = 0; i < numTexCoords; ++i) + vtkTexCoords->InsertNextTuple(texCoords[i].elems); + + polyData->GetPointData()->SetTCoords(vtkTexCoords); + + vtkTexCoords->Delete(); + } + vtkPolyDataMapper* polyDataMapper = vtkPolyDataMapper::New(); polyDataMapper->SetInput(polyData); vtkActor* actor = vtkActor::New(); actor->SetMapper(polyDataMapper); actor->SetScale(Simulation::ScaleFactor); - sofa::core::loader::Material m = g.materialId >= 0 - ? materials.getValue()[g.materialId] - : material.getValue(); + if (m.useTexture && m.activated) + actor->SetTexture(m_Textures[g.materialId]); sofa::defaulttype::Vec4f ambient = !m.useAmbient ? sofa::defaulttype::Vec4f() : m.ambient; sofa::defaulttype::Vec4f diffuse = !m.useDiffuse ? sofa::defaulttype::Vec4f() : m.diffuse; sofa::defaulttype::Vec4f specular = !m.useSpecular ? sofa::defaulttype::Vec4f() : m.specular; float shininess = m.useShininess ? m.shininess : 45.0f; if (shininess == 0.0f) { specular.clear(); shininess = 1.0f; } vtkProperty* property = actor->GetProperty(); property->SetAmbientColor(ambient.x(), ambient.y(), ambient.z()); property->SetDiffuseColor(diffuse.x(), diffuse.y(), diffuse.z()); + property->SetSpecular(1.0); property->SetSpecularColor(specular.x(), specular.y(), specular.z()); property->SetSpecularPower(shininess); if (vparams->displayFlags().getShowWireFrame()) property->SetRepresentationToWireframe(); m_VtkObjects.push_back(polyData); m_VtkObjects.push_back(polyDataMapper); m_Actors.push_back(actor); if (!m_LastShowNormals) return; points = vtkPoints::New(); points->SetNumberOfPoints(numVertices * 2); vtkCellArray* lines = vtkCellArray::New(); for (unsigned int i = 0; i < numVertices; ++i) { unsigned int j = 2 * i; unsigned int k = j + 1; points->SetPoint(j, vertices[i].elems); points->SetPoint(k, (vertices[i] + normals[i]).elems); vtkIdType line[] = { j, k }; lines->InsertNextCell(2, line); } polyData = vtkPolyData::New(); polyData->SetPoints(points); polyData->SetLines(lines); points->Delete(); lines->Delete(); polyDataMapper = vtkPolyDataMapper::New(); polyDataMapper->SetInput(polyData); actor = vtkActor::New(); actor->SetMapper(polyDataMapper); actor->SetScale(Simulation::ScaleFactor); m_VtkObjects.push_back(polyData); m_VtkObjects.push_back(polyDataMapper); m_Actors.push_back(actor); } std::vector mitk::SimulationModel::GetActors() const { return m_Actors; } void mitk::SimulationModel::internalDraw(const sofa::core::visual::VisualParams* vparams, bool transparent) { double time = this->getContext()->getTime(); const sofa::core::visual::DisplayFlags& displayFlags = vparams->displayFlags(); bool showNormals = displayFlags.getShowNormals(); bool showVisualModels = displayFlags.getShowVisualModels(); bool showWireFrame = displayFlags.getShowWireFrame(); if (time == m_LastTime && showNormals == m_LastShowNormals && showVisualModels == m_LastShowVisualModels && showWireFrame == m_LastShowWireFrame) return; m_LastTime = time; m_LastShowNormals = showNormals; m_LastShowVisualModels = showVisualModels; m_LastShowWireFrame = showWireFrame; if (transparent) return; this->DeleteVtkObjects(); if (!vparams->displayFlags().getShowVisualModels()) return; sofa::helper::ReadAccessor > > groups = this->groups; if (groups.empty()) { this->DrawGroup(-1, vparams, transparent); } else { for (unsigned int i = 0; i < groups.size(); ++i) this->DrawGroup(i, vparams, transparent); } } + +bool mitk::SimulationModel::loadTexture(const std::string& filename) +{ + return false; +} + +bool mitk::SimulationModel::loadTextures() +{ + this->DeleteVtkTextures(); + + std::vector activatedTextures; + + const sofa::helper::vector& materials = this->materials.getValue(); + unsigned int numMaterials = materials.size(); + + for (unsigned int i = 0; i < numMaterials; ++i) + { + const sofa::core::loader::Material& material = materials[i]; + + if (material.useTexture && material.activated) + activatedTextures.push_back(i); + } + + unsigned int numActivatedTextures = activatedTextures.size(); + + for (unsigned int i = 0; i < numActivatedTextures; ++i) + { + std::string textureFilename = materials[activatedTextures[i]].textureFilename; + + if (!sofa::helper::system::DataRepository.findFile(textureFilename)) + return false; + + vtkImageReader2* imageReader = vtkImageReader2Factory::CreateImageReader2(textureFilename.c_str()); + + if (imageReader == NULL) + return false; + + imageReader->SetFileName(textureFilename.c_str()); + + vtkTexture* texture = vtkTexture::New(); + texture->SetInput(imageReader->GetOutputDataObject(0)); + texture->InterpolateOn(); + + imageReader->Delete(); + + m_Textures.insert(std::make_pair(activatedTextures[i], texture)); + } + + return true; +} diff --git a/Modules/Simulation/mitkSimulationModel.h b/Modules/Simulation/mitkSimulationModel.h index 2ae2ccc285..b2a06fe9e1 100644 --- a/Modules/Simulation/mitkSimulationModel.h +++ b/Modules/Simulation/mitkSimulationModel.h @@ -1,58 +1,64 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef mitkSimulationModel_h #define mitkSimulationModel_h #include #include class vtkActor; class vtkObjectBase; +class vtkTexture; namespace mitk { class Simulation_EXPORT SimulationModel : public sofa::component::visualmodel::VisualModelImpl { public: SOFA_CLASS(SimulationModel, sofa::component::visualmodel::VisualModelImpl); std::vector GetActors() const; + bool loadTexture(const std::string& filename); + bool loadTextures(); + protected: void internalDraw(const sofa::core::visual::VisualParams* vparams, bool transparent); private: SimulationModel(); ~SimulationModel(); SimulationModel(const MyType&); MyType& operator=(const MyType&); void DeleteVtkObjects(); + void DeleteVtkTextures(); void DrawGroup(int ig, const sofa::core::visual::VisualParams* vparams, bool transparent); std::vector m_VtkObjects; std::vector m_Actors; + std::map m_Textures; double m_LastTime; bool m_LastShowNormals; bool m_LastShowVisualModels; bool m_LastShowWireFrame; }; } #endif diff --git a/Modules/Simulation/mitkSimulationObjectFactory.cpp b/Modules/Simulation/mitkSimulationObjectFactory.cpp index 4e520280a9..ff1c0cef4b 100644 --- a/Modules/Simulation/mitkSimulationObjectFactory.cpp +++ b/Modules/Simulation/mitkSimulationObjectFactory.cpp @@ -1,137 +1,128 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkSimulation.h" #include "mitkSimulationMapper3D.h" #include "mitkSimulationObjectFactory.h" -#include "mitkSimulationLight.h" -#include "mitkSimulationLightManager.h" #include "mitkSimulationModel.h" #include #include #include #include mitk::SimulationObjectFactory::SimulationObjectFactory() : m_SimulationIOFactory(SimulationIOFactory::New()) { itk::ObjectFactoryBase::RegisterFactory(m_SimulationIOFactory); const std::string description = "SOFA Scene Files"; m_FileExtensionsMap.insert(std::pair("*.scn", description)); m_FileExtensionsMap.insert(std::pair("*.xml", description)); sofa::component::init(); sofa::simulation::xml::initXml(); - int SimulationLightClass = sofa::core::RegisterObject("").add(); - sofa::core::ObjectFactory::AddAlias("DirectionalLight", "SimulationLight", true); - sofa::core::ObjectFactory::AddAlias("PositionalLight", "SimulationLight", true); - sofa::core::ObjectFactory::AddAlias("SpotLight", "SimulationLight", true); - - int SimulationLightManagerClass = sofa::core::RegisterObject("").add(); - sofa::core::ObjectFactory::AddAlias("LightManager", "SimulationLightManager", true); - int SimulationModelClass = sofa::core::RegisterObject("").add(); sofa::core::ObjectFactory::AddAlias("OglModel", "SimulationModel", true); + sofa::core::ObjectFactory::AddAlias("VisualModel", "SimulationModel", true); } mitk::SimulationObjectFactory::~SimulationObjectFactory() { itk::ObjectFactoryBase::UnRegisterFactory(m_SimulationIOFactory); } mitk::Mapper::Pointer mitk::SimulationObjectFactory::CreateMapper(mitk::DataNode* node, MapperSlotId slotId) { mitk::Mapper::Pointer mapper; if (dynamic_cast(node->GetData()) != NULL) { if (slotId == mitk::BaseRenderer::Standard3D) mapper = mitk::SimulationMapper3D::New(); if (mapper.IsNotNull()) mapper->SetDataNode(node); } return mapper; } const char* mitk::SimulationObjectFactory::GetDescription() const { return "mitk::SimulationObjectFactory"; } const char* mitk::SimulationObjectFactory::GetFileExtensions() { std::string fileExtensions; this->CreateFileExtensions(m_FileExtensionsMap, fileExtensions); return fileExtensions.c_str(); } mitk::CoreObjectFactoryBase::MultimapType mitk::SimulationObjectFactory::GetFileExtensionsMap() { return m_FileExtensionsMap; } const char* mitk::SimulationObjectFactory::GetITKSourceVersion() const { return ITK_SOURCE_VERSION; } const char* mitk::SimulationObjectFactory::GetSaveFileExtensions() { std::string saveFileExtensions; this->CreateFileExtensions(m_FileExtensionsMap, saveFileExtensions); return saveFileExtensions.c_str(); } mitk::CoreObjectFactoryBase::MultimapType mitk::SimulationObjectFactory::GetSaveFileExtensionsMap() { return m_SaveFileExtensionsMap; } void mitk::SimulationObjectFactory::SetDefaultProperties(mitk::DataNode* node) { if (node != NULL) { if (dynamic_cast(node->GetData()) != NULL) SimulationMapper3D::SetDefaultProperties(node); } } class RegisterSimulationObjectFactory { public: RegisterSimulationObjectFactory() : m_Factory(mitk::SimulationObjectFactory::New()) { mitk::CoreObjectFactory::GetInstance()->RegisterExtraFactory(m_Factory); } ~RegisterSimulationObjectFactory() { mitk::CoreObjectFactory::GetInstance()->UnRegisterExtraFactory(m_Factory); } private: RegisterSimulationObjectFactory(const RegisterSimulationObjectFactory&); RegisterSimulationObjectFactory& operator=(const RegisterSimulationObjectFactory&); mitk::SimulationObjectFactory::Pointer m_Factory; }; static RegisterSimulationObjectFactory registerSimulationObjectFactory;