1 // Copyright (C) 2007-2015 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
23 // SMESH SMESH_OctreeNode : Octree with Nodes set
24 // inherites class SMESH_Octree
25 // File : SMESH_OctreeNode.cxx
26 // Created : Tue Jan 16 16:00:00 2007
27 // Author : Nicolas Geimer & Aurelien Motteux (OCC)
30 #include "SMESH_OctreeNode.hxx"
32 #include "SMDS_SetIterator.hxx"
33 #include "SMESH_TypeDefs.hxx"
38 //===============================================================
40 * \brief Constructor : Build all the Octree using Compute()
41 * \param theNodes - Set of nodes, the Octree is built from this nodes
42 * \param maxLevel - Maximum level for the leaves
43 * \param maxNbNodes - Maximum number of nodes, a leaf can contain
44 * \param minBoxSize - Minimal size of the Octree Box
46 //================================================================
48 SMESH_OctreeNode::SMESH_OctreeNode (const TIDSortedNodeSet & theNodes, const int maxLevel,
49 const int maxNbNodes , const double minBoxSize )
50 :SMESH_Octree( new Limit( maxLevel,minBoxSize,maxNbNodes)),
56 //================================================================================
58 * \brief Constructor used to allocate a child
60 //================================================================================
62 SMESH_OctreeNode::SMESH_OctreeNode ():SMESH_Octree()
66 //================================================================================
68 * \brief Return max number of nodes in a tree leaf
70 //================================================================================
72 int SMESH_OctreeNode::getMaxNbNodes() const
74 return ((Limit*)myLimit)->myMaxNbNodes;
77 //==================================================================================
79 * \brief Construct an empty SMESH_OctreeNode used by SMESH_Octree::buildChildren()
81 //==================================================================================
83 SMESH_Octree* SMESH_OctreeNode::newChild() const
85 return new SMESH_OctreeNode();
88 //======================================
90 * \brief Compute the first bounding box
92 * We take the max/min coord of the nodes
94 //======================================
96 Bnd_B3d* SMESH_OctreeNode::buildRootBox()
98 Bnd_B3d* box = new Bnd_B3d;
99 TIDSortedNodeSet::iterator it = myNodes.begin();
100 for (; it != myNodes.end(); it++) {
101 const SMDS_MeshNode* n1 = *it;
102 gp_XYZ p1( n1->X(), n1->Y(), n1->Z() );
105 if ( myNodes.size() <= getMaxNbNodes() )
111 //====================================================================================
113 * \brief Tells us if Node is inside the current box with the precision "precision"
115 * \param precision - The box is enlarged with this precision
116 * \retval bool - True if Node is in the box within precision
118 //====================================================================================
120 const bool SMESH_OctreeNode::isInside (const gp_XYZ& p, const double precision)
123 return !(getBox()->IsOut(p));
124 Bnd_B3d BoxWithPrecision = *getBox();
125 BoxWithPrecision.Enlarge(precision);
126 return ! BoxWithPrecision.IsOut(p);
129 //================================================
131 * \brief Set the data of the children
132 * Shares the father's data with each of his child
134 //================================================
135 void SMESH_OctreeNode::buildChildrenData()
137 gp_XYZ min = getBox()->CornerMin();
138 gp_XYZ max = getBox()->CornerMax();
139 gp_XYZ mid = (min + max)/2.;
141 TIDSortedNodeSet::iterator it = myNodes.begin();
142 while (it != myNodes.end())
144 const SMDS_MeshNode* n1 = *it;
145 int ChildBoxNum = getChildIndex( n1->X(), n1->Y(), n1->Z(), mid );
146 SMESH_OctreeNode* myChild = dynamic_cast<SMESH_OctreeNode*> (myChildren[ChildBoxNum]);
147 myChild->myNodes.insert(myChild->myNodes.end(),n1);
149 it = myNodes.begin();
151 for (int i = 0; i < 8; i++)
153 SMESH_OctreeNode* myChild = dynamic_cast<SMESH_OctreeNode*> (myChildren[i]);
154 if ( myChild->myNodes.size() <= getMaxNbNodes() )
155 myChild->myIsLeaf = true;
159 //===================================================================
161 * \brief Return in Result a list of Nodes potentials to be near Node
163 * \param precision - precision used
164 * \param Result - list of Nodes potentials to be near Node
166 //====================================================================
167 void SMESH_OctreeNode::NodesAround (const SMDS_MeshNode * Node,
168 list<const SMDS_MeshNode*>* Result,
169 const double precision)
171 SMESH_TNodeXYZ p(Node);
172 if (isInside(p, precision))
176 Result->insert(Result->end(), myNodes.begin(), myNodes.end());
180 for (int i = 0; i < 8; i++)
182 SMESH_OctreeNode* myChild = dynamic_cast<SMESH_OctreeNode*> (myChildren[i]);
183 myChild->NodesAround(Node, Result, precision);
189 //================================================================================
191 * \brief Return in dist2Nodes nodes mapped to their square distance from Node
192 * \param node - node to find nodes closest to
193 * \param dist2Nodes - map of found nodes and their distances
194 * \param precision - radius of a sphere to check nodes inside
195 * \retval bool - true if an exact overlapping found !!!
197 //================================================================================
199 bool SMESH_OctreeNode::NodesAround(const gp_XYZ &node,
200 map<double, const SMDS_MeshNode*>& dist2Nodes,
203 if ( !dist2Nodes.empty() )
204 precision = min ( precision, sqrt( dist2Nodes.begin()->first ));
205 else if ( precision == 0. )
206 precision = maxSize() / 2;
208 if (isInside(node, precision))
212 // first check a child containing node
213 gp_XYZ mid = (getBox()->CornerMin() + getBox()->CornerMax()) / 2.;
214 int nodeChild = getChildIndex( node.X(), node.Y(), node.Z(), mid );
215 if ( ((SMESH_OctreeNode*) myChildren[nodeChild])->NodesAround(node, dist2Nodes, precision))
218 for (int i = 0; i < 8; i++)
219 if ( i != nodeChild )
220 if (((SMESH_OctreeNode*) myChildren[i])->NodesAround(node, dist2Nodes, precision))
223 else if ( NbNodes() > 0 )
225 double minDist = precision * precision;
226 TIDSortedNodeSet::iterator nIt = myNodes.begin();
227 for ( ; nIt != myNodes.end(); ++nIt )
229 SMESH_TNodeXYZ p2( *nIt );
230 double dist2 = ( node - p2 ).SquareModulus();
231 if ( dist2 < minDist )
232 dist2Nodes.insert( make_pair( minDist = dist2, p2._node ));
234 // if ( dist2Nodes.size() > 1 ) // leave only closest node in dist2Nodes
235 // dist2Nodes.erase( ++dist2Nodes.begin(), dist2Nodes.end());
237 // true if an exact overlapping found
238 return ( sqrt( minDist ) <= precision * 1e-12 );
244 //=============================
246 * \brief Return in theGroupsOfNodes a list of group of nodes close to each other within theTolerance
247 * Search for all the nodes in theSetOfNodes
248 * Static Method : no need to create an SMESH_OctreeNode
249 * \param theSetOfNodes - set of nodes we look at, modified during research
250 * \param theGroupsOfNodes - list of nodes closed to each other returned
251 * \param theTolerance - Precision used, default value is 0.00001
252 * \param maxLevel - Maximum level for SMESH_OctreeNode constructed, default value is -1 (Infinite)
253 * \param maxNbNodes - maximum Nodes in a Leaf of the SMESH_OctreeNode constructed, default value is 5
255 //=============================
256 void SMESH_OctreeNode::FindCoincidentNodes (TIDSortedNodeSet& theSetOfNodes,
257 list< list< const SMDS_MeshNode*> >* theGroupsOfNodes,
258 const double theTolerance,
260 const int maxNbNodes)
262 // VSR 14/10/2011: limit max number of the levels in order to avoid endless recursing
263 const int MAX_LEVEL = 10;
264 SMESH_OctreeNode theOctreeNode(theSetOfNodes, maxLevel < 0 ? MAX_LEVEL : maxLevel, maxNbNodes, theTolerance);
265 theOctreeNode.FindCoincidentNodes (&theSetOfNodes, theTolerance, theGroupsOfNodes);
268 //=============================
270 * \brief Return in theGroupsOfNodes a list of group of nodes close to each other within theTolerance
271 * Search for all the nodes in theSetOfNodes
272 * \note The Octree itself is also modified by this method
273 * \param theSetOfNodes - set of nodes we look at, modified during research
274 * \param theTolerance - Precision used
275 * \param theGroupsOfNodes - list of nodes closed to each other returned
277 //=============================
278 void SMESH_OctreeNode::FindCoincidentNodes ( TIDSortedNodeSet* theSetOfNodes,
279 const double theTolerance,
280 list< list< const SMDS_MeshNode*> >* theGroupsOfNodes)
282 TIDSortedNodeSet::iterator it1 = theSetOfNodes->begin();
283 list<const SMDS_MeshNode*>::iterator it2;
285 list<const SMDS_MeshNode*> ListOfCoincidentNodes;
288 while (it1 != theSetOfNodes->end())
290 const SMDS_MeshNode * n1 = *it1;
292 // Searching for Nodes around n1 and put them in ListofCoincidentNodes.
293 // Found nodes are also erased from theSetOfNodes
294 FindCoincidentNodes(n1, theSetOfNodes, &ListOfCoincidentNodes, theTolerance);
296 if ( !ListOfCoincidentNodes.empty() )
298 // We build a list {n1 + his neigbours} and add this list in theGroupsOfNodes
299 if ( idLess( n1, ListOfCoincidentNodes.front() )) ListOfCoincidentNodes.push_front( n1 );
300 else ListOfCoincidentNodes.push_back ( n1 );
301 ListOfCoincidentNodes.sort( idLess );
302 theGroupsOfNodes->push_back( list<const SMDS_MeshNode*>() );
303 theGroupsOfNodes->back().splice( theGroupsOfNodes->back().end(), ListOfCoincidentNodes );
306 theSetOfNodes->erase(it1);
307 it1 = theSetOfNodes->begin();
311 //======================================================================================
313 * \brief Return a list of nodes closed to Node and remove it from SetOfNodes
314 * \note The Octree itself is also modified by this method
315 * \param Node - We're searching the nodes next to him.
316 * \param SetOfNodes - set of nodes in which we erase the found nodes
317 * \param Result - list of nodes closed to Node
318 * \param precision - Precision used
320 //======================================================================================
321 void SMESH_OctreeNode::FindCoincidentNodes (const SMDS_MeshNode * Node,
322 TIDSortedNodeSet* SetOfNodes,
323 list<const SMDS_MeshNode*>* Result,
324 const double precision)
326 gp_Pnt p1 (Node->X(), Node->Y(), Node->Z());
327 bool isInsideBool = isInside( p1.XYZ(), precision );
331 // I'm only looking in the leaves, since all the nodes are stored there.
334 TIDSortedNodeSet::iterator it = myNodes.begin();
335 const double tol2 = precision * precision;
338 while (it != myNodes.end())
340 const SMDS_MeshNode* n2 = *it;
342 // We're only looking at nodes with a superior Id.
344 //if (Node->GetID() < n2->GetID())
345 if (Node->GetID() != n2->GetID()) // JFA: for bug 0020185
347 gp_Pnt p2 (n2->X(), n2->Y(), n2->Z());
348 // Distance optimized computation
349 squareBool = (p1.SquareDistance( p2 ) <= tol2);
351 // If n2 inside the SquareDistance, we add it in Result and remove it from SetOfNodes and myNodes
354 Result->insert(Result->begin(), n2);
355 SetOfNodes->erase( n2 );
356 myNodes.erase( *it++ ); // it++ goes forward and returns it's previous position
362 if ( !Result->empty() )
363 myNodes.erase(Node); // JFA: for bug 0020185
367 // If I'm not a leaf, I'm going to see my children !
368 for (int i = 0; i < 8; i++)
370 SMESH_OctreeNode* myChild = dynamic_cast<SMESH_OctreeNode*> (myChildren[i]);
371 myChild->FindCoincidentNodes(Node, SetOfNodes, Result, precision);
377 //================================================================================
379 * \brief Update data according to node movement
381 //================================================================================
383 void SMESH_OctreeNode::UpdateByMoveNode( const SMDS_MeshNode* node, const gp_Pnt& toPnt )
387 TIDSortedNodeSet::iterator pNode = myNodes.find( node );
388 bool nodeInMe = ( pNode != myNodes.end() );
390 bool pointInMe = isInside( toPnt.Coord(), 1e-10 );
392 if ( pointInMe != nodeInMe )
395 myNodes.insert( node );
397 myNodes.erase( node );
400 else if ( myChildren )
402 gp_XYZ mid = (getBox()->CornerMin() + getBox()->CornerMax()) / 2.;
403 int nodeChild = getChildIndex( node->X(), node->Y(), node->Z(), mid );
404 int pointChild = getChildIndex( toPnt.X(), toPnt.Y(), toPnt.Z(), mid );
405 if ( nodeChild != pointChild )
407 ((SMESH_OctreeNode*) myChildren[ nodeChild ])->UpdateByMoveNode( node, toPnt );
408 ((SMESH_OctreeNode*) myChildren[ pointChild ])->UpdateByMoveNode( node, toPnt );
413 //================================================================================
415 * \brief Return iterator over children
417 //================================================================================
418 SMESH_OctreeNodeIteratorPtr SMESH_OctreeNode::GetChildrenIterator()
420 return SMESH_OctreeNodeIteratorPtr
421 ( new SMDS_SetIterator< SMESH_OctreeNode*, TBaseTree** >
422 ( myChildren, (( isLeaf() || !myChildren ) ? myChildren : &myChildren[ 8 ] )));
425 //================================================================================
427 * \brief Return nodes iterator
429 //================================================================================
430 SMDS_NodeIteratorPtr SMESH_OctreeNode::GetNodeIterator()
432 return SMDS_NodeIteratorPtr
433 ( new SMDS_SetIterator< SMDS_pNode, TIDSortedNodeSet::const_iterator >
434 ( myNodes.begin(), myNodes.size() ? myNodes.end() : myNodes.begin()));