1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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
22 // File : SMESH_MeshAlgos.hxx
23 // Created : Tue Apr 30 18:00:36 2013
24 // Author : Edward AGAPOV (eap)
26 // This file holds some low level algorithms extracted from SMESH_MeshEditor
27 // to make them accessible from Controls package
30 #ifndef __SMESH_MeshAlgos_HXX__
31 #define __SMESH_MeshAlgos_HXX__
33 #include "SMESH_Utils.hxx"
35 #include "SMDSAbs_ElementType.hxx"
36 #include "SMDS_ElemIterator.hxx"
37 #include "SMESH_TypeDefs.hxx"
39 #include <TopAbs_State.hxx>
45 class SMDS_MeshElement;
48 //=======================================================================
50 * \brief Searcher for the node closest to a point
52 //=======================================================================
54 struct SMESHUtils_EXPORT SMESH_NodeSearcher
56 virtual const SMDS_MeshNode* FindClosestTo( const gp_Pnt& pnt ) = 0;
57 virtual void MoveNode( const SMDS_MeshNode* node, const gp_Pnt& toPnt ) = 0;
58 virtual int FindNearPoint(const gp_Pnt& point,
59 const double tolerance,
60 std::vector< const SMDS_MeshNode* >& foundNodes) = 0;
61 virtual ~SMESH_NodeSearcher() {}
64 //=======================================================================
66 * \brief Searcher for elements
68 //=======================================================================
70 struct SMESHUtils_EXPORT SMESH_ElementSearcher
73 * \brief Find elements of given type where the given point is IN or ON.
74 * Returns nb of found elements and elements them-selves.
76 * 'ALL' type means elements of any type excluding nodes and 0D elements
78 virtual int FindElementsByPoint(const gp_Pnt& point,
79 SMDSAbs_ElementType type,
80 std::vector< const SMDS_MeshElement* >& foundElems) = 0;
82 * \brief Return an element most close to the given point
84 virtual const SMDS_MeshElement* FindClosestTo( const gp_Pnt& point,
85 SMDSAbs_ElementType type) = 0;
87 * \brief Return elements possibly intersecting the line
89 virtual void GetElementsNearLine( const gp_Ax1& line,
90 SMDSAbs_ElementType type,
91 std::vector< const SMDS_MeshElement* >& foundElems) = 0;
93 * \brief Return elements whose bounding box intersects a sphere
95 virtual void GetElementsInSphere( const gp_XYZ& center,
97 SMDSAbs_ElementType type,
98 std::vector< const SMDS_MeshElement* >& foundElems) = 0;
100 * \brief Find out if the given point is out of closed 2D mesh.
102 virtual TopAbs_State GetPointState(const gp_Pnt& point) = 0;
105 * \brief Return a projection of a given point to a 2D mesh.
106 * Optionally return the closest face
108 virtual gp_XYZ Project(const gp_Pnt& point,
109 SMDSAbs_ElementType type,
110 const SMDS_MeshElement** closestFace= 0) = 0;
112 virtual ~SMESH_ElementSearcher();
115 namespace SMESH_MeshAlgos
118 * \brief Return true if the point is IN or ON of the element
121 bool IsOut( const SMDS_MeshElement* element, const gp_Pnt& point, double tol );
124 double GetDistance( const SMDS_MeshElement* elem, const gp_Pnt& point, gp_XYZ* closestPnt = 0 );
127 double GetDistance( const SMDS_MeshEdge* edge, const gp_Pnt& point, gp_XYZ* closestPnt = 0 );
130 double GetDistance( const SMDS_MeshFace* face, const gp_Pnt& point, gp_XYZ* closestPnt = 0 );
133 double GetDistance( const SMDS_MeshVolume* volume, const gp_Pnt& point, gp_XYZ* closestPnt = 0 );
136 void GetBarycentricCoords( const gp_XY& point,
137 const gp_XY& t0, const gp_XY& t1, const gp_XY& t2,
138 double & bc0, double & bc1);
141 * Return a face having linked nodes n1 and n2 and which is
143 * - in elemSet provided that !elemSet.empty()
144 * i1 and i2 optionally returns indices of n1 and n2
147 const SMDS_MeshElement* FindFaceInSet(const SMDS_MeshNode* n1,
148 const SMDS_MeshNode* n2,
149 const TIDSortedElemSet& elemSet,
150 const TIDSortedElemSet& avoidSet,
154 * \brief Calculate normal of a mesh face
157 bool FaceNormal(const SMDS_MeshElement* F, gp_XYZ& normal, bool normalized=true);
160 * \brief Return nodes common to two elements
163 std::vector< const SMDS_MeshNode*> GetCommonNodes(const SMDS_MeshElement* e1,
164 const SMDS_MeshElement* e2);
166 * \brief Return true if node1 encounters first in the face and node2, after.
167 * The nodes are supposed to be neighbor nodes in the face.
170 bool IsRightOrder( const SMDS_MeshElement* face,
171 const SMDS_MeshNode* node0,
172 const SMDS_MeshNode* node1 );
175 * \brief Mark elements given by SMDS_Iterator
177 template< class ElemIter >
178 void MarkElems( ElemIter it, const bool isMarked )
180 while ( it->more() ) it->next()->setIsMarked( isMarked );
183 * \brief Mark elements given by std iterators
185 template< class ElemIter >
186 void MarkElems( ElemIter it, ElemIter end, const bool isMarked )
188 for ( ; it != end; ++it ) (*it)->setIsMarked( isMarked );
191 * \brief Mark nodes of elements given by SMDS_Iterator
193 template< class ElemIter >
194 void MarkElemNodes( ElemIter it, const bool isMarked, const bool markElem = false )
197 while ( it->more() ) {
198 const SMDS_MeshElement* e = it->next();
199 e->setIsMarked( isMarked );
200 MarkElems( e->nodesIterator(), isMarked );
204 MarkElems( it->next()->nodesIterator(), isMarked );
207 * \brief Mark elements given by std iterators
209 template< class ElemIter >
210 void MarkElemNodes( ElemIter it, ElemIter end, const bool isMarked, const bool markElem = false )
213 for ( ; it != end; ++it ) {
214 (*it)->setIsMarked( isMarked );
215 MarkElems( (*it)->nodesIterator(), isMarked );
218 for ( ; it != end; ++it )
219 MarkElems( (*it)->nodesIterator(), isMarked );
223 * \brief Return SMESH_NodeSearcher. The caller is responsible for deleting it
226 SMESH_NodeSearcher* GetNodeSearcher( SMDS_Mesh& mesh );
229 SMESH_NodeSearcher* GetNodeSearcher( SMDS_ElemIteratorPtr elemIt );
232 * \brief Return SMESH_ElementSearcher. The caller is responsible for deleting it
235 SMESH_ElementSearcher* GetElementSearcher( SMDS_Mesh& mesh,
236 double tolerance=-1.);
238 SMESH_ElementSearcher* GetElementSearcher( SMDS_Mesh& mesh,
239 SMDS_ElemIteratorPtr elemIt,
240 double tolerance=-1. );
244 typedef std::vector<const SMDS_MeshNode*> TFreeBorder;
245 typedef std::vector<TFreeBorder> TFreeBorderVec;
246 struct TFreeBorderPart
248 int _border; // border index within a TFreeBorderVec
249 int _node1; // node index within the border-th TFreeBorder
253 typedef std::vector<TFreeBorderPart> TCoincidentGroup;
254 typedef std::vector<TCoincidentGroup> TCoincidentGroupVec;
255 struct CoincidentFreeBorders
257 TFreeBorderVec _borders; // nodes of all free borders
258 TCoincidentGroupVec _coincidentGroups; // groups of coincident parts of borders
262 * Returns TFreeBorder's coincident within the given tolerance.
263 * If the tolerance <= 0.0 then one tenth of an average size of elements adjacent
264 * to free borders being compared is used.
266 * (Implemented in ./SMESH_FreeBorders.cxx)
269 void FindCoincidentFreeBorders(SMDS_Mesh& mesh,
271 CoincidentFreeBorders & foundFreeBordes);
273 * Returns all or only closed TFreeBorder's.
274 * Optionally check if the mesh is manifold and if faces are correctly oriented.
276 * (Implemented in ./SMESH_FreeBorders.cxx)
279 void FindFreeBorders(SMDS_Mesh& mesh,
280 TFreeBorderVec & foundFreeBordes,
281 const bool closedOnly,
282 bool* isManifold = 0,
283 bool* isGoodOri = 0);
285 * Fill a hole defined by a TFreeBorder with 2D elements.
287 * (Implemented in ./SMESH_FillHole.cxx)
290 void FillHole(const TFreeBorder & freeBorder,
292 std::vector<const SMDS_MeshElement*>& newFaces);
296 * \brief Find nodes whose merge makes the element invalid
298 * (Implemented in SMESH_DeMerge.cxx)
301 void DeMerge(const SMDS_MeshElement* elem,
302 std::vector< const SMDS_MeshNode* >& newNodes,
303 std::vector< const SMDS_MeshNode* >& noMergeNodes);
305 } // namespace SMESH_MeshAlgos