Visual Computing Library  devel
Loading...
Searching...
No Matches
import_buffer.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_ALGORITHMS_MESH_IMPORT_EXPORT_IMPORT_BUFFER_H
24#define VCL_ALGORITHMS_MESH_IMPORT_EXPORT_IMPORT_BUFFER_H
25
26#include "detail.h"
27
28#include <vclib/mesh.h>
29#include <vclib/space/core.h>
30
41namespace vcl {
42
92template<MeshConcept MeshType>
94 MeshType& mesh,
95 const auto* buffer,
96 uint vertexNumber,
97 bool clearBeforeSet = true,
98 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
99 uint rowNumber = UINT_NULL)
100{
101 using namespace detail;
102
103 const uint ROW_NUM = rowNumber == UINT_NULL ? vertexNumber : rowNumber;
104
105 if (clearBeforeSet) {
106 mesh.clearVertices();
107 mesh.resizeVertices(vertexNumber);
108 }
109 else {
110 if (vertexNumber != mesh.vertexNumber()) {
111 throw WrongSizeException(
112 "The input vertex number does not match the number of vertices "
113 "of the mesh\n"
114 "Number of vertices in the mesh: " +
115 std::to_string(mesh.vertexNumber()) +
116 "\nNumber of input vertex number: " +
117 std::to_string(vertexNumber));
118 }
119 }
120
121 for (uint i = 0; auto& p : mesh.vertices() | views::positions) {
122 p.x() = at(buffer, i, 0, ROW_NUM, 3, storage);
123 p.y() = at(buffer, i, 1, ROW_NUM, 3, storage);
124 p.z() = at(buffer, i, 2, ROW_NUM, 3, storage);
125
126 ++i;
127 }
128}
129
189template<FaceMeshConcept MeshType>
191 MeshType& mesh,
192 const auto* buffer,
193 uint faceNumber,
194 uint faceSize = 3,
195 bool clearBeforeSet = true,
196 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
197 uint rowNumber = UINT_NULL)
198{
199 using namespace detail;
200
201 const uint ROW_NUM = rowNumber == UINT_NULL ? faceNumber : rowNumber;
202
203 if (clearBeforeSet) {
204 mesh.clearFaces();
205 mesh.resizeFaces(faceNumber);
206 }
207 else {
208 if (faceNumber != mesh.faceNumber()) {
209 throw WrongSizeException(
210 "The input face number does not match the number of faces "
211 "of the mesh\n"
212 "Number of faces in the mesh: " +
213 std::to_string(mesh.faceNumber()) +
214 "\nNumber of input face number: " + std::to_string(faceNumber));
215 }
216 }
217
218 if constexpr (HasPolygons<MeshType>) {
219 uint i = 0;
220 for (auto& f : mesh.faces()) {
221 uint vertexNumber = 0;
222
223 // count the number of vertices of the face
224 while (vertexNumber < faceSize &&
225 at(buffer, i, vertexNumber, ROW_NUM, faceSize, storage) !=
226 -1 &&
227 at(buffer, i, vertexNumber, ROW_NUM, faceSize, storage) !=
228 UINT_NULL)
229 vertexNumber++;
230
231 f.resizeVertices(vertexNumber);
232
233 for (uint j = 0; j < vertexNumber; ++j)
234 f.setVertex(j, at(buffer, i, j, ROW_NUM, faceSize, storage));
235 ++i;
236 }
237 }
238 else { // the vertex number of mesh faces is fixed
239 using FaceType = MeshType::FaceType;
240
241 constexpr int VN = FaceType::VERTEX_NUMBER;
242 if (faceSize == VN) { // faces of matrix and mesh have same size
243 uint i = 0;
244 for (auto& f : mesh.faces()) {
245 for (uint j = 0; j < VN; ++j)
246 f.setVertex(
247 j, at(buffer, i, j, ROW_NUM, faceSize, storage));
248 ++i;
249 }
250 }
251 else {
252 // cannot import the faces if the number of face indices in the
253 // buffer is different w.r.t. face size of the mesh
254 throw WrongSizeException(
255 "The input face buffer has a different face size "
256 "than the vertex number of the faces of the mesh.\n"
257 "Vertex number of faces in the mesh: " +
258 std::to_string(VN) +
259 "\nNumber of columns in the input face buffer: " +
260 std::to_string(faceSize));
261 }
262 }
263}
264
308template<EdgeMeshConcept MeshType>
310 MeshType& mesh,
311 const auto* buffer,
312 uint edgeNumber,
313 bool clearBeforeSet = true,
314 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
315 uint rowNumber = UINT_NULL)
316{
317 using namespace detail;
318
319 if (clearBeforeSet) {
320 mesh.clearEdges();
321 mesh.resizeEdges(edgeNumber);
322 }
323 else {
324 if (edgeNumber != mesh.edgeNumber()) {
325 throw WrongSizeException(
326 "The input edge number does not match the number of edges "
327 "of the mesh\n"
328 "Number of edges in the mesh: " +
329 std::to_string(mesh.edgeNumber()) +
330 "\nNumber of input edge number: " + std::to_string(edgeNumber));
331 }
332 }
333
334 uint i = 0;
335 for (auto& e : mesh.edges()) {
336 e.setVertex(0, at(buffer, i, 0, rowNumber, 2, storage));
337 e.setVertex(1, at(buffer, i, 1, rowNumber, 2, storage));
338 i++;
339 }
340}
341
356template<uint ELEM_ID, MeshConcept MeshType>
357void elementSelectionFromBuffer(MeshType& mesh, const auto* buffer)
358{
359 for (uint i = 0; auto& e : mesh.template elements<ELEM_ID>()) {
360 e.selected() = buffer[i];
361 ++i;
362 }
363}
364
379template<MeshConcept MeshType>
380void vertexSelectionFromBuffer(MeshType& mesh, const auto* buffer)
381{
383}
384
399template<FaceMeshConcept MeshType>
400void faceSelectionFromBuffer(MeshType& mesh, const auto* buffer)
401{
403}
404
421template<EdgeMeshConcept MeshType>
422void edgeSelectionFromBuffer(MeshType& mesh, const auto* buffer)
423{
425}
426
457template<uint ELEM_ID, MeshConcept MeshType>
459 MeshType& mesh,
460 const auto* buffer,
461 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
462 uint rowNumber = UINT_NULL)
463{
464 using namespace detail;
465
466 const uint ROW_NUM =
467 rowNumber == UINT_NULL ? mesh.template number<ELEM_ID>() : rowNumber;
468
471
472 for (uint i = 0;
473 auto& n : mesh.template elements<ELEM_ID>() | views::normals) {
474 n.x() = at(buffer, i, 0, ROW_NUM, 3, storage);
475 n.y() = at(buffer, i, 1, ROW_NUM, 3, storage);
476 n.z() = at(buffer, i, 2, ROW_NUM, 3, storage);
477
478 ++i;
479 }
480}
481
509template<MeshConcept MeshType>
511 MeshType& mesh,
512 const auto* buffer,
513 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
514 uint rowNumber = UINT_NULL)
515{
517 mesh, buffer, storage, rowNumber);
518}
519
547template<FaceMeshConcept MeshType>
549 MeshType& mesh,
550 const auto* buffer,
551 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
552 uint rowNumber = UINT_NULL)
553{
555 mesh, buffer, storage, rowNumber);
556}
557
585template<EdgeMeshConcept MeshType>
587 MeshType& mesh,
588 const auto* buffer,
589 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
590 uint rowNumber = UINT_NULL)
591{
593 mesh, buffer, storage, rowNumber);
594}
595
636template<uint ELEM_ID, MeshConcept MeshType>
638 MeshType& mesh,
639 const auto* buffer,
640 uint channelsNumber = 4,
641 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
642 Color::Representation representation = Color::Representation::INT_0_255,
643 uint rowNumber = UINT_NULL)
644{
645 using namespace detail;
646
647 const uint ROW_NUM =
648 rowNumber == UINT_NULL ? mesh.template number<ELEM_ID>() : rowNumber;
649
650 if (channelsNumber != 3 && channelsNumber != 4)
651 throw WrongSizeException(
652 "The input " + elementEnumString<ELEM_ID>() +
653 " colors must have 3 or 4 channels.");
654
657
658 for (uint i = 0;
659 auto& c : mesh.template elements<ELEM_ID>() | views::colors) {
660 if (representation == Color::Representation::INT_0_255) {
661 c.x() = at(buffer, i, 0, ROW_NUM, channelsNumber, storage);
662 c.y() = at(buffer, i, 1, ROW_NUM, channelsNumber, storage);
663 c.z() = at(buffer, i, 2, ROW_NUM, channelsNumber, storage);
664
665 if (channelsNumber == 4)
666 c.w() = at(buffer, i, 3, ROW_NUM, channelsNumber, storage);
667 else
668 c.w() = 255;
669 }
670 else {
671 c.x() = at(buffer, i, 0, ROW_NUM, channelsNumber, storage) * 255;
672 c.y() = at(buffer, i, 1, ROW_NUM, channelsNumber, storage) * 255;
673 c.z() = at(buffer, i, 2, ROW_NUM, channelsNumber, storage) * 255;
674
675 if (channelsNumber == 4)
676 c.w() =
677 at(buffer, i, 3, ROW_NUM, channelsNumber, storage) * 255;
678 else
679 c.w() = 255;
680 }
681 ++i;
682 }
683}
684
707template<uint ELEM_ID, MeshConcept MeshType>
709 MeshType& mesh,
710 const auto* buffer,
712{
715
716 for (uint i = 0;
717 auto& c : mesh.template elements<ELEM_ID>() | views::colors) {
718 c = Color(buffer[i++], colorFormat);
719 }
720}
721
759template<MeshConcept MeshType>
761 MeshType& mesh,
762 const auto* buffer,
763 uint channelsNumber = 4,
764 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
765 Color::Representation representation = Color::Representation::INT_0_255,
766 uint rowNumber = UINT_NULL)
767{
770}
771
792template<MeshConcept MeshType>
794 MeshType& mesh,
795 const auto* buffer,
797{
799 mesh, buffer, colorFormat);
800}
801
839template<FaceMeshConcept MeshType>
841 MeshType& mesh,
842 const auto* buffer,
843 uint channelsNumber = 4,
844 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
845 Color::Representation representation = Color::Representation::INT_0_255,
846 uint rowNumber = UINT_NULL)
847{
850}
851
872template<FaceMeshConcept MeshType>
874 MeshType& mesh,
875 const auto* buffer,
877{
879}
880
918template<EdgeMeshConcept MeshType>
920 MeshType& mesh,
921 const auto* buffer,
922 uint channelsNumber = 4,
923 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
924 Color::Representation representation = Color::Representation::INT_0_255,
925 uint rowNumber = UINT_NULL)
926{
929}
930
951template<EdgeMeshConcept MeshType>
953 MeshType& mesh,
954 const auto* buffer,
956{
958}
959
979template<uint ELEM_ID, MeshConcept MeshType>
980void elementQualityFromBuffer(MeshType& mesh, const auto* buffer)
981{
984
985 for (uint i = 0; auto& e : mesh.template elements<ELEM_ID>()) {
986 e.quality() = buffer[i];
987 ++i;
988 }
989}
990
1010template<MeshConcept MeshType>
1011void vertexQualityFromBuffer(MeshType& mesh, const auto* buffer)
1012{
1014}
1015
1035template<FaceMeshConcept MeshType>
1036void faceQualityFromBuffer(MeshType& mesh, const auto* buffer)
1037{
1039}
1040
1060template<EdgeMeshConcept MeshType>
1061void edgeQualityFromBuffer(MeshType& mesh, const auto* buffer)
1062{
1064}
1065
1092template<MeshConcept MeshType>
1094 MeshType& mesh,
1095 const auto* buffer,
1096 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
1097 uint rowNumber = UINT_NULL)
1098{
1099 using namespace detail;
1100
1101 const uint ROW_NUM =
1102 rowNumber == UINT_NULL ? mesh.vertexNumber() : rowNumber;
1103
1104 enableIfPerVertexTexCoordOptional(mesh);
1105 requirePerVertexTexCoord(mesh);
1106
1107 for (uint i = 0; auto& t : mesh.vertices() | views::texCoords) {
1108 t.u() = at(buffer, i, 0, ROW_NUM, 2, storage);
1109 t.v() = at(buffer, i, 1, ROW_NUM, 2, storage);
1110
1111 ++i;
1112 }
1113}
1114
1135template<MeshConcept MeshType>
1136void vertexMaterialIndicesFromBuffer(MeshType& mesh, const auto* buffer)
1137{
1138 enableIfPerVertexMaterialIndexOptional(mesh);
1139 requirePerVertexMaterialIndex(mesh);
1140
1141 for (uint i = 0; auto& v : mesh.vertices()) {
1142 v.materialIndex() = buffer[i];
1143 ++i;
1144 }
1145}
1146
1183template<FaceMeshConcept MeshType>
1185 MeshType& mesh,
1186 const auto* buffer,
1187 uint largestFaceSize = 3,
1188 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
1189 uint rowNumber = UINT_NULL)
1190{
1191 using namespace detail;
1192
1193 const uint ROW_NUM = rowNumber == UINT_NULL ? mesh.faceNumber() : rowNumber;
1194 const uint COL_NUM = largestFaceSize * 2;
1195
1198
1199 for (uint i = 0; auto& f : mesh.faces()) {
1200 for (uint j = 0; auto& w : f.wedgeTexCoords()) {
1201 if (j < largestFaceSize) {
1202 w.u() = at(buffer, i, 2 * j + 0, ROW_NUM, COL_NUM, storage);
1203 w.v() = at(buffer, i, 2 * j + 1, ROW_NUM, COL_NUM, storage);
1204 }
1205 ++j;
1206 }
1207 ++i;
1208 }
1209};
1210
1231template<FaceMeshConcept MeshType>
1232void faceMaterialIndicesFromBuffer(MeshType& mesh, const auto* buffer)
1233{
1236
1237 for (uint i = 0; auto& f : mesh.faces()) {
1238 f.materialIndex() = buffer[i];
1239 ++i;
1240 }
1241}
1242
1243} // namespace vcl
1244
1245#endif // VCL_ALGORITHMS_MESH_IMPORT_EXPORT_IMPORT_BUFFER_H
A class representing a box in N-dimensional space.
Definition box.h:46
The Color class represents a 32 bit color.
Definition color.h:48
Format
Color format enumeration.
Definition color.h:77
Representation
Color representation enumeration.
Definition color.h:64
Exception thrown when the size (generally of a container) is not the expected one.
Definition exceptions.h:45
Definition face_requirements.h:60
MatrixStorageType
A simple type that enumerates the main storage types for matrices (row or column major).
Definition base.h:88
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
bool enableIfPerFaceWedgeTexCoordsOptional(MeshType &m)
If the input mesh has a FaceContainer, and the Face Element has a WedgeTexCoords Component,...
Definition face_requirements.h:936
void requirePerFaceWedgeTexCoords(const MeshType &m)
This function asserts that a Mesh has a FaceContainer, the Face has a WedgeTexCoords Component,...
Definition face_requirements.h:1322
void requirePerFaceMaterialIndex(const MeshType &m)
This function asserts that a Mesh has a FaceContainer, the Face has a MaterialIndex Component,...
Definition face_requirements.h:1143
bool enableIfPerFaceMaterialIndexOptional(MeshType &m)
If the input mesh has a FaceContainer, and the Face Element has a MaterialIndex Component,...
Definition face_requirements.h:633
void vertexPositionsFromBuffer(MeshType &mesh, const auto *buffer, uint vertexNumber, bool clearBeforeSet=true, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint rowNumber=UINT_NULL)
Sets the vertex positions of the given input mesh from the input buffer, that is expected to be a con...
Definition import_buffer.h:93
void elementQualityFromBuffer(MeshType &mesh, const auto *buffer)
Sets the element identified by ELEM_ID quality of the given input mesh from the input quality buffer,...
Definition import_buffer.h:980
void vertexNormalsFromBuffer(MeshType &mesh, const auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint rowNumber=UINT_NULL)
Sets the vertex normals of the given input mesh from the input buffer, that is expected to be a conti...
Definition import_buffer.h:510
void edgeNormalsFromBuffer(MeshType &mesh, const auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint rowNumber=UINT_NULL)
Sets the edge normals of the given input mesh from the input buffer, that is expected to be a contigu...
Definition import_buffer.h:586
void elementColorsFromBuffer(MeshType &mesh, const auto *buffer, uint channelsNumber=4, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, Color::Representation representation=Color::Representation::INT_0_255, uint rowNumber=UINT_NULL)
Sets the element identified by ELEM_ID colors of the given input mesh from the input buffer,...
Definition import_buffer.h:637
void elementSelectionFromBuffer(MeshType &mesh, const auto *buffer)
Sets the element identified by ELEM_ID selection of the given input mesh from the input selection buf...
Definition import_buffer.h:357
void faceIndicesFromBuffer(MeshType &mesh, const auto *buffer, uint faceNumber, uint faceSize=3, bool clearBeforeSet=true, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint rowNumber=UINT_NULL)
Sets the face indices of the given input mesh from the input face buffer, that is expected to be a co...
Definition import_buffer.h:190
void faceColorsFromBuffer(MeshType &mesh, const auto *buffer, uint channelsNumber=4, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, Color::Representation representation=Color::Representation::INT_0_255, uint rowNumber=UINT_NULL)
Sets the face colors of the given input mesh from the input buffer, that is expected to be a contiguo...
Definition import_buffer.h:840
void edgeSelectionFromBuffer(MeshType &mesh, const auto *buffer)
Sets the edge selection of the given input mesh from the input selection buffer.
Definition import_buffer.h:422
void edgeColorsFromBuffer(MeshType &mesh, const auto *buffer, uint channelsNumber=4, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, Color::Representation representation=Color::Representation::INT_0_255, uint rowNumber=UINT_NULL)
Sets the edge colors of the given input mesh from the input buffer, that is expected to be a contiguo...
Definition import_buffer.h:919
void vertexColorsFromBuffer(MeshType &mesh, const auto *buffer, uint channelsNumber=4, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, Color::Representation representation=Color::Representation::INT_0_255, uint rowNumber=UINT_NULL)
Sets the vertex colors of the given input mesh from the input buffer, that is expected to be a contig...
Definition import_buffer.h:760
void faceSelectionFromBuffer(MeshType &mesh, const auto *buffer)
Sets the face selection of the given input mesh from the input selection buffer.
Definition import_buffer.h:400
void elementNormalsFromBuffer(MeshType &mesh, const auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint rowNumber=UINT_NULL)
Sets the element identified by ELEM_ID normals of the given input mesh from the input buffer,...
Definition import_buffer.h:458
void faceWedgeTexCoordsFromBuffer(MeshType &mesh, const auto *buffer, uint largestFaceSize=3, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint rowNumber=UINT_NULL)
Sets the face wedge texcoords of the given input mesh from the input buffer, that is expected to be a...
Definition import_buffer.h:1184
void vertexQualityFromBuffer(MeshType &mesh, const auto *buffer)
Sets the vertex quality of the given input mesh from the input quality buffer, that is expected to be...
Definition import_buffer.h:1011
void vertexTexCoordsFromBuffer(MeshType &mesh, const auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint rowNumber=UINT_NULL)
Sets the vertex texcoords of the given input mesh from the input buffer, that is expected to be a con...
Definition import_buffer.h:1093
void faceMaterialIndicesFromBuffer(MeshType &mesh, const auto *buffer)
Sets the face material indices of the given input mesh from the input material indices buffer,...
Definition import_buffer.h:1232
void faceNormalsFromBuffer(MeshType &mesh, const auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint rowNumber=UINT_NULL)
Sets the face normals of the given input mesh from the input buffer, that is expected to be a contigu...
Definition import_buffer.h:548
void faceQualityFromBuffer(MeshType &mesh, const auto *buffer)
Sets the face quality of the given input mesh from the input quality buffer, that is expected to be a...
Definition import_buffer.h:1036
void vertexSelectionFromBuffer(MeshType &mesh, const auto *buffer)
Sets the vertex selection of the given input mesh from the input selection buffer.
Definition import_buffer.h:380
void edgeIndicesFromBuffer(MeshType &mesh, const auto *buffer, uint edgeNumber, bool clearBeforeSet=true, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint rowNumber=UINT_NULL)
Sets the edge indices of the given input mesh from the input edge buffer, that is expected to be a co...
Definition import_buffer.h:309
void vertexMaterialIndicesFromBuffer(MeshType &mesh, const auto *buffer)
Sets the vertex material indices of the given input mesh from the input material indices buffer,...
Definition import_buffer.h:1136
void edgeQualityFromBuffer(MeshType &mesh, const auto *buffer)
Sets the edge quality of the given input mesh from the input quality buffer, that is expected to be a...
Definition import_buffer.h:1061
uint largestFaceSize(const FaceMeshConcept auto &mesh)
Returns the largest face size in the mesh.
Definition topology.h:212