1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13 
14 #include <V3d_Viewer.hxx>
15 
16 #include <Aspect_Grid.hxx>
17 #include <Aspect_IdentDefinitionError.hxx>
18 #include <Graphic3d_ArrayOfPoints.hxx>
19 #include <Graphic3d_ArrayOfSegments.hxx>
20 #include <Graphic3d_AspectLine3d.hxx>
21 #include <Graphic3d_AspectMarker3d.hxx>
22 #include <Graphic3d_AspectText3d.hxx>
23 #include <Graphic3d_GraphicDriver.hxx>
24 #include <Graphic3d_Group.hxx>
25 #include <Graphic3d_Structure.hxx>
26 #include <Graphic3d_Text.hxx>
27 #include <Standard_ErrorHandler.hxx>
28 #include <V3d.hxx>
29 #include <V3d_BadValue.hxx>
30 #include <V3d_CircularGrid.hxx>
31 #include <V3d_AmbientLight.hxx>
32 #include <V3d_DirectionalLight.hxx>
33 #include <V3d_RectangularGrid.hxx>
34 #include <V3d_View.hxx>
35 
IMPLEMENT_STANDARD_RTTIEXT(V3d_Viewer,Standard_Transient)36 IMPLEMENT_STANDARD_RTTIEXT(V3d_Viewer, Standard_Transient)
37 
38 // ========================================================================
39 // function : V3d_Viewer
40 // purpose  :
41 // ========================================================================
42 V3d_Viewer::V3d_Viewer (const Handle(Graphic3d_GraphicDriver)& theDriver)
43 : myDriver (theDriver),
44   myStructureManager (new Graphic3d_StructureManager (theDriver)),
45   myZLayerGenId (1, IntegerLast()),
46   myBackground (Quantity_NOC_GRAY30),
47   myViewSize (1000.0),
48   myViewProj (V3d_XposYnegZpos),
49   myVisualization (V3d_ZBUFFER),
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   //
61 }
62 
63 // ========================================================================
64 // function : CreateView
65 // purpose  :
66 // ========================================================================
Handle(V3d_View)67 Handle(V3d_View) V3d_Viewer::CreateView ()
68 {
69   return new V3d_View(this, myDefaultTypeOfView);
70 }
71 
72 // ========================================================================
73 // function : SetViewOn
74 // purpose  :
75 // ========================================================================
SetViewOn()76 void V3d_Viewer::SetViewOn()
77 {
78   for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
79   {
80     SetViewOn (aDefViewIter.Value());
81   }
82 }
83 
84 // ========================================================================
85 // function : SetViewOff
86 // purpose  :
87 // ========================================================================
SetViewOff()88 void V3d_Viewer::SetViewOff()
89 {
90   for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
91   {
92     SetViewOff (aDefViewIter.Value());
93   }
94 }
95 
96 // ========================================================================
97 // function : SetViewOn
98 // purpose  :
99 // ========================================================================
SetViewOn(const Handle (V3d_View)& theView)100 void V3d_Viewer::SetViewOn (const Handle(V3d_View)& theView)
101 {
102   Handle(Graphic3d_CView) aViewImpl = theView->View();
103   if (!aViewImpl->IsDefined() || myActiveViews.Contains (theView))
104   {
105     return;
106   }
107 
108   myActiveViews.Append (theView);
109   aViewImpl->Activate();
110   for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
111   {
112     theView->SetLightOn (anActiveLightIter.Value());
113   }
114   if (Handle(Aspect_Grid) aGrid = Grid (false))
115   {
116     theView->SetGrid (myPrivilegedPlane, aGrid);
117     theView->SetGridActivity (aGrid->IsActive());
118   }
119   if (theView->SetImmediateUpdate (Standard_False))
120   {
121     theView->Redraw();
122     theView->SetImmediateUpdate (Standard_True);
123   }
124 }
125 
126 // ========================================================================
127 // function : SetViewOff
128 // purpose  :
129 // ========================================================================
SetViewOff(const Handle (V3d_View)& theView)130 void V3d_Viewer::SetViewOff (const Handle(V3d_View)& theView)
131 {
132   Handle(Graphic3d_CView) aViewImpl = theView->View();
133   if (aViewImpl->IsDefined() && myActiveViews.Contains (theView))
134   {
135     myActiveViews.Remove (theView);
136     aViewImpl->Deactivate() ;
137   }
138 }
139 
140 // ========================================================================
141 // function : Redraw
142 // purpose  :
143 // ========================================================================
Redraw() const144 void V3d_Viewer::Redraw() const
145 {
146   for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
147   {
148     aDefViewIter.Value()->Redraw();
149   }
150 }
151 
152 // ========================================================================
153 // function : RedrawImmediate
154 // purpose  :
155 // ========================================================================
RedrawImmediate() const156 void V3d_Viewer::RedrawImmediate() const
157 {
158   for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
159   {
160     aDefViewIter.Value()->RedrawImmediate();
161   }
162 }
163 
164 // ========================================================================
165 // function : Invalidate
166 // purpose  :
167 // ========================================================================
Invalidate() const168 void V3d_Viewer::Invalidate() const
169 {
170   for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
171   {
172     aDefViewIter.Value()->Invalidate();
173   }
174 }
175 
176 // ========================================================================
177 // function : Remove
178 // purpose  :
179 // ========================================================================
Remove()180 void V3d_Viewer::Remove()
181 {
182   myStructureManager->Remove();
183 }
184 
185 // ========================================================================
186 // function : Erase
187 // purpose  :
188 // ========================================================================
Erase() const189 void V3d_Viewer::Erase() const
190 {
191   myStructureManager->Erase();
192 }
193 
194 // ========================================================================
195 // function : UnHighlight
196 // purpose  :
197 // ========================================================================
UnHighlight() const198 void V3d_Viewer::UnHighlight() const
199 {
200   myStructureManager->UnHighlight();
201 }
202 
SetDefaultViewSize(const Standard_Real theSize)203 void V3d_Viewer::SetDefaultViewSize (const Standard_Real theSize)
204 {
205   if (theSize <= 0.0)
206     throw V3d_BadValue("V3d_Viewer::SetDefaultViewSize, bad size");
207   myViewSize = theSize;
208 }
209 
210 // ========================================================================
211 // function : IfMoreViews
212 // purpose  :
213 // ========================================================================
IfMoreViews() const214 Standard_Boolean V3d_Viewer::IfMoreViews() const
215 {
216   return myDefinedViews.Size() < myStructureManager->MaxNumOfViews();
217 }
218 
219 // ========================================================================
220 // function : AddView
221 // purpose  :
222 // ========================================================================
AddView(const Handle (V3d_View)& theView)223 void V3d_Viewer::AddView (const Handle(V3d_View)& theView)
224 {
225   if (!myDefinedViews.Contains (theView))
226   {
227     myDefinedViews.Append (theView);
228   }
229 }
230 
231 // ========================================================================
232 // function : DelView
233 // purpose  :
234 // ========================================================================
DelView(const Handle (V3d_View)& theView)235 void V3d_Viewer::DelView (const Handle(V3d_View)& theView)
236 {
237   myActiveViews.Remove (theView);
238   myDefinedViews.Remove (theView);
239 }
240 
241 //=======================================================================
242 //function : InsertLayerBefore
243 //purpose  :
244 //=======================================================================
InsertLayerBefore(Graphic3d_ZLayerId & theNewLayerId,const Graphic3d_ZLayerSettings & theSettings,const Graphic3d_ZLayerId theLayerAfter)245 Standard_Boolean V3d_Viewer::InsertLayerBefore (Graphic3d_ZLayerId& theNewLayerId,
246                                                 const Graphic3d_ZLayerSettings& theSettings,
247                                                 const Graphic3d_ZLayerId theLayerAfter)
248 {
249   if (myZLayerGenId.Next (theNewLayerId))
250   {
251     myLayerIds.Add (theNewLayerId);
252     myDriver->InsertLayerBefore (theNewLayerId, theSettings, theLayerAfter);
253     return Standard_True;
254   }
255   return Standard_False;
256 }
257 
258 //=======================================================================
259 //function : InsertLayerAfter
260 //purpose  :
261 //=======================================================================
InsertLayerAfter(Graphic3d_ZLayerId & theNewLayerId,const Graphic3d_ZLayerSettings & theSettings,const Graphic3d_ZLayerId theLayerBefore)262 Standard_Boolean V3d_Viewer::InsertLayerAfter (Graphic3d_ZLayerId& theNewLayerId,
263                                                const Graphic3d_ZLayerSettings& theSettings,
264                                                const Graphic3d_ZLayerId theLayerBefore)
265 {
266   if (myZLayerGenId.Next (theNewLayerId))
267   {
268     myLayerIds.Add (theNewLayerId);
269     myDriver->InsertLayerAfter (theNewLayerId, theSettings, theLayerBefore);
270     return Standard_True;
271   }
272   return Standard_False;
273 }
274 
275 //=======================================================================
276 //function : RemoveZLayer
277 //purpose  :
278 //=======================================================================
RemoveZLayer(const Graphic3d_ZLayerId theLayerId)279 Standard_Boolean V3d_Viewer::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
280 {
281   if (!myLayerIds.Contains (theLayerId)
282     || theLayerId < myZLayerGenId.Lower()
283     || theLayerId > myZLayerGenId.Upper())
284   {
285     return Standard_False;
286   }
287 
288   myDriver->RemoveZLayer (theLayerId);
289   myLayerIds.Remove  (theLayerId);
290   myZLayerGenId.Free (theLayerId);
291 
292   return Standard_True;
293 }
294 
295 //=======================================================================
296 //function : GetAllZLayers
297 //purpose  :
298 //=======================================================================
GetAllZLayers(TColStd_SequenceOfInteger & theLayerSeq) const299 void V3d_Viewer::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
300 {
301   myDriver->ZLayers (theLayerSeq);
302 }
303 
304 //=======================================================================
305 //function : SetZLayerSettings
306 //purpose  :
307 //=======================================================================
SetZLayerSettings(const Graphic3d_ZLayerId theLayerId,const Graphic3d_ZLayerSettings & theSettings)308 void V3d_Viewer::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId, const Graphic3d_ZLayerSettings& theSettings)
309 {
310   myDriver->SetZLayerSettings (theLayerId, theSettings);
311 }
312 
313 //=======================================================================
314 //function : ZLayerSettings
315 //purpose  :
316 //=======================================================================
ZLayerSettings(const Graphic3d_ZLayerId theLayerId) const317 const Graphic3d_ZLayerSettings& V3d_Viewer::ZLayerSettings (const Graphic3d_ZLayerId theLayerId) const
318 {
319   return myDriver->ZLayerSettings (theLayerId);
320 }
321 
322 //=======================================================================
323 //function : UpdateLights
324 //purpose  :
325 //=======================================================================
UpdateLights()326 void V3d_Viewer::UpdateLights()
327 {
328   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
329   {
330     anActiveViewIter.Value()->UpdateLights();
331   }
332 }
333 
334 //=======================================================================
335 //function : SetLightOn
336 //purpose  :
337 //=======================================================================
SetLightOn(const Handle (V3d_Light)& theLight)338 void V3d_Viewer::SetLightOn (const Handle(V3d_Light)& theLight)
339 {
340   if (!myActiveLights.Contains (theLight))
341   {
342     myActiveLights.Append (theLight);
343   }
344 
345   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
346   {
347     anActiveViewIter.Value()->SetLightOn (theLight);
348   }
349 }
350 
351 //=======================================================================
352 //function : SetLightOff
353 //purpose  :
354 //=======================================================================
SetLightOff(const Handle (V3d_Light)& theLight)355 void V3d_Viewer::SetLightOff (const Handle(V3d_Light)& theLight)
356 {
357   myActiveLights.Remove (theLight);
358   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
359   {
360     anActiveViewIter.Value()->SetLightOff (theLight);
361   }
362 }
363 
364 //=======================================================================
365 //function : SetLightOn
366 //purpose  :
367 //=======================================================================
SetLightOn()368 void V3d_Viewer::SetLightOn()
369 {
370   for (V3d_ListOfLight::Iterator aDefLightIter (myDefinedLights); aDefLightIter.More(); aDefLightIter.Next())
371   {
372     if (!myActiveLights.Contains (aDefLightIter.Value()))
373     {
374       myActiveLights.Append (aDefLightIter.Value());
375       for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
376       {
377         anActiveViewIter.Value()->SetLightOn (aDefLightIter.Value());
378       }
379     }
380   }
381 }
382 
383 //=======================================================================
384 //function : SetLightOff
385 //purpose  :
386 //=======================================================================
SetLightOff()387 void V3d_Viewer::SetLightOff()
388 {
389   for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
390   {
391     for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
392     {
393       anActiveViewIter.Value()->SetLightOff (anActiveLightIter.Value());
394     }
395   }
396   myActiveLights.Clear();
397 }
398 
399 //=======================================================================
400 //function : IsGlobalLight
401 //purpose  :
402 //=======================================================================
IsGlobalLight(const Handle (V3d_Light)& theLight) const403 Standard_Boolean V3d_Viewer::IsGlobalLight (const Handle(V3d_Light)& theLight) const
404 {
405   return myActiveLights.Contains (theLight);
406 }
407 
408 //=======================================================================
409 //function : AddLight
410 //purpose  :
411 //=======================================================================
AddLight(const Handle (V3d_Light)& theLight)412 void V3d_Viewer::AddLight (const Handle(V3d_Light)& theLight)
413 {
414   if (!myDefinedLights.Contains (theLight))
415   {
416     myDefinedLights.Append (theLight);
417   }
418 }
419 
420 //=======================================================================
421 //function : DelLight
422 //purpose  :
423 //=======================================================================
DelLight(const Handle (V3d_Light)& theLight)424 void V3d_Viewer::DelLight (const Handle(V3d_Light)& theLight)
425 {
426   SetLightOff (theLight);
427   myDefinedLights.Remove (theLight);
428 }
429 
430 //=======================================================================
431 //function : SetDefaultLights
432 //purpose  :
433 //=======================================================================
SetDefaultLights()434 void V3d_Viewer::SetDefaultLights()
435 {
436   while (!myDefinedLights.IsEmpty())
437   {
438     Handle(V3d_Light) aLight = myDefinedLights.First();
439     DelLight (aLight);
440   }
441 
442   Handle(V3d_DirectionalLight) aDirLight = new V3d_DirectionalLight (V3d_Zneg, Quantity_NOC_WHITE);
443   aDirLight->SetName ("headlight");
444   aDirLight->SetHeadlight (true);
445   Handle(V3d_AmbientLight) anAmbLight = new V3d_AmbientLight (Quantity_NOC_WHITE);
446   anAmbLight->SetName ("amblight");
447   AddLight (aDirLight);
448   AddLight (anAmbLight);
449   SetLightOn (aDirLight);
450   SetLightOn (anAmbLight);
451 }
452 
453 //=======================================================================
454 //function : SetPrivilegedPlane
455 //purpose  :
456 //=======================================================================
SetPrivilegedPlane(const gp_Ax3 & thePlane)457 void V3d_Viewer::SetPrivilegedPlane (const gp_Ax3& thePlane)
458 {
459   myPrivilegedPlane = thePlane;
460   Handle(Aspect_Grid) aGrid = Grid (true);
461   aGrid->SetDrawMode (aGrid->DrawMode()); // aGrid->UpdateDisplay();
462   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
463   {
464     anActiveViewIter.Value()->SetGrid (myPrivilegedPlane, aGrid);
465   }
466 
467   if (myDisplayPlane)
468   {
469     DisplayPrivilegedPlane (Standard_True, myDisplayPlaneLength);
470   }
471 }
472 
473 //=======================================================================
474 //function : DisplayPrivilegedPlane
475 //purpose  :
476 //=======================================================================
DisplayPrivilegedPlane(const Standard_Boolean theOnOff,const Standard_Real theSize)477 void V3d_Viewer::DisplayPrivilegedPlane (const Standard_Boolean theOnOff, const Standard_Real theSize)
478 {
479   myDisplayPlane = theOnOff;
480   myDisplayPlaneLength = theSize;
481 
482   if (!myDisplayPlane)
483   {
484     if (!myPlaneStructure.IsNull())
485     {
486       myPlaneStructure->Erase();
487     }
488     return;
489   }
490 
491   if (myPlaneStructure.IsNull())
492   {
493     myPlaneStructure = new Graphic3d_Structure (StructureManager());
494     myPlaneStructure->SetInfiniteState (Standard_True);
495     myPlaneStructure->Display();
496   }
497   else
498   {
499     myPlaneStructure->Clear();
500   }
501 
502   Handle(Graphic3d_Group) aGroup = myPlaneStructure->NewGroup();
503 
504   Handle(Graphic3d_AspectLine3d) aLineAttrib = new Graphic3d_AspectLine3d (Quantity_NOC_GRAY60, Aspect_TOL_SOLID, 1.0);
505   aGroup->SetGroupPrimitivesAspect (aLineAttrib);
506 
507   Handle(Graphic3d_AspectText3d) aTextAttrib = new Graphic3d_AspectText3d();
508   aTextAttrib->SetColor (Quantity_Color (Quantity_NOC_ROYALBLUE1));
509   aGroup->SetGroupPrimitivesAspect (aTextAttrib);
510 
511   Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments (6);
512 
513   const gp_Pnt& p0 = myPrivilegedPlane.Location();
514 
515   const gp_Pnt pX (p0.XYZ() + myDisplayPlaneLength * myPrivilegedPlane.XDirection().XYZ());
516   aPrims->AddVertex (p0);
517   aPrims->AddVertex (pX);
518   Handle(Graphic3d_Text) aText = new Graphic3d_Text (1.0f / 81.0f);
519   aText->SetText ("X");
520   aText->SetPosition (pX);
521   aGroup->AddText (aText);
522 
523   const gp_Pnt pY (p0.XYZ() + myDisplayPlaneLength * myPrivilegedPlane.YDirection().XYZ());
524   aPrims->AddVertex (p0);
525   aPrims->AddVertex (pY);
526   aText = new Graphic3d_Text (1.0f / 81.0f);
527   aText->SetText ("Y");
528   aText->SetPosition (pY);
529   aGroup->AddText (aText);
530 
531   const gp_Pnt pZ (p0.XYZ() + myDisplayPlaneLength * myPrivilegedPlane.Direction().XYZ());
532   aPrims->AddVertex (p0);
533   aPrims->AddVertex (pZ);
534   aText = new Graphic3d_Text (1.0f / 81.0f);
535   aText->SetText ("Z");
536   aText->SetPosition (pZ);
537   aGroup->AddText (aText);
538 
539   aGroup->AddPrimitiveArray (aPrims);
540 
541   myPlaneStructure->Display();
542 }
543 
544 // =======================================================================
545 // function : Grid
546 // purpose  :
547 // =======================================================================
Handle(Aspect_Grid)548 Handle(Aspect_Grid) V3d_Viewer::Grid (Aspect_GridType theGridType, bool theToCreate)
549 {
550   switch (theGridType)
551   {
552     case Aspect_GT_Circular:
553     {
554       if (myCGrid.IsNull() && theToCreate)
555       {
556         myCGrid = new V3d_CircularGrid (this, Quantity_Color(Quantity_NOC_GRAY50), Quantity_Color(Quantity_NOC_GRAY70));
557       }
558       return Handle(Aspect_Grid) (myCGrid);
559     }
560     case Aspect_GT_Rectangular:
561     {
562       if (myRGrid.IsNull() && theToCreate)
563       {
564         myRGrid = new V3d_RectangularGrid (this, Quantity_Color(Quantity_NOC_GRAY50), Quantity_Color(Quantity_NOC_GRAY70));
565       }
566       return Handle(Aspect_Grid) (myRGrid);
567     }
568   }
569   return Handle(Aspect_Grid)();
570 }
571 
572 // =======================================================================
573 // function : GridDrawMode
574 // purpose  :
575 // =======================================================================
GridDrawMode()576 Aspect_GridDrawMode V3d_Viewer::GridDrawMode()
577 {
578   Handle(Aspect_Grid) aGrid = Grid (false);
579   return !aGrid.IsNull() ? aGrid->DrawMode() : Aspect_GDM_Lines;
580 }
581 
582 // =======================================================================
583 // function : ActivateGrid
584 // purpose  :
585 // =======================================================================
ActivateGrid(const Aspect_GridType theType,const Aspect_GridDrawMode theMode)586 void V3d_Viewer::ActivateGrid (const Aspect_GridType     theType,
587                                const Aspect_GridDrawMode theMode)
588 {
589   if (Handle(Aspect_Grid) anOldGrid = Grid (false))
590   {
591     anOldGrid->Erase();
592   }
593 
594   myGridType = theType;
595   Handle(Aspect_Grid) aGrid = Grid (true);
596   aGrid->SetDrawMode (theMode);
597   if (theMode != Aspect_GDM_None)
598   {
599     aGrid->Display();
600   }
601   aGrid->Activate();
602   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
603   {
604     anActiveViewIter.Value()->SetGrid (myPrivilegedPlane, aGrid);
605   }
606 }
607 
608 // =======================================================================
609 // function : DeactivateGrid
610 // purpose  :
611 // =======================================================================
DeactivateGrid()612 void V3d_Viewer::DeactivateGrid()
613 {
614   Handle(Aspect_Grid) aGrid = Grid (false);
615   if (aGrid.IsNull())
616   {
617     return;
618   }
619 
620   aGrid->Erase();
621   aGrid->Deactivate();
622 
623   myGridType = Aspect_GT_Rectangular;
624   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
625   {
626     anActiveViewIter.Value()->SetGridActivity (Standard_False);
627     if (myGridEcho
628     && !myGridEchoStructure.IsNull())
629     {
630       myGridEchoStructure->Erase();
631     }
632   }
633 }
634 
635 // =======================================================================
636 // function : IsGridActive
637 // purpose  :
638 // =======================================================================
IsGridActive()639 Standard_Boolean V3d_Viewer::IsGridActive()
640 {
641   Handle(Aspect_Grid) aGrid = Grid (false);
642   return !aGrid.IsNull() && aGrid->IsActive();
643 }
644 
645 // =======================================================================
646 // function : RectangularGridValues
647 // purpose  :
648 // =======================================================================
RectangularGridValues(Standard_Real & theXOrigin,Standard_Real & theYOrigin,Standard_Real & theXStep,Standard_Real & theYStep,Standard_Real & theRotationAngle)649 void V3d_Viewer::RectangularGridValues (Standard_Real& theXOrigin,
650                                         Standard_Real& theYOrigin,
651                                         Standard_Real& theXStep,
652                                         Standard_Real& theYStep,
653                                         Standard_Real& theRotationAngle)
654 {
655   Grid (Aspect_GT_Rectangular, true);
656   theXOrigin       = myRGrid->XOrigin();
657   theYOrigin       = myRGrid->YOrigin();
658   theXStep         = myRGrid->XStep();
659   theYStep         = myRGrid->YStep();
660   theRotationAngle = myRGrid->RotationAngle();
661 }
662 
663 // =======================================================================
664 // function : SetRectangularGridValues
665 // purpose  :
666 // =======================================================================
SetRectangularGridValues(const Standard_Real theXOrigin,const Standard_Real theYOrigin,const Standard_Real theXStep,const Standard_Real theYStep,const Standard_Real theRotationAngle)667 void V3d_Viewer::SetRectangularGridValues (const Standard_Real theXOrigin,
668                                            const Standard_Real theYOrigin,
669                                            const Standard_Real theXStep,
670                                            const Standard_Real theYStep,
671                                            const Standard_Real theRotationAngle)
672 {
673   Grid (Aspect_GT_Rectangular, true);
674   myRGrid->SetGridValues (theXOrigin, theYOrigin, theXStep, theYStep, theRotationAngle);
675   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
676   {
677     anActiveViewIter.Value()->SetGrid (myPrivilegedPlane, myRGrid);
678   }
679 }
680 
681 // =======================================================================
682 // function : CircularGridValues
683 // purpose  :
684 // =======================================================================
CircularGridValues(Standard_Real & theXOrigin,Standard_Real & theYOrigin,Standard_Real & theRadiusStep,Standard_Integer & theDivisionNumber,Standard_Real & theRotationAngle)685 void V3d_Viewer::CircularGridValues (Standard_Real& theXOrigin,
686                                      Standard_Real& theYOrigin,
687                                      Standard_Real& theRadiusStep,
688                                      Standard_Integer& theDivisionNumber,
689                                      Standard_Real& theRotationAngle)
690 {
691   Grid (Aspect_GT_Circular, true);
692   theXOrigin        = myCGrid->XOrigin();
693   theYOrigin        = myCGrid->YOrigin();
694   theRadiusStep     = myCGrid->RadiusStep();
695   theDivisionNumber = myCGrid->DivisionNumber();
696   theRotationAngle  = myCGrid->RotationAngle();
697 }
698 
699 // =======================================================================
700 // function : SetCircularGridValues
701 // purpose  :
702 // =======================================================================
SetCircularGridValues(const Standard_Real theXOrigin,const Standard_Real theYOrigin,const Standard_Real theRadiusStep,const Standard_Integer theDivisionNumber,const Standard_Real theRotationAngle)703 void V3d_Viewer::SetCircularGridValues (const Standard_Real theXOrigin,
704                                         const Standard_Real theYOrigin,
705                                         const Standard_Real theRadiusStep,
706                                         const Standard_Integer theDivisionNumber,
707                                         const Standard_Real theRotationAngle)
708 {
709   Grid (Aspect_GT_Circular, true);
710   myCGrid->SetGridValues (theXOrigin, theYOrigin, theRadiusStep,
711                           theDivisionNumber, theRotationAngle);
712   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
713   {
714     anActiveViewIter.Value()->SetGrid (myPrivilegedPlane, myCGrid);
715   }
716 }
717 
718 // =======================================================================
719 // function : RectangularGridGraphicValues
720 // purpose  :
721 // =======================================================================
RectangularGridGraphicValues(Standard_Real & theXSize,Standard_Real & theYSize,Standard_Real & theOffSet)722 void V3d_Viewer::RectangularGridGraphicValues (Standard_Real& theXSize,
723                                                Standard_Real& theYSize,
724                                                Standard_Real& theOffSet)
725 {
726   Grid (Aspect_GT_Rectangular, true);
727   myRGrid->GraphicValues (theXSize, theYSize, theOffSet);
728 }
729 
730 // =======================================================================
731 // function : SetRectangularGridGraphicValues
732 // purpose  :
733 // =======================================================================
SetRectangularGridGraphicValues(const Standard_Real theXSize,const Standard_Real theYSize,const Standard_Real theOffSet)734 void V3d_Viewer::SetRectangularGridGraphicValues (const Standard_Real theXSize,
735                                                   const Standard_Real theYSize,
736                                                   const Standard_Real theOffSet)
737 {
738   Grid (Aspect_GT_Rectangular, true);
739   myRGrid->SetGraphicValues (theXSize, theYSize, theOffSet);
740 }
741 
742 // =======================================================================
743 // function : CircularGridGraphicValues
744 // purpose  :
745 // =======================================================================
CircularGridGraphicValues(Standard_Real & theRadius,Standard_Real & theOffSet)746 void V3d_Viewer::CircularGridGraphicValues (Standard_Real& theRadius,
747                                             Standard_Real& theOffSet)
748 {
749   Grid (Aspect_GT_Circular, true);
750   myCGrid->GraphicValues (theRadius, theOffSet);
751 }
752 
753 // =======================================================================
754 // function : SetCircularGridGraphicValues
755 // purpose  :
756 // =======================================================================
SetCircularGridGraphicValues(const Standard_Real theRadius,const Standard_Real theOffSet)757 void V3d_Viewer::SetCircularGridGraphicValues (const Standard_Real theRadius,
758                                                const Standard_Real theOffSet)
759 {
760   Grid (Aspect_GT_Circular, true);
761   myCGrid->SetGraphicValues (theRadius, theOffSet);
762 }
763 
764 // =======================================================================
765 // function : SetGridEcho
766 // purpose  :
767 // =======================================================================
SetGridEcho(const Standard_Boolean theToShowGrid)768 void V3d_Viewer::SetGridEcho (const Standard_Boolean theToShowGrid)
769 {
770   if (myGridEcho == theToShowGrid)
771   {
772     return;
773   }
774 
775   myGridEcho = theToShowGrid;
776   if (theToShowGrid
777    || myGridEchoStructure.IsNull())
778   {
779     return;
780   }
781 
782   myGridEchoStructure->Erase();
783 }
784 
785 // =======================================================================
786 // function : SetGridEcho
787 // purpose  :
788 // =======================================================================
SetGridEcho(const Handle (Graphic3d_AspectMarker3d)& theMarker)789 void V3d_Viewer::SetGridEcho (const Handle(Graphic3d_AspectMarker3d)& theMarker)
790 {
791   if (myGridEchoStructure.IsNull())
792   {
793     myGridEchoStructure = new Graphic3d_Structure (StructureManager());
794     myGridEchoGroup     = myGridEchoStructure->NewGroup();
795   }
796 
797   myGridEchoAspect = theMarker;
798   myGridEchoGroup->SetPrimitivesAspect (theMarker);
799 }
800 
801 // =======================================================================
802 // function : ShowGridEcho
803 // purpose  :
804 // =======================================================================
ShowGridEcho(const Handle (V3d_View)& theView,const Graphic3d_Vertex & theVertex)805 void V3d_Viewer::ShowGridEcho (const Handle(V3d_View)& theView,
806                                const Graphic3d_Vertex& theVertex)
807 {
808   if (!myGridEcho)
809   {
810     return;
811   }
812 
813   if (myGridEchoStructure.IsNull())
814   {
815     myGridEchoStructure = new Graphic3d_Structure (StructureManager());
816     myGridEchoGroup = myGridEchoStructure->NewGroup();
817 
818     myGridEchoAspect = new Graphic3d_AspectMarker3d (Aspect_TOM_STAR, Quantity_Color (Quantity_NOC_GRAY90), 3.0);
819     myGridEchoGroup->SetPrimitivesAspect (myGridEchoAspect);
820   }
821 
822   if (theVertex.X() == myGridEchoLastVert.X()
823    && theVertex.Y() == myGridEchoLastVert.Y()
824    && theVertex.Z() == myGridEchoLastVert.Z())
825   {
826     return;
827   }
828 
829   myGridEchoLastVert = theVertex;
830   myGridEchoGroup->Clear();
831   myGridEchoGroup->SetPrimitivesAspect (myGridEchoAspect);
832 
833   Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
834   anArrayOfPoints->AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z());
835   myGridEchoGroup->AddPrimitiveArray (anArrayOfPoints);
836 
837   myGridEchoStructure->SetZLayer (Graphic3d_ZLayerId_Topmost);
838   myGridEchoStructure->SetInfiniteState (Standard_True);
839   myGridEchoStructure->CStructure()->ViewAffinity = new Graphic3d_ViewAffinity();
840   myGridEchoStructure->CStructure()->ViewAffinity->SetVisible (Standard_False);
841   myGridEchoStructure->CStructure()->ViewAffinity->SetVisible (theView->View()->Identification(), true);
842   myGridEchoStructure->Display();
843 }
844 
845 // =======================================================================
846 // function : HideGridEcho
847 // purpose  :
848 // =======================================================================
HideGridEcho(const Handle (V3d_View)& theView)849 void V3d_Viewer::HideGridEcho (const Handle(V3d_View)& theView)
850 {
851   if (myGridEchoStructure.IsNull())
852   {
853     return;
854   }
855 
856   myGridEchoLastVert.SetCoord (ShortRealLast(), ShortRealLast(), ShortRealLast());
857   const Handle(Graphic3d_ViewAffinity)& anAffinity = myGridEchoStructure->CStructure()->ViewAffinity;
858   if (!anAffinity.IsNull() && anAffinity->IsVisible (theView->View()->Identification()))
859   {
860     myGridEchoStructure->Erase();
861   }
862 }
863 
864 //=======================================================================
865 //function : DumpJson
866 //purpose  :
867 //=======================================================================
DumpJson(Standard_OStream & theOStream,Standard_Integer theDepth) const868 void V3d_Viewer::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
869 {
870   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
871 
872   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myDriver.get())
873   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myStructureManager.get())
874   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myZLayerGenId)
875 
876   for (V3d_ListOfView::Iterator anIter (myDefinedViews); anIter.More(); anIter.Next())
877   {
878     const Handle(V3d_View)& aDefinedView = anIter.Value();
879     OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aDefinedView.get())
880   }
881 
882   for (V3d_ListOfView::Iterator anIter (myActiveViews); anIter.More(); anIter.Next())
883   {
884     const Handle(V3d_View)& anActiveView = anIter.Value();
885     OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, anActiveView.get())
886   }
887 
888   for (V3d_ListOfLight::Iterator anIter (myDefinedLights); anIter.More(); anIter.Next())
889   {
890     const Handle(Graphic3d_CLight)& aDefinedLight = anIter.Value();
891     OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aDefinedLight.get())
892   }
893 
894   for (V3d_ListOfLight::Iterator anIter (myActiveLights); anIter.More(); anIter.Next())
895   {
896     const Handle(Graphic3d_CLight)& anActiveLight = anIter.Value();
897     OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, anActiveLight.get())
898   }
899 
900   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myBackground)
901   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myGradientBackground)
902 
903   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myViewSize)
904   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myViewProj)
905   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myVisualization)
906   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDefaultTypeOfView)
907 
908   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myDefaultRenderingParams)
909   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myComputedMode)
910   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDefaultComputedMode)
911 
912   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myPrivilegedPlane)
913   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myPlaneStructure.get())
914   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDisplayPlane)
915   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDisplayPlaneLength)
916 
917   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myRGrid.get())
918   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myCGrid.get())
919 
920   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myGridType)
921   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myGridEcho)
922   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myGridEchoStructure.get())
923   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myGridEchoGroup.get())
924   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myGridEchoAspect.get())
925   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myGridEchoLastVert)
926 }
927