973c2be1 |
1 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
2 | // |
973c2be1 |
3 | // This file is part of Open CASCADE Technology software library. |
b311480e |
4 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
10 | // |
973c2be1 |
11 | // Alternatively, this file may be used under the terms of Open CASCADE |
12 | // commercial license or contractual agreement. |
b311480e |
13 | |
6a24c6de |
14 | #include <V3d_Viewer.hxx> |
42cf5bc1 |
15 | |
42cf5bc1 |
16 | #include <Aspect_Grid.hxx> |
c357e426 |
17 | #include <Aspect_IdentDefinitionError.hxx> |
42cf5bc1 |
18 | #include <Graphic3d_AspectMarker3d.hxx> |
19 | #include <Graphic3d_GraphicDriver.hxx> |
20 | #include <Graphic3d_Group.hxx> |
21 | #include <Graphic3d_Structure.hxx> |
c357e426 |
22 | #include <Standard_ErrorHandler.hxx> |
42cf5bc1 |
23 | #include <Standard_Type.hxx> |
7fd59977 |
24 | #include <V3d.hxx> |
6942f04a |
25 | #include <V3d_BadValue.hxx> |
42cf5bc1 |
26 | #include <V3d_CircularGrid.hxx> |
6a24c6de |
27 | #include <V3d_AmbientLight.hxx> |
28 | #include <V3d_DirectionalLight.hxx> |
42cf5bc1 |
29 | #include <V3d_RectangularGrid.hxx> |
30 | #include <V3d_View.hxx> |
7fd59977 |
31 | |
6a24c6de |
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 | } |
92efcf78 |
60 | |
c357e426 |
61 | // ======================================================================== |
62 | // function : V3d_Viewer |
63 | // purpose : |
64 | // ======================================================================== |
6942f04a |
65 | V3d_Viewer::V3d_Viewer (const Handle(Graphic3d_GraphicDriver)& theDriver, |
6a24c6de |
66 | const Standard_ExtString , |
67 | const Standard_CString , |
6942f04a |
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, |
83da37b1 |
75 | const Standard_Boolean theDefaultComputedMode) |
6a24c6de |
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)); |
6942f04a |
96 | SetDefaultViewSize (theViewSize); |
6a24c6de |
97 | SetUpdateMode (theUpdateMode); |
7fd59977 |
98 | } |
99 | |
c357e426 |
100 | // ======================================================================== |
101 | // function : CreateView |
102 | // purpose : |
103 | // ======================================================================== |
b5ac8292 |
104 | Handle(V3d_View) V3d_Viewer::CreateView () |
105 | { |
6a24c6de |
106 | return new V3d_View(this, myDefaultTypeOfView); |
7fd59977 |
107 | } |
108 | |
c357e426 |
109 | // ======================================================================== |
110 | // function : SetViewOn |
111 | // purpose : |
112 | // ======================================================================== |
113 | void V3d_Viewer::SetViewOn() |
114 | { |
6a24c6de |
115 | for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next()) |
c357e426 |
116 | { |
6a24c6de |
117 | SetViewOn (aDefViewIter.Value()); |
c357e426 |
118 | } |
7fd59977 |
119 | } |
120 | |
c357e426 |
121 | // ======================================================================== |
122 | // function : SetViewOff |
123 | // purpose : |
124 | // ======================================================================== |
125 | void V3d_Viewer::SetViewOff() |
126 | { |
6a24c6de |
127 | for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next()) |
c357e426 |
128 | { |
6a24c6de |
129 | SetViewOff (aDefViewIter.Value()); |
c357e426 |
130 | } |
7fd59977 |
131 | } |
132 | |
c357e426 |
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(); |
6a24c6de |
140 | if (!aViewImpl->IsDefined() || myActiveViews.Contains (theView)) |
c357e426 |
141 | { |
6a24c6de |
142 | return; |
143 | } |
c357e426 |
144 | |
6a24c6de |
145 | myActiveViews.Append (theView); |
146 | aViewImpl->Activate(); |
147 | for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next()) |
148 | { |
149 | theView->SetLightOn (anActiveLightIter.Value()); |
7fd59977 |
150 | } |
6a24c6de |
151 | |
152 | theView->SetGrid (myPrivilegedPlane, Grid ()); |
153 | theView->SetGridActivity (Grid ()->IsActive ()); |
154 | theView->Redraw(); |
7fd59977 |
155 | } |
156 | |
c357e426 |
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(); |
6a24c6de |
164 | if (aViewImpl->IsDefined() && myActiveViews.Contains (theView)) |
c357e426 |
165 | { |
6a24c6de |
166 | myActiveViews.Remove (theView); |
c357e426 |
167 | aViewImpl->Deactivate() ; |
7fd59977 |
168 | } |
169 | } |
170 | |
c357e426 |
171 | // ======================================================================== |
172 | // function : Redraw |
173 | // purpose : |
174 | // ======================================================================== |
6a24c6de |
175 | void V3d_Viewer::Redraw() const |
679ecdee |
176 | { |
6a24c6de |
177 | for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next()) |
c357e426 |
178 | { |
6a24c6de |
179 | aDefViewIter.Value()->Redraw(); |
c357e426 |
180 | } |
7fd59977 |
181 | } |
182 | |
c357e426 |
183 | // ======================================================================== |
184 | // function : RedrawImmediate |
185 | // purpose : |
186 | // ======================================================================== |
679ecdee |
187 | void V3d_Viewer::RedrawImmediate() const |
188 | { |
6a24c6de |
189 | for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next()) |
c357e426 |
190 | { |
6a24c6de |
191 | aDefViewIter.Value()->RedrawImmediate(); |
c357e426 |
192 | } |
679ecdee |
193 | } |
194 | |
c357e426 |
195 | // ======================================================================== |
196 | // function : Invalidate |
197 | // purpose : |
198 | // ======================================================================== |
679ecdee |
199 | void V3d_Viewer::Invalidate() const |
200 | { |
6a24c6de |
201 | for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next()) |
c357e426 |
202 | { |
6a24c6de |
203 | aDefViewIter.Value()->Invalidate(); |
c357e426 |
204 | } |
679ecdee |
205 | } |
206 | |
c357e426 |
207 | // ======================================================================== |
208 | // function : Remove |
209 | // purpose : |
210 | // ======================================================================== |
211 | void V3d_Viewer::Remove() |
212 | { |
213 | myStructureManager->Remove(); |
7fd59977 |
214 | } |
215 | |
c357e426 |
216 | // ======================================================================== |
217 | // function : Erase |
218 | // purpose : |
219 | // ======================================================================== |
220 | void V3d_Viewer::Erase() const |
221 | { |
222 | myStructureManager->Erase(); |
7fd59977 |
223 | } |
224 | |
c357e426 |
225 | // ======================================================================== |
226 | // function : UnHighlight |
227 | // purpose : |
228 | // ======================================================================== |
229 | void V3d_Viewer::UnHighlight() const |
230 | { |
231 | myStructureManager->UnHighlight(); |
7fd59977 |
232 | } |
233 | |
6a24c6de |
234 | void V3d_Viewer::SetDefaultViewSize (const Standard_Real theSize) |
b8ddfc2f |
235 | { |
6a24c6de |
236 | V3d_BadValue_Raise_if (theSize <= 0.0, "V3d_Viewer::SetDefaultViewSize, bad size"); |
237 | myViewSize = theSize; |
7fd59977 |
238 | } |
239 | |
c357e426 |
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)); |
7fd59977 |
247 | } |
248 | |
c357e426 |
249 | // ======================================================================== |
250 | // function : UpdateMode |
251 | // purpose : |
252 | // ======================================================================== |
253 | V3d_TypeOfUpdate V3d_Viewer::UpdateMode() const |
254 | { |
255 | return static_cast<V3d_TypeOfUpdate> (myStructureManager->UpdateMode()); |
7fd59977 |
256 | } |
257 | |
c357e426 |
258 | // ======================================================================== |
6a24c6de |
259 | // function : IfMoreViews |
c357e426 |
260 | // purpose : |
261 | // ======================================================================== |
6a24c6de |
262 | Standard_Boolean V3d_Viewer::IfMoreViews() const |
c357e426 |
263 | { |
6a24c6de |
264 | return myDefinedViews.Size() < myStructureManager->MaxNumOfViews(); |
7fd59977 |
265 | } |
266 | |
6a24c6de |
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 | } |
7fd59977 |
277 | } |
278 | |
6a24c6de |
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); |
7fd59977 |
287 | } |
288 | |
c5751993 |
289 | //======================================================================= |
c357e426 |
290 | //function : AddZLayer |
c5751993 |
291 | //purpose : |
292 | //======================================================================= |
c357e426 |
293 | Standard_Boolean V3d_Viewer::AddZLayer (Standard_Integer& theLayerId) |
c5751993 |
294 | { |
c357e426 |
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; |
c5751993 |
310 | } |
311 | |
312 | //======================================================================= |
c357e426 |
313 | //function : RemoveZLayer |
314 | //purpose : |
c5751993 |
315 | //======================================================================= |
c357e426 |
316 | Standard_Boolean V3d_Viewer::RemoveZLayer (const Standard_Integer theLayerId) |
c5751993 |
317 | { |
c357e426 |
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; |
c5751993 |
330 | } |
331 | |
59f45b7c |
332 | //======================================================================= |
c357e426 |
333 | //function : GetAllZLayers |
59f45b7c |
334 | //purpose : |
335 | //======================================================================= |
c357e426 |
336 | void V3d_Viewer::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const |
59f45b7c |
337 | { |
c357e426 |
338 | myDriver->ZLayers (theLayerSeq); |
59f45b7c |
339 | } |
340 | |
341 | //======================================================================= |
c357e426 |
342 | //function : SetZLayerSettings |
343 | //purpose : |
59f45b7c |
344 | //======================================================================= |
c357e426 |
345 | void V3d_Viewer::SetZLayerSettings (const Standard_Integer theLayerId, const Graphic3d_ZLayerSettings& theSettings) |
59f45b7c |
346 | { |
c357e426 |
347 | myDriver->SetZLayerSettings (theLayerId, theSettings); |
59f45b7c |
348 | } |
349 | |
350 | //======================================================================= |
c357e426 |
351 | //function : ZLayerSettings |
59f45b7c |
352 | //purpose : |
353 | //======================================================================= |
c357e426 |
354 | Graphic3d_ZLayerSettings V3d_Viewer::ZLayerSettings (const Standard_Integer theLayerId) |
59f45b7c |
355 | { |
c357e426 |
356 | return myDriver->ZLayerSettings (theLayerId); |
59f45b7c |
357 | } |
6942f04a |
358 | |
359 | //======================================================================= |
6a24c6de |
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 |
6942f04a |
373 | //purpose : |
374 | //======================================================================= |
6a24c6de |
375 | void V3d_Viewer::SetLightOn (const Handle(V3d_Light)& theLight) |
6942f04a |
376 | { |
6a24c6de |
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 | } |
6942f04a |
386 | } |
387 | |
388 | //======================================================================= |
6a24c6de |
389 | //function : SetLightOff |
6942f04a |
390 | //purpose : |
391 | //======================================================================= |
6a24c6de |
392 | void V3d_Viewer::SetLightOff (const Handle(V3d_Light)& theLight) |
6942f04a |
393 | { |
6a24c6de |
394 | myActiveLights.Remove (theLight); |
395 | for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next()) |
396 | { |
397 | anActiveViewIter.Value()->SetLightOff (theLight); |
398 | } |
6942f04a |
399 | } |
400 | |
401 | //======================================================================= |
6a24c6de |
402 | //function : SetLightOn |
6942f04a |
403 | //purpose : |
404 | //======================================================================= |
6a24c6de |
405 | void V3d_Viewer::SetLightOn() |
6942f04a |
406 | { |
6a24c6de |
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 | } |
6942f04a |
418 | } |
419 | |
420 | //======================================================================= |
6a24c6de |
421 | //function : SetLightOff |
6942f04a |
422 | //purpose : |
423 | //======================================================================= |
6a24c6de |
424 | void V3d_Viewer::SetLightOff() |
6942f04a |
425 | { |
6a24c6de |
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(); |
973c2be1 |
434 | } |
832ae82d |
435 | |
436 | //======================================================================= |
6a24c6de |
437 | //function : IsGlobalLight |
832ae82d |
438 | //purpose : |
439 | //======================================================================= |
6a24c6de |
440 | Standard_Boolean V3d_Viewer::IsGlobalLight (const Handle(V3d_Light)& theLight) const |
832ae82d |
441 | { |
6a24c6de |
442 | return myActiveLights.Contains (theLight); |
832ae82d |
443 | } |
444 | |
445 | //======================================================================= |
6a24c6de |
446 | //function : AddLight |
832ae82d |
447 | //purpose : |
448 | //======================================================================= |
6a24c6de |
449 | void V3d_Viewer::AddLight (const Handle(V3d_Light)& theLight) |
832ae82d |
450 | { |
6a24c6de |
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)); |
832ae82d |
481 | } |