1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <V3d_Viewer.hxx>
16 #include <Aspect_Grid.hxx>
17 #include <Aspect_IdentDefinitionError.hxx>
18 #include <Graphic3d_ArrayOfSegments.hxx>
19 #include <Graphic3d_AspectLine3d.hxx>
20 #include <Graphic3d_AspectMarker3d.hxx>
21 #include <Graphic3d_AspectText3d.hxx>
22 #include <Graphic3d_GraphicDriver.hxx>
23 #include <Graphic3d_Group.hxx>
24 #include <Graphic3d_Structure.hxx>
25 #include <Standard_ErrorHandler.hxx>
26 #include <Standard_Type.hxx>
28 #include <V3d_BadValue.hxx>
29 #include <V3d_CircularGrid.hxx>
30 #include <V3d_AmbientLight.hxx>
31 #include <V3d_DirectionalLight.hxx>
32 #include <V3d_RectangularGrid.hxx>
33 #include <V3d_View.hxx>
35 IMPLEMENT_STANDARD_RTTIEXT(V3d_Viewer, Standard_Transient)
37 // ========================================================================
38 // function : V3d_Viewer
40 // ========================================================================
41 V3d_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),
47 myViewProj (V3d_XposYnegZpos),
48 myVisualization (V3d_ZBUFFER),
49 myShadingModel (Graphic3d_TOSM_VERTEX),
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())
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));
64 // ========================================================================
65 // function : V3d_Viewer
67 // ========================================================================
68 V3d_Viewer::V3d_Viewer (const Handle(Graphic3d_GraphicDriver)& theDriver,
69 const Standard_ExtString ,
70 const Standard_CString ,
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)
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())
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));
98 SetDefaultViewSize (theViewSize);
101 // ========================================================================
102 // function : CreateView
104 // ========================================================================
105 Handle(V3d_View) V3d_Viewer::CreateView ()
107 return new V3d_View(this, myDefaultTypeOfView);
110 // ========================================================================
111 // function : SetViewOn
113 // ========================================================================
114 void V3d_Viewer::SetViewOn()
116 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
118 SetViewOn (aDefViewIter.Value());
122 // ========================================================================
123 // function : SetViewOff
125 // ========================================================================
126 void V3d_Viewer::SetViewOff()
128 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
130 SetViewOff (aDefViewIter.Value());
134 // ========================================================================
135 // function : SetViewOn
137 // ========================================================================
138 void V3d_Viewer::SetViewOn (const Handle(V3d_View)& theView)
140 Handle(Graphic3d_CView) aViewImpl = theView->View();
141 if (!aViewImpl->IsDefined() || myActiveViews.Contains (theView))
146 myActiveViews.Append (theView);
147 aViewImpl->Activate();
148 for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
150 theView->SetLightOn (anActiveLightIter.Value());
153 theView->SetGrid (myPrivilegedPlane, Grid ());
154 theView->SetGridActivity (Grid ()->IsActive ());
155 if (theView->SetImmediateUpdate (Standard_False))
158 theView->SetImmediateUpdate (Standard_True);
162 // ========================================================================
163 // function : SetViewOff
165 // ========================================================================
166 void V3d_Viewer::SetViewOff (const Handle(V3d_View)& theView)
168 Handle(Graphic3d_CView) aViewImpl = theView->View();
169 if (aViewImpl->IsDefined() && myActiveViews.Contains (theView))
171 myActiveViews.Remove (theView);
172 aViewImpl->Deactivate() ;
176 // ========================================================================
179 // ========================================================================
180 void V3d_Viewer::Redraw() const
182 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
184 aDefViewIter.Value()->Redraw();
188 // ========================================================================
189 // function : RedrawImmediate
191 // ========================================================================
192 void V3d_Viewer::RedrawImmediate() const
194 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
196 aDefViewIter.Value()->RedrawImmediate();
200 // ========================================================================
201 // function : Invalidate
203 // ========================================================================
204 void V3d_Viewer::Invalidate() const
206 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
208 aDefViewIter.Value()->Invalidate();
212 // ========================================================================
215 // ========================================================================
216 void V3d_Viewer::Remove()
218 myStructureManager->Remove();
221 // ========================================================================
224 // ========================================================================
225 void V3d_Viewer::Erase() const
227 myStructureManager->Erase();
230 // ========================================================================
231 // function : UnHighlight
233 // ========================================================================
234 void V3d_Viewer::UnHighlight() const
236 myStructureManager->UnHighlight();
239 void V3d_Viewer::SetDefaultViewSize (const Standard_Real theSize)
242 throw V3d_BadValue("V3d_Viewer::SetDefaultViewSize, bad size");
243 myViewSize = theSize;
246 // ========================================================================
247 // function : IfMoreViews
249 // ========================================================================
250 Standard_Boolean V3d_Viewer::IfMoreViews() const
252 return myDefinedViews.Size() < myStructureManager->MaxNumOfViews();
255 // ========================================================================
256 // function : AddView
258 // ========================================================================
259 void V3d_Viewer::AddView (const Handle(V3d_View)& theView)
261 if (!myDefinedViews.Contains (theView))
263 myDefinedViews.Append (theView);
267 // ========================================================================
268 // function : DelView
270 // ========================================================================
271 void V3d_Viewer::DelView (const Handle(V3d_View)& theView)
273 myActiveViews.Remove (theView);
274 myDefinedViews.Remove (theView);
277 //=======================================================================
278 //function : AddZLayer
280 //=======================================================================
281 Standard_Boolean V3d_Viewer::AddZLayer (Graphic3d_ZLayerId& theLayerId)
286 theLayerId = myZLayerGenId.Next();
288 catch (Aspect_IdentDefinitionError)
290 // new index can't be generated
291 return Standard_False;
294 myLayerIds.Add (theLayerId);
295 myDriver->AddZLayer (theLayerId);
297 return Standard_True;
300 //=======================================================================
301 //function : RemoveZLayer
303 //=======================================================================
304 Standard_Boolean V3d_Viewer::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
306 if (!myLayerIds.Contains (theLayerId)
307 || theLayerId < myZLayerGenId.Lower()
308 || theLayerId > myZLayerGenId.Upper())
310 return Standard_False;
313 myDriver->RemoveZLayer (theLayerId);
314 myLayerIds.Remove (theLayerId);
315 myZLayerGenId.Free (theLayerId);
317 return Standard_True;
320 //=======================================================================
321 //function : GetAllZLayers
323 //=======================================================================
324 void V3d_Viewer::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
326 myDriver->ZLayers (theLayerSeq);
329 //=======================================================================
330 //function : SetZLayerSettings
332 //=======================================================================
333 void V3d_Viewer::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId, const Graphic3d_ZLayerSettings& theSettings)
335 myDriver->SetZLayerSettings (theLayerId, theSettings);
338 //=======================================================================
339 //function : ZLayerSettings
341 //=======================================================================
342 Graphic3d_ZLayerSettings V3d_Viewer::ZLayerSettings (const Graphic3d_ZLayerId theLayerId)
344 return myDriver->ZLayerSettings (theLayerId);
347 //=======================================================================
348 //function : UpdateLights
350 //=======================================================================
351 void V3d_Viewer::UpdateLights()
353 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
355 anActiveViewIter.Value()->UpdateLights();
359 //=======================================================================
360 //function : SetLightOn
362 //=======================================================================
363 void V3d_Viewer::SetLightOn (const Handle(V3d_Light)& theLight)
365 if (!myActiveLights.Contains (theLight))
367 myActiveLights.Append (theLight);
370 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
372 anActiveViewIter.Value()->SetLightOn (theLight);
376 //=======================================================================
377 //function : SetLightOff
379 //=======================================================================
380 void V3d_Viewer::SetLightOff (const Handle(V3d_Light)& theLight)
382 myActiveLights.Remove (theLight);
383 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
385 anActiveViewIter.Value()->SetLightOff (theLight);
389 //=======================================================================
390 //function : SetLightOn
392 //=======================================================================
393 void V3d_Viewer::SetLightOn()
395 for (V3d_ListOfLight::Iterator aDefLightIter (myDefinedLights); aDefLightIter.More(); aDefLightIter.Next())
397 if (!myActiveLights.Contains (aDefLightIter.Value()))
399 myActiveLights.Append (aDefLightIter.Value());
400 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
402 anActiveViewIter.Value()->SetLightOn (aDefLightIter.Value());
408 //=======================================================================
409 //function : SetLightOff
411 //=======================================================================
412 void V3d_Viewer::SetLightOff()
414 for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
416 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
418 anActiveViewIter.Value()->SetLightOff (anActiveLightIter.Value());
421 myActiveLights.Clear();
424 //=======================================================================
425 //function : IsGlobalLight
427 //=======================================================================
428 Standard_Boolean V3d_Viewer::IsGlobalLight (const Handle(V3d_Light)& theLight) const
430 return myActiveLights.Contains (theLight);
433 //=======================================================================
434 //function : AddLight
436 //=======================================================================
437 void V3d_Viewer::AddLight (const Handle(V3d_Light)& theLight)
439 if (!myDefinedLights.Contains (theLight))
441 myDefinedLights.Append (theLight);
445 //=======================================================================
446 //function : DelLight
448 //=======================================================================
449 void V3d_Viewer::DelLight (const Handle(V3d_Light)& theLight)
451 SetLightOff (theLight);
452 myDefinedLights.Remove (theLight);
455 //=======================================================================
456 //function : SetDefaultLights
458 //=======================================================================
459 void V3d_Viewer::SetDefaultLights()
461 while (!myDefinedLights.IsEmpty())
463 Handle(V3d_Light) aLight = myDefinedLights.First();
467 Handle(V3d_DirectionalLight) aDirLight = new V3d_DirectionalLight (V3d_Zneg, Quantity_NOC_WHITE, Standard_True);
468 Handle(V3d_AmbientLight) anAmbLight = new V3d_AmbientLight (Quantity_NOC_WHITE);
469 AddLight (aDirLight);
470 AddLight (anAmbLight);
471 SetLightOn (aDirLight);
472 SetLightOn (anAmbLight);
475 //=======================================================================
476 //function : SetPrivilegedPlane
478 //=======================================================================
479 void V3d_Viewer::SetPrivilegedPlane (const gp_Ax3& thePlane)
481 myPrivilegedPlane = thePlane;
482 Grid()->SetDrawMode(Grid()->DrawMode());
483 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
485 anActiveViewIter.Value()->SetGrid (myPrivilegedPlane, Grid());
490 DisplayPrivilegedPlane (Standard_True, myDisplayPlaneLength);
494 //=======================================================================
495 //function : DisplayPrivilegedPlane
497 //=======================================================================
498 void V3d_Viewer::DisplayPrivilegedPlane (const Standard_Boolean theOnOff, const Standard_Real theSize)
500 myDisplayPlane = theOnOff;
501 myDisplayPlaneLength = theSize;
505 if (!myPlaneStructure.IsNull())
507 myPlaneStructure->Erase();
512 if (myPlaneStructure.IsNull())
514 myPlaneStructure = new Graphic3d_Structure (StructureManager());
515 myPlaneStructure->SetInfiniteState (Standard_True);
516 myPlaneStructure->Display();
520 myPlaneStructure->Clear();
523 Handle(Graphic3d_Group) aGroup = myPlaneStructure->NewGroup();
525 Handle(Graphic3d_AspectLine3d) aLineAttrib = new Graphic3d_AspectLine3d (Quantity_NOC_GRAY60, Aspect_TOL_SOLID, 1.0);
526 aGroup->SetGroupPrimitivesAspect (aLineAttrib);
528 Handle(Graphic3d_AspectText3d) aTextAttrib = new Graphic3d_AspectText3d();
529 aTextAttrib->SetColor (Quantity_Color (Quantity_NOC_ROYALBLUE1));
530 aGroup->SetGroupPrimitivesAspect (aTextAttrib);
532 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments (6);
534 const gp_Pnt& p0 = myPrivilegedPlane.Location();
536 const gp_Pnt pX (p0.XYZ() + myDisplayPlaneLength * myPrivilegedPlane.XDirection().XYZ());
537 aPrims->AddVertex (p0);
538 aPrims->AddVertex (pX);
539 aGroup->Text ("X", Graphic3d_Vertex (pX.X(), pX.Y(), pX.Z()), 1.0 / 81.0);
541 const gp_Pnt pY (p0.XYZ() + myDisplayPlaneLength * myPrivilegedPlane.YDirection().XYZ());
542 aPrims->AddVertex (p0);
543 aPrims->AddVertex (pY);
544 aGroup->Text ("Y", Graphic3d_Vertex (pY.X(), pY.Y(), pY.Z()), 1.0 / 81.0);
546 const gp_Pnt pZ (p0.XYZ() + myDisplayPlaneLength * myPrivilegedPlane.Direction().XYZ());
547 aPrims->AddVertex (p0);
548 aPrims->AddVertex (pZ);
549 aGroup->Text ("Z", Graphic3d_Vertex (pZ.X(), pZ.Y(), pZ.Z()), 1.0 / 81.0);
551 aGroup->AddPrimitiveArray (aPrims);
553 myPlaneStructure->Display();