23#ifndef VCL_MESH_MESH_H
24#define VCL_MESH_MESH_H
26#include "mesh_components.h"
27#include "mesh_containers.h"
29#include <vclib/algorithms/core.h>
66template<
typename... Args>
69 static_assert(
HasVertices<Args...>,
"A Mesh must have a VertexContainer.");
71 template<
typename El,
bool b>
72 friend struct comp::detail::CustomComponentsData;
74 template<
typename El,
bool b>
75 friend struct comp::detail::ComponentData;
77 template<ElementConcept T>
85 template<u
int ELEM_ID>
101 template<ElementConcept El>
136 template<u
int ELEM_ID>
150 template<u
int ELEM_ID>
179 template<ElementConcept El>
202 template<u
int ELEM_ID>
226 template<u
int ELEM_ID, u
int COMP_ID>
251 template<u
int ELEM_ID, u
int COMP_ID>
255 HasPerElementOptionalComponent<
Mesh<Args...>, ELEM_ID,
COMP_ID>;
266 updateAllParentMeshPointers();
281 updateAllParentMeshPointers();
287 std::array<const void*, N_CONTAINERS>
othBases =
386 template<
typename OtherMeshType>
414 std::array<std::size_t, N_CONTAINERS>
sizes =
419 std::array<const void*, N_CONTAINERS>
bases =
437 if (
matrix != Matrixtype::Identity()) {
448 std::vector<uint>
mapping(
m.materialsNumber());
452 for (
uint i = 0;
i <
m.materialsNumber(); ++
i) {
454 this->materialBegin(), this->materialEnd(),
m.material(
i));
456 if (
it == this->materialEnd()) {
457 this->pushMaterial(
m.material(
i));
461 mapping[
i] = std::distance(this->materialBegin(),
it);
468 for (
const auto&
p :
m.textureImages()) {
469 this->pushTextureImage(
p.first,
p.second);
493 template<
typename OtherMeshType>
502 (Args::importFrom(
m), ...);
505 updateAllParentMeshPointers();
531 using FaceContainer =
typename Mesh<Args...>::FaceContainer;
532 FaceContainer::manageImportTriFromPoly(
m);
543 Mesh<Args...>&
m1 = *
this;
551 std::array<const void*, N_CONTAINERS>
m1Bases =
553 std::array<const void*, N_CONTAINERS>
m2Bases =
560 (
swap((Args&)
m1, (Args&)
m2), ...);
563 m1.updateAllParentMeshPointers();
564 m2.updateAllParentMeshPointers();
607 template<ElementConcept El>
611 return Container::index(&e);
625 template<ElementConcept El>
629 return Container::index(e);
646 template<u
int ELEM_ID>
651 return Cont::element(
i);
668 template<u
int ELEM_ID>
673 return Cont::element(
i);
685 template<u
int ELEM_ID>
690 return Cont::elementNumber();
707 template<u
int ELEM_ID>
712 return Cont::elementContainerSize();
725 template<u
int ELEM_ID>
730 return Cont::deletedElementNumber();
747 template<u
int ELEM_ID>
752 return Cont::addElement();
770 template<u
int ELEM_ID>
775 return Cont::addElements(
n);
798 template<u
int ELEM_ID>
803 Cont::clearElements();
839 template<u
int ELEM_ID>
844 Cont::resizeElements(
n);
867 template<u
int ELEM_ID>
872 Cont::reserveElements(
n);
881 template<u
int ELEM_ID>
886 Cont::compactElements();
906 template<u
int ELEM_ID>
911 Cont::deleteElement(
i);
931 template<ElementConcept El>
935 return Cont::deleteElement(e);
955 template<ElementConcept El>
959 return Cont::deleteElement(&e);
979 template<u
int ELEM_ID>
985 return Cont::elementCompactIndices();
1014 template<u
int ELEM_ID>
1020 return Cont::updateElementIndices(
newIndices);
1023 void serialize(std::ostream&
os)
const
1030 void deserialize(std::istream& is)
1032 (preDeserialization<Args>(is), ...);
1034 (postDeserialization<Args>(is), ...);
1054 template<u
int ELEM_ID>
1073 template<u
int ELEM_ID>
1078 return Cont::elementEnd();
1098 template<u
int ELEM_ID>
1118 template<u
int ELEM_ID>
1123 return Cont::elementEnd();
1148 template<u
int ELEM_ID>
1185 template<u
int ELEM_ID>
1216 template<u
int ELEM_ID>
1254 template<u
int ELEM_ID>
1276 template<u
int ELEM_ID, u
int COMP_ID>
1296 template<u
int ELEM_ID, u
int COMP_ID>
1316 template<u
int ELEM_ID, u
int COMP_ID>
1341 template<u
int ELEM_ID>
1347 return Cont::hasElemCustomComponent(name);
1362 template<u
int ELEM_ID>
1368 return Cont::elemCustomComponentNames();
1385 template<u
int ELEM_ID,
typename K>
1407 template<u
int ELEM_ID>
1413 return Cont::elemCustomComponentType(name);
1429 template<u
int ELEM_ID,
typename K>
1450 template<u
int ELEM_ID,
typename K>
1470 template<u
int ELEM_ID>
1476 Cont::deleteElemCustomComponent(name);
1520 template<u
int ELEM_ID,
typename K>
1522 const std::string& name)
1571 template<u
int ELEM_ID,
typename K>
1573 const std::string& name)
const
1606 template<u
int ELEM_ID,
typename K>
1626 template<u
int ELEM_ID,
typename K>
1635 template<
typename Cont>
1636 bool isContainerCompact()
const
1638 if constexpr (mesh::ElementContainerConcept<Cont>) {
1639 return Cont::elementNumber() == Cont::elementContainerSize();
1655 template<
typename Cont>
1658 if constexpr (mesh::ElementContainerConcept<Cont>) {
1659 if (Cont::elementNumber() != Cont::elementContainerSize()) {
1660 Cont::compactElements();
1674 template<
typename Cont>
1677 if constexpr (mesh::ElementContainerConcept<Cont>) {
1678 Cont::enableAllOptionalComponents();
1691 template<
typename Cont>
1694 if constexpr (mesh::ElementContainerConcept<Cont>) {
1695 Cont::disableAllOptionalComponents();
1708 template<
typename Cont>
1711 if constexpr (mesh::ElementContainerConcept<Cont>) {
1712 Cont::clearElements();
1716 template<ElementConcept Element>
1724 template<
typename Element,
typename... A>
1725 void updateReferences(
const Element* oldBase, TypeWrapper<A...>)
1728 oldBase, TypeWrapper<A...>(), std::array<std::size_t, 0>(), 0);
1732 template<
typename Cont,
typename Element>
1733 void updateReferences(
const Element* oldBase)
1735 updateReferences<Cont>(oldBase, std::array<std::size_t, 0>(), 0);
1741 template<
typename Element, std::size_t N,
typename... A>
1742 void updateReferences(
1743 const Element* oldBase,
1745 const std::array<std::size_t, N>& sizes = std::array<std::size_t, 0>(),
1748 (updateReferences<A>(oldBase, sizes, offset), ...);
1751 template<
typename Cont,
typename Element, std::
size_t N>
1752 void updateReferences(
1753 const Element* oldBase,
1754 const std::array<std::size_t, N>& sizes = std::array<std::size_t, 0>(),
1757 if constexpr (mesh::ElementContainerConcept<Cont>) {
1758 if constexpr (N > 0) {
1760 constexpr uint I = IndexInTypes<Cont, Containers>::value;
1761 static_assert(I >= 0 && I !=
UINT_NULL);
1762 Cont::updateReferences(oldBase, sizes[I], offset);
1765 Cont::updateReferences(oldBase);
1770 template<ElementConcept Element>
1771 void updateAllReferences(
const std::vector<uint>& newIndices)
1773 (updateReferences<Args, Element>(newIndices), ...);
1776 template<
typename Cont,
typename Element>
1777 void updateReferences(
const std::vector<uint>& newIndices)
1779 if constexpr (mesh::ElementContainerConcept<Cont>) {
1780 Cont::template updateReferences<Element>(newIndices);
1788 bool isAvailable()
const {
return true; }
1792 template<
typename Cont,
typename OtherMeshType>
1795 if constexpr (mesh::ElementContainerConcept<Cont>) {
1796 Cont::enableOptionalComponentsOf(m);
1802 void updateAllParentMeshPointers() { (setParentMeshPointers<Args>(), ...); }
1804 template<
typename Cont>
1805 void setParentMeshPointers()
1807 if constexpr (mesh::ElementContainerConcept<Cont>) {
1808 Cont::setParentMeshPointers(
this);
1812 template<
typename Cont>
1813 void appendContainer(
const Mesh& m)
1815 if constexpr (mesh::ElementContainerConcept<Cont>) {
1816 Cont::append((
const Cont&) m);
1829 template<uint I,
typename Cont,
typename Array,
typename... A>
1835 if constexpr (mesh::ElementContainerConcept<Cont>) {
1836 static_assert(I >= 0 && I !=
UINT_NULL);
1837 bases[I] =
m.Cont::mElemVec.data();
1841 template<
typename... A>
1842 static auto getContainerBases(
const Mesh<A...>&
m)
1850 std::array<const void*, N_CONTAINERS>
bases;
1867 template<uint I,
typename Cont,
typename Array,
typename... A>
1873 if constexpr (mesh::ElementContainerConcept<Cont>) {
1874 static_assert(I >= 0 && I !=
UINT_NULL);
1879 template<
typename... A>
1880 static auto getContainerSizes(
const Mesh<A...>&
m)
1888 std::array<std::size_t, N_CONTAINERS>
sizes;
1922 template<
typename Cont,
typename Array,
typename... A>
1930 if constexpr (mesh::ElementContainerConcept<Cont>) {
1934 using ElType = Cont::ElementType;
1938 static_assert(I >= 0 && I !=
UINT_NULL);
1951 static void updateReferencesOfContainerTypeAfterAppend(
1959 if constexpr (mesh::ElementContainerConcept<Cont>) {
1963 using ElType = Cont::ElementType;
1967 static_assert(I >= 0 && I !=
UINT_NULL);
1982 template<
typename ElementType>
1985 using Cont = ContainerOf<ElementType>::type;
1987 Cont::swapVerticalComponents(e1.index(), e2.index());
1995 Matrix44Concept MatrixType,
1997 static void updatePosAndNormalsOfContainerTypeAfterAppend(
1999 const ArrayS& sizes,
2000 const MatrixType& matrix)
2005 if constexpr (mesh::ElementContainerConcept<Cont>) {
2009 using ElType = Cont::ElementType;
2010 static constexpr uint ELEM_ID = ElType::ELEMENT_ID;
2013 constexpr uint I = IndexInTypes<Cont, Containers>::value;
2014 static_assert(I >= 0 && I !=
UINT_NULL);
2016 if constexpr (hasPerElementComponent<ELEM_ID, CompId::POSITION>()) {
2017 auto posview = m.template elements<ELEM_ID>((uint) sizes[I]) |
2018 vcl::views::positions;
2020 multiplyPointsByMatrix(posview, matrix);
2023 if constexpr (hasPerElementComponent<ELEM_ID, CompId::NORMAL>()) {
2024 if (m.Cont::template isComponentAvailable<CompId::NORMAL>()) {
2026 m.template elements<ELEM_ID>((uint) sizes[I]) |
2027 vcl::views::normals;
2029 multiplyNormalsByMatrix(norview, matrix);
2035 template<
typename Cont,
typename ArrayS,
typename... A>
2036 static void updateMaterialIndicesOfContainerTypeAfterAppend(
2038 const ArrayS& sizes,
2039 const std::vector<uint>& mapping)
2044 if constexpr (mesh::ElementContainerConcept<Cont>) {
2048 using ElType = Cont::ElementType;
2049 static constexpr uint ELEM_ID = ElType::ELEMENT_ID;
2052 constexpr uint I = IndexInTypes<Cont, Containers>::value;
2053 static_assert(I >= 0 && I !=
UINT_NULL);
2057 CompId::MATERIAL_INDEX>()) {
2058 if (m.Cont::template isComponentAvailable<
2059 CompId::MATERIAL_INDEX>()) {
2060 auto elems = m.template elements<ELEM_ID>((uint) sizes[I]);
2061 for (
auto& e : elems) {
2062 e.materialIndex() = mapping[e.materialIndex()];
2071 template<
typename Cont>
2072 void preSerialization(std::ostream& os)
const
2074 if constexpr (mesh::ElementContainerConcept<Cont>) {
2075 Cont::serializeOptionalComponentsAndElementsNumber(os);
2079 template<
typename Cont>
2080 void postSerialization(std::ostream& os)
const
2082 if constexpr (mesh::ElementContainerConcept<Cont>) {
2083 Cont::serializeElements(os);
2087 Cont::serialize(os);
2091 template<
typename Cont>
2092 void preDeserialization(std::istream& is)
2094 if constexpr (mesh::ElementContainerConcept<Cont>) {
2095 Cont::deserializeOptionalComponentsAndElementsNumber(is);
2099 template<
typename Cont>
2100 void postDeserialization(std::istream& is)
2102 if constexpr (mesh::ElementContainerConcept<Cont>) {
2103 Cont::deserializeElements(is);
2107 Cont::deserialize(is);
2113 template<u
int ELEM_ID,
typename T>
2114 uint elementIndex(
const T* el)
const
2116 using Cont = ContainerOfElement<ELEM_ID>::type;
2117 using ElType = Cont::ElementType;
2118 return index(
static_cast<const ElType*
>(el));
2121 template<
typename El>
2122 auto& customComponents()
2124 using ElCont = ContainerOf<El>::type;
2126 return ElCont::mCustomCompVecMap;
2129 template<
typename El>
2130 const auto& customComponents()
const
2132 using ElCont = ContainerOf<El>::type;
2134 return ElCont::mCustomCompVecMap;
2137 template<
typename El>
2138 auto& verticalComponents()
2140 using ElCont = ContainerOf<El>::type;
2142 return ElCont::mVerticalCompVecTuple;
2145 template<
typename El>
2146 const auto& verticalComponents()
const
2148 using ElCont = ContainerOf<El>::type;
2150 return ElCont::mVerticalCompVecTuple;
The Array class is a dynamically allocated N-dimensional array stored in RowWise mode.
Definition array.h:60
A class representing a box in N-dimensional space.
Definition box.h:46
PointT size() const
Computes the size of the box.
Definition box.h:267
The Element class.
Definition element.h:75
The Mesh class represents a generic 3D mesh. A mesh is composed of a generic number of containers of ...
Definition mesh.h:68
bool isCompact() const
Returns true if this mesh is compact, meaning that all its containers have no deleted elements (size ...
Definition mesh.h:318
auto elements(bool jumpDeleted=true) const
Definition mesh.h:1217
uint deletedNumber() const
Returns the number of deleted elements of the given type in this mesh.
Definition mesh.h:726
void deleteElement(uint i)
Marks as deleted the element at the given index from its container, deduced from the template index E...
Definition mesh.h:907
void deleteElement(const El &e) const
Marks as deleted the given element from its container.
Definition mesh.h:956
Mesh()
Empty constructor, constructs an empty mesh.
Definition mesh.h:263
Mesh(const Mesh &oth)
Copy constructor of the Mesh. Will create a deep copy of the given input mesh, taking care of copying...
Definition mesh.h:276
auto begin(bool jumpDeleted=true)
Returns an iterator to the begining of the container of the elements having ID ELEM_ID in the mesh.
Definition mesh.h:1055
CustomComponentVectorHandle< K > perElementCustomComponentVectorHandle(const std::string &name)
Returns a vector handle to the custom component of type K having the given name associated to the Ele...
Definition mesh.h:1521
std::type_index perElementCustomComponentType(const std::string &name) const
Returns the std::type_index of the custom component having the given name associated to the Element h...
Definition mesh.h:1408
void compactContainer()
Definition mesh.h:1656
auto elements(bool jumpDeleted=true)
Definition mesh.h:1149
void compactElements()
Compacts the Container of the given element, removing all the elements marked as deleted....
Definition mesh.h:882
void enableSameOptionalComponentsOf(const OtherMeshType &m)
Enables all the OptionalComponents of this mesh according to the Components available on the OtherMes...
Definition mesh.h:387
Mesh(Mesh &&oth)
Move constructor, moves the given mesh into this one, without any other resource acquisition.
Definition mesh.h:304
bool hasPerElementCustomComponent(const std::string &name) const
Returns true if the Element having ID ELEM_ID has a custom component with the given name.
Definition mesh.h:1342
std::vector< uint > compactIndices() const
Returns a vector that tells, for each element of the container of ELEM_ID in the mesh,...
Definition mesh.h:980
void deserializePerElementCustomComponentsOfType(std::istream &is)
Deserializes the custom components of type K of the element with ID ELEM_ID.
Definition mesh.h:1627
void deleteElement(const El *e) const
Marks as deleted the given element from its container.
Definition mesh.h:932
void disableAllOptionalComponents()
Disables all the optional components of the elements of the containers if the mesh.
Definition mesh.h:352
void addPerElementCustomComponent(const std::string &name)
Adds a custom component of type K having the given name to the Element having ID ELEM_ID.
Definition mesh.h:1451
void append(const Mesh &m)
Appends all the elements contained in the mesh m to this mesh.
Definition mesh.h:408
auto end()
Returns an iterator to the end of the container of the elements having ID ELEM_ID in the mesh.
Definition mesh.h:1074
void enableAllOptionalComponentsInContainer()
Definition mesh.h:1675
bool isPerElementCustomComponentOfType(const std::string &name) const
Returns true if the Element having ID ELEM_ID has a custom component of type K with the given name.
Definition mesh.h:1386
uint add()
Adds a new element of the given type into its container, returning the index of the added element in ...
Definition mesh.h:748
void clear()
Clears all the Elements contained in the mesh.
Definition mesh.h:329
uint add(uint n)
Adds n new elements of the given type into its container, returning the index of the first added elem...
Definition mesh.h:771
void swap(Mesh &m2)
Swaps this mesh with the other input Mesh m2.
Definition mesh.h:541
uint containerSize() const
Returns the size of the container of elements of the given type in this mesh.
Definition mesh.h:708
void clearElements()
Clears the container of ELEM_ID elements the Mesh, deleting all the Elements.
Definition mesh.h:799
void importFrom(const OtherMeshType &m)
Imports all the components that can be imported from another type of mesh.
Definition mesh.h:494
bool isPerElementComponentEnabled() const
Returns true if optional Component having ID COMP_ID is enabled for elements having ID ELEM_ID in the...
Definition mesh.h:1277
const auto & element(uint i) const
Returns the element of the given type at the given index inside its container of this mesh.
Definition mesh.h:647
auto elements(uint begin, uint end=UINT_NULL) const
Returns a lightweight const view object that stores the begin and end const iterators of the containe...
Definition mesh.h:1255
void compact()
Compacts all the containers of the mesh.
Definition mesh.h:337
void updateIndices(const std::vector< uint > &newIndices)
Updates all the indices and pointers of the elements of the container of ELEM_ID in the mesh,...
Definition mesh.h:1015
Mesh & operator=(Mesh oth)
Assignment operator of the Mesh.
Definition mesh.h:588
void clearContainer()
Definition mesh.h:1709
void deletePerElementCustomComponent(const std::string &name)
Deletes the custom component of the given name from the Element having ID ELEM_ID.
Definition mesh.h:1471
void reserve(uint n)
Reserves a number of elements of the given type in its container. The function does not add any eleme...
Definition mesh.h:868
void resize(uint n)
Resizes the Element container to contain n Elements of type ELEM_ID.
Definition mesh.h:840
static void updateReferencesOfContainerType(Mesh< A... > &m, const Array &bases)
Definition mesh.h:1923
auto end() const
Returns a const iterator to the end of the container of the elements having ID ELEM_ID in the mesh.
Definition mesh.h:1119
void enableAllOptionalComponents()
Enables all the optional components of the elements of the containers if the mesh.
Definition mesh.h:343
std::vector< std::string > perElementCustomComponentNames() const
Returns a vector containing all the names of the custom components of any type associated to the Elem...
Definition mesh.h:1363
void disableAllOptionalComponentsInContainer()
Definition mesh.h:1692
static constexpr bool hasPerElementComponent()
Returns true if this Mesh has a container of elements having the same Element ID of the template ELEM...
Definition mesh.h:227
static void setContainerBase(const Mesh< A... > &m, Array &bases)
Definition mesh.h:1830
friend void swap(Mesh &a, Mesh &b)
Specializes the swap function to allow the swapping of two Mesh objects.
Definition mesh.h:580
FilterTypesByCondition< mesh::IsElementContainerPred, TypeWrapper< Args... > >::type Containers
Containers is a vcl::TypeWrapper that wraps all the types from which the Mesh inherits (Args) that ar...
Definition mesh.h:114
auto & element(uint i)
Returns the element of the given type at the given index inside its container of this mesh.
Definition mesh.h:669
void enablePerElementComponent()
Enables the optional Component having ID COMP_ID for elements having ID ELEM_ID in the mesh.
Definition mesh.h:1297
uint index(const El *e) const
Returns the index of the given element in its Container of the Mesh.
Definition mesh.h:626
void disablePerElementComponent()
Disables the optional Component having ID COMP_ID for elements having ID ELEM_ID in the mesh.
Definition mesh.h:1317
ConstCustomComponentVectorHandle< K > perElementCustomComponentVectorHandle(const std::string &name) const
Returns a const vector handle to the custom component of type K having the given name associated to t...
Definition mesh.h:1572
uint number() const
Returns the number of elements of the given type in this mesh.
Definition mesh.h:686
ContainerType< ELEM_ID >::ElementType ElementType
ElementType is an alias that exposes the type of the Element identified by the template parameter ELE...
Definition mesh.h:151
uint index(const El &e) const
Returns the index of the given element in its Container of the Mesh.
Definition mesh.h:608
auto begin(bool jumpDeleted=true) const
Returns a const iterator to the begining of the container of the elements having ID ELEM_ID in the me...
Definition mesh.h:1099
auto elements(uint begin, uint end=UINT_NULL)
Definition mesh.h:1186
static constexpr bool hasPerElementOptionalComponent()
Returns true if this Mesh has a container of elements having the same Element ID of the template ELEM...
Definition mesh.h:252
static constexpr bool hasContainerOf()
Returns true if this Mesh has a container of elements having the same Element ID of the template Elem...
Definition mesh.h:180
std::vector< std::string > perElementCustomComponentNamesOfType() const
Returns a vector containing all the names of the custom components of type K associated to the Elemen...
Definition mesh.h:1430
void serializePerElementCustomComponentsOfType(std::ostream &os) const
Serializes the custom components of type K of the element with ID ELEM_ID.
Definition mesh.h:1607
static void setContainerSize(const Mesh< A... > &m, Array &sizes)
Definition mesh.h:1868
A concept that checks whether a class is a Mesh or an Element.
Definition mesh.h:2182
HasVertices concepts is satisfied when at least one of its types is (or inherits from) a vcl::mesh::V...
Definition vertex_container.h:1346
A concept that checks whether a class is (inherits from) a Mesh class.
Definition mesh.h:2169
The ComponentConcept is evaluated to true whenever the type T is a valid component,...
Definition component.h:46
A concept that checks whether a class has (inherits from) an FaceContainer class.
Definition face_container.h:1389
Definition mesh_components.h:55
A concept that checks whether a class has (inherits from) a VertexContainer class.
Definition vertex_container.h:1305
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
The CompId struct enumerates the components that can compose a element or a mesh.
Definition base.h:48
Removes all types that do not satisfy a condition, and get them as a TypeWrapper.
Definition filter_types.h:70
Get the index of a type T in a pack of types (variadic templates) or a TypeWrapper.
Definition variadic_templates.h:138
The ContainerOf struct allows to get the Container of an Element on this Mesh.
Definition mesh.h:103
Get the number of types in a pack of types (variadic templates) or a TypeWrapper.
Definition variadic_templates.h:189
A simple structure that wraps a list of variadic templates, without instantiating anything....
Definition type_wrapper.h:39
The ContainerOfElement structure exposes the type of the container of the given MeshType having eleme...
Definition base.h:107
The predicate IsElementContainerPred sets its bool value to true when the type T satisfies the Elemen...
Definition base.h:81