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_Delaunay.cxx
23 // Created : Wed Apr 19 15:41:15 2017
24 // Author : Edward AGAPOV (eap)
26 #include "SMESH_Delaunay.hxx"
27 #include "SMESH_MeshAlgos.hxx"
29 #include <BRepAdaptor_Surface.hxx>
30 #include <BRepMesh_Delaun.hxx>
32 //================================================================================
34 * \brief Construct a Delaunay triangulation of given boundary nodes
35 * \param [in] boundaryNodes - vector of nodes of a wire
36 * \param [in] face - the face
37 * \param [in] faceID - the face ID
38 * \param [in] nbNodesToVisit - nb of non-marked nodes on the face
40 //================================================================================
42 SMESH_Delaunay::SMESH_Delaunay(const std::vector< const UVPtStructVec* > & boundaryNodes,
43 const TopoDS_Face& face,
45 : _face( face ), _faceID( faceID ), _scale( 1., 1. )
48 BRepAdaptor_Surface surf( face );
49 if ( surf.GetType() != GeomAbs_Plane )
51 const int nbDiv = 100;
52 const double uRange = surf.LastUParameter() - surf.FirstUParameter();
53 const double vRange = surf.LastVParameter() - surf.FirstVParameter();
54 const double dU = uRange / nbDiv;
55 const double dV = vRange / nbDiv;
56 double u = surf.FirstUParameter(), v = surf.FirstVParameter();
57 gp_Pnt p0U = surf.Value( u, v ), p0V = p0U;
58 double lenU = 0, lenV = 0;
59 for ( ; u < surf.LastUParameter(); u += dU, v += dV )
61 gp_Pnt p1U = surf.Value( u, surf.FirstVParameter() );
62 lenU += p1U.Distance( p0U );
64 gp_Pnt p1V = surf.Value( surf.FirstUParameter(), v );
65 lenV += p1V.Distance( p0V );
68 _scale.SetCoord( lenU / uRange, lenV / vRange );
71 // count boundary points
73 for ( size_t iW = 0; iW < boundaryNodes.size(); ++iW ) // loop on wires
75 nbP += boundaryNodes[iW]->size();
76 if ( boundaryNodes[iW]->front().node == boundaryNodes[iW]->back().node )
77 --nbP; // 1st and last points coincide
79 _bndNodes.resize( nbP );
81 // fill boundary points
82 BRepMesh::Array1OfVertexOfDelaun bndVert( 1, 1 + nbP );
83 BRepMesh_Vertex v( 0, 0, BRepMesh_Frontier );
84 for ( size_t iW = 0; iW < boundaryNodes.size(); ++iW )
86 const UVPtStructVec& bndPnt = *boundaryNodes[iW];
87 int i = 0, nb = bndPnt.size();
88 if ( bndPnt[0].node == bndPnt.back().node )
90 for ( ; i < nb; ++i, ++iP )
92 _bndNodes[ iP-1 ] = bndPnt[i].node;
93 bndPnt[i].node->setIsMarked( true );
95 v.ChangeCoord() = bndPnt[i].UV().Multiplied( _scale );
100 // triangulate the srcFace in 2D
101 BRepMesh_Delaun Delaunay( bndVert );
102 _triaDS = Delaunay.Result();
105 //================================================================================
107 * \brief Prepare to the exploration of nodes
109 //================================================================================
111 void SMESH_Delaunay::InitTraversal(const int nbNodesToVisit)
113 _nbNodesToVisit = (size_t) nbNodesToVisit;
114 _nbVisitedNodes = _iBndNode = 0;
118 //================================================================================
120 * \brief Return a node with its Barycentric Coordinates within the triangle
121 * defined by its node indices (zero based)
122 * \param [out] bc - Barycentric Coordinates of the returned node
123 * \param [out] triaNodes - indices of triangle nodes
124 * \return const SMDS_MeshNode* - the next node or NULL
126 //================================================================================
128 const SMDS_MeshNode* SMESH_Delaunay::NextNode( double bc[3], int triaNodes[3] )
130 while ( _nbVisitedNodes < _nbNodesToVisit )
132 while ( !_noTriQueue.empty() )
134 const SMDS_MeshNode* node = _noTriQueue.front().first;
135 const BRepMesh_Triangle* tria = _noTriQueue.front().second;
136 _noTriQueue.pop_front();
137 if ( node->isMarked() )
140 node->setIsMarked( true );
142 // find a Delaunay triangle containing the src node
143 gp_XY uv = getNodeUV( _face, node );
144 tria = FindTriangle( uv, tria, bc, triaNodes );
147 addCloseNodes( node, tria, _faceID, _noTriQueue );
151 for ( ; _iBndNode < _bndNodes.size() && _noTriQueue.empty(); ++_iBndNode )
153 if ( const BRepMesh_Triangle* tria = GetTriangleNear( _iBndNode ))
154 addCloseNodes( _bndNodes[ _iBndNode ], tria, _faceID, _noTriQueue );
156 if ( _noTriQueue.empty() )
160 // if ( _nbVisitedNodes < _nbNodesToVisit )
161 // _nbVisitedNodes = std::numeric_limits<int>::max();
165 //================================================================================
167 * \brief Find a Delaunay triangle containing a given 2D point and return
168 * barycentric coordinates within the found triangle
170 //================================================================================
172 const BRepMesh_Triangle* SMESH_Delaunay::FindTriangle( const gp_XY& UV,
173 const BRepMesh_Triangle* tria,
180 Standard_Boolean ori[3];
182 gp_XY uv = UV.Multiplied( _scale );
186 // check if the uv is in tria
188 _triaDS->ElementNodes( *tria, nodeIDs );
189 nodeUVs[0] = _triaDS->GetNode( nodeIDs[0] ).Coord();
190 nodeUVs[1] = _triaDS->GetNode( nodeIDs[1] ).Coord();
191 nodeUVs[2] = _triaDS->GetNode( nodeIDs[2] ).Coord();
193 SMESH_MeshAlgos::GetBarycentricCoords( uv,
194 nodeUVs[0], nodeUVs[1], nodeUVs[2],
196 if ( bc[0] >= 0 && bc[1] >= 0 && bc[0] + bc[1] <= 1 )
198 bc[2] = 1 - bc[0] - bc[1];
199 triaNodes[0] = nodeIDs[0] - 1;
200 triaNodes[1] = nodeIDs[1] - 1;
201 triaNodes[2] = nodeIDs[2] - 1;
205 // look for a neighbor triangle, which is adjacent to a link intersected
206 // by a segment( triangle center -> uv )
208 gp_XY gc = ( nodeUVs[0] + nodeUVs[1] + nodeUVs[2] ) / 3.;
211 tria->Edges( linkIDs, ori );
212 int triaID = _triaDS->IndexOf( *tria );
215 for ( int i = 0; i < 3; ++i )
217 const BRepMesh_PairOfIndex & triIDs = _triaDS->ElementsConnectedTo( linkIDs[i] );
218 if ( triIDs.Extent() < 2 )
219 continue; // no neighbor triangle
221 // check if a link intersects gc2uv
222 const BRepMesh_Edge & link = _triaDS->GetLink( linkIDs[i] );
223 const BRepMesh_Vertex & n1 = _triaDS->GetNode( link.FirstNode() );
224 const BRepMesh_Vertex & n2 = _triaDS->GetNode( link.LastNode() );
225 gp_XY uv1 = n1.Coord();
226 gp_XY lin = n2.Coord() - uv1; // link direction
228 double crossSegLin = seg ^ lin;
229 if ( Abs( crossSegLin ) < std::numeric_limits<double>::min() )
230 continue; // parallel
232 double uSeg = ( uv1 - gc ) ^ lin / crossSegLin;
233 if ( 0. <= uSeg && uSeg <= 1. )
235 tria = & _triaDS->GetElement( triIDs.Index( 1 + ( triIDs.Index(1) == triaID )));
243 //================================================================================
245 * \brief Return a triangle sharing a given boundary node
246 * \param [in] iBndNode - index of the boundary node
247 * \return const BRepMesh_Triangle* - a found triangle
249 //================================================================================
251 const BRepMesh_Triangle* SMESH_Delaunay::GetTriangleNear( int iBndNode )
253 const BRepMesh::ListOfInteger & linkIds = _triaDS->LinksConnectedTo( iBndNode + 1 );
254 const BRepMesh_PairOfIndex & triaIds = _triaDS->ElementsConnectedTo( linkIds.First() );
255 const BRepMesh_Triangle& tria = _triaDS->GetElement( triaIds.Index(1) );
259 //================================================================================
261 * \brief Return UV of the i-th source boundary node (zero based)
263 //================================================================================
265 gp_XY SMESH_Delaunay::GetBndUV(const int iNode) const
267 return _triaDS->GetNode( iNode+1 ).Coord();
270 //================================================================================
272 * \brief Add non-marked nodes surrounding a given one to a queue
274 //================================================================================
276 void SMESH_Delaunay::addCloseNodes( const SMDS_MeshNode* node,
277 const BRepMesh_Triangle* tria,
279 TNodeTriaList & _noTriQueue )
281 // find in-FACE nodes
282 SMDS_ElemIteratorPtr elems = node->GetInverseElementIterator(SMDSAbs_Face);
283 while ( elems->more() )
285 const SMDS_MeshElement* elem = elems->next();
286 if ( elem->getshapeId() == faceID )
288 for ( int i = 0, nb = elem->NbNodes(); i < nb; ++i )
290 const SMDS_MeshNode* n = elem->GetNode( i );
291 if ( !n->isMarked() /*&& n->getshapeId() == faceID*/ )
292 _noTriQueue.push_back( std::make_pair( n, tria ));