26761bde67c622450ba86561a6f6a582cfe15558
[occt.git] / src / Visual3d / Visual3d_View.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifdef _WIN32
16   #include <windows.h>
17 #endif
18
19 #include <Visual3d_View.ixx>
20
21 #include <Graphic3d_DataStructureManager.hxx>
22 #include <Graphic3d_GraphicDriver.hxx>
23 #include <Graphic3d_MapOfStructure.hxx>
24 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
25 #include <Graphic3d_Structure.hxx>
26 #include <Graphic3d_TextureEnv.hxx>
27 #include <Graphic3d_Vector.hxx>
28 #include <Graphic3d_Vertex.hxx>
29 #include <Visual3d_DepthCueingDefinitionError.hxx>
30 #include <Visual3d_Light.hxx>
31 #include <Visual3d_HSequenceOfView.hxx>
32 #include <Visual3d_ZClippingDefinitionError.hxx>
33 #include <OSD.hxx>
34 #include <TColStd_HArray2OfReal.hxx>
35
36 #if defined(_WIN32)
37   #include <WNT_Window.hxx>
38 #elif (defined(__APPLE__) && !defined(MACOSX_USE_GLX))
39   #include <Cocoa_Window.hxx>
40 #else
41   #include <Xw_Window.hxx>
42 #endif
43
44 #include <float.h>
45
46 // =======================================================================
47 // function : Visual3d_View
48 // purpose  :
49 // =======================================================================
50 Visual3d_View::Visual3d_View (const Handle(Visual3d_ViewManager)& theMgr)
51 : myViewManager         (theMgr.operator->()),
52   myIsInComputedMode    (Standard_False),
53   myAutoZFitIsOn        (Standard_True),
54   myAutoZFitScaleFactor (1.0),
55   myStructuresUpdated   (Standard_True)
56 {
57   MyCView.ViewId                  = theMgr->Identification (this);
58   MyCView.Active                  = 0;
59   MyCView.IsDeleted               = 0;
60   MyCView.WsId                    = -1;
61   MyCView.DefWindow.IsDefined     = 0;
62   MyCView.Context.NbActiveLight   = 0;
63   MyCView.Context.ZBufferActivity = -1;
64
65   MyCView.Backfacing    = 0;
66   MyCView.ptrUnderLayer = 0;
67   MyCView.ptrOverLayer  = 0;
68   MyCView.GContext      = 0;
69   MyCView.GDisplayCB    = 0;
70   MyCView.GClientData   = 0;
71
72   myGraphicDriver = myViewManager->GraphicDriver();
73 }
74
75 // =======================================================================
76 // function : SetWindow
77 // purpose  :
78 // =======================================================================
79 void Visual3d_View::SetWindow (const Handle(Aspect_Window)&      theWindow,
80                                const Aspect_RenderingContext     theContext,
81                                const Aspect_GraphicCallbackProc& theDisplayCB,
82                                const Standard_Address            theClientData)
83 {
84   if (IsDeleted())
85   {
86     return;
87   }
88   else if (IsDefined())
89   {
90     Visual3d_ViewDefinitionError::Raise ("Window already defined");
91   }
92
93   MyCView.GContext    = theContext;
94   MyCView.GDisplayCB  = theDisplayCB;
95   MyCView.GClientData = theClientData;
96   SetWindow (theWindow);
97 }
98
99 // =======================================================================
100 // function : SetWindow
101 // purpose  :
102 // =======================================================================
103 void Visual3d_View::SetWindow (const Handle(Aspect_Window)& theWindow)
104 {
105   if (IsDeleted())
106   {
107     return;
108   }
109   else if (IsDefined())
110   {
111     Visual3d_ViewDefinitionError::Raise ("Window already defined");
112   }
113
114   MyWindow = theWindow;
115   MyCView.WsId = MyCView.ViewId;
116   MyCView.DefWindow.IsDefined = 1;
117 #if defined(_WIN32)
118   const Handle(WNT_Window) aWin   = Handle(WNT_Window)::DownCast (theWindow);
119   MyCView.DefWindow.XWindow       = (HWND )(aWin->HWindow());
120   MyCView.DefWindow.XParentWindow = (HWND )(aWin->HParentWindow());
121 #elif (defined(__APPLE__) && !defined(MACOSX_USE_GLX))
122   const Handle(Cocoa_Window) aWin = Handle(Cocoa_Window)::DownCast (theWindow);
123   MyCView.DefWindow.XWindow       = (Aspect_Drawable )aWin->HView();
124   MyCView.DefWindow.XParentWindow = NULL;
125   //MyCView.DefWindow.XParentWindow = aWin->HParentWindow();
126 #elif defined(__ANDROID__)
127   //
128 #else
129   const Handle(Xw_Window) aWin    = Handle(Xw_Window)::DownCast (theWindow);
130   MyCView.DefWindow.XWindow       = aWin->XWindow();
131   //MyCView.DefWindow.XParentWindow = aWin->XParentWindow();
132 #endif
133
134   Standard_Integer Width, Height;
135   theWindow->Size (Width, Height);
136   MyCView.DefWindow.dx = float(Width);
137   MyCView.DefWindow.dy = float(Height);
138
139   Standard_Real R, G, B;
140   MyBackground = MyWindow->Background ();
141   MyBackground.Color().Values (R, G, B, Quantity_TOC_RGB);
142   MyCView.DefWindow.Background.r = float (R);
143   MyCView.DefWindow.Background.g = float (G);
144   MyCView.DefWindow.Background.b = float (B);
145
146   UpdateView();
147   if (!myGraphicDriver->View (MyCView))
148   {
149     Visual3d_ViewDefinitionError::Raise ("Association failed");
150   }
151
152   MyGradientBackground = MyWindow->GradientBackground();
153   SetGradientBackground(MyGradientBackground,1);
154
155   Standard_Boolean AWait = Standard_False; // => immediate update
156   myGraphicDriver->SetVisualisation (MyCView);
157   myGraphicDriver->AntiAliasing (MyCView, MyContext.AliasingIsOn());
158   myGraphicDriver->DepthCueing  (MyCView, MyContext.DepthCueingIsOn());
159   myGraphicDriver->ClipLimit    (MyCView, AWait);
160   myGraphicDriver->Environment  (MyCView);
161
162   // Make view manager z layer list consistent with the view's list.
163   myViewManager->InstallZLayers (this);
164
165   // Update planses of model clipping
166   UpdatePlanes();
167
168   // Update light sources
169   UpdateLights();
170
171   // Association view-window does not cause the display
172   // of structures that can be displayed in the new view.
173   // In fact, association view-window is done, but the
174   // display is produced only if the view is activated (Activate).
175   SetRatio();
176 }
177
178 // =======================================================================
179 // function : Window
180 // purpose  :
181 // =======================================================================
182 Handle(Aspect_Window) Visual3d_View::Window() const
183 {
184   if (!IsDefined())
185   {
186     Visual3d_ViewDefinitionError::Raise ("Window not defined");
187   }
188   return MyWindow;
189 }
190
191 // =======================================================================
192 // function : IsDefined
193 // purpose  :
194 // =======================================================================
195 Standard_Boolean Visual3d_View::IsDefined() const
196 {
197   return MyCView.DefWindow.IsDefined != 0;
198 }
199
200 // =======================================================================
201 // function : IsDeleted
202 // purpose  :
203 // =======================================================================
204 Standard_Boolean Visual3d_View::IsDeleted() const
205 {
206   return MyCView.IsDeleted != 0;
207 }
208
209 // =======================================================================
210 // function : Destroy
211 // purpose  :
212 // =======================================================================
213 void Visual3d_View::Destroy()
214 {
215   // Since myViewManager can be already destroyed,
216   // avoid attempts to access it in SetBackground()
217   myViewManager = NULL;
218   Remove();
219 }
220
221 // =======================================================================
222 // function : Remove
223 // purpose  :
224 // =======================================================================
225 void Visual3d_View::Remove()
226 {
227   if (IsDeleted()
228   || !IsDefined())
229   {
230     return;
231   }
232
233   myStructsToCompute.Clear();
234   myStructsComputed .Clear();
235   myStructsDisplayed.Clear();
236
237   Aspect_GradientBackground aBlackGrad;
238   SetBackground (Aspect_Background (Quantity_NOC_BLACK));
239   SetGradientBackground (aBlackGrad, Standard_False);
240
241   if (myViewManager != NULL)
242   {
243     myViewManager->UnIdentification (MyCView.ViewId);
244   }
245
246   myGraphicDriver->RemoveView (MyCView);
247
248   MyCView.WsId                = -1;
249   MyCView.IsDeleted           = 1;
250   MyCView.DefWindow.IsDefined = 0;
251
252   MyWindow.Nullify();
253 }
254
255 // =======================================================================
256 // function : Resized
257 // purpose  :
258 // =======================================================================
259 void Visual3d_View::Resized()
260 {
261   if (IsDeleted())
262   {
263     return;
264   }
265   else if (!IsDefined())
266   {
267     Visual3d_ViewDefinitionError::Raise ("Window not defined");
268   }
269   MyWindow->DoResize();
270   SetRatio();
271 }
272
273 // =======================================================================
274 // function : SetRatio
275 // purpose  :
276 // =======================================================================
277 void Visual3d_View::SetRatio()
278 {
279   if (IsDeleted())
280   {
281     return;
282   }
283
284   const Aspect_TypeOfUpdate anUpdateMode = myViewManager->UpdateMode();
285   myViewManager->SetUpdateMode (Aspect_TOU_WAIT);
286
287   Standard_Integer aWidth, aHeight;
288   MyWindow->Size (aWidth, aHeight);
289   if (aWidth > 0 && aHeight > 0)
290   {
291     Standard_Real aRatio = (Standard_Real)aWidth / (Standard_Real)aHeight; 
292
293     MyCView.DefWindow.dx = Standard_ShortReal (aWidth);
294     MyCView.DefWindow.dy = Standard_ShortReal (aHeight);
295
296     myGraphicDriver->RatioWindow (MyCView);
297
298     // Update camera aspect
299     const Handle(Graphic3d_Camera)& aCamera = MyCView.Context.Camera;
300     if (!aCamera.IsNull())
301     {
302       aCamera->SetAspect (aRatio);
303     }
304
305     if (!myDefaultCamera.IsNull())
306     {
307       myDefaultCamera->SetAspect (aRatio);
308     }
309   }
310
311   myViewManager->SetUpdateMode (anUpdateMode);
312   Update (anUpdateMode);
313 }
314
315 // =======================================================================
316 // function : UpdateLights
317 // purpose  :
318 // =======================================================================
319 void Visual3d_View::UpdateLights()
320 {
321   if (IsDeleted()
322    || !IsDefined())
323   {
324     return;
325   }
326
327   if (MyContext.Model() == Visual3d_TOM_NONE)
328   {
329     // activate only a white ambient light
330     Graphic3d_CLight aCLight;
331     aCLight.Type        = Visual3d_TOLS_AMBIENT;
332     aCLight.IsHeadlight = Standard_False;
333     aCLight.Color.r() = aCLight.Color.g() = aCLight.Color.b() = 1.0f;
334
335     MyCView.Context.NbActiveLight = 1;
336     MyCView.Context.ActiveLight   = &aCLight;
337     myGraphicDriver->SetLight (MyCView);
338     MyCView.Context.ActiveLight   = NULL;
339     return;
340   }
341
342   MyCView.Context.NbActiveLight = Min (MyContext.NumberOfActivatedLights(),
343                                        myGraphicDriver->InquireLightLimit());
344   if (MyCView.Context.NbActiveLight < 1)
345   {
346     myGraphicDriver->SetLight (MyCView);
347     return;
348   }
349
350   // parcing of light sources
351   MyCView.Context.ActiveLight = new Graphic3d_CLight[MyCView.Context.NbActiveLight];
352   for (Standard_Integer aLightIter = 0; aLightIter < MyCView.Context.NbActiveLight; ++aLightIter)
353   {
354     MyCView.Context.ActiveLight[aLightIter] = MyContext.ActivatedLight (aLightIter + 1)->CLight();
355   }
356   myGraphicDriver->SetLight (MyCView);
357   delete[] MyCView.Context.ActiveLight;
358   MyCView.Context.ActiveLight = NULL;
359 }
360
361 // =======================================================================
362 // function : UpdatePlanes
363 // purpose  :
364 // =======================================================================
365 void Visual3d_View::UpdatePlanes()
366 {
367   MyCView.Context.ClipPlanes = MyContext.ClipPlanes();
368   if (IsDeleted() || !IsDefined())
369   {
370     return;
371   }
372
373   myGraphicDriver->SetClipPlanes (MyCView);
374 }
375
376 // =======================================================================
377 // function : SetBackground
378 // purpose  :
379 // =======================================================================
380 void Visual3d_View::SetBackground (const Aspect_Background& theBack)
381 {
382   if (IsDeleted())
383   {
384     return;
385   }
386   else if (!IsDefined())
387   {
388     Visual3d_ViewDefinitionError::Raise ("Window not defined");
389   }
390
391   // At this level, only GL can update the background.
392   // It is not necessary to call MyWindow->SetBackground (ABack); as
393   // this method starts update of window background by X
394   // (if the windowing is X)
395
396   Standard_Real R, G, B;
397   MyBackground = theBack;
398   MyBackground.Color().Values (R, G, B, Quantity_TOC_RGB);
399   MyCView.DefWindow.Background.r = float (R);
400   MyCView.DefWindow.Background.g = float (G);
401   MyCView.DefWindow.Background.b = float (B);
402
403   myGraphicDriver->Background (MyCView);
404
405   if (myViewManager != NULL)
406   {
407     Update (myViewManager->UpdateMode());
408   }
409 }
410
411 // =======================================================================
412 // function : SetGradientBackground
413 // purpose  :
414 // =======================================================================
415 void Visual3d_View::SetGradientBackground (const Aspect_GradientBackground& theBack,
416                                            const Standard_Boolean           theToUpdate)
417 {
418   if (IsDeleted())
419   {
420     return;
421   }
422   else if (!IsDefined())
423   {
424     Visual3d_ViewDefinitionError::Raise ("Window not defined");
425   }
426
427   MyGradientBackground = theBack;
428   Quantity_Color aCol1, aCol2;
429   MyGradientBackground.Colors (aCol1, aCol2);
430   myGraphicDriver->GradientBackground (MyCView, aCol1, aCol2, MyGradientBackground.BgGradientFillMethod());
431
432   if (theToUpdate)
433   {
434      Update (Aspect_TOU_ASAP);
435   }
436   else if (myViewManager != NULL)
437   {
438     Update (myViewManager->UpdateMode());
439   }
440 }
441
442 // =======================================================================
443 // function : SetBackgroundImage
444 // purpose  :
445 // =======================================================================
446 void Visual3d_View::SetBackgroundImage (const Standard_CString  theFileName,
447                                         const Aspect_FillMethod theFillStyle,
448                                         const Standard_Boolean  theToUpdate)
449 {
450   if (IsDeleted())
451   {
452     return;
453   }
454   if (!IsDefined())
455   {
456     Visual3d_ViewDefinitionError::Raise ("Window not defined");
457   }
458
459   myGraphicDriver->BackgroundImage (theFileName, MyCView, theFillStyle);
460
461   Update (theToUpdate ? Aspect_TOU_ASAP : myViewManager->UpdateMode());
462 }
463
464 // =======================================================================
465 // function : SetBgImageStyle
466 // purpose  :
467 // =======================================================================
468 void Visual3d_View::SetBgImageStyle (const Aspect_FillMethod theFillStyle,
469                                      const Standard_Boolean  theToUpdate)
470 {
471   if (IsDeleted())
472   {
473     return;
474   }
475   if (!IsDefined())
476   {
477     Visual3d_ViewDefinitionError::Raise ("Window not defined");
478   }
479
480   myGraphicDriver->SetBgImageStyle (MyCView, theFillStyle);
481
482   Update (theToUpdate ? Aspect_TOU_ASAP : myViewManager->UpdateMode());
483 }
484
485 // =======================================================================
486 // function : Background
487 // purpose  :
488 // =======================================================================
489 Aspect_Background Visual3d_View::Background() const
490 {
491   return MyBackground;
492 }
493
494 // =======================================================================
495 // function : SetBgGradientStyle
496 // purpose  :
497 // =======================================================================
498 void Visual3d_View::SetBgGradientStyle (const Aspect_GradientFillMethod theFillStyle,
499                                         const Standard_Boolean          theToUpdate)
500 {
501   if (IsDeleted())
502   {
503     return;
504   }
505   else if (!IsDefined())
506   {
507     Visual3d_ViewDefinitionError::Raise ("Window not defined");
508   }
509
510   myGraphicDriver->SetBgGradientStyle (MyCView, theFillStyle);
511
512   Update (theToUpdate ? Aspect_TOU_ASAP : myViewManager->UpdateMode());
513 }
514
515 // =======================================================================
516 // function : GradientBackground
517 // purpose  :
518 // =======================================================================
519 Aspect_GradientBackground Visual3d_View::GradientBackground() const
520 {
521   return MyGradientBackground;
522 }
523
524 // =======================================================================
525 // function : DefaultCamera
526 // purpose  :
527 // =======================================================================
528 const Handle(Graphic3d_Camera)& Visual3d_View::DefaultCamera() const
529 {
530   return myDefaultCamera;
531 }
532
533 // =======================================================================
534 // function : Camera
535 // purpose  :
536 // =======================================================================
537 const Handle(Graphic3d_Camera)& Visual3d_View::Camera() const
538 {
539   return MyCView.Context.Camera;
540 }
541
542 // =======================================================================
543 // function : SetCamera
544 // purpose  :
545 // =======================================================================
546 void Visual3d_View::SetCamera (const Handle(Graphic3d_Camera)& theCamera)
547 {
548   MyCView.Context.Camera = theCamera;
549
550   myGraphicDriver->SetCamera (MyCView);
551
552   Update (myViewManager->UpdateMode());
553 }
554
555 // =======================================================================
556 // function : SetViewOrientationDefault
557 // purpose  :
558 // =======================================================================
559 void Visual3d_View::SetViewOrientationDefault()
560 {
561   if (myDefaultCamera.IsNull())
562   {
563     myDefaultCamera = new Graphic3d_Camera();
564   }
565
566   myDefaultCamera->CopyOrientationData (MyCView.Context.Camera);
567 }
568
569 // =======================================================================
570 // function : ViewOrientationReset
571 // purpose  :
572 // =======================================================================
573 void Visual3d_View::ViewOrientationReset()
574 {
575   if (IsDeleted())
576   {
577     return;
578   }
579
580   if (!myDefaultCamera.IsNull())
581   {
582     MyCView.Context.Camera->CopyOrientationData (myDefaultCamera);
583   }
584
585   Update (myViewManager->UpdateMode());
586 }
587
588 // =======================================================================
589 // function : SetViewMappingDefault
590 // purpose  :
591 // =======================================================================
592 void Visual3d_View::SetViewMappingDefault()
593 {
594   if (myDefaultCamera.IsNull())
595   {
596     myDefaultCamera = new Graphic3d_Camera();
597   }
598   myDefaultCamera->CopyMappingData (MyCView.Context.Camera);
599 }
600
601 // =======================================================================
602 // function : ViewMappingReset
603 // purpose  :
604 // =======================================================================
605 void Visual3d_View::ViewMappingReset()
606 {
607   if (IsDeleted())
608   {
609     return;
610   }
611
612   if (!myDefaultCamera.IsNull())
613   {
614     MyCView.Context.Camera->CopyMappingData (myDefaultCamera);
615   }
616
617   Update (myViewManager->UpdateMode());
618 }
619
620 // =======================================================================
621 // function : SetContext
622 // purpose  :
623 // =======================================================================
624 void Visual3d_View::SetContext (const Visual3d_ContextView& theViewCtx)
625 {
626   if (IsDeleted())
627   {
628     return;
629   }
630
631   // To manage display only in case of change of visualisation mode
632   const bool isVisModeChanged = theViewCtx.Visualization() != MyContext.Visualization();
633   const bool isModelChanged   = theViewCtx.Model()         != MyContext.Model();
634
635   // To manage antialiasing only in case of change
636   const Standard_Boolean anAliasingModeOld = MyContext.AliasingIsOn();
637   const Standard_Boolean anAliasingModeNew = theViewCtx.AliasingIsOn();
638
639   // To manage the depth cueing only in case of change
640   const Standard_Boolean aDepthCueingModeOld = MyContext.DepthCueingIsOn();
641   const Standard_Boolean aDepthCueingModeNew = theViewCtx.DepthCueingIsOn();
642
643   const Standard_Real aDepthCueingFrontPlaneOld = MyContext.DepthCueingFrontPlane();
644   const Standard_Real aDepthCueingFrontPlaneNew = theViewCtx.DepthCueingFrontPlane();
645   const Standard_Real aDepthCueingBackPlaneOld  = MyContext.DepthCueingBackPlane();
646   const Standard_Real aDepthCueingBackPlaneNew  = theViewCtx.DepthCueingBackPlane();
647
648   // To manage the Zclipping only in case of change
649   const Standard_Boolean aFrontZClippingModeOld = MyContext.FrontZClippingIsOn();
650   const Standard_Boolean aFrontZClippingModeNew = theViewCtx.FrontZClippingIsOn();
651   const Standard_Boolean aBackZClippingModeOld  = MyContext.BackZClippingIsOn();
652   const Standard_Boolean aBackZClippingModeNew  = theViewCtx.BackZClippingIsOn();
653
654   const Standard_Real aZClippingFrontPlaneOld = MyContext.ZClippingFrontPlane();
655   const Standard_Real aZClippingFrontPlaneNew = theViewCtx.ZClippingFrontPlane();
656   const Standard_Real aZClippingBackPlaneOld  = MyContext.ZClippingBackPlane();
657   const Standard_Real aZClippingBackPlaneNew  = theViewCtx.ZClippingBackPlane();
658
659   const bool isTextEnvChanged    = theViewCtx.TextureEnv()    != MyContext.TextureEnv();
660   const bool isSurfDetailChanged = theViewCtx.SurfaceDetail() != MyContext.SurfaceDetail();
661
662   MyContext = theViewCtx;
663
664   UpdateView();
665
666   Standard_Boolean toWait = Standard_False; // => immediate update
667   if (IsDefined())
668   {
669     // management of visualization modes and types of shading.
670     if (isVisModeChanged
671      || isModelChanged)
672     {
673       myGraphicDriver->SetVisualisation (MyCView);
674     }
675
676     // management of antialiasing
677     if (anAliasingModeOld != anAliasingModeNew)
678     {
679       myGraphicDriver->AntiAliasing (MyCView, anAliasingModeNew);
680     }
681
682     // management of depth_cueing
683     if (aDepthCueingModeOld       != aDepthCueingModeNew
684      || aDepthCueingFrontPlaneOld != aDepthCueingFrontPlaneNew
685      || aDepthCueingBackPlaneOld  != aDepthCueingBackPlaneNew)
686     {
687       if (aDepthCueingModeNew
688       &&  aDepthCueingBackPlaneNew >= aDepthCueingFrontPlaneNew)
689       {
690         Visual3d_DepthCueingDefinitionError::Raise ("Bad value for DepthCueingPlanes position");
691       }
692       myGraphicDriver->DepthCueing (MyCView, aDepthCueingModeNew);
693     }
694
695     // management of Zclipping
696     if (aFrontZClippingModeOld  != aFrontZClippingModeNew
697      || aBackZClippingModeOld   != aBackZClippingModeNew
698      || aZClippingFrontPlaneOld != aZClippingFrontPlaneNew
699      || aZClippingBackPlaneOld  != aZClippingBackPlaneNew)
700     {
701       if (aBackZClippingModeNew
702        && aFrontZClippingModeNew
703        && aZClippingBackPlaneNew >= aZClippingFrontPlaneNew)
704       {
705         Visual3d_ZClippingDefinitionError::Raise ("Bad value for ZClippingPlanes position");
706       }
707
708       myGraphicDriver->ClipLimit (MyCView, toWait);
709     }
710
711     // management of textures
712     if (isTextEnvChanged
713      || isSurfDetailChanged)
714     {
715       myGraphicDriver->Environment (MyCView);
716     }
717
718     UpdatePlanes(); // Update of planes of model clipping
719     UpdateLights(); // Update of light sources
720   }
721
722   if (isVisModeChanged)
723   {
724     // Change of context =>
725     // Remove structures that cannot be displayed in the new visualisation mode.
726     // It is not necessary to warn ViewManager as this structure should not disappear from
727     // the list of structures displayed in it.
728     NCollection_Sequence<Handle(Graphic3d_Structure)> aStructs;
729     for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (myStructsDisplayed); aStructIter.More(); aStructIter.Next())
730     {
731       const Handle(Graphic3d_Structure)& aStruct  = aStructIter.Key();
732       const Visual3d_TypeOfAnswer        anAnswer = AcceptDisplay (aStruct);
733       if (anAnswer == Visual3d_TOA_NO
734        || anAnswer == Visual3d_TOA_COMPUTE)
735       {
736         aStructs.Append (aStruct);
737       }
738     }
739     for (NCollection_Sequence<Handle(Graphic3d_Structure)>::Iterator aStructIter (aStructs); aStructIter.More(); aStructIter.Next())
740     {
741       Erase (aStructIter.ChangeValue(), Aspect_TOU_WAIT);
742     }
743     aStructs.Clear();
744
745     // Change of context =>
746     // Display structures that can be displayed with the new visualisation mode.
747     // All structures with status Displayed are removed from the ViewManager
748     // and displayed in the view directly, if the structure is not already
749     // displayed and if the view accepts it in its context.
750     Graphic3d_MapOfStructure aMapDisplayed;
751     myViewManager->DisplayedStructures (aMapDisplayed);
752     for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aMapDisplayed); aStructIter.More(); aStructIter.Next())
753     {
754       const Handle(Graphic3d_Structure)& aStruct = aStructIter.Key();
755       if (IsDisplayed (aStruct))
756       {
757         continue;
758       }
759
760       const Visual3d_TypeOfAnswer anAnswer = AcceptDisplay (aStruct);
761       if (anAnswer == Visual3d_TOA_YES
762        || anAnswer == Visual3d_TOA_COMPUTE)
763       {
764         aStructs.Append (aStruct);
765       }
766     }
767
768     for (NCollection_Sequence<Handle(Graphic3d_Structure)>::Iterator aStructIter (aStructs); aStructIter.More(); aStructIter.Next())
769     {
770       Display (aStructIter.ChangeValue(), Aspect_TOU_WAIT);
771     }
772   }
773
774   Update (myViewManager->UpdateMode());
775 }
776
777 // =======================================================================
778 // function : Context
779 // purpose  :
780 // =======================================================================
781 const Visual3d_ContextView& Visual3d_View::Context() const
782 {
783   return MyContext;
784 }
785
786 // =======================================================================
787 // function : DisplayedStructures
788 // purpose  :
789 // =======================================================================
790 void Visual3d_View::DisplayedStructures (Graphic3d_MapOfStructure& theStructures) const
791 {
792   if (IsDeleted())
793   {
794     return;
795   }
796
797   for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (myStructsDisplayed); aStructIter.More(); aStructIter.Next())
798   {
799     theStructures.Add (aStructIter.Key());
800   }
801 }
802
803 // =======================================================================
804 // function : Activate
805 // purpose  :
806 // =======================================================================
807 void Visual3d_View::Activate()
808 {
809   if (IsDeleted())
810   {
811     return;
812   }
813   else if (!IsDefined())
814   {
815     Visual3d_ViewDefinitionError::Raise ("Window not defined");
816   }
817
818   if (!IsActive())
819   {
820     myGraphicDriver->ActivateView (MyCView);
821     myGraphicDriver->Background   (MyCView);
822     myGraphicDriver->Transparency (MyCView, myViewManager->Transparency());
823
824     MyCView.Active = 1;
825
826     // Activation of a new view =>
827     // Display structures that can be displayed in this new view.
828     // All structures with status
829     // Displayed in ViewManager are returned and displayed in
830     // the view directly, if the structure is not already
831     // displayed and if the view accepts it in its context.
832     Graphic3d_MapOfStructure aDisplayedStructs;
833     myViewManager->DisplayedStructures (aDisplayedStructs);
834     for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aDisplayedStructs); aStructIter.More(); aStructIter.Next())
835     {
836       const Handle(Graphic3d_Structure)& aStruct = aStructIter.Key();
837       if (IsDisplayed (aStruct))
838       {
839         continue;
840       }
841
842       // If the structure can be displayed in the new context of the view, it is displayed.
843       const Visual3d_TypeOfAnswer anAnswer = AcceptDisplay (aStruct);
844       if (anAnswer == Visual3d_TOA_YES
845        || anAnswer == Visual3d_TOA_COMPUTE)
846       {
847         Display (aStruct, Aspect_TOU_WAIT);
848       }
849     }
850   }
851
852   // If the activation/desactivation of ZBuffer should be automatic
853   // depending on the presence or absence of facets.
854   if (myViewManager->ZBufferAuto())
855   {
856     const Standard_Boolean containsFacet = ContainsFacet();
857     const Standard_Boolean hasZBuffer    = ZBufferIsActivated();
858     if (containsFacet && !hasZBuffer)
859     {
860       SetZBufferActivity (1); // If the view contains facets and if ZBuffer is not active
861     }
862     else if (!containsFacet && hasZBuffer)
863     {
864       SetZBufferActivity (0); // If the view does not contain facets and if ZBuffer is active
865     }
866   }
867
868   Update (myViewManager->UpdateMode());
869 }
870
871 // =======================================================================
872 // function : IsActive
873 // purpose  :
874 // =======================================================================
875 Standard_Boolean Visual3d_View::IsActive() const
876 {
877   return !IsDeleted()
878       &&  MyCView.Active;
879 }
880
881 // =======================================================================
882 // function : Deactivate
883 // purpose  :
884 // =======================================================================
885 void Visual3d_View::Deactivate()
886 {
887   if (IsDeleted())
888   {
889     return;
890   }
891   else if (!IsDefined())
892   {
893     Visual3d_ViewDefinitionError::Raise ("Window not defined");
894   }
895
896   if (IsActive())
897   {
898     myGraphicDriver->DeactivateView (MyCView);
899
900     // Deactivation of a view =>
901     // Removal of structures displayed in this view.
902     // All structures with status
903     // Displayed in ViewManager are returned and removed from
904     // the view directly, if the structure is not already
905     // displayed and if the view accepts it in its context.
906     Graphic3d_MapOfStructure aDisplayedStructs;
907     myViewManager->DisplayedStructures (aDisplayedStructs);
908     for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (aDisplayedStructs); aStructIter.More(); aStructIter.Next())
909     {
910       const Handle(Graphic3d_Structure)& aStruct = aStructIter.Key();
911       if (IsDisplayed (aStruct))
912       {
913         continue;
914       }
915
916       const Visual3d_TypeOfAnswer anAnswer = AcceptDisplay (aStruct);
917       if (anAnswer == Visual3d_TOA_YES
918        || anAnswer == Visual3d_TOA_COMPUTE)
919       {
920         Erase (aStruct, Aspect_TOU_WAIT);
921       }
922     }
923
924     Update (myViewManager->UpdateMode());
925     MyCView.Active = 0; // No action currently possible in the view
926   }
927 }
928
929 // =======================================================================
930 // function : Redraw
931 // purpose  :
932 // =======================================================================
933 void Visual3d_View::Redraw()
934 {
935   Redraw (myViewManager->UnderLayer(), myViewManager->OverLayer(), 0, 0, 0, 0);
936 }
937
938 // =======================================================================
939 // function : Redraw
940 // purpose  :
941 // =======================================================================
942 void Visual3d_View::Redraw (const Standard_Integer theX,
943                             const Standard_Integer theY,
944                             const Standard_Integer theWidth,
945                             const Standard_Integer theHeight)
946 {
947   Redraw (myViewManager->UnderLayer(), myViewManager->OverLayer(),
948           theX, theY, theWidth, theHeight);
949 }
950
951 // =======================================================================
952 // function : Redraw
953 // purpose  :
954 // =======================================================================
955 void Visual3d_View::Redraw (const Handle(Visual3d_Layer)& theUnderLayer,
956                             const Handle(Visual3d_Layer)& theOverLayer)
957 {
958   Redraw (theUnderLayer, theOverLayer, 0, 0, 0, 0);
959 }
960
961 // =======================================================================
962 // function : Redraw
963 // purpose  :
964 // =======================================================================
965 void Visual3d_View::Redraw (const Handle(Visual3d_Layer)& theUnderLayer,
966                             const Handle(Visual3d_Layer)& theOverLayer,
967                             const Standard_Integer        theX,
968                             const Standard_Integer        theY,
969                             const Standard_Integer        theWidth,
970                             const Standard_Integer        theHeight)
971 {
972   if (IsDeleted()
973    || !IsDefined()
974    || !IsActive()
975    || !MyWindow->IsMapped())
976   {
977     return;
978   }
979
980   Aspect_CLayer2d anOverCLayer, anUnderCLayer;
981   anOverCLayer.ptrLayer = anUnderCLayer.ptrLayer = NULL;
982   if (!theOverLayer .IsNull()) anOverCLayer  = theOverLayer ->CLayer();
983   if (!theUnderLayer.IsNull()) anUnderCLayer = theUnderLayer->CLayer();
984
985   for (Standard_Integer aRetryIter = 0; aRetryIter < 2; ++aRetryIter)
986   {
987     if (myGraphicDriver->IsDeviceLost())
988     {
989       myViewManager->RecomputeStructures();
990       myViewManager->RecomputeStructures (myImmediateStructures);
991       myGraphicDriver->ResetDeviceLostFlag();
992     }
993
994     // set up Z buffer state before redrawing
995     if (myViewManager->ZBufferAuto())
996     {
997       const Standard_Boolean hasFacet   = ContainsFacet();
998       const Standard_Boolean hasZBuffer = ZBufferIsActivated();
999       // if the view contains facets and if ZBuffer is not active
1000       if (hasFacet && !hasZBuffer)
1001       {
1002         SetZBufferActivity (1);
1003       }
1004       // if the view contains only facets and if ZBuffer is active
1005       if (!hasFacet && hasZBuffer)
1006       {
1007         SetZBufferActivity (0);
1008       }
1009     }
1010
1011     if (myStructuresUpdated)
1012     {
1013       AutoZFit();
1014       myStructuresUpdated = Standard_False;
1015     }
1016
1017     myGraphicDriver->Redraw (MyCView, anUnderCLayer, anOverCLayer, theX, theY, theWidth, theHeight);
1018     if (!myGraphicDriver->IsDeviceLost())
1019     {
1020       return;
1021     }
1022   }
1023 }
1024
1025 // =======================================================================
1026 // function : RedrawImmediate
1027 // purpose  :
1028 // =======================================================================
1029 void Visual3d_View::RedrawImmediate()
1030 {
1031   RedrawImmediate (myViewManager->UnderLayer(), myViewManager->OverLayer());
1032 }
1033
1034 // =======================================================================
1035 // function : RedrawImmediate
1036 // purpose  :
1037 // =======================================================================
1038 void Visual3d_View::RedrawImmediate (const Handle(Visual3d_Layer)& theUnderLayer,
1039                                      const Handle(Visual3d_Layer)& theOverLayer)
1040 {
1041   if (IsDeleted()
1042    || !IsDefined()
1043    || !IsActive()
1044    || !MyWindow->IsMapped())
1045   {
1046     return;
1047   }
1048
1049   Aspect_CLayer2d anOverCLayer, anUnderCLayer;
1050   anOverCLayer.ptrLayer = anUnderCLayer.ptrLayer = NULL;
1051   if (!theOverLayer .IsNull()) anOverCLayer  = theOverLayer ->CLayer();
1052   if (!theUnderLayer.IsNull()) anUnderCLayer = theUnderLayer->CLayer();
1053   myGraphicDriver->RedrawImmediate (MyCView, anUnderCLayer, anOverCLayer);
1054 }
1055
1056 // =======================================================================
1057 // function : Invalidate
1058 // purpose  :
1059 // =======================================================================
1060 void Visual3d_View::Invalidate()
1061 {
1062   myGraphicDriver->Invalidate (MyCView);
1063 }
1064
1065 // =======================================================================
1066 // function : Update
1067 // purpose  :
1068 // =======================================================================
1069 void Visual3d_View::Update (Aspect_TypeOfUpdate theUpdateMode)
1070 {
1071   myStructuresUpdated = Standard_True;
1072   if (theUpdateMode == Aspect_TOU_ASAP)
1073   {
1074     Compute();
1075     Redraw (myViewManager->UnderLayer(), myViewManager->OverLayer(), 0, 0, 0, 0);
1076   }
1077 }
1078
1079 // =======================================================================
1080 // function : Update
1081 // purpose  :
1082 // =======================================================================
1083 void Visual3d_View::Update (const Handle(Visual3d_Layer)& theUnderLayer,
1084                             const Handle(Visual3d_Layer)& theOverLayer)
1085 {
1086   Compute();
1087   myStructuresUpdated = Standard_True;
1088   Redraw (theUnderLayer, theOverLayer, 0, 0, 0, 0);
1089 }
1090
1091 // ========================================================================
1092 // function : SetAutoZFitMode
1093 // purpose  :
1094 // ========================================================================
1095 void Visual3d_View::SetAutoZFitMode (const Standard_Boolean theIsOn,
1096                                      const Standard_Real    theScaleFactor)
1097 {
1098   Standard_ASSERT_RAISE (theScaleFactor > 0.0, "Zero or negative scale factor is not allowed.");
1099   myAutoZFitScaleFactor = theScaleFactor;
1100   myAutoZFitIsOn = theIsOn;
1101 }
1102
1103 // ========================================================================
1104 // function : AutoZFitMode
1105 // purpose  :
1106 // ========================================================================
1107 Standard_Boolean Visual3d_View::AutoZFitMode() const
1108 {
1109   return myAutoZFitIsOn;
1110 }
1111
1112 // ========================================================================
1113 // function : AutoZFitScaleFactor
1114 // purpose  :
1115 // ========================================================================
1116 Standard_Real Visual3d_View::AutoZFitScaleFactor() const
1117 {
1118   return myAutoZFitScaleFactor;
1119 }
1120
1121 // ========================================================================
1122 // function : AutoZFit
1123 // purpose  :
1124 // ========================================================================
1125 void Visual3d_View::AutoZFit()
1126 {
1127   if (!AutoZFitMode())
1128   {
1129     return;
1130   }
1131
1132   ZFitAll (myAutoZFitScaleFactor);
1133 }
1134
1135 // ========================================================================
1136 // function : ZFitAll
1137 // purpose  :
1138 // ========================================================================
1139 void Visual3d_View::ZFitAll (const Standard_Real theScaleFactor)
1140 {
1141   Standard_Real aMinMax[6];    // applicative min max boundaries
1142   MinMaxValues (aMinMax[0], aMinMax[1], aMinMax[2],
1143     aMinMax[3], aMinMax[4], aMinMax[5],
1144     Standard_False);
1145
1146   Standard_Real aGraphicBB[6]; // real graphical boundaries (not accounting infinite flag).
1147   MinMaxValues (aGraphicBB[0], aGraphicBB[1], aGraphicBB[2],
1148     aGraphicBB[3], aGraphicBB[4], aGraphicBB[5],
1149     Standard_True);
1150
1151   Bnd_Box aMinMaxBox;
1152   Bnd_Box aGraphicBox;
1153
1154   aMinMaxBox.Update (aMinMax[0], aMinMax[1], aMinMax[2],
1155     aMinMax[3], aMinMax[4], aMinMax[5]);
1156
1157   aGraphicBox.Update (aGraphicBB[0], aGraphicBB[1], aGraphicBB[2],
1158     aGraphicBB[3], aGraphicBB[4], aGraphicBB[5]);
1159
1160   const Handle(Graphic3d_Camera)& aCamera = MyCView.Context.Camera;
1161   aCamera->ZFitAll (theScaleFactor, aMinMaxBox, aGraphicBox);
1162 }
1163
1164 // ========================================================================
1165 // function : AcceptDisplay
1166 // purpose  :
1167 // ========================================================================
1168 Visual3d_TypeOfAnswer Visual3d_View::AcceptDisplay (const Handle(Graphic3d_Structure)& theStruct) const
1169 {
1170   const Visual3d_TypeOfVisualization ViewType = MyContext.Visualization();
1171   switch (theStruct->Visual())
1172   {
1173     case Graphic3d_TOS_ALL:
1174     {
1175       return Visual3d_TOA_YES; // The structure accepts any type of view
1176     }
1177     case Graphic3d_TOS_SHADING:
1178     {
1179       return ViewType == Visual3d_TOV_SHADING
1180            ? Visual3d_TOA_YES
1181            : Visual3d_TOA_NO;
1182     }
1183     case Graphic3d_TOS_WIREFRAME:
1184     {
1185       return ViewType == Visual3d_TOV_WIREFRAME
1186            ? Visual3d_TOA_YES
1187            : Visual3d_TOA_NO;
1188     }
1189     case Graphic3d_TOS_COMPUTED:
1190     {
1191       return (ViewType == Visual3d_TOV_SHADING || ViewType == Visual3d_TOV_WIREFRAME)
1192            ?  Visual3d_TOA_COMPUTE
1193            :  Visual3d_TOA_NO;
1194     }
1195   }
1196   return Visual3d_TOA_NO;
1197 }
1198
1199 // ========================================================================
1200 // function : ChangeDisplayPriority
1201 // purpose  :
1202 // ========================================================================
1203 void Visual3d_View::ChangeDisplayPriority (const Handle(Graphic3d_Structure)& theStruct,
1204                                            const Standard_Integer           /*theOldPriority*/,
1205                                            const Standard_Integer             theNewPriority)
1206 {
1207   if (IsDeleted()
1208   || !IsDefined()
1209   || !IsActive()
1210   || !IsDisplayed (theStruct))
1211   {
1212     return;
1213   }
1214
1215   if (!myIsInComputedMode)
1216   {
1217     myGraphicDriver->ChangePriority (*theStruct->CStructure(), MyCView, theNewPriority);
1218     return;
1219   }
1220
1221   const Standard_Integer      anIndex  = IsComputed (theStruct);
1222   const Graphic3d_CStructure& aCStruct = anIndex != 0
1223                                        ? *(myStructsComputed.Value (anIndex)->CStructure())
1224                                        : *theStruct->CStructure();
1225   myGraphicDriver->ChangePriority (aCStruct, MyCView, theNewPriority);
1226 }
1227
1228 // ========================================================================
1229 // function : Clear
1230 // purpose  :
1231 // ========================================================================
1232 void Visual3d_View::Clear (const Handle(Graphic3d_Structure)& theStruct,
1233                            const Standard_Boolean             theWithDestruction)
1234 {
1235   const Standard_Integer anIndex = IsComputed (theStruct);
1236   if (anIndex != 0)
1237   {
1238     const Handle(Graphic3d_Structure)& aCompStruct = myStructsComputed.Value (anIndex);
1239     aCompStruct->GraphicClear (theWithDestruction);
1240     aCompStruct->SetHLRValidation (Standard_False);
1241   }
1242 }
1243
1244 // ========================================================================
1245 // function : Connect
1246 // purpose  :
1247 // ========================================================================
1248 void Visual3d_View::Connect (const Handle(Graphic3d_Structure)& theMother,
1249                              const Handle(Graphic3d_Structure)& theDaughter)
1250 {
1251   Standard_Integer anIndexM = IsComputed (theMother);
1252   Standard_Integer anIndexD = IsComputed (theDaughter);
1253   if (anIndexM != 0
1254    && anIndexD != 0)
1255   {
1256     const Handle(Graphic3d_Structure)& aStructM = myStructsComputed.Value (anIndexM);
1257     const Handle(Graphic3d_Structure)& aStructD = myStructsComputed.Value (anIndexD);
1258     aStructM->GraphicConnect (aStructD);
1259   }
1260 }
1261
1262 // ========================================================================
1263 // function : Disconnect
1264 // purpose  :
1265 // ========================================================================
1266 void Visual3d_View::Disconnect (const Handle(Graphic3d_Structure)& theMother,
1267                                 const Handle(Graphic3d_Structure)& theDaughter)
1268 {
1269   Standard_Integer anIndexM = IsComputed (theMother);
1270   Standard_Integer anIndexD = IsComputed (theDaughter);
1271   if (anIndexM != 0
1272    && anIndexD != 0)
1273   {
1274     const Handle(Graphic3d_Structure)& aStructM = myStructsComputed.Value (anIndexM);
1275     const Handle(Graphic3d_Structure)& aStructD = myStructsComputed.Value (anIndexD);
1276     aStructM->GraphicDisconnect (aStructD);
1277   }
1278 }
1279
1280 // ========================================================================
1281 // function : DisplayImmediate
1282 // purpose  :
1283 // ========================================================================
1284 Standard_Boolean Visual3d_View::DisplayImmediate (const Handle(Graphic3d_Structure)& theStructure,
1285                                                   const Standard_Boolean             theIsSingleView)
1286 {
1287   if (!myImmediateStructures.Add (theStructure))
1288   {
1289     return Standard_False;
1290   }
1291
1292   if (theIsSingleView)
1293   {
1294     Handle(Visual3d_HSequenceOfView) aViews = myViewManager->DefinedView();
1295
1296     for (int i=1;i<=aViews->Length();i++)
1297     {
1298       if (aViews->Value(i).Access() != this)
1299       {
1300         aViews->Value(i)->EraseImmediate (theStructure);
1301       }
1302     }
1303   }
1304
1305   myGraphicDriver->DisplayImmediateStructure (MyCView, *theStructure->CStructure());
1306   return Standard_True;
1307 }
1308
1309 // ========================================================================
1310 // function : EraseImmediate
1311 // purpose  :
1312 // ========================================================================
1313 Standard_Boolean Visual3d_View::EraseImmediate (const Handle(Graphic3d_Structure)& theStructure)
1314 {
1315   const Standard_Boolean isErased = myImmediateStructures.Remove (theStructure);
1316   if (isErased)
1317   {
1318     myGraphicDriver->EraseImmediateStructure (MyCView, *theStructure->CStructure());
1319   }
1320
1321   return isErased;
1322 }
1323
1324 // ========================================================================
1325 // function : ClearImmediate
1326 // purpose  :
1327 // ========================================================================
1328 Standard_Boolean Visual3d_View::ClearImmediate()
1329 {
1330   if (myImmediateStructures.IsEmpty())
1331   {
1332     return Standard_False;
1333   }
1334
1335   for (Graphic3d_MapIteratorOfMapOfStructure anIter (myImmediateStructures); anIter.More(); anIter.Next())
1336   {
1337     myGraphicDriver->EraseImmediateStructure (MyCView, *anIter.Key()->CStructure());
1338   }
1339   myImmediateStructures.Clear();
1340   return Standard_True;
1341 }
1342
1343 // ========================================================================
1344 // function : Display
1345 // purpose  :
1346 // ========================================================================
1347 void Visual3d_View::Display (const Handle(Graphic3d_Structure)& theStruct)
1348 {
1349   Display (theStruct, myViewManager->UpdateMode());
1350 }
1351
1352 // ========================================================================
1353 // function : Display
1354 // purpose  :
1355 // ========================================================================
1356 void Visual3d_View::Display (const Handle(Graphic3d_Structure)& theStruct,
1357                              const Aspect_TypeOfUpdate          theUpdateMode)
1358 {
1359   if (IsDeleted()
1360   || !IsDefined()
1361   || !IsActive())
1362   {
1363     return;
1364   }
1365
1366   // If Display on a structure present in the list of calculated structures while it is not
1367   // or more, of calculated type =>
1368   // - removes it as well as the associated old computed
1369   // THis happens when hlhsr becomes again of type e non computed after SetVisual.
1370   Standard_Integer anIndex = IsComputed (theStruct);
1371   if (anIndex != 0
1372    && theStruct->Visual() != Graphic3d_TOS_COMPUTED)
1373   {
1374     myStructsToCompute.Remove (anIndex);
1375     myStructsComputed .Remove (anIndex);
1376     anIndex = 0;
1377   }
1378
1379   Visual3d_TypeOfAnswer anAnswer = AcceptDisplay (theStruct);
1380   if (anAnswer == Visual3d_TOA_NO)
1381   {
1382     return;
1383   }
1384
1385   if (!ComputedMode())
1386   {
1387     anAnswer = Visual3d_TOA_YES;
1388   }
1389
1390   if (anAnswer == Visual3d_TOA_YES)
1391   {
1392     if (IsDisplayed (theStruct))
1393     {
1394       return;
1395     }
1396
1397     theStruct->CalculateBoundBox();
1398     myGraphicDriver->DisplayStructure (MyCView, *theStruct->CStructure(), theStruct->DisplayPriority());
1399     myStructsDisplayed.Add (theStruct);
1400     Update (theUpdateMode);
1401     return;
1402   }
1403   else if (anAnswer != Visual3d_TOA_COMPUTE)
1404   {
1405     return;
1406   }
1407
1408   if (anIndex != 0)
1409   {
1410     // Already computed, is COMPUTED still valid?
1411     const Handle(Graphic3d_Structure)& anOldStruct = myStructsComputed.Value (anIndex);
1412     if (anOldStruct->HLRValidation())
1413     {
1414       // Case COMPUTED valid
1415       // to be displayed
1416       if (!IsDisplayed (theStruct))
1417       {
1418         myStructsDisplayed.Add (theStruct);
1419         myGraphicDriver->DisplayStructure (MyCView, *anOldStruct->CStructure(), theStruct->DisplayPriority());
1420         Update (theUpdateMode);
1421       }
1422       return;
1423     }
1424     else
1425     {
1426       // Case COMPUTED invalid
1427       // Is there another valid representation?
1428       // Find in the sequence of already calculated structures
1429       // 1/ Structure having the same Owner as <AStructure>
1430       // 2/ That is not <AStructure>
1431       // 3/ The COMPUTED which of is valid
1432       const Standard_Integer aNewIndex = HaveTheSameOwner (theStruct);
1433       if (aNewIndex != 0)
1434       {
1435         // Case of COMPUTED invalid, WITH a valid of replacement
1436         // to be displayed
1437         if (!IsDisplayed (theStruct))
1438         {
1439           const Handle(Graphic3d_Structure)& aNewStruct = myStructsComputed.Value (aNewIndex);
1440           myStructsComputed.SetValue (anIndex, aNewStruct);
1441           myStructsDisplayed.Add (theStruct);
1442           myGraphicDriver->DisplayStructure (MyCView, *aNewStruct->CStructure(), theStruct->DisplayPriority());
1443           Update (theUpdateMode);
1444         }
1445         return;
1446       }
1447       else
1448       {
1449         // Case COMPUTED invalid, WITHOUT a valid of replacement
1450         // COMPUTED is removed if displayed
1451         if (IsDisplayed (theStruct))
1452         {
1453           myGraphicDriver->EraseStructure (MyCView, *anOldStruct->CStructure());
1454         }
1455       }
1456     }
1457   }
1458
1459   // Compute + Validation
1460   Handle(Graphic3d_Structure) aStruct;
1461   TColStd_Array2OfReal aTrsf (0, 3, 0, 3);
1462   theStruct->Transform (aTrsf);
1463   if (anIndex != 0)
1464   {
1465     TColStd_Array2OfReal anIdent (0, 3, 0, 3);
1466     for (Standard_Integer ii = 0; ii <= 3; ++ii)
1467     {
1468       for (Standard_Integer jj = 0; jj <= 3; ++jj)
1469       {
1470         anIdent (ii, jj) = (ii == jj ? 1.0 : 0.0);
1471       }
1472     }
1473
1474     aStruct = myStructsComputed.Value (anIndex);
1475     aStruct->SetTransform (anIdent, Graphic3d_TOC_REPLACE);
1476     if (theStruct->IsTransformed())
1477     {
1478       theStruct->Compute (this, aTrsf, aStruct);
1479     }
1480     else
1481     {
1482       theStruct->Compute (this, aStruct);
1483     }
1484   }
1485   else
1486   {
1487     aStruct = theStruct->IsTransformed()
1488             ? theStruct->Compute (this, aTrsf)
1489             : theStruct->Compute (this);
1490   }
1491
1492   aStruct->SetHLRValidation (Standard_True);
1493
1494   // TOCOMPUTE and COMPUTED associated to sequences are added
1495   myStructsToCompute.Append (theStruct);
1496   myStructsComputed .Append (aStruct);
1497
1498   // The previous are removed if necessary
1499   if (anIndex != 0)
1500   {
1501     myStructsToCompute.Remove (anIndex);
1502     myStructsComputed .Remove (anIndex);
1503   }
1504
1505   // Of which type will be the computed?
1506   const Visual3d_TypeOfVisualization aViewType = MyContext.Visualization();
1507   const Standard_Boolean toComputeWireframe = aViewType == Visual3d_TOV_WIREFRAME
1508                                            && theStruct->ComputeVisual() != Graphic3d_TOS_SHADING;
1509   const Standard_Boolean toComputeShading   = aViewType == Visual3d_TOV_SHADING
1510                                            && theStruct->ComputeVisual() != Graphic3d_TOS_WIREFRAME;
1511   if (!toComputeShading && !toComputeWireframe)
1512   {
1513     anAnswer = Visual3d_TOA_NO;
1514   }
1515   else
1516   {
1517     aStruct->SetVisual (toComputeWireframe ? Graphic3d_TOS_WIREFRAME : Graphic3d_TOS_SHADING);
1518     anAnswer = AcceptDisplay (aStruct);
1519   }
1520
1521   if (theStruct->IsHighlighted())
1522   {
1523     aStruct->SetHighlightColor (theStruct->HighlightColor());
1524     aStruct->GraphicHighlight (Aspect_TOHM_COLOR);
1525   }
1526
1527   // It is displayed only if the calculated structure
1528   // has a proper type corresponding to the one of the view.
1529   if (anAnswer != Visual3d_TOA_NO)
1530   {
1531     if (!IsDisplayed (theStruct))
1532     {
1533       myStructsDisplayed.Add (theStruct);
1534     }
1535     myGraphicDriver->DisplayStructure (MyCView, *aStruct->CStructure(), theStruct->DisplayPriority());
1536     Update (theUpdateMode);
1537   }
1538 }
1539
1540 // ========================================================================
1541 // function : Erase
1542 // purpose  :
1543 // ========================================================================
1544 void Visual3d_View::Erase (const Handle(Graphic3d_Structure)& theStruct)
1545 {
1546   if (!IsDeleted())
1547   {
1548     Erase (theStruct, myViewManager->UpdateMode());
1549   }
1550 }
1551
1552 // ========================================================================
1553 // function : Erase
1554 // purpose  :
1555 // ========================================================================
1556 void Visual3d_View::Erase (const Handle(Graphic3d_Structure)& theStruct,
1557                            const Aspect_TypeOfUpdate          theUpdateMode)
1558 {
1559   if (IsDeleted()
1560   || !IsDisplayed (theStruct))
1561   {
1562     return;
1563   }
1564
1565   Visual3d_TypeOfAnswer anAnswer = AcceptDisplay (theStruct);
1566   if (!ComputedMode())
1567   {
1568     anAnswer = Visual3d_TOA_YES;
1569   }
1570
1571   if (anAnswer != Visual3d_TOA_COMPUTE)
1572   {
1573     myGraphicDriver->EraseStructure (MyCView, *theStruct->CStructure());
1574   }
1575   else if (anAnswer == Visual3d_TOA_COMPUTE
1576        && myIsInComputedMode)
1577   {
1578     const Standard_Integer anIndex = IsComputed (theStruct);
1579     if (anIndex != 0)
1580     {
1581       const Handle(Graphic3d_Structure)& aCompStruct = myStructsComputed.ChangeValue (anIndex);
1582       myGraphicDriver->EraseStructure (MyCView, *aCompStruct->CStructure());
1583     }
1584   }
1585   myStructsDisplayed.Remove (theStruct);
1586   Update (theUpdateMode);
1587 }
1588
1589 // ========================================================================
1590 // function : Highlight
1591 // purpose  :
1592 // ========================================================================
1593 void Visual3d_View::Highlight (const Handle(Graphic3d_Structure)& theStruct,
1594                                const Aspect_TypeOfHighlightMethod theMethod)
1595 {
1596   const Standard_Integer anIndex = IsComputed (theStruct);
1597   if (anIndex != 0)
1598   {
1599     const Handle(Graphic3d_Structure)& aCompStruct = myStructsComputed.ChangeValue (anIndex);
1600     aCompStruct->SetHighlightColor (theStruct->HighlightColor());
1601     aCompStruct->GraphicHighlight (theMethod);
1602   }
1603 }
1604
1605 // ========================================================================
1606 // function : SetTransform
1607 // purpose  :
1608 // ========================================================================
1609 void Visual3d_View::SetTransform (const Handle(Graphic3d_Structure)& theStruct,
1610                                   const TColStd_Array2OfReal&        theTrsf)
1611 {
1612   const Standard_Integer anIndex = IsComputed (theStruct);
1613   if (anIndex != 0)
1614   {
1615     // Test is somewhat light !
1616     // trsf is transferred only if it is :
1617     // a translation
1618     // a scale
1619     if (theTrsf (0, 1) != 0.0 || theTrsf (0, 2) != 0.0
1620      || theTrsf (1, 0) != 0.0 || theTrsf (1, 2) != 0.0
1621      || theTrsf (2, 0) != 0.0 || theTrsf (2, 1) != 0.0)
1622     {
1623       ReCompute (theStruct);
1624     }
1625     else
1626     {
1627       const Handle(Graphic3d_Structure)& aCompStruct = myStructsComputed.ChangeValue (anIndex);
1628       aCompStruct->GraphicTransform (theTrsf);
1629     }
1630   }
1631
1632   theStruct->CalculateBoundBox();
1633   if (!theStruct->IsMutable()
1634    && !theStruct->CStructure()->IsForHighlight
1635    && !theStruct->CStructure()->IsInfinite)
1636   {
1637     const Standard_Integer aLayerId = theStruct->GetZLayer();
1638     myGraphicDriver->InvalidateBVHData (MyCView, aLayerId);
1639   }
1640 }
1641
1642 // ========================================================================
1643 // function : UnHighlight
1644 // purpose  :
1645 // ========================================================================
1646 void Visual3d_View::UnHighlight (const Handle(Graphic3d_Structure)& theStruct)
1647 {
1648   Standard_Integer anIndex = IsComputed (theStruct);
1649   if (anIndex != 0)
1650   {
1651     const Handle(Graphic3d_Structure)& aCompStruct = myStructsComputed.ChangeValue (anIndex);
1652     aCompStruct->GraphicUnHighlight();
1653   }
1654 }
1655
1656 // ========================================================================
1657 // function : IsComputed
1658 // purpose  :
1659 // ========================================================================
1660 Standard_Integer Visual3d_View::IsComputed (const Handle(Graphic3d_Structure)& theStruct) const
1661 {
1662   const Standard_Integer aStructId  = theStruct->Identification();
1663   const Standard_Integer aNbStructs = myStructsToCompute.Length();
1664   for (Standard_Integer aStructIter = 1; aStructIter <= aNbStructs; ++aStructIter)
1665   {
1666     const Handle(Graphic3d_Structure)& aStruct = myStructsToCompute.Value (aStructIter);
1667     if (aStruct->Identification() == aStructId)
1668     {
1669       return aStructIter;
1670     }
1671   }
1672   return 0;
1673 }
1674
1675 // ========================================================================
1676 // function : IsDisplayed
1677 // purpose  :
1678 // ========================================================================
1679 Standard_Boolean Visual3d_View::IsDisplayed (const Handle(Graphic3d_Structure)& theStruct) const
1680 {
1681   return !IsDeleted()
1682       &&  myStructsDisplayed.Contains (theStruct);
1683 }
1684
1685 // ========================================================================
1686 // function : ContainsFacet
1687 // purpose  :
1688 // ========================================================================
1689 Standard_Boolean Visual3d_View::ContainsFacet() const
1690 {
1691   return ContainsFacet (myStructsDisplayed);
1692 }
1693
1694 // ========================================================================
1695 // function : ContainsFacet
1696 // purpose  :
1697 // ========================================================================
1698 Standard_Boolean Visual3d_View::ContainsFacet (const Graphic3d_MapOfStructure& theSet) const
1699 {
1700   for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (theSet); aStructIter.More(); aStructIter.Next())
1701   {
1702     if (aStructIter.Key()->ContainsFacet())
1703     {
1704       return Standard_True;
1705     }
1706   }
1707   return Standard_False;
1708 }
1709
1710 // ========================================================================
1711 // function : MinMaxValues
1712 // purpose  :
1713 // ========================================================================
1714 void Visual3d_View::MinMaxValues (Standard_Real& theXMin,
1715                                   Standard_Real& theYMin,
1716                                   Standard_Real& theZMin,
1717                                   Standard_Real& theXMax,
1718                                   Standard_Real& theYMax,
1719                                   Standard_Real& theZMax,
1720                                   const Standard_Boolean theToIgnoreInfiniteFlag) const
1721 {
1722   MinMaxValues (myStructsDisplayed,
1723                 theXMin, theYMin, theZMin,
1724                 theXMax, theYMax, theZMax,
1725                 theToIgnoreInfiniteFlag);
1726 }
1727
1728 // ========================================================================
1729 // function : MinMaxValues
1730 // purpose  :
1731 // ========================================================================
1732 void Visual3d_View::MinMaxValues (const Graphic3d_MapOfStructure& theSet,
1733                                   Standard_Real& theXMin,
1734                                   Standard_Real& theYMin,
1735                                   Standard_Real& theZMin,
1736                                   Standard_Real& theXMax,
1737                                   Standard_Real& theYMax,
1738                                   Standard_Real& theZMax,
1739                                   const Standard_Boolean theToIgnoreInfiniteFlag) const
1740 {
1741   if (theSet.IsEmpty ())
1742   {
1743     theXMin = RealFirst();
1744     theYMin = RealFirst();
1745     theZMin = RealFirst();
1746
1747     theXMax = RealLast();
1748     theYMax = RealLast();
1749     theZMax = RealLast();
1750   }
1751   else
1752   {
1753     Standard_Real aXm, aYm, aZm, aXM, aYM, aZM;
1754     Graphic3d_MapIteratorOfMapOfStructure anIterator (theSet);
1755
1756     theXMin = RealLast();
1757     theYMin = RealLast();
1758     theZMin = RealLast();
1759
1760     theXMax = RealFirst ();
1761     theYMax = RealFirst ();
1762     theZMax = RealFirst ();
1763
1764     for (anIterator.Initialize (theSet); anIterator.More(); anIterator.Next())
1765     {
1766       const Handle(Graphic3d_Structure)& aStructure = anIterator.Key();
1767
1768       if (!aStructure->IsVisible())
1769         continue;
1770
1771       if (aStructure->IsInfinite() && !theToIgnoreInfiniteFlag)
1772       {
1773         //XMin, YMin .... ZMax are initialized by means of infinite line data
1774         aStructure->MinMaxValues (aXm, aYm, aZm, aXM, aYM, aZM, Standard_False);
1775         if (aXm != RealFirst() && aXm < theXMin)
1776         {
1777           theXMin = aXm;
1778         }
1779         if (aYm != RealFirst() && aYm < theYMin)
1780         {
1781           theYMin = aYm;
1782         }
1783         if (aZm != RealFirst() && aZm < theZMin)
1784         {
1785           theZMin = aZm;
1786         }
1787         if (aXM != RealLast() && aXM > theXMax)
1788         {
1789           theXMax = aXM;
1790         }
1791         if (aYM != RealLast() && aYM > theYMax)
1792         {
1793           theYMax = aYM;
1794         }
1795         if (aZM != RealLast() && aZM > theZMax)
1796         {
1797           theZMax = aZM;
1798         }
1799       }
1800
1801       // Only non-empty and non-infinite structures
1802       // are taken into account for calculation of MinMax
1803       if ((!aStructure->IsInfinite() || theToIgnoreInfiniteFlag) && !aStructure->IsEmpty())
1804       {
1805         aStructure->MinMaxValues (aXm, aYm, aZm, aXM, aYM, aZM, theToIgnoreInfiniteFlag);
1806
1807         /* ABD 29/10/04  Transform Persistence of Presentation( pan, zoom, rotate ) */
1808         //"FitAll" operation ignores object with transform persitence parameter
1809         if(aStructure->TransformPersistenceMode() == Graphic3d_TMF_None )
1810         {
1811           theXMin = Min (aXm, theXMin);
1812           theYMin = Min (aYm, theYMin);
1813           theZMin = Min (aZm, theZMin);
1814           theXMax = Max (aXM, theXMax);
1815           theYMax = Max (aYM, theYMax);
1816           theZMax = Max (aZM, theZMax);
1817         }
1818       }
1819     }
1820
1821     // The following cases are relevant
1822     // For exemple if all structures are empty or infinite
1823     if (theXMax < theXMin) { aXm = theXMin; theXMin = theXMax; theXMax = aXm; }
1824     if (theYMax < theYMin) { aYm = theYMin; theYMin = theYMax; theYMax = aYm; }
1825     if (theZMax < theZMin) { aZm = theZMin; theZMin = theZMax; theZMax = aZm; }
1826   }
1827 }
1828
1829 // ========================================================================
1830 // function : MinMaxValues
1831 // purpose  :
1832 // ========================================================================
1833 void Visual3d_View::MinMaxValues (Standard_Real& theXMin,
1834                                   Standard_Real& theYMin,
1835                                   Standard_Real& theXMax,
1836                                   Standard_Real& theYMax,
1837                                   const Standard_Boolean theToIgnoreInfiniteFlag) const
1838 {
1839   MinMaxValues (myStructsDisplayed,
1840                 theXMin, theYMin,
1841                 theXMax, theYMax,
1842                 theToIgnoreInfiniteFlag);
1843 }
1844
1845 // ========================================================================
1846 // function : MinMaxValues
1847 // purpose  :
1848 // ========================================================================
1849 void Visual3d_View::MinMaxValues (const Graphic3d_MapOfStructure& theSet,
1850                                   Standard_Real& theXMin,
1851                                   Standard_Real& theYMin,
1852                                   Standard_Real& theXMax,
1853                                   Standard_Real& theYMax,
1854                                   const Standard_Boolean theToIgnoreInfiniteFlag) const
1855 {
1856   Standard_Real aXm, aYm, aZm, aXM, aYM, aZM;
1857   Standard_Real aXp, aYp, aZp;
1858
1859   MinMaxValues (theSet, aXm, aYm, aZm, aXM, aYM, aZM, theToIgnoreInfiniteFlag);
1860
1861   Projects (aXm, aYm, aZm, aXp, aYp, aZp);
1862   theXMin = aXp;
1863   theYMin = aYp;
1864
1865   Projects (aXM, aYM, aZM, aXp, aYp, aZp);
1866   theXMax = aXp;
1867   theYMax = aYp;
1868
1869   if (theXMax < theXMin) { aXp = theXMax; theXMax = theXMin; theXMin = aXp; }
1870   if (theYMax < theYMin) { aYp = theYMax; theYMax = theYMin; theYMin = aYp; }
1871 }
1872
1873 // =======================================================================
1874 // function : NumberOfDisplayedStructures
1875 // purpose  :
1876 // =======================================================================
1877 Standard_Integer Visual3d_View::NumberOfDisplayedStructures() const
1878 {
1879   return myStructsDisplayed.Extent();
1880 }
1881
1882 // =======================================================================
1883 // function : Projects
1884 // purpose  :
1885 // =======================================================================
1886 void Visual3d_View::Projects (const Standard_Real theX,
1887                               const Standard_Real theY,
1888                               const Standard_Real theZ,
1889                               Standard_Real& thePX,
1890                               Standard_Real& thePY,
1891                               Standard_Real& thePZ) const
1892 {
1893   const Handle(Graphic3d_Camera)& aCamera = MyCView.Context.Camera;
1894
1895   gp_XYZ aViewSpaceDimensions = aCamera->ViewDimensions();
1896   Standard_Real aXSize = aViewSpaceDimensions.X();
1897   Standard_Real aYSize = aViewSpaceDimensions.Y();
1898   Standard_Real aZSize = aViewSpaceDimensions.Z();
1899
1900   gp_Pnt aPoint = aCamera->Project (gp_Pnt (theX, theY, theZ));
1901
1902   // NDC [-1, 1] --> PROJ [ -size / 2, +size / 2 ]
1903   thePX = aPoint.X() * aXSize * 0.5;
1904   thePY = aPoint.Y() * aYSize * 0.5;
1905   thePZ = aPoint.Z() * aZSize * 0.5;
1906 }
1907
1908 // =======================================================================
1909 // function : Identification
1910 // purpose  :
1911 // =======================================================================
1912 Standard_Integer Visual3d_View::Identification() const
1913 {
1914   return MyCView.ViewId;
1915 }
1916
1917 // =======================================================================
1918 // function : ZBufferIsActivated
1919 // purpose  :
1920 // =======================================================================
1921 Standard_Boolean Visual3d_View::ZBufferIsActivated() const
1922 {
1923   if (IsDeleted()
1924   || !IsDefined()
1925   || !IsActive())
1926   {
1927     return Standard_False;
1928   }
1929
1930   if (MyCView.Context.ZBufferActivity == -1)
1931   {
1932     // not forced by the programmer => depends on the type of visualisation
1933     return MyContext.Visualization () == Visual3d_TOV_SHADING;
1934   }
1935   return MyCView.Context.ZBufferActivity != 0; //  0 or 1 => forced by the programmer
1936 }
1937
1938 // =======================================================================
1939 // function : SetTransparency
1940 // purpose  :
1941 // =======================================================================
1942 void Visual3d_View::SetTransparency (const Standard_Boolean theActivity)
1943 {
1944   if (IsDeleted()
1945   || !IsDefined()
1946   || !IsActive())
1947   {
1948     return;
1949   }
1950
1951   myGraphicDriver->Transparency (MyCView, theActivity);
1952 }
1953
1954 // =======================================================================
1955 // function : SetZBufferActivity
1956 // purpose  :
1957 // =======================================================================
1958 void Visual3d_View::SetZBufferActivity (const Standard_Integer theActivity)
1959 {
1960   if (IsDeleted()
1961   ||  MyCView.Context.ZBufferActivity == theActivity
1962   || !IsDefined()
1963   || !IsActive())
1964   {
1965     return;
1966   }
1967
1968   MyCView.Context.ZBufferActivity = theActivity;
1969   myGraphicDriver->SetVisualisation (MyCView);
1970 }
1971
1972 // =======================================================================
1973 // function : UpdateView
1974 // purpose  :
1975 // =======================================================================
1976 void Visual3d_View::UpdateView()
1977 {
1978   MyCView.Context.Aliasing        = MyContext.AliasingIsOn();
1979   MyCView.Context.BackZClipping   = MyContext.BackZClippingIsOn();
1980   MyCView.Context.FrontZClipping  = MyContext.FrontZClippingIsOn();
1981   MyCView.Context.DepthCueing     = MyContext.DepthCueingIsOn();
1982
1983   MyCView.Context.ZClipFrontPlane = float (MyContext.ZClippingFrontPlane());
1984   MyCView.Context.ZClipBackPlane  = float (MyContext.ZClippingBackPlane());
1985   MyCView.Context.DepthFrontPlane = float (MyContext.DepthCueingFrontPlane());
1986   MyCView.Context.DepthBackPlane  = float (MyContext.DepthCueingBackPlane());
1987
1988   MyCView.Context.Model           = MyContext.Model();
1989   MyCView.Context.Visualization   = MyContext.Visualization();
1990
1991   MyCView.Context.TextureEnv      = MyContext.TextureEnv();
1992   MyCView.Context.SurfaceDetail   = MyContext.SurfaceDetail();
1993 }
1994
1995 // =======================================================================
1996 // function : Compute
1997 // purpose  :
1998 // =======================================================================
1999 void Visual3d_View::Compute()
2000 {
2001   // force HLRValidation to False on all structures calculated in the view
2002   const Standard_Integer aNbCompStructs = myStructsComputed.Length();
2003   for (Standard_Integer aStructIter = 1; aStructIter <= aNbCompStructs; ++aStructIter)
2004   {
2005     myStructsComputed.Value (aStructIter)->SetHLRValidation (Standard_False);
2006   }
2007
2008   if (!ComputedMode())
2009   {
2010     return;
2011   }
2012
2013   // Change of orientation or of projection type =>
2014   // Remove structures that were calculated for the previous orientation.
2015   // Recalculation of new structures.
2016   NCollection_Sequence<Handle(Graphic3d_Structure)> aStructsSeq;
2017   for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (myStructsDisplayed); aStructIter.More(); aStructIter.Next())
2018   {
2019     const Visual3d_TypeOfAnswer anAnswer = AcceptDisplay (aStructIter.Key());
2020     if (anAnswer == Visual3d_TOA_COMPUTE)
2021     {
2022       aStructsSeq.Append (aStructIter.Key()); // if the structure was calculated, it is recalculated
2023     }
2024   }
2025
2026   for (NCollection_Sequence<Handle(Graphic3d_Structure)>::Iterator aStructIter (aStructsSeq); aStructIter.More(); aStructIter.Next())
2027   {
2028     Display (aStructIter.ChangeValue(), Aspect_TOU_WAIT);
2029   }
2030 }
2031
2032 // =======================================================================
2033 // function : ReCompute
2034 // purpose  :
2035 // =======================================================================
2036 void Visual3d_View::ReCompute (const Handle(Graphic3d_Structure)& theStruct)
2037 {
2038   theStruct->CalculateBoundBox();
2039   if (!theStruct->IsMutable()
2040    && !theStruct->CStructure()->IsForHighlight
2041    && !theStruct->CStructure()->IsInfinite)
2042   {
2043     const Standard_Integer aLayerId = theStruct->DisplayPriority();
2044     myGraphicDriver->InvalidateBVHData(MyCView, aLayerId);
2045   }
2046
2047   if (!ComputedMode()
2048    ||  IsDeleted()
2049    || !IsDefined()
2050    || !IsActive()
2051    || !MyWindow->IsMapped()
2052    || !theStruct->IsDisplayed())
2053   {
2054     return;
2055   }
2056
2057   const Visual3d_TypeOfAnswer anAnswer = AcceptDisplay (theStruct);
2058   if (anAnswer != Visual3d_TOA_COMPUTE)
2059   {
2060     return;
2061   }
2062
2063   const Standard_Integer anIndex = IsComputed (theStruct);
2064   if (anIndex == 0)
2065   {
2066     return;
2067   }
2068
2069   // compute + validation
2070   TColStd_Array2OfReal anIdent (0, 3, 0, 3);
2071   for (Standard_Integer aRow = 0; aRow <= 3; ++aRow)
2072   {
2073     for (Standard_Integer aCol = 0; aCol <= 3; ++aCol)
2074     {
2075       anIdent (aRow, aCol) = (aRow == aCol ? 1.0 : 0.0);
2076     }
2077   }
2078   TColStd_Array2OfReal aTrsf (0, 3, 0, 3);
2079   theStruct->Transform (aTrsf);
2080
2081   Handle(Graphic3d_Structure) aCompStructOld = myStructsComputed.ChangeValue (anIndex);
2082   Handle(Graphic3d_Structure) aCompStruct    = aCompStructOld;
2083   aCompStruct->SetTransform (anIdent, Graphic3d_TOC_REPLACE);
2084   theStruct->IsTransformed() ? theStruct->Compute (this, aTrsf, aCompStruct)
2085                              : theStruct->Compute (this,        aCompStruct);
2086   aCompStruct->SetHLRValidation (Standard_True);
2087
2088   // of which type will be the computed?
2089   const Visual3d_TypeOfVisualization aViewType = MyContext.Visualization();
2090   const Standard_Boolean toComputeWireframe = aViewType == Visual3d_TOV_WIREFRAME
2091                                            && theStruct->ComputeVisual() != Graphic3d_TOS_SHADING;
2092   const Standard_Boolean toComputeShading   = aViewType == Visual3d_TOV_SHADING
2093                                            && theStruct->ComputeVisual() != Graphic3d_TOS_WIREFRAME;
2094   if (toComputeWireframe)
2095   {
2096     aCompStruct->SetVisual (Graphic3d_TOS_WIREFRAME);
2097   }
2098   else if (toComputeShading)
2099   {
2100     aCompStruct->SetVisual (Graphic3d_TOS_SHADING);
2101   }
2102
2103   if (theStruct->IsHighlighted())
2104   {
2105     aCompStruct->SetHighlightColor (theStruct->HighlightColor());
2106     aCompStruct->GraphicHighlight (Aspect_TOHM_COLOR);
2107   }
2108
2109   // The previous calculation is removed and the new one is dislayed
2110   myGraphicDriver->EraseStructure   (MyCView, *aCompStructOld->CStructure());
2111   myGraphicDriver->DisplayStructure (MyCView, *aCompStruct->CStructure(), theStruct->DisplayPriority());
2112
2113   // why not just replace existing items?
2114   //myStructsToCompute.ChangeValue (anIndex) = theStruct;
2115   //myStructsComputed .ChangeValue (anIndex) = aCompStruct;
2116
2117   // hlhsr and the new associated compute are added
2118   myStructsToCompute.Append (theStruct);
2119   myStructsComputed .Append (aCompStruct);
2120
2121   // hlhsr and the new associated compute are removed
2122   myStructsToCompute.Remove (anIndex);
2123   myStructsComputed .Remove (anIndex);
2124 }
2125
2126 // =======================================================================
2127 // function : GraphicDriver
2128 // purpose  :
2129 // =======================================================================
2130 const Handle(Graphic3d_GraphicDriver)& Visual3d_View::GraphicDriver() const
2131 {
2132   return myGraphicDriver;
2133 }
2134
2135 // =======================================================================
2136 // function : HaveTheSameOwner
2137 // purpose  :
2138 // =======================================================================
2139 Standard_Integer Visual3d_View::HaveTheSameOwner (const Handle(Graphic3d_Structure)& theStruct) const
2140 {
2141   // Find in the sequence of already calculated structures
2142   // 1/ Structure with the same Owner as <AStructure>
2143   // 2/ Which is not <AStructure>
2144   // 3/ COMPUTED which of is valid
2145   const Standard_Integer aNbToCompStructs = myStructsToCompute.Length();
2146   for (Standard_Integer aStructIter = 1; aStructIter <= aNbToCompStructs; ++aStructIter)
2147   {
2148     const Handle(Graphic3d_Structure)& aStructToComp = myStructsToCompute.Value (aStructIter);
2149     if (aStructToComp->Owner()          == theStruct->Owner()
2150      && aStructToComp->Identification() != theStruct->Identification())
2151     {
2152       const Handle(Graphic3d_Structure)& aStructComp = myStructsComputed.Value (aStructIter);
2153       if (aStructComp->HLRValidation())
2154       {
2155         return aStructIter;
2156       }
2157     }
2158   }
2159   return 0;
2160 }
2161
2162 // =======================================================================
2163 // function : CView
2164 // purpose  :
2165 // =======================================================================
2166 Standard_Address Visual3d_View::CView() const
2167 {
2168   return Standard_Address (&MyCView);
2169 }
2170
2171 // =======================================================================
2172 // function : ZBufferTriedronSetup
2173 // purpose  :
2174 // =======================================================================
2175 void Visual3d_View::ZBufferTriedronSetup (const Quantity_NameOfColor theXColor,
2176                                           const Quantity_NameOfColor theYColor,
2177                                           const Quantity_NameOfColor theZColor,
2178                                           const Standard_Real        theSizeRatio,
2179                                           const Standard_Real        theAxisDiametr,
2180                                           const Standard_Integer     theNbFacettes)
2181 {
2182   myGraphicDriver->ZBufferTriedronSetup (theXColor, theYColor, theZColor,
2183                                          theSizeRatio, theAxisDiametr, theNbFacettes);
2184 }
2185
2186 // =======================================================================
2187 // function : TriedronDisplay
2188 // purpose  :
2189 // =======================================================================
2190 void Visual3d_View::TriedronDisplay (const Aspect_TypeOfTriedronPosition thePosition,
2191                                      const Quantity_NameOfColor          theColor,
2192                                      const Standard_Real                 theScale,
2193                                      const Standard_Boolean              theAsWireframe)
2194 {
2195   myGraphicDriver->TriedronDisplay (MyCView, thePosition, theColor, theScale, theAsWireframe);
2196 }
2197
2198 // =======================================================================
2199 // function : TriedronErase
2200 // purpose  :
2201 // =======================================================================
2202 void Visual3d_View::TriedronErase()
2203 {
2204   myGraphicDriver->TriedronErase (MyCView);
2205 }
2206
2207 // =======================================================================
2208 // function : TriedronEcho
2209 // purpose  :
2210 // =======================================================================
2211 void Visual3d_View::TriedronEcho (const Aspect_TypeOfTriedronEcho theType)
2212 {
2213   myGraphicDriver->TriedronEcho (MyCView, theType);
2214 }
2215
2216 static Standard_Boolean checkFloat (const Standard_Real theValue)
2217 {
2218   return theValue > -FLT_MAX
2219       && theValue <  FLT_MAX;
2220 }
2221
2222 static void SetMinMaxValuesCallback (Visual3d_View* theView)
2223 {
2224   Graphic3d_Vec3d aMin, aMax;
2225   theView->MinMaxValues (aMin.x(), aMin.y(), aMin.z(),
2226                          aMax.x(), aMax.y(), aMax.z());
2227   if (checkFloat (aMin.x()) && checkFloat (aMin.y()) && checkFloat (aMin.z())
2228    && checkFloat (aMax.x()) && checkFloat (aMax.y()) && checkFloat (aMax.z()))
2229   {
2230     const Handle(Graphic3d_GraphicDriver)& aDriver = theView->GraphicDriver();
2231     aDriver->GraduatedTrihedronMinMaxValues ((Standard_ShortReal )aMin.x(), (Standard_ShortReal )aMin.y(), (Standard_ShortReal )aMin.z(),
2232                                              (Standard_ShortReal )aMax.x(), (Standard_ShortReal )aMax.y(), (Standard_ShortReal )aMax.z());
2233   }
2234 }
2235
2236 // =======================================================================
2237 // function : GetGraduatedTrihedron
2238 // purpose  :
2239 // =======================================================================
2240 Standard_Boolean Visual3d_View::GetGraduatedTrihedron (TCollection_ExtendedString& theXName,
2241                                                        TCollection_ExtendedString& theYName,
2242                                                        TCollection_ExtendedString& theZName,
2243                                                        Standard_Boolean&           theToDrawXName,
2244                                                        Standard_Boolean&           theToDrawYName,
2245                                                        Standard_Boolean&           theToDrawZName,
2246                                                        Standard_Boolean&           theToDrawXValues,
2247                                                        Standard_Boolean&           theToDrawYValues,
2248                                                        Standard_Boolean&           theToDrawZValues,
2249                                                        Standard_Boolean&           theToDrawGrid,
2250                                                        Standard_Boolean&           theToDrawAxes,
2251                                                        Standard_Integer&           theNbX,
2252                                                        Standard_Integer&           theNbY,
2253                                                        Standard_Integer&           theNbZ,
2254                                                        Standard_Integer&           theXOffset,
2255                                                        Standard_Integer&           theYOffset,
2256                                                        Standard_Integer&           theZOffset,
2257                                                        Standard_Integer&           theXAxisOffset,
2258                                                        Standard_Integer&           theYAxisOffset,
2259                                                        Standard_Integer&           theZAxisOffset,
2260                                                        Standard_Boolean&           theToDrawXTickMarks,
2261                                                        Standard_Boolean&           theToDrawYTickMarks,
2262                                                        Standard_Boolean&           theToDrawZTickMarks,
2263                                                        Standard_Integer&           theXTickMarkLength,
2264                                                        Standard_Integer&           theYTickMarkLength,
2265                                                        Standard_Integer&           theZTickMarkLength,
2266                                                        Quantity_Color&             theGridColor,
2267                                                        Quantity_Color&             theXNameColor,
2268                                                        Quantity_Color&             theYNameColor,
2269                                                        Quantity_Color&             theZNameColor,
2270                                                        Quantity_Color&             theXColor,
2271                                                        Quantity_Color&             theYColor,
2272                                                        Quantity_Color&             theZColor,
2273                                                        TCollection_AsciiString&    theFontOfNames,
2274                                                        Font_FontAspect&            theStyleOfNames,
2275                                                        Standard_Integer&           theSizeOfNames,
2276                                                        TCollection_AsciiString&    theFontOfValues,
2277                                                        Font_FontAspect&            theStyleOfValues,
2278                                                        Standard_Integer&           theSizeOfValues) const
2279 {
2280   if (!MyGTrihedron.ptrVisual3dView)
2281   {
2282     return Standard_False;
2283   }
2284
2285   theXName = MyGTrihedron.xname;
2286   theYName = MyGTrihedron.yname;
2287   theZName = MyGTrihedron.zname;
2288   theToDrawXName   = MyGTrihedron.xdrawname;
2289   theToDrawYName   = MyGTrihedron.ydrawname;
2290   theToDrawZName   = MyGTrihedron.zdrawname;
2291   theToDrawXValues = MyGTrihedron.xdrawvalues;
2292   theToDrawYValues = MyGTrihedron.ydrawvalues;
2293   theToDrawZValues = MyGTrihedron.zdrawvalues;
2294   theToDrawGrid    = MyGTrihedron.drawgrid;
2295   theToDrawAxes    = MyGTrihedron.drawaxes;
2296   theNbX = MyGTrihedron.nbx;
2297   theNbY = MyGTrihedron.nby;
2298   theNbZ = MyGTrihedron.nbz;
2299   theXOffset     = MyGTrihedron.xoffset;
2300   theYOffset     = MyGTrihedron.yoffset;
2301   theZOffset     = MyGTrihedron.zoffset;
2302   theXAxisOffset = MyGTrihedron.xaxisoffset;
2303   theYAxisOffset = MyGTrihedron.yaxisoffset;
2304   theZAxisOffset = MyGTrihedron.zaxisoffset;
2305   theToDrawXTickMarks = MyGTrihedron.xdrawtickmarks;
2306   theToDrawYTickMarks = MyGTrihedron.ydrawtickmarks;
2307   theToDrawZTickMarks = MyGTrihedron.zdrawtickmarks;
2308   theXTickMarkLength  = MyGTrihedron.xtickmarklength;
2309   theYTickMarkLength  = MyGTrihedron.ytickmarklength;
2310   theZTickMarkLength  = MyGTrihedron.ztickmarklength;
2311   theGridColor  = MyGTrihedron.gridcolor;
2312   theXNameColor = MyGTrihedron.xnamecolor;
2313   theYNameColor = MyGTrihedron.ynamecolor;
2314   theZNameColor = MyGTrihedron.znamecolor;
2315   theXColor     = MyGTrihedron.xcolor;
2316   theYColor     = MyGTrihedron.ycolor;
2317   theZColor     = MyGTrihedron.zcolor;
2318   theFontOfNames   = MyGTrihedron.fontOfNames;
2319   theStyleOfNames  = MyGTrihedron.styleOfNames;
2320   theSizeOfNames   = MyGTrihedron.sizeOfNames;
2321   theFontOfValues  = MyGTrihedron.fontOfValues;
2322   theStyleOfValues = MyGTrihedron.styleOfValues;
2323   theSizeOfValues  = MyGTrihedron.sizeOfValues;
2324   return Standard_True;
2325 }
2326
2327 // =======================================================================
2328 // function : GraduatedTrihedronDisplay
2329 // purpose  :
2330 // =======================================================================
2331 void Visual3d_View::GraduatedTrihedronDisplay (const TCollection_ExtendedString& theXName,
2332                                                const TCollection_ExtendedString& theYName,
2333                                                const TCollection_ExtendedString& theZName,
2334                                                const Standard_Boolean theToDrawXName,
2335                                                const Standard_Boolean theToDrawYName,
2336                                                const Standard_Boolean theToDrawZName,
2337                                                const Standard_Boolean theToDrawXValues,
2338                                                const Standard_Boolean theToDrawYValues,
2339                                                const Standard_Boolean theToDrawZValues,
2340                                                const Standard_Boolean theToDrawGrid,
2341                                                const Standard_Boolean theToDrawAxes,
2342                                                const Standard_Integer theNbX,
2343                                                const Standard_Integer theNbY,
2344                                                const Standard_Integer theNbZ,
2345                                                const Standard_Integer theXOffset,
2346                                                const Standard_Integer theYOffset,
2347                                                const Standard_Integer theZOffset,
2348                                                const Standard_Integer theXAxisOffset,
2349                                                const Standard_Integer theYAxisOffset,
2350                                                const Standard_Integer theZAxisOffset,
2351                                                const Standard_Boolean theToDrawXTickMarks,
2352                                                const Standard_Boolean theToDrawYTickMarks,
2353                                                const Standard_Boolean theToDrawZTickMarks,
2354                                                const Standard_Integer theXTickMarkLength,
2355                                                const Standard_Integer theYTickMarkLength,
2356                                                const Standard_Integer theZTickMarkLength,
2357                                                const Quantity_Color&  theGridColor,
2358                                                const Quantity_Color&  theXNameColor,
2359                                                const Quantity_Color&  theYNameColor,
2360                                                const Quantity_Color&  theZNameColor,
2361                                                const Quantity_Color&  theXColor,
2362                                                const Quantity_Color&  theYColor,
2363                                                const Quantity_Color&  theZColor,
2364                                                const TCollection_AsciiString& theFontOfNames,
2365                                                const Font_FontAspect  theStyleOfNames,
2366                                                const Standard_Integer theSizeOfNames,
2367                                                const TCollection_AsciiString& theFontOfValues,
2368                                                const Font_FontAspect  theStyleOfValues,
2369                                                const Standard_Integer theSizeOfValues)
2370 {
2371   MyGTrihedron.xname = theXName;
2372   MyGTrihedron.yname = theYName;
2373   MyGTrihedron.zname = theZName;
2374   MyGTrihedron.xdrawname = theToDrawXName;
2375   MyGTrihedron.ydrawname = theToDrawYName;
2376   MyGTrihedron.zdrawname = theToDrawZName;
2377   MyGTrihedron.xdrawvalues = theToDrawXValues;
2378   MyGTrihedron.ydrawvalues = theToDrawYValues;
2379   MyGTrihedron.zdrawvalues = theToDrawZValues;
2380   MyGTrihedron.drawgrid = theToDrawGrid;
2381   MyGTrihedron.drawaxes = theToDrawAxes;
2382   MyGTrihedron.nbx = theNbX;
2383   MyGTrihedron.nby = theNbY;
2384   MyGTrihedron.nbz = theNbZ;
2385   MyGTrihedron.xoffset = theXOffset;
2386   MyGTrihedron.yoffset = theYOffset;
2387   MyGTrihedron.zoffset = theZOffset;
2388   MyGTrihedron.xaxisoffset = theXAxisOffset;
2389   MyGTrihedron.yaxisoffset = theYAxisOffset;
2390   MyGTrihedron.zaxisoffset = theZAxisOffset;
2391   MyGTrihedron.xdrawtickmarks = theToDrawXTickMarks;
2392   MyGTrihedron.ydrawtickmarks = theToDrawYTickMarks;
2393   MyGTrihedron.zdrawtickmarks = theToDrawZTickMarks;
2394   MyGTrihedron.xtickmarklength = theXTickMarkLength;
2395   MyGTrihedron.ytickmarklength = theYTickMarkLength;
2396   MyGTrihedron.ztickmarklength = theZTickMarkLength;
2397   MyGTrihedron.gridcolor  = theGridColor;
2398   MyGTrihedron.xnamecolor = theXNameColor;
2399   MyGTrihedron.ynamecolor = theYNameColor;
2400   MyGTrihedron.znamecolor = theZNameColor;
2401   MyGTrihedron.xcolor = theXColor;
2402   MyGTrihedron.ycolor = theYColor;
2403   MyGTrihedron.zcolor = theZColor;
2404   MyGTrihedron.fontOfNames   = theFontOfNames;
2405   MyGTrihedron.styleOfNames  = theStyleOfNames;
2406   MyGTrihedron.sizeOfNames   = theSizeOfNames;
2407   MyGTrihedron.fontOfValues  = theFontOfValues;
2408   MyGTrihedron.styleOfValues = theStyleOfValues;
2409   MyGTrihedron.sizeOfValues  = theSizeOfValues;
2410
2411   MyGTrihedron.ptrVisual3dView = this;
2412   MyGTrihedron.cbCubicAxes     = SetMinMaxValuesCallback;
2413   myGraphicDriver->GraduatedTrihedronDisplay (MyCView, MyGTrihedron);
2414 }
2415
2416 // =======================================================================
2417 // function : GraduatedTrihedronErase
2418 // purpose  :
2419 // =======================================================================
2420 void Visual3d_View::GraduatedTrihedronErase()
2421 {
2422   MyGTrihedron.ptrVisual3dView = NULL;
2423   myGraphicDriver->GraduatedTrihedronErase (MyCView);
2424 }
2425
2426 // =======================================================================
2427 // function : UnderLayer
2428 // purpose  :
2429 // =======================================================================
2430 const Handle(Visual3d_Layer)& Visual3d_View::UnderLayer() const
2431 {
2432   return myViewManager->UnderLayer();
2433 }
2434
2435 // =======================================================================
2436 // function : OverLayer
2437 // purpose  :
2438 // =======================================================================
2439 const Handle(Visual3d_Layer)& Visual3d_View::OverLayer() const
2440 {
2441   return myViewManager->OverLayer();
2442 }
2443
2444 // =======================================================================
2445 // function : LightLimit
2446 // purpose  :
2447 // =======================================================================
2448 Standard_Integer Visual3d_View::LightLimit() const
2449 {
2450   return myGraphicDriver->InquireLightLimit();
2451 }
2452
2453 // =======================================================================
2454 // function : PlaneLimit
2455 // purpose  :
2456 // =======================================================================
2457 Standard_Integer Visual3d_View::PlaneLimit() const
2458 {
2459   return myGraphicDriver->InquirePlaneLimit();
2460 }
2461
2462 // =======================================================================
2463 // function : ViewManager
2464 // purpose  :
2465 // =======================================================================
2466 Handle(Visual3d_ViewManager) Visual3d_View::ViewManager() const
2467 {
2468   return myViewManager;
2469 }
2470
2471 // =======================================================================
2472 // function : SetComputedMode
2473 // purpose  :
2474 // =======================================================================
2475 void Visual3d_View::SetComputedMode (const Standard_Boolean theMode)
2476 {
2477   if (( theMode &&  myIsInComputedMode)
2478    || (!theMode && !myIsInComputedMode))
2479   {
2480     return;
2481   }
2482
2483   myIsInComputedMode = theMode;
2484   if (!myIsInComputedMode)
2485   {
2486     for (Graphic3d_MapIteratorOfMapOfStructure aStructIter (myStructsDisplayed); aStructIter.More(); aStructIter.Next())
2487     {
2488       const Handle(Graphic3d_Structure)& aStruct  = aStructIter.Key();
2489       const Visual3d_TypeOfAnswer        anAnswer = AcceptDisplay (aStruct);
2490       if (anAnswer != Visual3d_TOA_COMPUTE)
2491       {
2492         continue;
2493       }
2494
2495       const Standard_Integer anIndex = IsComputed (aStruct);
2496       if (anIndex != 0)
2497       {
2498         const Handle(Graphic3d_Structure)& aStructComp = myStructsComputed.Value (anIndex);
2499         myGraphicDriver->EraseStructure   (MyCView, *aStructComp->CStructure());
2500         myGraphicDriver->DisplayStructure (MyCView, *aStruct->CStructure(), aStruct->DisplayPriority());
2501       }
2502     }
2503     return;
2504   }
2505
2506   for (Graphic3d_MapIteratorOfMapOfStructure aDispStructIter (myStructsDisplayed); aDispStructIter.More(); aDispStructIter.Next())
2507   {
2508     Handle(Graphic3d_Structure) aStruct = aDispStructIter.Key();
2509     const Visual3d_TypeOfAnswer anAnswer = AcceptDisplay (aStruct);
2510     if (anAnswer != Visual3d_TOA_COMPUTE)
2511     {
2512       continue;
2513     }
2514
2515     const Standard_Integer anIndex = IsComputed (aStruct);
2516     if (anIndex != 0)
2517     {
2518       myGraphicDriver->EraseStructure   (MyCView, *aStruct->CStructure());
2519       myGraphicDriver->DisplayStructure (MyCView, *(myStructsComputed.Value (anIndex)->CStructure()), aStruct->DisplayPriority());
2520
2521       Display (aStruct, Aspect_TOU_WAIT);
2522       if (aStruct->IsHighlighted())
2523       {
2524         const Handle(Graphic3d_Structure)& aCompStruct = myStructsComputed.Value (anIndex);
2525         if (!aCompStruct->IsHighlighted())
2526         {
2527           aCompStruct->SetHighlightColor (aStruct->HighlightColor());
2528           aCompStruct->GraphicHighlight (Aspect_TOHM_COLOR);
2529         }
2530       }
2531     }
2532     else
2533     {
2534       TColStd_Array2OfReal aTrsf (0, 3, 0, 3);
2535       aStruct->Transform (aTrsf);
2536       Handle(Graphic3d_Structure) aCompStruct = aStruct->IsTransformed() ? aStruct->Compute (this, aTrsf) : aStruct->Compute (this);
2537       aCompStruct->SetHLRValidation (Standard_True);
2538
2539       const Visual3d_TypeOfVisualization aViewType = MyContext.Visualization();
2540       const Standard_Boolean toComputeWireframe = aViewType == Visual3d_TOV_WIREFRAME
2541                                                 && aStruct->ComputeVisual() != Graphic3d_TOS_SHADING;
2542       const Standard_Boolean toComputeShading   = aViewType == Visual3d_TOV_SHADING
2543                                                 && aStruct->ComputeVisual() != Graphic3d_TOS_WIREFRAME;
2544       if (toComputeWireframe) aCompStruct->SetVisual (Graphic3d_TOS_WIREFRAME);
2545       if (toComputeShading  ) aCompStruct->SetVisual (Graphic3d_TOS_SHADING);
2546
2547       if (aStruct->IsHighlighted())
2548       {
2549         aCompStruct->SetHighlightColor (aStruct->HighlightColor());
2550         aCompStruct->GraphicHighlight (Aspect_TOHM_COLOR);
2551       }
2552
2553       Standard_Boolean hasResult = Standard_False;
2554       const Standard_Integer aNbToCompute = myStructsToCompute.Length();
2555       const Standard_Integer aStructId    = aStruct->Identification();
2556       for (Standard_Integer aToCompStructIter = 1; aToCompStructIter <= aNbToCompute; ++aToCompStructIter)
2557       {
2558         if (myStructsToCompute.Value (aToCompStructIter)->Identification() == aStructId)
2559         {
2560           hasResult = Standard_True;
2561           myStructsComputed.ChangeValue (aToCompStructIter) = aCompStruct;
2562           break;
2563         }
2564       }
2565
2566       if (!hasResult)
2567       {
2568         myStructsToCompute.Append (aStruct);
2569         myStructsComputed .Append (aCompStruct);
2570       }
2571
2572       myGraphicDriver->EraseStructure   (MyCView, *aStruct->CStructure());
2573       myGraphicDriver->DisplayStructure (MyCView, *aCompStruct->CStructure(), aStruct->DisplayPriority());
2574     }
2575   }
2576   Update (myViewManager->UpdateMode());
2577 }
2578
2579 // =======================================================================
2580 // function : ComputedMode
2581 // purpose  :
2582 // =======================================================================
2583 Standard_Boolean Visual3d_View::ComputedMode() const
2584 {
2585   return myIsInComputedMode;
2586 }
2587
2588 // =======================================================================
2589 // function : SetBackFacingModel
2590 // purpose  :
2591 // =======================================================================
2592 void Visual3d_View::SetBackFacingModel (const Visual3d_TypeOfBackfacingModel theModel)
2593 {
2594   switch (theModel)
2595   {
2596     default:
2597     case Visual3d_TOBM_AUTOMATIC:
2598       MyCView.Backfacing = 0;
2599       break;
2600     case Visual3d_TOBM_FORCE:
2601       MyCView.Backfacing = 1;
2602       break;
2603     case Visual3d_TOBM_DISABLE:
2604       MyCView.Backfacing = -1;
2605       break;
2606   }
2607   myGraphicDriver->SetBackFacingModel (MyCView);
2608 }
2609
2610 // =======================================================================
2611 // function : BackFacingModel
2612 // purpose  :
2613 // =======================================================================
2614 Visual3d_TypeOfBackfacingModel Visual3d_View::BackFacingModel() const
2615 {
2616   switch (MyCView.Backfacing)
2617   {
2618     case 0: return Visual3d_TOBM_AUTOMATIC;
2619     case 1: return Visual3d_TOBM_FORCE;
2620   }
2621   return Visual3d_TOBM_DISABLE;
2622 }
2623
2624 // =======================================================================
2625 // function : EnableDepthTest
2626 // purpose  :
2627 // =======================================================================
2628 void Visual3d_View::EnableDepthTest (const Standard_Boolean theToEnable) const
2629 {
2630   myGraphicDriver->SetDepthTestEnabled (MyCView, theToEnable);
2631 }
2632
2633 // =======================================================================
2634 // function : IsDepthTestEnabled
2635 // purpose  :
2636 // =======================================================================
2637 Standard_Boolean Visual3d_View::IsDepthTestEnabled() const
2638 {
2639   return myGraphicDriver->IsDepthTestEnabled (MyCView);
2640 }
2641
2642 // =======================================================================
2643 // function : ReadDepths
2644 // purpose  :
2645 // =======================================================================
2646 void Visual3d_View::ReadDepths (const Standard_Integer theX,
2647                                 const Standard_Integer theY,
2648                                 const Standard_Integer theWidth,
2649                                 const Standard_Integer theHeight,
2650                                 const Standard_Address theBuffer) const
2651 {
2652   myGraphicDriver->ReadDepths (MyCView, theX, theY, theWidth, theHeight, theBuffer);
2653 }
2654
2655 // =======================================================================
2656 // function : FBOCreate
2657 // purpose  :
2658 // =======================================================================
2659 Graphic3d_PtrFrameBuffer Visual3d_View::FBOCreate(const Standard_Integer theWidth,
2660                                                   const Standard_Integer theHeight)
2661 {
2662   return myGraphicDriver->FBOCreate( MyCView, theWidth, theHeight );
2663 }
2664
2665 // =======================================================================
2666 // function : FBORelease
2667 // purpose  :
2668 // =======================================================================
2669 void Visual3d_View::FBORelease(Graphic3d_PtrFrameBuffer& theFBOPtr)
2670 {
2671   myGraphicDriver->FBORelease( MyCView, theFBOPtr );
2672 }
2673
2674 // =======================================================================
2675 // function : FBOGetDimensions
2676 // purpose  :
2677 // =======================================================================
2678 void Visual3d_View::FBOGetDimensions(const Graphic3d_PtrFrameBuffer theFBOPtr,
2679                                      Standard_Integer& theWidth,    Standard_Integer& theHeight,
2680                                      Standard_Integer& theWidthMax, Standard_Integer& theHeightMax)
2681 {
2682   myGraphicDriver->FBOGetDimensions( MyCView, theFBOPtr,
2683                                      theWidth, theHeight,
2684                                      theWidthMax, theHeightMax );
2685 }
2686
2687 // =======================================================================
2688 // function : FBOChangeViewport
2689 // purpose  :
2690 // =======================================================================
2691 void Visual3d_View::FBOChangeViewport(Graphic3d_PtrFrameBuffer& theFBOPtr,
2692                                       const Standard_Integer theWidth, const Standard_Integer theHeight)
2693 {
2694   myGraphicDriver->FBOChangeViewport( MyCView, theFBOPtr,
2695                                      theWidth, theHeight );
2696 }
2697
2698 // =======================================================================
2699 // function : BufferDump
2700 // purpose  :
2701 // =======================================================================
2702 Standard_Boolean Visual3d_View::BufferDump (Image_PixMap&               theImage,
2703                                             const Graphic3d_BufferType& theBufferType)
2704 {
2705   return myGraphicDriver->BufferDump (MyCView, theImage, theBufferType);
2706 }
2707
2708 // =======================================================================
2709 // function : EnableGLLight
2710 // purpose  :
2711 // =======================================================================
2712 void Visual3d_View::EnableGLLight( const Standard_Boolean enable ) const
2713 {
2714   myGraphicDriver->SetGLLightEnabled( MyCView, enable );
2715 }
2716
2717 // =======================================================================
2718 // function : IsGLLightEnabled
2719 // purpose  :
2720 // =======================================================================
2721 Standard_Boolean Visual3d_View::IsGLLightEnabled() const
2722 {
2723   return myGraphicDriver->IsGLLightEnabled( MyCView );
2724 }
2725
2726 // =======================================================================
2727 // function : Export
2728 // purpose  :
2729 // =======================================================================
2730 Standard_Boolean Visual3d_View::Export (const Standard_CString       theFileName,
2731                                         const Graphic3d_ExportFormat theFormat,
2732                                         const Graphic3d_SortType     theSortType,
2733                                         const Standard_Real          thePrecision,
2734                                         const Standard_Address       theProgressBarFunc,
2735                                         const Standard_Address       theProgressObject) const
2736 {
2737   Handle(Visual3d_Layer) anUnderLayer = myViewManager->UnderLayer();
2738   Handle(Visual3d_Layer) anOverLayer  = myViewManager->OverLayer();
2739
2740   Aspect_CLayer2d anOverCLayer;
2741   Aspect_CLayer2d anUnderCLayer;
2742   anOverCLayer.ptrLayer = anUnderCLayer.ptrLayer = NULL;
2743
2744   if (!anOverLayer.IsNull())
2745     anOverCLayer = anOverLayer->CLayer();
2746   if (!anUnderLayer.IsNull())
2747     anUnderCLayer = anUnderLayer->CLayer();
2748
2749   Standard_Integer aWidth, aHeight;
2750   Window()->Size (aWidth, aHeight);
2751
2752   return myGraphicDriver->Export (theFileName, theFormat, theSortType,
2753                                   aWidth, aHeight, MyCView, anUnderCLayer, anOverCLayer,
2754                                   thePrecision, theProgressBarFunc, theProgressObject);
2755 }
2756
2757 // =======================================================================
2758 // function : SetZLayerSettings
2759 // purpose  :
2760 // =======================================================================
2761 void Visual3d_View::SetZLayerSettings (const Standard_Integer theLayerId,
2762                                        const Graphic3d_ZLayerSettings& theSettings)
2763 {
2764   myGraphicDriver->SetZLayerSettings (MyCView, theLayerId, theSettings);
2765 }
2766
2767 // =======================================================================
2768 // function : AddZLayer
2769 // purpose  :
2770 // =======================================================================
2771 void Visual3d_View::AddZLayer (const Standard_Integer theLayerId)
2772 {
2773   myGraphicDriver->AddZLayer (MyCView, theLayerId);
2774 }
2775
2776 // =======================================================================
2777 // function : RemoveZLayer
2778 // purpose  :
2779 // =======================================================================
2780 void Visual3d_View::RemoveZLayer (const Standard_Integer theLayerId)
2781 {
2782   myGraphicDriver->RemoveZLayer (MyCView, theLayerId);
2783 }
2784
2785 // =======================================================================
2786 // function : ChangeZLayer
2787 // purpose  :
2788 // =======================================================================
2789 void Visual3d_View::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
2790                                   const Standard_Integer theLayerId)
2791 {
2792   myGraphicDriver->ChangeZLayer (*(theStructure->CStructure()), MyCView, theLayerId);
2793 }
2794
2795 // =======================================================================
2796 // function : Print
2797 // purpose  :
2798 // =======================================================================
2799 Standard_Boolean Visual3d_View::Print (const Aspect_Handle    thePrintDC, 
2800                                        const Standard_Boolean theToShowBackground,
2801                                        const Standard_CString theFilename,
2802                                        const Aspect_PrintAlgo thePrintAlgorithm,
2803                                        const Standard_Real    theScaleFactor) const
2804 {
2805   return Print (myViewManager->UnderLayer(),
2806                 myViewManager->OverLayer(),
2807                 thePrintDC, theToShowBackground,
2808                 theFilename, thePrintAlgorithm, 
2809                 theScaleFactor);
2810 }
2811
2812 // =======================================================================
2813 // function : Print
2814 // purpose  :
2815 // =======================================================================
2816 Standard_Boolean Visual3d_View::Print (const Handle(Visual3d_Layer)& theUnderLayer,
2817                                        const Handle(Visual3d_Layer)& theOverLayer,
2818                                        const Aspect_Handle           thePrintDC,
2819                                        const Standard_Boolean        theToShowBackground,
2820                                        const Standard_CString        theFilename,
2821                                        const Aspect_PrintAlgo        thePrintAlgorithm,
2822                                        const Standard_Real           theScaleFactor) const
2823 {
2824   if (IsDeleted()
2825   || !IsDefined()
2826   || !IsActive()
2827   || !MyWindow->IsMapped())
2828   {
2829     return Standard_False;
2830   }
2831
2832   Aspect_CLayer2d anOverCLayer;
2833   Aspect_CLayer2d anUnderCLayer;
2834   anOverCLayer.ptrLayer = anUnderCLayer.ptrLayer = NULL;
2835   if (!theOverLayer.IsNull())  anOverCLayer  = theOverLayer->CLayer();
2836   if (!theUnderLayer.IsNull()) anUnderCLayer = theUnderLayer->CLayer();
2837   return myGraphicDriver->Print (MyCView, anUnderCLayer, anOverCLayer,
2838                                  thePrintDC, theToShowBackground, theFilename,
2839                                  thePrintAlgorithm, theScaleFactor);
2840 }