Salome HOME
Porting to OCCT 7.4 dev
[modules/smesh.git] / src / SMESHUtils / SMESH_Delaunay.cxx
1 // Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File      : SMESH_Delaunay.cxx
23 // Created   : Wed Apr 19 15:41:15 2017
24 // Author    : Edward AGAPOV (eap)
25
26 #include "SMESH_Delaunay.hxx"
27
28 #include "SMESH_Comment.hxx"
29 #include "SMESH_File.hxx"
30 #include "SMESH_MeshAlgos.hxx"
31
32 #include <BRepAdaptor_Surface.hxx>
33 #include <BRepMesh_Delaun.hxx>
34
35 #include <Basics_OCCTVersion.hxx>
36
37 //================================================================================
38 /*!
39  * \brief Construct a Delaunay triangulation of given boundary nodes
40  *  \param [in] boundaryNodes - vector of nodes of a wire
41  *  \param [in] face - the face
42  *  \param [in] faceID - the face ID
43  *  \param [in] nbNodesToVisit - nb of non-marked nodes on the face
44  */
45 //================================================================================
46
47 SMESH_Delaunay::SMESH_Delaunay(const std::vector< const UVPtStructVec* > & boundaryNodes,
48                                const TopoDS_Face&                          face,
49                                const int                                   faceID)
50   : _face( face ), _faceID( faceID ), _scale( 1., 1. )
51 {
52   // compute _scale
53   BRepAdaptor_Surface surf( face );
54   if ( surf.GetType() != GeomAbs_Plane )
55   {
56     const int nbDiv = 100;
57     const double uRange = surf.LastUParameter() - surf.FirstUParameter();
58     const double vRange = surf.LastVParameter() - surf.FirstVParameter();
59     const double dU = uRange / nbDiv;
60     const double dV = vRange / nbDiv;
61     double u = surf.FirstUParameter(), v = surf.FirstVParameter();
62     gp_Pnt p0U = surf.Value( u, v ), p0V = p0U;
63     double lenU = 0, lenV = 0;
64     for ( ; u < surf.LastUParameter(); u += dU, v += dV )
65     {
66       gp_Pnt p1U = surf.Value( u, surf.FirstVParameter() );
67       lenU += p1U.Distance( p0U );
68       p0U = p1U;
69       gp_Pnt p1V = surf.Value( surf.FirstUParameter(), v );
70       lenV += p1V.Distance( p0V );
71       p0V = p1V;
72     }
73     _scale.SetCoord( lenU / uRange, lenV / vRange );
74   }
75
76   // count boundary points
77   int iP = 1, nbP = 0;
78   for ( size_t iW = 0; iW < boundaryNodes.size(); ++iW ) // loop on wires
79   {
80     nbP += boundaryNodes[iW]->size();
81     if ( boundaryNodes[iW]->front().node == boundaryNodes[iW]->back().node )
82       --nbP; // 1st and last points coincide
83   }
84   _bndNodes.resize( nbP );
85
86   // fill boundary points
87 #if OCC_VERSION_LARGE <= 0x07030000
88   BRepMesh::Array1OfVertexOfDelaun bndVert( 1, 1 + nbP );
89 #else
90   IMeshData::Array1OfVertexOfDelaun bndVert( 1, 1 + nbP );
91 #endif
92   BRepMesh_Vertex v( 0, 0, BRepMesh_Frontier );
93   for ( size_t iW = 0; iW < boundaryNodes.size(); ++iW )
94   {
95     const UVPtStructVec& bndPnt = *boundaryNodes[iW];
96     int i = 0, nb = bndPnt.size();
97     if ( bndPnt[0].node == bndPnt.back().node )
98       --nb;
99     for ( ; i < nb;  ++i, ++iP )
100     {
101       _bndNodes[ iP-1 ] = bndPnt[i].node;
102       bndPnt[i].node->setIsMarked( true );
103
104       v.ChangeCoord() = bndPnt[i].UV().Multiplied( _scale );
105       bndVert( iP )   = v;
106     }
107   }
108
109   // triangulate the srcFace in 2D
110   BRepMesh_Delaun Delaunay( bndVert );
111   _triaDS = Delaunay.Result();
112 }
113
114 //================================================================================
115 /*!
116  * \brief Prepare to the exploration of nodes
117  */
118 //================================================================================
119
120 void SMESH_Delaunay::InitTraversal(const int nbNodesToVisit)
121 {
122   _nbNodesToVisit = (size_t) nbNodesToVisit;
123   _nbVisitedNodes = _iBndNode = 0;
124   _noTriQueue.clear();
125 }
126
127 //================================================================================
128 /*!
129  * \brief Return a node with its Barycentric Coordinates within the triangle
130  *        defined by its node indices (zero based)
131  *  \param [out] bc - Barycentric Coordinates of the returned node
132  *  \param [out] triaNodes - indices of triangle nodes
133  *  \return const SMDS_MeshNode* - the next node or NULL
134  */
135 //================================================================================
136
137 const SMDS_MeshNode* SMESH_Delaunay::NextNode( double bc[3], int triaNodes[3] )
138 {
139   while ( _nbVisitedNodes < _nbNodesToVisit )
140   {
141     while ( !_noTriQueue.empty() )
142     {
143       const SMDS_MeshNode*     node = _noTriQueue.front().first;
144       const BRepMesh_Triangle* tria = _noTriQueue.front().second;
145       _noTriQueue.pop_front();
146       if ( node->isMarked() )
147         continue;
148       ++_nbVisitedNodes;
149       node->setIsMarked( true );
150
151       // find a Delaunay triangle containing the src node
152       gp_XY uv = getNodeUV( _face, node );
153       tria = FindTriangle( uv, tria, bc, triaNodes );
154       if ( tria )
155       {
156         addCloseNodes( node, tria, _faceID, _noTriQueue );
157         return node;
158       }
159     }
160     for ( ; _iBndNode < _bndNodes.size() &&  _noTriQueue.empty();  ++_iBndNode )
161     {
162       if ( const BRepMesh_Triangle* tria = GetTriangleNear( _iBndNode ))
163         addCloseNodes( _bndNodes[ _iBndNode ], tria, _faceID, _noTriQueue );
164     }
165     if ( _noTriQueue.empty() )
166       break;
167   }
168
169   // if ( _nbVisitedNodes < _nbNodesToVisit )
170   //   _nbVisitedNodes = std::numeric_limits<int>::max();
171   return NULL;
172 }
173
174 //================================================================================
175 /*!
176  * \brief Find a Delaunay triangle containing a given 2D point and return
177  *        barycentric coordinates within the found triangle
178  */
179 //================================================================================
180
181 const BRepMesh_Triangle* SMESH_Delaunay::FindTriangle( const gp_XY&             UV,
182                                                        const BRepMesh_Triangle* tria,
183                                                        double                   bc[3],
184                                                        int                      triaNodes[3] )
185 {
186   int   nodeIDs[3];
187   gp_XY nodeUVs[3];
188   int   linkIDs[3];
189   Standard_Boolean ori[3];
190
191   gp_XY uv = UV.Multiplied( _scale );
192
193   while ( tria )
194   {
195     // check if the uv is in tria
196
197     _triaDS->ElementNodes( *tria, nodeIDs );
198     nodeUVs[0] = _triaDS->GetNode( nodeIDs[0] ).Coord();
199     nodeUVs[1] = _triaDS->GetNode( nodeIDs[1] ).Coord();
200     nodeUVs[2] = _triaDS->GetNode( nodeIDs[2] ).Coord();
201
202     SMESH_MeshAlgos::GetBarycentricCoords( uv,
203                                            nodeUVs[0], nodeUVs[1], nodeUVs[2],
204                                            bc[0], bc[1] );
205     if ( bc[0] >= 0 && bc[1] >= 0 && bc[0] + bc[1] <= 1 )
206     {
207       if ( _triaDS->GetNode( nodeIDs[0] ).Movability() != BRepMesh_Frontier ||
208            _triaDS->GetNode( nodeIDs[1] ).Movability() != BRepMesh_Frontier ||
209            _triaDS->GetNode( nodeIDs[2] ).Movability() != BRepMesh_Frontier )
210       {
211         return 0;
212       }
213       bc[2] = 1 - bc[0] - bc[1];
214       triaNodes[0] = nodeIDs[0] - 1;
215       triaNodes[1] = nodeIDs[1] - 1;
216       triaNodes[2] = nodeIDs[2] - 1;
217       return tria;
218     }
219
220     // look for a neighbor triangle, which is adjacent to a link intersected
221     // by a segment( triangle center -> uv )
222
223     gp_XY gc = ( nodeUVs[0] + nodeUVs[1] + nodeUVs[2] ) / 3.;
224     gp_XY seg = uv - gc;
225
226     tria->Edges( linkIDs, ori );
227 #if OCC_VERSION_LARGE <= 0x07030000
228     int triaID = _triaDS->IndexOf( *tria );
229 #else
230     int triaID = tria - & ( _triaDS->GetElement( 0 ));
231 #endif
232     tria = 0;
233
234     for ( int i = 0; i < 3; ++i )
235     {
236       const BRepMesh_PairOfIndex & triIDs = _triaDS->ElementsConnectedTo( linkIDs[i] );
237       if ( triIDs.Extent() < 2 )
238         continue; // no neighbor triangle
239
240       // check if a link intersects gc2uv
241       const BRepMesh_Edge & link = _triaDS->GetLink( linkIDs[i] );
242       const BRepMesh_Vertex & n1 = _triaDS->GetNode( link.FirstNode() );
243       const BRepMesh_Vertex & n2 = _triaDS->GetNode( link.LastNode() );
244       gp_XY uv1 = n1.Coord();
245       gp_XY lin = n2.Coord() - uv1; // link direction
246
247       double crossSegLin = seg ^ lin;
248       if ( Abs( crossSegLin ) < std::numeric_limits<double>::min() )
249         continue; // parallel
250
251       double uSeg = ( uv1 - gc ) ^ lin / crossSegLin;
252       if ( 0. <= uSeg && uSeg <= 1. )
253       {
254         tria = & _triaDS->GetElement( triIDs.Index( 1 + ( triIDs.Index(1) == triaID )));
255         if ( tria->Movability() != BRepMesh_Deleted )
256           break;
257       }
258     }
259   }
260   return tria;
261 }
262
263 //================================================================================
264 /*!
265  * \brief Return a triangle sharing a given boundary node
266  *  \param [in] iBndNode - index of the boundary node
267  *  \return const BRepMesh_Triangle* - a found triangle
268  */
269 //================================================================================
270
271 const BRepMesh_Triangle* SMESH_Delaunay::GetTriangleNear( int iBndNode )
272 {
273   int nodeIDs[3];
274   int nbNbNodes = _bndNodes.size();
275 #if OCC_VERSION_LARGE <= 0x07030000
276   const BRepMesh::ListOfInteger & linkIds = _triaDS->LinksConnectedTo( iBndNode + 1 );
277   BRepMesh::ListOfInteger::const_iterator iLink = linkIds.cbegin();
278 #else
279   const IMeshData::ListOfInteger & linkIds = _triaDS->LinksConnectedTo( iBndNode + 1 );
280   IMeshData::ListOfInteger::const_iterator iLink = linkIds.cbegin();
281 #endif
282   for ( ; iLink != linkIds.cend(); ++iLink )
283   {
284     const BRepMesh_PairOfIndex & triaIds = _triaDS->ElementsConnectedTo( *iLink );
285     {
286       const BRepMesh_Triangle& tria = _triaDS->GetElement( triaIds.Index(1) );
287       if ( tria.Movability() != BRepMesh_Deleted )
288       {
289         _triaDS->ElementNodes( tria, nodeIDs );
290         if ( nodeIDs[0]-1 < nbNbNodes &&
291              nodeIDs[1]-1 < nbNbNodes &&
292              nodeIDs[2]-1 < nbNbNodes )
293           return &tria;
294       }
295     }
296     if ( triaIds.Extent() > 1 )
297     {
298       const BRepMesh_Triangle& tria = _triaDS->GetElement( triaIds.Index(2) );
299       if ( tria.Movability() != BRepMesh_Deleted )
300       {
301         _triaDS->ElementNodes( tria, nodeIDs );
302         if ( nodeIDs[0]-1 < nbNbNodes &&
303              nodeIDs[1]-1 < nbNbNodes &&
304              nodeIDs[2]-1 < nbNbNodes )
305           return &tria;
306       }
307     }
308   }
309   return 0;
310 }
311
312 //================================================================================
313 /*!
314  * \brief Return UV of the i-th source boundary node (zero based)
315  */
316 //================================================================================
317
318 gp_XY SMESH_Delaunay::GetBndUV(const int iNode) const
319 {
320   return _triaDS->GetNode( iNode+1 ).Coord();
321 }
322
323 //================================================================================
324 /*!
325  * \brief Add non-marked nodes surrounding a given one to a queue
326  */
327 //================================================================================
328
329 void SMESH_Delaunay::addCloseNodes( const SMDS_MeshNode*     node,
330                                     const BRepMesh_Triangle* tria,
331                                     const int                faceID,
332                                     TNodeTriaList &          _noTriQueue )
333 {
334   // find in-FACE nodes
335   SMDS_ElemIteratorPtr elems = node->GetInverseElementIterator(SMDSAbs_Face);
336   while ( elems->more() )
337   {
338     const SMDS_MeshElement* elem = elems->next();
339     if ( elem->getshapeId() == faceID )
340     {
341       for ( int i = 0, nb = elem->NbNodes(); i < nb; ++i )
342       {
343         const SMDS_MeshNode* n = elem->GetNode( i );
344         if ( !n->isMarked() /*&& n->getshapeId() == faceID*/ )
345           _noTriQueue.push_back( std::make_pair( n, tria ));
346       }
347     }
348   }
349 }
350
351 //================================================================================
352 /*!
353  * \brief Write a python script that creates an equal mesh in Mesh module
354  */
355 //================================================================================
356
357 void SMESH_Delaunay::ToPython() const
358 {
359   SMESH_Comment text;
360   text << "import salome, SMESH\n";
361   text << "salome.salome_init()\n";
362   text << "from salome.smesh import smeshBuilder\n";
363   text << "smesh = smeshBuilder.New(salome.myStudy)\n";
364   text << "mesh=smesh.Mesh()\n";
365   const char* endl = "\n";
366
367   for ( int i = 0; i < _triaDS->NbNodes(); ++i )
368   {
369     const BRepMesh_Vertex& v = _triaDS->GetNode( i+1 );
370     text << "mesh.AddNode( " << v.Coord().X() << ", " << v.Coord().Y() << ", 0 )" << endl;
371   }
372
373   int nodeIDs[3];
374   for ( int i = 0; i < _triaDS->NbElements(); ++i )
375   {
376     const BRepMesh_Triangle& t = _triaDS->GetElement( i+1 );
377     if ( t.Movability() == BRepMesh_Deleted )
378       continue;
379     _triaDS->ElementNodes( t, nodeIDs );
380     text << "mesh.AddFace([ " << nodeIDs[0] << ", " << nodeIDs[1] << ", " << nodeIDs[2] << " ])" << endl;
381   }
382
383   const char* fileName = "/tmp/Delaunay.py";
384   SMESH_File file( fileName, false );
385   file.remove();
386   file.openForWriting();
387   file.write( text.c_str(), text.size() );
388   cout << "execfile( '" << fileName << "')" << endl;
389 }