Salome HOME
Fix RemoveExtraEdges() to obtain valid shapes. Fix done by SKL.
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_PassKey.cxx
1 // File:        GEOMAlgo_PassKey.cxx
2 // Created:     
3 // Author:      Peter KURNEV
4 //              <pkv@irinox>
5
6
7 #include <GEOMAlgo_PassKey.ixx>
8
9 #include <stdio.h>
10 #include <string.h>
11
12 #include <TopTools_ListIteratorOfListOfShape.hxx>
13
14 #ifdef WNT
15 #pragma warning( disable : 4101) 
16 #endif
17
18 static 
19   void SortShell(const int n, int* a); 
20
21 //=======================================================================
22 //function :
23 //purpose  : 
24 //=======================================================================
25   GEOMAlgo_PassKey::GEOMAlgo_PassKey()
26 {
27   Clear();
28 }
29 //=======================================================================
30 //function :Clear
31 //purpose  : 
32 //=======================================================================
33   void GEOMAlgo_PassKey::Clear()
34 {
35   myNbIds=0;
36   myNbMax=8;
37   mySum=0;
38   myIds[0]=0;  myIds[1]=0;  myIds[2]=0;  myIds[3]=0;
39   myIds[4]=0;  myIds[5]=0;  myIds[6]=0;  myIds[7]=0;
40   myUpper=432123;//2147483647;
41 }
42 //=======================================================================
43 //function :Assign
44 //purpose  : 
45 //=======================================================================
46   GEOMAlgo_PassKey& GEOMAlgo_PassKey::Assign(const GEOMAlgo_PassKey& anOther)
47 {
48   myNbIds=anOther.myNbIds;
49   myNbMax=anOther.myNbMax;
50   mySum=anOther.mySum;
51   memcpy(myIds, anOther.myIds, sizeof(myIds));
52   return *this;
53 }
54 //=======================================================================
55 //function :SetIds
56 //purpose  : 
57 //=======================================================================
58   void GEOMAlgo_PassKey::SetIds(const TopoDS_Shape& aS1)
59                                
60 {
61   Standard_Integer anId1;
62   //
63   anId1=aS1.HashCode(myUpper);
64   //
65   myNbIds=1;
66   myIds[7]=anId1;
67   mySum=anId1;
68 }
69 //=======================================================================
70 //function :SetIds
71 //purpose  : 
72 //=======================================================================
73   void GEOMAlgo_PassKey::SetIds(const TopoDS_Shape& aS1,
74                                 const TopoDS_Shape& aS2)
75 {
76   Standard_Integer anId1, anId2;
77   //
78   anId1=aS1.HashCode(myUpper);
79   anId2=aS2.HashCode(myUpper);
80   //
81   myNbIds=2;
82   mySum=anId1+anId2;
83   if (anId1<anId2) {
84     myIds[6]=anId1;
85     myIds[7]=anId2;
86     return;
87   }
88   myIds[6]=anId2;
89   myIds[7]=anId1;
90 }
91 //=======================================================================
92 //function :SetIds
93 //purpose  : 
94 //=======================================================================
95   void GEOMAlgo_PassKey::SetIds(const TopoDS_Shape& aS1,
96                                 const TopoDS_Shape& aS2,
97                                 const TopoDS_Shape& aS3)
98 {
99   Standard_Integer anId1, anId2, anId3;
100   //
101   anId1=aS1.HashCode(myUpper);
102   anId2=aS2.HashCode(myUpper);
103   anId3=aS3.HashCode(myUpper);
104   //
105   myNbIds=3;
106   myIds[5]=anId1;
107   myIds[6]=anId2;
108   myIds[7]=anId3;
109   mySum=anId1+anId2+anId3;
110   Compute();
111 }
112 //=======================================================================
113 //function :SetIds
114 //purpose  : 
115 //=======================================================================
116   void GEOMAlgo_PassKey::SetIds(const TopoDS_Shape& aS1,
117                                 const TopoDS_Shape& aS2,
118                                 const TopoDS_Shape& aS3,
119                                 const TopoDS_Shape& aS4)
120 {
121   Standard_Integer anId1, anId2, anId3, anId4;
122   //
123   anId1=aS1.HashCode(myUpper);
124   anId2=aS2.HashCode(myUpper);
125   anId3=aS3.HashCode(myUpper);
126   anId4=aS4.HashCode(myUpper);
127   //
128   myNbIds=4;
129   myIds[4]=anId1;
130   myIds[5]=anId2;
131   myIds[6]=anId3;
132   myIds[7]=anId4;
133   mySum=anId1+anId2+anId3+anId4;
134   Compute();
135 }
136 //=======================================================================
137 //function :SetIds
138 //purpose  : 
139 //=======================================================================
140   void GEOMAlgo_PassKey::SetIds(const TopTools_ListOfShape& aLS)
141 {
142   Standard_Integer aNb, i, anId;
143   TopTools_ListIteratorOfListOfShape aIt;
144   //
145   aNb=aLS.Extent();
146   if (aNb<1 || aNb > myNbMax) {
147     return;
148   }
149   //
150   myNbIds=aNb;
151   mySum=0;
152   i=myNbMax-myNbIds;
153   aIt.Initialize(aLS);
154   for (; aIt.More(); aIt.Next(), ++i) {
155     const TopoDS_Shape& aS=aIt.Value();
156     anId=aS.HashCode(myUpper);
157     myIds[i]=anId;
158     mySum+=anId;
159   }
160   //
161   Compute();
162 }
163 //=======================================================================
164 //function :NbMax
165 //purpose  : 
166 //=======================================================================
167   Standard_Integer GEOMAlgo_PassKey::NbMax()const
168 {
169   return myNbMax;
170 }
171 //=======================================================================
172 //function :Compute
173 //purpose  : 
174 //=======================================================================
175   void GEOMAlgo_PassKey::Compute()
176 {
177   SortShell(myNbIds, myIds+myNbMax-myNbIds);
178 }
179 //=======================================================================
180 //function :IsEqual
181 //purpose  : 
182 //=======================================================================
183   Standard_Boolean GEOMAlgo_PassKey::IsEqual(const GEOMAlgo_PassKey& anOther) const
184 {
185   Standard_Integer iIsEqual;
186   Standard_Boolean bIsEqual;
187   //
188   iIsEqual=memcmp(myIds, anOther.myIds, sizeof(myIds));
189   bIsEqual=Standard_False;
190   if (!iIsEqual) {
191     bIsEqual=!bIsEqual;
192   }
193   return bIsEqual;
194 }
195 //=======================================================================
196 //function :Key
197 //purpose  : 
198 //=======================================================================
199   Standard_Address GEOMAlgo_PassKey::Key()const
200 {
201   return (Standard_Address)myIds;
202 }
203 //=======================================================================
204 //function : HashCode
205 //purpose  : 
206 //=======================================================================
207   Standard_Integer GEOMAlgo_PassKey::HashCode(const Standard_Integer Upper) const
208 {
209   return (mySum % Upper);
210 }
211 //=======================================================================
212 //function : Dump
213 //purpose  : 
214 //=======================================================================
215   void GEOMAlgo_PassKey::Dump()const
216 {
217   Standard_Integer i;
218   //
219   printf(" PassKey: {");
220   for (i=0; i<myNbMax; ++i) {
221     printf(" %d", myIds[i]);
222   }
223   printf(" }");
224 }
225 //=======================================================================
226 // function: SortShell
227 // purpose : 
228 //=======================================================================
229 void SortShell(const int n, int* a) 
230 {
231   int  x, nd, i, j, l, d=1;
232   //
233   while(d<=n) {
234     d*=2;
235   }
236   //
237   while (d) {
238     d=(d-1)/2;
239     //
240     nd=n-d;
241     for (i=0; i<nd; ++i) {
242       j=i;
243     m30:;
244       l=j+d;
245       if (a[l] < a[j]){
246         x=a[j];
247         a[j]=a[l];
248         a[l]=x;
249         j-=d;
250         if (j > -1) goto m30;
251       }//if (a[l] < a[j]){
252     }//for (i=0; i<nd; ++i) 
253   }//while (1)
254 }