0022627: Change OCCT memory management defaults
[occt.git] / src / BinTools / BinTools_LocationSet.cxx
CommitLineData
7fd59977 1// File: BinTools_LocationSet.cxx
2// Created: Tue Jun 15 12:04:06 2004
3// Author: Sergey ZARITCHNY <szy@opencascade.com>
4// Copyright: Open CasCade S.A. 2004
5
6#include <BinTools_LocationSet.ixx>
7#include <BinTools.hxx>
8#include <gp_Ax3.hxx>
9#include <gp_Vec.hxx>
10#include <Precision.hxx>
11#include <Standard_ErrorHandler.hxx>
12
13//=======================================================================
14//function : operator >> (gp_Trsf& T)
15//purpose :
16//=======================================================================
17static Standard_IStream& operator >>(Standard_IStream& IS, gp_Trsf& T)
18{
19 Standard_Real V1[3],V2[3],V3[3];
20 Standard_Real V[3];
21
22 BinTools::GetReal(IS, V1[0]);
23 BinTools::GetReal(IS, V1[1]);
24 BinTools::GetReal(IS, V1[2]);
25 BinTools::GetReal(IS, V[0]);
26
27 BinTools::GetReal(IS, V2[0]);
28 BinTools::GetReal(IS, V2[1]);
29 BinTools::GetReal(IS, V2[2]);
30 BinTools::GetReal(IS, V[1]);
31
32 BinTools::GetReal(IS, V3[0]);
33 BinTools::GetReal(IS, V3[1]);
34 BinTools::GetReal(IS, V3[2]);
35 BinTools::GetReal(IS, V[2]);
36
37 T.SetValues(V1[0],V1[1],V1[2],V[0],
38 V2[0],V2[1],V2[2],V[1],
39 V3[0],V3[1],V3[2],V[2],
40 Precision::Angular(),
41 Precision::Confusion());
42 return IS;
43}
44
45//=======================================================================
46//function : operator << (gp_Trsf& T)
47//purpose :
48//=======================================================================
49static Standard_OStream& operator <<(Standard_OStream& OS,const gp_Trsf& T)
50{
51 gp_XYZ V = T.TranslationPart();
52 gp_Mat M = T.VectorialPart();
53
54 BinTools::PutReal(OS, M(1,1));
55 BinTools::PutReal(OS, M(1,2));
56 BinTools::PutReal(OS, M(1,3));
57 BinTools::PutReal(OS,V.Coord(1));
58
59 BinTools::PutReal(OS, M(2,1));
60 BinTools::PutReal(OS, M(2,2));
61 BinTools::PutReal(OS, M(2,3));
62 BinTools::PutReal(OS,V.Coord(2));
63
64 BinTools::PutReal(OS, M(3,1));
65 BinTools::PutReal(OS, M(3,2));
66 BinTools::PutReal(OS, M(3,3));
67 BinTools::PutReal(OS,V.Coord(3));
68
69 return OS;
70}
71
72//=======================================================================
73//function : BinTools_LocationSet
74//purpose :
75//=======================================================================
76
77BinTools_LocationSet::BinTools_LocationSet()
78{
79}
80
81
82//=======================================================================
83//function : Clear
84//purpose :
85//=======================================================================
86
87void BinTools_LocationSet::Clear()
88{
89 myMap.Clear();
90}
91
92
93//=======================================================================
94//function : Add
95//purpose :
96//=======================================================================
97
98Standard_Integer BinTools_LocationSet::Add(const TopLoc_Location& L)
99{
100 if (L.IsIdentity()) return 0;
101 Standard_Integer n = myMap.FindIndex(L);
102 if (n > 0) return n;
103 TopLoc_Location N = L;
104 do {
105 myMap.Add(N.FirstDatum());
106 N = N.NextLocation();
107 } while (!N.IsIdentity());
108 return myMap.Add(L);
109}
110
111
112//=======================================================================
113//function : Location
114//purpose :
115//=======================================================================
116
117const TopLoc_Location& BinTools_LocationSet::Location
118 (const Standard_Integer I)const
119{
120 static TopLoc_Location identity;
121 if (I == 0) return identity;
122 else return myMap(I);
123}
124
125
126//=======================================================================
127//function : Index
128//purpose :
129//=======================================================================
130
131Standard_Integer BinTools_LocationSet::Index(const TopLoc_Location& L) const
132{
133 if (L.IsIdentity()) return 0;
134 return myMap.FindIndex(L);
135}
136
137//=======================================================================
138//function : NbLocations
139//purpose :
140//=======================================================================
141
142Standard_Integer BinTools_LocationSet::NbLocations() const
143{
144 return myMap.Extent();
145}
146
147//=======================================================================
148//function : Write locations
149//purpose :
150//=======================================================================
151
152void BinTools_LocationSet::Write(Standard_OStream& OS) const
153{
154
155 Standard_Integer i, nbLoc = myMap.Extent();
156 OS << "Locations "<< nbLoc <<endl;
157 try {
158 OCC_CATCH_SIGNALS
159 for (i = 1; i <= nbLoc; i++) {
160 TopLoc_Location L = myMap(i);
161
162 TopLoc_Location L2 = L.NextLocation();
163 Standard_Boolean simple = L2.IsIdentity();
164 Standard_Integer p = L.FirstPower();
165 TopLoc_Location L1 = L.FirstDatum();
166 Standard_Boolean elementary = (simple && p == 1);
167 if (elementary) {
168
169 OS.put((Standard_Byte)1); // 1
170 OS << L.Transformation();
171 }
172 else {
173 OS.put((Standard_Byte)2); // 2
174 BinTools::PutInteger(OS, myMap.FindIndex(L1));
175 BinTools::PutInteger(OS, p);
176 while (!L2.IsIdentity()) {
177 L1 = L2.FirstDatum();
178 p = L2.FirstPower();
179 L2 = L2.NextLocation();
180 BinTools::PutInteger(OS, myMap.FindIndex(L1));
181 BinTools::PutInteger(OS, p);
182 }
183
184 BinTools::PutInteger(OS, 0);
185 }
186 }
187 }
188 catch(Standard_Failure) {
189 Standard_SStream aMsg;
190 aMsg << "EXCEPTION in BinTools_LocatioSet::Write(..)" << endl;
191 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
192 aMsg << anExc << endl;
193 Standard_Failure::Raise(aMsg);
194 }
195}
196
197//=======================================================================
198//function : Read
199//purpose :
200//=======================================================================
201void BinTools_LocationSet::Read(Standard_IStream& IS)
202{
203
204 myMap.Clear();
205 char buffer[255];
206 Standard_Integer l1,p;
207
208 IS >> buffer;
209 if (IS.fail() || (strcmp(buffer,"Locations"))) {
210 Standard_SStream aMsg;
211 aMsg << "BinTools_LocationSet::Read: Not a location table"<<endl;
212 Standard_Failure::Raise(aMsg);
213 return;
214 }
215
216 Standard_Integer i, nbLoc;
217 IS >> nbLoc;
218 IS.get();// remove lf
219 TopLoc_Location L;
220 gp_Trsf T;
221
222 try {
223 OCC_CATCH_SIGNALS
224 for (i = 1; i <= nbLoc; i++) {
225
226 const Standard_Byte aTypLoc = IS.get();
227 if (aTypLoc == 1) {
228 IS >> T;
229 L = T;
230 }
231
232 else if (aTypLoc == 2) {
233 L = TopLoc_Location();
234 BinTools::GetInteger(IS, l1); //Index
235 while (l1 != 0) {
236 BinTools::GetInteger(IS, p);
237 TopLoc_Location L1 = myMap(l1);
238 L = L1.Powered(p) *L;
239 BinTools::GetInteger(IS, l1);
240 }
241 } else {
242 Standard_SStream aMsg;
243 aMsg << "Unexpected location's type = " << aTypLoc << endl;
244 Standard_Failure::Raise(aMsg);
245 }
246 if (!L.IsIdentity()) myMap.Add(L);
247 }
248 }
249 catch(Standard_Failure) {
250 Standard_SStream aMsg;
251 aMsg << "EXCEPTION in BinTools_LocationSet::Read(..)" << endl;
252 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
253 aMsg << anExc << endl;
254 Standard_Failure::Raise(aMsg);
255 }
256}