Skip to content

Commit fd3904d

Browse files
committed
[load_save] obj can save edges
1 parent 350210d commit fd3904d

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

vclib/core/include/vclib/load_save/obj/save.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <vclib/load_save/settings.h>
3232
#include <vclib/misc/logger.h>
3333
#include <vclib/space/complex/mesh_info.h>
34+
#include <vclib/space/core/texture.h>
3435

3536
#include <map>
3637

@@ -79,6 +80,21 @@ ObjMaterial objMaterialFromFace(
7980
return mat;
8081
}
8182

83+
template<EdgeConcept EdgeType, MeshConcept MeshType>
84+
ObjMaterial objMaterialFromEdge(const EdgeType& e, const MeshInfo& fi)
85+
{
86+
ObjMaterial mat;
87+
if constexpr (HasPerEdgeColor<MeshType>) {
88+
if (fi.hasEdgeColors()) {
89+
mat.hasColor = true;
90+
mat.Kd.x() = e.color().redF();
91+
mat.Kd.y() = e.color().greenF();
92+
mat.Kd.z() = e.color().blueF();
93+
}
94+
}
95+
return mat;
96+
}
97+
8298
template<
8399
ElementConcept ElementType,
84100
MeshConcept MeshType,
@@ -97,6 +113,7 @@ void writeElementObjMaterial(
97113
ObjMaterial mat;
98114
constexpr bool EL_IS_VERTEX = ElementType::ELEMENT_ID == ElemId::VERTEX;
99115
constexpr bool EL_IS_FACE = ElementType::ELEMENT_ID == ElemId::FACE;
116+
constexpr bool EL_IS_EDGE = ElementType::ELEMENT_ID == ElemId::EDGE;
100117

101118
if constexpr (EL_IS_VERTEX) {
102119
mat = objMaterialFromVertex<typename MeshType::VertexType, MeshType>(
@@ -105,6 +122,9 @@ void writeElementObjMaterial(
105122
if constexpr (EL_IS_FACE) {
106123
mat = objMaterialFromFace(e, m, fi);
107124
}
125+
if constexpr (EL_IS_EDGE) {
126+
mat = objMaterialFromEdge<typename MeshType::EdgeType, MeshType>(e, fi);
127+
}
108128
if (!mat.isEmpty()) {
109129
static const std::string MATERIAL_PREFIX = "MATERIAL_";
110130
std::string mname; // name of the material of the vertex
@@ -200,6 +220,9 @@ void saveObj(
200220

201221
// vertices
202222
using VertexType = MeshType::VertexType;
223+
224+
fp << std::endl << "# Vertices" << std::endl;
225+
203226
for (const VertexType& v : m.vertices()) {
204227
if (useMtl) { // mtl management
205228
detail::writeElementObjMaterial<VertexType, MeshType>(
@@ -243,6 +266,8 @@ void saveObj(
243266
using VertexType = MeshType::VertexType;
244267
using FaceType = MeshType::FaceType;
245268

269+
fp << std::endl << "# Faces" << std::endl;
270+
246271
// indices of vertices that do not consider deleted vertices
247272
std::vector<uint> vIndices = m.vertexCompactIndices();
248273

@@ -295,6 +320,34 @@ void saveObj(
295320
fp << std::endl;
296321
}
297322
}
323+
324+
if constexpr (HasEdges<MeshType>) {
325+
using VertexType = MeshType::VertexType;
326+
using EdgeType = MeshType::EdgeType;
327+
328+
fp << std::endl << "# Edges" << std::endl;
329+
330+
// indices of vertices that do not consider deleted vertices
331+
std::vector<uint> vIndices = m.vertexCompactIndices();
332+
333+
for (const EdgeType& e : m.edges()) {
334+
if (useMtl) { // mtl management
335+
detail::writeElementObjMaterial(
336+
e,
337+
m,
338+
meshInfo,
339+
lastMaterial,
340+
materialMap,
341+
fp,
342+
*mtlfp,
343+
settings,
344+
log);
345+
}
346+
fp << "l ";
347+
fp << vIndices[m.index(e.vertex(0))] + 1 << " ";
348+
fp << vIndices[m.index(e.vertex(1))] + 1 << std::endl;
349+
}
350+
}
298351
}
299352

300353
} // namespace detail

vclib/core/include/vclib/load_save/ply/detail/edge.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ void writePlyEdges(
6868
io::writeProperty(file, 0, p.type, format);
6969
}
7070
}
71+
if (!format.isBinary)
72+
file << std::endl;
7173
}
7274
}
7375

0 commit comments

Comments
 (0)