0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / BRepMesh / BRepMesh_VertexTool.cxx
1 // Created on: 2011-06-02
2 // Created by: Oleg AGASHIN
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <BRepMesh_VertexTool.hxx>
17 #include <Precision.hxx>
18
19 //=======================================================================
20 //function : Inspect
21 //purpose  : 
22 //=======================================================================
23 NCollection_CellFilter_Action BRepMesh_VertexInspector::Inspect(
24   const Standard_Integer theTarget)
25 {
26   const BRepMesh_Vertex& aVertex = myVertices->Value(theTarget - 1);
27   if(aVertex.Movability() == BRepMesh_Deleted)
28   {
29     myDelNodes.Append(theTarget);
30     return CellFilter_Purge;
31   }
32   
33   gp_XY aVec = (myPoint - aVertex.Coord());
34   Standard_Boolean inTol;
35   if (Abs(myTolerance[1]) < Precision::Confusion())
36   {
37     inTol = aVec.SquareModulus() < myTolerance[0];
38   }
39   else
40   {
41     inTol = ((aVec.X() * aVec.X()) < myTolerance[0]) && 
42             ((aVec.Y() * aVec.Y()) < myTolerance[1]);
43   }
44
45   if (inTol)
46   {
47     const Standard_Real aSqDist = aVec.SquareModulus();
48     if (aSqDist < myMinSqDist)
49     {
50       myMinSqDist = aSqDist;
51       myIndex     = theTarget;
52     }
53   }
54
55   return CellFilter_Keep;
56 }
57
58 //=======================================================================
59 //function : BRepMesh_VertexTool
60 //purpose  : 
61 //=======================================================================
62 BRepMesh_VertexTool::BRepMesh_VertexTool(
63   const Handle(NCollection_IncAllocator)& theAllocator)
64   : myAllocator (theAllocator),
65     myCellFilter(0., myAllocator),
66     mySelector  (myAllocator)
67 {
68   const Standard_Real aTol = Precision::Confusion();
69   SetCellSize ( aTol + 0.05 * aTol );
70   SetTolerance( aTol, aTol );
71 }
72
73 //=======================================================================
74 //function : Add
75 //purpose  : 
76 //=======================================================================
77 Standard_Integer BRepMesh_VertexTool::Add(
78   const BRepMesh_Vertex& theVertex,
79   const Standard_Boolean isForceAdd)
80 {
81   Standard_Integer aIndex = isForceAdd ? 0 : FindIndex(theVertex);
82   if (aIndex == 0)
83   {
84     aIndex = mySelector.Add(theVertex);
85
86     gp_XY aMinPnt, aMaxPnt;
87     expandPoint(theVertex.Coord(), aMinPnt, aMaxPnt);
88     myCellFilter.Add(aIndex, aMinPnt, aMaxPnt);
89   }
90   return aIndex;
91 }
92
93 //=======================================================================
94 //function : Delete
95 //purpose  : 
96 //=======================================================================
97 void BRepMesh_VertexTool::DeleteVertex(const Standard_Integer theIndex)
98 {
99   BRepMesh_Vertex& aV = mySelector.GetVertex(theIndex);
100
101   gp_XY aMinPnt, aMaxPnt;
102   expandPoint(aV.Coord(), aMinPnt, aMaxPnt);
103
104   myCellFilter.Remove(theIndex, aMinPnt, aMaxPnt);
105   mySelector.Delete(theIndex);
106 }
107
108 //=======================================================================
109 //function : Substitute
110 //purpose  : 
111 //=======================================================================
112 void BRepMesh_VertexTool::Substitute(
113   const Standard_Integer theIndex,
114   const BRepMesh_Vertex& theVertex)
115 {
116   BRepMesh_Vertex& aV = mySelector.GetVertex(theIndex);
117
118   gp_XY aMinPnt, aMaxPnt;
119   expandPoint(aV.Coord(), aMinPnt, aMaxPnt);
120
121   myCellFilter.Remove(theIndex, aMinPnt, aMaxPnt);
122
123   aV = theVertex;
124   expandPoint(aV.Coord(), aMinPnt, aMaxPnt);
125   myCellFilter.Add(theIndex, aMinPnt, aMaxPnt);
126 }
127
128 //=======================================================================
129 //function : Statistics
130 //purpose  : 
131 //=======================================================================
132 void BRepMesh_VertexTool::Statistics(Standard_OStream& theStream) const
133 {
134   theStream << "\nStructure Statistics\n---------------\n\n";
135   theStream << "This structure has " << mySelector.NbVertices() << " Nodes\n\n";
136 }