23#ifndef VCL_ALGORITHMS_MESH_IMPORT_EXPORT_IMPORT_MATRIX_H
24#define VCL_ALGORITHMS_MESH_IMPORT_EXPORT_IMPORT_MATRIX_H
26#include <vclib/concepts/space/matrix.h>
27#include <vclib/exceptions.h>
28#include <vclib/mesh/requirements.h>
29#include <vclib/space/core/polygon.h>
48template<MatrixConcept FMatrix>
49std::vector<uint> faceVertIndices(
const FMatrix& faces, uint f)
51 std::vector<uint> fVerts;
55 fVerts.push_back(
faces(f, j));
60template<u
int ELEM_ID, MeshConcept MeshType, MatrixConcept NMatrix>
61void importElementNormalsFromMatrix(MeshType& mesh,
const NMatrix& normals)
64 using NormalType = MeshType::template ElementType<ELEM_ID>::NormalType;
66 if (normals.cols() != 3)
67 throw WrongSizeException(
68 "The input " + elementEnumString<ELEM_ID>() +
69 " normal matrix must have 3 columns");
72 if (normals.rows() != mesh.template number<ELEM_ID>())
73 throw WrongSizeException(
74 "The input normal matrix must have the same number of rows "
76 elementEnumString<ELEM_ID>() +
" element in the mesh");
78 enableIfPerElementComponentOptional<ELEM_ID, CompId::NORMAL>(mesh);
79 requirePerElementComponent<ELEM_ID, CompId::NORMAL>(mesh);
82 for (
auto& e : mesh.template elements<ELEM_ID>()) {
83 e.normal() = NormalType(normals(i, 0), normals(i, 1), normals(i, 2));
88template<u
int ELEM_ID, MeshConcept MeshType, MatrixConcept CMatrix>
89void importElementColorsFromMatrix(MeshType& mesh,
const CMatrix& colors)
91 using MatrixScalar = CMatrix::Scalar;
93 if (colors.cols() != 3 && colors.cols() != 4)
94 throw WrongSizeException(
95 "The input " + elementEnumString<ELEM_ID>() +
96 " color matrix must have 3 or 4 columns");
99 if (colors.rows() != mesh.template number<ELEM_ID>())
100 throw WrongSizeException(
101 "The input color matrix must have the same number of rows "
102 "as the number of " +
103 elementEnumString<ELEM_ID>() +
" element in the mesh");
105 enableIfPerElementComponentOptional<ELEM_ID, CompId::COLOR>(mesh);
106 requirePerElementComponent<ELEM_ID, CompId::COLOR>(mesh);
109 for (
auto& e : mesh.template elements<ELEM_ID>()) {
111 if constexpr (std::integral<MatrixScalar>) {
112 if (colors.cols() == 3) {
113 e.color() = Color(colors(i, 0), colors(i, 1), colors(i, 2));
117 colors(i, 0), colors(i, 1), colors(i, 2), colors(i, 3));
121 if (colors.cols() == 3) {
123 colors(i, 0) * 255, colors(i, 1) * 255, colors(i, 2) * 255);
176 MeshConcept MeshType,
177 MatrixConcept VMatrix,
178 MatrixConcept VNMatrix = Eigen::MatrixX3d>
179MeshType pointCloudMeshFromMatrices(
180 const VMatrix& vertices,
181 const VNMatrix& vertexNormals = VNMatrix())
185 importMeshFromMatrices(
242 MeshConcept MeshType,
243 MatrixConcept VMatrix,
244 MatrixConcept FMatrix = Eigen::MatrixX3i,
245 MatrixConcept VNMatrix = Eigen::MatrixX3d,
246 MatrixConcept FNMatrix = Eigen::MatrixX3d>
247MeshType meshFromMatrices(
248 const VMatrix& vertices,
249 const FMatrix& faces = FMatrix(),
250 const VNMatrix& vertexNormals = VNMatrix(),
251 const FNMatrix& faceNormals = FNMatrix())
255 importMeshFromMatrices(
256 mesh, vertices, faces, Eigen::MatrixX2i(), vertexNormals, faceNormals);
320 MeshConcept MeshType,
321 MatrixConcept VMatrix,
322 MatrixConcept FMatrix = Eigen::MatrixX3i,
323 MatrixConcept EMatrix = Eigen::MatrixX2i,
324 MatrixConcept VNMatrix = Eigen::MatrixX3d,
325 MatrixConcept FNMatrix = Eigen::MatrixX3d>
326void importMeshFromMatrices(
328 const VMatrix& vertices,
329 const FMatrix& faces = FMatrix(),
330 const EMatrix& edges = EMatrix(),
331 const VNMatrix& vertexNormals = VNMatrix(),
332 const FNMatrix& faceNormals = FNMatrix())
335 mesh.disableAllOptionalComponents();
337 importVerticesFromMatrix(mesh, vertices);
339 if constexpr (HasPerVertexNormal<MeshType>) {
340 if (vertexNormals.rows() > 0) {
341 importVertexNormalsFromMatrix(mesh, vertexNormals);
345 if constexpr (HasFaces<MeshType>) {
346 if (
faces.rows() > 0)
347 importFacesFromMatrix(mesh, faces);
349 if constexpr (HasPerFaceNormal<MeshType>) {
350 if (faceNormals.rows() > 0) {
351 importFaceNormalsFromMatrix(mesh, faceNormals);
356 if constexpr (HasEdges<MeshType>) {
357 if (
edges.rows() > 0)
358 importEdgesFromMatrix(mesh, edges);
400template<MeshConcept MeshType, MatrixConcept VMatrix>
401void importVerticesFromMatrix(
403 const VMatrix& vertices,
404 bool clearBeforeSet =
true)
406 using CoordType = MeshType::VertexType::CoordType;
409 throw WrongSizeException(
"The input vertex matrix must have 3 columns");
411 if (clearBeforeSet) {
412 mesh.clearVertices();
413 mesh.resizeVertices(
vertices.rows());
416 if (
vertices.rows() != mesh.vertexNumber()) {
417 throw WrongSizeException(
418 "The input vertex matrix has a different number of rows than "
419 "the number of vertices of the mesh");
430template<FaceMeshConcept MeshType, MatrixConcept FMatrix>
431void importFacesFromMatrix(
433 const FMatrix& faces,
434 bool clearBeforeSet =
true)
436 if (clearBeforeSet) {
438 mesh.resizeFaces(
faces.rows());
441 if (
faces.rows() != mesh.faceNumber()) {
442 throw WrongSizeException(
443 "The input face matrix has a different number of rows "
444 "than the number of faces of the mesh.");
448 if constexpr (HasPolygons<MeshType>) {
450 for (
auto& f : mesh.
faces()) {
451 uint vertexNumber = 0;
454 while (vertexNumber <
faces.cols() &&
455 faces(i, vertexNumber) != -1 &&
459 f.resizeVertices(vertexNumber);
461 for (uint j = 0; j < vertexNumber; ++j)
462 f.setVertex(j,
faces(i, j));
467 using FaceType = MeshType::FaceType;
469 constexpr int VN = FaceType::VERTEX_NUMBER;
470 if (
faces.cols() == VN) {
472 for (
auto& f : mesh.
faces()) {
473 for (uint j = 0; j < VN; ++j)
474 f.setVertex(j,
faces(i, j));
481 if constexpr (VN == 3) {
482 if (!clearBeforeSet) {
483 throw WrongSizeException(
484 "Cannot import the input face matrix into the mesh "
485 "without clearing the face container first "
486 "(need to perform a triangulation to import polygons "
487 "in a triangle mesh, and this operation that does not "
488 "guarantee a predefined number of faces is required).");
490 mesh.reserveFaces(
faces.rows());
491 for (uint i = 0; i <
faces.rows(); ++i) {
492 std::vector<uint> fVertIndices =
493 detail::faceVertIndices(faces, i);
495 addTriangleFacesFromPolygon(mesh, fVertIndices);
501 throw WrongSizeException(
502 "The input face matrix has a different number of columns "
503 "than the number of vertices of the mesh faces.");
509template<EdgeMeshConcept MeshType, MatrixConcept EMatrix>
510void importEdgesFromMatrix(
512 const EMatrix& edges,
513 bool clearBeforeSet =
true)
515 if (
edges.cols() != 2)
516 throw WrongSizeException(
"The input edge matrix must have 2 columns");
518 if (clearBeforeSet) {
520 mesh.resizeEdges(
edges.rows());
523 if (
edges.rows() != mesh.edgeNumber()) {
524 throw WrongSizeException(
525 "The input edge matrix has a different number of rows than "
526 "the number of edges of the mesh");
531 for (
auto& e : mesh.
edges()) {
532 e.setVertex(0,
edges(i, 0));
533 e.setVertex(1,
edges(i, 1));
538template<MeshConcept MeshType, MatrixConcept VNMatrix>
539void importVertexNormalsFromMatrix(
541 const VNMatrix& vertexNormals)
543 detail::importElementNormalsFromMatrix<ElemId::VERTEX>(mesh, vertexNormals);
546template<FaceMeshConcept MeshType, MatrixConcept FNMatrix>
547void importFaceNormalsFromMatrix(MeshType& mesh,
const FNMatrix& faceNormals)
549 detail::importElementNormalsFromMatrix<ElemId::FACE>(mesh, faceNormals);
552template<MeshConcept MeshType, MatrixConcept VCMatrix>
553void importVertexColorsFromMatrix(MeshType& mesh,
const VCMatrix& vertexColors)
555 detail::importElementColorsFromMatrix<ElemId::VERTEX>(mesh, vertexColors);
558template<FaceMeshConcept MeshType, MatrixConcept FCMatrix>
559void importFaceColorsFromMatrix(MeshType& mesh,
const FCMatrix& faceColors)
561 detail::importElementColorsFromMatrix<ElemId::FACE>(mesh, faceColors);
564template<EdgeMeshConcept MeshType, MatrixConcept ECMatrix>
565void importEdgeColorsFromMatrix(MeshType& mesh,
const ECMatrix& edgeColors)
567 detail::importElementColorsFromMatrix<ElemId::EDGE>(mesh, edgeColors);
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
constexpr detail::EdgesView edges
A view that allows to iterate overt the Edge elements of an object.
Definition edge.h:52
constexpr detail::VerticesView vertices
A view that allows to iterate over the Vertex elements of an object.
Definition vertex.h:60