diff --git a/src/Mod/Fem/Gui/ViewProviderFemMesh.cpp b/src/Mod/Fem/Gui/ViewProviderFemMesh.cpp index 7c7e31115..3b98a34cf 100755 --- a/src/Mod/Fem/Gui/ViewProviderFemMesh.cpp +++ b/src/Mod/Fem/Gui/ViewProviderFemMesh.cpp @@ -498,20 +498,72 @@ void ViewProviderFemMesh::setColorByNodeId(const std::map &Node pcMatBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED; + long startId = NodeColorMap.begin()->first; + long endId = (--NodeColorMap.end())->first; + + std::vector colorVec(endId-startId+2,App::Color(0,1,0)); + for(std::map::const_iterator it=NodeColorMap.begin();it!=NodeColorMap.end();++it) + colorVec[it->first-startId] = it->second; + // resizing and writing the color vector: pcShapeMaterial->diffuseColor.setNum(vNodeElementIdx.size()); SbColor* colors = pcShapeMaterial->diffuseColor.startEditing(); + //int i=0; + //for(std::vector::const_iterator it=vNodeElementIdx.begin() + // ;it!=vNodeElementIdx.end() + // ;++it,i++){ + // const std::map::const_iterator pos = NodeColorMap.find(*it); + // if(pos == NodeColorMap.end()) + // colors[i] = SbColor(0,1,0); + // else + // colors[i] = SbColor(pos->second.r,pos->second.g,pos->second.b); + //} + int i=0; for(std::vector::const_iterator it=vNodeElementIdx.begin() ;it!=vNodeElementIdx.end() - ;++it,i++){ - const std::map::const_iterator pos = NodeColorMap.find(*it); - if(pos == NodeColorMap.end()) - colors[i] = SbColor(0,1,0); - else - colors[i] = SbColor(pos->second.r,pos->second.g,pos->second.b); - } + ;++it,i++) + colors[i] = SbColor(colorVec[*it-1].r,colorVec[*it-1].g,colorVec[*it-1].b); + + + pcShapeMaterial->diffuseColor.finishEditing(); +} +void ViewProviderFemMesh::setColorByNodeId(const std::vector &NodeIds,const std::vector &NodeColors) +{ + pcShapeMaterial->diffuseColor; + + pcMatBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED; + + long startId = *(std::min_element(NodeIds.begin(), NodeIds.end())); + long endId = *(std::max_element(NodeIds.begin(), NodeIds.end())); + + std::vector colorVec(endId-startId+2,App::Color(0,1,0)); + int i=0; + for(std::vector::const_iterator it=NodeIds.begin();it!=NodeIds.end();++it,i++) + colorVec[*it-startId] = NodeColors[i]; + + // resizing and writing the color vector: + pcShapeMaterial->diffuseColor.setNum(vNodeElementIdx.size()); + SbColor* colors = pcShapeMaterial->diffuseColor.startEditing(); + + //int i=0; + //for(std::vector::const_iterator it=vNodeElementIdx.begin() + // ;it!=vNodeElementIdx.end() + // ;++it,i++){ + // const std::map::const_iterator pos = NodeColorMap.find(*it); + // if(pos == NodeColorMap.end()) + // colors[i] = SbColor(0,1,0); + // else + // colors[i] = SbColor(pos->second.r,pos->second.g,pos->second.b); + //} + + i=0; + for(std::vector::const_iterator it=vNodeElementIdx.begin() + ;it!=vNodeElementIdx.end() + ;++it,i++) + colors[i] = SbColor(colorVec[*it-1].r,colorVec[*it-1].g,colorVec[*it-1].b); + pcShapeMaterial->diffuseColor.finishEditing(); } diff --git a/src/Mod/Fem/Gui/ViewProviderFemMesh.h b/src/Mod/Fem/Gui/ViewProviderFemMesh.h index e697c5911..16e6effb0 100755 --- a/src/Mod/Fem/Gui/ViewProviderFemMesh.h +++ b/src/Mod/Fem/Gui/ViewProviderFemMesh.h @@ -105,6 +105,8 @@ public: //@{ /// set the color for each node void setColorByNodeId(const std::map &NodeColorMap); + void setColorByNodeId(const std::vector &NodeIds,const std::vector &NodeColors); + /// reset the view of the node colors void resetColorByNodeId(void); /// set the displacement for each node diff --git a/src/Mod/Fem/Gui/ViewProviderFemMeshPy.xml b/src/Mod/Fem/Gui/ViewProviderFemMeshPy.xml index ff9104cec..2af3aacdf 100644 --- a/src/Mod/Fem/Gui/ViewProviderFemMeshPy.xml +++ b/src/Mod/Fem/Gui/ViewProviderFemMeshPy.xml @@ -20,7 +20,12 @@ - + + + + + + Postprocessing color of the nodes. The faces between the nodes gets interpolated. diff --git a/src/Mod/Fem/Gui/ViewProviderFemMeshPyImp.cpp b/src/Mod/Fem/Gui/ViewProviderFemMeshPyImp.cpp index 5c2147283..cbea69e5f 100644 --- a/src/Mod/Fem/Gui/ViewProviderFemMeshPyImp.cpp +++ b/src/Mod/Fem/Gui/ViewProviderFemMeshPyImp.cpp @@ -4,7 +4,11 @@ #include #include +#include + #include "Mod/Fem/Gui/ViewProviderFemMesh.h" +#include "Mod/Fem/App/FemResultVector.h" +#include "Mod/Fem/App/FemResultValue.h" // inclusion of the generated files (generated out of ViewProviderFemMeshPy.xml) #include "ViewProviderFemMeshPy.h" @@ -31,8 +35,50 @@ PyObject* ViewProviderFemMeshPy::animate(PyObject * args) Py_Return; } +PyObject* ViewProviderFemMeshPy::setNodeColorByResult(PyObject *args) +{ + PyObject *object=0; + if (PyArg_ParseTuple(args,"O!",&(App::DocumentObjectPy::Type), &object)) { + App::DocumentObject* obj = static_cast(object)->getDocumentObjectPtr(); + if (obj && obj->getTypeId().isDerivedFrom(Fem::FemResultValue::getClassTypeId())){ + Fem::FemResultValue *result = static_cast(obj); + }else if (obj && obj->getTypeId().isDerivedFrom(Fem::FemResultVector::getClassTypeId())){ + Fem::FemResultVector *result = static_cast(obj); + const std::vector & Ids = result->ElementNumbers.getValues() ; + const std::vector & Vecs = result->Values.getValues() ; + std::vector NodeColors(Vecs.size()); + float max = 0.0; + for(std::vector::const_iterator it= Vecs.begin();it!=Vecs.end();++it) + if(it->Length() > max) + max = it->Length(); + long i=0; + for(std::vector::const_iterator it= Vecs.begin();it!=Vecs.end();++it,i++){ + double value = it->Length(); + double min = 0.0; + + if (value < min) + NodeColors[i] = App::Color (0.0,1.0,0.0); + else if (value > max) + NodeColors[i] = App::Color (1.0,0.0,0.0); + else if ( value < (min + (max-min)/2.0 )) + NodeColors[i] = App::Color ((value-min) / ((max-min)/2.0),1.0,0.0) ; + else + NodeColors[i] = App::Color (1.0,1-((value-min-((max-min)/2.0)) / ((max-min)/2.0)),0.0); + + } + + this->getViewProviderFemMeshPtr()->setColorByNodeId(Ids,NodeColors); + }else{ + PyErr_SetString(PyExc_Exception, "Argument has to be a ResultValue or ResultVector!"); + return 0; + } + } + + Py_Return; + +} Py::Dict ViewProviderFemMeshPy::getNodeColor(void) const @@ -43,17 +89,34 @@ Py::Dict ViewProviderFemMeshPy::getNodeColor(void) const void ViewProviderFemMeshPy::setNodeColor(Py::Dict arg) { - if(arg.size() == 0) + long size = arg.size(); + if(size == 0) this->getViewProviderFemMeshPtr()->resetColorByNodeId(); else { - std::map NodeColorMap; + Base::TimeInfo Start; + Base::Console().Log("Start: ViewProviderFemMeshPy::setNodeColor() =================================\n"); + //std::map NodeColorMap; - for( Py::Dict::iterator it = arg.begin(); it!= arg.end();++it){ + //for( Py::Dict::iterator it = arg.begin(); it!= arg.end();++it){ + // Py::Int id((*it).first); + // Py::Tuple color((*it).second); + // NodeColorMap[id] = App::Color(Py::Float(color[0]),Py::Float(color[1]),Py::Float(color[2]),0); + //} + std::vector NodeIds(size); + std::vector NodeColors(size); + + long i = 0; + for( Py::Dict::iterator it = arg.begin(); it!= arg.end();++it,i++){ Py::Int id((*it).first); Py::Tuple color((*it).second); - NodeColorMap[id] = App::Color(Py::Float(color[0]),Py::Float(color[1]),Py::Float(color[2]),0); + NodeIds[i] = id; + NodeColors[i] = App::Color(Py::Float(color[0]),Py::Float(color[1]),Py::Float(color[2]),0); } - this->getViewProviderFemMeshPtr()->setColorByNodeId(NodeColorMap); + Base::Console().Log(" %f: Start ViewProviderFemMeshPy::setNodeColor() call \n",Base::TimeInfo::diffTimeF(Start,Base::TimeInfo())); + + //this->getViewProviderFemMeshPtr()->setColorByNodeId(NodeColorMap); + this->getViewProviderFemMeshPtr()->setColorByNodeId(NodeIds,NodeColors); + Base::Console().Log(" %f: Finish ViewProviderFemMeshPy::setNodeColor() call \n",Base::TimeInfo::diffTimeF(Start,Base::TimeInfo())); } }