1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : SMESH_Algo.hxx
25 // Author : Paul RASCLE, EDF
28 #ifndef _SMESH_ALGO_HXX_
29 #define _SMESH_ALGO_HXX_
31 #include "SMESH_SMESH.hxx"
33 #include "SMESH_Hypothesis.hxx"
34 #include "SMESH_ComputeError.hxx"
35 #include "SMESH_Comment.hxx"
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <GeomAbs_Shape.hxx>
48 class SMESH_HypoFilter;
55 class SMESH_MesherHelper;
58 typedef std::map< SMESH_subMesh*, std::vector<int> > MapShapeNbElems;
59 typedef std::map< SMESH_subMesh*, std::vector<int> >::iterator MapShapeNbElemsItr;
62 * \brief Root of all algorithms
64 * Methods of the class are grouped into several parts:
65 * - main lifecycle methods, like Compute()
66 * - methods describing features of the algorithm, like NeedShape()
67 * - methods related to dependencies between sub-meshes imposed by the algorith
68 * - static utilities, like EdgeLength()
70 class SMESH_EXPORT SMESH_Algo:public SMESH_Hypothesis
74 * \brief Creates algorithm
75 * \param hypId - algorithm ID
76 * \param studyId - study ID
77 * \param gen - SMESH_Gen
79 SMESH_Algo(int hypId, int studyId, SMESH_Gen * gen);
84 virtual ~ SMESH_Algo();
87 * \brief Saves nothing in a stream
88 * \param save - the stream
89 * \retval std::ostream & - the stream
91 virtual std::ostream & SaveTo(std::ostream & save);
94 * \brief Loads nothing from a stream
95 * \param load - the stream
96 * \retval std::ostream & - the stream
98 virtual std::istream & LoadFrom(std::istream & load);
101 * \brief Returns all types of compatible hypotheses
103 const std::vector < std::string > & GetCompatibleHypothesis();
106 * \brief Check hypothesis definition to mesh a shape
107 * \param aMesh - the mesh
108 * \param aShape - the shape
109 * \param aStatus - check result
110 * \retval bool - true if hypothesis is well defined
112 virtual bool CheckHypothesis(SMESH_Mesh& aMesh,
113 const TopoDS_Shape& aShape,
114 SMESH_Hypothesis::Hypothesis_Status& aStatus) = 0;
116 * \brief Computes mesh on a shape
117 * \param aMesh - the mesh
118 * \param aShape - the shape
119 * \retval bool - is a success
121 * Algorithms that !NeedDiscreteBoundary() || !OnlyUnaryInput() are
122 * to set SMESH_ComputeError returned by SMESH_submesh::GetComputeError()
123 * to report problematic sub-shapes
125 virtual bool Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape) = 0;
128 * \brief Computes mesh without geometry
129 * \param aMesh - the mesh
130 * \param aHelper - helper that must be used for adding elements to \aaMesh
131 * \retval bool - is a success
133 * The method is called if ( !aMesh->HasShapeToMesh() )
135 virtual bool Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper);
138 * \brief Sets _computeCanceled to true. It's usage depends on
139 * implementation of a particular mesher.
141 virtual void CancelCompute();
144 * \brief evaluates size of prospective mesh on a shape
145 * \param aMesh - the mesh
146 * \param aShape - the shape
147 * \param aNbElems - prospective number of elements by types
148 * \retval bool - is a success
150 virtual bool Evaluate(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape,
151 MapShapeNbElems& aResMap) = 0;
154 * \brief Returns a list of compatible hypotheses used to mesh a shape
155 * \param aMesh - the mesh
156 * \param aShape - the shape
157 * \param ignoreAuxiliary - do not include auxiliary hypotheses in the list
158 * \retval const std::list <const SMESHDS_Hypothesis*> - hypotheses list
160 * List the hypothesis used by the algorithm associated to the shape.
161 * Hypothesis associated to father shape -are- taken into account (see
162 * GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
163 * the algorithm. This method could be surcharged by specific algorithms, in
164 * case of several hypothesis simultaneously applicable.
166 virtual const std::list <const SMESHDS_Hypothesis *> &
167 GetUsedHypothesis(SMESH_Mesh & aMesh,
168 const TopoDS_Shape & aShape,
169 const bool ignoreAuxiliary=true);
171 * \brief Returns a list of compatible hypotheses assigned to a shape in a mesh
172 * \param aMesh - the mesh
173 * \param aShape - the shape
174 * \param ignoreAuxiliary - do not include auxiliary hypotheses in the list
175 * \retval const std::list <const SMESHDS_Hypothesis*> - hypotheses list
177 * List the relevant hypothesis associated to the shape. Relevant hypothesis
178 * have a name (type) listed in the algorithm. Hypothesis associated to
179 * father shape -are not- taken into account (see GetUsedHypothesis)
181 const list <const SMESHDS_Hypothesis *> &
182 GetAppliedHypothesis(SMESH_Mesh & aMesh,
183 const TopoDS_Shape & aShape,
184 const bool ignoreAuxiliary=true);
186 * \brief Make the filter recognize only compatible hypotheses
187 * \param theFilter - the filter to initialize
188 * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
189 * \retval bool - true if the algo has compatible hypotheses
191 bool InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
192 const bool ignoreAuxiliary) const;
194 * \brief Just return false as the algorithm does not hold parameters values
196 virtual bool SetParametersByMesh(const SMESH_Mesh* theMesh, const TopoDS_Shape& theShape);
197 virtual bool SetParametersByDefaults(const TDefaults& dflts, const SMESH_Mesh* theMesh=0);
199 * \brief return compute error
201 SMESH_ComputeErrorPtr GetComputeError() const;
203 * \brief initialize compute error
205 void InitComputeError();
208 // ==================================================================
209 // Algo features influencing how Compute() is called:
210 // in what turn and with what input shape
211 // ==================================================================
213 // SMESH_Hypothesis::GetDim();
214 // 1 - dimention of target mesh
216 bool OnlyUnaryInput() const { return _onlyUnaryInput; }
217 // 2 - is collection of tesselatable shapes inacceptable as input;
218 // "collection" means a shape containing shapes of dim equal
220 // Algo which can process a collection shape should expect
221 // an input temporary shape that is neither MainShape nor
224 bool NeedDiscreteBoundary() const { return _requireDiscreteBoundary; }
225 // 3 - is a Dim-1 mesh prerequisite
227 bool NeedShape() const { return _requireShape; }
228 // 4 - is shape existance required
230 bool SupportSubmeshes() const { return _supportSubmeshes; }
231 // 5 - whether supports submeshes if !NeedDiscreteBoundary()
235 // ==================================================================
236 // Methods to track non hierarchical dependencies between submeshes
237 // ==================================================================
240 * \brief Sets event listener to submeshes if necessary
241 * \param subMesh - submesh where algo is set
243 * This method is called when a submesh gets HYP_OK algo_state.
244 * After being set, event listener is notified on each event of a submesh.
245 * By default none listener is set
247 virtual void SetEventListener(SMESH_subMesh* subMesh);
250 * \brief Allow algo to do something after persistent restoration
251 * \param subMesh - restored submesh
253 * This method is called only if a submesh has HYP_OK algo_state.
255 virtual void SubmeshRestored(SMESH_subMesh* subMesh);
258 // ==================================================================
259 // Common algo utilities
260 // ==================================================================
262 * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
263 * \param theMesh - The mesh containing nodes
264 * \param theEdge - The geometrical edge of interest
265 * \param theParams - The resulting vector of sorted node parameters
266 * \retval bool - false if not all parameters are OK
268 static bool GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
269 const TopoDS_Edge& theEdge,
270 std::vector< double > & theParams);
272 * \brief Fill map of node parameter on geometrical edge to node it-self
273 * \param theMesh - The mesh containing nodes
274 * \param theEdge - The geometrical edge of interest
275 * \param theNodes - The resulting map
276 * \param ignoreMediumNodes - to store medium nodes of quadratic elements or not
277 * \retval bool - false if not all parameters are OK
279 static bool GetSortedNodesOnEdge(const SMESHDS_Mesh* theMesh,
280 const TopoDS_Edge& theEdge,
281 const bool ignoreMediumNodes,
282 std::map< double, const SMDS_MeshNode* > & theNodes);
284 * \brief Find out elements orientation on a geometrical face
285 * \param theFace - The face correctly oriented in the shape being meshed
286 * \param theMeshDS - The mesh data structure
287 * \retval bool - true if the face normal and the normal of first element
288 * in the correspoding submesh point in different directions
290 static bool IsReversedSubMesh (const TopoDS_Face& theFace,
291 SMESHDS_Mesh* theMeshDS);
293 * \brief Compute length of an edge
294 * \param E - the edge
295 * \retval double - the length
297 static double EdgeLength(const TopoDS_Edge & E);
300 * \brief Calculate normal of a mesh face
302 static bool FaceNormal(const SMDS_MeshElement* F, gp_XYZ& normal, bool normalized=true);
305 * \brief Return continuity of two edges
306 * \param E1 - the 1st edge
307 * \param E2 - the 2nd edge
308 * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
310 static GeomAbs_Shape Continuity(TopoDS_Edge E1, TopoDS_Edge E2);
313 * \brief Return true if an edge can be considered as a continuation of another
315 static bool IsContinuous(const TopoDS_Edge & E1, const TopoDS_Edge & E2) {
316 return ( Continuity( E1, E2 ) >= GeomAbs_G1 );
320 * \brief Return the node built on a vertex
321 * \param V - the vertex
322 * \param meshDS - mesh
323 * \retval const SMDS_MeshNode* - found node or NULL
325 static const SMDS_MeshNode* VertexNode(const TopoDS_Vertex& V, const SMESHDS_Mesh* meshDS);
328 * \brief Return nodes common to two elements
330 static std::vector< const SMDS_MeshNode*> GetCommonNodes(const SMDS_MeshElement* e1,
331 const SMDS_MeshElement* e2);
333 enum EMeshError { MEr_OK = 0, MEr_HOLES, MEr_BAD_ORI, MEr_EMPTY };
336 * \brief Finds topological errors of a sub-mesh
338 static EMeshError GetMeshError(SMESH_subMesh* subMesh);
343 * \brief store error and comment and then return ( error == COMPERR_OK )
345 bool error(int error, const SMESH_Comment& comment = "");
347 * \brief store COMPERR_ALGO_FAILED error and comment and then return false
349 bool error(const SMESH_Comment& comment = "")
350 { return error(COMPERR_ALGO_FAILED, comment); }
352 * \brief store error and return error->IsOK()
354 bool error(SMESH_ComputeErrorPtr error);
356 * \brief store a bad input element preventing computation,
357 * which may be a temporary one i.e. not residing the mesh,
358 * then it will be deleted by InitComputeError()
360 void addBadInputElement(const SMDS_MeshElement* elem);
364 std::vector<std::string> _compatibleHypothesis;
365 std::list<const SMESHDS_Hypothesis *> _appliedHypList;
366 std::list<const SMESHDS_Hypothesis *> _usedHypList;
368 // Algo features influencing which Compute() and how is called:
369 // in what turn and with what input shape.
370 // These fields must be redefined if necessary by each descendant at constructor.
371 bool _onlyUnaryInput; // mesh one shape of GetDim() at once. Default TRUE
372 bool _requireDiscreteBoundary; // GetDim()-1 mesh must be present. Default TRUE
373 bool _requireShape; // work with GetDim()-1 mesh bound to geom only. Default TRUE
374 bool _supportSubmeshes; // if !_requireDiscreteBoundary. Default FALSE
376 // indicates if quadratic mesh creation is required,
377 // is usually set like this: _quadraticMesh = SMESH_MesherHelper::IsQuadraticSubMesh(shape)
380 int _error; //!< SMESH_ComputeErrorName or anything algo specific
381 std::string _comment; //!< any text explaining what is wrong in Compute()
382 std::list<const SMDS_MeshElement*> _badInputElements; //!< to explain COMPERR_BAD_INPUT_MESH
384 volatile bool _computeCanceled; //!< is set to True while computing to stop it