0030895: Coding Rules - specify std namespace explicitly for std::cout and streams
[occt.git] / src / TopTools / TopTools_LocationSet.cxx
1 // Created on: 1993-07-16
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <GeomTools.hxx>
19 #include <gp_Ax3.hxx>
20 #include <gp_Vec.hxx>
21 #include <Message_ProgressIndicator.hxx>
22 #include <Message_ProgressSentry.hxx>
23 #include <Precision.hxx>
24 #include <Standard_OutOfRange.hxx>
25 #include <Standard_Stream.hxx>
26 #include <TopLoc_Location.hxx>
27 #include <TopTools_LocationSet.hxx>
28
29 //=======================================================================
30 //function : TopTools_LocationSet
31 //purpose  : 
32 //=======================================================================
33 TopTools_LocationSet::TopTools_LocationSet() 
34 {
35 }
36
37
38 //=======================================================================
39 //function : Clear
40 //purpose  : 
41 //=======================================================================
42
43 void  TopTools_LocationSet::Clear()
44 {
45   myMap.Clear();
46 }
47
48
49 //=======================================================================
50 //function : Add
51 //purpose  : 
52 //=======================================================================
53
54 Standard_Integer  TopTools_LocationSet::Add(const TopLoc_Location& L)
55 {
56   if (L.IsIdentity()) return 0;
57   Standard_Integer n = myMap.FindIndex(L);
58   if (n > 0) return n;
59   TopLoc_Location N = L;
60   do {
61     myMap.Add(N.FirstDatum());
62     N = N.NextLocation();
63   } while (!N.IsIdentity());
64   return myMap.Add(L);
65 }
66
67
68 //=======================================================================
69 //function : Location
70 //purpose  : 
71 //=======================================================================
72
73 const TopLoc_Location&  TopTools_LocationSet::Location
74   (const Standard_Integer I)const 
75 {
76   static TopLoc_Location identity;
77   if (I <= 0 || I > myMap.Extent()) return identity;
78   return myMap(I);
79 }
80
81
82 //=======================================================================
83 //function : Index
84 //purpose  : 
85 //=======================================================================
86
87 Standard_Integer  TopTools_LocationSet::Index(const TopLoc_Location& L) const
88 {
89   if (L.IsIdentity()) return 0;
90   return myMap.FindIndex(L);
91 }
92
93 //=======================================================================
94 //function : Write
95 //purpose  : 
96 //=======================================================================
97
98 static void WriteTrsf(const gp_Trsf& T,
99                       Standard_OStream& OS, 
100                       const Standard_Boolean compact)
101 {
102   gp_XYZ V = T.TranslationPart();
103   gp_Mat M = T.VectorialPart();
104
105   if (!compact) OS << "( ";
106   OS << std::setw(15) << M(1,1) << " ";
107   OS << std::setw(15) << M(1,2) << " ";
108   OS << std::setw(15) << M(1,3) << " ";
109   OS << std::setw(15) << V.Coord(1) << " ";
110   if (!compact) OS << " )";
111   OS << "\n";
112   if (!compact) OS << "( ";
113   OS << std::setw(15) << M(2,1) << " ";
114   OS << std::setw(15) << M(2,2) << " ";
115   OS << std::setw(15) << M(2,3) << " ";
116   OS << std::setw(15) << V.Coord(2) << " ";
117   if (!compact) OS << " )";
118   OS << "\n";
119   if (!compact) OS << "( ";
120   OS << std::setw(15) << M(3,1) << " ";
121   OS << std::setw(15) << M(3,2) << " ";
122   OS << std::setw(15) << M(3,3) << " ";
123   OS << std::setw(15) << V.Coord(3) << " ";
124   if (!compact) OS << " )";
125   OS << "\n";
126 }
127
128 //=======================================================================
129 //function : Dump
130 //purpose  : 
131 //=======================================================================
132
133 void  TopTools_LocationSet::Dump(Standard_OStream& OS) const 
134 {
135   Standard_Integer i, nbLoc = myMap.Extent();
136
137   OS << "\n\n";
138   OS << "\n -------";
139   OS << "\n Dump of "<< nbLoc << " Locations";
140   OS << "\n -------\n\n";
141   
142   for (i = 1; i <= nbLoc; i++) {
143     TopLoc_Location L = myMap(i);
144     OS << std::setw(5) << i << " : \n";
145     
146     TopLoc_Location L2 = L.NextLocation();
147     Standard_Boolean simple = L2.IsIdentity();
148     Standard_Integer p = L.FirstPower();
149     TopLoc_Location L1 = L.FirstDatum();
150     Standard_Boolean elementary = (simple && p == 1);
151     if (elementary) {
152       OS << "Elementary location\n";
153     }
154     else {
155       OS << "Complex : L"<<myMap.FindIndex(L1);
156       if (p != 1) OS <<"^"<<p;
157       while (!L2.IsIdentity()) {
158         L1 = L2.FirstDatum();
159         p = L2.FirstPower();
160         L2 = L2.NextLocation();
161         OS << " * L" << myMap.FindIndex(L1);
162         if (p != 1) OS << "^"<<p;
163       }
164       OS <<"\n";
165     }
166     WriteTrsf(L.Transformation(),OS,Standard_False);
167   }
168
169
170 //=======================================================================
171 //function : Write
172 //purpose  : 
173 //=======================================================================
174
175 void  TopTools_LocationSet::Write(Standard_OStream& OS) const 
176 {
177   
178   std::streamsize prec = OS.precision(15);
179
180   Standard_Integer i, nbLoc = myMap.Extent();
181   OS << "Locations " << nbLoc << "\n";
182   
183   //OCC19559
184   Message_ProgressSentry PS(GetProgress(), "Locations", 0, nbLoc, 1);
185   for (i = 1; i <= nbLoc && PS.More(); i++, PS.Next()) {
186     TopLoc_Location L = myMap(i);
187
188     
189     TopLoc_Location L2 = L.NextLocation();
190     Standard_Boolean simple = L2.IsIdentity();
191     Standard_Integer p = L.FirstPower();
192     TopLoc_Location L1 = L.FirstDatum();
193     Standard_Boolean elementary = (simple && p == 1);
194     if (elementary) {
195       OS << "1\n";
196       WriteTrsf(L.Transformation(),OS,Standard_True);
197     }
198     else {
199       OS << "2 ";
200       OS << " "<<myMap.FindIndex(L1) << " "<<p;
201       while (!L2.IsIdentity()) {
202         L1 = L2.FirstDatum();
203         p  = L2.FirstPower();
204         L2 = L2.NextLocation();
205         OS << " "<<myMap.FindIndex(L1) << " "<<p;
206       }
207       OS << " 0\n";
208     }
209   }
210   OS.precision(prec);
211 }
212
213 //=======================================================================
214 //function : Read
215 //purpose  : 
216 //=======================================================================
217
218 static void ReadTrsf(gp_Trsf& T,
219                      Standard_IStream& IS)
220 {
221   Standard_Real V1[3],V2[3],V3[3];
222   Standard_Real V[3];
223   
224   GeomTools::GetReal(IS, V1[0]);
225   GeomTools::GetReal(IS, V1[1]);
226   GeomTools::GetReal(IS, V1[2]);
227   GeomTools::GetReal(IS, V[0]);
228
229   GeomTools::GetReal(IS, V2[0]);
230   GeomTools::GetReal(IS, V2[1]);
231   GeomTools::GetReal(IS, V2[2]);
232   GeomTools::GetReal(IS, V[1]);
233
234   GeomTools::GetReal(IS, V3[0]);
235   GeomTools::GetReal(IS, V3[1]);
236   GeomTools::GetReal(IS, V3[2]);
237   GeomTools::GetReal(IS, V[2]);
238   
239   T.SetValues(V1[0],V1[1],V1[2],V[0],
240               V2[0],V2[1],V2[2],V[1],
241               V3[0],V3[1],V3[2],V[2]);
242   return;
243 }
244 //=======================================================================
245 //function : Read
246 //purpose  : 
247 //=======================================================================
248
249 void  TopTools_LocationSet::Read(Standard_IStream& IS)
250 {
251   myMap.Clear();
252
253   char buffer[255];
254   Standard_Integer l1,p;
255
256   IS >> buffer;
257   if (strcmp(buffer,"Locations")) {
258     std::cout << "Not a location table "<<std::endl;
259     return;
260   }
261
262   Standard_Integer i, nbLoc;
263   IS >> nbLoc;
264   
265   TopLoc_Location L;
266   gp_Trsf T;
267     
268   //OCC19559
269   Message_ProgressSentry PS(GetProgress(), "Locations", 0, nbLoc, 1);
270   for (i = 1; i <= nbLoc&& PS.More(); i++, PS.Next()) {
271     Standard_Integer typLoc;
272     IS >> typLoc;
273     
274     if (typLoc == 1) {
275       ReadTrsf(T,IS);
276       L = T;
277     }
278
279     else if (typLoc == 2) {
280       L = TopLoc_Location();
281       IS >> l1;
282       while (l1 != 0) { 
283         IS >> p;
284         TopLoc_Location L1 = myMap(l1);
285         L = L1.Powered(p) *L;
286         IS >> l1;
287       }
288     }
289     
290     if (!L.IsIdentity()) myMap.Add(L);
291   }
292 }
293
294 //=======================================================================
295 //function : GetProgress
296 //purpose  : 
297 //=======================================================================
298
299 Handle(Message_ProgressIndicator) TopTools_LocationSet::GetProgress() const
300 {
301   return myProgress;
302 }
303
304 //=======================================================================
305 //function : SetProgress
306 //purpose  : 
307 //=======================================================================
308
309 void TopTools_LocationSet::SetProgress(const Handle(Message_ProgressIndicator)& PR)
310 {
311   myProgress = PR;
312 }
313
314