0029754: Coding Rules - replace Standard_Integer with Graphic3d_ZLayerId for consistency
[occt.git] / src / V3d / V3d_Viewer.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
6a24c6de 14#include <V3d_Viewer.hxx>
42cf5bc1 15
42cf5bc1 16#include <Aspect_Grid.hxx>
c357e426 17#include <Aspect_IdentDefinitionError.hxx>
67b3d2a8 18#include <Graphic3d_ArrayOfSegments.hxx>
19#include <Graphic3d_AspectLine3d.hxx>
42cf5bc1 20#include <Graphic3d_AspectMarker3d.hxx>
67b3d2a8 21#include <Graphic3d_AspectText3d.hxx>
42cf5bc1 22#include <Graphic3d_GraphicDriver.hxx>
23#include <Graphic3d_Group.hxx>
24#include <Graphic3d_Structure.hxx>
c357e426 25#include <Standard_ErrorHandler.hxx>
42cf5bc1 26#include <Standard_Type.hxx>
7fd59977 27#include <V3d.hxx>
6942f04a 28#include <V3d_BadValue.hxx>
42cf5bc1 29#include <V3d_CircularGrid.hxx>
6a24c6de 30#include <V3d_AmbientLight.hxx>
31#include <V3d_DirectionalLight.hxx>
42cf5bc1 32#include <V3d_RectangularGrid.hxx>
33#include <V3d_View.hxx>
7fd59977 34
6a24c6de 35IMPLEMENT_STANDARD_RTTIEXT(V3d_Viewer, Standard_Transient)
36
37// ========================================================================
38// function : V3d_Viewer
39// purpose :
40// ========================================================================
41V3d_Viewer::V3d_Viewer (const Handle(Graphic3d_GraphicDriver)& theDriver)
42: myDriver (theDriver),
43 myStructureManager (new Graphic3d_StructureManager (theDriver)),
44 myZLayerGenId (1, IntegerLast()),
45 myBackground (Quantity_NOC_GRAY30),
46 myViewSize (1000.0),
47 myViewProj (V3d_XposYnegZpos),
48 myVisualization (V3d_ZBUFFER),
dc89236f 49 myShadingModel (Graphic3d_TOSM_VERTEX),
6a24c6de 50 myDefaultTypeOfView (V3d_ORTHOGRAPHIC),
51 myComputedMode (Standard_True),
52 myDefaultComputedMode (Standard_False),
53 myPrivilegedPlane (gp_Ax3 (gp_Pnt (0.,0.,0), gp_Dir (0.,0.,1.), gp_Dir (1.,0.,0.))),
54 myDisplayPlane (Standard_False),
55 myDisplayPlaneLength (1000.0),
56 myGridType (Aspect_GT_Rectangular),
57 myGridEcho (Standard_True),
58 myGridEchoLastVert (ShortRealLast(), ShortRealLast(), ShortRealLast())
59{
60 myRGrid = new V3d_RectangularGrid (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
61 myCGrid = new V3d_CircularGrid (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
62}
92efcf78 63
c357e426 64// ========================================================================
65// function : V3d_Viewer
66// purpose :
67// ========================================================================
6942f04a 68V3d_Viewer::V3d_Viewer (const Handle(Graphic3d_GraphicDriver)& theDriver,
6a24c6de 69 const Standard_ExtString ,
70 const Standard_CString ,
dc89236f 71 const Standard_Real theViewSize,
72 const V3d_TypeOfOrientation theViewProj,
73 const Quantity_Color& theViewBackground,
74 const V3d_TypeOfVisualization theVisualization,
75 const Graphic3d_TypeOfShadingModel theShadingModel,
76 const Standard_Boolean theComputedMode,
77 const Standard_Boolean theDefaultComputedMode)
6a24c6de 78: myDriver (theDriver),
79 myStructureManager (new Graphic3d_StructureManager (theDriver)),
80 myZLayerGenId (1, IntegerLast()),
81 myBackground (theViewBackground),
82 myViewSize (theViewSize),
83 myViewProj (theViewProj),
84 myVisualization (theVisualization),
85 myShadingModel (theShadingModel),
86 myDefaultTypeOfView (V3d_ORTHOGRAPHIC),
87 myComputedMode (theComputedMode),
88 myDefaultComputedMode (theDefaultComputedMode),
89 myPrivilegedPlane (gp_Ax3 (gp_Pnt (0.,0.,0), gp_Dir (0.,0.,1.), gp_Dir (1.,0.,0.))),
90 myDisplayPlane (Standard_False),
91 myDisplayPlaneLength (theViewSize),
92 myGridType (Aspect_GT_Rectangular),
93 myGridEcho (Standard_True),
94 myGridEchoLastVert (ShortRealLast(), ShortRealLast(), ShortRealLast())
95{
96 myRGrid = new V3d_RectangularGrid (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
97 myCGrid = new V3d_CircularGrid (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
6942f04a 98 SetDefaultViewSize (theViewSize);
7fd59977 99}
100
c357e426 101// ========================================================================
102// function : CreateView
103// purpose :
104// ========================================================================
b5ac8292 105Handle(V3d_View) V3d_Viewer::CreateView ()
106{
6a24c6de 107 return new V3d_View(this, myDefaultTypeOfView);
7fd59977 108}
109
c357e426 110// ========================================================================
111// function : SetViewOn
112// purpose :
113// ========================================================================
114void V3d_Viewer::SetViewOn()
115{
6a24c6de 116 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
c357e426 117 {
6a24c6de 118 SetViewOn (aDefViewIter.Value());
c357e426 119 }
7fd59977 120}
121
c357e426 122// ========================================================================
123// function : SetViewOff
124// purpose :
125// ========================================================================
126void V3d_Viewer::SetViewOff()
127{
6a24c6de 128 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
c357e426 129 {
6a24c6de 130 SetViewOff (aDefViewIter.Value());
c357e426 131 }
7fd59977 132}
133
c357e426 134// ========================================================================
135// function : SetViewOn
136// purpose :
137// ========================================================================
138void V3d_Viewer::SetViewOn (const Handle(V3d_View)& theView)
139{
140 Handle(Graphic3d_CView) aViewImpl = theView->View();
6a24c6de 141 if (!aViewImpl->IsDefined() || myActiveViews.Contains (theView))
c357e426 142 {
6a24c6de 143 return;
144 }
c357e426 145
6a24c6de 146 myActiveViews.Append (theView);
147 aViewImpl->Activate();
148 for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
149 {
150 theView->SetLightOn (anActiveLightIter.Value());
7fd59977 151 }
6a24c6de 152
153 theView->SetGrid (myPrivilegedPlane, Grid ());
154 theView->SetGridActivity (Grid ()->IsActive ());
155 theView->Redraw();
7fd59977 156}
157
c357e426 158// ========================================================================
159// function : SetViewOff
160// purpose :
161// ========================================================================
162void V3d_Viewer::SetViewOff (const Handle(V3d_View)& theView)
163{
164 Handle(Graphic3d_CView) aViewImpl = theView->View();
6a24c6de 165 if (aViewImpl->IsDefined() && myActiveViews.Contains (theView))
c357e426 166 {
6a24c6de 167 myActiveViews.Remove (theView);
c357e426 168 aViewImpl->Deactivate() ;
7fd59977 169 }
170}
171
c357e426 172// ========================================================================
c357e426 173// function : Redraw
174// purpose :
175// ========================================================================
6a24c6de 176void V3d_Viewer::Redraw() const
679ecdee 177{
6a24c6de 178 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
c357e426 179 {
6a24c6de 180 aDefViewIter.Value()->Redraw();
c357e426 181 }
7fd59977 182}
183
c357e426 184// ========================================================================
185// function : RedrawImmediate
186// purpose :
187// ========================================================================
679ecdee 188void V3d_Viewer::RedrawImmediate() const
189{
6a24c6de 190 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
c357e426 191 {
6a24c6de 192 aDefViewIter.Value()->RedrawImmediate();
c357e426 193 }
679ecdee 194}
195
c357e426 196// ========================================================================
197// function : Invalidate
198// purpose :
199// ========================================================================
679ecdee 200void V3d_Viewer::Invalidate() const
201{
6a24c6de 202 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
c357e426 203 {
6a24c6de 204 aDefViewIter.Value()->Invalidate();
c357e426 205 }
679ecdee 206}
207
c357e426 208// ========================================================================
209// function : Remove
210// purpose :
211// ========================================================================
212void V3d_Viewer::Remove()
213{
214 myStructureManager->Remove();
7fd59977 215}
216
c357e426 217// ========================================================================
218// function : Erase
219// purpose :
220// ========================================================================
221void V3d_Viewer::Erase() const
222{
223 myStructureManager->Erase();
7fd59977 224}
225
c357e426 226// ========================================================================
227// function : UnHighlight
228// purpose :
229// ========================================================================
230void V3d_Viewer::UnHighlight() const
231{
232 myStructureManager->UnHighlight();
7fd59977 233}
234
6a24c6de 235void V3d_Viewer::SetDefaultViewSize (const Standard_Real theSize)
b8ddfc2f 236{
9775fa61 237 if (theSize <= 0.0)
238 throw V3d_BadValue("V3d_Viewer::SetDefaultViewSize, bad size");
6a24c6de 239 myViewSize = theSize;
7fd59977 240}
241
c357e426 242// ========================================================================
6a24c6de 243// function : IfMoreViews
c357e426 244// purpose :
245// ========================================================================
6a24c6de 246Standard_Boolean V3d_Viewer::IfMoreViews() const
c357e426 247{
6a24c6de 248 return myDefinedViews.Size() < myStructureManager->MaxNumOfViews();
7fd59977 249}
250
6a24c6de 251// ========================================================================
252// function : AddView
253// purpose :
254// ========================================================================
255void V3d_Viewer::AddView (const Handle(V3d_View)& theView)
256{
257 if (!myDefinedViews.Contains (theView))
258 {
259 myDefinedViews.Append (theView);
260 }
7fd59977 261}
262
6a24c6de 263// ========================================================================
264// function : DelView
265// purpose :
266// ========================================================================
267void V3d_Viewer::DelView (const Handle(V3d_View)& theView)
268{
269 myActiveViews.Remove (theView);
270 myDefinedViews.Remove (theView);
7fd59977 271}
272
59f45b7c 273//=======================================================================
c357e426 274//function : AddZLayer
c5751993 275//purpose :
276//=======================================================================
8f138407 277Standard_Boolean V3d_Viewer::AddZLayer (Graphic3d_ZLayerId& theLayerId)
c5751993 278{
c357e426 279 try
280 {
281 OCC_CATCH_SIGNALS
282 theLayerId = myZLayerGenId.Next();
283 }
284 catch (Aspect_IdentDefinitionError)
285 {
286 // new index can't be generated
287 return Standard_False;
288 }
289
290 myLayerIds.Add (theLayerId);
291 myDriver->AddZLayer (theLayerId);
292
293 return Standard_True;
c5751993 294}
295
296//=======================================================================
c357e426 297//function : RemoveZLayer
298//purpose :
c5751993 299//=======================================================================
8f138407 300Standard_Boolean V3d_Viewer::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
c5751993 301{
c357e426 302 if (!myLayerIds.Contains (theLayerId)
303 || theLayerId < myZLayerGenId.Lower()
304 || theLayerId > myZLayerGenId.Upper())
305 {
306 return Standard_False;
307 }
308
309 myDriver->RemoveZLayer (theLayerId);
310 myLayerIds.Remove (theLayerId);
311 myZLayerGenId.Free (theLayerId);
312
313 return Standard_True;
c5751993 314}
315
316//=======================================================================
c357e426 317//function : GetAllZLayers
59f45b7c 318//purpose :
319//=======================================================================
c357e426 320void V3d_Viewer::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
59f45b7c 321{
c357e426 322 myDriver->ZLayers (theLayerSeq);
59f45b7c 323}
324
325//=======================================================================
c357e426 326//function : SetZLayerSettings
327//purpose :
59f45b7c 328//=======================================================================
8f138407 329void V3d_Viewer::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId, const Graphic3d_ZLayerSettings& theSettings)
59f45b7c 330{
c357e426 331 myDriver->SetZLayerSettings (theLayerId, theSettings);
59f45b7c 332}
333
334//=======================================================================
c357e426 335//function : ZLayerSettings
59f45b7c 336//purpose :
337//=======================================================================
8f138407 338Graphic3d_ZLayerSettings V3d_Viewer::ZLayerSettings (const Graphic3d_ZLayerId theLayerId)
59f45b7c 339{
c357e426 340 return myDriver->ZLayerSettings (theLayerId);
59f45b7c 341}
6942f04a 342
343//=======================================================================
6a24c6de 344//function : UpdateLights
345//purpose :
346//=======================================================================
347void V3d_Viewer::UpdateLights()
348{
349 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
350 {
351 anActiveViewIter.Value()->UpdateLights();
352 }
353}
354
355//=======================================================================
356//function : SetLightOn
6942f04a 357//purpose :
358//=======================================================================
6a24c6de 359void V3d_Viewer::SetLightOn (const Handle(V3d_Light)& theLight)
6942f04a 360{
6a24c6de 361 if (!myActiveLights.Contains (theLight))
362 {
363 myActiveLights.Append (theLight);
364 }
365
366 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
367 {
368 anActiveViewIter.Value()->SetLightOn (theLight);
369 }
6942f04a 370}
371
372//=======================================================================
6a24c6de 373//function : SetLightOff
6942f04a 374//purpose :
375//=======================================================================
6a24c6de 376void V3d_Viewer::SetLightOff (const Handle(V3d_Light)& theLight)
6942f04a 377{
6a24c6de 378 myActiveLights.Remove (theLight);
379 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
380 {
381 anActiveViewIter.Value()->SetLightOff (theLight);
382 }
6942f04a 383}
384
385//=======================================================================
6a24c6de 386//function : SetLightOn
6942f04a 387//purpose :
388//=======================================================================
6a24c6de 389void V3d_Viewer::SetLightOn()
6942f04a 390{
6a24c6de 391 for (V3d_ListOfLight::Iterator aDefLightIter (myDefinedLights); aDefLightIter.More(); aDefLightIter.Next())
392 {
393 if (!myActiveLights.Contains (aDefLightIter.Value()))
394 {
395 myActiveLights.Append (aDefLightIter.Value());
396 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
397 {
398 anActiveViewIter.Value()->SetLightOn (aDefLightIter.Value());
399 }
400 }
401 }
6942f04a 402}
403
404//=======================================================================
6a24c6de 405//function : SetLightOff
6942f04a 406//purpose :
407//=======================================================================
6a24c6de 408void V3d_Viewer::SetLightOff()
6942f04a 409{
6a24c6de 410 for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
411 {
412 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
413 {
414 anActiveViewIter.Value()->SetLightOff (anActiveLightIter.Value());
415 }
416 }
417 myActiveLights.Clear();
973c2be1 418}
832ae82d 419
420//=======================================================================
6a24c6de 421//function : IsGlobalLight
832ae82d 422//purpose :
423//=======================================================================
6a24c6de 424Standard_Boolean V3d_Viewer::IsGlobalLight (const Handle(V3d_Light)& theLight) const
832ae82d 425{
6a24c6de 426 return myActiveLights.Contains (theLight);
832ae82d 427}
428
429//=======================================================================
6a24c6de 430//function : AddLight
832ae82d 431//purpose :
432//=======================================================================
6a24c6de 433void V3d_Viewer::AddLight (const Handle(V3d_Light)& theLight)
832ae82d 434{
6a24c6de 435 if (!myDefinedLights.Contains (theLight))
436 {
437 myDefinedLights.Append (theLight);
438 }
439}
440
441//=======================================================================
442//function : DelLight
443//purpose :
444//=======================================================================
445void V3d_Viewer::DelLight (const Handle(V3d_Light)& theLight)
446{
447 SetLightOff (theLight);
448 myDefinedLights.Remove (theLight);
449}
450
451//=======================================================================
452//function : SetDefaultLights
453//purpose :
454//=======================================================================
455void V3d_Viewer::SetDefaultLights()
456{
457 while (!myDefinedLights.IsEmpty())
458 {
459 Handle(V3d_Light) aLight = myDefinedLights.First();
460 DelLight (aLight);
461 }
462
992ed6b3 463 Handle(V3d_DirectionalLight) aDirLight = new V3d_DirectionalLight (V3d_Zneg, Quantity_NOC_WHITE, Standard_True);
464 Handle(V3d_AmbientLight) anAmbLight = new V3d_AmbientLight (Quantity_NOC_WHITE);
465 AddLight (aDirLight);
466 AddLight (anAmbLight);
467 SetLightOn (aDirLight);
468 SetLightOn (anAmbLight);
832ae82d 469}
67b3d2a8 470
471//=======================================================================
472//function : SetPrivilegedPlane
473//purpose :
474//=======================================================================
475void V3d_Viewer::SetPrivilegedPlane (const gp_Ax3& thePlane)
476{
477 myPrivilegedPlane = thePlane;
478 Grid()->SetDrawMode(Grid()->DrawMode());
479 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
480 {
481 anActiveViewIter.Value()->SetGrid (myPrivilegedPlane, Grid());
482 }
483
484 if (myDisplayPlane)
485 {
486 DisplayPrivilegedPlane (Standard_True, myDisplayPlaneLength);
487 }
488}
489
490//=======================================================================
491//function : DisplayPrivilegedPlane
492//purpose :
493//=======================================================================
494void V3d_Viewer::DisplayPrivilegedPlane (const Standard_Boolean theOnOff, const Standard_Real theSize)
495{
496 myDisplayPlane = theOnOff;
497 myDisplayPlaneLength = theSize;
498
499 if (!myDisplayPlane)
500 {
501 if (!myPlaneStructure.IsNull())
502 {
503 myPlaneStructure->Erase();
504 }
505 return;
506 }
507
508 if (myPlaneStructure.IsNull())
509 {
510 myPlaneStructure = new Graphic3d_Structure (StructureManager());
511 myPlaneStructure->SetInfiniteState (Standard_True);
512 myPlaneStructure->Display();
513 }
514 else
515 {
516 myPlaneStructure->Clear();
517 }
518
519 Handle(Graphic3d_Group) aGroup = myPlaneStructure->NewGroup();
520
521 Handle(Graphic3d_AspectLine3d) aLineAttrib = new Graphic3d_AspectLine3d (Quantity_NOC_GRAY60, Aspect_TOL_SOLID, 1.0);
522 aGroup->SetGroupPrimitivesAspect (aLineAttrib);
523
524 Handle(Graphic3d_AspectText3d) aTextAttrib = new Graphic3d_AspectText3d();
525 aTextAttrib->SetColor (Quantity_Color (Quantity_NOC_ROYALBLUE1));
526 aGroup->SetGroupPrimitivesAspect (aTextAttrib);
527
528 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments (6);
529
530 const gp_Pnt& p0 = myPrivilegedPlane.Location();
531
532 const gp_Pnt pX (p0.XYZ() + myDisplayPlaneLength * myPrivilegedPlane.XDirection().XYZ());
533 aPrims->AddVertex (p0);
534 aPrims->AddVertex (pX);
535 aGroup->Text ("X", Graphic3d_Vertex (pX.X(), pX.Y(), pX.Z()), 1.0 / 81.0);
536
537 const gp_Pnt pY (p0.XYZ() + myDisplayPlaneLength * myPrivilegedPlane.YDirection().XYZ());
538 aPrims->AddVertex (p0);
539 aPrims->AddVertex (pY);
540 aGroup->Text ("Y", Graphic3d_Vertex (pY.X(), pY.Y(), pY.Z()), 1.0 / 81.0);
541
542 const gp_Pnt pZ (p0.XYZ() + myDisplayPlaneLength * myPrivilegedPlane.Direction().XYZ());
543 aPrims->AddVertex (p0);
544 aPrims->AddVertex (pZ);
545 aGroup->Text ("Z", Graphic3d_Vertex (pZ.X(), pZ.Y(), pZ.Z()), 1.0 / 81.0);
546
547 aGroup->AddPrimitiveArray (aPrims);
548
549 myPlaneStructure->Display();
550}