b311480e |
1 | // Created on: 2004-06-15 |
2 | // Created by: Sergey ZARITCHNY |
973c2be1 |
3 | // Copyright (c) 2004-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
7fd59977 |
15 | |
42cf5bc1 |
16 | |
7fd59977 |
17 | #include <BinTools.hxx> |
42cf5bc1 |
18 | #include <BinTools_LocationSet.hxx> |
7fd59977 |
19 | #include <gp_Ax3.hxx> |
20 | #include <gp_Vec.hxx> |
21 | #include <Precision.hxx> |
22 | #include <Standard_ErrorHandler.hxx> |
42cf5bc1 |
23 | #include <Standard_OutOfRange.hxx> |
24 | #include <TopLoc_Location.hxx> |
7fd59977 |
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], |
7a8c6a36 |
52 | V3[0],V3[1],V3[2],V[2]); |
7fd59977 |
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 <<endl; |
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) { |
200 | Standard_SStream aMsg; |
201 | aMsg << "EXCEPTION in BinTools_LocatioSet::Write(..)" << endl; |
202 | Handle(Standard_Failure) anExc = Standard_Failure::Caught(); |
203 | aMsg << anExc << endl; |
204 | Standard_Failure::Raise(aMsg); |
205 | } |
206 | } |
207 | |
208 | //======================================================================= |
209 | //function : Read |
210 | //purpose : |
211 | //======================================================================= |
212 | void BinTools_LocationSet::Read(Standard_IStream& IS) |
213 | { |
214 | |
215 | myMap.Clear(); |
216 | char buffer[255]; |
217 | Standard_Integer l1,p; |
218 | |
219 | IS >> buffer; |
220 | if (IS.fail() || (strcmp(buffer,"Locations"))) { |
221 | Standard_SStream aMsg; |
222 | aMsg << "BinTools_LocationSet::Read: Not a location table"<<endl; |
223 | Standard_Failure::Raise(aMsg); |
224 | return; |
225 | } |
226 | |
227 | Standard_Integer i, nbLoc; |
228 | IS >> nbLoc; |
229 | IS.get();// remove lf |
230 | TopLoc_Location L; |
231 | gp_Trsf T; |
232 | |
233 | try { |
234 | OCC_CATCH_SIGNALS |
235 | for (i = 1; i <= nbLoc; i++) { |
236 | |
8263fcd3 |
237 | const Standard_Byte aTypLoc = (Standard_Byte)IS.get(); |
7fd59977 |
238 | if (aTypLoc == 1) { |
239 | IS >> T; |
240 | L = T; |
241 | } |
242 | |
243 | else if (aTypLoc == 2) { |
244 | L = TopLoc_Location(); |
245 | BinTools::GetInteger(IS, l1); //Index |
246 | while (l1 != 0) { |
247 | BinTools::GetInteger(IS, p); |
248 | TopLoc_Location L1 = myMap(l1); |
249 | L = L1.Powered(p) *L; |
250 | BinTools::GetInteger(IS, l1); |
251 | } |
252 | } else { |
253 | Standard_SStream aMsg; |
254 | aMsg << "Unexpected location's type = " << aTypLoc << endl; |
255 | Standard_Failure::Raise(aMsg); |
256 | } |
257 | if (!L.IsIdentity()) myMap.Add(L); |
258 | } |
259 | } |
260 | catch(Standard_Failure) { |
261 | Standard_SStream aMsg; |
262 | aMsg << "EXCEPTION in BinTools_LocationSet::Read(..)" << endl; |
263 | Handle(Standard_Failure) anExc = Standard_Failure::Caught(); |
264 | aMsg << anExc << endl; |
265 | Standard_Failure::Raise(aMsg); |
266 | } |
267 | } |