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