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_AspectMarker3d.hxx>
19 #include <Graphic3d_GraphicDriver.hxx>
20 #include <Graphic3d_Group.hxx>
21 #include <Graphic3d_Structure.hxx>
22 #include <Standard_ErrorHandler.hxx>
23 #include <Standard_Type.hxx>
25 #include <V3d_BadValue.hxx>
26 #include <V3d_CircularGrid.hxx>
27 #include <V3d_AmbientLight.hxx>
28 #include <V3d_DirectionalLight.hxx>
29 #include <V3d_RectangularGrid.hxx>
30 #include <V3d_View.hxx>
32 IMPLEMENT_STANDARD_RTTIEXT(V3d_Viewer, Standard_Transient)
34 // ========================================================================
35 // function : V3d_Viewer
37 // ========================================================================
38 V3d_Viewer::V3d_Viewer (const Handle(Graphic3d_GraphicDriver)& theDriver)
39 : myDriver (theDriver),
40 myStructureManager (new Graphic3d_StructureManager (theDriver)),
41 myZLayerGenId (1, IntegerLast()),
42 myBackground (Quantity_NOC_GRAY30),
44 myViewProj (V3d_XposYnegZpos),
45 myVisualization (V3d_ZBUFFER),
46 myShadingModel (V3d_GOURAUD),
47 myDefaultTypeOfView (V3d_ORTHOGRAPHIC),
48 myComputedMode (Standard_True),
49 myDefaultComputedMode (Standard_False),
50 myPrivilegedPlane (gp_Ax3 (gp_Pnt (0.,0.,0), gp_Dir (0.,0.,1.), gp_Dir (1.,0.,0.))),
51 myDisplayPlane (Standard_False),
52 myDisplayPlaneLength (1000.0),
53 myGridType (Aspect_GT_Rectangular),
54 myGridEcho (Standard_True),
55 myGridEchoLastVert (ShortRealLast(), ShortRealLast(), ShortRealLast())
57 myRGrid = new V3d_RectangularGrid (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
58 myCGrid = new V3d_CircularGrid (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
61 // ========================================================================
62 // function : V3d_Viewer
64 // ========================================================================
65 V3d_Viewer::V3d_Viewer (const Handle(Graphic3d_GraphicDriver)& theDriver,
66 const Standard_ExtString ,
67 const Standard_CString ,
68 const Standard_Real theViewSize,
69 const V3d_TypeOfOrientation theViewProj,
70 const Quantity_NameOfColor theViewBackground,
71 const V3d_TypeOfVisualization theVisualization,
72 const V3d_TypeOfShadingModel theShadingModel,
73 const Standard_Boolean theComputedMode,
74 const Standard_Boolean theDefaultComputedMode)
75 : myDriver (theDriver),
76 myStructureManager (new Graphic3d_StructureManager (theDriver)),
77 myZLayerGenId (1, IntegerLast()),
78 myBackground (theViewBackground),
79 myViewSize (theViewSize),
80 myViewProj (theViewProj),
81 myVisualization (theVisualization),
82 myShadingModel (theShadingModel),
83 myDefaultTypeOfView (V3d_ORTHOGRAPHIC),
84 myComputedMode (theComputedMode),
85 myDefaultComputedMode (theDefaultComputedMode),
86 myPrivilegedPlane (gp_Ax3 (gp_Pnt (0.,0.,0), gp_Dir (0.,0.,1.), gp_Dir (1.,0.,0.))),
87 myDisplayPlane (Standard_False),
88 myDisplayPlaneLength (theViewSize),
89 myGridType (Aspect_GT_Rectangular),
90 myGridEcho (Standard_True),
91 myGridEchoLastVert (ShortRealLast(), ShortRealLast(), ShortRealLast())
93 myRGrid = new V3d_RectangularGrid (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
94 myCGrid = new V3d_CircularGrid (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
95 SetDefaultViewSize (theViewSize);
98 // ========================================================================
99 // function : CreateView
101 // ========================================================================
102 Handle(V3d_View) V3d_Viewer::CreateView ()
104 return new V3d_View(this, myDefaultTypeOfView);
107 // ========================================================================
108 // function : SetViewOn
110 // ========================================================================
111 void V3d_Viewer::SetViewOn()
113 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
115 SetViewOn (aDefViewIter.Value());
119 // ========================================================================
120 // function : SetViewOff
122 // ========================================================================
123 void V3d_Viewer::SetViewOff()
125 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
127 SetViewOff (aDefViewIter.Value());
131 // ========================================================================
132 // function : SetViewOn
134 // ========================================================================
135 void V3d_Viewer::SetViewOn (const Handle(V3d_View)& theView)
137 Handle(Graphic3d_CView) aViewImpl = theView->View();
138 if (!aViewImpl->IsDefined() || myActiveViews.Contains (theView))
143 myActiveViews.Append (theView);
144 aViewImpl->Activate();
145 for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
147 theView->SetLightOn (anActiveLightIter.Value());
150 theView->SetGrid (myPrivilegedPlane, Grid ());
151 theView->SetGridActivity (Grid ()->IsActive ());
155 // ========================================================================
156 // function : SetViewOff
158 // ========================================================================
159 void V3d_Viewer::SetViewOff (const Handle(V3d_View)& theView)
161 Handle(Graphic3d_CView) aViewImpl = theView->View();
162 if (aViewImpl->IsDefined() && myActiveViews.Contains (theView))
164 myActiveViews.Remove (theView);
165 aViewImpl->Deactivate() ;
169 // ========================================================================
172 // ========================================================================
173 void V3d_Viewer::Redraw() const
175 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
177 aDefViewIter.Value()->Redraw();
181 // ========================================================================
182 // function : RedrawImmediate
184 // ========================================================================
185 void V3d_Viewer::RedrawImmediate() const
187 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
189 aDefViewIter.Value()->RedrawImmediate();
193 // ========================================================================
194 // function : Invalidate
196 // ========================================================================
197 void V3d_Viewer::Invalidate() const
199 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
201 aDefViewIter.Value()->Invalidate();
205 // ========================================================================
208 // ========================================================================
209 void V3d_Viewer::Remove()
211 myStructureManager->Remove();
214 // ========================================================================
217 // ========================================================================
218 void V3d_Viewer::Erase() const
220 myStructureManager->Erase();
223 // ========================================================================
224 // function : UnHighlight
226 // ========================================================================
227 void V3d_Viewer::UnHighlight() const
229 myStructureManager->UnHighlight();
232 void V3d_Viewer::SetDefaultViewSize (const Standard_Real theSize)
234 V3d_BadValue_Raise_if (theSize <= 0.0, "V3d_Viewer::SetDefaultViewSize, bad size");
235 myViewSize = theSize;
238 // ========================================================================
239 // function : IfMoreViews
241 // ========================================================================
242 Standard_Boolean V3d_Viewer::IfMoreViews() const
244 return myDefinedViews.Size() < myStructureManager->MaxNumOfViews();
247 // ========================================================================
248 // function : AddView
250 // ========================================================================
251 void V3d_Viewer::AddView (const Handle(V3d_View)& theView)
253 if (!myDefinedViews.Contains (theView))
255 myDefinedViews.Append (theView);
259 // ========================================================================
260 // function : DelView
262 // ========================================================================
263 void V3d_Viewer::DelView (const Handle(V3d_View)& theView)
265 myActiveViews.Remove (theView);
266 myDefinedViews.Remove (theView);
269 //=======================================================================
270 //function : AddZLayer
272 //=======================================================================
273 Standard_Boolean V3d_Viewer::AddZLayer (Standard_Integer& theLayerId)
278 theLayerId = myZLayerGenId.Next();
280 catch (Aspect_IdentDefinitionError)
282 // new index can't be generated
283 return Standard_False;
286 myLayerIds.Add (theLayerId);
287 myDriver->AddZLayer (theLayerId);
289 return Standard_True;
292 //=======================================================================
293 //function : RemoveZLayer
295 //=======================================================================
296 Standard_Boolean V3d_Viewer::RemoveZLayer (const Standard_Integer theLayerId)
298 if (!myLayerIds.Contains (theLayerId)
299 || theLayerId < myZLayerGenId.Lower()
300 || theLayerId > myZLayerGenId.Upper())
302 return Standard_False;
305 myDriver->RemoveZLayer (theLayerId);
306 myLayerIds.Remove (theLayerId);
307 myZLayerGenId.Free (theLayerId);
309 return Standard_True;
312 //=======================================================================
313 //function : GetAllZLayers
315 //=======================================================================
316 void V3d_Viewer::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
318 myDriver->ZLayers (theLayerSeq);
321 //=======================================================================
322 //function : SetZLayerSettings
324 //=======================================================================
325 void V3d_Viewer::SetZLayerSettings (const Standard_Integer theLayerId, const Graphic3d_ZLayerSettings& theSettings)
327 myDriver->SetZLayerSettings (theLayerId, theSettings);
330 //=======================================================================
331 //function : ZLayerSettings
333 //=======================================================================
334 Graphic3d_ZLayerSettings V3d_Viewer::ZLayerSettings (const Standard_Integer theLayerId)
336 return myDriver->ZLayerSettings (theLayerId);
339 //=======================================================================
340 //function : UpdateLights
342 //=======================================================================
343 void V3d_Viewer::UpdateLights()
345 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
347 anActiveViewIter.Value()->UpdateLights();
351 //=======================================================================
352 //function : SetLightOn
354 //=======================================================================
355 void V3d_Viewer::SetLightOn (const Handle(V3d_Light)& theLight)
357 if (!myActiveLights.Contains (theLight))
359 myActiveLights.Append (theLight);
362 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
364 anActiveViewIter.Value()->SetLightOn (theLight);
368 //=======================================================================
369 //function : SetLightOff
371 //=======================================================================
372 void V3d_Viewer::SetLightOff (const Handle(V3d_Light)& theLight)
374 myActiveLights.Remove (theLight);
375 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
377 anActiveViewIter.Value()->SetLightOff (theLight);
381 //=======================================================================
382 //function : SetLightOn
384 //=======================================================================
385 void V3d_Viewer::SetLightOn()
387 for (V3d_ListOfLight::Iterator aDefLightIter (myDefinedLights); aDefLightIter.More(); aDefLightIter.Next())
389 if (!myActiveLights.Contains (aDefLightIter.Value()))
391 myActiveLights.Append (aDefLightIter.Value());
392 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
394 anActiveViewIter.Value()->SetLightOn (aDefLightIter.Value());
400 //=======================================================================
401 //function : SetLightOff
403 //=======================================================================
404 void V3d_Viewer::SetLightOff()
406 for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
408 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
410 anActiveViewIter.Value()->SetLightOff (anActiveLightIter.Value());
413 myActiveLights.Clear();
416 //=======================================================================
417 //function : IsGlobalLight
419 //=======================================================================
420 Standard_Boolean V3d_Viewer::IsGlobalLight (const Handle(V3d_Light)& theLight) const
422 return myActiveLights.Contains (theLight);
425 //=======================================================================
426 //function : AddLight
428 //=======================================================================
429 void V3d_Viewer::AddLight (const Handle(V3d_Light)& theLight)
431 if (!myDefinedLights.Contains (theLight))
433 myDefinedLights.Append (theLight);
437 //=======================================================================
438 //function : DelLight
440 //=======================================================================
441 void V3d_Viewer::DelLight (const Handle(V3d_Light)& theLight)
443 SetLightOff (theLight);
444 myDefinedLights.Remove (theLight);
447 //=======================================================================
448 //function : SetDefaultLights
450 //=======================================================================
451 void V3d_Viewer::SetDefaultLights()
453 while (!myDefinedLights.IsEmpty())
455 Handle(V3d_Light) aLight = myDefinedLights.First();
459 SetLightOn (new V3d_DirectionalLight (this, V3d_Zneg, Quantity_NOC_WHITE, Standard_True));
460 SetLightOn (new V3d_AmbientLight (this));