21 #include <sofa/component/visualmodel/VisualModelImpl.h>
22 #include <vtkCellArray.h>
23 #include <vtkFloatArray.h>
24 #include <vtkPointData.h>
25 #include <vtkPolyData.h>
26 #include <vtkSmartPointer.h>
30 using sofa::defaulttype::Vec4f;
32 if (dataNode.IsNull() ||
dynamic_cast<mitk::Surface*
>(dataNode->GetData()) ==
nullptr)
35 if (material.useDiffuse)
36 dataNode->SetFloatProperty(
"opacity", material.diffuse[3]);
38 Vec4f ambient = material.useAmbient ? material.ambient : Vec4f();
39 Vec4f diffuse = material.useDiffuse ? material.diffuse : Vec4f();
40 Vec4f specular = material.useSpecular ? material.specular : Vec4f();
41 float shininess = material.useShininess ?
std::min(material.shininess, 128.0f) : 45.0f;
43 if (shininess == 0.0f)
49 dataNode->SetFloatProperty(
"material.ambientCoefficient", 1.0f);
52 dataNode->SetFloatProperty(
"material.diffuseCoefficient", 1.0f);
55 dataNode->SetFloatProperty(
"material.specularCoefficient", 1.0f);
57 dataNode->SetFloatProperty(
"material.specularPower", shininess);
62 if (dataStorage.IsNull())
71 for (mitk::DataStorage::SetOfObjects::ConstIterator it = subset->Begin(); it != subset->End(); ++it)
76 if (simulation->GetRootNode() == rootNode)
93 m_VisualModelName(visualModelName)
105 for_each(
this, node, node->visualModel, &ExportMitkVisitor::processVisualModel);
106 return RESULT_CONTINUE;
112 void mitk::ExportMitkVisitor::processVisualModel(sofa::simulation::Node* node, sofa::core::visual::VisualModel* visualModel)
114 using sofa::defaulttype::ResizableExtVector;
115 typedef sofa::component::visualmodel::VisualModelImpl::VecCoord VecCoord;
116 typedef sofa::component::visualmodel::VisualModelImpl::Triangle Triangle;
117 typedef sofa::component::visualmodel::VisualModelImpl::Quad Quad;
118 typedef sofa::component::visualmodel::VisualModelImpl::Deriv Deriv;
119 typedef sofa::component::visualmodel::VisualModelImpl::VecTexCoord VecTexCoord;
121 sofa::component::visualmodel::VisualModelImpl* visualModelImpl =
dynamic_cast<sofa::component::visualmodel::VisualModelImpl*
>(visualModel);
123 if (visualModelImpl ==
nullptr)
126 if (!m_VisualModelName.empty() && m_VisualModelName != visualModelImpl->name.getValue())
132 const VecCoord& vertices = visualModelImpl->m_vertices2.getValue().empty()
133 ? visualModelImpl->m_positions.getValue()
134 : visualModelImpl->m_vertices2.getValue();
136 size_t numPoints = vertices.size();
138 points->SetNumberOfPoints(numPoints);
140 for (
size_t i = 0; i < numPoints; ++i)
141 points->SetPoint(i, vertices[i].elems);
143 polyData->SetPoints(points);
146 const ResizableExtVector<Triangle>& triangles = visualModelImpl->m_triangles.getValue();
148 if (!triangles.empty())
150 ResizableExtVector<Triangle>::const_iterator trianglesEnd = triangles.end();
152 for (ResizableExtVector<Triangle>::const_iterator it = triangles.begin(); it != trianglesEnd; ++it)
154 const Triangle& triangle = *it;
156 polys->InsertNextCell(3);
157 polys->InsertCellPoint(triangle[0]);
158 polys->InsertCellPoint(triangle[1]);
159 polys->InsertCellPoint(triangle[2]);
163 const ResizableExtVector<Quad>& quads = visualModelImpl->m_quads.getValue();
167 ResizableExtVector<Quad>::const_iterator quadsEnd = quads.end();
169 for (ResizableExtVector<Quad>::const_iterator it = quads.begin(); it != quadsEnd; ++it)
171 const Quad& quad = *it;
173 polys->InsertNextCell(4);
174 polys->InsertCellPoint(quad[0]);
175 polys->InsertCellPoint(quad[1]);
176 polys->InsertCellPoint(quad[2]);
177 polys->InsertCellPoint(quad[3]);
181 polyData->SetPolys(polys);
183 const ResizableExtVector<Deriv>& normals = visualModelImpl->m_vnormals.getValue();
185 if (!normals.empty())
187 size_t numNormals = normals.size();
190 vtkNormals->SetNumberOfComponents(3);
191 vtkNormals->SetNumberOfTuples(numNormals);
193 for (
size_t i = 0; i < numNormals; ++i)
194 vtkNormals->SetTuple(i, normals[i].elems);
196 polyData->GetPointData()->SetNormals(vtkNormals);
199 const VecTexCoord& texCoords = visualModelImpl->m_vtexcoords.getValue();
201 if (!texCoords.empty())
203 size_t numTexCoords = texCoords.size();
206 vtkTexCoords->SetNumberOfComponents(2);
207 vtkTexCoords->SetNumberOfTuples(numTexCoords);
209 for (
size_t i = 0; i < numTexCoords; ++i)
210 vtkTexCoords->SetTuple(i, texCoords[i].elems);
212 polyData->GetPointData()->SetTCoords(vtkTexCoords);
216 surface->SetVtkPolyData(polyData);
219 dataNode->SetName(visualModelImpl->name.getValue());
220 dataNode->SetData(surface);
222 ApplyMaterial(dataNode, visualModelImpl->material.getValue());
226 if (parentDataNode.IsNotNull())
227 surface->SetGeometry(parentDataNode->GetData()->GetGeometry());
Class for storing surfaces (vtkPolyData).
itk::SmartPointer< Self > Pointer
itk::SmartPointer< const Self > ConstPointer
ExportMitkVisitor(DataStorage::Pointer dataStorage, const sofa::core::ExecParams *params=sofa::core::ExecParams::defaultInstance())
itk::SmartPointer< Self > Pointer
Result processNodeTopDown(sofa::simulation::Node *node) override
void ApplyMaterial(mitk::DataNode::Pointer dataNode, const sofa::core::loader::Material &material)
mitk::DataStorage::Pointer m_DataStorage
static mitk::DataNode::Pointer GetSimulationDataNode(mitk::DataStorage::Pointer dataStorage, sofa::core::objectmodel::BaseNode::SPtr rootNode)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.