1 // Created on: 2002-02-04
2 // Created by: QA Admin
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
18 #include <Draw_Interpretor.hxx>
19 #include <Draw_Window.hxx>
20 #include <Draw_Viewer.hxx>
21 #include <Image_AlienPixMap.hxx>
22 #include <V3d_View.hxx>
23 #include <ViewerTest.hxx>
24 #include <ViewerTest_EventManager.hxx>
25 #include <TColStd_ListOfInteger.hxx>
26 #include <TColStd_ListIteratorOfListOfInteger.hxx>
27 #include <TColStd_HSequenceOfReal.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <AIS_InteractiveContext.hxx>
30 #include <Aspect_Window.hxx>
31 #include <Aspect_DisplayConnection.hxx>
33 #include <Bnd_Box.hxx>
34 #include <BRepBndLib.hxx>
35 #include <BRepExtrema_DistShapeShape.hxx>
36 #include <BRepBuilderAPI_MakeVertex.hxx>
37 #include <TopTools_SequenceOfShape.hxx>
38 #include <TColgp_SequenceOfXYZ.hxx>
39 #include <OSD_Timer.hxx>
42 #if defined(_WIN32) || defined(__WIN32__)
49 #include <Draw_PluginMacro.hxx>
51 Handle(TColStd_HSequenceOfReal) GetColorOfPixel (const Image_PixMap& theImage,
52 const Standard_Integer theCoordinateX,
53 const Standard_Integer theCoordinateY,
54 const Standard_Integer theRadius)
56 Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal();
57 if (theImage.IsEmpty()) {
58 std::cerr << "The image is null.\n";
61 Standard_Integer aWidth = (Standard_Integer )theImage.SizeX();
62 Standard_Integer anHeight = (Standard_Integer )theImage.SizeY();
64 Quantity_Color aColorTmp;
65 for (Standard_Integer anXIter = theCoordinateX - theRadius;
66 anXIter <= theCoordinateX + theRadius; ++anXIter)
68 if (anXIter < 0 || anXIter >= aWidth)
72 for (Standard_Integer anYIter = theCoordinateY - theRadius;
73 anYIter <= theCoordinateY + theRadius; ++anYIter)
75 if (anYIter < 0 || anYIter >= anHeight)
79 // Image_PixMap stores image upside-down in memory!
80 aColorTmp = theImage.PixelColor (anXIter, anYIter);
81 aSeq->Append (aColorTmp.Red());
82 aSeq->Append (aColorTmp.Green());
83 aSeq->Append (aColorTmp.Blue());
89 static Standard_Integer QAAISGetPixelColor (Draw_Interpretor& theDi,
90 Standard_Integer theArgsNb,
93 if (theArgsNb != 3 && theArgsNb != 6)
95 theDi << "Usage : " << theArgs[0] << " coordinate_X coordinate_Y [color_R color_G color_B]" << "\n";
96 return 1; // TCL_ERROR
99 Handle(V3d_View) aView3d = ViewerTest::CurrentView();
100 if (aView3d.IsNull())
102 theDi << "You must initialize AISViewer before this command.\n";
103 return 1; // TCL_ERROR
106 const Handle(Aspect_Window) anAISWindow = aView3d->Window();
107 Standard_Integer aWindowSizeX = 0;
108 Standard_Integer aWindowSizeY = 0;
109 anAISWindow->Size (aWindowSizeX, aWindowSizeY);
111 Standard_Integer anArgIter = 1;
112 const Standard_Integer aPickCoordX = Draw::Atoi (theArgs[anArgIter++]);
113 const Standard_Integer aPickCoordY = Draw::Atoi (theArgs[anArgIter++]);
114 const Standard_Integer aRadius = (theArgsNb == 3) ? 0 : 1;
116 Image_ColorRGBF aColorInput = {{ 0.0f, 0.0f, 0.0f }};
119 aColorInput.r() = (Standard_ShortReal )Draw::Atof (theArgs[anArgIter++]);
120 aColorInput.g() = (Standard_ShortReal )Draw::Atof (theArgs[anArgIter++]);
121 aColorInput.b() = (Standard_ShortReal )Draw::Atof (theArgs[anArgIter++]);
124 Image_PixMap anImage;
125 aView3d->ToPixMap (anImage, aWindowSizeX, aWindowSizeY);
126 const Handle(TColStd_HSequenceOfReal) aSeq = GetColorOfPixel (anImage, aPickCoordX, aPickCoordY, aRadius);
127 cout << "Length = " << aSeq->Length() << endl;
129 Image_ColorRGBF aColorPicked = {{ 0.0f, 0.0f, 0.0f }};
130 Standard_Boolean isNotEqual = Standard_True;
131 for (Standard_Integer i = 1; i <= aSeq->Length(); i += 3)
133 aColorPicked.r() = (Standard_ShortReal )aSeq->Value (i + 0);
134 aColorPicked.g() = (Standard_ShortReal )aSeq->Value (i + 1);
135 aColorPicked.b() = (Standard_ShortReal )aSeq->Value (i + 2);
137 if (theArgsNb == 3 ||
138 ((Abs (aColorPicked.r() - aColorInput.r()) <= Precision::Confusion())
139 && (Abs (aColorPicked.g() - aColorInput.g()) <= Precision::Confusion())
140 && (Abs (aColorPicked.b() - aColorInput.b()) <= Precision::Confusion())))
142 isNotEqual = Standard_False;
147 theDi << "RED : " << aColorPicked.r() << " "
148 << "GREEN : " << aColorPicked.g() << " "
149 << "BLUE : " << aColorPicked.b() << "\n";
153 theDi << "User color: \n"
154 << "RED : " << aColorInput.r() << " "
155 << "GREEN : " << aColorInput.g() << " "
156 << "BLUE : " << aColorInput.b() << "\n";
161 theDi << "Faulty : colors are not equal.\n";
162 return 1; // TCL_ERROR
167 //=======================================================================
169 extern ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
170 extern Handle(AIS_InteractiveContext)& TheAISContext();
172 Standard_EXPORT ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
173 Standard_EXPORT Handle(AIS_InteractiveContext)& TheAISContext();
175 #include <ViewerTest_DoubleMapOfInteractiveAndName.hxx>
176 #include <AIS_Trihedron.hxx>
177 #include <AIS_Axis.hxx>
178 #include <Geom_Line.hxx>
179 #include <AIS_Line.hxx>
181 //==============================================================================
182 // function : VTrihedronOrigins
184 // purpose : draws triheron axis origin lines.
185 // Draw arg : vtri_orig trihedron_name
186 //==============================================================================
187 static int VTrihedronOrigins(Draw_Interpretor& di,
188 Standard_Integer argc,
192 di <<"Usage : vtri_orig tri_name"<<"\n";
196 if(TheAISContext().IsNull()){
197 di<<"Make 'vinit' before this method call"<<"\n";
201 //get trihedron from AIS map.
202 TCollection_AsciiString aName(argv[1]);
203 if(!GetMapOfAIS().IsBound2(aName)){
204 di<<"No object named '"<<argv[1]<<"'"<<"\n";
208 Handle(AIS_Trihedron) aTrih =
209 Handle(AIS_Trihedron)::DownCast(GetMapOfAIS().Find2(aName));
211 di<<"Trihedron is not found, try another name"<<"\n";
216 Handle(AIS_Axis) XAxis = aTrih->XAxis();
217 Handle(AIS_Axis) YAxis = aTrih->YAxis();
218 Handle(AIS_Axis) ZAxis = aTrih->Axis();
220 //get geometrical lines
221 Handle(Geom_Line) XGLine = XAxis->Component();
222 Handle(Geom_Line) YGLine = YAxis->Component();
223 Handle(Geom_Line) ZGLine = ZAxis->Component();
226 Handle(AIS_Line) XLine = new AIS_Line(XGLine);
227 Handle(AIS_Line) YLine = new AIS_Line(YGLine);
228 Handle(AIS_Line) ZLine = new AIS_Line(ZGLine);
230 //put them into AIS map:
231 GetMapOfAIS().Bind(XLine,aName+"_X");
232 GetMapOfAIS().Bind(YLine,aName+"_Y");
233 GetMapOfAIS().Bind(ZLine,aName+"_Z");
234 //print names of created objects:
235 di<<argv[1]<<"_X "<<argv[1]<<"_Y "<<argv[1]<<"_Z"<<"\n";
238 TheAISContext()->Display(XLine);
239 TheAISContext()->Display(YLine);
240 TheAISContext()->Display(ZLine);
245 //=======================================================================
246 //function : QATestExtremaSS
248 //=======================================================================
249 static Standard_Integer QATestExtremaSS (Draw_Interpretor& theInterpretor,
250 Standard_Integer theArgNb,
251 const char** theArgs)
256 std::cerr << "Usage: type help " << theArgs[0] << std::endl;
261 TopoDS_Shape aShape = DBRep::Get (theArgs[1]);
264 std::cerr << "Error: " << theArgs[1] << " shape is null\n";
269 const Standard_Real aStep = Draw::Atof (theArgs[2]);
272 std::cerr << "Error: Step " << aStep << " is too small\n";
276 Extrema_ExtFlag aFlag = Extrema_ExtFlag_MIN;
279 Standard_Integer aVal = Draw::Atoi (theArgs[3]);
282 aFlag = aVal == 1 ? Extrema_ExtFlag_MAX : Extrema_ExtFlag_MINMAX;
286 // Get bounding box of the shape
288 BRepBndLib::Add (aShape, aBounds);
290 Standard_Real aXmin, aYmin, aZmin;
291 Standard_Real aXmax, aYmax, aZmax;
292 aBounds.Get (aXmin, aYmin, aZmin,
293 aXmax, aYmax, aZmax);
295 const Standard_Real aScaleFactor = 1.5;
296 aXmin *= aScaleFactor;
297 aYmin *= aScaleFactor;
298 aZmin *= aScaleFactor;
299 aXmax *= aScaleFactor;
300 aYmax *= aScaleFactor;
301 aZmax *= aScaleFactor;
303 TopTools_SequenceOfShape aList;
304 TColgp_SequenceOfXYZ aPoints;
305 for (Standard_Real aX = aXmin + 0.5 * aStep; aX < aXmax; aX += aStep)
307 for (Standard_Real aY = aYmin + 0.5 * aStep; aY < aYmax; aY += aStep)
309 aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aX, aY, aZmin)));
310 aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aX, aY, aZmax)));
312 aPoints.Append (gp_XYZ (aX, aY, aZmin));
313 aPoints.Append (gp_XYZ (aX, aY, aZmax));
316 for (Standard_Real aZ = aZmin + 0.5 * aStep; aZ < aZmax; aZ += aStep)
318 aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aX, aYmin, aZ)));
319 aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aX, aYmax, aZ)));
321 aPoints.Append (gp_XYZ (aX, aYmin, aZ));
322 aPoints.Append (gp_XYZ (aX, aYmax, aZ));
326 for (Standard_Real aY = aYmin + 0.5 * aStep; aY < aYmax; aY += aStep)
328 for (Standard_Real aZ = aZmin + 0.5 * aStep; aZ < aZmax; aZ += aStep)
330 aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aXmin, aY, aZ)));
331 aList.Append (BRepBuilderAPI_MakeVertex (gp_Pnt (aXmax, aY, aZ)));
333 aPoints.Append (gp_XYZ (aXmin, aY, aZ));
334 aPoints.Append (gp_XYZ (aXmax, aY, aZ));
338 const Standard_Integer aNbPoints = aList.Length();
339 theInterpretor << "Number of sampled points: " << aNbPoints << "\n";
341 // Perform projection of points
345 // Perform projection using standard method
346 BRepExtrema_DistShapeShape aTool;
347 aTool.SetFlag (aFlag);
348 aTool.LoadS1 (aShape);
349 for (Standard_Integer anIdx = 1; anIdx <= aNbPoints; ++anIdx)
351 aTool.LoadS2 (aList (anIdx));
356 theInterpretor << "Test for gradient descent complete in: " << aTimer.ElapsedTime() << "\n";
360 //=======================================================================
361 //function : CommonCommands
363 //=======================================================================
364 void QADraw::CommonCommands (Draw_Interpretor& theCommands)
366 const char* group = "QA_Commands";
368 theCommands.Add ("QAGetPixelColor",
369 "QAGetPixelColor coordinate_X coordinate_Y [color_R color_G color_B]",
374 theCommands.Add ("vtri_orig",
375 "vtri_orig : vtri_orig trihedron_name - draws axis origin lines",
380 theCommands.Add ("QATestExtremaSS",
381 "QATestExtremaSS Shape Step [Flag { MIN = 0 | MAX = 1 | MINMAX = 2 }]",
386 // adding commands "rename" leads to the fact that QA commands doesn't work properly OCC23410, use function "renamevar"
387 // theCommands.Add("rename","rename name1 toname1 name2 toname2 ...",__FILE__,QArename,group);
390 //==============================================================================
392 //==============================================================================
393 void QADraw::Factory(Draw_Interpretor& theCommands)
395 // definition of QA Command
396 QADraw::CommonCommands(theCommands);
397 QADraw::AdditionalCommands(theCommands);
400 // Declare entry point PLUGINFACTORY