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.h>
27#include <vclib/mesh.h>
28#include <vclib/space/complex.h>
49std::vector<uint> vertCompactIndices(
const auto& mesh,
bool wantCompact)
51 std::vector<uint> vertCompIndices;
53 bool isCompact = mesh.vertexNumber() == mesh.vertexContainerSize();
56 vertCompIndices = mesh.vertexCompactIndices();
57 return vertCompIndices;
61auto vIndexLambda(
const auto& mesh,
const std::vector<uint>& vertCompIndices)
64 auto vIndex = [&vertCompIndices](
const auto& f, uint i) {
65 if (vertCompIndices.size() > 0)
66 return vertCompIndices[f.vertexIndex(i)];
68 return f.vertexIndex(i);
74inline TriPolyIndexBiMap indexMap;
99template<MeshConcept MeshType>
101 const MeshType& mesh,
108 for (uint
i = 0;
const auto& c : mesh.vertices() | views::positions) {
109 if (
storage == MatrixStorageType::ROW_MAJOR) {
110 buffer[
i * 3 + 0] = c.x();
111 buffer[
i * 3 + 1] = c.y();
112 buffer[
i * 3 + 2] = c.z();
141template<MeshConcept MeshType>
157 for (uint
i = 0;
const auto& v : mesh.vertices()) {
194template<FaceMeshConcept MeshType>
198 for (uint
i = 0;
const auto& f : mesh.faces()) {
199 buffer[
i] = f.vertexNumber();
200 sum += f.vertexNumber();
256template<FaceMeshConcept MeshType>
258 const MeshType& mesh,
268 for (uint
i = 0;
const auto& f : mesh.faces()) {
269 for (uint
j = 0;
j < f.vertexNumber(); ++
j) {
322template<FaceMeshConcept MeshType>
324 const MeshType& mesh,
340 for (uint
i = 0;
const auto& f : mesh.faces()) {
343 if (
storage == MatrixStorageType::COLUMN_MAJOR)
345 if (
j < f.vertexNumber()) {
402template<FaceMeshConcept MeshType>
404 const MeshType& mesh,
419 indexMap.
reserve(mesh.faceNumber(), mesh.faceContainerSize());
423 for (uint t = 0;
const auto& f : mesh.faces()) {
425 indexMap.
insert(t, f.index());
437 storage == MatrixStorageType::COLUMN_MAJOR &&
438 mesh.faceNumber() > 0) {
441 for (uint t = 0;
const auto& f : mesh.faces()) {
447 indexMap.
insert(t, f.index());
449 if (
storage == MatrixStorageType::ROW_MAJOR) {
496template<EdgeMeshConcept MeshType>
498 const MeshType& mesh,
513 for (uint
i = 0;
const auto& e : mesh.edges()) {
514 if (
storage == MatrixStorageType::ROW_MAJOR) {
515 buffer[
i * 2 + 0] =
vIndex(e, 0);
516 buffer[
i * 2 + 1] =
vIndex(e, 1);
555template<FaceMeshConcept MeshType>
557 const MeshType& mesh,
572 for (uint
i = 0;
const auto& f : mesh.faces()) {
573 for (uint
j = 0;
j < f.vertexNumber(); ++
j) {
575 uint v1 =
vIndex(f, (
j + 1) % f.vertexNumber());
576 if (
storage == MatrixStorageType::ROW_MAJOR) {
577 buffer[
i * 2 + 0] =
v0;
578 buffer[
i * 2 + 1] = v1;
616template<u
int ELEM_ID, MeshConcept MeshType>
617void elementSelectionToBuffer(
const MeshType& mesh,
auto* buffer)
619 for (uint i = 0;
const auto& e : mesh.template elements<ELEM_ID>()) {
620 buffer[i] = e.selected();
650template<MeshConcept MeshType>
651void vertexSelectionToBuffer(
const MeshType& mesh,
auto* buffer)
653 elementSelectionToBuffer<ElemId::VERTEX>(mesh, buffer);
681template<FaceMeshConcept MeshType>
682void faceSelectionToBuffer(
const MeshType& mesh,
auto* buffer)
684 elementSelectionToBuffer<ElemId::FACE>(mesh, buffer);
712template<EdgeMeshConcept MeshType>
713void edgeSelectionToBuffer(
const MeshType& mesh,
auto* buffer)
715 elementSelectionToBuffer<ElemId::EDGE>(mesh, buffer);
740template<u
int ELEM_ID, MeshConcept MeshType>
741void elementNormalsToBuffer(
742 const MeshType& mesh,
747 requirePerElementComponent<ELEM_ID, CompId::NORMAL>(mesh);
749 const uint ELEM_NUM =
750 rowNumber ==
UINT_NULL ? mesh.template number<ELEM_ID>() : rowNumber;
753 const auto& n : mesh.template elements<ELEM_ID>() | views::normals) {
754 if (storage == MatrixStorageType::ROW_MAJOR) {
755 buffer[i * 3 + 0] = n.x();
756 buffer[i * 3 + 1] = n.y();
757 buffer[i * 3 + 2] = n.z();
760 buffer[0 * ELEM_NUM + i] = n.x();
761 buffer[1 * ELEM_NUM + i] = n.y();
762 buffer[2 * ELEM_NUM + i] = n.z();
789template<MeshConcept MeshType>
790void vertexNormalsToBuffer(
791 const MeshType& mesh,
796 elementNormalsToBuffer<ElemId::VERTEX>(mesh, buffer, storage, rowNumber);
819template<FaceMeshConcept MeshType>
820void faceNormalsToBuffer(
821 const MeshType& mesh,
826 elementNormalsToBuffer<ElemId::FACE>(mesh, buffer, storage, rowNumber);
853template<FaceMeshConcept MeshType>
854void triangulatedFaceNormalsToBuffer(
855 const MeshType& mesh,
857 const TriPolyIndexBiMap& indexMap,
863 const uint FACE_NUM =
866 for (
const auto& f : mesh.
faces()) {
867 const auto& n = f.normal();
870 for (uint t = first; t < last; ++t) {
871 if (storage == MatrixStorageType::ROW_MAJOR) {
872 buffer[t * 3 + 0] = n.x();
873 buffer[t * 3 + 1] = n.y();
874 buffer[t * 3 + 2] = n.z();
877 buffer[0 * FACE_NUM + t] = n.x();
878 buffer[1 * FACE_NUM + t] = n.y();
879 buffer[2 * FACE_NUM + t] = n.z();
905template<EdgeMeshConcept MeshType>
906void edgeNormalsToBuffer(
907 const MeshType& mesh,
912 elementNormalsToBuffer<ElemId::EDGE>(mesh, buffer, storage, rowNumber);
939template<u
int ELEM_ID, MeshConcept MeshType>
940void elementColorsToBuffer(
941 const MeshType& mesh,
947 requirePerElementComponent<ELEM_ID, CompId::COLOR>(mesh);
949 const bool R_INT = representation == Color::Representation::INT_0_255;
951 const uint ELEM_NUM =
952 rowNumber ==
UINT_NULL ? mesh.template number<ELEM_ID>() : rowNumber;
955 const auto& c : mesh.template elements<ELEM_ID>() | views::colors) {
956 if (storage == MatrixStorageType::ROW_MAJOR) {
957 buffer[i * 4 + 0] = R_INT ? c.red() : c.redF();
958 buffer[i * 4 + 1] = R_INT ? c.green() : c.greenF();
959 buffer[i * 4 + 2] = R_INT ? c.blue() : c.blueF();
960 buffer[i * 4 + 3] = R_INT ? c.alpha() : c.alphaF();
963 buffer[0 * ELEM_NUM + i] = R_INT ? c.red() : c.redF();
964 buffer[1 * ELEM_NUM + i] = R_INT ? c.green() : c.greenF();
965 buffer[2 * ELEM_NUM + i] = R_INT ? c.blue() : c.blueF();
966 buffer[3 * ELEM_NUM + i] = R_INT ? c.alpha() : c.alphaF();
993template<u
int ELEM_ID, MeshConcept MeshType>
994void elementColorsToBuffer(
995 const MeshType& mesh,
999 requirePerElementComponent<ELEM_ID, CompId::COLOR>(mesh);
1002 const auto& c : mesh.template elements<ELEM_ID>() | views::colors) {
1003 switch (colorFormat) {
1005 case ABGR: buffer[i] = c.abgr();
break;
1006 case ARGB: buffer[i] = c.argb();
break;
1007 case RGBA: buffer[i] = c.rgba();
break;
1008 case BGRA: buffer[i] = c.bgra();
break;
1038template<MeshConcept MeshType>
1039void vertexColorsToBuffer(
1040 const MeshType& mesh,
1046 elementColorsToBuffer<ElemId::VERTEX>(
1047 mesh, buffer, storage, representation, rowNumber);
1070template<MeshConcept MeshType>
1071void vertexColorsToBuffer(
1072 const MeshType& mesh,
1076 elementColorsToBuffer<ElemId::VERTEX>(mesh, buffer, colorFormat);
1102template<FaceMeshConcept MeshType>
1103void faceColorsToBuffer(
1104 const MeshType& mesh,
1110 elementColorsToBuffer<ElemId::FACE>(
1111 mesh, buffer, storage, representation, rowNumber);
1140template<FaceMeshConcept MeshType>
1141void triangulatedFaceColorsToBuffer(
1142 const MeshType& mesh,
1144 const TriPolyIndexBiMap& indexMap,
1151 const bool R_INT = representation == Color::Representation::INT_0_255;
1153 const uint FACE_NUM =
1156 for (
const auto& f : mesh.
faces()) {
1157 const auto& c = f.color();
1160 for (uint t = first; t < last; ++t) {
1161 if (storage == MatrixStorageType::ROW_MAJOR) {
1162 buffer[t * 4 + 0] = R_INT ? c.red() : c.redF();
1163 buffer[t * 4 + 1] = R_INT ? c.green() : c.greenF();
1164 buffer[t * 4 + 2] = R_INT ? c.blue() : c.blueF();
1165 buffer[t * 4 + 3] = R_INT ? c.alpha() : c.alphaF();
1168 buffer[0 * FACE_NUM + t] = R_INT ? c.red() : c.redF();
1169 buffer[1 * FACE_NUM + t] = R_INT ? c.green() : c.greenF();
1170 buffer[2 * FACE_NUM + t] = R_INT ? c.blue() : c.blueF();
1171 buffer[3 * FACE_NUM + t] = R_INT ? c.alpha() : c.alphaF();
1197template<FaceMeshConcept MeshType>
1198void faceColorsToBuffer(
1199 const MeshType& mesh,
1203 elementColorsToBuffer<ElemId::FACE>(mesh, buffer, colorFormat);
1229template<FaceMeshConcept MeshType>
1230void triangulatedFaceColorsToBuffer(
1231 const MeshType& mesh,
1233 const TriPolyIndexBiMap& indexMap,
1236 requirePerElementComponent<ElemId::FACE, CompId::COLOR>(mesh);
1238 for (
const auto& f : mesh.
faces()) {
1239 const auto& c = f.color();
1242 for (uint t = first; t < last; ++t) {
1243 switch (colorFormat) {
1245 case ABGR: buffer[t] = c.abgr();
break;
1246 case ARGB: buffer[t] = c.argb();
break;
1247 case RGBA: buffer[t] = c.rgba();
break;
1248 case BGRA: buffer[t] = c.bgra();
break;
1277template<EdgeMeshConcept MeshType>
1278void edgeColorsToBuffer(
1279 const MeshType& mesh,
1285 elementColorsToBuffer<ElemId::EDGE>(
1286 mesh, buffer, storage, representation, rowNumber);
1309template<EdgeMeshConcept MeshType>
1310void edgeColorsToBuffer(
1311 const MeshType& mesh,
1315 elementColorsToBuffer<ElemId::EDGE>(mesh, buffer, colorFormat);
1337template<u
int ELEM_ID, MeshConcept MeshType>
1338void elementQualityToBuffer(
const MeshType& mesh,
auto* buffer)
1340 requirePerElementComponent<ELEM_ID, CompId::QUALITY>(mesh);
1343 const auto& q : mesh.template elements<ELEM_ID>() | views::quality) {
1367template<MeshConcept MeshType>
1368void vertexQualityToBuffer(
const MeshType& mesh,
auto* buffer)
1370 elementQualityToBuffer<ElemId::VERTEX>(mesh, buffer);
1390template<MeshConcept MeshType>
1391void faceQualityToBuffer(
const MeshType& mesh,
auto* buffer)
1393 elementQualityToBuffer<ElemId::FACE>(mesh, buffer);
1413template<MeshConcept MeshType>
1414void edgeQualityToBuffer(
const MeshType& mesh,
auto* buffer)
1416 elementQualityToBuffer<ElemId::FACE>(mesh, buffer);
1440template<MeshConcept MeshType>
1441void vertexTexCoordsToBuffer(
1442 const MeshType& mesh,
1447 requirePerVertexTexCoord(mesh);
1449 const uint VERT_NUM =
1450 rowNumber ==
UINT_NULL ? mesh.vertexNumber() : rowNumber;
1452 for (uint i = 0;
const auto& t : mesh.vertices() | views::texCoords) {
1453 if (storage == MatrixStorageType::ROW_MAJOR) {
1454 buffer[i * 2 + 0] = t.u();
1455 buffer[i * 2 + 1] = t.v();
1458 buffer[0 * VERT_NUM + i] = t.u();
1459 buffer[1 * VERT_NUM + i] = t.v();
1483template<MeshConcept MeshType>
1484void vertexTexCoordIndicesToBuffer(
const MeshType& mesh,
auto* buffer)
1486 requirePerVertexTexCoord(mesh);
1488 for (uint i = 0;
const auto& t : mesh.vertices() | views::texCoords) {
1489 buffer[i] = t.index();
1517template<FaceMeshConcept MeshType>
1518void vertexTexCoordIndicesAsFaceTexCoordIndicesToBuffer(
1519 const MeshType& mesh,
1522 requirePerVertexTexCoord(mesh);
1524 for (uint i = 0;
const auto& f : mesh.faces()) {
1525 ushort ti = f.vertex(0)->texCoord()->index();
1555template<FaceMeshConcept MeshType>
1556void vertexTexCoordIndicesAsTriangulatedFaceTexCoordIndicesToBuffer(
1557 const MeshType& mesh,
1559 const TriPolyIndexBiMap& indexMap)
1561 requirePerVertexTexCoord(mesh);
1563 for (
const auto& f : mesh.
faces()) {
1564 ushort ti = f.vertex(0)->texCoord().index();
1567 for (uint t = first; t < last; ++t) {
1610template<FaceMeshConcept MeshType>
1612 const MeshType& mesh,
1623 for (uint
i = 0;
const auto& f : mesh.faces()) {
1627 if (
storage == MatrixStorageType::COLUMN_MAJOR)
1629 if (
fi < f.wedgeTexCoordsNumber()) {
1630 const auto& w = f.wedgeTexCoord(
fi);
1632 buffer[index] = w.u();
1634 buffer[index] = w.v();
1662template<FaceMeshConcept MeshType>
1667 for (uint
i = 0;
const auto& f : mesh.faces()) {
1668 buffer[
i] = f.textureIndex();
1694template<FaceMeshConcept MeshType>
1696 const MeshType& mesh,
1702 for (
const auto& f : mesh.faces()) {
1705 for (uint t = first; t <
last; ++t) {
1706 buffer[t] = f.textureIndex();
1755template<FaceMeshConcept MeshType>
1757 const MeshType& mesh,
1758 const std::vector<std::pair<vcl::uint, vcl::uint>>&
vertWedgeMap,
1759 const std::list<std::list<std::pair<vcl::uint, vcl::uint>>>&
1764 using TexCoordType =
typename MeshType::FaceType::WedgeTexCoordType;
1773 for (
const auto& v : mesh.vertices()) {
1780 w = mesh.face(
fInd).wedgeTexCoord(
wInd);
1782 if (
storage == MatrixStorageType::ROW_MAJOR) {
1783 buffer[
vi * 2 + 0] = w.u();
1784 buffer[
vi * 2 + 1] = w.v();
1796 assert(list.begin() != list.end());
1797 const auto&
p = list.front();
1798 uint
fInd =
p.first;
1799 uint
wInd =
p.second;
1801 const auto& w = mesh.face(
fInd).wedgeTexCoord(
wInd);
1802 if (
storage == MatrixStorageType::ROW_MAJOR) {
1803 buffer[
vi * 2 + 0] = w.u();
1804 buffer[
vi * 2 + 1] = w.v();
1858template<FaceMeshConcept MeshType>
1860 const MeshType& mesh,
1861 const std::vector<std::pair<vcl::uint, vcl::uint>>&
vertWedgeMap,
1862 const std::list<std::list<std::pair<vcl::uint, vcl::uint>>>&
1873 for (
const auto& v : mesh.vertices()) {
1883 assert(list.begin() != list.end());
1884 const auto&
p = list.front();
1885 uint
fInd =
p.first;
A class representing a box in N-dimensional space.
Definition box.h:46
PointT size() const
Computes the size of the box.
Definition box.h:267
Format
Color format enumeration.
Definition color.h:77
Representation
Color representation enumeration.
Definition color.h:64
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 face_requirements.h:73
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:1756
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:1859
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
std::vector< uint > earCut(Iterator begin, Iterator end)
Triangulates a simple polygon with no holes using the ear-cutting algorithm.
Definition ear_cut.h:90
void vertexPositionsToBuffer(const MeshType &mesh, auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint rowNumber=UINT_NULL)
Export the vertex positions of a mesh to a buffer.
Definition export_buffer.h:100
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:403
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:1695
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:497
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:257
void faceWedgeTexCoordIndicesToBuffer(const MeshType &mesh, auto *buffer)
Export into a buffer the per face wedge texture indices of a mesh.
Definition export_buffer.h:1663
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:1611
void vertexQuadIndicesToBuffer(const MeshType &mesh, auto *buffer)
Export the indices of a quad per vertex to a buffer.
Definition export_buffer.h:142
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:195
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:556
void requirePerFaceWedgeTexCoords(const MeshType &m)
This function asserts that a Mesh has a FaceContainer, the Face has a WedgeTexCoords Component,...
Definition face_requirements.h:1209
void requirePerFaceNormal(const MeshType &m)
This function asserts that a Mesh has a FaceContainer, the Face has a Normal Component,...
Definition face_requirements.h:1066
void requirePerFaceColor(const MeshType &m)
This function asserts that a Mesh has a FaceContainer, the Face has a Color Component,...
Definition face_requirements.h:996
bool isCompact(const MeshType &m)
Checks if a Mesh is compact, that is if it does not contains deleted elements.
Definition mesh_requirements.h:204
uint countPerFaceVertexReferences(const FaceMeshConcept auto &mesh)
Count the number of references to vertices in the mesh faces.
Definition topology.h:182
uint largestFaceSize(const FaceMeshConcept auto &mesh)
Returns the largest face size in the mesh.
Definition topology.h:211
uint countTriangulatedTriangles(const FaceMeshConcept auto &mesh)
Counts the number of resulting triangles if the input mesh would be triangulated by splitting each fa...
Definition topology.h:239
constexpr detail::FacesView faces
A view that allows to iterate overt the Face elements of an object.
Definition face.h:84