1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
20 /***********************************************************************
24 Classe Visual3d_ViewManager.cxx :
26 Declaration of variables specific to visualisers
28 HISTORIQUE DES MODIFICATIONS :
29 --------------------------------
30 Mars 1992 : NW,JPB,CAL ; Creation.
31 19-06-96 : FMN ; Suppression variables inutiles
32 04-02-97 : FMN ; Suppression de PSOutput, XWDOutput ...
33 06-05-97 : CAL ; Ajout du Clear sur les TOS_COMPUTED.
34 19-09-97 : CAL ; Remplacement de Window->Position par Window->Size;
35 24-10-97 : CAL ; Retrait de DownCast.
36 20-11-97 : CAL ; Disparition de la dependance avec math
37 01-12-97 : CAL ; Retrait du test IsActive sur l'Update et le Redraw
38 31-12-97 : CAL ; Disparition de MathGra
39 16-01-98 : CAL ; Ajout du SetTransform sur une TOS_COMPUTED
40 11-03-98 : CAL ; Visual3d_ViewManager::Remove ()
41 20-05-98 : CAL ; Perfs. Connection entre structures COMPUTED.
42 10-06-98 : CAL ; Modification des signatures de xxProjectRaster.
43 10-06-98 : CAL ; Modification de la signature de ViewExists.
44 01-12-98 : CAL ; S4062. Ajout des layers.
45 02-12-98 : CAL ; Remove () ne detruit plus les vues.
47 ************************************************************************/
49 /*----------------------------------------------------------------------*/
57 /*----------------------------------------------------------------------*/
63 #include <Visual3d_ViewManager.ixx>
64 #include <Visual3d_ViewManager.pxx>
66 #include <Standard_ErrorHandler.hxx>
69 #include <Aspect_IdentDefinitionError.hxx>
71 #include <Graphic3d_GraphicDriver.hxx>
72 #include <Graphic3d_MapOfStructure.hxx>
73 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
75 #include <Visual3d_PickPath.hxx>
76 #include <Visual3d_SetIteratorOfSetOfView.hxx>
78 #if defined (_WIN32) || defined(__WIN32__)
79 # include <WNT_Window.hxx>
80 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
81 # include <Cocoa_Window.hxx>
83 # include <Xw_Window.hxx>
88 //-Global data definitions
90 // -- les vues definies
91 // MyDefinedView : SetOfView;
93 // -- le generateur d'identificateurs de vues
94 // MyViewGenId : GenId;
98 Visual3d_ViewManager::Visual3d_ViewManager (const Handle(Graphic3d_GraphicDriver)& theDriver):
99 Graphic3d_StructureManager (theDriver),
101 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),
102 MyZBufferAuto (Standard_False),
103 MyTransparency (Standard_False)
105 // default layer is always presented in display layer sequence
106 // it can not be removed
108 myLayerSeq.Append (0);
110 MyGraphicDriver = theDriver;
115 void Visual3d_ViewManager::Destroy () {
118 cout << "Visual3d_ViewManager::Destroy (" << MyId << ")\n" << flush;
126 void Visual3d_ViewManager::Remove () {
129 cout << "Visual3d_ViewManager::Remove (" << MyId << ")\n" << flush;
133 // Destroy all defined views
137 cout << "The Manager " << MyId << " have " << Length << " defined views\n";
141 // clear all structures whilst views are alive for correct GPU memory management
142 MyDisplayedStructure.Clear();
143 MyHighlightedStructure.Clear();
144 MyVisibleStructure.Clear();
145 MyPickStructure.Clear();
147 // clear list of managed views
148 MyDefinedView.Clear();
151 void Visual3d_ViewManager::ChangeDisplayPriority (const Handle(Graphic3d_Structure)& AStructure, const Standard_Integer OldPriority, const Standard_Integer NewPriority) {
154 cout << "Visual3d_ViewManager::ChangeDisplayPriority ("
155 << AStructure->Identification ()
156 << ", " << OldPriority << ", " << NewPriority << ")\n";
161 // Change structure priority in all defined views
163 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
165 while (MyIterator.More ()) {
166 (MyIterator.Value ())->ChangeDisplayPriority
167 (AStructure, OldPriority, NewPriority);
169 // MyIterator.Next () is located on the next view
175 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure) {
177 //Standard_Integer LengthD = MyDisplayedStructure.Extent() ();
179 // Even if physically the structure cannot
180 // be displayed (pb of visualisation type)
181 // it has status Displayed.
183 if (!MyDisplayedStructure.Contains(AStructure))
187 // Recompute structure in all activated views
189 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
191 while (MyIterator.More ()) {
192 (MyIterator.Value ())->ReCompute (AStructure);
194 // MyIterator.Next () is located on the next view
200 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure,
201 const Handle(Graphic3d_DataStructureManager)& AProjector)
204 if (! AProjector->IsKind (STANDARD_TYPE (Visual3d_View))) return;
207 Handle(Visual3d_View) theView = Handle(Visual3d_View)::DownCast (AProjector);
209 Handle(Visual3d_View) theView = *(Handle(Visual3d_View) *) &AProjector;
211 Standard_Integer ViewId = theView->Identification ();
213 // Even if physically the structure cannot
214 // be displayed (pb of visualisation type)
215 // it has status Displayed.
216 if (!MyDisplayedStructure.Contains(AStructure))
220 // Recompute structure in all activated views
222 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
224 while (MyIterator.More ()) {
225 if ((MyIterator.Value ())->Identification () == ViewId)
226 theView->ReCompute (AStructure);
228 // MyIterator.Next () is located on the next view
234 void Visual3d_ViewManager::Clear (const Handle(Graphic3d_Structure)& AStructure, const Standard_Boolean WithDestruction) {
236 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
238 while (MyIterator.More ()) {
239 (MyIterator.Value ())->Clear (AStructure, WithDestruction);
241 // MyIterator.Next () is located on the next view
247 void Visual3d_ViewManager::Connect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter) {
249 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
251 while (MyIterator.More ()) {
252 (MyIterator.Value ())->Connect (AMother, ADaughter);
254 // MyIterator.Next () is located on the next view
260 void Visual3d_ViewManager::Disconnect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter) {
262 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
264 while (MyIterator.More ()) {
265 (MyIterator.Value ())->Disconnect (AMother, ADaughter);
267 // MyIterator.Next () is located on the next view
273 void Visual3d_ViewManager::Display (const Handle(Graphic3d_Structure)& AStructure) {
276 // Even if physically the structure cannot
277 // be displayed (pb of visualisation type)
278 // it has status Displayed.
280 MyDisplayedStructure.Add(AStructure);
283 // Display structure in all activated views
285 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
287 while (MyIterator.More ()) {
288 (MyIterator.Value ())->Display (AStructure);
290 // MyIterator.Next () is located on the next view
296 void Visual3d_ViewManager::Erase (const Handle(Graphic3d_Structure)& AStructure) {
299 // Even if physically the structure cannot
300 // be displayed (pb of visualisation type)
301 // it has status Displayed.
303 MyDisplayedStructure.Remove(AStructure);
308 // Erase structure in all defined views
310 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
312 while (MyIterator.More ()) {
313 (MyIterator.Value ())->Erase (AStructure);
315 // MyIterator.Next () is located on the next view
319 MyHighlightedStructure.Remove (AStructure);
320 MyVisibleStructure.Remove (AStructure);
321 MyPickStructure.Remove (AStructure);
325 void Visual3d_ViewManager::Erase () {
327 Graphic3d_MapIteratorOfMapOfStructure it( MyDisplayedStructure);
329 for (; it.More(); it.Next()) {
330 Handle(Graphic3d_Structure) SG = it.Key();
336 void Visual3d_ViewManager::Highlight (const Handle(Graphic3d_Structure)& AStructure, const Aspect_TypeOfHighlightMethod AMethod) {
338 MyHighlightedStructure.Add(AStructure);
341 // Highlight in all activated views
343 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
345 while (MyIterator.More ()) {
346 (MyIterator.Value ())->Highlight (AStructure, AMethod);
348 // MyIterator.Next () is located on the next view
354 void Visual3d_ViewManager::SetTransform (const Handle(Graphic3d_Structure)& AStructure, const TColStd_Array2OfReal& ATrsf) {
356 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
358 while (MyIterator.More ()) {
359 (MyIterator.Value ())->SetTransform (AStructure, ATrsf);
361 // MyIterator.Next () is located on the next view
367 void Visual3d_ViewManager::UnHighlight () {
369 Graphic3d_MapIteratorOfMapOfStructure it(MyHighlightedStructure);
371 for (; it.More(); it.Next()) {
372 Handle(Graphic3d_Structure) SG = it.Key();
379 void Visual3d_ViewManager::UnHighlight (const Handle(Graphic3d_Structure)& AStructure) {
381 MyHighlightedStructure.Remove(AStructure);
385 // UnHighlight in all activated views
387 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
389 while (MyIterator.More ()) {
390 (MyIterator.Value ())->UnHighlight (AStructure);
392 // MyIterator.Next () is located on the next view
398 void Visual3d_ViewManager::Redraw () const {
400 Standard_Integer MaxDx, MaxDy;
401 Standard_Integer Dx, Dy;
402 MaxDx = MaxDy = IntegerFirst ();
405 // Redraw all activated views
407 Standard_Integer j = MyDefinedView.Extent ();
409 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
411 if (! MyUnderLayer.IsNull () || ! MyOverLayer.IsNull ()) {
412 while (MyIterator.More ()) {
413 (MyIterator.Value ())->Window ()->Size (Dx, Dy);
414 if (Dx > MaxDx) MaxDx = Dx;
415 if (Dy > MaxDy) MaxDy = Dy;
417 // MyIterator.Next () is located on the next view
420 if (! MyUnderLayer.IsNull ())
421 MyUnderLayer->SetViewport (MaxDx, MaxDy);
422 if (! MyOverLayer.IsNull ())
423 MyOverLayer->SetViewport (MaxDx, MaxDy);
426 if (! MyUnderLayer.IsNull () || ! MyOverLayer.IsNull ())
427 MyIterator.Initialize (MyDefinedView);
428 while (MyIterator.More ()) {
429 (MyIterator.Value ())->Redraw (MyUnderLayer, MyOverLayer);
431 // MyIterator.Next () is located on the next view
437 void Visual3d_ViewManager::Update () const {
440 // Update all activated views
442 Standard_Integer j = MyDefinedView.Extent ();
444 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
446 while (MyIterator.More ()) {
447 (MyIterator.Value ())->Update (MyUnderLayer, MyOverLayer);
449 // MyIterator.Next () is located on the next view
455 Handle(Visual3d_HSetOfView) Visual3d_ViewManager::ActivatedView () const {
457 Handle (Visual3d_HSetOfView) SG = new Visual3d_HSetOfView ();
459 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
461 while (MyIterator.More ()) {
462 if ((MyIterator.Value ())->IsActive ())
463 SG->Add (MyIterator.Value ());
465 // MyIterator.Next () is located on the next view
474 Standard_Boolean Visual3d_ViewManager::ContainsComputedStructure () const {
476 Standard_Boolean Result = Standard_False;
479 // Check all activated views
481 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
483 Standard_Integer i = MyDefinedView.Extent ();
485 while ((! Result) && (MyIterator.More ())) {
486 if ((MyIterator.Value ())->IsActive ())
488 (MyIterator.Value ())->ContainsComputedStructure ();
490 // MyIterator.Next () is located on the next view
498 Handle(Visual3d_HSetOfView) Visual3d_ViewManager::DefinedView () const {
500 Handle (Visual3d_HSetOfView) SG = new Visual3d_HSetOfView ();
502 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
504 while (MyIterator.More ()) {
505 SG->Add (MyIterator.Value ());
507 // MyIterator.Next () is located on the next view
515 void Visual3d_ViewManager::ConvertCoord (const Handle(Aspect_Window)& AWindow, const Graphic3d_Vertex& AVertex, Standard_Integer& AU, Standard_Integer& AV) const {
517 // Convert only if the data is correct
518 Standard_Boolean Exist;
519 Graphic3d_CView TheCView;
520 //Graphic3d_Vertex Point;
522 TColStd_Array2OfReal Ori_Matrix (0,3,0,3);
523 TColStd_Array2OfReal Map_Matrix (0,3,0,3);
525 Standard_Integer Width, Height;
526 Standard_Real AX, AY, AZ;
527 Standard_Real Dx, Dy, Ratio;
529 Exist = ViewExists (AWindow, TheCView);
532 AU = AV = IntegerLast ();
535 // NKV - 11.02.08 - Use graphic driver functions
536 Standard_Boolean Result;
538 AVertex.Coord (AX, AY, AZ);
540 Result = MyGraphicDriver->ProjectRaster (TheCView,
541 Standard_ShortReal (AX), Standard_ShortReal (AY), Standard_ShortReal (AZ),
547 Standard_Real PtX, PtY, PtZ, PtT;
548 Standard_Real APX, APY, APZ;
551 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
553 Standard_Integer stop = 0;
555 while ((! stop) && (MyIterator.More ())) {
556 if (TheCView.ViewId ==
557 (MyIterator.Value ())->Identification ()) {
559 (MyIterator.Value ())->MatrixOfOrientation ();
561 (MyIterator.Value ())->MatrixOfMapping ();
565 // MyIterator.Next () is located on the next view
570 // WCS -> View Reference Coordinate Space
571 PtX = Ori_Matrix (0, 0) * AX
572 + Ori_Matrix (0, 1) * AY
573 + Ori_Matrix (0, 2) * AZ
575 PtY = Ori_Matrix (1, 0) * AX
576 + Ori_Matrix (1, 1) * AY
577 + Ori_Matrix (1, 2) * AZ
579 PtZ = Ori_Matrix (2, 0) * AX
580 + Ori_Matrix (2, 1) * AY
581 + Ori_Matrix (2, 2) * AZ
583 PtT = Ori_Matrix (3, 0) * AX
584 + Ori_Matrix (3, 1) * AY
585 + Ori_Matrix (3, 2) * AZ
588 // VRCS -> Normalized Projection Coordinate Space
589 APX = Map_Matrix (0, 0) * PtX
590 + Map_Matrix (0, 1) * PtY
591 + Map_Matrix (0, 2) * PtZ
592 + Map_Matrix (0, 3) * PtT;
593 APY = Map_Matrix (1, 0) * PtX
594 + Map_Matrix (1, 1) * PtY
595 + Map_Matrix (1, 2) * PtZ
596 + Map_Matrix (1, 3) * PtT;
597 APZ = Map_Matrix (2, 0) * PtX
598 + Map_Matrix (2, 1) * PtY
599 + Map_Matrix (2, 2) * PtZ
600 + Map_Matrix (2, 3) * PtT;
601 APT = Map_Matrix (3, 0) * PtX
602 + Map_Matrix (3, 1) * PtY
603 + Map_Matrix (3, 2) * PtZ
604 + Map_Matrix (3, 3) * PtT;
606 if (APT == 0. || stop == 0) {
607 AU = AV = IntegerLast ();
614 // NPCS -> Device Coordinate Space
615 AWindow->Size (Width, Height);
616 Dx = Standard_Real (Width);
617 Dy = Standard_Real (Height);
620 AU = Standard_Integer (APX * Dx);
621 AV = Standard_Integer (Dy - APY * Dy * Ratio);
624 AU = Standard_Integer (APX * Dx / Ratio);
625 AV = Standard_Integer (Dy - APY * Dy);
633 Graphic3d_Vertex Visual3d_ViewManager::ConvertCoord (const Handle(Aspect_Window)& AWindow, const Standard_Integer AU, const Standard_Integer AV) const {
635 // Convert only if the data is correct
636 Graphic3d_CView TheCView;
637 Graphic3d_Vertex Point;
639 if (ViewExists (AWindow, TheCView))
641 Standard_Integer Width, Height;
642 Standard_ShortReal x, y, z;
643 Standard_Boolean Result;
645 AWindow->Size (Width, Height);
647 Result = MyGraphicDriver->UnProjectRaster (TheCView,
651 // unproject is done by UnProjectRaster
654 (Standard_Real (x), Standard_Real (y), Standard_Real (z));
656 // unproject cannot be done by UnProjectRaster
657 // Code suspended since drivers Phigs and Pex are abandoned.
660 Standard_Real NPCX, NPCY, NPCZ;
661 Standard_Real VRCX, VRCY, VRCZ, VRCT;
662 Standard_Real WCX, WCY, WCZ, WCT;
664 TColStd_Array2OfReal TOri_Matrix (0,3,0,3);
665 TColStd_Array2OfReal TMap_Matrix (0,3,0,3);
666 TColStd_Array2OfReal TOri_Matrix_Inv (0,3,0,3);
667 TColStd_Array2OfReal TMap_Matrix_Inv (0,3,0,3);
669 Standard_Real Dx, Dy, Ratio;
670 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
673 Standard_Integer stop = 0;
675 Standard_Boolean BResult;
677 j = MyDefinedView.Extent ();
679 while ((! stop) && (MyIterator.More ())) {
680 if (TheCView.ViewId ==
681 (MyIterator.Value ())->Identification ()) {
683 (MyIterator.Value ())->MatrixOfOrientation ();
685 (MyIterator.Value ())->MatrixOfMapping ();
689 // MyIterator.Next () is located on the next view
693 // View Mapping Transformation and View Clip, inversion
694 BResult = Aspect::Inverse (TMap_Matrix, TMap_Matrix_Inv);
696 // View Orientation Transformation, inversion
697 BResult = Aspect::Inverse (TOri_Matrix, TOri_Matrix_Inv);
699 // (AU, AV) : Device Coordinate Space
700 // DCS -> NPCS Normalized Projection Coordinate Space
701 Dx = Standard_Real (Width);
702 Dy = Standard_Real (Height);
706 NPCX = Standard_Real (AU) / Dx;
707 NPCY = (Dy - Standard_Real (AV)) / Dx;
710 NPCX = Standard_Real (AU) / Dy;
711 NPCY = (Dy - Standard_Real (AV)) / Dy;
715 // NPCS -> VRCS View Reference Coordinate Space
716 // PtVRC = Map_Matrix_Inv.Multiplied (PtNPC);
718 VRCX = TMap_Matrix_Inv (0, 0) * NPCX
719 + TMap_Matrix_Inv (0, 1) * NPCY
720 + TMap_Matrix_Inv (0, 2) * NPCZ
721 + TMap_Matrix_Inv (0, 3);
722 VRCY = TMap_Matrix_Inv (1, 0) * NPCX
723 + TMap_Matrix_Inv (1, 1) * NPCY
724 + TMap_Matrix_Inv (1, 2) * NPCZ
725 + TMap_Matrix_Inv (1, 3);
726 VRCZ = TMap_Matrix_Inv (2, 0) * NPCX
727 + TMap_Matrix_Inv (2, 1) * NPCY
728 + TMap_Matrix_Inv (2, 2) * NPCZ
729 + TMap_Matrix_Inv (2, 3);
730 VRCT = TMap_Matrix_Inv (3, 0) * NPCX
731 + TMap_Matrix_Inv (3, 1) * NPCY
732 + TMap_Matrix_Inv (3, 2) * NPCZ
733 + TMap_Matrix_Inv (3, 3);
735 // VRCS -> WCS World Coordinate Space
736 // PtWC = Ori_Matrix_Inv.Multiplied (PtVRC);
738 WCX = TOri_Matrix_Inv (0, 0) * VRCX
739 + TOri_Matrix_Inv (0, 1) * VRCY
740 + TOri_Matrix_Inv (0, 2) * VRCZ
741 + TOri_Matrix_Inv (0, 3) * VRCT;
742 WCY = TOri_Matrix_Inv (1, 0) * VRCX
743 + TOri_Matrix_Inv (1, 1) * VRCY
744 + TOri_Matrix_Inv (1, 2) * VRCZ
745 + TOri_Matrix_Inv (1, 3) * VRCT;
746 WCZ = TOri_Matrix_Inv (2, 0) * VRCX
747 + TOri_Matrix_Inv (2, 1) * VRCY
748 + TOri_Matrix_Inv (2, 2) * VRCZ
749 + TOri_Matrix_Inv (2, 3) * VRCT;
750 WCT = TOri_Matrix_Inv (3, 0) * VRCX
751 + TOri_Matrix_Inv (3, 1) * VRCY
752 + TOri_Matrix_Inv (3, 2) * VRCZ
753 + TOri_Matrix_Inv (3, 3) * VRCT;
756 Point.SetCoord (WCX/WCT, WCY/WCT, WCZ/WCT);
764 void Visual3d_ViewManager::ConvertCoordWithProj (const Handle(Aspect_Window)& AWindow, const Standard_Integer AU, const Standard_Integer AV, Graphic3d_Vertex& Point, Graphic3d_Vector& Proj) const {
766 // Conversion only if the data is correct
767 Graphic3d_CView TheCView;
769 if (! ViewExists (AWindow, TheCView)) {
770 Point.SetCoord (0., 0., 0.);
771 Proj.SetCoord (0., 0., 0.);
774 Standard_Integer Width, Height;
775 Standard_ShortReal x, y, z;
776 Standard_ShortReal dx, dy, dz;
777 Standard_Boolean Result;
779 AWindow->Size (Width, Height);
781 Result = MyGraphicDriver->UnProjectRasterWithRay (TheCView,
783 AU, AV, x, y, z, dx, dy, dz);
785 // unproject is done by UnProjectRaster
788 (Standard_Real (x), Standard_Real (y), Standard_Real (z));
790 (Standard_Real (dx), Standard_Real (dy), Standard_Real (dz));
793 // unproject cannot be done by UnProjectRaster
794 // Code is suspended since drivers Phigs are Pex abandoned.
797 Standard_Real NPCX, NPCY, NPCZ;
798 Standard_Real VRCX, VRCY, VRCZ, VRCT;
799 Standard_Real WCX, WCY, WCZ, WCT;
801 TColStd_Array2OfReal TOri_Matrix (0,3,0,3);
802 TColStd_Array2OfReal TMap_Matrix (0,3,0,3);
803 TColStd_Array2OfReal TOri_Matrix_Inv (0,3,0,3);
804 TColStd_Array2OfReal TMap_Matrix_Inv (0,3,0,3);
806 Standard_Real Dx, Dy, Ratio;
807 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
810 Standard_Integer stop = 0;
812 Standard_Boolean BResult;
814 j = MyDefinedView.Extent ();
816 while ((! stop) && (MyIterator.More ())) {
817 if (TheCView.ViewId ==
818 (MyIterator.Value ())->Identification ()) {
820 (MyIterator.Value ())->MatrixOfOrientation ();
822 (MyIterator.Value ())->MatrixOfMapping ();
826 // MyIterator.Next () is located on the next view
830 // View Mapping Transformation and View Clip, inversion
831 BResult = Aspect::Inverse (TMap_Matrix, TMap_Matrix_Inv);
833 // View Orientation Transformation, inversion
834 BResult = Aspect::Inverse (TOri_Matrix, TOri_Matrix_Inv);
836 // (AU, AV) : Device Coordinate Space
837 // DCS -> NPCS Normalized Projection Coordinate Space
838 Dx = Standard_Real (Width);
839 Dy = Standard_Real (Height);
843 NPCX = Standard_Real (AU) / Dx;
844 NPCY = (Dy - Standard_Real (AV)) / Dx;
847 NPCX = Standard_Real (AU) / Dy;
848 NPCY = (Dy - Standard_Real (AV)) / Dy;
852 // NPCS -> VRCS View Reference Coordinate Space
853 // PtVRC = Map_Matrix_Inv.Multiplied (PtNPC);
855 VRCX = TMap_Matrix_Inv (0, 0) * NPCX
856 + TMap_Matrix_Inv (0, 1) * NPCY
857 + TMap_Matrix_Inv (0, 2) * NPCZ
858 + TMap_Matrix_Inv (0, 3);
859 VRCY = TMap_Matrix_Inv (1, 0) * NPCX
860 + TMap_Matrix_Inv (1, 1) * NPCY
861 + TMap_Matrix_Inv (1, 2) * NPCZ
862 + TMap_Matrix_Inv (1, 3);
863 VRCZ = TMap_Matrix_Inv (2, 0) * NPCX
864 + TMap_Matrix_Inv (2, 1) * NPCY
865 + TMap_Matrix_Inv (2, 2) * NPCZ
866 + TMap_Matrix_Inv (2, 3);
867 VRCT = TMap_Matrix_Inv (3, 0) * NPCX
868 + TMap_Matrix_Inv (3, 1) * NPCY
869 + TMap_Matrix_Inv (3, 2) * NPCZ
870 + TMap_Matrix_Inv (3, 3);
872 // VRCS -> WCS World Coordinate Space
873 // PtWC = Ori_Matrix_Inv.Multiplied (PtVRC);
875 WCX = TOri_Matrix_Inv (0, 0) * VRCX
876 + TOri_Matrix_Inv (0, 1) * VRCY
877 + TOri_Matrix_Inv (0, 2) * VRCZ
878 + TOri_Matrix_Inv (0, 3) * VRCT;
879 WCY = TOri_Matrix_Inv (1, 0) * VRCX
880 + TOri_Matrix_Inv (1, 1) * VRCY
881 + TOri_Matrix_Inv (1, 2) * VRCZ
882 + TOri_Matrix_Inv (1, 3) * VRCT;
883 WCZ = TOri_Matrix_Inv (2, 0) * VRCX
884 + TOri_Matrix_Inv (2, 1) * VRCY
885 + TOri_Matrix_Inv (2, 2) * VRCZ
886 + TOri_Matrix_Inv (2, 3) * VRCT;
887 WCT = TOri_Matrix_Inv (3, 0) * VRCX
888 + TOri_Matrix_Inv (3, 1) * VRCY
889 + TOri_Matrix_Inv (3, 2) * VRCZ
890 + TOri_Matrix_Inv (3, 3) * VRCT;
893 Point.SetCoord (WCX/WCT, WCY/WCT, WCZ/WCT);
895 Point.SetCoord (0., 0., 0.);
897 // Define projection ray
900 // NPCS -> VRCS View Reference Coordinate Space
901 // PtVRC = Map_Matrix_Inv.Multiplied (PtNPC);
903 VRCX = TMap_Matrix_Inv (0, 0) * NPCX
904 + TMap_Matrix_Inv (0, 1) * NPCY
905 + TMap_Matrix_Inv (0, 2) * NPCZ
906 + TMap_Matrix_Inv (0, 3);
907 VRCY = TMap_Matrix_Inv (1, 0) * NPCX
908 + TMap_Matrix_Inv (1, 1) * NPCY
909 + TMap_Matrix_Inv (1, 2) * NPCZ
910 + TMap_Matrix_Inv (1, 3);
911 VRCZ = TMap_Matrix_Inv (2, 0) * NPCX
912 + TMap_Matrix_Inv (2, 1) * NPCY
913 + TMap_Matrix_Inv (2, 2) * NPCZ
914 + TMap_Matrix_Inv (2, 3);
915 VRCT = TMap_Matrix_Inv (3, 0) * NPCX
916 + TMap_Matrix_Inv (3, 1) * NPCY
917 + TMap_Matrix_Inv (3, 2) * NPCZ
918 + TMap_Matrix_Inv (3, 3);
920 // VRCS -> WCS World Coordinate Space
921 // PtWC = Ori_Matrix_Inv.Multiplied (PtVRC);
923 WCX = TOri_Matrix_Inv (0, 0) * VRCX
924 + TOri_Matrix_Inv (0, 1) * VRCY
925 + TOri_Matrix_Inv (0, 2) * VRCZ
926 + TOri_Matrix_Inv (0, 3) * VRCT;
927 WCY = TOri_Matrix_Inv (1, 0) * VRCX
928 + TOri_Matrix_Inv (1, 1) * VRCY
929 + TOri_Matrix_Inv (1, 2) * VRCZ
930 + TOri_Matrix_Inv (1, 3) * VRCT;
931 WCZ = TOri_Matrix_Inv (2, 0) * VRCX
932 + TOri_Matrix_Inv (2, 1) * VRCY
933 + TOri_Matrix_Inv (2, 2) * VRCZ
934 + TOri_Matrix_Inv (2, 3) * VRCT;
935 WCT = TOri_Matrix_Inv (3, 0) * VRCX
936 + TOri_Matrix_Inv (3, 1) * VRCY
937 + TOri_Matrix_Inv (3, 2) * VRCZ
938 + TOri_Matrix_Inv (3, 3) * VRCT;
941 Proj.SetCoord (WCX/WCT, WCY/WCT, WCZ/WCT);
945 Proj.SetCoord (0., 0., 0.);
952 Standard_Boolean Visual3d_ViewManager::ViewExists (const Handle(Aspect_Window)& AWindow, Graphic3d_CView& TheCView) const {
954 Standard_Boolean Exist = Standard_False;
956 // Parse the list of views to find
957 // a view with the specified window
958 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
960 #if defined(_WIN32) || defined(__WIN32__)
961 const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
962 int TheSpecifiedWindowId = int (THEWindow->HWindow ());
963 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
964 const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
965 NSView* TheSpecifiedWindowId = THEWindow->HView();
967 const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
968 int TheSpecifiedWindowId = int (THEWindow->XWindow ());
971 while ((! Exist) && (MyIterator.More ())) {
973 if ( ((MyIterator.Value ())->IsDefined ()) &&
974 ((MyIterator.Value ())->IsActive ()) ) {
976 const Handle(Aspect_Window) AspectWindow = (MyIterator.Value ())->Window ();
977 #if defined(_WIN32) || defined(__WIN32__)
978 const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
979 int TheWindowIdOfView = int (theWindow->HWindow ());
980 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
981 const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
982 NSView* TheWindowIdOfView = theWindow->HView();
984 const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
985 int TheWindowIdOfView = int (theWindow->XWindow ());
987 // Comparaison on window IDs
988 if (TheWindowIdOfView == TheSpecifiedWindowId) {
989 Exist = Standard_True;
990 TheCView = *(Graphic3d_CView* )(MyIterator.Value())->CView();
992 } /* if ((MyIterator.Value ())->IsDefined ()) */
994 // MyIterator.Next () is located on the next view
1002 void Visual3d_ViewManager::Activate () {
1005 // Activates all deactivated views
1007 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
1009 while (MyIterator.More ()) {
1010 if (! (MyIterator.Value ())->IsActive ())
1011 (MyIterator.Value ())->Activate ();
1013 // MyIterator.Next () is located on the next view
1019 void Visual3d_ViewManager::Deactivate () {
1022 // Deactivates all activated views
1024 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
1026 while (MyIterator.More ()) {
1027 if ((MyIterator.Value ())->IsActive ())
1028 (MyIterator.Value ())->Deactivate ();
1030 // MyIterator.Next () is located on the next view
1036 Standard_Integer Visual3d_ViewManager::MaxNumOfViews () const {
1038 // Retourne the planned of definable views for the current
1039 // Visual3d_ViewManager.
1041 (Standard_Integer ((View_IDMAX-View_IDMIN+1)/Visual3d_ViewManager::Limit ()));
1045 Handle(Graphic3d_Structure) Visual3d_ViewManager::Identification (const Standard_Integer AId) const {
1047 return (Graphic3d_StructureManager::Identification (AId));
1051 Standard_Integer Visual3d_ViewManager::Identification () const {
1053 return (Graphic3d_StructureManager::Identification ());
1057 Standard_Integer Visual3d_ViewManager::Identification (const Handle(Visual3d_View)& AView) {
1059 MyDefinedView.Add (AView);
1060 return (MyViewGenId.Next ());
1064 void Visual3d_ViewManager::UnIdentification (const Standard_Integer aViewId)
1066 MyViewGenId.Free(aViewId);
1069 void Visual3d_ViewManager::SetTransparency (const Standard_Boolean AFlag) {
1071 if (MyTransparency && AFlag) return;
1072 if (! MyTransparency && ! AFlag) return;
1074 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
1075 while (MyIterator.More ()) {
1076 (MyIterator.Value ())->SetTransparency (AFlag);
1077 // MyIterator.Next () is located on the next view
1081 MyTransparency = AFlag;
1085 Standard_Boolean Visual3d_ViewManager::Transparency () const {
1087 return (MyTransparency);
1091 void Visual3d_ViewManager::SetZBufferAuto (const Standard_Boolean AFlag) {
1093 if (MyZBufferAuto && AFlag) return;
1094 if (! MyZBufferAuto && ! AFlag) return;
1096 // if pass from False to True :
1097 // no problem, at the next view update, it
1098 // will properly ask questions to answer (SetVisualisation)
1099 // if pass from True to False :
1100 // it is necessary to modify ZBufferActivity at each view so that
1101 // zbuffer could be active only if required by context.
1102 // In this case -1 is passed so that the view ask itself the question
1103 // Note : 0 forces the desactivation, 1 forces the activation
1105 Visual3d_SetIteratorOfSetOfView MyIterator(MyDefinedView);
1106 while (MyIterator.More ()) {
1107 (MyIterator.Value ())->SetZBufferActivity (-1);
1108 // MyIterator.Next () is located on the next view
1112 MyZBufferAuto = AFlag;
1116 Standard_Boolean Visual3d_ViewManager::ZBufferAuto () const {
1118 return (MyZBufferAuto);
1122 void Visual3d_ViewManager::SetLayer (const Handle(Visual3d_Layer)& ALayer) {
1125 cout << "Visual3d_ViewManager::SetLayer\n" << flush;
1128 if (ALayer->Type () == Aspect_TOL_OVERLAY) {
1130 if (MyOverLayer.IsNull ())
1131 cout << "MyOverLayer is defined" << endl;
1133 cout << "MyOverLayer is redefined" << endl;
1135 MyOverLayer = ALayer;
1139 if (MyUnderLayer.IsNull ())
1140 cout << "MyUnderLayer is defined" << endl;
1142 cout << "MyUnderLayer is redefined" << endl;
1144 MyUnderLayer = ALayer;
1149 const Handle(Visual3d_Layer)& Visual3d_ViewManager::UnderLayer () const {
1151 return (MyUnderLayer);
1155 const Handle(Visual3d_Layer)& Visual3d_ViewManager::OverLayer () const {
1157 return (MyOverLayer);
1161 //=======================================================================
1162 //function : ChangeZLayer
1164 //=======================================================================
1166 void Visual3d_ViewManager::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
1167 const Standard_Integer theLayerId)
1169 if (!myLayerIds.Contains (theLayerId))
1172 // change display layer for structure in all views
1173 if (MyDisplayedStructure.Contains (theStructure))
1175 Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
1176 for ( ; aViewIt.More (); aViewIt.Next ())
1177 (aViewIt.Value ())->ChangeZLayer (theStructure, theLayerId);
1180 // tell graphic driver to update the structure's display layer
1181 MyGraphicDriver->ChangeZLayer (
1182 (*(Graphic3d_CStructure*)theStructure->CStructure ()), theLayerId);
1185 //=======================================================================
1186 //function : GetZLayer
1188 //=======================================================================
1190 Standard_Integer Visual3d_ViewManager::GetZLayer (const Handle(Graphic3d_Structure)& theStructure) const
1192 Graphic3d_CStructure& aStructure =
1193 (*(Graphic3d_CStructure*)theStructure->CStructure ());
1195 return MyGraphicDriver->GetZLayer (aStructure);
1198 //=======================================================================
1199 //function : AddZLayer
1201 //=======================================================================
1203 Standard_Boolean Visual3d_ViewManager::AddZLayer (Standard_Integer& theLayerId)
1208 theLayerId = getZLayerGenId ().Next ();
1209 myLayerIds.Add (theLayerId);
1210 myLayerSeq.Append (theLayerId);
1212 catch (Aspect_IdentDefinitionError)
1214 // new index can't be generated
1215 return Standard_False;
1218 // tell all managed views to remove display layers
1219 Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
1220 for ( ; aViewIt.More (); aViewIt.Next ())
1221 (aViewIt.Value ())->AddZLayer (theLayerId);
1223 return Standard_True;
1226 //=======================================================================
1227 //function : RemoveZLayer
1229 //=======================================================================
1231 Standard_Boolean Visual3d_ViewManager::RemoveZLayer (const Standard_Integer theLayerId)
1233 if (!myLayerIds.Contains (theLayerId) || theLayerId == 0)
1234 return Standard_False;
1236 // tell all managed views to remove display layers
1237 Visual3d_SetIteratorOfSetOfView aViewIt(MyDefinedView);
1238 for ( ; aViewIt.More (); aViewIt.Next ())
1239 (aViewIt.Value ())->RemoveZLayer (theLayerId);
1241 MyGraphicDriver->UnsetZLayer (theLayerId);
1244 for (int aIdx = 1; aIdx <= myLayerSeq.Length (); aIdx++)
1245 if (myLayerSeq(aIdx) == theLayerId)
1247 myLayerSeq.Remove (aIdx);
1251 myLayerIds.Remove (theLayerId);
1252 getZLayerGenId ().Free (theLayerId);
1254 return Standard_True;
1257 //=======================================================================
1258 //function : GetAllZLayers
1260 //=======================================================================
1262 void Visual3d_ViewManager::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
1264 theLayerSeq.Assign (myLayerSeq);
1267 //=======================================================================
1268 //function : getZLayerGenId
1270 //=======================================================================
1272 Aspect_GenId& Visual3d_ViewManager::getZLayerGenId ()
1274 static Aspect_GenId aGenId (1, IntegerLast());
1278 //=======================================================================
1279 //function : InstallZLayers
1281 //=======================================================================
1283 void Visual3d_ViewManager::InstallZLayers(const Handle(Visual3d_View)& theView) const
1285 if (!MyDefinedView.Contains (theView))
1288 // erase and insert layers iteratively to provide the same layer order as
1289 // in the view manager's sequence. This approach bases on the layer insertion
1290 // order: the new layers are always appended to the end of the list
1291 // inside of view, while layer remove operation doesn't affect the order.
1292 // Starting from second layer : no need to change the default z layer.
1293 for (Standard_Integer aSeqIdx = 2; aSeqIdx <= myLayerSeq.Length (); aSeqIdx++)
1295 Standard_Integer aLayerID = myLayerSeq.Value (aSeqIdx);
1296 theView->RemoveZLayer (aLayerID);
1297 theView->AddZLayer (aLayerID);