0023024: Update headers of OCCT files
[occt.git] / src / BRepAlgo / BRepAlgo_Image.cxx
1 // Created on: 1995-10-26
2 // Created by: Yves FRICAUD
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22
23 #include <BRepAlgo_Image.ixx>
24
25 #include <Standard_ConstructionError.hxx>
26 #include <TopTools_ListIteratorOfListOfShape.hxx>
27 #include <TopTools_MapOfShape.hxx>
28 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
29 #include <TopExp_Explorer.hxx>
30
31 //=======================================================================
32 //function : BRepAlgo_Image
33 //purpose  : 
34 //=======================================================================
35
36 BRepAlgo_Image::BRepAlgo_Image()
37 {
38 }
39
40
41 //=======================================================================
42 //function : SetRoot
43 //purpose  : 
44 //=======================================================================
45
46 void BRepAlgo_Image::SetRoot(const TopoDS_Shape& S)
47 {
48   roots.Append(S);
49 }
50
51 //=======================================================================
52 //function : Bind
53 //purpose  : 
54 //=======================================================================
55
56 void BRepAlgo_Image::Bind(const TopoDS_Shape& OldS, 
57                             const TopoDS_Shape& NewS)
58 {
59   if (down.IsBound(OldS)) {
60     Standard_ConstructionError::Raise(" BRepAlgo_Image::Bind");
61     return;
62   }
63   TopTools_ListOfShape L;
64   down.Bind(OldS,L);
65   down(OldS).Append(NewS);
66   up.Bind(NewS,OldS);
67 }
68
69
70 //=======================================================================
71 //function : Bind
72 //purpose  : 
73 //=======================================================================
74
75 void BRepAlgo_Image::Bind(const TopoDS_Shape& OldS, 
76                             const TopTools_ListOfShape& L)
77 {  
78   if (HasImage(OldS)) {
79     Standard_ConstructionError::Raise(" BRepAlgo_Image::Bind");
80     return; 
81   }
82   TopTools_ListIteratorOfListOfShape it(L);
83   for (; it.More(); it.Next()) {
84     if (!HasImage(OldS))
85       Bind(OldS, it.Value());
86     else 
87       Add (OldS, it.Value());
88   }
89 }
90
91 //=======================================================================
92 //function : Clear
93 //purpose  : 
94 //=======================================================================
95
96 void BRepAlgo_Image::Clear()
97 {
98   roots.Clear();
99   up   .Clear();
100   down .Clear();
101 }
102
103 //=======================================================================
104 //function : Add
105 //purpose  : 
106 //=======================================================================
107
108 void BRepAlgo_Image::Add(const TopoDS_Shape& OldS, const TopoDS_Shape& NewS)
109 {
110   if (!HasImage(OldS)) {    
111     Standard_ConstructionError::Raise(" BRepAlgo_Image::Add");
112   }
113   down(OldS).Append(NewS);
114   up.Bind(NewS,OldS);
115 }
116
117 //=======================================================================
118 //function : Add
119 //purpose  : 
120 //=======================================================================
121
122 void BRepAlgo_Image::Add(const TopoDS_Shape&         OldS, 
123                            const TopTools_ListOfShape& L)
124 {
125   TopTools_ListIteratorOfListOfShape it(L);
126   for (; it.More(); it.Next()) { 
127     Add(OldS,it.Value());
128   }
129 }
130
131
132 //=======================================================================
133 //function : Remove
134 //purpose  : 
135 //=======================================================================
136
137 void BRepAlgo_Image::Remove(const TopoDS_Shape& S)
138 {
139   if (!up.IsBound(S)) {
140     Standard_ConstructionError::Raise(" BRepAlgo_Image::Remove");
141   }
142   const TopoDS_Shape& OldS = up(S);
143   TopTools_ListOfShape& L = down(OldS);
144   TopTools_ListIteratorOfListOfShape it(L);
145   while (it.More()) {
146     if (it.Value().IsSame(S)) {
147       L.Remove(it);
148       break;
149     }
150     it.Next();
151   }
152   if (L.IsEmpty()) down.UnBind(OldS);
153   up.UnBind(S);
154 }
155
156
157 //=======================================================================
158 //function : TopTools_ListOfShape&
159 //purpose  : 
160 //=======================================================================
161
162 const TopTools_ListOfShape& BRepAlgo_Image::Roots() const 
163 {
164   return roots;
165 }
166
167 //=======================================================================
168 //function : IsImage
169 //purpose  : 
170 //=======================================================================
171
172 Standard_Boolean  BRepAlgo_Image::IsImage(const TopoDS_Shape& S) const 
173 {
174   return up.IsBound(S);
175 }
176
177 //=======================================================================
178 //function : ImageFrom
179 //purpose  : 
180 //=======================================================================
181
182 const TopoDS_Shape&  BRepAlgo_Image::ImageFrom(const TopoDS_Shape& S) const 
183 {
184   if (!up.IsBound(S)) {
185     Standard_ConstructionError::Raise(" BRepAlgo_Image::ImageFrom");
186   }  
187   return up(S);
188 }
189
190 //=======================================================================
191 //function : FirstImageFrom
192 //purpose  : 
193 //=======================================================================
194
195 const TopoDS_Shape&  BRepAlgo_Image::Root(const TopoDS_Shape& S) 
196 const 
197 {
198   if (!up.IsBound(S)) {
199     Standard_ConstructionError::Raise(" BRepAlgo_Image::FirstImageFrom");
200   }
201
202   TopoDS_Shape S1 = up(S);
203   TopoDS_Shape S2 = S;
204
205   if ( S1.IsSame(S2)) 
206     return up(S);
207
208   while ( up.IsBound(S1)) {
209     S2 = S1;
210     S1 = up(S1);
211     if ( S1.IsSame(S2)) break;
212   }
213   return up(S2);
214 }
215
216 //=======================================================================
217 //function : HasImage
218 //purpose  : 
219 //=======================================================================
220
221 Standard_Boolean  BRepAlgo_Image::HasImage(const TopoDS_Shape& S) const 
222 {
223   return down.IsBound(S);
224 }
225
226 //=======================================================================
227 //function : TopTools_ListOfShape&
228 //purpose  : 
229 //=======================================================================
230
231 const TopTools_ListOfShape& BRepAlgo_Image::Image(const TopoDS_Shape& S) const 
232 {
233   if (!HasImage(S)) { 
234     static TopTools_ListOfShape L;
235     L.Append(S);
236     return L;
237   }
238   return down(S);
239 }
240
241
242 //=======================================================================
243 //function : TopTools_ListOfShape&
244 //purpose  : 
245 //=======================================================================
246 void BRepAlgo_Image::LastImage(const TopoDS_Shape&  S,
247                                   TopTools_ListOfShape& L) const 
248 {
249   if (!down.IsBound(S)) {
250     L.Append(S);
251   }
252   else {
253     TopTools_ListIteratorOfListOfShape it(down(S));
254     for (; it.More(); it.Next()) {
255       if (it.Value().IsSame(S)) {
256         L.Append(S);
257       }
258       else {
259         LastImage(it.Value(),L);
260       }
261     }
262   }
263 }
264
265
266 //=======================================================================
267 //function : Compact
268 //purpose  : 
269 //=======================================================================
270
271 void BRepAlgo_Image::Compact()
272 {
273   TopTools_DataMapOfShapeListOfShape M;
274   TopTools_ListIteratorOfListOfShape it(roots);
275   for (; it.More(); it.Next()) {
276     const TopoDS_Shape&   S = it.Value();
277     TopTools_ListOfShape  LI;
278     if (HasImage(S)) LastImage(S,LI);
279     M.Bind   (S,LI);
280   }
281   up.Clear();
282   down.Clear();
283   for (it.Initialize(roots); it.More(); it.Next()) {
284     if (M.IsBound(it.Value())) {
285       Bind(it.Value(), M(it.Value()));
286     }
287   }
288 }
289
290 //=======================================================================
291 //function : Filter
292 //purpose  : 
293 //=======================================================================
294
295 void BRepAlgo_Image::Filter(const TopoDS_Shape&     S,
296                               const TopAbs_ShapeEnum  T)
297
298 {
299   TopExp_Explorer      exp(S,T) ;
300   TopTools_MapOfShape  M;
301   for (; exp.More(); exp.Next()) {M.Add(exp.Current());}
302   Standard_Boolean Change = Standard_True;
303   while (Change) {
304     Change = Standard_False;
305     TopTools_DataMapIteratorOfDataMapOfShapeShape mit(up);
306     for (; mit.More(); mit.Next()) {
307       const TopoDS_Shape& S = mit.Key();
308       if (S.ShapeType() == T && !M.Contains(S)) {
309         Remove(S);
310         Change = Standard_True;
311         break;
312       }
313     }
314   }
315   
316 }
317