23#ifndef VCL_ALGORITHMS_MESH_IMPORT_EXPORT_EXPORT_BUFFER_H
24#define VCL_ALGORITHMS_MESH_IMPORT_EXPORT_EXPORT_BUFFER_H
26#include <vclib/algorithms/core/polygon/ear_cut.h>
27#include <vclib/mesh/requirements.h>
28#include <vclib/space/complex/tri_poly_index_bimap.h>
29#include <vclib/views/mesh.h>
50std::vector<uint> vertCompactIndices(
const auto& mesh,
bool wantCompact)
52 std::vector<uint> vertCompIndices;
54 bool isCompact = mesh.vertexNumber() == mesh.vertexContainerSize();
57 vertCompIndices = mesh.vertexCompactIndices();
58 return vertCompIndices;
62auto vIndexLambda(
const auto& mesh,
const std::vector<uint>& vertCompIndices)
65 auto vIndex = [&vertCompIndices](
const auto& f, uint i) {
66 if (vertCompIndices.size() > 0)
67 return vertCompIndices[f.vertexIndex(i)];
69 return f.vertexIndex(i);
75inline TriPolyIndexBiMap indexMap;
100template<MeshConcept MeshType>
102 const MeshType& mesh,
109 for (uint
i = 0;
const auto& c : mesh.vertices() | views::coords) {
110 if (
storage == MatrixStorageType::ROW_MAJOR) {
111 buffer[
i * 3 + 0] = c.x();
112 buffer[
i * 3 + 1] = c.y();
113 buffer[
i * 3 + 2] = c.z();
148template<FaceMeshConcept MeshType>
152 for (uint
i = 0;
const auto& f : mesh.faces()) {
153 buffer[
i] = f.vertexNumber();
154 sum += f.vertexNumber();
210template<FaceMeshConcept MeshType>
212 const MeshType& mesh,
222 for (uint
i = 0;
const auto& f : mesh.faces()) {
223 for (uint
j = 0;
j < f.vertexNumber(); ++
j) {
276template<FaceMeshConcept MeshType>
278 const MeshType& mesh,
280 uint largestFaceSize,
294 for (uint
i = 0;
const auto& f : mesh.faces()) {
295 for (uint
j = 0;
j < largestFaceSize; ++
j) {
296 uint index =
i * largestFaceSize +
j;
297 if (
storage == MatrixStorageType::COLUMN_MAJOR)
299 if (
j < f.vertexNumber()) {
356template<FaceMeshConcept MeshType>
358 const MeshType& mesh,
373 indexMap.
reserve(mesh.faceNumber(), mesh.faceContainerSize());
377 for (uint t = 0;
const auto& f : mesh.faces()) {
379 indexMap.
insert(t, f.index());
391 storage == MatrixStorageType::COLUMN_MAJOR &&
392 mesh.faceNumber() > 0) {
395 for (uint t = 0;
const auto& f : mesh.faces()) {
399 for (uint
vi = 0;
vi <
vind.size();
vi += 3) {
401 indexMap.
insert(t, f.index());
403 if (
storage == MatrixStorageType::ROW_MAJOR) {
450template<EdgeMeshConcept MeshType>
452 const MeshType& mesh,
467 for (uint
i = 0;
const auto& e : mesh.edges()) {
468 if (
storage == MatrixStorageType::ROW_MAJOR) {
469 buffer[
i * 2 + 0] =
vIndex(e, 0);
470 buffer[
i * 2 + 1] =
vIndex(e, 1);
509template<FaceMeshConcept MeshType>
511 const MeshType& mesh,
526 for (uint
i = 0;
const auto& f : mesh.faces()) {
527 for (uint
j = 0;
j < f.vertexNumber(); ++
j) {
529 uint v1 =
vIndex(f, (
j + 1) % f.vertexNumber());
530 if (
storage == MatrixStorageType::ROW_MAJOR) {
531 buffer[
i * 2 + 0] =
v0;
532 buffer[
i * 2 + 1] = v1;
570template<u
int ELEM_ID, MeshConcept MeshType>
571void elementSelectionToBuffer(
const MeshType& mesh,
auto* buffer)
573 for (uint i = 0;
const auto& e : mesh.template elements<ELEM_ID>()) {
574 buffer[i] = e.selected();
604template<MeshConcept MeshType>
605void vertexSelectionToBuffer(
const MeshType& mesh,
auto* buffer)
607 elementSelectionToBuffer<ElemId::VERTEX>(mesh, buffer);
635template<FaceMeshConcept MeshType>
636void faceSelectionToBuffer(
const MeshType& mesh,
auto* buffer)
638 elementSelectionToBuffer<ElemId::FACE>(mesh, buffer);
666template<EdgeMeshConcept MeshType>
667void edgeSelectionToBuffer(
const MeshType& mesh,
auto* buffer)
669 elementSelectionToBuffer<ElemId::EDGE>(mesh, buffer);
694template<u
int ELEM_ID, MeshConcept MeshType>
695void elementNormalsToBuffer(
696 const MeshType& mesh,
701 requirePerElementComponent<ELEM_ID, CompId::NORMAL>(mesh);
703 const uint ELEM_NUM =
704 rowNumber ==
UINT_NULL ? mesh.template number<ELEM_ID>() : rowNumber;
707 const auto& n : mesh.template elements<ELEM_ID>() | views::normals) {
708 if (storage == MatrixStorageType::ROW_MAJOR) {
709 buffer[i * 3 + 0] = n.x();
710 buffer[i * 3 + 1] = n.y();
711 buffer[i * 3 + 2] = n.z();
714 buffer[0 * ELEM_NUM + i] = n.x();
715 buffer[1 * ELEM_NUM + i] = n.y();
716 buffer[2 * ELEM_NUM + i] = n.z();
743template<MeshConcept MeshType>
744void vertexNormalsToBuffer(
745 const MeshType& mesh,
750 elementNormalsToBuffer<ElemId::VERTEX>(mesh, buffer, storage, rowNumber);
773template<FaceMeshConcept MeshType>
774void faceNormalsToBuffer(
775 const MeshType& mesh,
780 elementNormalsToBuffer<ElemId::FACE>(mesh, buffer, storage, rowNumber);
807template<FaceMeshConcept MeshType>
808void triangulatedFaceNormalsToBuffer(
809 const MeshType& mesh,
811 const TriPolyIndexBiMap& indexMap,
817 const uint FACE_NUM =
820 for (
const auto& f : mesh.
faces()) {
821 const auto& n = f.normal();
824 for (uint t = first; t < last; ++t) {
825 if (storage == MatrixStorageType::ROW_MAJOR) {
826 buffer[t * 3 + 0] = n.x();
827 buffer[t * 3 + 1] = n.y();
828 buffer[t * 3 + 2] = n.z();
831 buffer[0 * FACE_NUM + t] = n.x();
832 buffer[1 * FACE_NUM + t] = n.y();
833 buffer[2 * FACE_NUM + t] = n.z();
859template<EdgeMeshConcept MeshType>
860void edgeNormalsToBuffer(
861 const MeshType& mesh,
866 elementNormalsToBuffer<ElemId::EDGE>(mesh, buffer, storage, rowNumber);
893template<u
int ELEM_ID, MeshConcept MeshType>
894void elementColorsToBuffer(
895 const MeshType& mesh,
901 requirePerElementComponent<ELEM_ID, CompId::COLOR>(mesh);
903 const bool R_INT = representation == Color::Representation::INT_0_255;
905 const uint ELEM_NUM =
906 rowNumber ==
UINT_NULL ? mesh.template number<ELEM_ID>() : rowNumber;
909 const auto& c : mesh.template elements<ELEM_ID>() | views::colors) {
910 if (storage == MatrixStorageType::ROW_MAJOR) {
911 buffer[i * 4 + 0] = R_INT ? c.red() : c.redF();
912 buffer[i * 4 + 1] = R_INT ? c.green() : c.greenF();
913 buffer[i * 4 + 2] = R_INT ? c.blue() : c.blueF();
914 buffer[i * 4 + 3] = R_INT ? c.alpha() : c.alphaF();
917 buffer[0 * ELEM_NUM + i] = R_INT ? c.red() : c.redF();
918 buffer[1 * ELEM_NUM + i] = R_INT ? c.green() : c.greenF();
919 buffer[2 * ELEM_NUM + i] = R_INT ? c.blue() : c.blueF();
920 buffer[3 * ELEM_NUM + i] = R_INT ? c.alpha() : c.alphaF();
947template<u
int ELEM_ID, MeshConcept MeshType>
948void elementColorsToBuffer(
949 const MeshType& mesh,
953 requirePerElementComponent<ELEM_ID, CompId::COLOR>(mesh);
956 const auto& c : mesh.template elements<ELEM_ID>() | views::colors) {
957 switch (colorFormat) {
959 case ABGR: buffer[i] = c.abgr();
break;
960 case ARGB: buffer[i] = c.argb();
break;
961 case RGBA: buffer[i] = c.rgba();
break;
962 case BGRA: buffer[i] = c.bgra();
break;
992template<MeshConcept MeshType>
993void vertexColorsToBuffer(
994 const MeshType& mesh,
1000 elementColorsToBuffer<ElemId::VERTEX>(
1001 mesh, buffer, storage, representation, rowNumber);
1024template<MeshConcept MeshType>
1025void vertexColorsToBuffer(
1026 const MeshType& mesh,
1030 elementColorsToBuffer<ElemId::VERTEX>(mesh, buffer, colorFormat);
1056template<FaceMeshConcept MeshType>
1057void faceColorsToBuffer(
1058 const MeshType& mesh,
1064 elementColorsToBuffer<ElemId::FACE>(
1065 mesh, buffer, storage, representation, rowNumber);
1094template<FaceMeshConcept MeshType>
1095void triangulatedFaceColorsToBuffer(
1096 const MeshType& mesh,
1098 const TriPolyIndexBiMap& indexMap,
1105 const bool R_INT = representation == Color::Representation::INT_0_255;
1107 const uint FACE_NUM =
1110 for (
const auto& f : mesh.
faces()) {
1111 const auto& c = f.color();
1114 for (uint t = first; t < last; ++t) {
1115 if (storage == MatrixStorageType::ROW_MAJOR) {
1116 buffer[t * 4 + 0] = R_INT ? c.red() : c.redF();
1117 buffer[t * 4 + 1] = R_INT ? c.green() : c.greenF();
1118 buffer[t * 4 + 2] = R_INT ? c.blue() : c.blueF();
1119 buffer[t * 4 + 3] = R_INT ? c.alpha() : c.alphaF();
1122 buffer[0 * FACE_NUM + t] = R_INT ? c.red() : c.redF();
1123 buffer[1 * FACE_NUM + t] = R_INT ? c.green() : c.greenF();
1124 buffer[2 * FACE_NUM + t] = R_INT ? c.blue() : c.blueF();
1125 buffer[3 * FACE_NUM + t] = R_INT ? c.alpha() : c.alphaF();
1151template<FaceMeshConcept MeshType>
1152void faceColorsToBuffer(
1153 const MeshType& mesh,
1157 elementColorsToBuffer<ElemId::FACE>(mesh, buffer, colorFormat);
1183template<FaceMeshConcept MeshType>
1184void triangulatedFaceColorsToBuffer(
1185 const MeshType& mesh,
1187 const TriPolyIndexBiMap& indexMap,
1190 requirePerElementComponent<ElemId::FACE, CompId::COLOR>(mesh);
1192 for (
const auto& f : mesh.
faces()) {
1193 const auto& c = f.color();
1196 for (uint t = first; t < last; ++t) {
1197 switch (colorFormat) {
1199 case ABGR: buffer[t] = c.abgr();
break;
1200 case ARGB: buffer[t] = c.argb();
break;
1201 case RGBA: buffer[t] = c.rgba();
break;
1202 case BGRA: buffer[t] = c.bgra();
break;
1231template<EdgeMeshConcept MeshType>
1232void edgeColorsToBuffer(
1233 const MeshType& mesh,
1239 elementColorsToBuffer<ElemId::EDGE>(
1240 mesh, buffer, storage, representation, rowNumber);
1263template<EdgeMeshConcept MeshType>
1264void edgeColorsToBuffer(
1265 const MeshType& mesh,
1269 elementColorsToBuffer<ElemId::EDGE>(mesh, buffer, colorFormat);
1291template<u
int ELEM_ID, MeshConcept MeshType>
1292void elementQualityToBuffer(
const MeshType& mesh,
auto* buffer)
1294 requirePerElementComponent<ELEM_ID, CompId::QUALITY>(mesh);
1297 const auto& q : mesh.template elements<ELEM_ID>() | views::quality) {
1321template<MeshConcept MeshType>
1322void vertexQualityToBuffer(
const MeshType& mesh,
auto* buffer)
1324 elementQualityToBuffer<ElemId::VERTEX>(mesh, buffer);
1344template<MeshConcept MeshType>
1345void faceQualityToBuffer(
const MeshType& mesh,
auto* buffer)
1347 elementQualityToBuffer<ElemId::FACE>(mesh, buffer);
1367template<MeshConcept MeshType>
1368void edgeQualityToBuffer(
const MeshType& mesh,
auto* buffer)
1370 elementQualityToBuffer<ElemId::FACE>(mesh, buffer);
1394template<MeshConcept MeshType>
1395void vertexTexCoordsToBuffer(
1396 const MeshType& mesh,
1401 requirePerVertexTexCoord(mesh);
1403 const uint VERT_NUM =
1404 rowNumber ==
UINT_NULL ? mesh.vertexNumber() : rowNumber;
1406 for (uint i = 0;
const auto& t : mesh.vertices() | views::texCoords) {
1407 if (storage == MatrixStorageType::ROW_MAJOR) {
1408 buffer[i * 2 + 0] = t.u();
1409 buffer[i * 2 + 1] = t.v();
1412 buffer[0 * VERT_NUM + i] = t.u();
1413 buffer[1 * VERT_NUM + i] = t.v();
1437template<MeshConcept MeshType>
1438void vertexTexCoordIndicesToBuffer(
const MeshType& mesh,
auto* buffer)
1440 requirePerVertexTexCoord(mesh);
1442 for (uint i = 0;
const auto& t : mesh.vertices() | views::texCoords) {
1443 buffer[i] = t.index();
1471template<FaceMeshConcept MeshType>
1472void vertexTexCoordIndicesAsFaceTexCoordIndicesToBuffer(
1473 const MeshType& mesh,
1476 requirePerVertexTexCoord(mesh);
1478 for (uint i = 0;
const auto& f : mesh.faces()) {
1479 ushort ti = f.vertex(0)->texCoord()->index();
1509template<FaceMeshConcept MeshType>
1510void vertexTexCoordIndicesAsTriangulatedFaceTexCoordIndicesToBuffer(
1511 const MeshType& mesh,
1513 const TriPolyIndexBiMap& indexMap)
1515 requirePerVertexTexCoord(mesh);
1517 for (
const auto& f : mesh.
faces()) {
1518 ushort ti = f.vertex(0)->texCoord().index();
1521 for (uint t = first; t < last; ++t) {
1564template<FaceMeshConcept MeshType>
1566 const MeshType& mesh,
1568 uint largestFaceSize = 3,
1577 for (uint
i = 0;
const auto& f : mesh.faces()) {
1578 for (uint
j = 0;
j < largestFaceSize * 2; ++
j) {
1580 uint index =
i * largestFaceSize * 2 +
j;
1581 if (
storage == MatrixStorageType::COLUMN_MAJOR)
1583 if (
fi < f.wedgeTexCoordsNumber()) {
1584 const auto& w = f.wedgeTexCoord(
fi);
1586 buffer[index] = w.u();
1588 buffer[index] = w.v();
1616template<FaceMeshConcept MeshType>
1621 for (uint
i = 0;
const auto& f : mesh.faces()) {
1622 buffer[
i] = f.textureIndex();
1648template<FaceMeshConcept MeshType>
1650 const MeshType& mesh,
1656 for (
const auto& f : mesh.faces()) {
1659 for (uint t = first; t <
last; ++t) {
1660 buffer[t] = f.textureIndex();
1709template<FaceMeshConcept MeshType>
1711 const MeshType& mesh,
1712 const std::vector<std::pair<vcl::uint, vcl::uint>>&
vertWedgeMap,
1713 const std::list<std::list<std::pair<vcl::uint, vcl::uint>>>&
1718 using TexCoordType =
typename MeshType::FaceType::WedgeTexCoordType;
1727 for (
const auto& v : mesh.vertices()) {
1734 w = mesh.face(
fInd).wedgeTexCoord(
wInd);
1736 if (
storage == MatrixStorageType::ROW_MAJOR) {
1737 buffer[
vi * 2 + 0] = w.u();
1738 buffer[
vi * 2 + 1] = w.v();
1750 assert(list.begin() != list.end());
1751 const auto&
p = list.front();
1752 uint
fInd =
p.first;
1753 uint
wInd =
p.second;
1755 const auto& w = mesh.face(
fInd).wedgeTexCoord(
wInd);
1756 if (
storage == MatrixStorageType::ROW_MAJOR) {
1757 buffer[
vi * 2 + 0] = w.u();
1758 buffer[
vi * 2 + 1] = w.v();
1812template<FaceMeshConcept MeshType>
1814 const MeshType& mesh,
1815 const std::vector<std::pair<vcl::uint, vcl::uint>>&
vertWedgeMap,
1816 const std::list<std::list<std::pair<vcl::uint, vcl::uint>>>&
1827 for (
const auto& v : mesh.vertices()) {
1837 assert(list.begin() != list.end());
1838 const auto&
p = list.front();
1839 uint
fInd =
p.first;
Format
Color format enumeration.
Definition color.h:77
Representation
Color representation enumeration.
Definition color.h:64
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
The TriPolyIndexBiMap class allows to store a bidirectional mapping between a Polygon Mesh and a Tria...
Definition tri_poly_index_bimap.h:50
void reserve(uint nTriangles, uint nPolygons)
Reserves enogh memory for the BiMap. Allows fast insertions.
Definition tri_poly_index_bimap.h:155
void insert(uint triangleIndex, uint polygonIndex)
Performs an insertion into the BiMap, and associates:
Definition tri_poly_index_bimap.h:175
void clear()
Clears the BiMap.
Definition tri_poly_index_bimap.h:142
uint triangleBegin(uint polygonIndex) const
Returns the smallest index of set of triangles mapped to the polygon having the index given as input ...
Definition tri_poly_index_bimap.h:83
uint triangleNumber(uint polygonIndex) const
Returns the number of (consecutive index) triangles mapped to a polygon.
Definition tri_poly_index_bimap.h:112
Definition mesh_concept.h:221
void wedgeTexCoordsAsDuplicatedVertexTexCoordsToBuffer(const MeshType &mesh, const std::vector< std::pair< vcl::uint, vcl::uint > > &vertWedgeMap, const std::list< std::list< std::pair< vcl::uint, vcl::uint > > > &facesToReassign, auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR)
Export wedge texture coordinates to a buffer of the duplicated vertex texture coordinates.
Definition export_buffer.h:1710
void wedgeTexCoordIndicesAsDuplicatedVertexTexCoordIndicesToBuffer(const MeshType &mesh, const std::vector< std::pair< vcl::uint, vcl::uint > > &vertWedgeMap, const std::list< std::list< std::pair< vcl::uint, vcl::uint > > > &facesToReassign, auto *buffer)
Export wedge texture coordinate indices to a buffer of the duplicated vertex texture coordinate indic...
Definition export_buffer.h:1813
std::vector< uint > earCut(Iterator begin, Iterator end)
Triangulates a simple polygon with no holes using the ear-cutting algorithm.
Definition ear_cut.h:92
void triangulatedFaceIndicesToBuffer(const MeshType &mesh, auto *buffer, TriPolyIndexBiMap &indexMap=detail::indexMap, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint numTriangles=UINT_NULL, bool getIndicesAsIfContainerCompact=true)
Export into a buffer the vertex indices for each triangle computed by triangulating the faces of a Me...
Definition export_buffer.h:357
void triangulatedFaceWedgeTexCoordIndicesToBuffer(const MeshType &mesh, auto *buffer, const TriPolyIndexBiMap &indexMap)
Export into a buffer the per triangle wedge texture indices of a mesh. Triangles are computed by tria...
Definition export_buffer.h:1649
void edgeIndicesToBuffer(const MeshType &mesh, auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, bool getIndicesAsIfContainerCompact=true, uint rowNumber=UINT_NULL)
Export into a buffer the vertex indices for each edge of a Mesh.
Definition export_buffer.h:451
void vertexCoordsToBuffer(const MeshType &mesh, auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint rowNumber=UINT_NULL)
Export the vertex coordinates of a mesh to a buffer.
Definition export_buffer.h:101
void faceIndicesToBuffer(const MeshType &mesh, auto *buffer, bool getIndicesAsIfContainerCompact=true)
Export into a buffer the vertex indices for each face of a Mesh. Faces can be polygons.
Definition export_buffer.h:211
void faceWedgeTexCoordIndicesToBuffer(const MeshType &mesh, auto *buffer)
Export into a buffer the per face wedge texture indices of a mesh.
Definition export_buffer.h:1617
void faceWedgeTexCoordsToBuffer(const MeshType &mesh, auto *buffer, uint largestFaceSize=3, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint rowNumber=UINT_NULL)
Export the selection status of the elements identified by ELEM_ID of a mesh to a buffer.
Definition export_buffer.h:1565
uint faceSizesToBuffer(const MeshType &mesh, auto *buffer)
Export into a buffer the sizes of the faces of a Mesh, and return the sum of the sizes.
Definition export_buffer.h:149
void wireframeIndicesToBuffer(const MeshType &mesh, auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, bool getIndicesAsIfContainerCompact=true, uint rowNumber=UINT_NULL)
Export into a buffer the vertex indices for each edge that composes the wireframe of the Mesh (i....
Definition export_buffer.h:510
void requirePerFaceWedgeTexCoords(const MeshType &m)
This function asserts that a Mesh has a FaceContainer, the Face has a WedgeTexCoords Component,...
Definition face_requirements.h:947
void requirePerFaceNormal(const MeshType &m)
This function asserts that a Mesh has a FaceContainer, the Face has a Normal Component,...
Definition face_requirements.h:804
void requirePerFaceColor(const MeshType &m)
This function asserts that a Mesh has a FaceContainer, the Face has a Color Component,...
Definition face_requirements.h:734
bool isCompact(const MeshType &m)
Checks if a Mesh is compact, that is if it does not contains deleted elements.
Definition mesh_requirements.h:118
MatrixStorageType
A simple type that enumerates the main storage types for matrices (row or column major).
Definition base.h:76
constexpr uint UINT_NULL
The UINT_NULL value represent a null value of uint that is the maximum value that can be represented ...
Definition base.h:48
constexpr detail::FacesView faces
A view that allows to iterate overt the Face elements of an object.
Definition face.h:52