0024510: Remove unused local variables
[occt.git] / src / HLRTest / HLRTest.cxx
1 // Created on: 1995-04-05
2 // Created by: Christophe MARION
3 // Copyright (c) 1995-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
9 // under the terms of the GNU Lesser General Public 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 #include <HLRTest.ixx>
18 #include <HLRTest_Projector.hxx>
19 #include <HLRTest_OutLiner.hxx>
20 #include <TColStd_ListIteratorOfListOfInteger.hxx>
21 #include <gp_Ax3.hxx>
22 #include <Draw_Appli.hxx>
23 #include <Draw.hxx>
24 #include <DBRep.hxx>
25 #include <HLRBRep_Algo.hxx>
26 #include <HLRBRep_HLRToShape.hxx>
27 #include <HLRAppli_ReflectLines.hxx>
28
29 static Handle(HLRBRep_Algo) hider;
30 #ifdef WNT
31 Standard_IMPORT Draw_Viewer dout;
32 #endif
33
34 #include <BRepTopAdaptor_MapOfShapeTool.hxx>
35
36 //=======================================================================
37 //function : Set
38 //purpose  : 
39 //=======================================================================
40 void HLRTest::Set (const Standard_CString Name,
41                    const HLRAlgo_Projector& P)
42 {
43   Draw::Set(Name,new HLRTest_Projector(P));
44 }
45
46 //=======================================================================
47 //function : GetProjector
48 //purpose  : 
49 //=======================================================================
50 Standard_Boolean HLRTest::GetProjector (Standard_CString& Name,
51                                         HLRAlgo_Projector& P)
52 {
53   Handle(HLRTest_Projector) HP = 
54     Handle(HLRTest_Projector)::DownCast(Draw::Get(Name));
55   if (HP.IsNull()) return Standard_False;
56   P = HP->Projector();
57   return Standard_True;
58   
59 }
60
61 //=======================================================================
62 //function : Set
63 //purpose  : 
64 //=======================================================================
65
66 void HLRTest::Set (const Standard_CString Name,
67                    const TopoDS_Shape& S)
68 {
69   Draw::Set(Name,new HLRTest_OutLiner(S));
70 }
71
72 //=======================================================================
73 //function : GetOutLiner
74 //purpose  : 
75 //=======================================================================
76 Handle(HLRTopoBRep_OutLiner) HLRTest::GetOutLiner (Standard_CString& Name)
77 {
78   Handle(Draw_Drawable3D) D = Draw::Get(Name);
79   Handle(HLRTest_OutLiner) HS = Handle(HLRTest_OutLiner)::DownCast(D);
80   if (!HS.IsNull()) return HS->OutLiner();
81   Handle(HLRTopoBRep_OutLiner) HO;
82   return HO;
83 }
84
85 //=======================================================================
86 //function : hprj
87 //purpose  : 
88 //=======================================================================
89
90 static Standard_Integer
91 hprj (Draw_Interpretor& , Standard_Integer n, const char** a)
92 {
93   if (n < 2) return 1;
94   //
95   gp_Ax2 anAx2 = gp::XOY();
96   if (n == 11)
97   {
98     Standard_Real x = Draw::Atof(a[2]);
99     Standard_Real y = Draw::Atof(a[3]);
100     Standard_Real z = Draw::Atof(a[4]);
101
102     Standard_Real dx = Draw::Atof(a[5]);
103     Standard_Real dy = Draw::Atof(a[6]);
104     Standard_Real dz = Draw::Atof(a[7]);
105
106     Standard_Real dx1 = Draw::Atof(a[8]);
107     Standard_Real dy1 = Draw::Atof(a[9]);
108     Standard_Real dz1 = Draw::Atof(a[10]);
109
110     gp_Pnt anOrigin (x, y, z);
111     gp_Dir aNormal  (dx, dy, dz);
112     gp_Dir aDX      (dx1, dy1, dz1);
113     anAx2 = gp_Ax2(anOrigin, aNormal, aDX);
114   }
115   
116   HLRAlgo_Projector P(anAx2);
117   HLRTest::Set(a[1],P);
118   return 0;
119 }
120
121 //=======================================================================
122 //function : hout
123 //purpose  : 
124 //=======================================================================
125
126 static Standard_Integer
127 hout (Draw_Interpretor& di, Standard_Integer n, const char** a)
128 {
129   if (n < 2) return 1;
130   const char *name = a[2];
131   TopoDS_Shape S = DBRep::Get(name);
132   if (S.IsNull()) {
133     di << name << " is not a shape." << "\n";
134     return 1;
135   }
136   HLRTest::Set(a[1],S);
137   return 0;
138 }
139
140 //=======================================================================
141 //function : hfil
142 //purpose  : 
143 //=======================================================================
144
145 static Standard_Integer
146 hfil (Draw_Interpretor& di, Standard_Integer n, const char** a)
147 {
148   Standard_Integer nbIso = 0;
149   if (n < 3) return 1;
150   if (n > 3) nbIso = Draw::Atoi(a[3]);
151   const char *name1 = a[1];
152   Handle(HLRTopoBRep_OutLiner) HS = HLRTest::GetOutLiner(name1);
153   if (HS.IsNull()) {
154     di << name1 << " is not an OutLiner." << "\n";
155     return 1;
156   }
157   const char *name2 = a[2];
158   HLRAlgo_Projector P;
159   if (!HLRTest::GetProjector(name2,P)) {
160     di << name2 << " is not a projector." << "\n";
161     return 1;
162   }
163   BRepTopAdaptor_MapOfShapeTool MST;
164   HS->Fill(P,MST,nbIso);
165   return 0;
166 }
167
168 //=======================================================================
169 //function : sori
170 //purpose  : 
171 //=======================================================================
172
173 static Standard_Integer
174 sori (Draw_Interpretor& di, Standard_Integer n, const char** a)
175 {
176   if (n < 3) return 1;
177   const char *name1 = a[1];
178   const char *name2 = a[2];
179   Handle(HLRTopoBRep_OutLiner) HS = HLRTest::GetOutLiner(name2);
180   if (HS.IsNull()) {
181     di << name2 << " is not an OutLiner." << "\n";
182     return 1;
183   }
184   DBRep::Set(name1,HS->OriginalShape());
185   return 0;
186 }
187
188 //=======================================================================
189 //function : sout
190 //purpose  : 
191 //=======================================================================
192
193 static Standard_Integer
194 sout (Draw_Interpretor& di, Standard_Integer n, const char** a)
195 {
196   if (n < 3) return 1;
197   const char *name1 = a[1];
198   const char *name2 = a[2];
199   Handle(HLRTopoBRep_OutLiner) HS = HLRTest::GetOutLiner(name2);
200   if (HS.IsNull()) {
201     di << name2 << " is not an OutLiner." << "\n";
202     return 1;
203   }
204   if (HS->OutLinedShape().IsNull()) {
205     di << name2 << " has no OutLinedShape." << "\n";
206     return 1;
207   }
208   DBRep::Set(name1,HS->OutLinedShape());
209   return 0;
210 }
211
212 //=======================================================================
213 //function : hloa
214 //purpose  : 
215 //=======================================================================
216
217 static Standard_Integer
218 hloa (Draw_Interpretor& di, Standard_Integer n, const char** a)
219 {
220   if (n < 2) return 1;
221   const char *name1 = a[1];
222   Handle(HLRTopoBRep_OutLiner) HS = HLRTest::GetOutLiner(name1);
223   if (HS.IsNull()) {
224     di << name1 << " is not an OutLiner." << "\n";
225     return 1;
226   }
227   hider->Load(HS);
228   return 0;
229 }
230
231 //=======================================================================
232 //function : hrem
233 //purpose  : 
234 //=======================================================================
235
236 static Standard_Integer
237 hrem (Draw_Interpretor& di, Standard_Integer n, const char** a)
238 {
239   if (n > 1) {
240     const char *name = a[1];
241     Standard_Integer index;
242     Handle(HLRTopoBRep_OutLiner) HS = HLRTest::GetOutLiner(name);
243     if (HS.IsNull()) {
244       TopoDS_Shape S = DBRep::Get(name);
245       if (S.IsNull()) {
246         di << name << " is not an OutLiner and not a shape." << "\n";
247         return 1;
248       }
249       else {
250         index = hider->Index(S);
251         if (index == 0) {
252           di << name << " not loaded shape." << "\n";
253           return 1;
254         }
255       }
256     }
257     else {
258       index = hider->Index(HS->OriginalShape());
259       if (index == 0) {
260         di << name << " not loaded outliner." << "\n";
261         return 1;
262       }
263     }
264     hider->Remove(index);
265     di << name << " removed" << "\n";
266   }
267   else {
268     while (hider->NbShapes() > 0) {
269       hider->Remove(1);
270     }
271     di << " all shapes removed" << "\n";
272   }
273   return 0;
274 }
275
276 //=======================================================================
277 //function : sprj
278 //purpose  : 
279 //=======================================================================
280
281 static Standard_Integer
282 sprj (Draw_Interpretor& di, Standard_Integer n, const char** a)
283 {
284   if (n < 2) return 1;
285   const char *name = a[1];
286   HLRAlgo_Projector P;
287   if (!HLRTest::GetProjector(name,P)) {
288     di << name << " is not a projector." << "\n";
289     return 1;
290   }
291   hider->Projector(P);
292   return 0;
293 }
294
295 //=======================================================================
296 //function : upda
297 //purpose  : 
298 //=======================================================================
299
300 static Standard_Integer
301 upda (Draw_Interpretor& , Standard_Integer, const char**)
302 {
303   hider->Update();
304   return 0;
305 }
306
307 //=======================================================================
308 //function : hide
309 //purpose  : 
310 //=======================================================================
311
312 static Standard_Integer
313 hide (Draw_Interpretor& , Standard_Integer, const char**)
314 {
315   hider->Hide();
316   return 0;
317 }
318
319 //=======================================================================
320 //function : show
321 //purpose  : 
322 //=======================================================================
323
324 static Standard_Integer
325 show (Draw_Interpretor& , Standard_Integer, const char**)
326 {
327   hider->ShowAll();
328   return 0;
329 }
330
331 //=======================================================================
332 //function : hdbg
333 //purpose  : 
334 //=======================================================================
335
336 static Standard_Integer
337 hdbg (Draw_Interpretor& di, Standard_Integer, const char**)
338 {
339   hider->Debug(!hider->Debug());
340   if (hider->Debug())
341     di << "debug" << "\n";
342   else
343     di << "no debug" << "\n";
344   return 0;
345 }
346
347 //=======================================================================
348 //function : hnul
349 //purpose  : 
350 //=======================================================================
351
352 static Standard_Integer
353 hnul (Draw_Interpretor& , Standard_Integer, const char**)
354 {
355   hider->OutLinedShapeNullify();
356   return 0;
357 }
358
359 //=======================================================================
360 //function : hres
361 //purpose  : 
362 //=======================================================================
363
364 static Standard_Integer
365 hres (Draw_Interpretor& , Standard_Integer n, const char** a)
366 {
367   TopoDS_Shape S,V,V1,VN,VO,VI,H,H1,HN,HO,HI;
368   if (n > 1) {
369     const char *name = a[1];
370     S = DBRep::Get(name);
371   }
372   HLRBRep_HLRToShape HS(hider);
373
374   if (S.IsNull()) {
375     V  = HS.VCompound();
376     V1 = HS.Rg1LineVCompound();
377     VN = HS.RgNLineVCompound();
378     VO = HS.OutLineVCompound();
379     VI = HS.IsoLineVCompound();
380     H  = HS.HCompound();
381     H1 = HS.Rg1LineHCompound();
382     HN = HS.RgNLineHCompound();
383     HO = HS.OutLineHCompound();
384     HI = HS.IsoLineHCompound();
385   }
386   else {
387     V  = HS.VCompound(S);
388     V1 = HS.Rg1LineVCompound(S);
389     VN = HS.RgNLineVCompound(S);
390     VO = HS.OutLineVCompound(S);
391     VI = HS.IsoLineVCompound(S);
392     H  = HS.HCompound(S);
393     H1 = HS.Rg1LineHCompound(S);
394     HN = HS.RgNLineHCompound(S);
395     HO = HS.OutLineHCompound(S);
396     HI = HS.IsoLineHCompound(S);
397   }
398   if (!V .IsNull()) DBRep::Set("vl",V);
399   if (!V1.IsNull()) DBRep::Set("v1l",V1);
400   if (!VN.IsNull()) DBRep::Set("vnl",VN);
401   if (!VO.IsNull()) DBRep::Set("vol",VO);
402   if (!VI.IsNull()) DBRep::Set("vil",VI);
403   if (!H .IsNull()) DBRep::Set("hl",H);
404   if (!H1.IsNull()) DBRep::Set("h1l",H1);
405   if (!HN.IsNull()) DBRep::Set("hnl",HN);
406   if (!HO.IsNull()) DBRep::Set("hol",HO);
407   if (!HI.IsNull()) DBRep::Set("hil",HI);
408   return 0;
409 }
410
411 //=======================================================================
412 //function : reflectlines
413 //purpose  : 
414 //=======================================================================
415
416 static Standard_Integer reflectlines(Draw_Interpretor& , Standard_Integer n, const char** a)
417 {
418   if (n < 6)
419     return 1;
420
421   TopoDS_Shape aShape =  DBRep::Get(a[2]);
422   if (aShape.IsNull())
423     return 1;
424
425   Standard_Real anAISViewProjX = atof(a[3]);
426   Standard_Real anAISViewProjY = atof(a[4]);
427   Standard_Real anAISViewProjZ = atof(a[5]);
428   
429   gp_Pnt anOrigin(0.,0.,0.);
430   gp_Dir aNormal(anAISViewProjX, anAISViewProjY, anAISViewProjZ);
431   gp_Ax2 theAxes(anOrigin, aNormal);
432   gp_Dir aDX = theAxes.XDirection();
433
434   HLRAppli_ReflectLines Reflector(aShape);
435
436   Reflector.SetAxes(aNormal.X(), aNormal.Y(), aNormal.Z(),
437                     anOrigin.X(), anOrigin.Y(), anOrigin.Z(),
438                     aDX.X(), aDX.Y(), aDX.Z());
439
440   Reflector.Perform();
441
442   TopoDS_Shape Result = Reflector.GetResult();
443   DBRep::Set(a[1], Result);
444
445   return 0;
446 }
447
448 //=======================================================================
449 //function : Commands
450 //purpose  : 
451 //=======================================================================
452
453 void HLRTest::Commands (Draw_Interpretor& theCommands)
454 {
455   const char* g = "ADVALGOS HLR Commands";
456
457   theCommands.Add("hprj"     ,"hprj name [view-id = 1]"     ,__FILE__,hprj,g);
458   theCommands.Add("houtl"    ,"houtl name shape"            ,__FILE__,hout,g);
459   theCommands.Add("hfill"    ,"hfill name proj [nbIso]"     ,__FILE__,hfil,g);
460   theCommands.Add("hsin"     ,"hsin name outliner"          ,__FILE__,sori,g);
461   theCommands.Add("hsout"    ,"hsout name outliner"         ,__FILE__,sout,g);
462   theCommands.Add("hload"    ,"hload outliner"              ,__FILE__,hloa,g);
463   theCommands.Add("hremove"  ,"hremove [name]"              ,__FILE__,hrem,g);
464   theCommands.Add("hsetprj"  ,"hsetprj [name]"              ,__FILE__,sprj,g);
465   theCommands.Add("hupdate"  ,"hupdate"                     ,__FILE__,upda,g);
466   theCommands.Add("hhide"    ,"hhide"                       ,__FILE__,hide,g);
467   theCommands.Add("hshowall" ,"hshowall"                    ,__FILE__,show,g);
468   theCommands.Add("hdebug"   ,"hdebug"                      ,__FILE__,hdbg,g);
469   theCommands.Add("hnullify" ,"hnullify"                    ,__FILE__,hnul,g);
470   theCommands.Add("hres2d"   ,"hres2d"                      ,__FILE__,hres,g);
471
472   theCommands.Add("reflectlines",
473                   "reflectlines res shape proj_X proj_Y proj_Z",
474                   __FILE__, reflectlines, g);
475   
476   hider = new HLRBRep_Algo();
477 }
478
479 //=======================================================================
480 //function : save and restore projector
481 //purpose  : 
482 //=======================================================================
483
484 static Standard_Boolean stest(const Handle(Draw_Drawable3D)& d) 
485 {
486   return d->IsInstance(STANDARD_TYPE(HLRTest_Projector));
487 }
488
489 //=======================================================================
490 //function : ssave
491 //purpose  : 
492 //=======================================================================
493
494 static void ssave (const Handle(Draw_Drawable3D)&d, ostream& OS)
495 {
496   Handle(HLRTest_Projector) HP =
497     Handle(HLRTest_Projector)::DownCast(d);
498
499   const HLRAlgo_Projector& P = HP->Projector();
500   OS << (P.Perspective() ? "1" : "0") << "\n";
501   if (P.Perspective())
502     OS << P.Focus() << "\n";
503   
504   gp_Trsf T = P.Transformation();
505   gp_XYZ V = T.TranslationPart();
506   gp_Mat M = T.VectorialPart();
507
508   OS << M(1,1) << " ";
509   OS << M(1,2) << " ";
510   OS << M(1,3) << " ";
511   OS << V.Coord(1) << " ";
512   OS << "\n";
513   OS << M(2,1) << " ";
514   OS << M(2,2) << " ";
515   OS << M(2,3) << " ";
516   OS << V.Coord(2) << " ";
517   OS << "\n";
518   OS << M(3,1) << " ";
519   OS << M(3,2) << " ";
520   OS << M(3,3) << " ";
521   OS << V.Coord(3) << " ";
522   OS << "\n";
523
524 }
525
526 //=======================================================================
527 //function : srestore
528 //purpose  : 
529 //=======================================================================
530
531 static Handle(Draw_Drawable3D) srestore (istream& IS)
532 {
533   Standard_Boolean pers;
534   IS >> pers;
535   Standard_Real focus = 1;
536   if (pers) IS >> focus;
537   
538   gp_Trsf T;
539   Standard_Real V1[3],V2[3],V3[3];
540   Standard_Real V[3];
541
542   IS >> V1[0] >> V1[1] >> V1[2] >> V[0];
543   IS >> V2[0] >> V2[1] >> V2[2] >> V[1];
544   IS >> V3[0] >> V3[1] >> V3[2] >> V[2];
545
546   gp_Dir D1(V1[0],V1[1],V1[2]);
547   gp_Dir D2(V2[0],V2[1],V2[2]);
548   gp_Dir D3(V3[0],V3[1],V3[2]);
549   gp_Ax3 axes(gp_Pnt(0,0,0),D3,D1);
550   D3.Cross(D1);
551   if (D3.Dot(D2) < 0) axes.YReverse();
552   T.SetTransformation(axes);
553
554   T.SetTranslationPart(gp_Vec(V[0],V[1],V[2]));
555
556   HLRAlgo_Projector P(T,pers,focus);
557   Handle(HLRTest_Projector) HP = new HLRTest_Projector(P);
558   return HP;
559 }
560
561 //=======================================================================
562 //function : ssr
563 //purpose  : 
564 //=======================================================================
565
566 static Draw_SaveAndRestore ssr("HLRTest_Projector",stest,ssave,srestore);
567