0024001: Stereographic rendering support
[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
7 // under the terms of the GNU Lesser General Public 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 void Visual3d_View::MinMaxValues (Standard_Real& XMin, Standard_Real& YMin, Standard_Real& ZMin, Standard_Real& XMax, Standard_Real& YMax, Standard_Real& ZMax) const {
1758
1759         MinMaxValues
1760         (MyDisplayedStructure, XMin, YMin, ZMin, XMax, YMax, ZMax);
1761
1762 }
1763
1764 void Visual3d_View::MinMaxValues (const Graphic3d_MapOfStructure& ASet, Standard_Real& XMin, Standard_Real& YMin, Standard_Real& ZMin, Standard_Real& XMax, Standard_Real& YMax, Standard_Real& ZMax) const {
1765
1766   if (ASet.IsEmpty ()) {
1767     XMin = RealFirst ();
1768     YMin = RealFirst ();
1769     ZMin = RealFirst ();
1770
1771     XMax = RealLast ();
1772     YMax = RealLast ();
1773     ZMax = RealLast ();
1774   }
1775   else {
1776
1777   Standard_Real Xm, Ym, Zm, XM, YM, ZM;
1778   Graphic3d_MapIteratorOfMapOfStructure Iterator (ASet);
1779
1780   XMin = RealLast ();
1781   YMin = RealLast ();
1782   ZMin = RealLast ();
1783
1784   XMax = RealFirst ();
1785   YMax = RealFirst ();
1786   ZMax = RealFirst ();
1787
1788   for ( Iterator.Initialize (ASet);
1789         Iterator.More ();
1790         Iterator.Next ()) {
1791
1792       if ((Iterator.Key ())->IsInfinite ()){
1793         //XMin, YMin .... ZMax are initialized by means of infinite line data
1794         (Iterator.Key ())->MinMaxValues (Xm, Ym, Zm, XM, YM, ZM);
1795         if ( Xm != RealFirst() && Xm < XMin )
1796           XMin = Xm ;
1797         if ( Ym != RealFirst() && Ym < YMin )
1798           YMin = Ym ;
1799         if ( Zm != RealFirst() && Zm < ZMin )
1800           ZMin = Zm ;
1801         if ( XM != RealLast()  && XM > XMax )
1802           XMax = XM ;
1803         if ( YM != RealLast()  && YM > YMax )
1804           YMax = YM ;
1805         if ( ZM != RealLast()  && ZM > ZMax )
1806           ZMax = ZM ;
1807       }
1808       // Only non-empty and non-infinite structures
1809       // are taken into account for calculation of MinMax
1810       if (! (Iterator.Key ())->IsInfinite () &&
1811           ! (Iterator.Key ())->IsEmpty ()) {
1812             (Iterator.Key ())->MinMaxValues(Xm, Ym, Zm, XM, YM, ZM);
1813           /* ABD 29/10/04  Transform Persistence of Presentation( pan, zoom, rotate ) */
1814           //"FitAll" operation ignores object with transform persitence parameter
1815           if( (Iterator.Key ())->TransformPersistenceMode() == Graphic3d_TMF_None )
1816           {
1817             if (Xm < XMin) XMin = Xm;
1818             if (Ym < YMin) YMin = Ym;
1819             if (Zm < ZMin) ZMin = Zm;
1820             if (XM > XMax) XMax = XM;
1821             if (YM > YMax) YMax = YM;
1822             if (ZM > ZMax) ZMax = ZM;
1823           }
1824         }
1825     }
1826
1827     // The following cases are relevant
1828     // For exemple if all structures are empty or infinite
1829     if (XMax < XMin) { Xm = XMin; XMin = XMax; XMax = Xm; }
1830     if (YMax < YMin) { Ym = YMin; YMin = YMax; YMax = Ym; }
1831     if (ZMax < ZMin) { Zm = ZMin; ZMin = ZMax; ZMax = Zm; }
1832   }
1833 }
1834
1835 void Visual3d_View::MinMaxValues (Standard_Real& XMin, Standard_Real& YMin, Standard_Real& XMax, Standard_Real& YMax) {
1836
1837         MinMaxValues (MyDisplayedStructure, XMin, YMin, XMax, YMax);
1838
1839 }
1840
1841 void Visual3d_View::MinMaxValues (const Graphic3d_MapOfStructure& ASet, Standard_Real& XMin, Standard_Real& YMin, Standard_Real& XMax, Standard_Real& YMax) {
1842
1843 Standard_Real Xm, Ym, Zm, XM, YM, ZM;
1844 Standard_Real Xp, Yp, Zp;
1845
1846         MinMaxValues (ASet, Xm, Ym, Zm, XM, YM, ZM);
1847
1848         Projects (Xm, Ym, Zm, Xp, Yp, Zp);
1849         XMin    = Xp;
1850         YMin    = Yp;
1851
1852         Projects (XM, YM, ZM, Xp, Yp, Zp);
1853         XMax    = Xp;
1854         YMax    = Yp;
1855
1856         if (XMax < XMin) { Xp = XMax; XMax = XMin; XMin = Xp; }
1857         if (YMax < YMin) { Yp = YMax; YMax = YMin; YMin = Yp; }
1858 }
1859
1860
1861 Standard_Integer Visual3d_View::NumberOfDisplayedStructures () const {
1862
1863 Standard_Integer Result = MyDisplayedStructure.Extent ();
1864
1865         return (Result);
1866
1867 }
1868
1869 void Visual3d_View::Projects (const Standard_Real AX,
1870                               const Standard_Real AY,
1871                               const Standard_Real AZ,
1872                               Standard_Real& APX,
1873                               Standard_Real& APY,
1874                               Standard_Real& APZ)
1875 {
1876   Handle(Graphic3d_Camera) aCamera = MyCView.Context.Camera;
1877
1878   Standard_Real aUmin, aVMin, aUMax, aVMax;  
1879   Standard_Real aNear, aFar;
1880   aCamera->WindowLimit (aUmin, aVMin, aUMax, aVMax);
1881
1882   aNear = aCamera->ZNear();
1883   aFar = aCamera->ZFar();
1884
1885   gp_Pnt aPoint (AX, AY, AZ);
1886   aPoint = aCamera->Project (aPoint);
1887
1888   APX = (aPoint.X() + 1) * 0.5 * (aUMax - aUmin) + aUmin;
1889   APY = (aPoint.Y() + 1) * 0.5 * (aVMax - aVMin) + aVMin;
1890   APZ = aPoint.Z() * (aFar - aNear) + aNear;
1891 }
1892
1893 Standard_Integer Visual3d_View::Identification () const {
1894
1895         return (Standard_Integer (MyCView.ViewId));
1896
1897 }
1898
1899
1900 Standard_Boolean Visual3d_View::ZBufferIsActivated () const {
1901
1902         if (IsDeleted ()) return (Standard_False);
1903
1904         if ((! IsDefined ()) || (! IsActive ())) return (Standard_False);
1905
1906 // -1 => not forced by the programmer => depends on the type of visualisation
1907 //  0 ou 1 => forced by the programmer
1908
1909         if (MyCView.Context.ZBufferActivity == -1)
1910                 if (MyContext.Visualization () == Visual3d_TOV_SHADING)
1911                         return (Standard_True);
1912                 else
1913                         return (Standard_False);
1914         else
1915                 if (MyCView.Context.ZBufferActivity)
1916                         return (Standard_True);
1917                 else
1918                         return (Standard_False);
1919
1920 }
1921
1922 void Visual3d_View::SetTransparency (const Standard_Boolean AnActivity) {
1923
1924         if (IsDeleted ()) return;
1925
1926         if ((! IsDefined ()) || (! IsActive ())) return;
1927
1928         MyGraphicDriver->Transparency (MyCView, AnActivity);
1929
1930 }
1931
1932 void Visual3d_View::SetZBufferActivity (const Standard_Integer AnActivity) {
1933
1934         if (MyCView.Context.ZBufferActivity == AnActivity) return;
1935
1936         if (IsDeleted ()) return;
1937
1938         if ((! IsDefined ()) || (! IsActive ())) return;
1939
1940         MyCView.Context.ZBufferActivity = AnActivity;
1941         MyGraphicDriver->SetVisualisation (MyCView);
1942
1943 }
1944
1945 void Visual3d_View::UpdateView () {
1946
1947         MyCView.Context.Aliasing        = (MyContext.AliasingIsOn () ? 1:0);
1948         MyCView.Context.BackZClipping   =
1949                 (MyContext.BackZClippingIsOn () ? 1:0);
1950         MyCView.Context.FrontZClipping  =
1951                 (MyContext.FrontZClippingIsOn() ? 1:0);
1952         MyCView.Context.DepthCueing     =
1953                 (MyContext.DepthCueingIsOn () ? 1:0);
1954
1955         MyCView.Context.ZClipFrontPlane =
1956                                 float (MyContext.ZClippingFrontPlane ());
1957         MyCView.Context.ZClipBackPlane          =
1958                                 float (MyContext.ZClippingBackPlane ());
1959         MyCView.Context.DepthFrontPlane =
1960                                 float (MyContext.DepthCueingFrontPlane ());
1961         MyCView.Context.DepthBackPlane          =
1962                                 float (MyContext.DepthCueingBackPlane ());
1963
1964         MyCView.Context.Model           = int (MyContext.Model ());
1965         MyCView.Context.Visualization   = int (MyContext.Visualization ());
1966
1967         MyCView.Context.TextureEnv    = MyContext.TextureEnv();
1968         MyCView.Context.SurfaceDetail = MyContext.SurfaceDetail();
1969
1970 }
1971
1972 void Visual3d_View::Compute () {
1973
1974 Standard_Integer i;
1975 Standard_Integer Length = MyCOMPUTEDSequence.Length ();
1976         for (i=1; i<=Length; i++)
1977             (MyCOMPUTEDSequence.Value (i))->SetHLRValidation (Standard_False);
1978
1979         if (!ComputedMode())
1980         {
1981           return;
1982         }
1983
1984         /*
1985          * Force HLRValidation to False on all structures
1986          * calculated in the view.
1987          */
1988 #ifdef TRACE_LENGTH
1989         if (MyTOCOMPUTESequence.Length () != MyCOMPUTEDSequence.Length ()) {
1990                 cout << "In Visual3d_View::Compute, ";
1991                 cout << "TOCOMPUTE " << MyTOCOMPUTESequence.Length ()
1992                      << " != COMPUTED " << MyCOMPUTEDSequence.Length ()
1993                      << "\n" << flush;
1994         }
1995 #endif
1996
1997         /*
1998          * Change of orientation or of projection type =>
1999          * Remove structures that were calculated for the
2000          * previous orientation.
2001          * Recalculation of new structures.
2002          */
2003 Graphic3d_MapIteratorOfMapOfStructure S1Iterator (MyDisplayedStructure);
2004 Visual3d_TypeOfAnswer Answer;
2005
2006         i       = MyDisplayedStructure.Extent ();
2007
2008 Graphic3d_SequenceOfStructure FooSequence;
2009
2010         while (S1Iterator.More ()) {
2011                 Answer  = AcceptDisplay (S1Iterator.Key ());
2012                 // If the structure was calculated, it is recalculated.
2013                 if (Answer == Visual3d_TOA_COMPUTE) {
2014 #ifdef TRACE
2015                         cout << "AcceptDisplay ("
2016                              << (S1Iterator.Key ())->Identification ()
2017                              << ") == Visual3d_TOA_COMPUTE;\n";
2018                         cout << flush;
2019 #endif
2020                         //Display (S1Iterator.Key (), Aspect_TOU_WAIT);
2021                         FooSequence.Append (S1Iterator.Key ());
2022                 }
2023
2024                 // S1Iterator.Next () is located on the next structure
2025                 S1Iterator.Next ();
2026         }
2027
2028         Length  = FooSequence.Length ();
2029         // All stored structures are displayed
2030         for (i=1; i<=Length; i++)
2031                 Display (FooSequence.Value (i), Aspect_TOU_WAIT);
2032         if (Length != 0) FooSequence.Clear ();
2033
2034 }
2035
2036 void Visual3d_View::ReCompute (const Handle(Graphic3d_Structure)& AStructure) {
2037         if (!ComputedMode()) return;
2038
2039         if (IsDeleted ()) return;
2040
2041         if ((! IsDefined ()) || (! IsActive ())) return;
2042
2043         if (! MyWindow->IsMapped ()) return;
2044
2045         if (! AStructure->IsDisplayed ()) return;
2046
2047 Visual3d_TypeOfAnswer Answer;
2048
2049         Answer  = AcceptDisplay (AStructure);
2050
2051         if (Answer == Visual3d_TOA_COMPUTE) {
2052 Standard_Integer Index = IsComputed (AStructure);
2053                 if (Index == 0) {
2054 #ifdef TRACE_COMP
2055         cout << "Structure " << AStructure->Identification ()
2056              << " is not calculated in the view "
2057              << Identification () << "\n";
2058         cout << flush;
2059 #endif
2060                 }
2061                 else {
2062                   Standard_Integer OldStructId, NewStructId;
2063                         OldStructId =
2064                         MyCOMPUTEDSequence.Value (Index)->Identification ();
2065 #ifdef TRACE_COMP
2066         Standard_Integer StructId = AStructure->Identification ();
2067         cout << "Structure " << StructId
2068              << " calculated, in the view "
2069              << Identification () << ", by the structure "
2070              << OldStructId << " is recalculated.\n";
2071         cout << flush;
2072 #endif
2073
2074         // Compute + Validation
2075 #ifdef OLD
2076 Handle(Graphic3d_Structure) TheStructure = AStructure->Compute (this);
2077 #else
2078 Handle(Graphic3d_Structure) TheStructure;
2079 TColStd_Array2OfReal ATrsf (0, 3, 0, 3);
2080         AStructure->Transform (ATrsf);
2081         if (Index != 0) {
2082 TColStd_Array2OfReal Ident (0, 3, 0, 3);
2083 Standard_Integer ii, jj;
2084         for (ii=0; ii<=3; ii++)
2085             for (jj=0; jj<=3; jj++)
2086                 Ident (ii, jj) = (ii == jj ? 1.0 : 0.0);
2087             TheStructure = MyCOMPUTEDSequence.Value (Index);
2088             TheStructure->SetTransform (Ident, Graphic3d_TOC_REPLACE);
2089             if (AStructure->IsTransformed ()) {
2090                 AStructure->Compute (this, ATrsf, TheStructure);
2091             }
2092             else {
2093                 AStructure->Compute (this, TheStructure);
2094             }
2095         }
2096         else {
2097             if (AStructure->IsTransformed ()) {
2098                 TheStructure = AStructure->Compute (this, ATrsf);
2099             }
2100             else {
2101                 TheStructure = AStructure->Compute (this);
2102             }
2103         }
2104 #endif
2105         TheStructure->SetHLRValidation (Standard_True);
2106
2107 // Return type of visualisation of the view
2108 Visual3d_TypeOfVisualization ViewType = MyContext.Visualization ();
2109
2110 // Of which type will be the computed ?
2111 Standard_Boolean ComputeWireframe = ((ViewType == Visual3d_TOV_WIREFRAME) &&
2112         (AStructure->ComputeVisual () != Graphic3d_TOS_SHADING));
2113
2114 Standard_Boolean ComputeShading = ((ViewType == Visual3d_TOV_SHADING) &&
2115         (AStructure->ComputeVisual () != Graphic3d_TOS_WIREFRAME));
2116
2117                         if (ComputeWireframe)
2118                             TheStructure->SetVisual (Graphic3d_TOS_WIREFRAME);
2119                         if (ComputeShading)
2120                             TheStructure->SetVisual (Graphic3d_TOS_SHADING);
2121
2122                         if (AStructure->IsHighlighted ()) {
2123                             TheStructure->SetHighlightColor
2124                                 (AStructure->HighlightColor ());
2125                             TheStructure->GraphicHighlight (Aspect_TOHM_COLOR);
2126                         }
2127
2128                         // Ot is ranged
2129                         // Find structure <AStructure>
2130                         // in the sequence of structures to be calculated.
2131                         NewStructId = TheStructure->Identification ();
2132
2133                         // The previous calculation is removed and the new one is dislayed
2134                         MyGraphicDriver->EraseStructure (
2135                                 MyCView,
2136                                 *(Graphic3d_CStructure *)MyCOMPUTEDSequence.Value (Index)->CStructure ());
2137                         MyGraphicDriver->DisplayStructure (
2138                                 MyCView,
2139                                 *(Graphic3d_CStructure *)TheStructure->CStructure (),
2140                                 int (AStructure->DisplayPriority ())
2141                         );
2142
2143 #ifdef TRACE_LENGTH
2144         if (MyTOCOMPUTESequence.Length () != MyCOMPUTEDSequence.Length ()) {
2145                 cout << "In Visual3d_View::ReCompute, ";
2146                 cout << "TOCOMPUTE " << MyTOCOMPUTESequence.Length ()
2147                      << " != COMPUTED " << MyCOMPUTEDSequence.Length ()
2148                      << "\n" << flush;
2149         }
2150 #endif
2151
2152                           // hlhsr and the new associated compute are added
2153                           MyTOCOMPUTESequence.Append (AStructure);
2154                           MyCOMPUTEDSequence.Append (TheStructure);
2155
2156 #ifdef TRACE_LENGTH
2157         if (MyTOCOMPUTESequence.Length () != MyCOMPUTEDSequence.Length ())
2158                 cout << "\tTOCOMPUTE " << MyTOCOMPUTESequence.Length ()
2159                      << " != COMPUTED " << MyCOMPUTEDSequence.Length ()
2160                      << "\n" << flush;
2161 #endif
2162
2163                           // hlhsr and the new associated compute are removed
2164
2165                           MyTOCOMPUTESequence.Remove (Index);
2166                           MyCOMPUTEDSequence.Remove (Index);
2167
2168 #ifdef TRACE_LENGTH
2169         if (MyTOCOMPUTESequence.Length () != MyCOMPUTEDSequence.Length ())
2170                 cout << "\tTOCOMPUTE " << MyTOCOMPUTESequence.Length ()
2171                      << " != COMPUTED " << MyCOMPUTEDSequence.Length ()
2172                      << "\n" << flush;
2173 #endif
2174                 }
2175         }
2176
2177 }
2178
2179 const Handle(Graphic3d_GraphicDriver)& Visual3d_View::GraphicDriver () const {
2180
2181         return MyGraphicDriver;
2182
2183 }
2184
2185 void Visual3d_View::Plot (const Handle(Graphic3d_Plotter)& thePlotter) const
2186 {
2187   for (Graphic3d_MapIteratorOfMapOfStructure S1Iterator (MyDisplayedStructure); S1Iterator.More(); S1Iterator.Next())
2188   {
2189     Standard_Integer Index = IsComputed (S1Iterator.Key ());
2190     // displayed structure is plotted as if it was not calculated
2191     if (Index == 0)
2192       (S1Iterator.Key ())->Plot (thePlotter);
2193     else
2194       (MyCOMPUTEDSequence.Value (Index))->Plot (thePlotter);
2195   }
2196 }
2197
2198 Standard_Integer Visual3d_View::HaveTheSameOwner (const Handle(Graphic3d_Structure)& AStructure) const {
2199
2200 Standard_Integer Result = 0;
2201 Standard_Integer Length = MyTOCOMPUTESequence.Length ();
2202
2203         // Find in the sequence of already calculated structures
2204         // 1/ Structure with the same Owner as <AStructure>
2205         // 2/ Which is not <AStructure>
2206         // 3/ COMPUTED which of is valid
2207         for (Standard_Integer i=1; i<=Length && Result==0; i++)
2208            if ((MyTOCOMPUTESequence.Value (i)->Owner () == AStructure->Owner ())
2209             && (MyTOCOMPUTESequence.Value (i)->Identification () !=
2210                                                 AStructure->Identification ())
2211             && (MyCOMPUTEDSequence.Value (i)->HLRValidation ())) Result   = i;
2212
2213         return (Result);
2214
2215 }
2216
2217 Standard_Address Visual3d_View::CView () const {
2218
2219         return Standard_Address (&MyCView);
2220
2221 }
2222
2223
2224 // Triedron methods : the Triedron is a non-zoomable object.
2225
2226 void Visual3d_View::ZBufferTriedronSetup(const Quantity_NameOfColor XColor,
2227                                     const Quantity_NameOfColor YColor,
2228                                     const Quantity_NameOfColor ZColor,
2229                                     const Standard_Real        SizeRatio,
2230                                     const Standard_Real        AxisDiametr,
2231                                     const Standard_Integer     NbFacettes) {
2232   MyGraphicDriver->ZBufferTriedronSetup(XColor, YColor, ZColor,
2233                                         SizeRatio, AxisDiametr, NbFacettes);
2234 }
2235
2236
2237
2238 void Visual3d_View::TriedronDisplay (const Aspect_TypeOfTriedronPosition APosition,
2239  const Quantity_NameOfColor AColor, const Standard_Real AScale, const Standard_Boolean AsWireframe ) {
2240
2241         MyGraphicDriver->TriedronDisplay (MyCView,APosition, AColor, AScale, AsWireframe);
2242
2243 }
2244
2245 void Visual3d_View::TriedronErase () {
2246
2247         MyGraphicDriver->TriedronErase (MyCView);
2248
2249
2250 }
2251
2252 void Visual3d_View::TriedronEcho (const Aspect_TypeOfTriedronEcho AType ) {
2253
2254         MyGraphicDriver->TriedronEcho (MyCView,AType);
2255
2256 }
2257
2258 static Standard_Boolean checkFloat(const Standard_Real value)
2259 {
2260     return value > -FLT_MAX && value < FLT_MAX;
2261 }
2262
2263 static void SetMinMaxValuesCallback(Visual3d_View* theView)
2264 {
2265     Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
2266     theView->MinMaxValues(xmin, ymin, zmin, xmax, ymax, zmax);
2267
2268     if (checkFloat(xmin) && checkFloat(ymin) && checkFloat(zmin) &&
2269         checkFloat(xmax) && checkFloat(ymax) && checkFloat(zmax))
2270     {
2271         Handle(Graphic3d_GraphicDriver) driver = Handle(Graphic3d_GraphicDriver)::DownCast(theView->GraphicDriver());
2272         driver->GraduatedTrihedronMinMaxValues((Standard_ShortReal)xmin, (Standard_ShortReal)ymin, (Standard_ShortReal)zmin,
2273                                                (Standard_ShortReal)xmax, (Standard_ShortReal)ymax, (Standard_ShortReal)zmax);
2274     }
2275 }
2276
2277 Standard_Boolean Visual3d_View::GetGraduatedTrihedron
2278     (/* Names of axes */
2279      TCollection_ExtendedString& xname,
2280      TCollection_ExtendedString& yname,
2281      TCollection_ExtendedString& zname,
2282      /* Draw names */
2283      Standard_Boolean& xdrawname,
2284      Standard_Boolean& ydrawname,
2285      Standard_Boolean& zdrawname,
2286      /* Draw values */
2287      Standard_Boolean& xdrawvalues,
2288      Standard_Boolean& ydrawvalues,
2289      Standard_Boolean& zdrawvalues,
2290      /* Draw grid */
2291      Standard_Boolean& drawgrid,
2292      /* Draw axes */
2293      Standard_Boolean& drawaxes,
2294      /* Number of splits along axes */
2295      Standard_Integer& nbx,
2296      Standard_Integer& nby,
2297      Standard_Integer& nbz,
2298      /* Offset for drawing values */
2299      Standard_Integer& xoffset,
2300      Standard_Integer& yoffset,
2301      Standard_Integer& zoffset,
2302      /* Offset for drawing names of axes */
2303      Standard_Integer& xaxisoffset,
2304      Standard_Integer& yaxisoffset,
2305      Standard_Integer& zaxisoffset,
2306      /* Draw tickmarks */
2307      Standard_Boolean& xdrawtickmarks,
2308      Standard_Boolean& ydrawtickmarks,
2309      Standard_Boolean& zdrawtickmarks,
2310      /* Length of tickmarks */
2311      Standard_Integer& xtickmarklength,
2312      Standard_Integer& ytickmarklength,
2313      Standard_Integer& ztickmarklength,
2314      /* Grid color */
2315      Quantity_Color& gridcolor,
2316      /* Colors of axis names */
2317      Quantity_Color& xnamecolor,
2318      Quantity_Color& ynamecolor,
2319      Quantity_Color& znamecolor,
2320      /* Colors of axis and values */
2321      Quantity_Color& xcolor,
2322      Quantity_Color& ycolor,
2323      Quantity_Color& zcolor,
2324      /* Name of font for names of axes */
2325      TCollection_AsciiString& fontOfNames,
2326      /* Style of names of axes */
2327      Font_FontAspect& styleOfNames,
2328      /* Size of names of axes */
2329      Standard_Integer& sizeOfNames,
2330      /* Name of font for values */
2331      TCollection_AsciiString& fontOfValues,
2332      /* Style of values */
2333      Font_FontAspect& styleOfValues,
2334      /* Size of values */
2335      Standard_Integer& sizeOfValues) const
2336 {
2337     if (!MyGTrihedron.ptrVisual3dView)
2338         return Standard_False;
2339
2340     /* Names of axes */
2341     xname = MyGTrihedron.xname;
2342     yname = MyGTrihedron.yname;
2343     zname = MyGTrihedron.zname;
2344     /* Draw names */
2345     xdrawname = MyGTrihedron.xdrawname;
2346     ydrawname = MyGTrihedron.ydrawname;
2347     zdrawname = MyGTrihedron.zdrawname;
2348     /* Draw values */
2349     xdrawvalues = MyGTrihedron.xdrawvalues;
2350     ydrawvalues = MyGTrihedron.ydrawvalues;
2351     zdrawvalues = MyGTrihedron.zdrawvalues;
2352     /* Draw grid */
2353     drawgrid = MyGTrihedron.drawgrid;
2354     /* Draw axes */
2355     drawaxes = MyGTrihedron.drawaxes;
2356     /* Number of splits along axes */
2357     nbx = MyGTrihedron.nbx;
2358     nby = MyGTrihedron.nby;
2359     nbz = MyGTrihedron.nbz;
2360     /* Offset for drawing values */
2361     xoffset = MyGTrihedron.xoffset;
2362     yoffset = MyGTrihedron.yoffset;
2363     zoffset = MyGTrihedron.zoffset;
2364     /* Offset for drawing names of axes */
2365     xaxisoffset = MyGTrihedron.xaxisoffset;
2366     yaxisoffset = MyGTrihedron.yaxisoffset;
2367     zaxisoffset = MyGTrihedron.zaxisoffset;
2368     /* Draw tickmarks */
2369     xdrawtickmarks = MyGTrihedron.xdrawtickmarks;
2370     ydrawtickmarks = MyGTrihedron.ydrawtickmarks;
2371     zdrawtickmarks = MyGTrihedron.zdrawtickmarks;
2372     /* Length of tickmarks */
2373     xtickmarklength = MyGTrihedron.xtickmarklength;
2374     ytickmarklength = MyGTrihedron.ytickmarklength;
2375     ztickmarklength = MyGTrihedron.ztickmarklength;
2376     /* Grid color */
2377     gridcolor = MyGTrihedron.gridcolor;
2378     /* Colors of axis names */
2379     xnamecolor = MyGTrihedron.xnamecolor;
2380     ynamecolor = MyGTrihedron.ynamecolor;
2381     znamecolor = MyGTrihedron.znamecolor;
2382     /* Colors of axis and values */
2383     xcolor = MyGTrihedron.xcolor;
2384     ycolor = MyGTrihedron.ycolor;
2385     zcolor = MyGTrihedron.zcolor;
2386     /* Name of font for names of axes */
2387     fontOfNames = MyGTrihedron.fontOfNames;
2388     /* Style of names of axes */
2389     styleOfNames = MyGTrihedron.styleOfNames;
2390     /* Size of names of axes */
2391     sizeOfNames = MyGTrihedron.sizeOfNames;
2392     /* Name of font for values */
2393     fontOfValues = MyGTrihedron.fontOfValues;
2394     /* Style of values */
2395     styleOfValues = MyGTrihedron.styleOfValues;
2396     /* Size of values */
2397     sizeOfValues = MyGTrihedron.sizeOfValues;
2398
2399     return Standard_True;
2400 }
2401
2402 void Visual3d_View::GraduatedTrihedronDisplay
2403     (/* Names of axes */
2404      const TCollection_ExtendedString &xname,
2405      const TCollection_ExtendedString &yname,
2406      const TCollection_ExtendedString &zname,
2407      /* Draw names */
2408      const Standard_Boolean xdrawname,
2409      const Standard_Boolean ydrawname,
2410      const Standard_Boolean zdrawname,
2411      /* Draw values */
2412      const Standard_Boolean xdrawvalues,
2413      const Standard_Boolean ydrawvalues,
2414      const Standard_Boolean zdrawvalues,
2415      /* Draw grid */
2416      const Standard_Boolean drawgrid,
2417      /* Draw axes */
2418      const Standard_Boolean drawaxes,
2419      /* Number of splits along axes */
2420      const Standard_Integer nbx,
2421      const Standard_Integer nby,
2422      const Standard_Integer nbz,
2423      /* Offset for drawing values */
2424      const Standard_Integer xoffset,
2425      const Standard_Integer yoffset,
2426      const Standard_Integer zoffset,
2427      /* Offset for drawing names of axes */
2428      const Standard_Integer xaxisoffset,
2429      const Standard_Integer yaxisoffset,
2430      const Standard_Integer zaxisoffset,
2431      /* Draw tickmarks */
2432      const Standard_Boolean xdrawtickmarks,
2433      const Standard_Boolean ydrawtickmarks,
2434      const Standard_Boolean zdrawtickmarks,
2435      /* Length of tickmarks */
2436      const Standard_Integer xtickmarklength,
2437      const Standard_Integer ytickmarklength,
2438      const Standard_Integer ztickmarklength,
2439      /* Grid color */
2440      const Quantity_Color& gridcolor,
2441      /* Colors of axis names */
2442      const Quantity_Color& xnamecolor,
2443      const Quantity_Color& ynamecolor,
2444      const Quantity_Color& znamecolor,
2445      /* Colors of axis and values */
2446      const Quantity_Color& xcolor,
2447      const Quantity_Color& ycolor,
2448      const Quantity_Color& zcolor,
2449      /* Name of font for names of axes */
2450      const TCollection_AsciiString &fontOfNames,
2451      /* Style of names of axes */
2452      const Font_FontAspect styleOfNames,
2453      /* Size of names of axes */
2454      const Standard_Integer sizeOfNames,
2455      /* Name of font for values */
2456      const TCollection_AsciiString &fontOfValues,
2457      /* Style of values */
2458      const Font_FontAspect styleOfValues,
2459      /* Size of values */
2460      const Standard_Integer sizeOfValues)
2461 {
2462     /* Names of axes */
2463     MyGTrihedron.xname = xname;
2464     MyGTrihedron.yname = yname;
2465     MyGTrihedron.zname = zname;
2466     /* Draw names */
2467     MyGTrihedron.xdrawname = xdrawname;
2468     MyGTrihedron.ydrawname = ydrawname;
2469     MyGTrihedron.zdrawname = zdrawname;
2470     /* Draw values */
2471     MyGTrihedron.xdrawvalues = xdrawvalues;
2472     MyGTrihedron.ydrawvalues = ydrawvalues;
2473     MyGTrihedron.zdrawvalues = zdrawvalues;
2474     /* Draw grid */
2475     MyGTrihedron.drawgrid = drawgrid;
2476     /* Draw axes */
2477     MyGTrihedron.drawaxes = drawaxes;
2478     /* Number of splits along axes */
2479     MyGTrihedron.nbx = nbx;
2480     MyGTrihedron.nby = nby;
2481     MyGTrihedron.nbz = nbz;
2482     /* Offset for drawing values */
2483     MyGTrihedron.xoffset = xoffset;
2484     MyGTrihedron.yoffset = yoffset;
2485     MyGTrihedron.zoffset = zoffset;
2486     /* Offset for drawing names of axes */
2487     MyGTrihedron.xaxisoffset = xaxisoffset;
2488     MyGTrihedron.yaxisoffset = yaxisoffset;
2489     MyGTrihedron.zaxisoffset = zaxisoffset;
2490     /* Draw tickmarks */
2491     MyGTrihedron.xdrawtickmarks = xdrawtickmarks;
2492     MyGTrihedron.ydrawtickmarks = ydrawtickmarks;
2493     MyGTrihedron.zdrawtickmarks = zdrawtickmarks;
2494     /* Length of tickmarks */
2495     MyGTrihedron.xtickmarklength = xtickmarklength;
2496     MyGTrihedron.ytickmarklength = ytickmarklength;
2497     MyGTrihedron.ztickmarklength = ztickmarklength;
2498     /* Grid color */
2499     MyGTrihedron.gridcolor = gridcolor;
2500     /* Colors of axis names */
2501     MyGTrihedron.xnamecolor = xnamecolor;
2502     MyGTrihedron.ynamecolor = ynamecolor;
2503     MyGTrihedron.znamecolor = znamecolor;
2504     /* Colors of axis and values */
2505     MyGTrihedron.xcolor = xcolor;
2506     MyGTrihedron.ycolor = ycolor;
2507     MyGTrihedron.zcolor = zcolor;
2508     /* Name of font for names of axes */
2509     MyGTrihedron.fontOfNames = fontOfNames;
2510     /* Style of names of axes */
2511     MyGTrihedron.styleOfNames = styleOfNames;
2512     /* Size of names of axes */
2513     MyGTrihedron.sizeOfNames = sizeOfNames;
2514     /* Name of font for values */
2515     MyGTrihedron.fontOfValues = fontOfValues;
2516     /* Style of values */
2517     MyGTrihedron.styleOfValues = styleOfValues;
2518     /* Size of values */
2519     MyGTrihedron.sizeOfValues = sizeOfValues;
2520
2521     MyGTrihedron.ptrVisual3dView = this;
2522     MyGTrihedron.cbCubicAxes = SetMinMaxValuesCallback;
2523     MyGraphicDriver->GraduatedTrihedronDisplay(MyCView,MyGTrihedron);
2524 }
2525
2526 void Visual3d_View::GraduatedTrihedronErase()
2527 {
2528     MyGTrihedron.ptrVisual3dView = NULL;
2529     MyGraphicDriver->GraduatedTrihedronErase(MyCView);
2530 }
2531
2532 const Handle(Visual3d_Layer)& Visual3d_View::UnderLayer () const {
2533
2534         return (MyViewManager->UnderLayer ());
2535
2536 }
2537
2538 const Handle(Visual3d_Layer)& Visual3d_View::OverLayer () const {
2539
2540         return (MyViewManager->OverLayer ());
2541
2542 }
2543
2544 Standard_Integer Visual3d_View::LightLimit() const {
2545
2546         Standard_Integer maxlight = MyGraphicDriver->InquireLightLimit ();
2547 //      printf(" $$$ Visual3d_View::LightLimit() is %d\n",maxlight);
2548         return maxlight;
2549 }
2550
2551 Standard_Integer Visual3d_View::PlaneLimit() const {
2552
2553         Standard_Integer maxplane = MyGraphicDriver->InquirePlaneLimit ();
2554 //      printf(" $$$ Visual3d_View::PlaneLimit() is %d\n",maxplane);
2555         return maxplane;
2556 }
2557
2558 Handle(Visual3d_ViewManager) Visual3d_View::ViewManager() const
2559 {
2560   return MyPtrViewManager;
2561 }
2562
2563 void Visual3d_View :: SetComputedMode ( const Standard_Boolean aMode )
2564 {
2565   if ((aMode &&  ComputedModeIsActive) ||
2566      (!aMode && !ComputedModeIsActive))
2567   {
2568     return;
2569   }
2570
2571  Graphic3d_MapIteratorOfMapOfStructure S1Iterator ( MyDisplayedStructure );
2572  Visual3d_TypeOfAnswer                 Answer;
2573  Standard_Integer                      StructId;
2574  Standard_Integer                      i = MyDisplayedStructure.Extent ();
2575  ComputedModeIsActive = aMode;
2576  if (!ComputedModeIsActive) {
2577
2578   while (  S1Iterator.More ()  ) {
2579
2580    Answer = AcceptDisplay (  S1Iterator.Key ()  );
2581
2582    if ( Answer == Visual3d_TOA_COMPUTE ) {
2583
2584     Standard_Integer Index = IsComputed (  S1Iterator.Key ()  );
2585
2586     if ( Index != 0 ) {
2587
2588      StructId = MyCOMPUTEDSequence.Value ( Index ) -> Identification ();
2589
2590      MyGraphicDriver -> EraseStructure (
2591                          MyCView,
2592                          *( Graphic3d_CStructure* )
2593                           MyCOMPUTEDSequence.Value ( Index ) -> CStructure ()
2594                         );
2595      MyGraphicDriver -> DisplayStructure (
2596                          MyCView,
2597                                          *( Graphic3d_CStructure* )
2598                           S1Iterator.Key () -> CStructure (),
2599                          int (  S1Iterator.Key () -> DisplayPriority ()  )
2600                                     );
2601     }  // end if ( Index != 0 ) . . .
2602
2603    }  // end if ( Answer . . . )
2604
2605    S1Iterator.Next ();
2606
2607   }  // end while
2608
2609  } else {
2610
2611   while (  S1Iterator.More ()  ) {
2612
2613    Answer = AcceptDisplay (  S1Iterator.Key ()  );
2614
2615    if ( Answer == Visual3d_TOA_COMPUTE ) {
2616
2617     Standard_Integer Index = IsComputed (  S1Iterator.Key ()  );
2618
2619     if ( Index != 0 ) {
2620
2621      StructId = MyCOMPUTEDSequence.Value ( Index ) -> Identification ();
2622
2623      MyGraphicDriver -> EraseStructure (
2624                          MyCView,
2625                          *( Graphic3d_CStructure* )
2626                           S1Iterator.Key () -> CStructure ()
2627                         );
2628      MyGraphicDriver -> DisplayStructure (
2629                          MyCView,
2630                                          *( Graphic3d_CStructure* )
2631                           MyCOMPUTEDSequence.Value ( Index ) -> CStructure (),
2632                                          int (  S1Iterator.Key () -> DisplayPriority ()  )
2633                         );
2634
2635      Display (  S1Iterator.Key (), Aspect_TOU_WAIT  );
2636
2637      if (  S1Iterator.Key () -> IsHighlighted ()  ) {
2638
2639       if (  !MyCOMPUTEDSequence.Value ( Index ) -> IsHighlighted ()  ) {
2640
2641        MyCOMPUTEDSequence.Value ( Index ) -> SetHighlightColor (
2642                                               S1Iterator.Key () ->
2643                                                HighlightColor ()
2644                                              );
2645        MyCOMPUTEDSequence.Value ( Index ) -> GraphicHighlight (
2646                                               Aspect_TOHM_COLOR
2647                                              );
2648       }  // end if
2649
2650      }  // end if
2651
2652     } else {
2653
2654      Handle( Graphic3d_Structure ) AStructure = S1Iterator.Key ();
2655 #ifdef OLD
2656      Handle( Graphic3d_Structure ) TheStructure = AStructure -> Compute ( this );
2657 #else
2658      Handle( Graphic3d_Structure ) TheStructure;
2659      TColStd_Array2OfReal          ATrsf ( 0, 3, 0, 3 );
2660
2661      AStructure -> Transform ( ATrsf );
2662
2663      if ( Index != 0 ) {
2664
2665       TColStd_Array2OfReal Ident ( 0, 3, 0, 3 );
2666       Standard_Integer     ii, jj;
2667
2668       for ( ii = 0; ii <= 3; ++ii )
2669
2670        for ( jj = 0; jj <= 3; ++jj  )
2671
2672         Ident ( ii, jj ) = ( ii == jj ? 1.0 : 0.0 );
2673
2674       TheStructure = MyCOMPUTEDSequence.Value ( Index );
2675
2676       TheStructure -> SetTransform ( Ident, Graphic3d_TOC_REPLACE );
2677
2678       if (  AStructure->IsTransformed ()  )
2679
2680        AStructure -> Compute ( this, ATrsf, TheStructure );
2681
2682       else
2683
2684        AStructure -> Compute ( this, TheStructure );
2685
2686      } else {
2687
2688       if ( AStructure -> IsTransformed ()  )
2689
2690        TheStructure = AStructure -> Compute ( this, ATrsf );
2691
2692       else
2693
2694        TheStructure = AStructure -> Compute ( this );
2695
2696      }  // end else
2697 #endif  // OLD
2698      TheStructure -> SetHLRValidation ( Standard_True );
2699
2700      Visual3d_TypeOfVisualization ViewType = MyContext.Visualization ();
2701
2702      Standard_Boolean ComputeWireframe =
2703       (  ( ViewType == Visual3d_TOV_WIREFRAME ) &&
2704              (  S1Iterator.Key () -> ComputeVisual () != Graphic3d_TOS_SHADING  )
2705       );
2706
2707      Standard_Boolean ComputeShading =
2708       (  ( ViewType == Visual3d_TOV_SHADING ) &&
2709              (  S1Iterator.Key () -> ComputeVisual () != Graphic3d_TOS_WIREFRAME  )
2710       );
2711
2712      if ( ComputeWireframe ) TheStructure -> SetVisual ( Graphic3d_TOS_WIREFRAME );
2713      if ( ComputeShading   ) TheStructure -> SetVisual ( Graphic3d_TOS_SHADING   );
2714
2715      if (  S1Iterator.Key () -> IsHighlighted ()  ) {
2716
2717       TheStructure -> SetHighlightColor (
2718                        S1Iterator.Key () -> HighlightColor ()
2719                       );
2720       TheStructure -> GraphicHighlight ( Aspect_TOHM_COLOR );
2721
2722      }  // end if
2723
2724      Standard_Integer Result = 0;
2725      Standard_Integer Length = MyTOCOMPUTESequence.Length ();
2726      StructId = S1Iterator.Key () -> Identification ();
2727
2728      for ( i = 1; i <= Length && Result == 0; ++i )
2729
2730       if (  MyTOCOMPUTESequence.Value ( i ) -> Identification () == StructId  )
2731
2732        Result = i;
2733
2734      if ( Result )
2735
2736       MyCOMPUTEDSequence.ChangeValue ( Result ) = TheStructure;
2737
2738      else {
2739
2740       MyTOCOMPUTESequence.Append (  S1Iterator.Key ()  );
2741       MyCOMPUTEDSequence.Append  ( TheStructure );
2742
2743      }  // end else
2744
2745      MyGraphicDriver -> EraseStructure (
2746                          MyCView,
2747                          *( Graphic3d_CStructure* )
2748                           S1Iterator.Key () -> CStructure ()
2749                         );
2750      MyGraphicDriver -> DisplayStructure (
2751                          MyCView,
2752                          *( Graphic3d_CStructure* )TheStructure -> CStructure (),
2753                          int (  S1Iterator.Key () -> DisplayPriority ()  )
2754                         );
2755     }  // end else
2756
2757    }  // end if
2758
2759    S1Iterator.Next ();
2760
2761   }  // end while
2762
2763   if (  MyViewManager -> UpdateMode () == Aspect_TOU_ASAP  ) Update ();
2764
2765  }  // end else
2766
2767 }  // end Visual3d_View :: SetComputedMode
2768
2769 Standard_Boolean Visual3d_View :: ComputedMode () const {
2770
2771  return ComputedModeIsActive;
2772
2773 }  // end Visual3d_View :: ComputedMode
2774
2775 void Visual3d_View :: SetBackFacingModel (
2776                        const Visual3d_TypeOfBackfacingModel aModel
2777                       ) {
2778  switch ( aModel ) {
2779
2780   default:
2781   case Visual3d_TOBM_AUTOMATIC:
2782    MyCView.Backfacing = 0;
2783    break;
2784
2785   case Visual3d_TOBM_FORCE:
2786    MyCView.Backfacing = 1;
2787    break;
2788
2789   case Visual3d_TOBM_DISABLE:
2790    MyCView.Backfacing = -1;
2791    break;
2792
2793  }  // end switch
2794
2795  MyGraphicDriver -> SetBackFacingModel ( MyCView );
2796
2797 }  // end Visual3d_View :: SetBackFacingModel
2798
2799 Visual3d_TypeOfBackfacingModel Visual3d_View :: BackFacingModel () const {
2800
2801  switch ( MyCView.Backfacing ) {
2802
2803   case 0:
2804    return Visual3d_TOBM_AUTOMATIC;
2805
2806   case 1:
2807    return Visual3d_TOBM_FORCE;
2808
2809  }  // end switch
2810
2811  return Visual3d_TOBM_DISABLE;
2812
2813 }  // end Visual3d_View :: BackFacingModel
2814
2815 void Visual3d_View::EnableDepthTest( const Standard_Boolean enable ) const
2816 {
2817   MyGraphicDriver->SetDepthTestEnabled( MyCView, enable );
2818 }
2819
2820 Standard_Boolean Visual3d_View::IsDepthTestEnabled() const
2821 {
2822   return MyGraphicDriver->IsDepthTestEnabled( MyCView );
2823 }
2824
2825 void Visual3d_View::ReadDepths(const Standard_Integer x,
2826                                const Standard_Integer y,
2827                                const Standard_Integer width,
2828                                const Standard_Integer height,
2829                                const Standard_Address buffer) const
2830 {
2831   MyGraphicDriver->ReadDepths( MyCView, x, y, width, height, buffer );
2832 }
2833
2834 Graphic3d_PtrFrameBuffer Visual3d_View::FBOCreate(const Standard_Integer theWidth,
2835                                                   const Standard_Integer theHeight)
2836 {
2837   return MyGraphicDriver->FBOCreate( MyCView, theWidth, theHeight );
2838 }
2839
2840 void Visual3d_View::FBORelease(Graphic3d_PtrFrameBuffer& theFBOPtr)
2841 {
2842   MyGraphicDriver->FBORelease( MyCView, theFBOPtr );
2843 }
2844
2845 void Visual3d_View::FBOGetDimensions(const Graphic3d_PtrFrameBuffer theFBOPtr,
2846                                      Standard_Integer& theWidth,    Standard_Integer& theHeight,
2847                                      Standard_Integer& theWidthMax, Standard_Integer& theHeightMax)
2848 {
2849   MyGraphicDriver->FBOGetDimensions( MyCView, theFBOPtr,
2850                                      theWidth, theHeight,
2851                                      theWidthMax, theHeightMax );
2852 }
2853
2854 void Visual3d_View::FBOChangeViewport(Graphic3d_PtrFrameBuffer& theFBOPtr,
2855                                       const Standard_Integer theWidth, const Standard_Integer theHeight)
2856 {
2857   MyGraphicDriver->FBOChangeViewport( MyCView, theFBOPtr,
2858                                      theWidth, theHeight );
2859 }
2860
2861 Standard_Boolean Visual3d_View::BufferDump (Image_PixMap&               theImage,
2862                                             const Graphic3d_BufferType& theBufferType)
2863 {
2864   return MyGraphicDriver->BufferDump (MyCView, theImage, theBufferType);
2865 }
2866
2867 void Visual3d_View::EnableGLLight( const Standard_Boolean enable ) const
2868 {
2869   MyGraphicDriver->SetGLLightEnabled( MyCView, enable );
2870 }
2871
2872
2873 Standard_Boolean Visual3d_View::IsGLLightEnabled() const
2874 {
2875   return MyGraphicDriver->IsGLLightEnabled( MyCView );
2876 }
2877
2878 Standard_Boolean Visual3d_View::Export (const Standard_CString       theFileName,
2879                                         const Graphic3d_ExportFormat theFormat,
2880                                         const Graphic3d_SortType     theSortType,
2881                                         const Standard_Real          thePrecision,
2882                                         const Standard_Address       theProgressBarFunc,
2883                                         const Standard_Address       theProgressObject) const
2884 {
2885   Handle(Visual3d_Layer) anUnderLayer = MyViewManager->UnderLayer();
2886   Handle(Visual3d_Layer) anOverLayer  = MyViewManager->OverLayer();
2887
2888   Aspect_CLayer2d anOverCLayer;
2889   Aspect_CLayer2d anUnderCLayer;
2890   anOverCLayer.ptrLayer = anUnderCLayer.ptrLayer = NULL;
2891
2892   if (!anOverLayer.IsNull())
2893     anOverCLayer = anOverLayer->CLayer();
2894   if (!anUnderLayer.IsNull())
2895     anUnderCLayer = anUnderLayer->CLayer();
2896
2897   Standard_Integer aWidth, aHeight;
2898   Window()->Size (aWidth, aHeight);
2899
2900   return MyGraphicDriver->Export (theFileName, theFormat, theSortType,
2901                                   aWidth, aHeight, MyCView, anUnderCLayer, anOverCLayer,
2902                                   thePrecision, theProgressBarFunc, theProgressObject);
2903 }
2904
2905 //=======================================================================
2906 //function : AddZLayer
2907 //purpose  :
2908 //=======================================================================
2909
2910 void Visual3d_View::AddZLayer (const Standard_Integer theLayerId)
2911 {
2912   MyGraphicDriver->AddZLayer (MyCView, theLayerId);
2913 }
2914
2915 //=======================================================================
2916 //function : RemoveZLayer
2917 //purpose  :
2918 //=======================================================================
2919
2920 void Visual3d_View::RemoveZLayer (const Standard_Integer theLayerId)
2921 {
2922   MyGraphicDriver->RemoveZLayer (MyCView, theLayerId);
2923 }
2924
2925 //=======================================================================
2926 //function : ChangeZLayer
2927 //purpose  :
2928 //=======================================================================
2929
2930 void Visual3d_View::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
2931                                   const Standard_Integer theLayerId)
2932 {
2933   MyGraphicDriver->ChangeZLayer (
2934     (*(Graphic3d_CStructure*)theStructure->CStructure()), MyCView, theLayerId);
2935 }