Skip to content

Commit

Permalink
+ support reading mesh files with colors
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Dec 31, 2015
1 parent e236999 commit 342198e
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/Mod/Mesh/App/AppMesh.cpp
Expand Up @@ -83,6 +83,7 @@ void MeshExport initMesh()
Mesh::MeshObject ::init();

Mesh::Feature ::init();
Mesh::FeatureCustom ::init();
Mesh::FeaturePython ::init();
Mesh::Import ::init();
Mesh::Export ::init();
Expand Down
32 changes: 31 additions & 1 deletion src/Mod/Mesh/App/AppMeshPy.cpp
Expand Up @@ -83,7 +83,8 @@ static PyObject * open(PyObject *self, PyObject *args)

PY_TRY {
MeshObject mesh;
if (mesh.load(EncodedName.c_str())) {
MeshCore::Material mat;
if (mesh.load(EncodedName.c_str(), &mat)) {
Base::FileInfo file(EncodedName.c_str());
// create new document and add Import feature
App::Document *pcDoc = App::GetApplication().newDocument("Unnamed");
Expand All @@ -98,6 +99,20 @@ static PyObject * open(PyObject *self, PyObject *args)
pcFeature->purgeTouched();
}
}
else if (mat.binding == MeshCore::MeshIO::PER_VERTEX &&
mat.diffuseColor.size() == mesh.countPoints()) {
FeatureCustom *pcFeature = new FeatureCustom();
pcFeature->Label.setValue(file.fileNamePure().c_str());
pcFeature->Mesh.swapMesh(mesh);
App::PropertyColorList* prop = static_cast<App::PropertyColorList*>
(pcFeature->addDynamicProperty("App::PropertyColorList", "VertexColors"));
if (prop) {
prop->setValues(mat.diffuseColor);
}
pcFeature->purgeTouched();

pcDoc->addObject(pcFeature, file.fileNamePure().c_str());
}
else {
Mesh::Feature *pcFeature = static_cast<Mesh::Feature *>
(pcDoc->addObject("Mesh::Feature", file.fileNamePure().c_str()));
Expand Down Expand Up @@ -132,6 +147,7 @@ static PyObject * importer(PyObject *self, PyObject *args)
}

MeshObject mesh;
MeshCore::Material mat;
if (mesh.load(EncodedName.c_str())) {
Base::FileInfo file(EncodedName.c_str());
unsigned long segmct = mesh.countSegments();
Expand All @@ -145,6 +161,20 @@ static PyObject * importer(PyObject *self, PyObject *args)
pcFeature->purgeTouched();
}
}
else if (mat.binding == MeshCore::MeshIO::PER_VERTEX &&
mat.diffuseColor.size() == mesh.countPoints()) {
FeatureCustom *pcFeature = new FeatureCustom();
pcFeature->Label.setValue(file.fileNamePure().c_str());
pcFeature->Mesh.swapMesh(mesh);
App::PropertyColorList* prop = static_cast<App::PropertyColorList*>
(pcFeature->addDynamicProperty("App::PropertyColorList", "VertexColors"));
if (prop) {
prop->setValues(mat.diffuseColor);
}
pcFeature->purgeTouched();

pcDoc->addObject(pcFeature, file.fileNamePure().c_str());
}
else {
Mesh::Feature *pcFeature = static_cast<Mesh::Feature *>
(pcDoc->addObject("Mesh::Feature", file.fileNamePure().c_str()));
Expand Down
10 changes: 10 additions & 0 deletions src/Mod/Mesh/App/Core/MeshIO.cpp
Expand Up @@ -720,6 +720,16 @@ bool MeshInput::LoadPLY (std::istream &inp)
if (num_z != 1)
return false;

for (std::vector<std::pair<std::string, Ply::Number> >::iterator it =
vertex_props.begin(); it != vertex_props.end(); ++it) {
if (it->first == "diffuse_red")
it->first = "red";
else if (it->first == "diffuse_green")
it->first = "green";
else if (it->first == "diffuse_blue")
it->first = "blue";
}

// check if valid colors are set
std::size_t num_r = std::count_if(vertex_props.begin(), vertex_props.end(),
std::bind2nd(property, "red"));
Expand Down
11 changes: 11 additions & 0 deletions src/Mod/Mesh/App/MeshFeature.cpp
Expand Up @@ -97,6 +97,17 @@ void Feature::onChanged(const App::Property* prop)

// ---------------------------------------------------------

namespace App {
/// @cond DOXERR
PROPERTY_SOURCE_TEMPLATE(Mesh::FeatureCustom, Mesh::Feature)
/// @endcond

// explicit template instantiation
template class MeshExport FeatureCustomT<Mesh::Feature>;
}

// ---------------------------------------------------------

namespace App {
/// @cond DOXERR
PROPERTY_SOURCE_TEMPLATE(Mesh::FeaturePython, Mesh::Feature)
Expand Down
2 changes: 2 additions & 0 deletions src/Mod/Mesh/App/MeshFeature.h
Expand Up @@ -25,6 +25,7 @@
#define MESH_FEATURE_H

#include <App/GeoFeature.h>
#include <App/FeatureCustom.h>
#include <App/FeaturePython.h>

#include "Core/MeshKernel.h"
Expand Down Expand Up @@ -81,6 +82,7 @@ class MeshExport Feature : public App::GeoFeature
virtual PyObject* getPyObject(void);
};

typedef App::FeatureCustomT<Feature> FeatureCustom;
typedef App::FeaturePythonT<Feature> FeaturePython;

} //namespace Mesh
Expand Down
75 changes: 69 additions & 6 deletions src/Mod/Mesh/Gui/ViewProvider.cpp
Expand Up @@ -498,6 +498,13 @@ void ViewProviderMesh::attach(App::DocumentObject *pcFeat)
addDisplayMaskMode(pcFlatWireRoot, "FlatWireframe");
}

void ViewProviderMesh::updateData(const App::Property* prop)
{
Gui::ViewProviderGeometryObject::updateData(prop);
//if (prop->getTypeId() == Mesh::PropertyMeshKernel::getClassTypeId()) {
//}
}

QIcon ViewProviderMesh::getIcon() const
{
#if 1
Expand Down Expand Up @@ -531,16 +538,70 @@ QIcon ViewProviderMesh::getIcon() const
#endif
}

App::PropertyColorList* ViewProviderMesh::getColorProperty() const
{
if (pcObject) {
std::map<std::string,App::Property*> Map;
pcObject->getPropertyMap(Map);
for (std::map<std::string,App::Property*>::iterator it = Map.begin(); it != Map.end(); ++it) {
Base::Type type = it->second->getTypeId();
if (type == App::PropertyColorList::getClassTypeId()) {
App::PropertyColorList* colors = static_cast<App::PropertyColorList*>(it->second);
return colors;
}
}
}

return 0; // no such property found
}

void ViewProviderMesh::tryColorPerVertex()
{
App::PropertyColorList* colors = getColorProperty();
if (colors) {
const Mesh::PropertyMeshKernel& meshProp = static_cast<Mesh::Feature*>(pcObject)->Mesh;
const Mesh::MeshObject& mesh = meshProp.getValue();
int numPoints = static_cast<int>(mesh.countPoints());

if (colors->getSize() == numPoints) {
setColorPerVertex(colors);
}
}
}

void ViewProviderMesh::setColorPerVertex(const App::PropertyColorList* prop)
{
pcMatBinding->value = SoMaterialBinding::PER_VERTEX;
const std::vector<App::Color>& val = prop->getValues();

pcShapeMaterial->diffuseColor.setNum(val.size());
SbColor* col = pcShapeMaterial->diffuseColor.startEditing();

std::size_t i=0;
for (std::vector<App::Color>::const_iterator it = val.begin(); it != val.end(); ++it) {
col[i++].setValue(it->r, it->g, it->b);
}

pcShapeMaterial->diffuseColor.finishEditing();
}

void ViewProviderMesh::setDisplayMode(const char* ModeName)
{
if (strcmp("Shaded",ModeName)==0)
if (strcmp("Shaded",ModeName)==0) {
setDisplayMaskMode("Flat");
else if (strcmp("Points",ModeName)==0)
}
else if (strcmp("Points",ModeName)==0) {
setDisplayMaskMode("Point");
else if (strcmp("Flat Lines",ModeName)==0)
}
else if (strcmp("Flat Lines",ModeName)==0) {
setDisplayMaskMode("FlatWireframe");
else if (strcmp("Wireframe",ModeName)==0)
}
else if (strcmp("Wireframe",ModeName)==0) {
setDisplayMaskMode("Wireframe");
}
else if (strcmp("Colors",ModeName)==0) {
tryColorPerVertex();
}

ViewProviderGeometryObject::setDisplayMode(ModeName);
}
Expand All @@ -554,6 +615,8 @@ std::vector<std::string> ViewProviderMesh::getDisplayModes(void) const
StrList.push_back("Wireframe");
StrList.push_back("Flat Lines");
StrList.push_back("Points");
if (getColorProperty())
StrList.push_back("Colors");

return StrList;
}
Expand Down Expand Up @@ -1799,7 +1862,7 @@ void ViewProviderIndexedFaceSet::attach(App::DocumentObject *pcFeat)

void ViewProviderIndexedFaceSet::updateData(const App::Property* prop)
{
Gui::ViewProviderGeometryObject::updateData(prop);
ViewProviderMesh::updateData(prop);
if (prop->getTypeId() == Mesh::PropertyMeshKernel::getClassTypeId()) {
ViewProviderMeshBuilder builder;
builder.createMesh(prop, pcMeshCoord, pcMeshFaces);
Expand Down Expand Up @@ -1884,7 +1947,7 @@ void ViewProviderMeshObject::attach(App::DocumentObject *pcFeat)

void ViewProviderMeshObject::updateData(const App::Property* prop)
{
Gui::ViewProviderGeometryObject::updateData(prop);
ViewProviderMesh::updateData(prop);
if (prop->getTypeId() == Mesh::PropertyMeshKernel::getClassTypeId()) {
const Mesh::PropertyMeshKernel* mesh = static_cast<const Mesh::PropertyMeshKernel*>(prop);
this->pcMeshNode->mesh.setValue(mesh->getValuePtr());
Expand Down
5 changes: 5 additions & 0 deletions src/Mod/Mesh/Gui/ViewProvider.h
Expand Up @@ -50,6 +50,7 @@ class SbPlane;

namespace App {
class Color;
class PropertyColorList;
}

namespace Base {
Expand Down Expand Up @@ -122,6 +123,7 @@ class MeshGuiExport ViewProviderMesh : public Gui::ViewProviderGeometryObject
App::PropertyColor LineColor;

virtual void attach(App::DocumentObject *);
virtual void updateData(const App::Property*);
virtual bool useNewSelectionModel(void) const {return false;}
Gui::SoFCSelection* getHighlightNode() const { return pcHighlight; }
virtual QIcon getIcon() const;
Expand Down Expand Up @@ -173,6 +175,9 @@ class MeshGuiExport ViewProviderMesh : public Gui::ViewProviderGeometryObject
void unhighlightSelection();
void highlightComponents();
void setHighlightedComponents(bool);
App::PropertyColorList* getColorProperty() const;
void tryColorPerVertex();
void setColorPerVertex(const App::PropertyColorList*);

virtual SoShape* getShapeNode() const;
virtual SoNode* getCoordNode() const;
Expand Down

0 comments on commit 342198e

Please sign in to comment.