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 ());
158 // ========================================================================
159 // function : SetViewOff
161 // ========================================================================
162 void V3d_Viewer::SetViewOff (const Handle(V3d_View)& theView)
164 Handle(Graphic3d_CView) aViewImpl = theView->View();
165 if (aViewImpl->IsDefined() && myActiveViews.Contains (theView))
167 myActiveViews.Remove (theView);
168 aViewImpl->Deactivate() ;
172 // ========================================================================
175 // ========================================================================
176 void V3d_Viewer::Redraw() const
178 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
180 aDefViewIter.Value()->Redraw();
184 // ========================================================================
185 // function : RedrawImmediate
187 // ========================================================================
188 void V3d_Viewer::RedrawImmediate() const
190 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
192 aDefViewIter.Value()->RedrawImmediate();
196 // ========================================================================
197 // function : Invalidate
199 // ========================================================================
200 void V3d_Viewer::Invalidate() const
202 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
204 aDefViewIter.Value()->Invalidate();
208 // ========================================================================
211 // ========================================================================
212 void V3d_Viewer::Remove()
214 myStructureManager->Remove();
217 // ========================================================================
220 // ========================================================================
221 void V3d_Viewer::Erase() const
223 myStructureManager->Erase();
226 // ========================================================================
227 // function : UnHighlight
229 // ========================================================================
230 void V3d_Viewer::UnHighlight() const
232 myStructureManager->UnHighlight();
235 void V3d_Viewer::SetDefaultViewSize (const Standard_Real theSize)
238 throw V3d_BadValue("V3d_Viewer::SetDefaultViewSize, bad size");
239 myViewSize = theSize;
242 // ========================================================================
243 // function : IfMoreViews
245 // ========================================================================
246 Standard_Boolean V3d_Viewer::IfMoreViews() const
248 return myDefinedViews.Size() < myStructureManager->MaxNumOfViews();
251 // ========================================================================
252 // function : AddView
254 // ========================================================================
255 void V3d_Viewer::AddView (const Handle(V3d_View)& theView)
257 if (!myDefinedViews.Contains (theView))
259 myDefinedViews.Append (theView);
263 // ========================================================================
264 // function : DelView
266 // ========================================================================
267 void V3d_Viewer::DelView (const Handle(V3d_View)& theView)
269 myActiveViews.Remove (theView);
270 myDefinedViews.Remove (theView);
273 //=======================================================================
274 //function : AddZLayer
276 //=======================================================================
277 Standard_Boolean V3d_Viewer::AddZLayer (Graphic3d_ZLayerId& theLayerId)
282 theLayerId = myZLayerGenId.Next();
284 catch (Aspect_IdentDefinitionError)
286 // new index can't be generated
287 return Standard_False;
290 myLayerIds.Add (theLayerId);
291 myDriver->AddZLayer (theLayerId);
293 return Standard_True;
296 //=======================================================================
297 //function : RemoveZLayer
299 //=======================================================================
300 Standard_Boolean V3d_Viewer::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
302 if (!myLayerIds.Contains (theLayerId)
303 || theLayerId < myZLayerGenId.Lower()
304 || theLayerId > myZLayerGenId.Upper())
306 return Standard_False;
309 myDriver->RemoveZLayer (theLayerId);
310 myLayerIds.Remove (theLayerId);
311 myZLayerGenId.Free (theLayerId);
313 return Standard_True;
316 //=======================================================================
317 //function : GetAllZLayers
319 //=======================================================================
320 void V3d_Viewer::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
322 myDriver->ZLayers (theLayerSeq);
325 //=======================================================================
326 //function : SetZLayerSettings
328 //=======================================================================
329 void V3d_Viewer::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId, const Graphic3d_ZLayerSettings& theSettings)
331 myDriver->SetZLayerSettings (theLayerId, theSettings);
334 //=======================================================================
335 //function : ZLayerSettings
337 //=======================================================================
338 Graphic3d_ZLayerSettings V3d_Viewer::ZLayerSettings (const Graphic3d_ZLayerId theLayerId)
340 return myDriver->ZLayerSettings (theLayerId);
343 //=======================================================================
344 //function : UpdateLights
346 //=======================================================================
347 void V3d_Viewer::UpdateLights()
349 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
351 anActiveViewIter.Value()->UpdateLights();
355 //=======================================================================
356 //function : SetLightOn
358 //=======================================================================
359 void V3d_Viewer::SetLightOn (const Handle(V3d_Light)& theLight)
361 if (!myActiveLights.Contains (theLight))
363 myActiveLights.Append (theLight);
366 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
368 anActiveViewIter.Value()->SetLightOn (theLight);
372 //=======================================================================
373 //function : SetLightOff
375 //=======================================================================
376 void V3d_Viewer::SetLightOff (const Handle(V3d_Light)& theLight)
378 myActiveLights.Remove (theLight);
379 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
381 anActiveViewIter.Value()->SetLightOff (theLight);
385 //=======================================================================
386 //function : SetLightOn
388 //=======================================================================
389 void V3d_Viewer::SetLightOn()
391 for (V3d_ListOfLight::Iterator aDefLightIter (myDefinedLights); aDefLightIter.More(); aDefLightIter.Next())
393 if (!myActiveLights.Contains (aDefLightIter.Value()))
395 myActiveLights.Append (aDefLightIter.Value());
396 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
398 anActiveViewIter.Value()->SetLightOn (aDefLightIter.Value());
404 //=======================================================================
405 //function : SetLightOff
407 //=======================================================================
408 void V3d_Viewer::SetLightOff()
410 for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
412 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
414 anActiveViewIter.Value()->SetLightOff (anActiveLightIter.Value());
417 myActiveLights.Clear();
420 //=======================================================================
421 //function : IsGlobalLight
423 //=======================================================================
424 Standard_Boolean V3d_Viewer::IsGlobalLight (const Handle(V3d_Light)& theLight) const
426 return myActiveLights.Contains (theLight);
429 //=======================================================================
430 //function : AddLight
432 //=======================================================================
433 void V3d_Viewer::AddLight (const Handle(V3d_Light)& theLight)
435 if (!myDefinedLights.Contains (theLight))
437 myDefinedLights.Append (theLight);
441 //=======================================================================
442 //function : DelLight
444 //=======================================================================
445 void V3d_Viewer::DelLight (const Handle(V3d_Light)& theLight)
447 SetLightOff (theLight);
448 myDefinedLights.Remove (theLight);
451 //=======================================================================
452 //function : SetDefaultLights
454 //=======================================================================
455 void V3d_Viewer::SetDefaultLights()
457 while (!myDefinedLights.IsEmpty())
459 Handle(V3d_Light) aLight = myDefinedLights.First();
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);
471 //=======================================================================
472 //function : SetPrivilegedPlane
474 //=======================================================================
475 void V3d_Viewer::SetPrivilegedPlane (const gp_Ax3& thePlane)
477 myPrivilegedPlane = thePlane;
478 Grid()->SetDrawMode(Grid()->DrawMode());
479 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
481 anActiveViewIter.Value()->SetGrid (myPrivilegedPlane, Grid());
486 DisplayPrivilegedPlane (Standard_True, myDisplayPlaneLength);
490 //=======================================================================
491 //function : DisplayPrivilegedPlane
493 //=======================================================================
494 void V3d_Viewer::DisplayPrivilegedPlane (const Standard_Boolean theOnOff, const Standard_Real theSize)
496 myDisplayPlane = theOnOff;
497 myDisplayPlaneLength = theSize;
501 if (!myPlaneStructure.IsNull())
503 myPlaneStructure->Erase();
508 if (myPlaneStructure.IsNull())
510 myPlaneStructure = new Graphic3d_Structure (StructureManager());
511 myPlaneStructure->SetInfiniteState (Standard_True);
512 myPlaneStructure->Display();
516 myPlaneStructure->Clear();
519 Handle(Graphic3d_Group) aGroup = myPlaneStructure->NewGroup();
521 Handle(Graphic3d_AspectLine3d) aLineAttrib = new Graphic3d_AspectLine3d (Quantity_NOC_GRAY60, Aspect_TOL_SOLID, 1.0);
522 aGroup->SetGroupPrimitivesAspect (aLineAttrib);
524 Handle(Graphic3d_AspectText3d) aTextAttrib = new Graphic3d_AspectText3d();
525 aTextAttrib->SetColor (Quantity_Color (Quantity_NOC_ROYALBLUE1));
526 aGroup->SetGroupPrimitivesAspect (aTextAttrib);
528 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments (6);
530 const gp_Pnt& p0 = myPrivilegedPlane.Location();
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);
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);
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);
547 aGroup->AddPrimitiveArray (aPrims);
549 myPlaneStructure->Display();