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