0027900: Coding rules - drop redundant Name parameter from V3d_Viewer constructor
[occt.git] / src / V3d / V3d_Viewer.cxx
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_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>
24 #include <V3d.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>
31
32 IMPLEMENT_STANDARD_RTTIEXT(V3d_Viewer, Standard_Transient)
33
34 // ========================================================================
35 // function : V3d_Viewer
36 // purpose  :
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),
43   myViewSize (1000.0),
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())
56 {
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));
59 }
60
61 // ========================================================================
62 // function : V3d_Viewer
63 // purpose  :
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 V3d_TypeOfUpdate        theUpdateMode,
74                         const Standard_Boolean        theComputedMode,
75                         const Standard_Boolean        theDefaultComputedMode)
76 : myDriver (theDriver),
77   myStructureManager (new Graphic3d_StructureManager (theDriver)),
78   myZLayerGenId (1, IntegerLast()),
79   myBackground (theViewBackground),
80   myViewSize (theViewSize),
81   myViewProj (theViewProj),
82   myVisualization (theVisualization),
83   myShadingModel (theShadingModel),
84   myDefaultTypeOfView (V3d_ORTHOGRAPHIC),
85   myComputedMode (theComputedMode),
86   myDefaultComputedMode (theDefaultComputedMode),
87   myPrivilegedPlane (gp_Ax3 (gp_Pnt (0.,0.,0), gp_Dir (0.,0.,1.), gp_Dir (1.,0.,0.))),
88   myDisplayPlane (Standard_False),
89   myDisplayPlaneLength (theViewSize),
90   myGridType (Aspect_GT_Rectangular),
91   myGridEcho (Standard_True),
92   myGridEchoLastVert (ShortRealLast(), ShortRealLast(), ShortRealLast())
93 {
94   myRGrid = new V3d_RectangularGrid (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
95   myCGrid = new V3d_CircularGrid    (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
96   SetDefaultViewSize (theViewSize);
97   SetUpdateMode (theUpdateMode);
98 }
99
100 // ========================================================================
101 // function : CreateView
102 // purpose  :
103 // ========================================================================
104 Handle(V3d_View) V3d_Viewer::CreateView ()
105 {
106   return new V3d_View(this, myDefaultTypeOfView);
107 }
108
109 // ========================================================================
110 // function : SetViewOn
111 // purpose  :
112 // ========================================================================
113 void V3d_Viewer::SetViewOn()
114 {
115   for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
116   {
117     SetViewOn (aDefViewIter.Value());
118   }
119 }
120
121 // ========================================================================
122 // function : SetViewOff
123 // purpose  :
124 // ========================================================================
125 void V3d_Viewer::SetViewOff()
126 {
127   for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
128   {
129     SetViewOff (aDefViewIter.Value());
130   }
131 }
132
133 // ========================================================================
134 // function : SetViewOn
135 // purpose  :
136 // ========================================================================
137 void V3d_Viewer::SetViewOn (const Handle(V3d_View)& theView)
138 {
139   Handle(Graphic3d_CView) aViewImpl = theView->View();
140   if (!aViewImpl->IsDefined() || myActiveViews.Contains (theView))
141   {
142     return;
143   }
144
145   myActiveViews.Append (theView);
146   aViewImpl->Activate();
147   for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
148   {
149     theView->SetLightOn (anActiveLightIter.Value());
150   }
151
152   theView->SetGrid (myPrivilegedPlane, Grid ());
153   theView->SetGridActivity (Grid ()->IsActive ());
154   theView->Redraw();
155 }
156
157 // ========================================================================
158 // function : SetViewOff
159 // purpose  :
160 // ========================================================================
161 void V3d_Viewer::SetViewOff (const Handle(V3d_View)& theView)
162 {
163   Handle(Graphic3d_CView) aViewImpl = theView->View();
164   if (aViewImpl->IsDefined() && myActiveViews.Contains (theView))
165   {
166     myActiveViews.Remove (theView);
167     aViewImpl->Deactivate() ;
168   }
169 }
170
171 // ========================================================================
172 // function : Redraw
173 // purpose  :
174 // ========================================================================
175 void V3d_Viewer::Redraw() const
176 {
177   for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
178   {
179     aDefViewIter.Value()->Redraw();
180   }
181 }
182
183 // ========================================================================
184 // function : RedrawImmediate
185 // purpose  :
186 // ========================================================================
187 void V3d_Viewer::RedrawImmediate() const
188 {
189   for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
190   {
191     aDefViewIter.Value()->RedrawImmediate();
192   }
193 }
194
195 // ========================================================================
196 // function : Invalidate
197 // purpose  :
198 // ========================================================================
199 void V3d_Viewer::Invalidate() const
200 {
201   for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
202   {
203     aDefViewIter.Value()->Invalidate();
204   }
205 }
206
207 // ========================================================================
208 // function : Remove
209 // purpose  :
210 // ========================================================================
211 void V3d_Viewer::Remove()
212 {
213   myStructureManager->Remove();
214 }
215
216 // ========================================================================
217 // function : Erase
218 // purpose  :
219 // ========================================================================
220 void V3d_Viewer::Erase() const
221 {
222   myStructureManager->Erase();
223 }
224
225 // ========================================================================
226 // function : UnHighlight
227 // purpose  :
228 // ========================================================================
229 void V3d_Viewer::UnHighlight() const
230 {
231   myStructureManager->UnHighlight();
232 }
233
234 void V3d_Viewer::SetDefaultViewSize (const Standard_Real theSize)
235 {
236   V3d_BadValue_Raise_if (theSize <= 0.0, "V3d_Viewer::SetDefaultViewSize, bad size");
237   myViewSize = theSize;
238 }
239
240 // ========================================================================
241 // function : SetUpdateMode
242 // purpose  :
243 // ========================================================================
244 void V3d_Viewer::SetUpdateMode (const V3d_TypeOfUpdate theMode)
245 {
246   myStructureManager->SetUpdateMode (static_cast<Aspect_TypeOfUpdate> (theMode));
247 }
248
249 // ========================================================================
250 // function : UpdateMode
251 // purpose  :
252 // ========================================================================
253 V3d_TypeOfUpdate V3d_Viewer::UpdateMode() const
254 {
255   return static_cast<V3d_TypeOfUpdate> (myStructureManager->UpdateMode());
256 }
257
258 // ========================================================================
259 // function : IfMoreViews
260 // purpose  :
261 // ========================================================================
262 Standard_Boolean V3d_Viewer::IfMoreViews() const
263 {
264   return myDefinedViews.Size() < myStructureManager->MaxNumOfViews();
265 }
266
267 // ========================================================================
268 // function : AddView
269 // purpose  :
270 // ========================================================================
271 void V3d_Viewer::AddView (const Handle(V3d_View)& theView)
272 {
273   if (!myDefinedViews.Contains (theView))
274   {
275     myDefinedViews.Append (theView);
276   }
277 }
278
279 // ========================================================================
280 // function : DelView
281 // purpose  :
282 // ========================================================================
283 void V3d_Viewer::DelView (const Handle(V3d_View)& theView)
284 {
285   myActiveViews.Remove (theView);
286   myDefinedViews.Remove (theView);
287 }
288
289 //=======================================================================
290 //function : AddZLayer
291 //purpose  :
292 //=======================================================================
293 Standard_Boolean V3d_Viewer::AddZLayer (Standard_Integer& theLayerId)
294 {
295   try
296   {
297     OCC_CATCH_SIGNALS
298     theLayerId = myZLayerGenId.Next();
299   }
300   catch (Aspect_IdentDefinitionError)
301   {
302     // new index can't be generated
303     return Standard_False;
304   }
305
306   myLayerIds.Add (theLayerId);
307   myDriver->AddZLayer (theLayerId);
308
309   return Standard_True;
310 }
311
312 //=======================================================================
313 //function : RemoveZLayer
314 //purpose  : 
315 //=======================================================================
316 Standard_Boolean V3d_Viewer::RemoveZLayer (const Standard_Integer theLayerId)
317 {
318   if (!myLayerIds.Contains (theLayerId)
319     || theLayerId < myZLayerGenId.Lower()
320     || theLayerId > myZLayerGenId.Upper())
321   {
322     return Standard_False;
323   }
324
325   myDriver->RemoveZLayer (theLayerId);
326   myLayerIds.Remove  (theLayerId);
327   myZLayerGenId.Free (theLayerId);
328
329   return Standard_True;
330 }
331
332 //=======================================================================
333 //function : GetAllZLayers
334 //purpose  :
335 //=======================================================================
336 void V3d_Viewer::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
337 {
338   myDriver->ZLayers (theLayerSeq);
339 }
340
341 //=======================================================================
342 //function : SetZLayerSettings
343 //purpose  :
344 //=======================================================================
345 void V3d_Viewer::SetZLayerSettings (const Standard_Integer theLayerId, const Graphic3d_ZLayerSettings& theSettings)
346 {
347   myDriver->SetZLayerSettings (theLayerId, theSettings);
348 }
349
350 //=======================================================================
351 //function : ZLayerSettings
352 //purpose  :
353 //=======================================================================
354 Graphic3d_ZLayerSettings V3d_Viewer::ZLayerSettings (const Standard_Integer theLayerId)
355 {
356   return myDriver->ZLayerSettings (theLayerId);
357 }
358
359 //=======================================================================
360 //function : UpdateLights
361 //purpose  :
362 //=======================================================================
363 void V3d_Viewer::UpdateLights()
364 {
365   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
366   {
367     anActiveViewIter.Value()->UpdateLights();
368   }
369 }
370
371 //=======================================================================
372 //function : SetLightOn
373 //purpose  :
374 //=======================================================================
375 void V3d_Viewer::SetLightOn (const Handle(V3d_Light)& theLight)
376 {
377   if (!myActiveLights.Contains (theLight))
378   {
379     myActiveLights.Append (theLight);
380   }
381
382   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
383   {
384     anActiveViewIter.Value()->SetLightOn (theLight);
385   }
386 }
387
388 //=======================================================================
389 //function : SetLightOff
390 //purpose  :
391 //=======================================================================
392 void V3d_Viewer::SetLightOff (const Handle(V3d_Light)& theLight)
393 {
394   myActiveLights.Remove (theLight);
395   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
396   {
397     anActiveViewIter.Value()->SetLightOff (theLight);
398   }
399 }
400
401 //=======================================================================
402 //function : SetLightOn
403 //purpose  :
404 //=======================================================================
405 void V3d_Viewer::SetLightOn()
406 {
407   for (V3d_ListOfLight::Iterator aDefLightIter (myDefinedLights); aDefLightIter.More(); aDefLightIter.Next())
408   {
409     if (!myActiveLights.Contains (aDefLightIter.Value()))
410     {
411       myActiveLights.Append (aDefLightIter.Value());
412       for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
413       {
414         anActiveViewIter.Value()->SetLightOn (aDefLightIter.Value());
415       }
416     }
417   }
418 }
419
420 //=======================================================================
421 //function : SetLightOff
422 //purpose  :
423 //=======================================================================
424 void V3d_Viewer::SetLightOff()
425 {
426   for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
427   {
428     for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
429     {
430       anActiveViewIter.Value()->SetLightOff (anActiveLightIter.Value());
431     }
432   }
433   myActiveLights.Clear();
434 }
435
436 //=======================================================================
437 //function : IsGlobalLight
438 //purpose  :
439 //=======================================================================
440 Standard_Boolean V3d_Viewer::IsGlobalLight (const Handle(V3d_Light)& theLight) const
441 {
442   return myActiveLights.Contains (theLight);
443 }
444
445 //=======================================================================
446 //function : AddLight
447 //purpose  :
448 //=======================================================================
449 void V3d_Viewer::AddLight (const Handle(V3d_Light)& theLight)
450 {
451   if (!myDefinedLights.Contains (theLight))
452   {
453     myDefinedLights.Append (theLight);
454   }
455 }
456
457 //=======================================================================
458 //function : DelLight
459 //purpose  :
460 //=======================================================================
461 void V3d_Viewer::DelLight (const Handle(V3d_Light)& theLight)
462 {
463   SetLightOff (theLight);
464   myDefinedLights.Remove (theLight);
465 }
466
467 //=======================================================================
468 //function : SetDefaultLights
469 //purpose  :
470 //=======================================================================
471 void V3d_Viewer::SetDefaultLights()
472 {
473   while (!myDefinedLights.IsEmpty())
474   {
475     Handle(V3d_Light) aLight = myDefinedLights.First();
476     DelLight (aLight);
477   }
478
479   SetLightOn (new V3d_DirectionalLight (this, V3d_Zneg, Quantity_NOC_WHITE, Standard_True));
480   SetLightOn (new V3d_AmbientLight (this));
481 }