Visual Computing Library  devel
Loading...
Searching...
No Matches
import_buffer.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2026 *
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 vertexCount,
97 bool clearBeforeSet = true,
98 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
99 uint numRows = UINT_NULL)
100{
101 using namespace detail;
102
103 const uint NUM_ROWS = numRows == UINT_NULL ? vertexCount : numRows;
104
105 if (clearBeforeSet) {
106 mesh.clearVertices();
107 mesh.resizeVertices(vertexCount);
108 }
109 else {
110 if (vertexCount != mesh.vertexCount()) {
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.vertexCount()) +
116 "\nNumber of input vertex number: " +
117 std::to_string(vertexCount));
118 }
119 }
120
121 for (uint i = 0; auto& p : mesh.vertices() | views::positions) {
122 p.x() = at(buffer, i, 0, NUM_ROWS, 3, storage);
123 p.y() = at(buffer, i, 1, NUM_ROWS, 3, storage);
124 p.z() = at(buffer, i, 2, NUM_ROWS, 3, storage);
125
126 ++i;
127 }
128}
129
189template<FaceMeshConcept MeshType>
191 MeshType& mesh,
192 const auto* buffer,
193 uint faceCount,
194 uint faceSize = 3,
195 bool clearBeforeSet = true,
196 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
197 uint numRows = UINT_NULL)
198{
199 using namespace detail;
200
201 const uint NUM_ROWS = numRows == UINT_NULL ? faceCount : numRows;
202
203 if (clearBeforeSet) {
204 mesh.clearFaces();
205 mesh.resizeFaces(faceCount);
206 }
207 else {
208 if (faceCount != mesh.faceCount()) {
209 throw WrongSizeException(
210 "The input face count does not match the number of faces of "
211 "the mesh\n"
212 "Number of faces in the mesh: " +
213 std::to_string(mesh.faceCount()) +
214 "\nNumber of input face count: " + std::to_string(faceCount));
215 }
216 }
217
218 if constexpr (HasPolygons<MeshType>) {
219 uint i = 0;
220 for (auto& f : mesh.faces()) {
221 uint vertexCount = 0;
222
223 // count the number of vertices of the face
224 while (vertexCount < faceSize &&
225 at(buffer, i, vertexCount, NUM_ROWS, faceSize, storage) !=
226 -1 &&
227 at(buffer, i, vertexCount, NUM_ROWS, faceSize, storage) !=
228 UINT_NULL)
229 vertexCount++;
230
231 f.resizeVertices(vertexCount);
232
233 for (uint j = 0; j < vertexCount; ++j)
234 f.setVertex(j, at(buffer, i, j, NUM_ROWS, 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 VC = FaceType::VERTEX_COUNT;
242 if (faceSize == VC) { // faces of matrix and mesh have same size
243 uint i = 0;
244 for (auto& f : mesh.faces()) {
245 for (uint j = 0; j < VC; ++j)
246 f.setVertex(
247 j, at(buffer, i, j, NUM_ROWS, 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 count of the faces of the mesh.\n"
257 "Faces vertex count in the mesh: " +
258 std::to_string(VC) +
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 edgeCount,
313 bool clearBeforeSet = true,
314 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
315 uint numRows = UINT_NULL)
316{
317 using namespace detail;
318
319 if (clearBeforeSet) {
320 mesh.clearEdges();
321 mesh.resizeEdges(edgeCount);
322 }
323 else {
324 if (edgeCount != mesh.edgeCount()) {
325 throw WrongSizeException(
326 "The input edge count does not match the number of edges "
327 "of the mesh\n"
328 "Number of edges in the mesh: " +
329 std::to_string(mesh.edgeCount()) +
330 "\nNumber of input edge count: " + std::to_string(edgeCount));
331 }
332 }
333
334 uint i = 0;
335 for (auto& e : mesh.edges()) {
336 e.setVertex(0, at(buffer, i, 0, numRows, 2, storage));
337 e.setVertex(1, at(buffer, i, 1, numRows, 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
419template<EdgeMeshConcept MeshType>
420void edgeSelectionFromBuffer(MeshType& mesh, const auto* buffer)
421{
423}
424
455template<uint ELEM_ID, MeshConcept MeshType>
457 MeshType& mesh,
458 const auto* buffer,
459 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
460 uint numRows = UINT_NULL)
461{
462 using namespace detail;
463
464 const uint NUM_ROWS =
465 numRows == UINT_NULL ? mesh.template count<ELEM_ID>() : numRows;
466
469
470 for (uint i = 0;
471 auto& n : mesh.template elements<ELEM_ID>() | views::normals) {
472 n.x() = at(buffer, i, 0, NUM_ROWS, 3, storage);
473 n.y() = at(buffer, i, 1, NUM_ROWS, 3, storage);
474 n.z() = at(buffer, i, 2, NUM_ROWS, 3, storage);
475
476 ++i;
477 }
478}
479
507template<MeshConcept MeshType>
509 MeshType& mesh,
510 const auto* buffer,
511 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
512 uint numRows = UINT_NULL)
513{
515 mesh, buffer, storage, numRows);
516}
517
545template<FaceMeshConcept MeshType>
547 MeshType& mesh,
548 const auto* buffer,
549 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
550 uint numRows = UINT_NULL)
551{
553 mesh, buffer, storage, numRows);
554}
555
583template<EdgeMeshConcept MeshType>
585 MeshType& mesh,
586 const auto* buffer,
587 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
588 uint numRows = UINT_NULL)
589{
591 mesh, buffer, storage, numRows);
592}
593
634template<uint ELEM_ID, MeshConcept MeshType>
636 MeshType& mesh,
637 const auto* buffer,
638 uint channelCount = 4,
639 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
640 Color::Representation representation = Color::Representation::INT_0_255,
641 uint numRows = UINT_NULL)
642{
643 using namespace detail;
644
645 const uint NUM_ROWS =
646 numRows == UINT_NULL ? mesh.template count<ELEM_ID>() : numRows;
647
648 if (channelCount != 3 && channelCount != 4)
649 throw WrongSizeException(
650 "The input " + elementEnumString<ELEM_ID>() +
651 " colors must have 3 or 4 channels.");
652
655
656 for (uint i = 0;
657 auto& c : mesh.template elements<ELEM_ID>() | views::colors) {
658 if (representation == Color::Representation::INT_0_255) {
659 c.x() = at(buffer, i, 0, NUM_ROWS, channelCount, storage);
660 c.y() = at(buffer, i, 1, NUM_ROWS, channelCount, storage);
661 c.z() = at(buffer, i, 2, NUM_ROWS, channelCount, storage);
662
663 if (channelCount == 4)
664 c.w() = at(buffer, i, 3, NUM_ROWS, channelCount, storage);
665 else
666 c.w() = 255;
667 }
668 else {
669 c.x() = at(buffer, i, 0, NUM_ROWS, channelCount, storage) * 255;
670 c.y() = at(buffer, i, 1, NUM_ROWS, channelCount, storage) * 255;
671 c.z() = at(buffer, i, 2, NUM_ROWS, channelCount, storage) * 255;
672
673 if (channelCount == 4)
674 c.w() = at(buffer, i, 3, NUM_ROWS, channelCount, storage) * 255;
675 else
676 c.w() = 255;
677 }
678 ++i;
679 }
680}
681
704template<uint ELEM_ID, MeshConcept MeshType>
706 MeshType& mesh,
707 const auto* buffer,
709{
712
713 for (uint i = 0;
714 auto& c : mesh.template elements<ELEM_ID>() | views::colors) {
715 c = Color(buffer[i++], colorFormat);
716 }
717}
718
756template<MeshConcept MeshType>
758 MeshType& mesh,
759 const auto* buffer,
760 uint channelCount = 4,
761 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
762 Color::Representation representation = Color::Representation::INT_0_255,
763 uint numRows = UINT_NULL)
764{
766 mesh, buffer, channelCount, storage, representation, numRows);
767}
768
789template<MeshConcept MeshType>
791 MeshType& mesh,
792 const auto* buffer,
794{
796 mesh, buffer, colorFormat);
797}
798
836template<FaceMeshConcept MeshType>
838 MeshType& mesh,
839 const auto* buffer,
840 uint channelCount = 4,
841 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
842 Color::Representation representation = Color::Representation::INT_0_255,
843 uint numRows = UINT_NULL)
844{
846 mesh, buffer, channelCount, storage, representation, numRows);
847}
848
869template<FaceMeshConcept MeshType>
871 MeshType& mesh,
872 const auto* buffer,
874{
876}
877
915template<EdgeMeshConcept MeshType>
917 MeshType& mesh,
918 const auto* buffer,
919 uint channelCount = 4,
920 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
921 Color::Representation representation = Color::Representation::INT_0_255,
922 uint numRows = UINT_NULL)
923{
925 mesh, buffer, channelCount, storage, representation, numRows);
926}
927
948template<EdgeMeshConcept MeshType>
950 MeshType& mesh,
951 const auto* buffer,
953{
955}
956
976template<uint ELEM_ID, MeshConcept MeshType>
977void elementQualityFromBuffer(MeshType& mesh, const auto* buffer)
978{
981
982 for (uint i = 0; auto& e : mesh.template elements<ELEM_ID>()) {
983 e.quality() = buffer[i];
984 ++i;
985 }
986}
987
1007template<MeshConcept MeshType>
1008void vertexQualityFromBuffer(MeshType& mesh, const auto* buffer)
1009{
1011}
1012
1032template<FaceMeshConcept MeshType>
1033void faceQualityFromBuffer(MeshType& mesh, const auto* buffer)
1034{
1036}
1037
1057template<EdgeMeshConcept MeshType>
1058void edgeQualityFromBuffer(MeshType& mesh, const auto* buffer)
1059{
1061}
1062
1089template<MeshConcept MeshType>
1091 MeshType& mesh,
1092 const auto* buffer,
1093 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
1094 uint numRows = UINT_NULL)
1095{
1096 using namespace detail;
1097
1098 const uint NUM_ROWS = numRows == UINT_NULL ? mesh.vertexCount() : numRows;
1099
1100 enableIfPerVertexTexCoordOptional(mesh);
1101 requirePerVertexTexCoord(mesh);
1102
1103 for (uint i = 0; auto& t : mesh.vertices() | views::texCoords) {
1104 t.u() = at(buffer, i, 0, NUM_ROWS, 2, storage);
1105 t.v() = at(buffer, i, 1, NUM_ROWS, 2, storage);
1106
1107 ++i;
1108 }
1109}
1110
1131template<MeshConcept MeshType>
1132void vertexMaterialIndicesFromBuffer(MeshType& mesh, const auto* buffer)
1133{
1134 enableIfPerVertexMaterialIndexOptional(mesh);
1135 requirePerVertexMaterialIndex(mesh);
1136
1137 for (uint i = 0; auto& v : mesh.vertices()) {
1138 v.materialIndex() = buffer[i];
1139 ++i;
1140 }
1141}
1142
1179template<FaceMeshConcept MeshType>
1181 MeshType& mesh,
1182 const auto* buffer,
1183 uint largestFaceSize = 3,
1184 MatrixStorageType storage = MatrixStorageType::ROW_MAJOR,
1185 uint numRows = UINT_NULL)
1186{
1187 using namespace detail;
1188
1189 const uint NUM_ROWS = numRows == UINT_NULL ? mesh.faceCount() : numRows;
1190 const uint NUM_COLS = largestFaceSize * 2;
1191
1194
1195 for (uint i = 0; auto& f : mesh.faces()) {
1196 for (uint j = 0; auto& w : f.wedgeTexCoords()) {
1197 if (j < largestFaceSize) {
1198 w.u() = at(buffer, i, 2 * j + 0, NUM_ROWS, NUM_COLS, storage);
1199 w.v() = at(buffer, i, 2 * j + 1, NUM_ROWS, NUM_COLS, storage);
1200 }
1201 ++j;
1202 }
1203 ++i;
1204 }
1205};
1206
1227template<FaceMeshConcept MeshType>
1228void faceMaterialIndicesFromBuffer(MeshType& mesh, const auto* buffer)
1229{
1232
1233 for (uint i = 0; auto& f : mesh.faces()) {
1234 f.materialIndex() = buffer[i];
1235 ++i;
1236 }
1237}
1238
1239} // namespace vcl
1240
1241#endif // VCL_ALGORITHMS_MESH_IMPORT_EXPORT_IMPORT_BUFFER_H
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
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:41
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:89
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:49
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 vertexTexCoordsFromBuffer(MeshType &mesh, const auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint numRows=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:1090
void edgeIndicesFromBuffer(MeshType &mesh, const auto *buffer, uint edgeCount, bool clearBeforeSet=true, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint numRows=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 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:977
void faceIndicesFromBuffer(MeshType &mesh, const auto *buffer, uint faceCount, uint faceSize=3, bool clearBeforeSet=true, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint numRows=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 faceWedgeTexCoordsFromBuffer(MeshType &mesh, const auto *buffer, uint largestFaceSize=3, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint numRows=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:1180
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 edgeSelectionFromBuffer(MeshType &mesh, const auto *buffer)
Sets the edge selection of the given input mesh from the input selection buffer.
Definition import_buffer.h:420
void elementNormalsFromBuffer(MeshType &mesh, const auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint numRows=UINT_NULL)
Sets the element identified by ELEM_ID normals of the given input mesh from the input buffer,...
Definition import_buffer.h:456
void faceNormalsFromBuffer(MeshType &mesh, const auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint numRows=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:546
void edgeColorsFromBuffer(MeshType &mesh, const auto *buffer, uint channelCount=4, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, Color::Representation representation=Color::Representation::INT_0_255, uint numRows=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:916
void vertexNormalsFromBuffer(MeshType &mesh, const auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint numRows=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:508
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 edgeNormalsFromBuffer(MeshType &mesh, const auto *buffer, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint numRows=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:584
void faceColorsFromBuffer(MeshType &mesh, const auto *buffer, uint channelCount=4, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, Color::Representation representation=Color::Representation::INT_0_255, uint numRows=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:837
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:1008
void vertexPositionsFromBuffer(MeshType &mesh, const auto *buffer, uint vertexCount, bool clearBeforeSet=true, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, uint numRows=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 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:1228
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:1033
void vertexColorsFromBuffer(MeshType &mesh, const auto *buffer, uint channelCount=4, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, Color::Representation representation=Color::Representation::INT_0_255, uint numRows=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:757
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 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:1132
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:1058
void elementColorsFromBuffer(MeshType &mesh, const auto *buffer, uint channelCount=4, MatrixStorageType storage=MatrixStorageType::ROW_MAJOR, Color::Representation representation=Color::Representation::INT_0_255, uint numRows=UINT_NULL)
Sets the element identified by ELEM_ID colors of the given input mesh from the input buffer,...
Definition import_buffer.h:635
uint largestFaceSize(const FaceMeshConcept auto &mesh)
Returns the largest face size in the mesh.
Definition topology.h:212