Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/geom.git] / src / NMTDS / NMTDS_Tools.cxx
1 //  Copyright (C) 2007-2008  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.
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:        NMTDS_Tools.cxx
23 // Created:     Tue Feb 20 14:57:28 2007
24 // Author:      Peter KURNEV
25 //              <pkv@irinox>
26 //
27 #include <NMTDS_Tools.ixx>
28 #include <TopoDS_Vertex.hxx>
29 #include <gp_Pnt.hxx>
30 #include <BRep_Tool.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <TopTools_IndexedDataMapOfShapeShape.hxx>
33 #include <TopAbs_ShapeEnum.hxx>
34 #include <TopoDS_Iterator.hxx>
35 #include <BRep_Builder.hxx>
36
37 void CopySource(const TopoDS_Shape& aS, 
38                 TopTools_IndexedDataMapOfShapeShape& aMapSS, 
39                 TopoDS_Shape& aSC);
40
41 //=======================================================================
42 //function : CopyShape
43 //purpose  : 
44 //=======================================================================
45   void NMTDS_Tools::CopyShape(const TopoDS_Shape& aS, 
46                               TopoDS_Shape& aSC)
47 {
48   TopTools_IndexedDataMapOfShapeShape aMapSS;
49   //
50   CopySource(aS, aMapSS, aSC);
51 }
52 //=======================================================================
53 //function : CopyShape
54 //purpose  : 
55 //=======================================================================
56   void NMTDS_Tools::CopyShape(const TopoDS_Shape& aS, 
57                               TopoDS_Shape& aSC,
58                               TopTools_IndexedDataMapOfShapeShape& aMapSS)
59 {
60   CopySource(aS, aMapSS, aSC);
61 }
62 //=======================================================================
63 //function : CopySource
64 //purpose  : 
65 //=======================================================================
66 void CopySource(const TopoDS_Shape& aS, 
67                 TopTools_IndexedDataMapOfShapeShape& aMapSS, 
68                 TopoDS_Shape& aSC)
69 {
70   Standard_Boolean bFree;
71   TopAbs_ShapeEnum aT;
72   TopoDS_Iterator aIt;
73   BRep_Builder BB;
74   //
75   aT=aS.ShapeType();
76   //
77   if (aMapSS.Contains(aS)) {
78     aSC=aMapSS.ChangeFromKey(aS);
79     aSC.Orientation(aS.Orientation());
80     return;
81   }
82   else {
83     aSC=aS.EmptyCopied();
84     aMapSS.Add(aS, aSC);
85   }
86   //
87   bFree=aSC.Free();
88   aSC.Free(Standard_True);
89   aIt.Initialize(aS);
90   for (; aIt.More();  aIt.Next()) {
91     TopoDS_Shape aSCx;
92     //
93     const TopoDS_Shape& aSx=aIt.Value();
94     //
95     CopySource (aSx, aMapSS, aSCx);  
96     //
97     aSCx.Orientation(aSx.Orientation());
98     BB.Add(aSC, aSCx);
99   }
100   aSC.Free(bFree);
101 }
102 //=======================================================================
103 // function: ComputeVV
104 // purpose: 
105 //=======================================================================
106   Standard_Integer NMTDS_Tools::ComputeVV(const TopoDS_Vertex& aV1, 
107                                           const TopoDS_Vertex& aV2)
108 {
109   Standard_Real aTolV1, aTolV2, aTolSum, aTolSum2, aD2;
110   gp_Pnt aP1, aP2;
111   //
112   aTolV1=BRep_Tool::Tolerance(aV1);
113   aTolV2=BRep_Tool::Tolerance(aV2);
114   aTolSum=aTolV1+aTolV2;
115   aTolSum2=aTolSum*aTolSum;
116   //
117   aP1=BRep_Tool::Pnt(aV1);
118   aP2=BRep_Tool::Pnt(aV2);
119   //
120   aD2=aP1.SquareDistance(aP2);
121   if (aD2>aTolSum2) {
122     return -1;
123   }
124   return 0;
125 }
126 //=======================================================================
127 // function: HasBRep
128 // purpose: 
129 //=======================================================================
130   Standard_Boolean NMTDS_Tools::HasBRep(const TopAbs_ShapeEnum aTi)
131 {
132   return (aTi==TopAbs_VERTEX || aTi==TopAbs_EDGE || aTi==TopAbs_FACE);
133 }
134 //=======================================================================
135 //function : TypeToInteger
136 //purpose  : 
137 //=======================================================================
138   Standard_Integer NMTDS_Tools::TypeToInteger(const TopAbs_ShapeEnum aType1,
139                                             const TopAbs_ShapeEnum aType2)
140 {
141   Standard_Integer iRet, iT1, iT2, iX;
142   //
143   iRet=-1;
144   iT1=(Standard_Integer)aType1;
145   iT2=(Standard_Integer)aType2;
146   //
147   iX=iT2*10+iT1;
148   switch (iX) {
149     case 77:
150       iRet=5; // VV
151       break;
152     case 76:
153     case 67:
154       iRet=4; // VE
155       break;
156     case 74:
157     case 47:
158       iRet=2; // VF
159       break;
160     case 66:
161       iRet=3; // EE
162       break;
163     case 64:
164     case 46:
165       iRet=1; // EF
166       break;
167     case 44:
168       iRet=0; // FF
169       break;
170     default:
171       break;
172   }
173   return iRet; 
174 }