37d177ba688227600ec485e19bf3e3ff3e9e7183
[occt.git] / src / QADraw / QADraw.cxx
1 // Created on: 2002-02-04
2 // Created by: QA Admin
3 // Copyright (c) 2002-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 #include <QADraw.hxx>
17 #include <Draw.hxx>
18 #include <Draw_Interpretor.hxx>
19 #include <ViewerTest.hxx>
20 #include <ViewerTest_EventManager.hxx>
21 #include <TopoDS_Shape.hxx>
22 #include <AIS_InteractiveContext.hxx>
23 #include <DBRep.hxx>
24 #include <Bnd_Box.hxx>
25 #include <BRepBndLib.hxx>
26 #include <BRepExtrema_DistShapeShape.hxx>
27 #include <BRepBuilderAPI_MakeVertex.hxx>
28 #include <TopTools_SequenceOfShape.hxx>
29 #include <TColgp_SequenceOfXYZ.hxx>
30 #include <OSD_Timer.hxx>
31 #include <stdio.h>
32
33 #if defined(_WIN32) || defined(__WIN32__)
34 #  include <windows.h>
35 #  include <io.h>
36 #else
37 #  include <unistd.h>
38 #endif
39
40 #include <Draw_PluginMacro.hxx>
41
42 //=======================================================================
43
44 #include <ViewerTest_DoubleMapOfInteractiveAndName.hxx>
45 #include <AIS_Trihedron.hxx>
46 #include <AIS_Axis.hxx>
47 #include <Geom_Line.hxx>
48 #include <AIS_Line.hxx>
49
50 Standard_EXPORT ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
51 Standard_EXPORT Handle(AIS_InteractiveContext)& TheAISContext();
52
53 //==============================================================================
54 // function : VTrihedronOrigins
55 // author   : ota
56 // purpose  : draws triheron axis origin lines.
57 // Draw arg : vtri_orig trihedron_name
58 //==============================================================================
59 static int VTrihedronOrigins(Draw_Interpretor& di,
60                               Standard_Integer argc,
61                               const char ** argv)
62 {
63   if(argc != 2){
64     di <<"Usage : vtri_orig tri_name\n";
65     return 1;
66   }
67
68   if(TheAISContext().IsNull()){
69     di<<"Make 'vinit' before this method call\n";
70     return 1;
71   }
72
73   //get trihedron from AIS map.
74   TCollection_AsciiString aName(argv[1]);
75   if(!GetMapOfAIS().IsBound2(aName)){
76     di<<"No object named '"<<argv[1]<<"'\n";
77     return 1;
78   }
79
80   Handle(AIS_Trihedron) aTrih =
81     Handle(AIS_Trihedron)::DownCast(GetMapOfAIS().Find2(aName));
82   if(aTrih.IsNull()){
83     di<<"Trihedron is not found, try another name\n";
84     return 1;
85   }
86
87   //get axis
88   Handle(AIS_Axis) XAxis = aTrih->XAxis();
89   Handle(AIS_Axis) YAxis = aTrih->YAxis();
90   Handle(AIS_Axis) ZAxis = aTrih->Axis();
91
92   //get geometrical lines
93   Handle(Geom_Line) XGLine = XAxis->Component();
94   Handle(Geom_Line) YGLine = YAxis->Component();
95   Handle(Geom_Line) ZGLine = ZAxis->Component();
96
97   //make AIS_Lines
98   Handle(AIS_Line) XLine = new AIS_Line(XGLine);
99   Handle(AIS_Line) YLine = new AIS_Line(YGLine);
100   Handle(AIS_Line) ZLine = new AIS_Line(ZGLine);
101
102   //put them into AIS map:
103   GetMapOfAIS().Bind(XLine,aName+"_X");
104   GetMapOfAIS().Bind(YLine,aName+"_Y");
105   GetMapOfAIS().Bind(ZLine,aName+"_Z");
106   //print names of created objects:
107   di<<argv[1]<<"_X  "<<argv[1]<<"_Y  "<<argv[1]<<"_Z\n";
108
109   //try to draw them:
110   TheAISContext()->Display (XLine, Standard_False);
111   TheAISContext()->Display (YLine, Standard_False);
112   TheAISContext()->Display (ZLine, Standard_True);
113
114   return 0;
115 }
116
117 //=======================================================================
118 //function : QATestExtremaSS
119 //purpose  :
120 //=======================================================================
121 static Standard_Integer QATestExtremaSS (Draw_Interpretor& theInterpretor,
122                                          Standard_Integer  theArgNb,
123                                          const char**      theArgs)
124 {
125   if (theArgNb < 3
126    || theArgNb > 4)
127   {
128     std::cerr << "Usage: type help " << theArgs[0] << std::endl;
129     return 1;
130   }
131
132   // Get target shape
133   TopoDS_Shape aShape = DBRep::Get (theArgs[1]);
134   if (aShape.IsNull())
135   {
136     std::cerr << "Error: " << theArgs[1] << " shape is null\n";
137     return 1;
138   }
139
140   // Get step value
141   const Standard_Real aStep = Draw::Atof (theArgs[2]);
142   if (aStep <= 1e-5)
143   {
144     std::cerr << "Error: Step " << aStep << " is too small\n";
145     return 1;
146   }
147
148   Extrema_ExtFlag aFlag = Extrema_ExtFlag_MIN;
149   if (theArgNb > 3)
150   {
151     Standard_Integer aVal = Draw::Atoi (theArgs[3]);
152     if (aVal > 0)
153     {
154       aFlag = aVal == 1 ? Extrema_ExtFlag_MAX : Extrema_ExtFlag_MINMAX;
155     }
156   }
157
158   // Get bounding box of the shape
159   Bnd_Box aBounds;
160   BRepBndLib::Add (aShape, aBounds);
161
162   Standard_Real aXmin, aYmin, aZmin;
163   Standard_Real aXmax, aYmax, aZmax;
164   aBounds.Get (aXmin, aYmin, aZmin,
165                aXmax, aYmax, aZmax);
166
167   const Standard_Real aScaleFactor = 1.5;
168   aXmin *= aScaleFactor;
169   aYmin *= aScaleFactor;
170   aZmin *= aScaleFactor;
171   aXmax *= aScaleFactor;
172   aYmax *= aScaleFactor;
173   aZmax *= aScaleFactor;
174
175   TopTools_SequenceOfShape aList;
176   TColgp_SequenceOfXYZ     aPoints;
177   for (Standard_Real aX = aXmin + 0.5 * aStep; aX < aXmax; aX += aStep)
178   {
179     for (Standard_Real aY = aYmin + 0.5 * aStep; aY < aYmax; aY += aStep)
180     {
181       aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aX, aY, aZmin)));
182       aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aX, aY, aZmax)));
183
184       aPoints.Append (gp_XYZ (aX, aY, aZmin));
185       aPoints.Append (gp_XYZ (aX, aY, aZmax));
186     }
187
188     for (Standard_Real aZ = aZmin + 0.5 * aStep; aZ < aZmax; aZ += aStep)
189     {
190       aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aX, aYmin, aZ)));
191       aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aX, aYmax, aZ)));
192
193       aPoints.Append (gp_XYZ (aX, aYmin, aZ));
194       aPoints.Append (gp_XYZ (aX, aYmax, aZ));
195     }
196   }
197
198   for (Standard_Real aY = aYmin + 0.5 * aStep; aY < aYmax; aY += aStep)
199   {
200     for (Standard_Real aZ = aZmin + 0.5 * aStep; aZ < aZmax; aZ += aStep)
201     {
202       aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aXmin, aY, aZ)));
203       aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aXmax, aY, aZ)));
204
205       aPoints.Append (gp_XYZ (aXmin, aY, aZ));
206       aPoints.Append (gp_XYZ (aXmax, aY, aZ));
207     }
208   }
209
210   const Standard_Integer aNbPoints = aList.Length();
211   theInterpretor << "Number of sampled points: " << aNbPoints << "\n";
212
213   // Perform projection of points
214   OSD_Timer aTimer;
215   aTimer.Start();
216
217   // Perform projection using standard method
218   BRepExtrema_DistShapeShape aTool;
219   aTool.SetFlag (aFlag);
220   aTool.LoadS1 (aShape);
221   for (Standard_Integer anIdx = 1; anIdx <= aNbPoints; ++anIdx)
222   {
223     aTool.LoadS2 (aList (anIdx));
224     aTool.Perform();
225   }
226
227   aTimer.Stop();
228   theInterpretor << "Test for gradient descent complete in: " << aTimer.ElapsedTime() << "\n";
229   return 0;
230 }
231
232 //=======================================================================
233 //function : CommonCommands
234 //purpose  :
235 //=======================================================================
236 void QADraw::CommonCommands (Draw_Interpretor& theCommands)
237 {
238   const char* group = "QA_Commands";
239
240   theCommands.Add ("vtri_orig",
241                    "vtri_orig         : vtri_orig trihedron_name  -  draws axis origin lines",
242                    __FILE__,
243                    VTrihedronOrigins,
244                    group);
245
246   theCommands.Add ("QATestExtremaSS",
247                    "QATestExtremaSS Shape Step [Flag { MIN = 0 | MAX = 1 | MINMAX = 2 }]",
248                    __FILE__,
249                    QATestExtremaSS,
250                    group);
251
252 // adding commands "rename" leads to the fact that QA commands doesn't work properly OCC23410, use function "renamevar"
253 // theCommands.Add("rename","rename name1 toname1 name2 toname2 ...",__FILE__,QArename,group);
254 }
255
256 //==============================================================================
257 // QADraw::Factory
258 //==============================================================================
259 void QADraw::Factory(Draw_Interpretor& theCommands)
260 {
261   // definition of QA Command
262   QADraw::CommonCommands(theCommands);
263   QADraw::AdditionalCommands(theCommands);
264 }
265
266 // Declare entry point PLUGINFACTORY
267 DPLUGIN(QADraw)