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