0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / BinTools / BinTools_LocationSet.cxx
1 // Created on: 2004-06-15
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2004-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <BinTools.hxx>
18 #include <BinTools_LocationSet.hxx>
19 #include <gp_Ax3.hxx>
20 #include <gp_Vec.hxx>
21 #include <Precision.hxx>
22 #include <Standard_ErrorHandler.hxx>
23 #include <Standard_OutOfRange.hxx>
24 #include <TopLoc_Location.hxx>
25
26 //=======================================================================
27 //function : operator >> (gp_Trsf& T)
28 //purpose  : 
29 //=======================================================================
30 static Standard_IStream& operator >>(Standard_IStream& IS, gp_Trsf& T)
31 {
32   Standard_Real V1[3],V2[3],V3[3];
33   Standard_Real V[3];
34   
35   BinTools::GetReal(IS, V1[0]);
36   BinTools::GetReal(IS, V1[1]);
37   BinTools::GetReal(IS, V1[2]);
38   BinTools::GetReal(IS,  V[0]);
39
40   BinTools::GetReal(IS, V2[0]);
41   BinTools::GetReal(IS, V2[1]);
42   BinTools::GetReal(IS, V2[2]);
43   BinTools::GetReal(IS,  V[1]);
44
45   BinTools::GetReal(IS, V3[0]);
46   BinTools::GetReal(IS, V3[1]);
47   BinTools::GetReal(IS, V3[2]);
48   BinTools::GetReal(IS,  V[2]);
49
50   T.SetValues(V1[0],V1[1],V1[2],V[0],
51               V2[0],V2[1],V2[2],V[1],
52               V3[0],V3[1],V3[2],V[2]);
53   return IS;
54 }
55
56 //=======================================================================
57 //function : operator << (gp_Trsf& T)
58 //purpose  : 
59 //=======================================================================
60 static Standard_OStream& operator <<(Standard_OStream& OS,const gp_Trsf& T)
61 {
62   gp_XYZ V = T.TranslationPart();
63   gp_Mat M = T.VectorialPart();
64
65   BinTools::PutReal(OS, M(1,1));
66   BinTools::PutReal(OS, M(1,2));
67   BinTools::PutReal(OS, M(1,3));
68   BinTools::PutReal(OS,V.Coord(1)); 
69
70   BinTools::PutReal(OS, M(2,1));
71   BinTools::PutReal(OS, M(2,2));
72   BinTools::PutReal(OS, M(2,3));
73   BinTools::PutReal(OS,V.Coord(2));  
74
75   BinTools::PutReal(OS, M(3,1));
76   BinTools::PutReal(OS, M(3,2));
77   BinTools::PutReal(OS, M(3,3));
78   BinTools::PutReal(OS,V.Coord(3));  
79
80   return OS;
81 }
82
83 //=======================================================================
84 //function : BinTools_LocationSet
85 //purpose  : 
86 //=======================================================================
87
88 BinTools_LocationSet::BinTools_LocationSet() 
89 {
90 }
91
92
93 //=======================================================================
94 //function : Clear
95 //purpose  : 
96 //=======================================================================
97
98 void  BinTools_LocationSet::Clear()
99 {
100   myMap.Clear();
101 }
102
103
104 //=======================================================================
105 //function : Add
106 //purpose  : 
107 //=======================================================================
108
109 Standard_Integer  BinTools_LocationSet::Add(const TopLoc_Location& L)
110 {
111   if (L.IsIdentity()) return 0;
112   Standard_Integer n = myMap.FindIndex(L);
113   if (n > 0) return n;
114   TopLoc_Location N = L;
115   do {
116     myMap.Add(N.FirstDatum());
117     N = N.NextLocation();
118   } while (!N.IsIdentity());
119   return myMap.Add(L);
120 }
121
122
123 //=======================================================================
124 //function : Location
125 //purpose  : 
126 //=======================================================================
127
128 const TopLoc_Location&  BinTools_LocationSet::Location
129   (const Standard_Integer I)const 
130 {
131   static TopLoc_Location identity;
132   if (I == 0) return identity;
133   else return myMap(I);
134 }
135
136
137 //=======================================================================
138 //function : Index
139 //purpose  : 
140 //=======================================================================
141
142 Standard_Integer  BinTools_LocationSet::Index(const TopLoc_Location& L) const
143 {
144   if (L.IsIdentity()) return 0;
145   return myMap.FindIndex(L);
146 }
147
148 //=======================================================================
149 //function : NbLocations
150 //purpose  : 
151 //=======================================================================
152
153 Standard_Integer  BinTools_LocationSet::NbLocations() const
154 {
155   return myMap.Extent();
156 }
157
158 //=======================================================================
159 //function : Write locations
160 //purpose  : 
161 //=======================================================================
162
163 void  BinTools_LocationSet::Write(Standard_OStream& OS) const 
164 {
165   
166   Standard_Integer i, nbLoc = myMap.Extent();
167   OS << "Locations "<< nbLoc << "\n";
168   try {
169     OCC_CATCH_SIGNALS
170     for (i = 1; i <= nbLoc; i++) {
171       TopLoc_Location L = myMap(i);
172       
173       TopLoc_Location L2 = L.NextLocation();
174       Standard_Boolean simple = L2.IsIdentity();
175       Standard_Integer p = L.FirstPower();
176       TopLoc_Location L1 = L.FirstDatum();
177       Standard_Boolean elementary = (simple && p == 1);
178       if (elementary) {
179         
180         OS.put((Standard_Byte)1); // 1
181         OS << L.Transformation();
182       }
183       else {
184         OS.put((Standard_Byte)2); // 2
185         BinTools::PutInteger(OS, myMap.FindIndex(L1));
186         BinTools::PutInteger(OS, p);
187         while (!L2.IsIdentity()) {
188           L1 = L2.FirstDatum();
189           p  = L2.FirstPower();
190           L2 = L2.NextLocation();
191           BinTools::PutInteger(OS,  myMap.FindIndex(L1));
192           BinTools::PutInteger(OS, p);
193         }
194         
195         BinTools::PutInteger(OS, 0);
196       }
197     }
198   }
199   catch(Standard_Failure const& anException) {
200     Standard_SStream aMsg;
201     aMsg << "EXCEPTION in BinTools_LocatioSet::Write(..)" << std::endl;
202     aMsg << anException << std::endl;
203     throw Standard_Failure(aMsg.str().c_str());
204   }
205 }
206
207 //=======================================================================
208 //function : Read
209 //purpose  : 
210 //=======================================================================
211 void  BinTools_LocationSet::Read(Standard_IStream& IS)
212 {
213
214   myMap.Clear();
215   char buffer[255];
216   Standard_Integer l1,p;
217
218   IS >> buffer;
219   if (IS.fail() || (strcmp(buffer,"Locations"))) {
220     Standard_SStream aMsg;
221     aMsg << "BinTools_LocationSet::Read: Not a location table"<<std::endl;
222     throw Standard_Failure(aMsg.str().c_str());
223     return;
224   }
225
226   Standard_Integer i, nbLoc;
227   IS >> nbLoc;
228   IS.get();// remove lf
229   TopLoc_Location L;
230   gp_Trsf T;
231
232   try {
233     OCC_CATCH_SIGNALS
234     for (i = 1; i <= nbLoc; i++) { 
235       
236       const Standard_Byte aTypLoc = (Standard_Byte)IS.get();
237       if (aTypLoc == 1) {
238         IS >> T;
239         L = T;
240       }
241       
242       else if (aTypLoc == 2) {
243         L = TopLoc_Location();
244         BinTools::GetInteger(IS, l1); //Index
245         while (l1 != 0) { 
246           BinTools::GetInteger(IS, p);
247           TopLoc_Location L1 = myMap(l1);
248           L = L1.Powered(p) *L;
249           BinTools::GetInteger(IS, l1);
250         }
251       } else {
252         Standard_SStream aMsg;
253         aMsg << "Unexpected location's type = " << aTypLoc << std::endl;
254         throw Standard_Failure(aMsg.str().c_str());
255       }    
256       if (!L.IsIdentity()) myMap.Add(L);
257     }
258   }
259   catch(Standard_Failure const& anException) {
260     Standard_SStream aMsg;
261     aMsg << "EXCEPTION in BinTools_LocationSet::Read(..)" << std::endl;
262     aMsg << anException << std::endl;
263     throw Standard_Failure(aMsg.str().c_str());
264   }
265 }