1 //                                               -*- C++ -*-
2 /**
3  *  @brief Mesh is defined as a collection of n-D vertices and simplices
4  *
5  *  Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
6  *
7  *  This library is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License
18  *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 #ifndef OPENTURNS_MESH_HXX
22 #define OPENTURNS_MESH_HXX
23 
24 #include "openturns/PersistentObject.hxx"
25 #include "openturns/Collection.hxx"
26 #include "openturns/PersistentCollection.hxx"
27 #include "openturns/Point.hxx"
28 #include "openturns/Indices.hxx"
29 #include "openturns/IndicesCollection.hxx"
30 #include "openturns/SquareMatrix.hxx"
31 #include "openturns/IdentityMatrix.hxx"
32 #include "openturns/Graph.hxx"
33 
34 BEGIN_NAMESPACE_OPENTURNS
35 
36 /**
37  * @class Mesh
38  *
39  * A class that holds a mesh
40  */
41 class OT_API Mesh
42   : public PersistentObject
43 {
44   CLASSNAME
45 
46 public:
47   /** Default constructor */
48   explicit Mesh(const UnsignedInteger dimension = 1);
49 
50   /** Parameters constructor */
51   explicit Mesh(const Sample & vertices);
52 
53   /** Parameters constructor */
54   Mesh(const Sample & vertices,
55        const IndicesCollection & simplices,
56        const Bool checkMeshValidity = ResourceMap::GetAsBool("Mesh-CheckValidity"));
57 
58   /** Virtual constructor method */
59   Mesh * clone() const override;
60 
61   /** Get the numerical volume of the domain */
62   Scalar getVolume() const;
63 
64   /** Check if the domain is empty, i.e if its volume is zero */
65   Bool isEmpty() const;
66   Bool isNumericallyEmpty() const;
67 
68   /** Get the dimension */
69   UnsignedInteger getDimension() const;
70 
71   /** Description accessor */
72   void setDescription(const Description & description);
73   Description getDescription() const;
74 
75   /** Get the number of vertices */
76   UnsignedInteger getVerticesNumber() const;
77 
78   /** Get the number of simplices */
79   UnsignedInteger getSimplicesNumber() const;
80 
81   /** Compute weights such that an integral of a function over the mesh
82    * is a weighted sum of its values at the vertices
83    */
84   Point computeWeights() const;
85 
86   /** Comparison operator */
87   Bool operator == (const Mesh & rhs) const;
88 
89   /** Check mesh validity, i.e:
90       non-overlaping simplices,
91       no unused vertex,
92       no simplices with duplicate vertices,
93       no coincident vertices */
94   Bool isValid() const;
95 
96   /** Check if the given point is in the given simplex and returns its barycentric coordinates */
97   Bool checkPointInSimplexWithCoordinates(const Point & point,
98                                           const UnsignedInteger index,
99                                           Point & coordinatesOut) const;
100 
101   /** Vertices accessor */
102   Sample getVertices() const;
103   void setVertices(const Sample & vertices);
104 
105   /** Vertex accessor */
106   Point getVertex(const UnsignedInteger index) const;
107   void setVertex(const UnsignedInteger index,
108                  const Point & vertex);
109 
110   /** Simplices accessor */
111   IndicesCollection getSimplices() const;
112   void setSimplices(const IndicesCollection & simplices);
113 
114   /** Simplex accessor */
115   Indices getSimplex(const UnsignedInteger index) const;
116 
117   /** Compute the volume of all simplices */
118   Point computeSimplicesVolume() const;
119 
120   /** Compute P1 gram matrix */
121   CovarianceMatrix computeP1Gram() const;
122 
123   /** Tells if the mesh is regular */
124   Bool isRegular() const;
125 
126   /** Lower bound of the bounding box */
127   Point getLowerBound() const;
128 
129   /** Upper bound of the bounding box */
130   Point getUpperBound() const;
131 
132   /** Orientation management */
133   void fixOrientation();
134 #ifndef SWIG
135   void fixOrientation(const UnsignedInteger & index,
136                       SquareMatrix & simplexMatrix);
137 #endif
138 
139   /** Drawing method */
140   Graph draw() const;
141   Graph draw1D() const;
142   Graph draw2D() const;
143   Graph draw3D(const Bool drawEdge = true,
144                const Scalar thetaX = 0.0,
145                const Scalar thetaY = 0.0,
146                const Scalar thetaZ = 0.0,
147                const Bool shading = false,
148                const Scalar rho = 1.0) const;
149   Graph draw3D(const Bool drawEdge,
150                const SquareMatrix & rotation,
151                const Bool shading,
152                const Scalar rho) const;
153 
154   /** String converter */
155   String __repr__() const override;
156   String __str__(const String & offset = "") const override;
157 
158   /** Method save() stores the object through the StorageManager */
159   void save(Advocate & adv) const override;
160 
161   /** Method load() reloads the object from the StorageManager */
162   void load(Advocate & adv) override;
163 
164   /** FreeFem mesh import */
165   static Mesh ImportFromMSHFile(const String & fileName);
166 
167   /** VTK export */
168   String streamToVTKFormat() const;
169   String streamToVTKFormat(const IndicesCollection & simplices) const;
170   void exportToVTKFile(const String & fileName) const;
171   void exportToVTKFile(const String & fileName,
172                        const IndicesCollection & simplices) const;
173 
174 protected:
175   // Build the affine matrix associated with a given simplex
176   void buildSimplexMatrix(const UnsignedInteger index,
177                           SquareMatrix & matrix) const;
178 
179   void checkValidity() const;
180 
181   // An n-D mesh is a set of vertices with a topology described by a set of simplices
182   // Spatial dimension
183   UnsignedInteger dimension_;
184 
185   // Mesh might be already checked (user provide the information)
186   // or we might need to check it for drawing for example.
187   mutable Bool hasBeenChecked_;
188 
189   // The vertices
190   Sample vertices_;
191 
192   // The simplices
193   IndicesCollection simplices_;
194 
195 }; /* class Mesh */
196 
197 END_NAMESPACE_OPENTURNS
198 
199 #endif /* OPENTURNS_MESH_HXX */
200