Salome HOME
a97f36c4b8cd72a4c7779ff009ca95233e6fbe53
[modules/geom.git] / src / NMTDS / NMTDS_Tools.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File:        NMTDS_Tools.cxx
21 // Created:     Tue Feb 20 14:57:28 2007
22 // Author:      Peter KURNEV
23
24 #include <NMTDS_Tools.hxx>
25 #include <TopoDS_Vertex.hxx>
26 #include <gp_Pnt.hxx>
27 #include <BRep_Tool.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopTools_IndexedDataMapOfShapeShape.hxx>
30 #include <TopAbs_ShapeEnum.hxx>
31 #include <TopoDS_Iterator.hxx>
32 #include <BRep_Builder.hxx>
33
34 void CopySource(const TopoDS_Shape& aS,
35                 TopTools_IndexedDataMapOfShapeShape& aMapSS,
36                 TopoDS_Shape& aSC);
37
38 //=======================================================================
39 //function : CopyShape
40 //purpose  :
41 //=======================================================================
42   void NMTDS_Tools::CopyShape(const TopoDS_Shape& aS,
43                               TopoDS_Shape& aSC)
44 {
45   TopTools_IndexedDataMapOfShapeShape aMapSS;
46   //
47   CopySource(aS, aMapSS, aSC);
48 }
49 //=======================================================================
50 //function : CopyShape
51 //purpose  :
52 //=======================================================================
53   void NMTDS_Tools::CopyShape(const TopoDS_Shape& aS,
54                               TopoDS_Shape& aSC,
55                               TopTools_IndexedDataMapOfShapeShape& aMapSS)
56 {
57   CopySource(aS, aMapSS, aSC);
58 }
59 //=======================================================================
60 //function : CopySource
61 //purpose  :
62 //=======================================================================
63 void CopySource(const TopoDS_Shape& aS,
64                 TopTools_IndexedDataMapOfShapeShape& aMapSS,
65                 TopoDS_Shape& aSC)
66 {
67   Standard_Boolean bFree;
68   TopAbs_ShapeEnum aT;
69   TopoDS_Iterator aIt;
70   TopoDS_Shape aSF;
71   BRep_Builder BB;
72   //
73   aT=aS.ShapeType();
74   //
75   if (aMapSS.Contains(aS)) {
76     aSC=aMapSS.ChangeFromKey(aS);
77     aSC.Orientation(aS.Orientation());
78     return;
79   }
80   else {
81     aSC=aS.EmptyCopied();
82     aMapSS.Add(aS, aSC);
83   }
84   //
85   bFree=aSC.Free();
86   aSC.Free(Standard_True);
87   aSF=aS;
88   if (aT==TopAbs_EDGE){
89     TopAbs_Orientation aOr;
90     //
91     aOr=aS.Orientation();
92     if(aOr==TopAbs_INTERNAL) {
93       aSF.Orientation(TopAbs_FORWARD);
94     }
95   }
96   aIt.Initialize(aSF);
97   for (; aIt.More();  aIt.Next()) {
98     TopoDS_Shape aSCx;
99     //
100     const TopoDS_Shape& aSx=aIt.Value();
101     //
102     CopySource (aSx, aMapSS, aSCx);
103     //
104     aSCx.Orientation(aSx.Orientation());
105     BB.Add(aSC, aSCx);
106   }
107   aSC.Free(bFree);
108 }
109 //=======================================================================
110 // function: ComputeVV
111 // purpose:
112 //=======================================================================
113   Standard_Integer NMTDS_Tools::ComputeVV(const TopoDS_Vertex& aV1,
114                                           const TopoDS_Vertex& aV2)
115 {
116   Standard_Real aTolV1, aTolV2, aTolSum, aTolSum2, aD2;
117   gp_Pnt aP1, aP2;
118   //
119   aTolV1=BRep_Tool::Tolerance(aV1);
120   aTolV2=BRep_Tool::Tolerance(aV2);
121   aTolSum=aTolV1+aTolV2;
122   aTolSum2=aTolSum*aTolSum;
123   //
124   aP1=BRep_Tool::Pnt(aV1);
125   aP2=BRep_Tool::Pnt(aV2);
126   //
127   aD2=aP1.SquareDistance(aP2);
128   if (aD2>aTolSum2) {
129     return -1;
130   }
131   return 0;
132 }
133 //=======================================================================
134 // function: HasBRep
135 // purpose:
136 //=======================================================================
137   Standard_Boolean NMTDS_Tools::HasBRep(const TopAbs_ShapeEnum aTi)
138 {
139   return (aTi==TopAbs_VERTEX || aTi==TopAbs_EDGE || aTi==TopAbs_FACE);
140 }
141 //=======================================================================
142 //function : TypeToInteger
143 //purpose  :
144 //=======================================================================
145   Standard_Integer NMTDS_Tools::TypeToInteger(const TopAbs_ShapeEnum aType1,
146                                             const TopAbs_ShapeEnum aType2)
147 {
148   Standard_Integer iRet, iT1, iT2, iX;
149   //
150   iRet=-1;
151   iT1=(Standard_Integer)aType1;
152   iT2=(Standard_Integer)aType2;
153   //
154   iX=iT2*10+iT1;
155   switch (iX) {
156     case 77:
157       iRet=5; // VV
158       break;
159     case 76:
160     case 67:
161       iRet=4; // VE
162       break;
163     case 74:
164     case 47:
165       iRet=2; // VF
166       break;
167     case 66:
168       iRet=3; // EE
169       break;
170     case 64:
171     case 46:
172       iRet=1; // EF
173       break;
174     case 44:
175       iRet=0; // FF
176       break;
177     default:
178       break;
179   }
180   return iRet;
181 }