Visual Computing Library  devel
Loading...
Searching...
No Matches
import_matrix.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_MATRIX_H
24#define VCL_ALGORITHMS_MESH_IMPORT_EXPORT_IMPORT_MATRIX_H
25
26#include "import_buffer.h"
27
28#include <vclib/mesh.h>
29#include <vclib/space/core.h>
30
41namespace vcl {
42
85template<
86 MeshConcept MeshType,
87 MatrixConcept VMatrix,
88 MatrixConcept FMatrix = Eigen::MatrixX3i,
89 MatrixConcept EMatrix = Eigen::MatrixX2i>
91 const VMatrix& vertices,
92 const FMatrix& faces = FMatrix(),
93 const EMatrix& edges = EMatrix())
94{
95 MeshType mesh;
96
97 meshFromMatrices(mesh, vertices, faces, edges);
98
99 return mesh;
100}
101
151template<
152 MeshConcept MeshType,
153 MatrixConcept VMatrix,
154 MatrixConcept FMatrix = Eigen::MatrixX3i,
155 MatrixConcept EMatrix = Eigen::MatrixX2i>
157 MeshType& mesh,
158 const VMatrix& vertices,
159 const FMatrix& faces = FMatrix(),
160 const EMatrix& edges = EMatrix())
161{
162 mesh.clear();
163 mesh.disableAllOptionalComponents();
164
165 vertexPositionsFromMatrix(mesh, vertices);
166
167 if constexpr (HasFaces<MeshType>) {
168 if (faces.rows() > 0)
169 faceIndicesFromMatrix(mesh, faces);
170 }
171
172 if constexpr (HasEdges<MeshType>) {
173 if (edges.rows() > 0)
174 edgeIndicesFromMatrix(mesh, edges);
175 }
176}
177
219template<MeshConcept MeshType, MatrixConcept VMatrix>
221 MeshType& mesh,
222 const VMatrix& vertices,
223 bool clearBeforeSet = true)
224{
225 if (vertices.cols() != 3)
226 throw WrongSizeException("The input vertex matrix must have 3 columns");
227
229
231 mesh, vertices.data(), vertices.rows(), clearBeforeSet, stg);
232}
233
283template<FaceMeshConcept MeshType, MatrixConcept FMatrix>
285 MeshType& mesh,
286 const FMatrix& faces,
287 bool clearBeforeSet = true)
288{
290
292 mesh, faces.data(), faces.rows(), faces.cols(), clearBeforeSet, stg);
293}
294
331template<EdgeMeshConcept MeshType, MatrixConcept EMatrix>
333 MeshType& mesh,
334 const EMatrix& edges,
335 bool clearBeforeSet = true)
336{
337 if (edges.cols() != 2)
338 throw WrongSizeException("The input edge matrix must have 2 columns");
339
341
343 mesh, edges.data(), edges.rows(), clearBeforeSet, stg);
344}
345
363template<uint ELEM_ID, MeshConcept MeshType, Range R>
364void elementSelectionFromRange(MeshType& mesh, R&& selection)
365{
366 if (std::ranges::size(selection) != mesh.template number<ELEM_ID>())
367 throw WrongSizeException(
368 "The input selection range must have the same number of elements "
369 "as the number of " +
370 elementEnumString<ELEM_ID>() + " element in the mesh");
371
372 auto s = selection.begin();
373 for (auto& e : mesh.template elements<ELEM_ID>()) {
374 e.selected() = *s;
375 ++s;
376 }
377}
378
395template<MeshConcept MeshType, Range R>
396void vertexSelectionFromRange(MeshType& mesh, R&& selection)
397{
399}
400
417template<FaceMeshConcept MeshType, Range R>
418void faceSelectionFromRange(MeshType& mesh, R&& selection)
419{
421}
422
439template<EdgeMeshConcept MeshType, Range R>
440void edgeSelectionFromRange(MeshType& mesh, R&& selection)
441{
443}
444
465template<uint ELEM_ID, MeshConcept MeshType, MatrixConcept NMatrix>
466void elementNormalsFromMatrix(MeshType& mesh, const NMatrix& normals)
467{
469
470 if (normals.cols() != 3)
471 throw WrongSizeException(
472 "The input " + elementEnumString<ELEM_ID>() +
473 " normal matrix must have 3 columns");
474
475 if (normals.rows() != mesh.template number<ELEM_ID>())
476 throw WrongSizeException(
477 "The input normal matrix must have the same number of rows as the "
478 "number of " +
479 elementEnumString<ELEM_ID>() + " elements in the mesh");
480
481 elementNormalsFromBuffer<ELEM_ID>(mesh, normals.data(), stg);
482}
483
504template<MeshConcept MeshType, MatrixConcept VNMatrix>
509
530template<FaceMeshConcept MeshType, MatrixConcept FNMatrix>
535
556template<EdgeMeshConcept MeshType, MatrixConcept ENMatrix>
561
590template<uint ELEM_ID, MeshConcept MeshType, MatrixConcept CMatrix>
591void elementColorsFromMatrix(MeshType& mesh, const CMatrix& colors)
592{
593 using MatrixScalar = CMatrix::Scalar;
594
595 Color::Representation repr = std::integral<MatrixScalar> ?
596 Color::Representation::INT_0_255 :
597 Color::Representation::FLOAT_0_1;
598
599 if (colors.rows() != mesh.template number<ELEM_ID>())
600 throw WrongSizeException(
601 "The input colors matrix must have the same number of rows as the "
602 "number of " +
603 elementEnumString<ELEM_ID>() + " elements in the mesh");
604
606 mesh, colors.data(), colors.cols(), matrixStorageType<CMatrix>(), repr);
607}
608
634template<uint ELEM_ID, MeshConcept MeshType, Range R>
636 MeshType& mesh,
637 R&& colors,
639{
640 if (std::ranges::size(colors) != mesh.template number<ELEM_ID>())
641 throw WrongSizeException(
642 "The input color range must have the same number of elements "
643 "as the number of " +
644 elementEnumString<ELEM_ID>() + " element in the mesh");
645
648
649 auto c = colors.begin();
650 for (auto& e : mesh.template elements<ELEM_ID>()) {
651 e.color() = Color(*c, colorFormat);
652 ++c;
653 }
654}
655
684template<MeshConcept MeshType, MatrixConcept VCMatrix>
689
714template<MeshConcept MeshType, Range R>
716 MeshType& mesh,
717 R&& colors,
719{
721}
722
751template<FaceMeshConcept MeshType, MatrixConcept FCMatrix>
756
781template<FaceMeshConcept MeshType, Range R>
782void faceColorsFromRange(MeshType& mesh, R&& colors, Color::Format colorFormat)
783{
785}
786
815template<EdgeMeshConcept MeshType, MatrixConcept ECMatrix>
820
845template<EdgeMeshConcept MeshType, Range R>
846void edgeColorsFromRange(MeshType& mesh, R&& colors, Color::Format colorFormat)
847{
849}
850
871template<uint ELEM_ID, MeshConcept MeshType, Range R>
872void elementQualityFromRange(MeshType& mesh, R&& quality)
873{
874 if (std::ranges::size(quality) != mesh.template number<ELEM_ID>())
875 throw WrongSizeException(
876 "The input quality range must have the same number of elements "
877 "as the number of " +
878 elementEnumString<ELEM_ID>() + " element in the mesh");
879
882
883 auto q = quality.begin();
884 for (auto& e : mesh.template elements<ELEM_ID>()) {
885 e.quality() = *q;
886 ++q;
887 }
888}
889
909template<MeshConcept MeshType, Range R>
910void vertexQualityFromRange(MeshType& mesh, R&& quality)
911{
913}
914
934template<FaceMeshConcept MeshType, Range R>
935void faceQualityFromRange(MeshType& mesh, R&& quality)
936{
938}
939
959template<EdgeMeshConcept MeshType, Range R>
960void edgeQualityFromRange(MeshType& mesh, R&& quality)
961{
963}
964
985template<MeshConcept MeshType, MatrixConcept VTMatrix>
987{
989
990 if (vertexTexCoords.cols() != 2)
991 throw WrongSizeException(
992 "The input vertex texcoords matrix must have 2 columns");
993
994 if (vertexTexCoords.rows() != mesh.vertexNumber())
995 throw WrongSizeException(
996 "The input vertex texcoords must have the same number of rows as "
997 "the number of vertices in the mesh");
998
1000}
1001
1023template<MeshConcept MeshType, Range R>
1025{
1026 if (std::ranges::size(materialIndices) != mesh.vertexNumber())
1027 throw WrongSizeException(
1028 "The input quality range must have the same number of elements "
1029 "as the number of vertices in the mesh");
1030
1031 enableIfPerVertexMaterialIndexOptional(mesh);
1032 requirePerVertexMaterialIndex(mesh);
1033
1034 auto tt = materialIndices.begin();
1035 for (auto& v : mesh.vertices()) {
1036 v.materialIndex() = *tt;
1037 ++tt;
1038 }
1039}
1040
1066template<FaceMeshConcept MeshType, MatrixConcept FTMatrix>
1068 MeshType& mesh,
1070{
1072
1073 if (faceWedgeTexCoords.rows() != mesh.faceNumber())
1074 throw WrongSizeException(
1075 "The input face wedge texcoords must have the same number of rows "
1076 "as the number of faces in the mesh");
1077
1079 mesh, faceWedgeTexCoords.data(), faceWedgeTexCoords.cols() / 2, stg);
1080}
1081
1103template<FaceMeshConcept MeshType, Range R>
1105{
1106 if (std::ranges::size(texCoordIndices) != mesh.faceNumber())
1107 throw WrongSizeException(
1108 "The input quality range must have the same number of elements "
1109 "as the number of faces in the mesh");
1110
1113
1114 auto tt = texCoordIndices.begin();
1115 for (auto& f : mesh.faces()) {
1116 f.materialIndex() = *tt;
1117 ++tt;
1118 }
1119}
1120
1121} // namespace vcl
1122
1123#endif // VCL_ALGORITHMS_MESH_IMPORT_EXPORT_IMPORT_MATRIX_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
HasEdges concepts is satisfied when at least one of its template types is (or inherits from) a vcl::m...
Definition edge_container.h:1064
HasFaces concepts is satisfied when at least one of its template types is (or inherits from) a vcl::m...
Definition face_container.h:1429
MatrixStorageType
A simple type that enumerates the main storage types for matrices (row or column major).
Definition base.h:88
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 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 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 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 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 edgeColorsFromRange(MeshType &mesh, R &&colors, Color::Format colorFormat)
Sets the edge colors of the given input mesh from the input color range (that could be anything that ...
Definition import_matrix.h:846
void vertexColorsFromRange(MeshType &mesh, R &&colors, Color::Format colorFormat)
Sets the vertex colors of the given input mesh from the input color range (that could be anything tha...
Definition import_matrix.h:715
void faceColorsFromMatrix(MeshType &mesh, const FCMatrix &faceColors)
Sets the face colors of the given input mesh from the input face colors matrix.
Definition import_matrix.h:752
void elementNormalsFromMatrix(MeshType &mesh, const NMatrix &normals)
Sets the element identified by ELEM_ID normals of the given input mesh from the input normals matrix.
Definition import_matrix.h:466
MeshType meshFromMatrices(const VMatrix &vertices, const FMatrix &faces=FMatrix(), const EMatrix &edges=EMatrix())
Creates and returns a new mesh from the input matrices that are given as arguments.
Definition import_matrix.h:90
void vertexMaterialIndicesFromRange(MeshType &mesh, R &&materialIndices)
Sets the vertex material indices of the given input mesh from the input material indices range (that ...
Definition import_matrix.h:1024
void elementSelectionFromRange(MeshType &mesh, R &&selection)
Sets the element identified by ELEM_ID selection of the given input mesh from the input selection ran...
Definition import_matrix.h:364
void edgeIndicesFromMatrix(MeshType &mesh, const EMatrix &edges, bool clearBeforeSet=true)
Sets the edge indices of the given input mesh from the input edge matrix.
Definition import_matrix.h:332
void faceMaterialIndicesFromRange(MeshType &mesh, R &&texCoordIndices)
Sets the face material indices of the given input mesh from the input material indices range (that co...
Definition import_matrix.h:1104
void vertexTexCoordsFromMatrix(MeshType &mesh, const VTMatrix &vertexTexCoords)
Sets the vertex texcoords of the given input mesh from the input vertex texcoords matrix.
Definition import_matrix.h:986
void faceQualityFromRange(MeshType &mesh, R &&quality)
Sets the face quality of the given input mesh from the input quality range (that could be anything th...
Definition import_matrix.h:935
void edgeQualityFromRange(MeshType &mesh, R &&quality)
Sets the edge quality of the given input mesh from the input quality range (that could be anything th...
Definition import_matrix.h:960
void edgeSelectionFromRange(MeshType &mesh, R &&selection)
Sets the edge selection of the given input mesh from the input selection range (that could be anythin...
Definition import_matrix.h:440
void elementColorsFromRange(MeshType &mesh, R &&colors, Color::Format colorFormat)
Sets the element identified by ELEM_ID colors of the given input mesh from the input color range (tha...
Definition import_matrix.h:635
void edgeColorsFromMatrix(MeshType &mesh, const ECMatrix &edgeColors)
Sets the edge colors of the given input mesh from the input edge colors matrix.
Definition import_matrix.h:816
void faceWedgeTexCoordsFromMatrix(MeshType &mesh, const FTMatrix &faceWedgeTexCoords)
Sets the face wedge texcoords of the given input mesh from the input face wedge texcoords matrix.
Definition import_matrix.h:1067
void vertexPositionsFromMatrix(MeshType &mesh, const VMatrix &vertices, bool clearBeforeSet=true)
Sets the vertex positions of the given input mesh from the input vertex matrix.
Definition import_matrix.h:220
void faceColorsFromRange(MeshType &mesh, R &&colors, Color::Format colorFormat)
Sets the face colors of the given input mesh from the input color range (that could be anything that ...
Definition import_matrix.h:782
void faceSelectionFromRange(MeshType &mesh, R &&selection)
Sets the face selection of the given input mesh from the input selection range (that could be anythin...
Definition import_matrix.h:418
void vertexNormalsFromMatrix(MeshType &mesh, const VNMatrix &vertexNormals)
Sets the vertex normals of the given input mesh from the input vertex normals matrix.
Definition import_matrix.h:505
void edgeNormalsFromMatrix(MeshType &mesh, const ENMatrix &edgeNormals)
Sets the edge normals of the given input mesh from the input edge normals matrix.
Definition import_matrix.h:557
void elementQualityFromRange(MeshType &mesh, R &&quality)
Sets the element identified by ELEM_ID quality of the given input mesh from the input quality range (...
Definition import_matrix.h:872
void vertexSelectionFromRange(MeshType &mesh, R &&selection)
Sets the vertex selection of the given input mesh from the input selection range (that could be anyth...
Definition import_matrix.h:396
void elementColorsFromMatrix(MeshType &mesh, const CMatrix &colors)
Sets the element identified by ELEM_ID colors of the given input mesh from the input colors matrix.
Definition import_matrix.h:591
void vertexColorsFromMatrix(MeshType &mesh, const VCMatrix &vertexColors)
Sets the vertex colors of the given input mesh from the input vertex colors matrix.
Definition import_matrix.h:685
void vertexQualityFromRange(MeshType &mesh, R &&quality)
Sets the vertex quality of the given input mesh from the input quality range (that could be anything ...
Definition import_matrix.h:910
void faceIndicesFromMatrix(MeshType &mesh, const FMatrix &faces, bool clearBeforeSet=true)
Sets the face indices of the given input mesh from the input face matrix.
Definition import_matrix.h:284
void faceNormalsFromMatrix(MeshType &mesh, const FNMatrix &faceNormals)
Sets the face normals of the given input mesh from the input face normals matrix.
Definition import_matrix.h:531