9f336eed45d807fc513a12493ba3a37201fab819
[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 <Precision.hxx>
22 #include <Standard_OutOfRange.hxx>
23 #include <Standard_Stream.hxx>
24 #include <Message_ProgressScope.hxx>
25 #include <TopLoc_Location.hxx>
26 #include <TopTools_LocationSet.hxx>
27
28 //=======================================================================
29 //function : TopTools_LocationSet
30 //purpose  : 
31 //=======================================================================
32 TopTools_LocationSet::TopTools_LocationSet() 
33 {
34 }
35
36
37 //=======================================================================
38 //function : Clear
39 //purpose  : 
40 //=======================================================================
41
42 void  TopTools_LocationSet::Clear()
43 {
44   myMap.Clear();
45 }
46
47
48 //=======================================================================
49 //function : Add
50 //purpose  : 
51 //=======================================================================
52
53 Standard_Integer  TopTools_LocationSet::Add(const TopLoc_Location& L)
54 {
55   if (L.IsIdentity()) return 0;
56   Standard_Integer n = myMap.FindIndex(L);
57   if (n > 0) return n;
58   TopLoc_Location N = L;
59   do {
60     myMap.Add(N.FirstDatum());
61     N = N.NextLocation();
62   } while (!N.IsIdentity());
63   return myMap.Add(L);
64 }
65
66
67 //=======================================================================
68 //function : Location
69 //purpose  : 
70 //=======================================================================
71
72 const TopLoc_Location&  TopTools_LocationSet::Location
73   (const Standard_Integer I)const 
74 {
75   static TopLoc_Location identity;
76   if (I <= 0 || I > myMap.Extent()) return identity;
77   return myMap(I);
78 }
79
80
81 //=======================================================================
82 //function : Index
83 //purpose  : 
84 //=======================================================================
85
86 Standard_Integer  TopTools_LocationSet::Index(const TopLoc_Location& L) const
87 {
88   if (L.IsIdentity()) return 0;
89   return myMap.FindIndex(L);
90 }
91
92 //=======================================================================
93 //function : Write
94 //purpose  : 
95 //=======================================================================
96
97 static void WriteTrsf(const gp_Trsf& T,
98                       Standard_OStream& OS, 
99                       const Standard_Boolean compact)
100 {
101   gp_XYZ V = T.TranslationPart();
102   gp_Mat M = T.VectorialPart();
103
104   if (!compact) OS << "( ";
105   OS << std::setw(15) << M(1,1) << " ";
106   OS << std::setw(15) << M(1,2) << " ";
107   OS << std::setw(15) << M(1,3) << " ";
108   OS << std::setw(15) << V.Coord(1) << " ";
109   if (!compact) OS << " )";
110   OS << "\n";
111   if (!compact) OS << "( ";
112   OS << std::setw(15) << M(2,1) << " ";
113   OS << std::setw(15) << M(2,2) << " ";
114   OS << std::setw(15) << M(2,3) << " ";
115   OS << std::setw(15) << V.Coord(2) << " ";
116   if (!compact) OS << " )";
117   OS << "\n";
118   if (!compact) OS << "( ";
119   OS << std::setw(15) << M(3,1) << " ";
120   OS << std::setw(15) << M(3,2) << " ";
121   OS << std::setw(15) << M(3,3) << " ";
122   OS << std::setw(15) << V.Coord(3) << " ";
123   if (!compact) OS << " )";
124   OS << "\n";
125 }
126
127 //=======================================================================
128 //function : Dump
129 //purpose  : 
130 //=======================================================================
131
132 void  TopTools_LocationSet::Dump(Standard_OStream& OS) const 
133 {
134   Standard_Integer i, nbLoc = myMap.Extent();
135
136   OS << "\n\n";
137   OS << "\n -------";
138   OS << "\n Dump of "<< nbLoc << " Locations";
139   OS << "\n -------\n\n";
140   
141   for (i = 1; i <= nbLoc; i++) {
142     TopLoc_Location L = myMap(i);
143     OS << std::setw(5) << i << " : \n";
144     
145     TopLoc_Location L2 = L.NextLocation();
146     Standard_Boolean simple = L2.IsIdentity();
147     Standard_Integer p = L.FirstPower();
148     TopLoc_Location L1 = L.FirstDatum();
149     Standard_Boolean elementary = (simple && p == 1);
150     if (elementary) {
151       OS << "Elementary location\n";
152     }
153     else {
154       OS << "Complex : L"<<myMap.FindIndex(L1);
155       if (p != 1) OS <<"^"<<p;
156       while (!L2.IsIdentity()) {
157         L1 = L2.FirstDatum();
158         p = L2.FirstPower();
159         L2 = L2.NextLocation();
160         OS << " * L" << myMap.FindIndex(L1);
161         if (p != 1) OS << "^"<<p;
162       }
163       OS <<"\n";
164     }
165     WriteTrsf(L.Transformation(),OS,Standard_False);
166   }
167
168
169 //=======================================================================
170 //function : Write
171 //purpose  : 
172 //=======================================================================
173
174 void  TopTools_LocationSet::Write(Standard_OStream& OS, const Message_ProgressRange& theProgress) const
175 {
176   
177   std::streamsize prec = OS.precision(15);
178
179   Standard_Integer i, nbLoc = myMap.Extent();
180   OS << "Locations " << nbLoc << "\n";
181   
182   //OCC19559
183   Message_ProgressScope PS(theProgress, "Locations", nbLoc);
184   for (i = 1; i <= nbLoc && PS.More(); i++, PS.Next()) {
185     TopLoc_Location L = myMap(i);
186
187     
188     TopLoc_Location L2 = L.NextLocation();
189     Standard_Boolean simple = L2.IsIdentity();
190     Standard_Integer p = L.FirstPower();
191     TopLoc_Location L1 = L.FirstDatum();
192     Standard_Boolean elementary = (simple && p == 1);
193     if (elementary) {
194       OS << "1\n";
195       WriteTrsf(L.Transformation(),OS,Standard_True);
196     }
197     else {
198       OS << "2 ";
199       OS << " "<<myMap.FindIndex(L1) << " "<<p;
200       while (!L2.IsIdentity()) {
201         L1 = L2.FirstDatum();
202         p  = L2.FirstPower();
203         L2 = L2.NextLocation();
204         OS << " "<<myMap.FindIndex(L1) << " "<<p;
205       }
206       OS << " 0\n";
207     }
208   }
209   OS.precision(prec);
210 }
211
212 //=======================================================================
213 //function : Read
214 //purpose  : 
215 //=======================================================================
216
217 static void ReadTrsf(gp_Trsf& T,
218                      Standard_IStream& IS)
219 {
220   Standard_Real V1[3],V2[3],V3[3];
221   Standard_Real V[3];
222   
223   GeomTools::GetReal(IS, V1[0]);
224   GeomTools::GetReal(IS, V1[1]);
225   GeomTools::GetReal(IS, V1[2]);
226   GeomTools::GetReal(IS, V[0]);
227
228   GeomTools::GetReal(IS, V2[0]);
229   GeomTools::GetReal(IS, V2[1]);
230   GeomTools::GetReal(IS, V2[2]);
231   GeomTools::GetReal(IS, V[1]);
232
233   GeomTools::GetReal(IS, V3[0]);
234   GeomTools::GetReal(IS, V3[1]);
235   GeomTools::GetReal(IS, V3[2]);
236   GeomTools::GetReal(IS, V[2]);
237   
238   T.SetValues(V1[0],V1[1],V1[2],V[0],
239               V2[0],V2[1],V2[2],V[1],
240               V3[0],V3[1],V3[2],V[2]);
241   return;
242 }
243 //=======================================================================
244 //function : Read
245 //purpose  : 
246 //=======================================================================
247
248 void  TopTools_LocationSet::Read(Standard_IStream& IS, const Message_ProgressRange& theProgress)
249 {
250   myMap.Clear();
251
252   char buffer[255];
253   Standard_Integer l1,p;
254
255   IS >> buffer;
256   if (strcmp(buffer,"Locations")) {
257     std::cout << "Not a location table "<<std::endl;
258     return;
259   }
260
261   Standard_Integer i, nbLoc;
262   IS >> nbLoc;
263   
264   TopLoc_Location L;
265   gp_Trsf T;
266     
267   //OCC19559
268   Message_ProgressScope PS(theProgress, "Locations", nbLoc);
269   for (i = 1; i <= nbLoc&& PS.More(); i++, PS.Next()) {
270     Standard_Integer typLoc;
271     IS >> typLoc;
272     
273     if (typLoc == 1) {
274       ReadTrsf(T,IS);
275       L = T;
276     }
277
278     else if (typLoc == 2) {
279       L = TopLoc_Location();
280       IS >> l1;
281       while (l1 != 0) { 
282         IS >> p;
283         TopLoc_Location L1 = myMap(l1);
284         L = L1.Powered(p) *L;
285         IS >> l1;
286       }
287     }
288     
289     if (!L.IsIdentity()) myMap.Add(L);
290   }
291 }