08c00fb828ec4528608561a2259cf3cea9724f7e
[occt.git] / TopOpeBRepDS_samdom.cxx
1 // file:        TopOpeBRepBuild_Grid.cxx
2 // Created:     Thu Mar  7 10:49:33 1996
3 // Author:      Jean Yves LEBEY
4 //              <jyl@meteox>
5
6 #include <TopOpeBRepDS_samdom.hxx>
7
8 #include <TopExp.hxx>
9 #include <TopoDS.hxx>
10 #include <TopOpeBRepDS_define.hxx>
11
12 static TopTools_IndexedDataMapOfShapeListOfShape *Gps1 = NULL; 
13 static TopTools_IndexedDataMapOfShapeListOfShape *Gps2 = NULL; 
14 //modified by NIZNHY-PKV Sun Dec 15 17:57:12 2002 f
15 //static Handle(TopOpeBRepDS_HDataStructure)      Ghds;
16 static Handle(TopOpeBRepDS_HDataStructure)*      Ghds;
17 //modified by NIZNHY-PKV Sun Dec 15 17:57:18 2002 t
18
19 //modified by NIZNHY-PKV Sun Dec 15 17:41:43 2002 f
20 //=======================================================================
21 //function :FDSSDM_Close
22 //purpose  : 
23 //=======================================================================
24 Standard_EXPORT void FDSSDM_Close()
25 {
26   if (Gps1) {
27     delete Gps1;
28     Gps1=NULL;
29   }
30   //
31   if (Gps2) {
32     delete Gps2;
33     Gps2=NULL;
34   }
35 }
36 //modified by NIZNHY-PKV Sun Dec 15 17:56:02 2002 t
37 //=======================================================================
38 //function :FDSSDM_prepare
39 //purpose  : 
40 //=======================================================================
41 Standard_EXPORT void FDSSDM_prepare(const Handle(TopOpeBRepDS_HDataStructure)& HDS)
42 {
43   if (Gps1 == NULL) {
44     Gps1 = (TopTools_IndexedDataMapOfShapeListOfShape*) new TopTools_IndexedDataMapOfShapeListOfShape();
45   }
46   if (Gps2 == NULL) {
47     Gps2 = (TopTools_IndexedDataMapOfShapeListOfShape*) new TopTools_IndexedDataMapOfShapeListOfShape();
48   }
49   //modified by NIZNHY-PKV Sun Dec 15 17:58:28 2002 f
50   //Ghds = HDS;
51   void *anAddr=(void *) &HDS;
52   Ghds = (Handle(TopOpeBRepDS_HDataStructure)*) anAddr;
53   //modified by NIZNHY-PKV Sun Dec 15 17:58:31 2002 t
54   Gps1->Clear();
55   Gps2->Clear();
56   Standard_Integer i,n = HDS->NbShapes();
57   for (i=1; i<=n; i++) {
58     const TopoDS_Shape& s = HDS->Shape(i);
59     Standard_Boolean hsd = HDS->HasSameDomain(s); if (!hsd) continue;
60     TopTools_ListOfShape thelist, thelist1;
61     if (!Gps1->Contains(s)) Gps1->Add(s, thelist);
62     if (!Gps2->Contains(s)) Gps2->Add(s, thelist1);
63     TopTools_ListOfShape& LS1 = Gps1->ChangeFromKey(s);
64     TopTools_ListOfShape& LS2 = Gps2->ChangeFromKey(s);
65     FDSSDM_makes1s2(s,LS1,LS2);
66   }
67 } //prepare
68 //=======================================================================
69 //function :FDSSDM_makes1s2
70 //purpose  : 
71 //=======================================================================
72 Standard_EXPORT void FDSSDM_makes1s2(const TopoDS_Shape& S,
73                                      TopTools_ListOfShape& L1,
74                                      TopTools_ListOfShape& L2) 
75 //L1 = S1, complete lists L1,L2 with the shapes of the DS having same domain
76 {
77   //modified by NIZNHY-PKV Sun Dec 15 17:59:11 2002 f
78   //const Handle(TopOpeBRepDS_HDataStructure)& HDS = Ghds;
79   const Handle(TopOpeBRepDS_HDataStructure)& HDS = *Ghds;
80   //modified by NIZNHY-PKV Sun Dec 15 17:59:15 2002 t
81   L1.Append(S);
82
83   Standard_Integer i; 
84   Standard_Integer nl1 = L1.Extent(), nl2 = L2.Extent();
85   
86   while ( nl1 > 0 || nl2 > 0 )  {
87     
88     TopTools_ListIteratorOfListOfShape it1(L1);
89     for (i=1 ; i<=nl1; i++) {
90       const TopoDS_Shape& S1 = it1.Value();
91 //                HDS->Shape(S1);
92       TopTools_ListIteratorOfListOfShape itsd(HDS->SameDomain(S1));
93       for (; itsd.More(); itsd.Next() ) {
94         const TopoDS_Shape& S2 = itsd.Value();
95 //                  HDS->Shape(S2);
96         Standard_Boolean found = FDSSDM_contains(S2,L2);
97         if ( ! found ) {
98           L2.Prepend(S2);
99           nl2++;
100         }
101       }
102       it1.Next();
103     }
104     nl1 = 0;
105     
106     TopTools_ListIteratorOfListOfShape it2(L2);
107     for (i=1 ; i<=nl2; i++) {
108       const TopoDS_Shape& S2 = it2.Value();
109 //      HDS->Shape(S2);
110       TopTools_ListIteratorOfListOfShape itsd(HDS->SameDomain(S2));
111       for (; itsd.More(); itsd.Next() ) {
112         const TopoDS_Shape& S1 = itsd.Value();
113 //                  HDS->Shape(S1);
114         Standard_Boolean found = FDSSDM_contains(S1,L1);
115         if ( ! found ) {
116           L1.Prepend(S1);
117           nl1++;
118         }
119       }
120       it2.Next();
121     }
122
123     nl2 = 0;
124   }
125 } // makes1s2
126 //=======================================================================
127 //function :FDSSDM_s1s2makesordor
128 //purpose  : 
129 //=======================================================================
130 Standard_EXPORT void FDSSDM_s1s2makesordor(const TopTools_ListOfShape& LS1,
131                                            const TopTools_ListOfShape& LS2,
132                                            TopTools_ListOfShape& LSO,
133                                            TopTools_ListOfShape& LDO)
134 {
135   //modified by NIZNHY-PKV Sun Dec 15 17:59:37 2002 f
136   //const Handle(TopOpeBRepDS_HDataStructure)& HDS = Ghds;  
137   const Handle(TopOpeBRepDS_HDataStructure)& HDS = *Ghds;  
138   //modified by NIZNHY-PKV Sun Dec 15 17:59:43 2002 t
139   TopTools_ListIteratorOfListOfShape it(LS1); if (!it.More()) return;
140   const TopoDS_Shape& sref = it.Value();
141 #ifdef DEB
142 //  Standard_Integer  iref =
143 #endif
144               HDS->SameDomainReference(sref);
145   TopOpeBRepDS_Config oref = HDS->SameDomainOrientation(sref);
146   
147   for (it.Initialize(LS1); it.More(); it.Next() ) {
148     const TopoDS_Shape& s = it.Value();
149     TopOpeBRepDS_Config o = HDS->SameDomainOrientation(s);
150 //  HDS->Shape(s);
151     if      ( o == oref && !FDSSDM_contains(s,LSO) ) LSO.Append(s);
152     else if ( o != oref && !FDSSDM_contains(s,LDO) ) LDO.Append(s);
153   }
154   
155   for (it.Initialize(LS2); it.More(); it.Next() ) {
156     const TopoDS_Shape& s = it.Value();
157     TopOpeBRepDS_Config o = HDS->SameDomainOrientation(s);
158 //             HDS->Shape(s);
159     if      ( o == oref && !FDSSDM_contains(s,LSO) ) LSO.Append(s);
160     else if ( o != oref && !FDSSDM_contains(s,LDO) ) LDO.Append(s);
161   }
162 } // s1s2makesordor
163
164 Standard_EXPORT Standard_Boolean FDSSDM_hass1s2(const TopoDS_Shape& S)
165 {
166   Standard_Boolean b1 = Gps1->Contains(S);
167   Standard_Boolean b2 = Gps2->Contains(S);
168   Standard_Boolean b = (b1 && b2);
169   return b;
170 } // hass1s2
171
172 Standard_EXPORT void FDSSDM_s1s2(const TopoDS_Shape& S,TopTools_ListOfShape& LS1,TopTools_ListOfShape& LS2)
173 {
174   LS1.Clear(); LS2.Clear();
175   Standard_Boolean b = FDSSDM_hass1s2(S);
176   if (!b) {
177     FDSSDM_makes1s2(S,LS1,LS2);
178     return;
179   }
180   const TopTools_ListOfShape& L1 = Gps1->FindFromKey(S);
181   const TopTools_ListOfShape& L2 = Gps2->FindFromKey(S);
182   FDSSDM_copylist(L1,LS1);
183   FDSSDM_copylist(L2,LS2);
184 } // s1s2
185
186 Standard_EXPORT void FDSSDM_sordor(const TopoDS_Shape& S,TopTools_ListOfShape& LSO,TopTools_ListOfShape& LDO)
187 {
188   LSO.Clear(); LDO.Clear();
189   TopTools_ListOfShape LS1,LS2; 
190   FDSSDM_s1s2(S,LS1,LS2);
191   FDSSDM_s1s2makesordor(LS1,LS2,LSO,LDO);
192 } // sordor
193
194 Standard_EXPORT Standard_Boolean FDSSDM_contains(const TopoDS_Shape& S,const TopTools_ListOfShape& L)
195 // True if S IsSame a shape of list L.
196 {
197   for (TopTools_ListIteratorOfListOfShape it(L); it.More(); it.Next() ) {
198     const TopoDS_Shape& SL = it.Value();
199     Standard_Boolean issame = SL.IsSame(S);
200     if ( issame ) return Standard_True;
201   }
202   return Standard_False;
203 } // contains
204
205 Standard_EXPORT void FDSSDM_copylist(const TopTools_ListOfShape& Lin,const Standard_Integer I1,const Standard_Integer I2,TopTools_ListOfShape& Lou)
206 // copie des elements [i1..i2] de Lin dans Lou. 1er element de Lin = index 1 
207 {
208   TopTools_ListIteratorOfListOfShape it(Lin);
209   Standard_Integer nadd = 0;
210   for ( Standard_Integer i = 1; it.More(); it.Next(),i++ ) {
211     const TopoDS_Shape& EL = it.Value();
212     if ( i >= I1 && i <= I2 ) {
213       Lou.Append(EL);
214       nadd++;
215     }
216   }
217 } // copylist
218
219 Standard_EXPORT void FDSSDM_copylist(const TopTools_ListOfShape& Lin,TopTools_ListOfShape& Lou)
220 // copy de Lin dans Lou
221 {
222   const Standard_Integer I1 = 1;
223   const Standard_Integer I2 = Lin.Extent();
224   FDSSDM_copylist(Lin,I1,I2,Lou);
225 } // copylist