Skip to content

Commit

Permalink
improve groups handling of obj mesh format
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jul 23, 2016
1 parent 39e5778 commit cc78503
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
22 changes: 16 additions & 6 deletions src/Mod/Mesh/App/AppMeshPy.cpp
Expand Up @@ -167,10 +167,15 @@ class Module : public Py::ExtensionModule<Module>
unsigned long segmct = mesh.countSegments();
if (segmct > 1) {
for (unsigned long i=0; i<segmct; i++) {
std::auto_ptr<MeshObject> segm(mesh.meshFromSegment(mesh.getSegment(i).getIndices()));
const Segment& group = mesh.getSegment(i);
std::string groupName = group.getName();
if (groupName.empty())
groupName = file.fileNamePure();

std::auto_ptr<MeshObject> segm(mesh.meshFromSegment(group.getIndices()));
Mesh::Feature *pcFeature = static_cast<Mesh::Feature *>
(pcDoc->addObject("Mesh::Feature", file.fileNamePure().c_str()));
pcFeature->Label.setValue(file.fileNamePure().c_str());
(pcDoc->addObject("Mesh::Feature", groupName.c_str()));
pcFeature->Label.setValue(groupName.c_str());
pcFeature->Mesh.swapMesh(*segm);
pcFeature->purgeTouched();
}
Expand Down Expand Up @@ -227,10 +232,15 @@ class Module : public Py::ExtensionModule<Module>
unsigned long segmct = mesh.countSegments();
if (segmct > 1) {
for (unsigned long i=0; i<segmct; i++) {
std::auto_ptr<MeshObject> segm(mesh.meshFromSegment(mesh.getSegment(i).getIndices()));
const Segment& group = mesh.getSegment(i);
std::string groupName = group.getName();
if (groupName.empty())
groupName = file.fileNamePure();

std::auto_ptr<MeshObject> segm(mesh.meshFromSegment(group.getIndices()));
Mesh::Feature *pcFeature = static_cast<Mesh::Feature *>
(pcDoc->addObject("Mesh::Feature", file.fileNamePure().c_str()));
pcFeature->Label.setValue(file.fileNamePure().c_str());
(pcDoc->addObject("Mesh::Feature", groupName.c_str()));
pcFeature->Label.setValue(groupName.c_str());
pcFeature->Mesh.swapMesh(*segm);
pcFeature->purgeTouched();
}
Expand Down
26 changes: 15 additions & 11 deletions src/Mod/Mesh/App/Core/MeshIO.cpp
Expand Up @@ -255,6 +255,7 @@ bool MeshInput::LoadSTL (std::istream &rstrIn)
/** Loads an OBJ file. */
bool MeshInput::LoadOBJ (std::istream &rstrIn)
{
boost::regex rx_g("^g\\s+([\\x21-\\x7E]+)\\s*$");
boost::regex rx_p("^v\\s+([-+]?[0-9]*)\\.?([0-9]+([eE][-+]?[0-9]+)?)"
"\\s+([-+]?[0-9]*)\\.?([0-9]+([eE][-+]?[0-9]+)?)"
"\\s+([-+]?[0-9]*)\\.?([0-9]+([eE][-+]?[0-9]+)?)\\s*$");
Expand Down Expand Up @@ -284,7 +285,6 @@ bool MeshInput::LoadOBJ (std::istream &rstrIn)
std::string line;
float fX, fY, fZ;
unsigned int i1=1,i2=1,i3=1,i4=1;
MeshGeomFacet clFacet;
MeshFacet item;

if (!rstrIn || rstrIn.bad() == true)
Expand All @@ -295,19 +295,20 @@ bool MeshInput::LoadOBJ (std::istream &rstrIn)
return false;

MeshIO::Binding rgb_value = MeshIO::OVERALL;
bool readvertices=false;
bool new_segment = true;
while (std::getline(rstrIn, line)) {
for (std::string::iterator it = line.begin(); it != line.end(); ++it)
*it = tolower(*it);
// when a group name comes don't make it lower case
if (!line.empty() && line[0] != 'g') {
for (std::string::iterator it = line.begin(); it != line.end(); ++it)
*it = tolower(*it);
}
if (boost::regex_match(line.c_str(), what, rx_p)) {
readvertices = true;
fX = (float)std::atof(what[1].first);
fY = (float)std::atof(what[4].first);
fZ = (float)std::atof(what[7].first);
meshPoints.push_back(MeshPoint(Base::Vector3f(fX, fY, fZ)));
}
else if (boost::regex_match(line.c_str(), what, rx_c)) {
readvertices = true;
fX = (float)std::atof(what[1].first);
fY = (float)std::atof(what[4].first);
fZ = (float)std::atof(what[7].first);
Expand All @@ -322,7 +323,6 @@ bool MeshInput::LoadOBJ (std::istream &rstrIn)
rgb_value = MeshIO::PER_VERTEX;
}
else if (boost::regex_match(line.c_str(), what, rx_t)) {
readvertices = true;
fX = (float)std::atof(what[1].first);
fY = (float)std::atof(what[4].first);
fZ = (float)std::atof(what[7].first);
Expand All @@ -336,10 +336,14 @@ bool MeshInput::LoadOBJ (std::istream &rstrIn)
meshPoints.back().SetProperty(prop);
rgb_value = MeshIO::PER_VERTEX;
}
else if (boost::regex_match(line.c_str(), what, rx_g)) {
new_segment = true;
_groupNames.push_back(what[1].first);
}
else if (boost::regex_match(line.c_str(), what, rx_f3)) {
// starts a new segment
if (readvertices) {
readvertices = false;
if (new_segment) {
new_segment = false;
segment++;
}

Expand All @@ -353,8 +357,8 @@ bool MeshInput::LoadOBJ (std::istream &rstrIn)
}
else if (boost::regex_match(line.c_str(), what, rx_f4)) {
// starts a new segment
if (readvertices) {
readvertices = false;
if (new_segment) {
new_segment = false;
segment++;
}

Expand Down
6 changes: 5 additions & 1 deletion src/Mod/Mesh/App/Core/MeshIO.h
Expand Up @@ -70,7 +70,7 @@ struct MeshExport Material
};

/**
* The MeshInput class is able to read a mesh object from a input stream
* The MeshInput class is able to read a mesh object from an input stream
* in various formats.
*/
class MeshExport MeshInput
Expand All @@ -81,6 +81,9 @@ class MeshExport MeshInput
MeshInput (MeshKernel &rclM, Material* m)
: _rclMesh(rclM), _material(m){}
virtual ~MeshInput (void) { }
const std::vector<std::string>& GetGroupNames() const {
return _groupNames;
}

/// Loads the file, decided by extension
bool LoadAny(const char* FileName);
Expand Down Expand Up @@ -114,6 +117,7 @@ class MeshExport MeshInput
protected:
MeshKernel &_rclMesh; /**< reference to mesh data structure */
Material* _material;
std::vector<std::string> _groupNames;
};

/**
Expand Down
16 changes: 12 additions & 4 deletions src/Mod/Mesh/App/Mesh.cpp
Expand Up @@ -362,7 +362,7 @@ bool MeshObject::load(const char* file, MeshCore::Material* mat)
if (!aReader.LoadAny(file))
return false;

swapKernel(kernel);
swapKernel(kernel, aReader.GetGroupNames());
return true;
}

Expand All @@ -373,16 +373,17 @@ bool MeshObject::load(std::istream& str, MeshCore::MeshIO::Format f, MeshCore::M
if (!aReader.LoadFormat(str, f))
return false;

swapKernel(kernel);
swapKernel(kernel, aReader.GetGroupNames());
return true;
}

void MeshObject::swapKernel(MeshCore::MeshKernel& kernel)
void MeshObject::swapKernel(MeshCore::MeshKernel& kernel,
const std::vector<std::string>& g)
{
_kernel.Swap(kernel);
// Some file formats define several objects per file (e.g. OBJ).
// Now we mark each object as an own segment so that we can break
// the object into its orriginal objects again.
// the object into its original objects again.
this->_segments.clear();
const MeshCore::MeshFacetArray& faces = _kernel.GetFacets();
MeshCore::MeshFacetArray::_TConstIterator it;
Expand All @@ -407,6 +408,13 @@ void MeshObject::swapKernel(MeshCore::MeshKernel& kernel)
this->_segments.push_back(Segment(this,segment,true));
}

// apply the group names to the segments
if (this->_segments.size() == g.size()) {
for (std::size_t index = 0; index < this->_segments.size(); index++) {
this->_segments[index].setName(g[index]);
}
}

#ifndef FC_DEBUG
try {
MeshCore::MeshEvalNeighbourhood nb(_kernel);
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Mesh/App/Mesh.h
Expand Up @@ -376,7 +376,7 @@ class MeshExport MeshObject : public Data::ComplexGeoData
void deletedFacets(const std::vector<unsigned long>& remFacets);
void updateMesh(const std::vector<unsigned long>&);
void updateMesh();
void swapKernel(MeshCore::MeshKernel& m);
void swapKernel(MeshCore::MeshKernel& m, const std::vector<std::string>& g);

private:
Base::Matrix4D _Mtrx;
Expand Down
7 changes: 6 additions & 1 deletion src/Mod/Mesh/App/Segment.h
Expand Up @@ -25,6 +25,7 @@
#define MESH_SEGMENT_H

#include <vector>
#include <string>
#include "Facet.h"
#include "Core/Iterator.h"

Expand All @@ -41,17 +42,21 @@ class MeshExport Segment
void addIndices(const std::vector<unsigned long>& inds);
void removeIndices(const std::vector<unsigned long>& inds);
const std::vector<unsigned long>& getIndices() const;
bool isEmpty() const { return _indices.empty(); };
bool isEmpty() const { return _indices.empty(); }

const Segment& operator = (const Segment&);
bool operator == (const Segment&) const;

void setName(const std::string& n) { _name = n; }
const std::string& getName() const { return _name; }

// friends
friend class MeshObject;

private:
MeshObject* _mesh;
std::vector<unsigned long> _indices;
std::string _name;
bool _modifykernel;

public:
Expand Down

0 comments on commit cc78503

Please sign in to comment.