23#ifndef VCL_ALGORITHMS_MESH_IMPORT_EXPORT_IMPORT_MATRIX_H
24#define VCL_ALGORITHMS_MESH_IMPORT_EXPORT_IMPORT_MATRIX_H
26#include <vclib/mesh.h>
27#include <vclib/space/core.h>
46template<MatrixConcept FMatrix>
47std::vector<uint> faceVertIndices(
const FMatrix& faces, uint f)
49 std::vector<uint> fVerts;
53 fVerts.push_back(
faces(f, j));
58template<u
int ELEM_ID, MeshConcept MeshType, MatrixConcept NMatrix>
59void importElementNormalsFromMatrix(MeshType& mesh,
const NMatrix& normals)
62 using NormalType = MeshType::template ElementType<ELEM_ID>::NormalType;
64 if (normals.cols() != 3)
65 throw WrongSizeException(
66 "The input " + elementEnumString<ELEM_ID>() +
67 " normal matrix must have 3 columns");
70 if (normals.rows() != mesh.template number<ELEM_ID>())
71 throw WrongSizeException(
72 "The input normal matrix must have the same number of rows "
74 elementEnumString<ELEM_ID>() +
" element in the mesh");
76 enableIfPerElementComponentOptional<ELEM_ID, CompId::NORMAL>(mesh);
77 requirePerElementComponent<ELEM_ID, CompId::NORMAL>(mesh);
80 for (
auto& e : mesh.template elements<ELEM_ID>()) {
81 e.normal() = NormalType(normals(i, 0), normals(i, 1), normals(i, 2));
86template<u
int ELEM_ID, MeshConcept MeshType, MatrixConcept CMatrix>
87void importElementColorsFromMatrix(MeshType& mesh,
const CMatrix& colors)
89 using MatrixScalar = CMatrix::Scalar;
91 if (colors.cols() != 3 && colors.cols() != 4)
92 throw WrongSizeException(
93 "The input " + elementEnumString<ELEM_ID>() +
94 " color matrix must have 3 or 4 columns");
97 if (colors.rows() != mesh.template number<ELEM_ID>())
98 throw WrongSizeException(
99 "The input color matrix must have the same number of rows "
100 "as the number of " +
101 elementEnumString<ELEM_ID>() +
" element in the mesh");
103 enableIfPerElementComponentOptional<ELEM_ID, CompId::COLOR>(mesh);
104 requirePerElementComponent<ELEM_ID, CompId::COLOR>(mesh);
107 for (
auto& e : mesh.template elements<ELEM_ID>()) {
109 if constexpr (std::integral<MatrixScalar>) {
110 if (colors.cols() == 3) {
111 e.color() = Color(colors(i, 0), colors(i, 1), colors(i, 2));
115 colors(i, 0), colors(i, 1), colors(i, 2), colors(i, 3));
119 if (colors.cols() == 3) {
121 colors(i, 0) * 255, colors(i, 1) * 255, colors(i, 2) * 255);
174 MeshConcept MeshType,
175 MatrixConcept VMatrix,
176 MatrixConcept VNMatrix = Eigen::MatrixX3d>
177MeshType pointCloudMeshFromMatrices(
178 const VMatrix& vertices,
179 const VNMatrix& vertexNormals = VNMatrix())
183 importMeshFromMatrices(
240 MeshConcept MeshType,
241 MatrixConcept VMatrix,
242 MatrixConcept FMatrix = Eigen::MatrixX3i,
243 MatrixConcept VNMatrix = Eigen::MatrixX3d,
244 MatrixConcept FNMatrix = Eigen::MatrixX3d>
245MeshType meshFromMatrices(
246 const VMatrix& vertices,
247 const FMatrix& faces = FMatrix(),
248 const VNMatrix& vertexNormals = VNMatrix(),
249 const FNMatrix& faceNormals = FNMatrix())
253 importMeshFromMatrices(
254 mesh, vertices, faces, Eigen::MatrixX2i(), vertexNormals, faceNormals);
318 MeshConcept MeshType,
319 MatrixConcept VMatrix,
320 MatrixConcept FMatrix = Eigen::MatrixX3i,
321 MatrixConcept EMatrix = Eigen::MatrixX2i,
322 MatrixConcept VNMatrix = Eigen::MatrixX3d,
323 MatrixConcept FNMatrix = Eigen::MatrixX3d>
324void importMeshFromMatrices(
326 const VMatrix& vertices,
327 const FMatrix& faces = FMatrix(),
328 const EMatrix& edges = EMatrix(),
329 const VNMatrix& vertexNormals = VNMatrix(),
330 const FNMatrix& faceNormals = FNMatrix())
333 mesh.disableAllOptionalComponents();
335 importVerticesFromMatrix(mesh, vertices);
337 if constexpr (HasPerVertexNormal<MeshType>) {
338 if (vertexNormals.rows() > 0) {
339 importVertexNormalsFromMatrix(mesh, vertexNormals);
343 if constexpr (HasFaces<MeshType>) {
344 if (
faces.rows() > 0)
345 importFacesFromMatrix(mesh, faces);
347 if constexpr (HasPerFaceNormal<MeshType>) {
348 if (faceNormals.rows() > 0) {
349 importFaceNormalsFromMatrix(mesh, faceNormals);
354 if constexpr (HasEdges<MeshType>) {
355 if (
edges.rows() > 0)
356 importEdgesFromMatrix(mesh, edges);
398template<MeshConcept MeshType, MatrixConcept VMatrix>
399void importVerticesFromMatrix(
401 const VMatrix& vertices,
402 bool clearBeforeSet =
true)
404 using PositionType = MeshType::VertexType::PositionType;
407 throw WrongSizeException(
"The input vertex matrix must have 3 columns");
409 if (clearBeforeSet) {
410 mesh.clearVertices();
411 mesh.resizeVertices(
vertices.rows());
414 if (
vertices.rows() != mesh.vertexNumber()) {
415 throw WrongSizeException(
416 "The input vertex matrix has a different number of rows than "
417 "the number of vertices of the mesh");
429template<FaceMeshConcept MeshType, MatrixConcept FMatrix>
430void importFacesFromMatrix(
432 const FMatrix& faces,
433 bool clearBeforeSet =
true)
435 if (clearBeforeSet) {
437 mesh.resizeFaces(
faces.rows());
440 if (
faces.rows() != mesh.faceNumber()) {
441 throw WrongSizeException(
442 "The input face matrix has a different number of rows "
443 "than the number of faces of the mesh.");
447 if constexpr (HasPolygons<MeshType>) {
449 for (
auto& f : mesh.
faces()) {
450 uint vertexNumber = 0;
453 while (vertexNumber <
faces.cols() &&
454 faces(i, vertexNumber) != -1 &&
458 f.resizeVertices(vertexNumber);
460 for (uint j = 0; j < vertexNumber; ++j)
461 f.setVertex(j,
faces(i, j));
466 using FaceType = MeshType::FaceType;
468 constexpr int VN = FaceType::VERTEX_NUMBER;
469 if (
faces.cols() == VN) {
471 for (
auto& f : mesh.
faces()) {
472 for (uint j = 0; j < VN; ++j)
473 f.setVertex(j,
faces(i, j));
480 if constexpr (VN == 3) {
481 if (!clearBeforeSet) {
482 throw WrongSizeException(
483 "Cannot import the input face matrix into the mesh "
484 "without clearing the face container first "
485 "(need to perform a triangulation to import polygons "
486 "in a triangle mesh, and this operation that does not "
487 "guarantee a predefined number of faces is required).");
489 mesh.reserveFaces(
faces.rows());
490 for (uint i = 0; i <
faces.rows(); ++i) {
491 std::vector<uint> fVertIndices =
492 detail::faceVertIndices(faces, i);
494 addTriangleFacesFromPolygon(mesh, fVertIndices);
500 throw WrongSizeException(
501 "The input face matrix has a different number of columns "
502 "than the number of vertices of the mesh faces.");
508template<EdgeMeshConcept MeshType, MatrixConcept EMatrix>
509void importEdgesFromMatrix(
511 const EMatrix& edges,
512 bool clearBeforeSet =
true)
514 if (
edges.cols() != 2)
515 throw WrongSizeException(
"The input edge matrix must have 2 columns");
517 if (clearBeforeSet) {
519 mesh.resizeEdges(
edges.rows());
522 if (
edges.rows() != mesh.edgeNumber()) {
523 throw WrongSizeException(
524 "The input edge matrix has a different number of rows than "
525 "the number of edges of the mesh");
530 for (
auto& e : mesh.
edges()) {
531 e.setVertex(0,
edges(i, 0));
532 e.setVertex(1,
edges(i, 1));
537template<MeshConcept MeshType, MatrixConcept VNMatrix>
538void importVertexNormalsFromMatrix(
540 const VNMatrix& vertexNormals)
542 detail::importElementNormalsFromMatrix<ElemId::VERTEX>(mesh, vertexNormals);
545template<FaceMeshConcept MeshType, MatrixConcept FNMatrix>
546void importFaceNormalsFromMatrix(MeshType& mesh,
const FNMatrix& faceNormals)
548 detail::importElementNormalsFromMatrix<ElemId::FACE>(mesh, faceNormals);
551template<MeshConcept MeshType, MatrixConcept VCMatrix>
552void importVertexColorsFromMatrix(MeshType& mesh,
const VCMatrix& vertexColors)
554 detail::importElementColorsFromMatrix<ElemId::VERTEX>(mesh, vertexColors);
557template<FaceMeshConcept MeshType, MatrixConcept FCMatrix>
558void importFaceColorsFromMatrix(MeshType& mesh,
const FCMatrix& faceColors)
560 detail::importElementColorsFromMatrix<ElemId::FACE>(mesh, faceColors);
563template<EdgeMeshConcept MeshType, MatrixConcept ECMatrix>
564void importEdgeColorsFromMatrix(MeshType& mesh,
const ECMatrix& edgeColors)
566 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:84
constexpr detail::EdgesView edges
A view that allows to iterate overt the Edge elements of an object.
Definition edge.h:84
constexpr detail::VerticesView vertices
A view that allows to iterate over the Vertex elements of an object.
Definition vertex.h:92