Skip to content

Commit

Permalink
[mesh] TexCoord component uses TexCoordIndexed class
Browse files Browse the repository at this point in the history
  • Loading branch information
alemuntoni committed Jan 27, 2025
1 parent b4afba3 commit 482d155
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 12 deletions.
15 changes: 15 additions & 0 deletions tests/core/006-load-save-mesh-obj/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ TEMPLATE_TEST_CASE(
REQUIRE(tm.faceNumber() == 12);
}

SECTION("TriMesh - Wedge TextureDouble")
{
TriMesh tm;
vcl::loadObj(tm, VCLIB_EXAMPLE_MESHES_PATH "/TextureDouble.obj");
REQUIRE(tm.vertexNumber() == 8);
REQUIRE(tm.faceNumber() == 4);
REQUIRE(tm.textureNumber() == 2);
REQUIRE(tm.isPerFaceWedgeTexCoordsEnabled());
for(const auto& f : tm.faces())
{
// first two faces have texture index 0, the other two have index 1
REQUIRE(f.textureIndex() == f.index() / 2);
}
}

SECTION("PolyMesh - PolyCube")
{
PolyMesh pm;
Expand Down
30 changes: 30 additions & 0 deletions tests/core/008-load-save-mesh-ply/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,36 @@ TEMPLATE_TEST_CASE(
REQUIRE(tm.faceNumber() == 12);
}

SECTION("TriMesh - VertTextureDouble")
{
TriMesh tm;
vcl::loadPly(tm, VCLIB_EXAMPLE_MESHES_PATH "/VertTextureDouble.ply");
REQUIRE(tm.vertexNumber() == 8);
REQUIRE(tm.faceNumber() == 4);
REQUIRE(tm.textureNumber() == 2);
REQUIRE(tm.isPerVertexTexCoordEnabled());
for(const auto& v : tm.vertices())
{
// first four vertices have index 0, the other four have index 1
REQUIRE(v.texCoord().index() == v.index() / 4);
}
}

SECTION("TriMesh - Wedge TextureDouble")
{
TriMesh tm;
vcl::loadPly(tm, VCLIB_EXAMPLE_MESHES_PATH "/TextureDouble.ply");
REQUIRE(tm.vertexNumber() == 8);
REQUIRE(tm.faceNumber() == 4);
REQUIRE(tm.textureNumber() == 2);
REQUIRE(tm.isPerFaceWedgeTexCoordsEnabled());
for(const auto& f : tm.faces())
{
// first two faces have texture index 0, the other two have index 1
REQUIRE(f.textureIndex() == f.index() / 2);
}
}

SECTION("PolyMesh - PolyCube")
{
PolyMesh pm;
Expand Down
13 changes: 8 additions & 5 deletions vclib/core/include/vclib/load_save/obj/load.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void readObjFace(
MeshType& m,
MeshInfo& loadedInfo,
const Tokenizer& tokens,
const std::vector<TexCoordd>& wedgeTexCoords,
const std::vector<TexCoordIndexedd>& wedgeTexCoords,
const ObjMaterial& currentMaterial,
const LoadSettings& settings)
{
Expand Down Expand Up @@ -380,7 +380,7 @@ void readObjFace(
std::to_string(fid));
}
f.wedgeTexCoord(i) =
wedgeTexCoords[wids[i]]
((vcl::TexCoordd)wedgeTexCoords[wids[i]])
.cast<typename FaceType::WedgeTexCoordType::
ScalarType>();
if (currentMaterial.hasTexture) {
Expand Down Expand Up @@ -409,7 +409,7 @@ void readObjFace(
// set the wedge texcoord in the same position of
// the vertex
f.wedgeTexCoord(i) =
wedgeTexCoords[wids[pos]]
((vcl::TexCoordd)wedgeTexCoords[wids[pos]])
.cast<typename FaceType::WedgeTexCoordType::
ScalarType>();
if (currentMaterial.hasTexture) {
Expand Down Expand Up @@ -507,7 +507,7 @@ void loadObj(
uint vn = 0; // number of vertex normals read
// save array of texcoords, that are stored later (into wedges when loading
// faces or into vertices as a fallback)
std::vector<TexCoordd> texCoords;
std::vector<TexCoordIndexedd> texCoords;

// map of materials loaded
std::map<std::string, detail::ObjMaterial> materialMap;
Expand Down Expand Up @@ -588,10 +588,13 @@ void loadObj(
HasPerFaceWedgeTexCoords<MeshType>) {
if (header == "vt") {
// save the texcoord for later
TexCoordd tf;
TexCoordIndexedd tf;
for (uint i = 0; i < 2; ++i) {
tf[i] = io::readDouble<double>(token);
}
if (currentMaterial.hasTexture) {
tf.index() = currentMaterial.mapId;
}
texCoords.push_back(tf);
}
}
Expand Down
15 changes: 13 additions & 2 deletions vclib/core/include/vclib/load_save/obj/save.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ namespace vcl {
namespace detail {

template<VertexConcept VertexType, MeshConcept MeshType>
ObjMaterial objMaterialFromVertex(const VertexType& v, const MeshInfo& fi)
ObjMaterial objMaterialFromVertex(
const VertexType& v,
const MeshType& m,
const MeshInfo& fi)
{
ObjMaterial mat;
if constexpr (HasPerVertexColor<MeshType>) {
Expand All @@ -51,6 +54,14 @@ ObjMaterial objMaterialFromVertex(const VertexType& v, const MeshInfo& fi)
mat.Kd.z() = v.color().blueF();
}
}
if constexpr(HasPerVertexTexCoord<MeshType>) {
if (fi.hasVertexTexCoords()) {
mat.hasTexture = true;
if constexpr (HasTexturePaths<MeshType>) {
mat.map_Kd = m.texturePath(v.texCoord().index());
}
}
}
return mat;
}

Expand Down Expand Up @@ -117,7 +128,7 @@ void writeElementObjMaterial(

if constexpr (EL_IS_VERTEX) {
mat = objMaterialFromVertex<typename MeshType::VertexType, MeshType>(
e, fi);
e, m, fi);
}
if constexpr (EL_IS_FACE) {
mat = objMaterialFromFace(e, m, fi);
Expand Down
16 changes: 16 additions & 0 deletions vclib/core/include/vclib/load_save/ply/detail/vertex.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ void readPlyVertexProperty(
}
}
}
if (p.name == ply::texnumber) {
if constexpr (HasPerVertexTexCoord<MeshType>) {
if (isPerVertexTexCoordAvailable(mesh)) {
v.texCoord().index() =
io::readPrimitiveType<ushort>(file, p.type, end);
hasBeenRead = true;
}
}
}
if (p.name == ply::unknown) {
if constexpr (HasPerVertexCustomComponents<MeshType>) {
if (mesh.hasPerVertexCustomComponent(p.unknownPropertyName)) {
Expand Down Expand Up @@ -193,6 +202,13 @@ void writePlyVertices(
hasBeenWritten = true;
}
}
if (p.name == ply::texnumber) {
if constexpr (HasPerVertexTexCoord<MeshType>) {
io::writeProperty(
file, v.texCoord().index(), p.type, format);
hasBeenWritten = true;
}
}
if (p.name == ply::unknown) {
if constexpr (HasPerVertexCustomComponents<MeshType>) {
if (mesh.hasPerVertexCustomComponent(
Expand Down
10 changes: 5 additions & 5 deletions vclib/core/include/vclib/mesh/components/tex_coord.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
#include "bases/component.h"

#include <vclib/concepts/mesh/components/tex_coord.h>
#include <vclib/space/core/tex_coord.h>
#include <vclib/space/core/tex_coord_indexed.h>

namespace vcl::comp {

/**
* @brief The TexCoord class represents a component that stores a texture
* coordinate.
*
* It exposes a vcl::TexCoord object, that stores a texture coordinate and an
* It exposes a vcl::TexCoordIndexed object, that stores a texture coordinate and an
* id of the texture to use.
*
* For example, if you have a Vertex Element `v` with the TexCoord component,
Expand All @@ -59,15 +59,15 @@ class TexCoord :
public Component<
TexCoord<Scalar, ParentElemType, OPT>,
CompId::TEX_COORD,
vcl::TexCoord<Scalar>,
vcl::TexCoordIndexed<Scalar>,
ParentElemType,
!std::is_same_v<ParentElemType, void>,
OPT>
{
using Base = Component<
TexCoord<Scalar, ParentElemType, OPT>,
CompId::TEX_COORD,
vcl::TexCoord<Scalar>,
vcl::TexCoordIndexed<Scalar>,
ParentElemType,
!std::is_same_v<ParentElemType, void>,
OPT>;
Expand All @@ -76,7 +76,7 @@ class TexCoord :
/**
* @brief Expose the type of the TexCoord.
*/
using TexCoordType = vcl::TexCoord<Scalar>;
using TexCoordType = vcl::TexCoordIndexed<Scalar>;

/* Constructors */

Expand Down

0 comments on commit 482d155

Please sign in to comment.