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()) {
447 std::vector<uint>
mapping(
m.textureNumber());
449 for (
uint i = 0;
i <
m.textureNumber(); ++
i) {
450 uint tpi = this->indexOfTexturePath(
m.texturePath(
i));
454 this->pushTexture(
m.texture(
i));
457 this->pushTexturePath(
m.texturePath(
i));
488 template<
typename OtherMeshType>
497 (Args::importFrom(
m), ...);
500 updateAllParentMeshPointers();
526 using FaceContainer =
typename Mesh<Args...>::FaceContainer;
527 FaceContainer::manageImportTriFromPoly(
m);
538 Mesh<Args...>&
m1 = *
this;
546 std::array<const void*, N_CONTAINERS>
m1Bases =
548 std::array<const void*, N_CONTAINERS>
m2Bases =
555 (
swap((Args&)
m1, (Args&)
m2), ...);
558 m1.updateAllParentMeshPointers();
559 m2.updateAllParentMeshPointers();
602 template<ElementConcept El>
606 return Container::index(&e);
620 template<ElementConcept El>
624 return Container::index(e);
641 template<u
int ELEM_ID>
646 return Cont::element(
i);
663 template<u
int ELEM_ID>
668 return Cont::element(
i);
680 template<u
int ELEM_ID>
685 return Cont::elementNumber();
702 template<u
int ELEM_ID>
707 return Cont::elementContainerSize();
720 template<u
int ELEM_ID>
725 return Cont::deletedElementNumber();
742 template<u
int ELEM_ID>
747 return Cont::addElement();
765 template<u
int ELEM_ID>
770 return Cont::addElements(
n);
793 template<u
int ELEM_ID>
798 Cont::clearElements();
834 template<u
int ELEM_ID>
839 Cont::resizeElements(
n);
862 template<u
int ELEM_ID>
867 Cont::reserveElements(
n);
876 template<u
int ELEM_ID>
881 Cont::compactElements();
901 template<u
int ELEM_ID>
906 Cont::deleteElement(
i);
926 template<ElementConcept El>
930 return Cont::deleteElement(e);
950 template<ElementConcept El>
954 return Cont::deleteElement(&e);
974 template<u
int ELEM_ID>
980 return Cont::elementConpactIndices();
1009 template<u
int ELEM_ID>
1015 return Cont::updateElementIndices(
newIndices);
1018 void serialize(std::ostream&
os)
const
1025 void deserialize(std::istream& is)
1027 (preDeserialization<Args>(is), ...);
1029 (postDeserialization<Args>(is), ...);
1049 template<u
int ELEM_ID>
1068 template<u
int ELEM_ID>
1073 return Cont::elementEnd();
1093 template<u
int ELEM_ID>
1113 template<u
int ELEM_ID>
1118 return Cont::elementEnd();
1143 template<u
int ELEM_ID>
1180 template<u
int ELEM_ID>
1211 template<u
int ELEM_ID>
1249 template<u
int ELEM_ID>
1271 template<u
int ELEM_ID, u
int COMP_ID>
1291 template<u
int ELEM_ID, u
int COMP_ID>
1311 template<u
int ELEM_ID, u
int COMP_ID>
1336 template<u
int ELEM_ID>
1342 return Cont::hasElemCustomComponent(name);
1357 template<u
int ELEM_ID>
1363 return Cont::elemCustomComponentNames();
1380 template<u
int ELEM_ID,
typename K>
1402 template<u
int ELEM_ID>
1408 return Cont::elemCustomComponentType(name);
1424 template<u
int ELEM_ID,
typename K>
1445 template<u
int ELEM_ID,
typename K>
1465 template<u
int ELEM_ID>
1471 Cont::deleteElemCustomComponent(name);
1515 template<u
int ELEM_ID,
typename K>
1517 const std::string& name)
1566 template<u
int ELEM_ID,
typename K>
1568 const std::string& name)
const
1601 template<u
int ELEM_ID,
typename K>
1621 template<u
int ELEM_ID,
typename K>
1630 template<
typename Cont>
1631 bool isContainerCompact()
const
1633 if constexpr (mesh::ElementContainerConcept<Cont>) {
1634 return Cont::elementNumber() == Cont::elementContainerSize();
1650 template<
typename Cont>
1653 if constexpr (mesh::ElementContainerConcept<Cont>) {
1654 if (Cont::elementNumber() != Cont::elementContainerSize()) {
1655 Cont::compactElements();
1669 template<
typename Cont>
1672 if constexpr (mesh::ElementContainerConcept<Cont>) {
1673 Cont::enableAllOptionalComponents();
1686 template<
typename Cont>
1689 if constexpr (mesh::ElementContainerConcept<Cont>) {
1690 Cont::disableAllOptionalComponents();
1703 template<
typename Cont>
1706 if constexpr (mesh::ElementContainerConcept<Cont>) {
1707 Cont::clearElements();
1711 template<ElementConcept Element>
1719 template<
typename Element,
typename... A>
1720 void updateReferences(
const Element* oldBase, TypeWrapper<A...>)
1723 oldBase, TypeWrapper<A...>(), std::array<std::size_t, 0>(), 0);
1727 template<
typename Cont,
typename Element>
1728 void updateReferences(
const Element* oldBase)
1730 updateReferences<Cont>(oldBase, std::array<std::size_t, 0>(), 0);
1736 template<
typename Element, std::size_t N,
typename... A>
1737 void updateReferences(
1738 const Element* oldBase,
1740 const std::array<std::size_t, N>& sizes = std::array<std::size_t, 0>(),
1743 (updateReferences<A>(oldBase, sizes, offset), ...);
1746 template<
typename Cont,
typename Element, std::
size_t N>
1747 void updateReferences(
1748 const Element* oldBase,
1749 const std::array<std::size_t, N>& sizes = std::array<std::size_t, 0>(),
1752 if constexpr (mesh::ElementContainerConcept<Cont>) {
1753 if constexpr (N > 0) {
1755 constexpr uint I = IndexInTypes<Cont, Containers>::value;
1756 static_assert(I >= 0 && I !=
UINT_NULL);
1757 Cont::updateReferences(oldBase, sizes[I], offset);
1760 Cont::updateReferences(oldBase);
1765 template<ElementConcept Element>
1766 void updateAllReferences(
const std::vector<uint>& newIndices)
1768 (updateReferences<Args, Element>(newIndices), ...);
1771 template<
typename Cont,
typename Element>
1772 void updateReferences(
const std::vector<uint>& newIndices)
1774 if constexpr (mesh::ElementContainerConcept<Cont>) {
1775 Cont::template updateReferences<Element>(newIndices);
1783 bool isAvailable()
const {
return true; }
1787 template<
typename Cont,
typename OtherMeshType>
1790 if constexpr (mesh::ElementContainerConcept<Cont>) {
1791 Cont::enableOptionalComponentsOf(m);
1797 void updateAllParentMeshPointers() { (setParentMeshPointers<Args>(), ...); }
1799 template<
typename Cont>
1800 void setParentMeshPointers()
1802 if constexpr (mesh::ElementContainerConcept<Cont>) {
1803 Cont::setParentMeshPointers(
this);
1807 template<
typename Cont>
1808 void appendContainer(
const Mesh& m)
1810 if constexpr (mesh::ElementContainerConcept<Cont>) {
1811 Cont::append((
const Cont&) m);
1824 template<uint I,
typename Cont,
typename Array,
typename... A>
1830 if constexpr (mesh::ElementContainerConcept<Cont>) {
1831 static_assert(I >= 0 && I !=
UINT_NULL);
1832 bases[I] =
m.Cont::mElemVec.data();
1836 template<
typename... A>
1837 static auto getContainerBases(
const Mesh<A...>&
m)
1845 std::array<const void*, N_CONTAINERS>
bases;
1862 template<uint I,
typename Cont,
typename Array,
typename... A>
1868 if constexpr (mesh::ElementContainerConcept<Cont>) {
1869 static_assert(I >= 0 && I !=
UINT_NULL);
1874 template<
typename... A>
1875 static auto getContainerSizes(
const Mesh<A...>&
m)
1883 std::array<std::size_t, N_CONTAINERS>
sizes;
1917 template<
typename Cont,
typename Array,
typename... A>
1925 if constexpr (mesh::ElementContainerConcept<Cont>) {
1929 using ElType = Cont::ElementType;
1933 static_assert(I >= 0 && I !=
UINT_NULL);
1946 static void updateReferencesOfContainerTypeAfterAppend(
1954 if constexpr (mesh::ElementContainerConcept<Cont>) {
1958 using ElType = Cont::ElementType;
1962 static_assert(I >= 0 && I !=
UINT_NULL);
1979 Matrix44Concept MatrixType,
1981 static void updatePosAndNormalsOfContainerTypeAfterAppend(
1983 const ArrayS& sizes,
1984 const MatrixType& matrix)
1989 if constexpr (mesh::ElementContainerConcept<Cont>) {
1993 using ElType = Cont::ElementType;
1994 static constexpr uint ELEM_ID = ElType::ELEMENT_ID;
1997 constexpr uint I = IndexInTypes<Cont, Containers>::value;
1998 static_assert(I >= 0 && I !=
UINT_NULL);
2000 if constexpr (hasPerElementComponent<ELEM_ID, CompId::POSITION>()) {
2001 auto posview = m.template elements<ELEM_ID>((uint) sizes[I]) |
2002 vcl::views::positions;
2004 multiplyPointsByMatrix(posview, matrix);
2007 if constexpr (hasPerElementComponent<ELEM_ID, CompId::NORMAL>()) {
2008 if (m.Cont::template isComponentAvailable<CompId::NORMAL>()) {
2010 m.template elements<ELEM_ID>((uint) sizes[I]) |
2011 vcl::views::normals;
2013 multiplyNormalsByMatrix(norview, matrix);
2019 template<
typename Cont,
typename ArrayS,
typename... A>
2020 static void updateTextureIndicesOfContainerTypeAfterAppend(
2022 const ArrayS& sizes,
2023 const std::vector<uint>& mapping)
2028 if constexpr (mesh::ElementContainerConcept<Cont>) {
2032 using ElType = Cont::ElementType;
2033 static constexpr uint ELEM_ID = ElType::ELEMENT_ID;
2036 constexpr uint I = IndexInTypes<Cont, Containers>::value;
2037 static_assert(I >= 0 && I !=
UINT_NULL);
2041 CompId::TEX_COORD>()) {
2042 if (m.Cont::template isComponentAvailable<
2043 CompId::TEX_COORD>()) {
2045 m.template elements<ELEM_ID>((uint) sizes[I]) |
2046 vcl::views::texCoords;
2047 for (
auto& tc : tcview) {
2048 tc.index() = mapping[tc.index()];
2055 CompId::WEDGE_TEX_COORDS>()) {
2056 if (m.Cont::template isComponentAvailable<
2057 CompId::WEDGE_TEX_COORDS>()) {
2058 auto elview = m.template elements<ELEM_ID>((uint) sizes[I]);
2059 for (
auto& e : elview) {
2060 e.textureIndex() = mapping[e.textureIndex()];
2069 template<
typename Cont>
2070 void preSerialization(std::ostream& os)
const
2072 if constexpr (mesh::ElementContainerConcept<Cont>) {
2073 Cont::serializeOptionalComponentsAndElementsNumber(os);
2077 template<
typename Cont>
2078 void postSerialization(std::ostream& os)
const
2080 if constexpr (mesh::ElementContainerConcept<Cont>) {
2081 Cont::serializeElements(os);
2085 Cont::serialize(os);
2089 template<
typename Cont>
2090 void preDeserialization(std::istream& is)
2092 if constexpr (mesh::ElementContainerConcept<Cont>) {
2093 Cont::deserializeOptionalComponentsAndElementsNumber(is);
2097 template<
typename Cont>
2098 void postDeserialization(std::istream& is)
2100 if constexpr (mesh::ElementContainerConcept<Cont>) {
2101 Cont::deserializeElements(is);
2105 Cont::deserialize(is);
2111 template<u
int ELEM_ID,
typename T>
2112 uint elementIndex(
const T* el)
const
2114 using Cont = ContainerOfElement<ELEM_ID>::type;
2115 using ElType = Cont::ElementType;
2116 return index(
static_cast<const ElType*
>(el));
2119 template<
typename El>
2120 auto& customComponents()
2122 using ElCont = ContainerOf<El>::type;
2124 return ElCont::mCustomCompVecMap;
2127 template<
typename El>
2128 const auto& customComponents()
const
2130 using ElCont = ContainerOf<El>::type;
2132 return ElCont::mCustomCompVecMap;
2135 template<
typename El>
2136 auto& verticalComponents()
2138 using ElCont = ContainerOf<El>::type;
2140 return ElCont::mVerticalCompVecTuple;
2143 template<
typename El>
2144 const auto& verticalComponents()
const
2146 using ElCont = ContainerOf<El>::type;
2148 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:1212
uint deletedNumber() const
Returns the number of deleted elements of the given type in this mesh.
Definition mesh.h:721
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:902
void deleteElement(const El &e) const
Marks as deleted the given element from its container.
Definition mesh.h:951
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:1050
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:1516
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:1403
void compactContainer()
Definition mesh.h:1651
auto elements(bool jumpDeleted=true)
Definition mesh.h:1144
void compactElements()
Compacts the Container of the given element, removing all the elements marked as deleted....
Definition mesh.h:877
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:1337
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:975
void deserializePerElementCustomComponentsOfType(std::istream &is)
Deserializes the custom components of type K of the element with ID ELEM_ID.
Definition mesh.h:1622
void deleteElement(const El *e) const
Marks as deleted the given element from its container.
Definition mesh.h:927
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:1446
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:1069
void enableAllOptionalComponentsInContainer()
Definition mesh.h:1670
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:1381
uint add()
Adds a new element of the given type into its container, returning the index of the added element in ...
Definition mesh.h:743
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:766
void swap(Mesh &m2)
Swaps this mesh with the other input Mesh m2.
Definition mesh.h:536
uint containerSize() const
Returns the size of the container of elements of the given type in this mesh.
Definition mesh.h:703
void clearElements()
Clears the container of ELEM_ID elements the Mesh, deleting all the Elements.
Definition mesh.h:794
void importFrom(const OtherMeshType &m)
Imports all the components that can be imported from another type of mesh.
Definition mesh.h:489
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:1272
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:642
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:1250
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:1010
Mesh & operator=(Mesh oth)
Assignment operator of the Mesh.
Definition mesh.h:583
void clearContainer()
Definition mesh.h:1704
void deletePerElementCustomComponent(const std::string &name)
Deletes the custom component of the given name from the Element having ID ELEM_ID.
Definition mesh.h:1466
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:863
void resize(uint n)
Resizes the Element container to contain n Elements of type ELEM_ID.
Definition mesh.h:835
static void updateReferencesOfContainerType(Mesh< A... > &m, const Array &bases)
Definition mesh.h:1918
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:1114
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:1358
void disableAllOptionalComponentsInContainer()
Definition mesh.h:1687
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:1825
friend void swap(Mesh &a, Mesh &b)
Specializes the swap function to allow the swapping of two Mesh objects.
Definition mesh.h:575
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:664
void enablePerElementComponent()
Enables the optional Component having ID COMP_ID for elements having ID ELEM_ID in the mesh.
Definition mesh.h:1292
uint index(const El *e) const
Returns the index of the given element in its Container of the Mesh.
Definition mesh.h:621
void disablePerElementComponent()
Disables the optional Component having ID COMP_ID for elements having ID ELEM_ID in the mesh.
Definition mesh.h:1312
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:1567
uint number() const
Returns the number of elements of the given type in this mesh.
Definition mesh.h:681
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:603
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:1094
auto elements(uint begin, uint end=UINT_NULL)
Definition mesh.h:1181
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:1425
void serializePerElementCustomComponentsOfType(std::ostream &os) const
Serializes the custom components of type K of the element with ID ELEM_ID.
Definition mesh.h:1602
static void setContainerSize(const Mesh< A... > &m, Array &sizes)
Definition mesh.h:1863
A concept that checks whether a class is a Mesh or an Element.
Definition mesh.h:2180
HasVertices concepts is satisfied when at least one of its types is (or inherits from) a vcl::mesh::V...
Definition vertex_container.h:1267
A concept that checks whether a class is (inherits from) a Mesh class.
Definition mesh.h:2167
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:1349
Definition mesh_components.h:58
Definition mesh_components.h:60
A concept that checks whether a class has (inherits from) a VertexContainer class.
Definition vertex_container.h:1226
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