0024428: Implementation of LGPL license
[occt.git] / src / QADraw / QADraw.cxx
CommitLineData
b311480e 1// Created on: 2002-02-04
2// Created by: QA Admin
973c2be1 3// Copyright (c) 2002-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
973c2be1 7// This library is free software; you can redistribute it and / or modify it
8// under the terms of the GNU Lesser General Public 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
7fd59977 16#include <QADraw.hxx>
7fd59977 17#include <Draw_Interpretor.hxx>
692613e5 18#include <Image_AlienPixMap.hxx>
7fd59977 19#include <V3d_View.hxx>
20#include <ViewerTest.hxx>
21#include <ViewerTest_EventManager.hxx>
22#include <TColStd_StackOfInteger.hxx>
23#include <TColStd_ListOfInteger.hxx>
24#include <TColStd_ListIteratorOfListOfInteger.hxx>
25#include <TColStd_HSequenceOfReal.hxx>
26#include <AIS_InteractiveContext.hxx>
91322f44 27#include <Draw.hxx>
7fd59977 28#include <Draw_Window.hxx>
29#include <Draw_Viewer.hxx>
30#include <Aspect_WindowDriver.hxx>
31#include <stdio.h>
dc3fe572 32#include <Aspect_DisplayConnection.hxx>
33#include <Graphic3d.hxx>
87225ffd 34#include <Aspect_Window.hxx>
7fd59977 35
87225ffd 36#if defined(_WIN32) || defined(__WIN32__)
37# include <windows.h>
38# include <io.h>
7fd59977 39#else
87225ffd 40# include <unistd.h>
7fd59977 41#endif
42
7fd59977 43#include <Draw_PluginMacro.hxx>
44
692613e5 45Handle(TColStd_HSequenceOfReal) GetColorOfPixel (const Image_PixMap& theImage,
7fd59977 46 const Standard_Integer theCoordinateX,
47 const Standard_Integer theCoordinateY,
48 const Standard_Integer theRadius)
49{
50 Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal();
692613e5 51 if (theImage.IsEmpty()) {
7fd59977 52 std::cerr << "The image is null.\n";
53 return aSeq;
54 }
692613e5 55 Standard_Integer aWidth = (Standard_Integer )theImage.SizeX();
56 Standard_Integer anHeight = (Standard_Integer )theImage.SizeY();
7fd59977 57
58 Quantity_Color aColorTmp;
59 for (Standard_Integer anXIter = theCoordinateX - theRadius;
60 anXIter <= theCoordinateX + theRadius; ++anXIter)
61 {
62 if (anXIter < 0 || anXIter >= aWidth)
63 {
64 continue;
65 }
66 for (Standard_Integer anYIter = theCoordinateY - theRadius;
67 anYIter <= theCoordinateY + theRadius; ++anYIter)
68 {
69 if (anYIter < 0 || anYIter >= anHeight)
70 {
71 continue;
72 }
73 // Image_PixMap stores image upside-down in memory!
692613e5 74 aColorTmp = theImage.PixelColor (anXIter, anYIter);
7fd59977 75 aSeq->Append (aColorTmp.Red());
76 aSeq->Append (aColorTmp.Green());
77 aSeq->Append (aColorTmp.Blue());
78 }
79 }
80 return aSeq;
81}
82
792c785c 83static Standard_Integer QAAISGetPixelColor (Draw_Interpretor& theDi,
84 Standard_Integer theArgsNb,
85 const char** theArgs)
7fd59977 86{
128cc8df 87 if (theArgsNb != 3 && theArgsNb != 6)
88 {
89 theDi << "Usage : " << theArgs[0] << " coordinate_X coordinate_Y [color_R color_G color_B]" << "\n";
90 return 1; // TCL_ERROR
91 }
92
93 Handle(V3d_View) aView3d = ViewerTest::CurrentView();
94 if (aView3d.IsNull())
95 {
96 theDi << "You must initialize AISViewer before this command.\n";
97 return 1; // TCL_ERROR
98 }
99
100 const Handle(Aspect_Window) anAISWindow = aView3d->Window();
101 Standard_Integer aWindowSizeX = 0;
102 Standard_Integer aWindowSizeY = 0;
103 anAISWindow->Size (aWindowSizeX, aWindowSizeY);
104
105 Standard_Integer anArgIter = 1;
91322f44 106 const Standard_Integer aPickCoordX = Draw::Atoi (theArgs[anArgIter++]);
107 const Standard_Integer aPickCoordY = Draw::Atoi (theArgs[anArgIter++]);
128cc8df 108 const Standard_Integer aRadius = (theArgsNb == 3) ? 0 : 1;
109
110 Image_ColorRGBF aColorInput = {{ 0.0f, 0.0f, 0.0f }};
111 if (theArgsNb == 6)
112 {
91322f44 113 aColorInput.r() = (Standard_ShortReal )Draw::Atof (theArgs[anArgIter++]);
114 aColorInput.g() = (Standard_ShortReal )Draw::Atof (theArgs[anArgIter++]);
115 aColorInput.b() = (Standard_ShortReal )Draw::Atof (theArgs[anArgIter++]);
128cc8df 116 }
117
118 Image_PixMap anImage;
119 aView3d->ToPixMap (anImage, aWindowSizeX, aWindowSizeY);
120 const Handle(TColStd_HSequenceOfReal) aSeq = GetColorOfPixel (anImage, aPickCoordX, aPickCoordY, aRadius);
121 cout << "Length = " << aSeq->Length() << endl;
122
123 Image_ColorRGBF aColorPicked = {{ 0.0f, 0.0f, 0.0f }};
124 Standard_Boolean isNotEqual = Standard_True;
125 for (Standard_Integer i = 1; i <= aSeq->Length(); i += 3)
126 {
127 aColorPicked.r() = (Standard_ShortReal )aSeq->Value (i + 0);
128 aColorPicked.g() = (Standard_ShortReal )aSeq->Value (i + 1);
129 aColorPicked.b() = (Standard_ShortReal )aSeq->Value (i + 2);
130
131 if (theArgsNb == 3 ||
132 ((Abs (aColorPicked.r() - aColorInput.r()) <= Precision::Confusion())
133 && (Abs (aColorPicked.g() - aColorInput.g()) <= Precision::Confusion())
134 && (Abs (aColorPicked.b() - aColorInput.b()) <= Precision::Confusion())))
135 {
136 isNotEqual = Standard_False;
137 break;
138 }
139 }
140
141 theDi << "RED : " << aColorPicked.r() << " "
142 << "GREEN : " << aColorPicked.g() << " "
143 << "BLUE : " << aColorPicked.b() << "\n";
144
145 if (theArgsNb == 6)
146 {
147 theDi << "User color: \n"
148 << "RED : " << aColorInput.r() << " "
149 << "GREEN : " << aColorInput.g() << " "
150 << "BLUE : " << aColorInput.b() << "\n";
151 }
152
153 if (isNotEqual)
154 {
155 theDi << "Faulty : colors are not equal.\n";
156 return 1; // TCL_ERROR
157 }
158 return 0;
7fd59977 159}
160
7fd59977 161//=======================================================================
7fd59977 162#if ! defined(WNT)
163extern ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
164extern Handle(AIS_InteractiveContext)& TheAISContext();
165#else
166Standard_EXPORT ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
167Standard_EXPORT Handle(AIS_InteractiveContext)& TheAISContext();
168#endif
169#include <ViewerTest_DoubleMapOfInteractiveAndName.hxx>
170#include <AIS_Trihedron.hxx>
171#include <AIS_Axis.hxx>
172#include <Geom_Line.hxx>
173#include <AIS_Line.hxx>
174
175//==============================================================================
176// function : VTrihedronOrigins
177// author : ota
178// purpose : draws triheron axis origin lines.
179// Draw arg : vtri_orig trihedron_name
180//==============================================================================
181static int VTrihedronOrigins(Draw_Interpretor& di,
182 Standard_Integer argc,
183 const char ** argv)
184{
185 if(argc != 2){
186 di <<"Usage : vtri_orig tri_name"<<"\n";
187 return 1;
188 }
189
190 if(TheAISContext().IsNull()){
191 di<<"Make 'vinit' before this method call"<<"\n";
192 return 1;
193 }
194
195 //get trihedron from AIS map.
196 TCollection_AsciiString aName(argv[1]);
197 if(!GetMapOfAIS().IsBound2(aName)){
198 di<<"No object named '"<<argv[1]<<"'"<<"\n";
199 return 1;
200 }
201
202 Handle(AIS_Trihedron) aTrih =
203 Handle(AIS_Trihedron)::DownCast(GetMapOfAIS().Find2(aName));
204 if(aTrih.IsNull()){
205 di<<"Trihedron is not found, try another name"<<"\n";
206 return 1;
207 }
208
209 //get axis
210 Handle(AIS_Axis) XAxis = aTrih->XAxis();
211 Handle(AIS_Axis) YAxis = aTrih->YAxis();
212 Handle(AIS_Axis) ZAxis = aTrih->Axis();
213
214 //get geometrical lines
215 Handle(Geom_Line) XGLine = XAxis->Component();
216 Handle(Geom_Line) YGLine = YAxis->Component();
217 Handle(Geom_Line) ZGLine = ZAxis->Component();
218
219 //make AIS_Lines
220 Handle(AIS_Line) XLine = new AIS_Line(XGLine);
221 Handle(AIS_Line) YLine = new AIS_Line(YGLine);
222 Handle(AIS_Line) ZLine = new AIS_Line(ZGLine);
223
224 //put them into AIS map:
225 GetMapOfAIS().Bind(XLine,aName+"_X");
226 GetMapOfAIS().Bind(YLine,aName+"_Y");
227 GetMapOfAIS().Bind(ZLine,aName+"_Z");
228 //print names of created objects:
229 di<<argv[1]<<"_X "<<argv[1]<<"_Y "<<argv[1]<<"_Z"<<"\n";
230
231 //try to draw them:
232 TheAISContext()->Display(XLine);
233 TheAISContext()->Display(YLine);
234 TheAISContext()->Display(ZLine);
235
236 return 0;
237}
238
7fd59977 239void QADraw::CommonCommands(Draw_Interpretor& theCommands)
240{
7fd59977 241 const char* group = "QA_Commands";
242
7fd59977 243 theCommands.Add("QAGetPixelColor", "QAGetPixelColor coordinate_X coordinate_Y [color_R color_G color_B]", __FILE__,QAAISGetPixelColor, group);
7fd59977 244 theCommands.Add("vtri_orig",
245 "vtri_orig : vtri_orig trihedron_name - draws axis origin lines",
246 __FILE__,VTrihedronOrigins,group);
7fd59977 247
319e4241 248// adding commands "rename" leads to the fact that QA commands doesn't work properly OCC23410, use function "renamevar"
249// theCommands.Add("rename","rename name1 toname1 name2 toname2 ...",__FILE__,QArename,group);
7fd59977 250}
7fd59977 251
7fd59977 252//==============================================================================
253// QADraw::Factory
254//==============================================================================
255void QADraw::Factory(Draw_Interpretor& theCommands)
256{
7fd59977 257 // definition of QA Command
258 QADraw::CommonCommands(theCommands);
259 QADraw::AdditionalCommands(theCommands);
7fd59977 260}
261
262// Declare entry point PLUGINFACTORY
263DPLUGIN(QADraw)