0023019: OSD_Chronometer fails to compile due to lack of clock_gettime() on Mac OS X
[occt.git] / src / Visual3d / Visual3d_ViewManager.cxx
1
2 /***********************************************************************
3
4      FONCTION :
5      ----------
6         Classe Visual3d_ViewManager.cxx :
7
8         Declaration of variables specific to visualisers
9
10      HISTORIQUE DES MODIFICATIONS   :
11      --------------------------------
12       Mars 1992 : NW,JPB,CAL ; Creation.
13       19-06-96  : FMN ; Suppression variables inutiles
14       04-02-97  : FMN ; Suppression de PSOutput, XWDOutput ...
15       06-05-97  : CAL ; Ajout du Clear sur les TOS_COMPUTED.
16       19-09-97  : CAL ; Remplacement de Window->Position par Window->Size;
17       24-10-97  : CAL ; Retrait de DownCast.
18       20-11-97  : CAL ; Disparition de la dependance avec math
19       01-12-97  : CAL ; Retrait du test IsActive sur l'Update et le Redraw
20       31-12-97  : CAL ; Disparition de MathGra 
21       16-01-98  : CAL ; Ajout du SetTransform sur une TOS_COMPUTED
22       11-03-98  : CAL ; Visual3d_ViewManager::Remove ()
23       20-05-98  : CAL ; Perfs. Connection entre structures COMPUTED.
24       10-06-98  : CAL ; Modification des signatures de xxProjectRaster.
25       10-06-98  : CAL ; Modification de la signature de ViewExists.
26       01-12-98  : CAL ; S4062. Ajout des layers.
27       02-12-98  : CAL ; Remove () ne detruit plus les vues.
28
29 ************************************************************************/
30
31 /*----------------------------------------------------------------------*/
32 /*
33  * Constants
34  */
35
36 #define NO_DOWNCAST
37 #define NO_DESTROY
38
39 /*----------------------------------------------------------------------*/
40 /*
41  * Includes
42  */
43
44 // for the class
45 #include <Visual3d_ViewManager.ixx>
46 #include <Visual3d_ViewManager.pxx>
47
48 #include <Standard_ErrorHandler.hxx>
49
50 #include <Aspect.hxx>
51 #include <Aspect_IdentDefinitionError.hxx>
52
53 #include <Graphic3d_GraphicDriver.hxx>
54 #include <Graphic3d_MapOfStructure.hxx>
55 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
56
57 #include <Visual3d_PickPath.hxx>
58 #include <Visual3d_SetIteratorOfSetOfView.hxx>
59
60 #ifndef WNT
61 # include <Xw_Window.hxx>
62 #else
63 # include <WNT_Window.hxx>
64 #endif  // WNT
65
66 //-Aliases
67
68 //-Global data definitions
69
70 //      -- les vues definies
71 //      MyDefinedView           :       SetOfView;
72
73 //      -- le generateur d'identificateurs de vues
74 //      MyViewGenId             :       GenId;
75
76 //-Constructors
77
78 Visual3d_ViewManager::Visual3d_ViewManager (const Handle(Aspect_GraphicDevice)& aDevice):
79 Graphic3d_StructureManager (aDevice),
80 MyDefinedView (),
81 MyViewGenId (View_IDMIN+((View_IDMIN+View_IDMAX)/(Visual3d_ViewManager::Limit ()))*(Visual3d_ViewManager::CurrentId ()-1),View_IDMIN+((View_IDMIN+View_IDMAX)/(Visual3d_ViewManager::Limit ()))*Visual3d_ViewManager::CurrentId ()-1),
82 MyZBufferAuto (Standard_False),
83 MyTransparency (Standard_False)
84 {
85   // default layer is always presented in display layer sequence
86   // it can not be removed
87   myLayerIds.Add (0);
88   myLayerSeq.Append (0);
89
90   Handle(Aspect_GraphicDriver) agd = aDevice->GraphicDriver ();
91
92   MyGraphicDriver = *(Handle(Graphic3d_GraphicDriver) *) &agd;
93 }
94
95 //-Destructors
96
97 void Visual3d_ViewManager::Destroy () {
98
99 #ifdef DESTROY
100         cout << "Visual3d_ViewManager::Destroy (" << MyId << ")\n" << flush;
101 #endif
102
103         Remove ();
104 }
105
106 //-Methods, in order
107
108 void Visual3d_ViewManager::Remove () {
109
110 #ifdef DESTROY
111         cout << "Visual3d_ViewManager::Remove (" << MyId << ")\n" << flush;
112 #endif
113
114         //
115         // Destroy all defined views
116         //
117
118 #ifdef DESTROY
119         cout << "The Manager " << MyId << " have " << Length << " defined views\n";
120         cout << flush;
121 #endif
122  
123         MyDefinedView.Clear ();
124
125 }
126
127 void Visual3d_ViewManager::ChangeDisplayPriority (const Handle(Graphic3d_Structure)& AStructure, const Standard_Integer OldPriority, const Standard_Integer NewPriority) {
128
129 #ifdef TRACE
130         cout << "Visual3d_ViewManager::ChangeDisplayPriority ("
131              << AStructure->Identification ()
132              << ", " << OldPriority << ", " << NewPriority << ")\n";
133         cout << flush;
134 #endif
135
136         //
137         // Change structure priority in all defined views
138         //
139         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
140  
141         while (MyIterator.More ()) {
142                 (MyIterator.Value ())->ChangeDisplayPriority
143                         (AStructure, OldPriority, NewPriority);
144
145                 // MyIterator.Next () is located on the next view
146                 MyIterator.Next ();
147         }
148
149 }
150
151 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure) { 
152
153   //Standard_Integer LengthD    = MyDisplayedStructure.Extent() ();
154
155   // Even if physically the structure cannot
156   // be displayed (pb of visualisation type)
157   // it has status Displayed.
158  
159   if (!MyDisplayedStructure.Contains(AStructure))
160     return;
161
162   //
163   // Recompute structure in all activated views
164   //
165   Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
166  
167   while (MyIterator.More ()) {
168     (MyIterator.Value ())->ReCompute (AStructure);
169
170     // MyIterator.Next () is located on the next view
171     MyIterator.Next ();
172   }
173   
174 }
175
176 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure, 
177                                       const Handle(Graphic3d_DataStructureManager)& AProjector) 
178
179
180   if (! AProjector->IsKind (STANDARD_TYPE (Visual3d_View))) return;
181
182 #ifdef DOWNCAST
183   Handle(Visual3d_View) theView = Handle(Visual3d_View)::DownCast (AProjector);
184 #else
185   Handle(Visual3d_View) theView = *(Handle(Visual3d_View) *) &AProjector;
186 #endif
187   Standard_Integer ViewId = theView->Identification ();
188
189   // Even if physically the structure cannot
190   // be displayed (pb of visualisation type)
191   // it has status Displayed.
192   if (!MyDisplayedStructure.Contains(AStructure))
193     return;
194         
195   //
196   // Recompute structure in all activated views
197   //
198   Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
199  
200   while (MyIterator.More ()) {
201     if ((MyIterator.Value ())->Identification () == ViewId)
202       theView->ReCompute (AStructure);
203
204     // MyIterator.Next () is located on the next view
205     MyIterator.Next ();
206   }
207
208 }
209
210 void Visual3d_ViewManager::Clear (const Handle(Graphic3d_Structure)& AStructure, const Standard_Boolean WithDestruction) {
211
212         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
213  
214         while (MyIterator.More ()) {
215                 (MyIterator.Value ())->Clear (AStructure, WithDestruction);
216
217                 // MyIterator.Next ()  is located on the next view
218                 MyIterator.Next ();
219         }
220
221 }
222
223 void Visual3d_ViewManager::Connect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter) {
224
225         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
226  
227         while (MyIterator.More ()) {
228                 (MyIterator.Value ())->Connect (AMother, ADaughter);
229
230                 // MyIterator.Next ()  is located on the next view
231                 MyIterator.Next ();
232         }
233
234 }
235
236 void Visual3d_ViewManager::Disconnect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter) {
237
238         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
239  
240         while (MyIterator.More ()) {
241                 (MyIterator.Value ())->Disconnect (AMother, ADaughter);
242
243                 // MyIterator.Next ()  is located on the next view
244                 MyIterator.Next ();
245         }
246
247 }
248
249 void Visual3d_ViewManager::Display (const Handle(Graphic3d_Structure)& AStructure) { 
250
251
252  // Even if physically the structure cannot
253   // be displayed (pb of visualisation type)
254   // it has status Displayed.
255
256   MyDisplayedStructure.Add(AStructure);
257
258         //
259         // Display structure in all activated views
260         //
261         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
262  
263         while (MyIterator.More ()) {
264                 (MyIterator.Value ())->Display (AStructure);
265
266                 // MyIterator.Next () is located on the next view
267                 MyIterator.Next ();
268         }
269
270 }
271
272 void Visual3d_ViewManager::Erase (const Handle(Graphic3d_Structure)& AStructure) {
273
274
275 // Even if physically the structure cannot
276   // be displayed (pb of visualisation type)
277   // it has status Displayed.
278
279  MyDisplayedStructure.Remove(AStructure);
280
281
282
283         //
284         // Erase structure in all defined views
285         //
286         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
287  
288         while (MyIterator.More ()) {
289                 (MyIterator.Value ())->Erase (AStructure);
290
291                 // MyIterator.Next () is located on the next view
292                 MyIterator.Next ();
293         }
294
295         MyHighlightedStructure.Remove (AStructure);
296         MyVisibleStructure.Remove (AStructure);
297         MyPickStructure.Remove (AStructure);
298
299 }
300
301 void Visual3d_ViewManager::Erase () {
302
303  Graphic3d_MapIteratorOfMapOfStructure it( MyDisplayedStructure);
304  
305  for (; it.More(); it.Next()) {
306    Handle(Graphic3d_Structure) SG = it.Key();
307    SG->Erase();
308  }
309
310 }
311
312 void Visual3d_ViewManager::Highlight (const Handle(Graphic3d_Structure)& AStructure, const Aspect_TypeOfHighlightMethod AMethod) {
313
314   MyHighlightedStructure.Add(AStructure);
315
316         //
317         // Highlight in all activated views
318         //
319         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
320  
321         while (MyIterator.More ()) {
322                 (MyIterator.Value ())->Highlight (AStructure, AMethod);
323
324                 // MyIterator.Next () is located on the next view
325                 MyIterator.Next ();
326         }
327
328 }
329
330 void Visual3d_ViewManager::SetTransform (const Handle(Graphic3d_Structure)& AStructure, const TColStd_Array2OfReal& ATrsf) {
331
332         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
333  
334         while (MyIterator.More ()) {
335                 (MyIterator.Value ())->SetTransform (AStructure, ATrsf);
336
337                 // MyIterator.Next () is located on the next view
338                 MyIterator.Next ();
339         }
340
341 }
342
343 void Visual3d_ViewManager::UnHighlight () {
344
345   Graphic3d_MapIteratorOfMapOfStructure it(MyHighlightedStructure);
346   
347   for (; it.More(); it.Next()) {
348     Handle(Graphic3d_Structure) SG = it.Key();
349     SG->UnHighlight ();
350   }
351
352
353 }
354
355 void Visual3d_ViewManager::UnHighlight (const Handle(Graphic3d_Structure)& AStructure) {
356
357   MyHighlightedStructure.Remove(AStructure);
358
359
360         //
361         // UnHighlight in all activated views
362         //
363         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
364  
365         while (MyIterator.More ()) {
366                 (MyIterator.Value ())->UnHighlight (AStructure);
367
368                 // MyIterator.Next () is located on the next view
369                 MyIterator.Next ();
370         }
371
372 }
373
374 void Visual3d_ViewManager::Redraw () const {
375
376 Standard_Integer MaxDx, MaxDy;
377 Standard_Integer Dx, Dy;
378         MaxDx = MaxDy = IntegerFirst ();
379
380         //
381         // Redraw all activated views
382         //
383         Standard_Integer j = MyDefinedView.Extent ();
384         if (j == 0) return;
385         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
386  
387         if (! MyUnderLayer.IsNull () || ! MyOverLayer.IsNull ()) {
388             while (MyIterator.More ()) {
389                 (MyIterator.Value ())->Window ()->Size (Dx, Dy);
390                 if (Dx > MaxDx) MaxDx = Dx;
391                 if (Dy > MaxDy) MaxDy = Dy;
392
393                 // MyIterator.Next () is located on the next view
394                 MyIterator.Next ();
395             }
396             if (! MyUnderLayer.IsNull ())
397                 MyUnderLayer->SetViewport (MaxDx, MaxDy);
398             if (! MyOverLayer.IsNull ())
399                 MyOverLayer->SetViewport (MaxDx, MaxDy);
400         }
401  
402         if (! MyUnderLayer.IsNull () || ! MyOverLayer.IsNull ())
403             MyIterator.Initialize (MyDefinedView);
404         while (MyIterator.More ()) {
405             (MyIterator.Value ())->Redraw (MyUnderLayer, MyOverLayer);
406
407             // MyIterator.Next () is located on the next view
408             MyIterator.Next ();
409         }
410
411 }
412
413 void Visual3d_ViewManager::Update () const {
414
415         //
416         // Update all activated views
417         //
418         Standard_Integer j = MyDefinedView.Extent ();
419         if (j == 0) return;
420         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
421  
422         while (MyIterator.More ()) {
423                 (MyIterator.Value ())->Update (MyUnderLayer, MyOverLayer);
424
425                 // MyIterator.Next () is located on the next view
426                 MyIterator.Next ();
427         }
428
429 }
430
431 Handle(Visual3d_HSetOfView) Visual3d_ViewManager::ActivatedView () const {
432
433 Handle (Visual3d_HSetOfView) SG = new Visual3d_HSetOfView ();
434
435 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
436
437         while (MyIterator.More ()) {
438                 if ((MyIterator.Value ())->IsActive ())
439                         SG->Add (MyIterator.Value ());
440
441                 // MyIterator.Next () is located on the next view
442                 MyIterator.Next ();
443         }
444
445         return (SG);
446
447 }
448
449 #ifdef IMPLEMENTED
450 Standard_Boolean Visual3d_ViewManager::ContainsComputedStructure () const {
451
452 Standard_Boolean Result = Standard_False;
453
454         //
455         // Check all activated views
456         //
457         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
458
459         Standard_Integer i      = MyDefinedView.Extent ();
460
461         while ((! Result) && (MyIterator.More ())) {
462                 if ((MyIterator.Value ())->IsActive ())
463                         Result  =
464                         (MyIterator.Value ())->ContainsComputedStructure ();
465
466                 // MyIterator.Next () is located on the next view
467                 MyIterator.Next ();
468         }
469
470         return Result;
471 }
472 #endif
473
474 Handle(Visual3d_HSetOfView) Visual3d_ViewManager::DefinedView () const {
475
476 Handle (Visual3d_HSetOfView) SG = new Visual3d_HSetOfView ();
477
478 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
479
480         while (MyIterator.More ()) {
481                 SG->Add (MyIterator.Value ());
482
483                 // MyIterator.Next () is located on the next view
484                 MyIterator.Next ();
485         }
486
487         return (SG);
488
489 }
490
491 void Visual3d_ViewManager::ConvertCoord (const Handle(Aspect_Window)& AWindow, const Graphic3d_Vertex& AVertex, Standard_Integer& AU, Standard_Integer& AV) const {
492
493 // Convert only if the data is correct
494 Standard_Boolean Exist;
495 Graphic3d_CView  TheCView;
496 //Graphic3d_Vertex Point;
497
498 TColStd_Array2OfReal Ori_Matrix (0,3,0,3);
499 TColStd_Array2OfReal Map_Matrix (0,3,0,3);
500
501 Standard_Integer Width, Height;
502 Standard_Real AX, AY, AZ;
503 Standard_Real Dx, Dy, Ratio;
504
505         Exist   = ViewExists (AWindow, TheCView);
506
507         if (! Exist) {
508                 AU = AV = IntegerLast ();
509         }
510         else {
511     // NKV - 11.02.08 - Use graphic driver functions
512     Standard_Boolean Result;
513
514     AVertex.Coord (AX, AY, AZ);
515
516     Result = MyGraphicDriver->ProjectRaster (TheCView, 
517       Standard_ShortReal (AX), Standard_ShortReal (AY), Standard_ShortReal (AZ),
518       AU, AV);
519
520     // the old code
521     if (!Result) {
522
523       Standard_Real PtX, PtY, PtZ, PtT;
524       Standard_Real APX, APY, APZ;
525       Standard_Real APT;
526
527       Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
528
529       Standard_Integer stop = 0;
530
531       while ((! stop) && (MyIterator.More ())) {
532         if (TheCView.ViewId ==
533             (MyIterator.Value ())->Identification ()) {
534           Ori_Matrix    =
535             (MyIterator.Value ())->MatrixOfOrientation ();
536           Map_Matrix    =
537             (MyIterator.Value ())->MatrixOfMapping ();
538           stop  = 1;
539         }
540
541         // MyIterator.Next () is located on the next view
542         MyIterator.Next ();
543       }
544
545
546       // WCS -> View Reference Coordinate Space
547       PtX       = Ori_Matrix (0, 0) * AX
548                 + Ori_Matrix (0, 1) * AY
549                 + Ori_Matrix (0, 2) * AZ
550                 + Ori_Matrix (0, 3);
551       PtY       = Ori_Matrix (1, 0) * AX
552                 + Ori_Matrix (1, 1) * AY
553                 + Ori_Matrix (1, 2) * AZ
554                 + Ori_Matrix (1, 3);
555       PtZ       = Ori_Matrix (2, 0) * AX
556                 + Ori_Matrix (2, 1) * AY
557                 + Ori_Matrix (2, 2) * AZ
558                 + Ori_Matrix (2, 3);
559       PtT       = Ori_Matrix (3, 0) * AX
560                 + Ori_Matrix (3, 1) * AY
561                 + Ori_Matrix (3, 2) * AZ
562                 + Ori_Matrix (3, 3);
563
564       // VRCS -> Normalized Projection Coordinate Space
565       APX       = Map_Matrix (0, 0) * PtX
566                 + Map_Matrix (0, 1) * PtY
567                 + Map_Matrix (0, 2) * PtZ
568                 + Map_Matrix (0, 3) * PtT;
569       APY       = Map_Matrix (1, 0) * PtX
570                 + Map_Matrix (1, 1) * PtY
571                 + Map_Matrix (1, 2) * PtZ
572                 + Map_Matrix (1, 3) * PtT;
573       APZ       = Map_Matrix (2, 0) * PtX
574                 + Map_Matrix (2, 1) * PtY
575                 + Map_Matrix (2, 2) * PtZ
576                 + Map_Matrix (2, 3) * PtT;
577       APT       = Map_Matrix (3, 0) * PtX
578                 + Map_Matrix (3, 1) * PtY
579                 + Map_Matrix (3, 2) * PtZ
580                 + Map_Matrix (3, 3) * PtT;
581
582       if (APT == 0. || stop == 0) {
583         AU = AV = IntegerLast ();
584       }
585       else {
586         APX /= APT;
587         APY /= APT;
588         APZ /= APT;
589
590         // NPCS -> Device Coordinate Space
591         AWindow->Size (Width, Height);
592         Dx      = Standard_Real (Width);
593         Dy      = Standard_Real (Height);
594         Ratio   = Dx / Dy;
595         if (Ratio >= 1.) {
596           AU = Standard_Integer (APX * Dx);
597           AV = Standard_Integer (Dy - APY * Dy * Ratio);
598         }
599         else {
600           AU = Standard_Integer (APX * Dx / Ratio);
601           AV = Standard_Integer (Dy - APY * Dy);
602         }
603       }
604     }
605   }
606
607 }
608
609 Graphic3d_Vertex Visual3d_ViewManager::ConvertCoord (const Handle(Aspect_Window)& AWindow, const Standard_Integer AU, const Standard_Integer AV) const {
610
611 // Convert only if the data is correct
612 Graphic3d_CView TheCView;
613 Graphic3d_Vertex Point;
614
615         if (! ViewExists (AWindow, TheCView))
616             Point.SetCoord (RealLast (), RealLast (), RealLast ());
617         else {
618 Standard_Integer Width, Height;
619 Standard_ShortReal x, y, z;
620 Standard_Boolean Result;
621
622             AWindow->Size (Width, Height);
623
624             Result      = MyGraphicDriver->UnProjectRaster (TheCView,
625                                 0, 0, Width, Height,
626                                 AU, AV, x, y, z);
627
628             // unproject is done by UnProjectRaster
629             if (Result) {
630                 Point.SetCoord
631                     (Standard_Real (x), Standard_Real (y), Standard_Real (z));
632             }
633             // unproject cannot be done by UnProjectRaster
634             // Code suspended since drivers Phigs and Pex are abandoned.
635             else {
636
637 Standard_Real NPCX, NPCY, NPCZ;
638 Standard_Real VRCX, VRCY, VRCZ, VRCT;
639 Standard_Real WCX, WCY, WCZ, WCT;
640
641 TColStd_Array2OfReal TOri_Matrix (0,3,0,3);
642 TColStd_Array2OfReal TMap_Matrix (0,3,0,3);
643 TColStd_Array2OfReal TOri_Matrix_Inv (0,3,0,3);
644 TColStd_Array2OfReal TMap_Matrix_Inv (0,3,0,3);
645
646 Standard_Real Dx, Dy, Ratio;
647 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
648 Standard_Integer j;
649
650 Standard_Integer stop = 0;
651
652 Standard_Boolean BResult;
653
654                 j = MyDefinedView.Extent ();
655
656                 while ((! stop) && (MyIterator.More ())) {
657                     if (TheCView.ViewId ==
658                         (MyIterator.Value ())->Identification ()) {
659                         TOri_Matrix     =
660                                 (MyIterator.Value ())->MatrixOfOrientation ();
661                         TMap_Matrix     =
662                                 (MyIterator.Value ())->MatrixOfMapping ();
663                         stop    = 1;
664                     }
665
666                     // MyIterator.Next () is located on the next view
667                     MyIterator.Next ();
668                 }
669
670                 // View Mapping Transformation and View Clip, inversion
671                 BResult = Aspect::Inverse (TMap_Matrix, TMap_Matrix_Inv);
672
673                 // View Orientation Transformation, inversion
674                 BResult = Aspect::Inverse (TOri_Matrix, TOri_Matrix_Inv);
675
676                 // (AU, AV) : Device Coordinate Space
677                 // DCS -> NPCS Normalized Projection Coordinate Space
678                 Dx      = Standard_Real (Width);
679                 Dy      = Standard_Real (Height);
680                 Ratio   = Dx / Dy;
681
682                 if (Ratio >= 1.) {
683                         NPCX    = Standard_Real (AU) / Dx;
684                         NPCY    = (Dy - Standard_Real (AV)) / Dx;
685                 }
686                 else {
687                         NPCX    = Standard_Real (AU) / Dy;
688                         NPCY    = (Dy - Standard_Real (AV)) / Dy;
689                 }
690                 NPCZ    = 0.0;
691
692                 // NPCS -> VRCS View Reference Coordinate Space
693                 // PtVRC = Map_Matrix_Inv.Multiplied (PtNPC);
694
695                 VRCX    = TMap_Matrix_Inv (0, 0) * NPCX
696                         + TMap_Matrix_Inv (0, 1) * NPCY
697                         + TMap_Matrix_Inv (0, 2) * NPCZ
698                         + TMap_Matrix_Inv (0, 3);
699                 VRCY    = TMap_Matrix_Inv (1, 0) * NPCX
700                         + TMap_Matrix_Inv (1, 1) * NPCY
701                         + TMap_Matrix_Inv (1, 2) * NPCZ
702                         + TMap_Matrix_Inv (1, 3);
703                 VRCZ    = TMap_Matrix_Inv (2, 0) * NPCX
704                         + TMap_Matrix_Inv (2, 1) * NPCY
705                         + TMap_Matrix_Inv (2, 2) * NPCZ
706                         + TMap_Matrix_Inv (2, 3);
707                 VRCT    = TMap_Matrix_Inv (3, 0) * NPCX
708                         + TMap_Matrix_Inv (3, 1) * NPCY
709                         + TMap_Matrix_Inv (3, 2) * NPCZ
710                         + TMap_Matrix_Inv (3, 3);
711
712                 // VRCS -> WCS World Coordinate Space
713                 // PtWC = Ori_Matrix_Inv.Multiplied (PtVRC);
714
715                 WCX     = TOri_Matrix_Inv (0, 0) * VRCX
716                         + TOri_Matrix_Inv (0, 1) * VRCY
717                         + TOri_Matrix_Inv (0, 2) * VRCZ
718                         + TOri_Matrix_Inv (0, 3) * VRCT;
719                 WCY     = TOri_Matrix_Inv (1, 0) * VRCX
720                         + TOri_Matrix_Inv (1, 1) * VRCY
721                         + TOri_Matrix_Inv (1, 2) * VRCZ
722                         + TOri_Matrix_Inv (1, 3) * VRCT;
723                 WCZ     = TOri_Matrix_Inv (2, 0) * VRCX
724                         + TOri_Matrix_Inv (2, 1) * VRCY
725                         + TOri_Matrix_Inv (2, 2) * VRCZ
726                         + TOri_Matrix_Inv (2, 3) * VRCT;
727                 WCT     = TOri_Matrix_Inv (3, 0) * VRCX
728                         + TOri_Matrix_Inv (3, 1) * VRCY
729                         + TOri_Matrix_Inv (3, 2) * VRCZ
730                         + TOri_Matrix_Inv (3, 3) * VRCT;
731
732                 if (WCT != 0.)
733                     Point.SetCoord (WCX/WCT, WCY/WCT, WCZ/WCT);
734                 else
735                     Point.SetCoord (RealLast (), RealLast (), RealLast ());
736             }
737         }
738
739         return (Point);
740
741 }
742
743 void Visual3d_ViewManager::ConvertCoordWithProj (const Handle(Aspect_Window)& AWindow, const Standard_Integer AU, const Standard_Integer AV, Graphic3d_Vertex& Point, Graphic3d_Vector& Proj) const {
744
745 // Conversion only if the data is correct
746 Graphic3d_CView TheCView;
747
748         if (! ViewExists (AWindow, TheCView)) {
749             Point.SetCoord (RealLast (), RealLast (), RealLast ());
750             Proj.SetCoord (0., 0., 0.);
751         }
752         else {
753 Standard_Integer Width, Height;
754 Standard_ShortReal x, y, z;
755 Standard_ShortReal dx, dy, dz;
756 Standard_Boolean Result;
757
758             AWindow->Size (Width, Height);
759
760             Result      = MyGraphicDriver->UnProjectRasterWithRay (TheCView,
761                                 0, 0, Width, Height,
762                                 AU, AV, x, y, z, dx, dy, dz);
763
764             // unproject is done by UnProjectRaster
765             if (Result) {
766                 Point.SetCoord
767                     (Standard_Real (x), Standard_Real (y), Standard_Real (z));
768                 Proj.SetCoord
769                     (Standard_Real (dx), Standard_Real (dy), Standard_Real (dz));
770                 Proj.Normalize();
771             }
772             // unproject cannot be done by UnProjectRaster
773             // Code is suspended since drivers Phigs are Pex abandoned.
774             else {
775
776 Standard_Real NPCX, NPCY, NPCZ;
777 Standard_Real VRCX, VRCY, VRCZ, VRCT;
778 Standard_Real WCX, WCY, WCZ, WCT;
779
780 TColStd_Array2OfReal TOri_Matrix (0,3,0,3);
781 TColStd_Array2OfReal TMap_Matrix (0,3,0,3);
782 TColStd_Array2OfReal TOri_Matrix_Inv (0,3,0,3);
783 TColStd_Array2OfReal TMap_Matrix_Inv (0,3,0,3);
784
785 Standard_Real Dx, Dy, Ratio;
786 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
787 Standard_Integer j;
788
789 Standard_Integer stop = 0;
790
791 Standard_Boolean BResult;
792
793                 j = MyDefinedView.Extent ();
794
795                 while ((! stop) && (MyIterator.More ())) {
796                     if (TheCView.ViewId ==
797                         (MyIterator.Value ())->Identification ()) {
798                         TOri_Matrix     =
799                                 (MyIterator.Value ())->MatrixOfOrientation ();
800                         TMap_Matrix     =
801                                 (MyIterator.Value ())->MatrixOfMapping ();
802                         stop    = 1;
803                     }
804
805                     // MyIterator.Next () is located on the next view
806                     MyIterator.Next ();
807                 }
808
809                 // View Mapping Transformation and View Clip, inversion
810                 BResult = Aspect::Inverse (TMap_Matrix, TMap_Matrix_Inv);
811
812                 // View Orientation Transformation, inversion
813                 BResult = Aspect::Inverse (TOri_Matrix, TOri_Matrix_Inv);
814
815                 // (AU, AV) : Device Coordinate Space
816                 // DCS -> NPCS Normalized Projection Coordinate Space
817                 Dx      = Standard_Real (Width);
818                 Dy      = Standard_Real (Height);
819                 Ratio   = Dx / Dy;
820
821                 if (Ratio >= 1.) {
822                         NPCX    = Standard_Real (AU) / Dx;
823                         NPCY    = (Dy - Standard_Real (AV)) / Dx;
824                 }
825                 else {
826                         NPCX    = Standard_Real (AU) / Dy;
827                         NPCY    = (Dy - Standard_Real (AV)) / Dy;
828                 }
829                 NPCZ    = 0.0;
830
831                 // NPCS -> VRCS View Reference Coordinate Space
832                 // PtVRC = Map_Matrix_Inv.Multiplied (PtNPC);
833
834                 VRCX    = TMap_Matrix_Inv (0, 0) * NPCX
835                         + TMap_Matrix_Inv (0, 1) * NPCY
836                         + TMap_Matrix_Inv (0, 2) * NPCZ
837                         + TMap_Matrix_Inv (0, 3);
838                 VRCY    = TMap_Matrix_Inv (1, 0) * NPCX
839                         + TMap_Matrix_Inv (1, 1) * NPCY
840                         + TMap_Matrix_Inv (1, 2) * NPCZ
841                         + TMap_Matrix_Inv (1, 3);
842                 VRCZ    = TMap_Matrix_Inv (2, 0) * NPCX
843                         + TMap_Matrix_Inv (2, 1) * NPCY
844                         + TMap_Matrix_Inv (2, 2) * NPCZ
845                         + TMap_Matrix_Inv (2, 3);
846                 VRCT    = TMap_Matrix_Inv (3, 0) * NPCX
847                         + TMap_Matrix_Inv (3, 1) * NPCY
848                         + TMap_Matrix_Inv (3, 2) * NPCZ
849                         + TMap_Matrix_Inv (3, 3);
850
851                 // VRCS -> WCS World Coordinate Space
852                 // PtWC = Ori_Matrix_Inv.Multiplied (PtVRC);
853
854                 WCX     = TOri_Matrix_Inv (0, 0) * VRCX
855                         + TOri_Matrix_Inv (0, 1) * VRCY
856                         + TOri_Matrix_Inv (0, 2) * VRCZ
857                         + TOri_Matrix_Inv (0, 3) * VRCT;
858                 WCY     = TOri_Matrix_Inv (1, 0) * VRCX
859                         + TOri_Matrix_Inv (1, 1) * VRCY
860                         + TOri_Matrix_Inv (1, 2) * VRCZ
861                         + TOri_Matrix_Inv (1, 3) * VRCT;
862                 WCZ     = TOri_Matrix_Inv (2, 0) * VRCX
863                         + TOri_Matrix_Inv (2, 1) * VRCY
864                         + TOri_Matrix_Inv (2, 2) * VRCZ
865                         + TOri_Matrix_Inv (2, 3) * VRCT;
866                 WCT     = TOri_Matrix_Inv (3, 0) * VRCX
867                         + TOri_Matrix_Inv (3, 1) * VRCY
868                         + TOri_Matrix_Inv (3, 2) * VRCZ
869                         + TOri_Matrix_Inv (3, 3) * VRCT;
870
871                 if (WCT != 0.)
872                     Point.SetCoord (WCX/WCT, WCY/WCT, WCZ/WCT);
873                 else
874                     Point.SetCoord (RealLast (), RealLast (), RealLast ());
875
876                 // Define projection ray
877                 NPCZ    = 10.0;
878
879                 // NPCS -> VRCS View Reference Coordinate Space
880                 // PtVRC = Map_Matrix_Inv.Multiplied (PtNPC);
881
882                 VRCX    = TMap_Matrix_Inv (0, 0) * NPCX
883                         + TMap_Matrix_Inv (0, 1) * NPCY
884                         + TMap_Matrix_Inv (0, 2) * NPCZ
885                         + TMap_Matrix_Inv (0, 3);
886                 VRCY    = TMap_Matrix_Inv (1, 0) * NPCX
887                         + TMap_Matrix_Inv (1, 1) * NPCY
888                         + TMap_Matrix_Inv (1, 2) * NPCZ
889                         + TMap_Matrix_Inv (1, 3);
890                 VRCZ    = TMap_Matrix_Inv (2, 0) * NPCX
891                         + TMap_Matrix_Inv (2, 1) * NPCY
892                         + TMap_Matrix_Inv (2, 2) * NPCZ
893                         + TMap_Matrix_Inv (2, 3);
894                 VRCT    = TMap_Matrix_Inv (3, 0) * NPCX
895                         + TMap_Matrix_Inv (3, 1) * NPCY
896                         + TMap_Matrix_Inv (3, 2) * NPCZ
897                         + TMap_Matrix_Inv (3, 3);
898
899                 // VRCS -> WCS World Coordinate Space
900                 // PtWC = Ori_Matrix_Inv.Multiplied (PtVRC);
901
902                 WCX     = TOri_Matrix_Inv (0, 0) * VRCX
903                         + TOri_Matrix_Inv (0, 1) * VRCY
904                         + TOri_Matrix_Inv (0, 2) * VRCZ
905                         + TOri_Matrix_Inv (0, 3) * VRCT;
906                 WCY     = TOri_Matrix_Inv (1, 0) * VRCX
907                         + TOri_Matrix_Inv (1, 1) * VRCY
908                         + TOri_Matrix_Inv (1, 2) * VRCZ
909                         + TOri_Matrix_Inv (1, 3) * VRCT;
910                 WCZ     = TOri_Matrix_Inv (2, 0) * VRCX
911                         + TOri_Matrix_Inv (2, 1) * VRCY
912                         + TOri_Matrix_Inv (2, 2) * VRCZ
913                         + TOri_Matrix_Inv (2, 3) * VRCT;
914                 WCT     = TOri_Matrix_Inv (3, 0) * VRCX
915                         + TOri_Matrix_Inv (3, 1) * VRCY
916                         + TOri_Matrix_Inv (3, 2) * VRCZ
917                         + TOri_Matrix_Inv (3, 3) * VRCT;
918
919                 if (WCT != 0.) {
920                     Proj.SetCoord (WCX/WCT, WCY/WCT, WCZ/WCT);
921                     Proj.Normalize();
922                 }
923                 else
924                     Proj.SetCoord (0., 0., 0.);
925             }
926         }
927
928 }
929
930
931 Standard_Boolean Visual3d_ViewManager::ViewExists (const Handle(Aspect_Window)& AWindow, Graphic3d_CView& TheCView) const {
932
933 Standard_Boolean Exist = Standard_False;
934
935         // Parse the list of views to find
936         // a view with the specified window
937         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
938         int TheWindowIdOfView;
939
940 #ifndef WNT
941 const Handle(Xw_Window) THEWindow = *(Handle(Xw_Window) *) &AWindow;
942         int TheSpecifiedWindowId = int (THEWindow->XWindow ());
943 #else
944 const Handle(WNT_Window) THEWindow = *(Handle(WNT_Window) *) &AWindow;
945         int TheSpecifiedWindowId = int (THEWindow->HWindow ());
946 #endif  // WNT
947
948         while ((! Exist) && (MyIterator.More ())) {
949
950            if ( ((MyIterator.Value ())->IsDefined ()) &&
951                 ((MyIterator.Value ())->IsActive ()) ) {
952
953 const Handle(Aspect_Window) AspectWindow = (MyIterator.Value ())->Window ();
954 #ifndef WNT
955 const Handle(Xw_Window) theWindow = *(Handle(Xw_Window) *) &AspectWindow;
956         TheWindowIdOfView = int (theWindow->XWindow ());
957 #else
958 const Handle(WNT_Window) theWindow = *(Handle(WNT_Window) *) &AspectWindow;
959         TheWindowIdOfView = int (theWindow->HWindow ());
960 #endif  // WNT
961                 // Comparaison on window IDs
962                 if (TheWindowIdOfView == TheSpecifiedWindowId) {
963                         Exist   = Standard_True;
964                         TheCView        = *(CALL_DEF_VIEW *)(MyIterator.Value ())->CView ();
965                 }
966            } /* if ((MyIterator.Value ())->IsDefined ()) */
967
968            // MyIterator.Next () is located on the next view
969            MyIterator.Next ();
970         }
971
972         return (Exist);
973
974 }
975
976 void Visual3d_ViewManager::Activate () {
977
978         //
979         // Activates all deactivated views
980         //
981         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
982  
983         while (MyIterator.More ()) {
984                 if (! (MyIterator.Value ())->IsActive ())
985                         (MyIterator.Value ())->Activate ();
986
987                 // MyIterator.Next () is located on the next view
988                 MyIterator.Next ();
989         }
990
991 }
992
993 void Visual3d_ViewManager::Deactivate () {
994
995         //
996         // Deactivates all activated views
997         //
998         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
999  
1000         while (MyIterator.More ()) {
1001                 if ((MyIterator.Value ())->IsActive ())
1002                         (MyIterator.Value ())->Deactivate ();
1003
1004                 // MyIterator.Next () is located on the next view
1005                 MyIterator.Next ();
1006         }
1007
1008 }
1009
1010 Standard_Integer Visual3d_ViewManager::MaxNumOfViews () const {
1011
1012         // Retourne the planned of definable views for the current
1013         // Visual3d_ViewManager.
1014         return
1015 (Standard_Integer ((View_IDMAX-View_IDMIN+1)/Visual3d_ViewManager::Limit ()));
1016
1017 }
1018
1019 Handle(Graphic3d_Structure) Visual3d_ViewManager::Identification (const Standard_Integer AId) const {
1020
1021         return (Graphic3d_StructureManager::Identification (AId));
1022
1023 }
1024
1025 Standard_Integer Visual3d_ViewManager::Identification () const {
1026  
1027         return (Graphic3d_StructureManager::Identification ());
1028
1029 }
1030
1031 Standard_Integer Visual3d_ViewManager::Identification (const Handle(Visual3d_View)& AView) {
1032
1033         MyDefinedView.Add (AView);
1034         return (MyViewGenId.Next ());
1035
1036 }
1037
1038 void Visual3d_ViewManager::UnIdentification (const Standard_Integer aViewId)
1039 {
1040   MyViewGenId.Free(aViewId);
1041 }
1042
1043 void Visual3d_ViewManager::SetTransparency (const Standard_Boolean AFlag) {
1044
1045         if (MyTransparency && AFlag) return;
1046         if (! MyTransparency && ! AFlag) return;
1047
1048         Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
1049         while (MyIterator.More ()) {
1050                 (MyIterator.Value ())->SetTransparency (AFlag);
1051                 // MyIterator.Next () is located on the next view
1052                 MyIterator.Next ();
1053         }
1054
1055         MyTransparency  = AFlag;
1056
1057 }
1058
1059 Standard_Boolean Visual3d_ViewManager::Transparency () const {
1060
1061         return (MyTransparency);
1062
1063 }
1064
1065 void Visual3d_ViewManager::SetZBufferAuto (const Standard_Boolean AFlag) {
1066
1067         if (MyZBufferAuto && AFlag) return;
1068         if (! MyZBufferAuto && ! AFlag) return;
1069
1070         // if pass from False to True :
1071         // no problem, at the next view update, it 
1072         // will properly ask questions to answer (SetVisualisation)
1073         // if pass from True to False :
1074         // it is necessary to modify ZBufferActivity at each view so that
1075         // zbuffer could be active only if required by context.
1076         // In this case -1 is passed so that the view ask itself the question
1077         // Note : 0 forces the desactivation, 1 forces the activation
1078         if (! AFlag) {
1079                 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
1080                 while (MyIterator.More ()) {
1081                         (MyIterator.Value ())->SetZBufferActivity (-1);
1082                         // MyIterator.Next () is located on the next view
1083                         MyIterator.Next ();
1084                 }
1085         }
1086         MyZBufferAuto   = AFlag;
1087
1088 }
1089
1090 Standard_Boolean Visual3d_ViewManager::ZBufferAuto () const {
1091
1092         return (MyZBufferAuto);
1093
1094 }
1095
1096 void Visual3d_ViewManager::SetLayer (const Handle(Visual3d_Layer)& ALayer) {
1097
1098 #ifdef TRACE_LAYER
1099         cout << "Visual3d_ViewManager::SetLayer\n" << flush;
1100 #endif
1101
1102         if (ALayer->Type () == Aspect_TOL_OVERLAY) {
1103 #ifdef TRACE_LAYER
1104                 if (MyOverLayer.IsNull ())
1105                         cout << "MyOverLayer is defined" << endl;
1106                 else
1107                         cout << "MyOverLayer is redefined" << endl;
1108 #endif
1109                 MyOverLayer = ALayer;
1110         }
1111         else {
1112 #ifdef TRACE_LAYER
1113                 if (MyUnderLayer.IsNull ())
1114                         cout << "MyUnderLayer is defined" << endl;
1115                 else
1116                         cout << "MyUnderLayer is redefined" << endl;
1117 #endif
1118                 MyUnderLayer = ALayer;
1119         }
1120
1121 }
1122
1123 const Handle(Visual3d_Layer)& Visual3d_ViewManager::UnderLayer () const {
1124
1125         return (MyUnderLayer);
1126
1127 }
1128
1129 const Handle(Visual3d_Layer)& Visual3d_ViewManager::OverLayer () const {
1130
1131         return (MyOverLayer);
1132
1133 }
1134
1135 //=======================================================================
1136 //function : ChangeZLayer
1137 //purpose  : 
1138 //=======================================================================
1139
1140 void Visual3d_ViewManager::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
1141                                          const Standard_Integer theLayerId)
1142 {
1143   if (!myLayerIds.Contains (theLayerId))
1144     return;
1145   
1146   // change display layer for structure in all views
1147   if (MyDisplayedStructure.Contains (theStructure))
1148   {
1149     Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
1150     for ( ; aViewIt.More (); aViewIt.Next ())
1151       (aViewIt.Value ())->ChangeZLayer (theStructure, theLayerId);
1152   }
1153   
1154   // tell graphic driver to update the structure's display layer
1155   MyGraphicDriver->ChangeZLayer (
1156     (*(Graphic3d_CStructure*)theStructure->CStructure ()), theLayerId);
1157 }
1158
1159 //=======================================================================
1160 //function : GetZLayer
1161 //purpose  :
1162 //=======================================================================
1163
1164 Standard_Integer Visual3d_ViewManager::GetZLayer (const Handle(Graphic3d_Structure)& theStructure) const
1165 {
1166   Graphic3d_CStructure& aStructure =
1167     (*(Graphic3d_CStructure*)theStructure->CStructure ());
1168
1169   return MyGraphicDriver->GetZLayer (aStructure);
1170 }
1171
1172 //=======================================================================
1173 //function : AddZLayer
1174 //purpose  :
1175 //=======================================================================
1176
1177 Standard_Boolean Visual3d_ViewManager::AddZLayer (Standard_Integer& theLayerId)
1178 {
1179   try
1180   {
1181     OCC_CATCH_SIGNALS
1182     theLayerId = getZLayerGenId ().Next ();
1183     myLayerIds.Add (theLayerId);
1184     myLayerSeq.Append (theLayerId);
1185   }
1186   catch (Aspect_IdentDefinitionError)
1187   {
1188     // new index can't be generated
1189     return Standard_False;
1190   }
1191
1192   // tell all managed views to remove display layers
1193   Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
1194   for ( ; aViewIt.More (); aViewIt.Next ())
1195     (aViewIt.Value ())->AddZLayer (theLayerId);
1196
1197   return Standard_True;
1198 }
1199
1200 //=======================================================================
1201 //function : RemoveZLayer
1202 //purpose  : 
1203 //=======================================================================
1204
1205 Standard_Boolean Visual3d_ViewManager::RemoveZLayer (const Standard_Integer theLayerId)
1206 {
1207   if (!myLayerIds.Contains (theLayerId) || theLayerId == 0)
1208     return Standard_False;
1209
1210   // tell all managed views to remove display layers
1211   Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
1212   for ( ; aViewIt.More (); aViewIt.Next ())
1213     (aViewIt.Value ())->RemoveZLayer (theLayerId);
1214
1215   MyGraphicDriver->UnsetZLayer (theLayerId);
1216
1217   // remove index
1218   for (int aIdx = 1; aIdx <= myLayerSeq.Length (); aIdx++)
1219     if (myLayerSeq(aIdx) == theLayerId)
1220     {
1221       myLayerSeq.Remove (aIdx);
1222       break;
1223     }
1224
1225   myLayerIds.Remove (theLayerId);
1226   getZLayerGenId ().Free (theLayerId);
1227
1228   return Standard_True;
1229 }
1230
1231 //=======================================================================
1232 //function : GetAllZLayers
1233 //purpose  :
1234 //=======================================================================
1235
1236 void Visual3d_ViewManager::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
1237 {
1238   theLayerSeq.Assign (myLayerSeq);
1239 }
1240
1241 //=======================================================================
1242 //function : getZLayerGenId
1243 //purpose  :
1244 //=======================================================================
1245
1246 Aspect_GenId& Visual3d_ViewManager::getZLayerGenId ()
1247 {
1248   static Aspect_GenId aGenId (1, IntegerLast());
1249   return aGenId;
1250 }