0027490: BRepMesh: Reduce number of memory allocations
[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 <gp_XY.hxx>
18 #include <gp_XYZ.hxx>
19 #include <Precision.hxx>
20 #include <BRepMesh_Vertex.hxx>
21 #include <BRepMesh_VertexInspector.hxx>
22
23 //=======================================================================
24 //function : Inspect
25 //purpose  : 
26 //=======================================================================
27 NCollection_CellFilter_Action BRepMesh_VertexInspector::Inspect(
28   const Standard_Integer theTarget)
29 {
30   const BRepMesh_Vertex& aVertex = myVertices->Value(theTarget - 1);
31   if(aVertex.Movability() == BRepMesh_Deleted)
32   {
33     myDelNodes.Append(theTarget);
34     return CellFilter_Purge;
35   }
36   
37   gp_XY aVec = (myPoint - aVertex.Coord());
38   Standard_Boolean inTol;
39   if (Abs(myTolerance[1]) < Precision::Confusion())
40   {
41     inTol = aVec.SquareModulus() < myTolerance[0];
42   }
43   else
44   {
45     inTol = ((aVec.X() * aVec.X()) < myTolerance[0]) && 
46             ((aVec.Y() * aVec.Y()) < myTolerance[1]);
47   }
48   if (inTol)
49     myResIndices.Append(theTarget);
50
51   return CellFilter_Keep;
52 }
53
54 //=======================================================================
55 //function : BRepMesh_VertexTool
56 //purpose  : 
57 //=======================================================================
58 BRepMesh_VertexTool::BRepMesh_VertexTool(
59   const Handle(NCollection_IncAllocator)& theAllocator)
60   : myAllocator (theAllocator),
61     myCellFilter(0., myAllocator),
62     mySelector  (myAllocator)
63 {
64   const Standard_Real aTol = Precision::Confusion();
65   SetCellSize ( aTol + 0.05 * aTol );
66   SetTolerance( aTol, aTol );
67 }
68
69 //=======================================================================
70 //function : Add
71 //purpose  : 
72 //=======================================================================
73 Standard_Integer BRepMesh_VertexTool::Add(
74   const BRepMesh_Vertex& theVertex,
75   const Standard_Boolean isForceAdd)
76 {
77   Standard_Integer aIndex = isForceAdd ? 0 : FindIndex(theVertex);
78   if (aIndex == 0)
79   {
80     aIndex = mySelector.Add(theVertex);
81
82     gp_XY aMinPnt, aMaxPnt;
83     expandPoint(theVertex.Coord(), aMinPnt, aMaxPnt);
84     myCellFilter.Add(aIndex, aMinPnt, aMaxPnt);
85   }
86   return aIndex;
87 }
88
89 //=======================================================================
90 //function : Delete
91 //purpose  : 
92 //=======================================================================
93 void BRepMesh_VertexTool::Delete(const Standard_Integer theIndex)
94 {
95   BRepMesh_Vertex& aV = mySelector.GetVertex(theIndex);
96
97   gp_XY aMinPnt, aMaxPnt;
98   expandPoint(aV.Coord(), aMinPnt, aMaxPnt);
99
100   myCellFilter.Remove(theIndex, aMinPnt, aMaxPnt);
101   mySelector.Delete(theIndex);
102 }
103
104 //=======================================================================
105 //function : Substitute
106 //purpose  : 
107 //=======================================================================
108 void BRepMesh_VertexTool::Substitute(
109   const Standard_Integer theIndex,
110   const BRepMesh_Vertex& theVertex)
111 {
112   BRepMesh_Vertex& aV = mySelector.GetVertex(theIndex);
113
114   gp_XY aMinPnt, aMaxPnt;
115   expandPoint(aV.Coord(), aMinPnt, aMaxPnt);
116
117   myCellFilter.Remove(theIndex, aMinPnt, aMaxPnt);
118
119   aV = theVertex;
120   expandPoint(aV.Coord(), aMinPnt, aMaxPnt);
121   myCellFilter.Add(theIndex, aMinPnt, aMaxPnt);
122 }
123
124 //=======================================================================
125 //function : Statistics
126 //purpose  : 
127 //=======================================================================
128 void BRepMesh_VertexTool::Statistics(Standard_OStream& theStream) const
129 {
130   theStream << "\nStructure Statistics\n---------------\n\n";
131   theStream << "This structure has " << mySelector.NbVertices() << " Nodes\n\n";
132 }