0023634: Eliminate Polyline and Polygon usage in drawers
[occt.git] / src / V3d / V3d_View.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 /***********************************************************************
19
20 FONCTION :
21 ----------
22 Classe V3d_View :
23
24 HISTORIQUE DES MODIFICATIONS   :
25 --------------------------------
26 00-09-92 : GG  ; Creation.
27 02-10-96 : FMN ; Suppression appel Redraw sans MustBeResized()
28 05-06-97 : FMN ; Correction FitAll()
29 30-06-97 : GG ; Correction + Optimisation de Panning(...)
30 On fait la translation + le zoom en une seule
31 operation au lieu de 2 precedemment qui etait buggee.
32 09-07-97 : FMN ; Correction FitAll() sur le Ratio
33 16-07-97 : FMN ; Correction FitAll() sur le calcul de la Box
34 22-07-97 : FMN ; Ajout mode RetainMode pour le Transient
35 15-12-97 : FMN ; Ajout texture mapping
36 17-12-97 : FMN ; CTS19129 Correction FitAll() multiple
37 18-12-97 : FMN ; Ajout mode Ajout
38 24-12-97 : FMN ; Remplacement de math par MathGra
39 24-12-97 : CQO ; BUC50037 Xw_Window -> Aspect_Window
40 31-12-97 : CAL ; Remplacement de MathGra par Array2OfReal
41 07-01-98 : CAL ; Ajout de la methode DoMapping.
42 07-01-98 : CAL ; Retrait de tous les "this->" inutiles
43 21-01-98 : CAL ; Remplacement des Window->Position () par Window->Size ()
44 27-01-98 : FMN ; PERF: OPTIMISATION LOADER (LOPTIM)
45 12-02-98 : GG  ; Reactivation du Redraw dans MustBeResized()
46 23-02-98 : FMN ; Remplacement PI par Standard_PI
47 25-02-98 : FMN ; PERF.27: Optimisation of view creation from existing view
48 11-03-98 : STT ; S3558
49 19-03-98 : FMN ; Probleme dans FitAll car la methode WNT_Window::Size(Real,Real)
50 ne marche pas.
51 08-04-98 : STT ; suppr. S3558
52 10-04-98 : CAL ; Ajout des methodes RefToPix et PixToRef
53 13-06-98 : FMN ; Probleme dans FitAll car la methode WNT_Window::Size(Real,Real)
54 ne marche pas. Contournement en appelant WNT_Window::Size(Int,Int).
55 16-08-98 : CAL ; S3892. Ajout grilles 3d.
56 09-09-98 : CAL ; S3892. Generalisation de TrsPoint.
57 24-09-98 : CAL ; Ajout d'un parametre a V3d_View::SetPlotter.
58 06-10-98 : CAL ; Ajout d'un TIMER si CSF_GraphicTimer est definie.
59 16-10-98 : CAL ; Retrait d'un TIMER si CSF_GraphicTimer est definie.
60 06-11-98 : CAL ; PRO ?????. Probleme dans ZFitAll si un point dans la vue.
61 13-06-98 : FMN ; PRO14896: Correction sur la gestion de la perspective (cf Programming Guinde)
62 29-OCT-98 : DCB : Adding ScreenCopy () method.
63 22-12-98 : FMN ; Rename CSF_WALKTHROW en CSF_WALKTHROUGH
64 10-11-99 : GG ; PRO19603 Add Redraw( area ) method
65 IMP130100 : GG
66 -> Don't increase too much the ZSize.
67 -> Initialize correctly the Z clipping and D cueing
68 planes.
69 IMP100701 : SZV ; Add ToPixMap() method
70
71 REMARQUES :
72 -----------
73 About  FitAll() multiple. This probleme is caused by missing
74 precision of transformation matrices. If it is supposed that
75 projection is made in the plane (U,V), there is a difference
76 after several Zoom - compared to the exact value (cf ZoomX).
77 Don't forget that the matrices work in float and not in double.
78 To solve the problem (for lack of a better solution) I make 2 passes.
79
80 ************************************************************************/
81
82 //GER61351  //GG_15/12/99 Add SetBackgroundColor() and BackgroundColor() methods
83
84
85 #define IMP240100 //GG
86 //      -> Remove PixToRef() method ,use
87 //        instead the equivalent Convert() method.
88 //      -> Rename RefToPix() to Convert() method.
89 //            -> Remove the grid computation in Convert()
90 //        method. Use instead the NEW ConvertToGrid() methods.
91 //        Reason is the Convert() method is call by
92 //        StdSelect_ViewSelector3d_Pick() from
93 //        AIS_InteractiveContext() and it's not possible
94 //        to select an object vertex when the grid is active!
95 //      -> Remove grid echo marker definition
96 //        (reported in the viewer)
97 //      -> Add SetProjModel() methods.
98
99 #define G003    //EUG 04-10-99
100 //      -> computed mode management
101 //         Add SetComputedMode(..) method
102 //      -> animation mode management
103 //         Add SetAnimationMode()
104 //      -> backfacing management
105 //         Add SetBackFacingModel() method
106
107 #define G004    //VKH 15-11-99
108 //      -> Add Dump() methods
109 //      -> GG 07/03/00 Use the new MMSize()
110 //         method from Aspect_Window class.
111
112 #define IMP210200       //GG Add Transparency() method
113
114 #define IMP250200 //GG With SetDepth() method, when the requested
115 //      depth is < 0.,
116 //      move the view ref point and the eye,instead
117 //      only the eye.
118
119 #define IMP020300 //GG Don't use ZFitAll in during Rotation
120 //      for perf improvment
121
122 #define IMP210600 //GG Avoid to have infinite loop when call Rotation() method
123 //      without call before StartRotation().
124 //      This problem occurs when CTRL MB3 is maintain press betwwen 2 views.
125
126 #define IMP250900 //GG Enable rotation around screen Z axis when
127 //      rotation begin far the center of the screen.
128 //      Thanks to Patrick REGINSTER (SAMTECH)
129 //      GG 21/12/00 Due to a regression on the previous specifications
130 //      this new functionnality is right now deactivated
131 //      by default (see StartRotation(...,zRotationThreshold)
132 //      method.
133
134 #define BUC60952  //GG Enable to rotate around the view axis
135 //      and the required view point
136
137 #define RIC120302 //GG Add a NEW SetWindow method which enable
138 //      to connect a graphic widget and context to OGL.
139
140 #define IMP260302 //GG To avoid conflicting in Window destructor
141 //      nullify this handle in Remove method
142
143 #define OCC280          //SAV fix for FitAll problem in the perspective view.
144
145 #define OCC1188         //SAV Added methods to set background image
146
147 /*----------------------------------------------------------------------*/
148 /*
149 * Includes
150 */
151
152 #include <Standard_TypeMismatch.hxx>
153 #include <Visual3d_ViewManager.hxx>
154 #include <Visual3d_Light.hxx>
155 #include <Visual3d_ClipPlane.hxx>
156 #include <Graphic3d_Structure.hxx>
157 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
158 #include <Graphic3d_MapOfStructure.hxx>
159 #include <Graphic3d_TextureEnv.hxx>
160 #include <Image_AlienPixMap.hxx>
161 #include <V3d.hxx>
162 #include <V3d_View.ixx>
163 #include <Viewer_BadValue.hxx>
164 #include <Standard_ShortReal.hxx>
165 #include <gp_Dir.hxx>
166 #include <TColStd_Array2OfReal.hxx>
167 #include <Visual3d_TransientManager.hxx>
168 #include <Precision.hxx>
169
170 // S3892
171 #include <Graphic3d_AspectMarker3d.hxx>
172
173 // S3603
174 #include <Aspect_GenericColorMap.hxx>
175 #include <Aspect_TypeMap.hxx>
176 #include <Aspect_WidthMap.hxx>
177 #include <Aspect_MarkMap.hxx>
178 #include <Aspect_FontMap.hxx>
179 #include <PlotMgt_ImageDriver.hxx>
180 #include <TColStd_HSequenceOfInteger.hxx>
181 #ifdef WNT
182 # include <WNT_WDriver.hxx>
183 #else
184 # include <Xw_Driver.hxx>
185 #endif
186
187 #ifdef G003
188 # define V3d_FLAG_ANIMATION     0x00000001
189 # define V3d_FLAG_DEGENERATION  0x00000002
190 # define V3d_FLAG_COMPUTATION   0x00000004
191 #endif  // G003
192
193 // Tumble
194 #include <OSD_Timer.hxx>
195 static OSD_Timer FullTimer;
196
197 // Perspective
198 #include <OSD_Environment.hxx>
199
200 /*----------------------------------------------------------------------*/
201 /*
202 * Constant
203 */
204
205 #define Zmargin 1.
206 #define DEUXPI (2. * M_PI)
207
208 /*----------------------------------------------------------------------*/
209 /*
210 * Local data definitions
211 */
212
213
214 static Standard_Real MyXwindowCenter ;
215 static Standard_Real MyYwindowCenter ;
216 static Standard_Real MyWindowWidth ;
217 static Standard_Real MyWindowHeight ;
218
219 #define LOPTIM
220 #ifndef LOPTIM
221 static Graphic3d_Vector MyXscreenAxis ;
222 static Graphic3d_Vector MyYscreenAxis ;
223 static Graphic3d_Vector MyZscreenAxis ;
224 static Graphic3d_Vector MyViewReferencePlane ;
225 static Graphic3d_Vector MyViewReferenceUp ;
226 static Graphic3d_Vector MyViewAxis ;
227 static Graphic3d_Vertex MyViewReferencePoint ;
228 static Graphic3d_Vertex MyGravityReferencePoint ;
229 static Graphic3d_Vertex MyProjReferencePoint ;
230 #else
231 static Graphic3d_Vector& _MyXscreenAxis() {
232   static Graphic3d_Vector MyXscreenAxis;
233   return MyXscreenAxis;
234 }
235 #define MyXscreenAxis _MyXscreenAxis()
236
237 static Graphic3d_Vector& _MyYscreenAxis() {
238   static Graphic3d_Vector MyYscreenAxis;
239   return MyYscreenAxis;
240 }
241 #define MyYscreenAxis _MyYscreenAxis()
242
243 static Graphic3d_Vector& _MyZscreenAxis() {
244   static Graphic3d_Vector MyZscreenAxis;
245   return MyZscreenAxis;
246 }
247 #define MyZscreenAxis _MyZscreenAxis()
248
249 static Graphic3d_Vector& _MyViewReferencePlane() {
250   static Graphic3d_Vector MyViewReferencePlane;
251   return MyViewReferencePlane;
252 }
253 #define MyViewReferencePlane _MyViewReferencePlane()
254
255 static Graphic3d_Vector& _MyViewReferenceUp() {
256   static Graphic3d_Vector MyViewReferenceUp;
257   return MyViewReferenceUp;
258 }
259 #define MyViewReferenceUp _MyViewReferenceUp()
260
261 static Graphic3d_Vector& _MyViewAxis() {
262   static Graphic3d_Vector MyViewAxis;
263   return MyViewAxis;
264 }
265 #define MyViewAxis _MyViewAxis()
266
267 static Graphic3d_Vertex& _MyViewReferencePoint() {
268   static Graphic3d_Vertex MyViewReferencePoint;
269   return MyViewReferencePoint;
270 }
271 #define MyViewReferencePoint _MyViewReferencePoint()
272
273 static Graphic3d_Vertex& _MyGravityReferencePoint() {
274   static Graphic3d_Vertex MyGravityReferencePoint;
275   return MyGravityReferencePoint;
276 }
277 #define MyGravityReferencePoint _MyGravityReferencePoint()
278
279 static Graphic3d_Vertex& _MyProjReferencePoint() {
280   static Graphic3d_Vertex MyProjReferencePoint;
281   return MyProjReferencePoint;
282 }
283 #define MyProjReferencePoint _MyProjReferencePoint()
284 #endif // LOPTIM
285
286 /*----------------------------------------------------------------------*/
287 //-Constructors
288
289 V3d_View::V3d_View(const Handle(V3d_Viewer)& VM, const V3d_TypeOfView Type ) :
290 MyType ( Type ),
291 MyViewer(VM.operator->()),
292 MyActiveLights(),
293 MyActivePlanes(),
294 MyViewContext (),
295 myActiveLightsIterator(),
296 myActivePlanesIterator(),
297 SwitchSetFront(Standard_False),
298 MyTrsf (1, 4, 1, 4),                                    // S3892
299 MyProjModel(V3d_TPM_SCREEN)
300 {
301   myImmediateUpdate = Standard_False;
302   MyView = new Visual3d_View(MyViewer->Viewer());
303
304   // { Begin to retrieve the definition from ViewContext.
305   // Step MyViewContext = MyView->Context() ;
306   // to permit MyView->SetContext to compare
307   // the old and the new context.
308   // No problem for MyViewMapping, MyViewOrientation
309   // as MyView->SetViewMapping and MyView->SetViewOrientation
310   // don't try to optimize the modifications introduced to
311   // viewmapping and vieworientation.
312
313   // Aliasing
314   if ((MyView->Context ()).AliasingIsOn ())
315     MyViewContext.SetAliasingOn ();
316   else
317     MyViewContext.SetAliasingOff ();
318
319   // DepthCueing
320   MyViewContext.SetDepthCueingBackPlane
321     ((MyView->Context ()).DepthCueingBackPlane ());
322   MyViewContext.SetDepthCueingFrontPlane
323     ((MyView->Context ()).DepthCueingFrontPlane ());
324
325   if ((MyView->Context ()).DepthCueingIsOn ())
326     MyViewContext.SetDepthCueingOn ();
327   else
328     MyViewContext.SetDepthCueingOff ();
329
330   // ZClipping
331   MyViewContext.SetZClippingBackPlane
332     ((MyView->Context ()).ZClippingBackPlane ());
333   MyViewContext.SetZClippingFrontPlane
334     ((MyView->Context ()).ZClippingFrontPlane ());
335
336   if ((MyView->Context ()).FrontZClippingIsOn ())
337     MyViewContext.SetFrontZClippingOn ();
338   else
339     MyViewContext.SetFrontZClippingOff ();
340
341   if ((MyView->Context ()).BackZClippingIsOn ())
342     MyViewContext.SetBackZClippingOn ();
343   else
344     MyViewContext.SetBackZClippingOff ();
345
346   // Visualisation and Shading Model
347   MyViewContext.SetModel ((MyView->Context ()).Model ());
348   MyViewContext.SetVisualization ((MyView->Context ()).Visualization ());
349
350   // Texture Mapping
351   MyViewContext.SetSurfaceDetail (MyView->Context ().SurfaceDetail ());
352   MyViewContext.SetTextureEnv (MyView->Context ().TextureEnv ());
353   // } End of retrieval of the definition of ViewContext.
354
355   MyViewMapping = MyView->ViewMapping() ;
356   MyViewOrientation = MyView->ViewOrientation() ;
357   MyBackground = VM->GetBackgroundColor() ;
358   MyGradientBackground = VM->GetGradientBackground() ;
359
360   SetAxis(0.,0.,0.,1.,1.,1.) ;
361   SetVisualization(VM->DefaultVisualization()) ;
362   SetShadingModel(VM->DefaultShadingModel()) ;
363   SetSurfaceDetail(VM->DefaultSurfaceDetail()) ;
364   SetTwist(0.) ;
365   SetAt(0.,0.,0.) ;
366   SetProj(VM->DefaultViewProj()) ;
367   SetSize(VM->DefaultViewSize()) ;
368   Standard_Real zsize = VM->DefaultViewSize();
369   SetZSize(2.*zsize+zsize*Zmargin) ;
370   SetZClippingDepth(0.);
371   SetZClippingWidth(zsize);
372   SetZCueingDepth(0.);
373   SetZCueingWidth(zsize);
374   SetDepth(VM->DefaultViewSize()/2.) ;
375   SetCenter(0.,0.) ;
376   SetViewMappingDefault();
377   VM->AddView(this) ;
378   Init();
379   myImmediateUpdate = Standard_True;
380
381   // S3892
382 #ifndef IMP240100
383 #endif  //IMP240100
384
385 #ifdef G003
386   MyAnimationFlags = 0;
387 #endif  // G003
388
389 #ifdef IMP210200
390   MyTransparencyFlag = Standard_False;
391 #endif
392 }
393
394 /*----------------------------------------------------------------------*/
395
396 V3d_View::V3d_View(const Handle(V3d_Viewer)& VM,const Handle(V3d_View)& V, const V3d_TypeOfView Type ) :
397 MyType ( Type ),
398 MyViewer(VM.operator->()),
399 MyActiveLights(),
400 MyActivePlanes(),
401 MyViewContext (),
402 myActiveLightsIterator(),
403 myActivePlanesIterator(),
404 SwitchSetFront(Standard_False),
405 MyTrsf (1, 4, 1, 4),                                    // S3892
406 MyProjModel(V3d_TPM_SCREEN)
407 {
408   Handle(Visual3d_View) FromView = V->View() ;
409
410   myImmediateUpdate = Standard_False;
411   MyView = new Visual3d_View(MyViewer->Viewer());
412
413   for (V->InitActiveLights();V->MoreActiveLights();V->NextActiveLights()){
414     MyActiveLights.Append(V->ActiveLight());}
415   for (V->InitActivePlanes();V->MoreActivePlanes();V->NextActivePlanes()){
416     MyActivePlanes.Append(V->ActivePlane());}
417
418   MyViewContext = FromView->Context() ;
419
420   MyViewMapping = FromView->ViewMapping() ;
421   MyViewOrientation = FromView->ViewOrientation() ;
422   MyBackground = FromView->Background() ;
423   MyGradientBackground = FromView->GradientBackground();
424
425   MyView->SetContext(MyViewContext) ;
426
427   SetAxis(0.,0.,0.,1.,1.,1.) ;
428   VM->AddView(this) ;
429   Init();
430   myImmediateUpdate = Standard_True;
431
432 #ifdef G003
433   MyAnimationFlags = 0;
434 #endif
435 }
436
437 /*----------------------------------------------------------------------*/
438
439 //-Methods, in order
440
441 void V3d_View::SetMagnify(const Handle(Aspect_Window)& TheWindow,
442                           const Handle(V3d_View)& aPreviousView,
443                           const Standard_Integer x1,
444                           const Standard_Integer y1,
445                           const Standard_Integer x2,
446                           const Standard_Integer y2)
447 {
448   if( !MyView->IsDefined() ) {
449     Standard_Real a,b,c,d;
450     aPreviousView->Convert(x1,y1,a,b);
451     aPreviousView->Convert(x2,y2,c,d);
452     MyView->SetWindow(TheWindow) ;
453     FitAll(TheWindow,a,b,c,d);
454     MyView->SetContext(MyViewContext) ;
455     MyView->SetViewOrientation(MyViewOrientation) ;
456     MyView->SetBackground(MyBackground) ;
457     MyViewer->SetViewOn(this) ;
458     MyWindow = TheWindow;
459     MyView->Redraw() ;
460     SetViewMappingDefault();
461   }
462 }
463
464 /*----------------------------------------------------------------------*/
465
466 void V3d_View::SetWindow(const Handle(Aspect_Window)& TheWindow)
467 {
468   Standard_MultiplyDefined_Raise_if( MyView->IsDefined(),
469     "V3d_View::SetWindow, window of view already defined");
470
471   MyView->SetWindow(TheWindow) ;
472   // AGV: Method V3d_View::SetWindow() should assign the field MyWindow before
473   // calling Redraw(). Otherwise it is impossible to call certain methods of
474   // V3d_View like Convert() inside the context of Redraw(),
475   // particularly in class NIS_View.
476   MyWindow = TheWindow;
477   // SetWindow carries out SetRatio and modifies
478   // ViewMapping and ViewMappingDefault of MyView.
479   MyViewMapping = MyView->ViewMapping() ;
480   MyView->SetContext(MyViewContext) ;
481   MyView->SetViewMapping(MyViewMapping) ;
482   MyView->SetViewOrientation(MyViewOrientation) ;
483   MyView->SetBackground(MyBackground) ;
484   MyViewer->SetViewOn(this) ;
485   MyView->Redraw() ;
486 }
487
488 // RIC120302
489 /*----------------------------------------------------------------------*/
490
491 void V3d_View::SetWindow(const Handle(Aspect_Window)&      aWindow,
492                          const Aspect_RenderingContext     aContext,
493                          const Aspect_GraphicCallbackProc& aDisplayCB,
494                          const Standard_Address            aClientData)
495 {
496   Standard_MultiplyDefined_Raise_if( MyView->IsDefined(),
497     "V3d_View::SetWindow, "
498     "window of view already defined");
499   // AGV: Method V3d_View::SetWindow() should assign the field MyWindow before
500   // calling Redraw(). Otherwise it is impossible to call certain methods of
501   // V3d_View like Convert() inside the context of Redraw(),
502   // particularly in class NIS_View.
503   MyWindow = aWindow;
504   MyView->SetWindow(aWindow, aContext, aDisplayCB, aClientData) ;
505   MyViewMapping = MyView->ViewMapping() ;
506   MyView->SetContext(MyViewContext) ;
507   MyView->SetViewMapping(MyViewMapping) ;
508   MyView->SetViewOrientation(MyViewOrientation) ;
509   MyView->SetBackground(MyBackground) ;
510   MyViewer->SetViewOn(this) ;
511   MyView->Redraw() ;
512 }
513 // RIC120302
514
515 /*----------------------------------------------------------------------*/
516
517 void V3d_View::Remove() const
518 {
519   MyViewer->DelView (this);
520   MyView->Remove();
521   Handle(Aspect_Window)& aWin = const_cast<Handle(Aspect_Window)&> (MyWindow);
522   aWin.Nullify();
523 }
524
525 /*----------------------------------------------------------------------*/
526
527 void V3d_View::Update() const
528 {
529   if( MyView->IsDefined() )  MyView->Update() ;
530 }
531
532 /*----------------------------------------------------------------------*/
533
534 void V3d_View::Redraw() const
535 {
536   if( MyView->IsDefined() ) MyView->Redraw() ;
537 }
538
539 /*----------------------------------------------------------------------*/
540
541 void V3d_View::Redraw(const Standard_Integer xc,const Standard_Integer yc,
542                       const Standard_Integer width,const Standard_Integer height) const
543 {
544   if( MyView->IsDefined() ) MyView->Redraw(xc,yc,width,height) ;
545 }
546
547 /*----------------------------------------------------------------------*/
548
549 Standard_Boolean V3d_View::IsEmpty() const
550 {
551   Standard_Boolean TheStatus = Standard_True ;
552   if( MyView->IsDefined() ) {
553     Standard_Integer Nstruct = MyView->NumberOfDisplayedStructures() ;
554     if( Nstruct > 0 ) TheStatus = Standard_False ;
555   }
556   return (TheStatus) ;
557 }
558
559 /*----------------------------------------------------------------------*/
560
561 void V3d_View::UpdateLights() const
562 {
563   MyView->SetContext(MyViewContext);
564   Update();
565 }
566
567 /*----------------------------------------------------------------------*/
568
569 void V3d_View::DoMapping()
570 {
571   if( MyView->IsDefined() ) {
572     (MyView->Window())->DoMapping() ;
573   }
574 }
575
576 /*----------------------------------------------------------------------*/
577
578 void V3d_View::MustBeResized()
579 {
580   if ( !MyLayerMgr.IsNull() )
581     MyLayerMgr->Resized();
582
583   if( MyView->IsDefined() ) {
584     MyView->Resized() ;
585     MyViewMapping = MyView->ViewMapping();
586     MyView->Redraw();
587   }
588 }
589
590 /*----------------------------------------------------------------------*/
591
592 void V3d_View::SetBackgroundColor(const Quantity_TypeOfColor Type, const Standard_Real v1, const Standard_Real v2, const Standard_Real v3)
593 {
594   Standard_Real V1 = Max( Min( v1, 1.0 ), 0.0 );
595   Standard_Real V2 = Max( Min( v2, 1.0 ), 0.0 );
596   Standard_Real V3 = Max( Min( v3, 1.0 ), 0.0 );
597
598   Quantity_Color C( V1, V2, V3, Type );
599   SetBackgroundColor( C );
600 }
601
602 /*----------------------------------------------------------------------*/
603
604 void V3d_View::SetBackgroundColor(const Quantity_Color &Color)
605 {
606   MyBackground.SetColor( Color );
607   if ( MyView->IsDefined() )
608     MyView->SetBackground( MyBackground );
609   //szv: Why?
610   if ( !MyLayerMgr.IsNull() )
611     MyLayerMgr->Resized();
612 }
613
614 /*----------------------------------------------------------------------*/
615
616 void V3d_View::SetBackgroundColor(const Quantity_NameOfColor Name)
617 {
618   Quantity_Color C( Name );
619   SetBackgroundColor( C );
620 }
621
622 /*----------------------------------------------------------------------*/
623
624 void V3d_View::SetBgGradientColors( const Quantity_Color& Color1,
625                                     const Quantity_Color& Color2,
626                                     const Aspect_GradientFillMethod FillStyle,
627                                     const Standard_Boolean status)
628 {
629   MyGradientBackground.SetColors(Color1, Color2, FillStyle);
630   if ( MyView->IsDefined() )
631     MyView->SetGradientBackground( MyGradientBackground, status );
632 }
633
634 /*----------------------------------------------------------------------*/
635
636 void V3d_View::SetBgGradientColors( const Quantity_NameOfColor Color1,
637                                     const Quantity_NameOfColor Color2,
638                                     const Aspect_GradientFillMethod FillStyle,
639                                     const Standard_Boolean status )
640 {
641   Quantity_Color C1( Color1 );
642   Quantity_Color C2( Color2 );
643   MyGradientBackground.SetColors( C1, C2, FillStyle );
644   if ( MyView->IsDefined() )
645     MyView->SetGradientBackground( MyGradientBackground, status );
646 }
647
648 /*----------------------------------------------------------------------*/
649
650 void V3d_View::SetBgGradientStyle( const Aspect_GradientFillMethod FillStyle,
651                                    const Standard_Boolean update)
652 {
653   Quantity_Color Color1, Color2;
654   MyGradientBackground.Colors( Color1, Color2 );
655   MyGradientBackground.SetColors( Color1, Color2, FillStyle );
656   if( MyView->IsDefined() )
657     MyView->SetBgGradientStyle( FillStyle, update ) ;
658 }
659
660 /*----------------------------------------------------------------------*/
661
662 void V3d_View::SetBackgroundImage( const Standard_CString FileName,
663                                    const Aspect_FillMethod FillStyle,
664                                    const Standard_Boolean update )
665 {
666 #ifdef OCC1188
667   if( MyView->IsDefined() )
668     MyView->SetBackgroundImage( FileName, FillStyle, update ) ;
669 #endif
670 }
671
672 /*----------------------------------------------------------------------*/
673
674 void V3d_View::SetBgImageStyle( const Aspect_FillMethod FillStyle,
675                                 const Standard_Boolean update )
676 {
677 #ifdef OCC1188
678   if( MyView->IsDefined() )
679     MyView->SetBgImageStyle( FillStyle, update ) ;
680 #endif
681 }
682
683 /*----------------------------------------------------------------------*/
684
685 void V3d_View::SetAxis(const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const Standard_Real Vx, const Standard_Real Vy, const Standard_Real Vz)
686 {
687   Standard_Real D,Nx = Vx,Ny = Vy,Nz = Vz ;
688
689   D = Sqrt( Vx*Vx + Vy*Vy + Vz*Vz ) ;
690   Viewer_BadValue_Raise_if ( D <= 0. , "V3d_View::SetAxis, bad axis");
691   Nx /= D ; Ny /= D ; Nz /= D ;
692   MyDefaultViewPoint.SetCoord(X,Y,Z) ;
693   MyDefaultViewAxis.SetCoord(Nx,Ny,Nz) ;
694   MyDefaultViewAxis.Normalize() ;
695 }
696
697 /*----------------------------------------------------------------------*/
698
699 void V3d_View::SetShadingModel(const V3d_TypeOfShadingModel Model)
700 {
701   MyViewContext.SetModel((Visual3d_TypeOfModel) Model) ;
702   MyView->SetContext(MyViewContext) ;
703 }
704
705 /*----------------------------------------------------------------------*/
706
707 void V3d_View::SetSurfaceDetail(const V3d_TypeOfSurfaceDetail Model)
708 {
709   MyViewContext.SetSurfaceDetail((Visual3d_TypeOfSurfaceDetail) Model) ;
710   MyView->SetContext(MyViewContext) ;
711 }
712
713 /*----------------------------------------------------------------------*/
714
715 void V3d_View::SetTextureEnv(const Handle(Graphic3d_TextureEnv)& ATexture)
716 {
717   MyViewContext.SetTextureEnv(ATexture) ;
718   MyView->SetContext(MyViewContext) ;
719 }
720
721 /*----------------------------------------------------------------------*/
722
723 void V3d_View::SetVisualization(const V3d_TypeOfVisualization Mode)
724 {
725   MyViewContext.SetVisualization((Visual3d_TypeOfVisualization) Mode);
726   MyView->SetContext(MyViewContext) ;
727 }
728
729 /*----------------------------------------------------------------------*/
730
731 void V3d_View::SetFront()
732 {
733   gp_Ax3 a = MyViewer->PrivilegedPlane();
734   Standard_Real xo,yo,zo,vx,vy,vz,xu,yu,zu;
735
736   a.Direction().Coord(vx,vy,vz);
737   a.YDirection().Coord(xu,yu,zu);
738   a.Location().Coord(xo,yo,zo);
739
740   if(SwitchSetFront)
741     MyViewOrientation.SetViewReferencePlane(Graphic3d_Vector(-vx,-vy,-vz));
742   else
743     MyViewOrientation.SetViewReferencePlane(Graphic3d_Vector(vx,vy,vz));
744
745   SwitchSetFront = !SwitchSetFront;
746
747   MyViewOrientation.SetViewReferenceUp(Graphic3d_Vector(xu,yu,zu));
748   MyViewOrientation.SetViewReferencePoint(Graphic3d_Vertex(xo,yo,zo));
749
750   MyView->SetViewOrientation(MyViewOrientation) ;
751
752   ImmediateUpdate();
753 }
754
755 /*----------------------------------------------------------------------*/
756
757 void V3d_View::Rotate(const Standard_Real ax, const Standard_Real ay, const Standard_Real az, const Standard_Boolean Start)
758 {
759   Standard_Real Ax = ax ;
760   Standard_Real Ay = ay ;
761   Standard_Real Az = az ;
762   Graphic3d_Vector Vpn,Vup ;
763   TColStd_Array2OfReal Matrix(0,3,0,3) ;
764   TColStd_Array2OfReal Rmatrix(0,3,0,3) ;
765
766   if( Ax > 0. ) while ( Ax > DEUXPI ) Ax -= DEUXPI ;
767   else if( Ax < 0. ) while ( Ax < -DEUXPI ) Ax += DEUXPI ;
768   if( Ay > 0. ) while ( Ay > DEUXPI ) Ay -= DEUXPI ;
769   else if( Ay < 0. ) while ( Ay < -DEUXPI ) Ay += DEUXPI ;
770   if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI ;
771   else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI ;
772
773   if( Start ) {
774     MyViewReferencePoint = MyViewOrientation.ViewReferencePoint() ;
775     MyViewReferencePlane = MyViewOrientation.ViewReferencePlane() ;
776     MyViewReferenceUp = MyViewOrientation.ViewReferenceUp() ;
777     if (!ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
778       MyXscreenAxis,MyYscreenAxis,MyZscreenAxis))
779       Viewer_BadValue::Raise ("V3d_View::Rotate, alignment of Eye,At,Up");
780   }
781
782   InitMatrix(Matrix) ;
783
784   if( Ax != 0. ) RotAxis(MyViewReferencePoint,MyYscreenAxis,-Ax,Matrix);
785   if( Ay != 0. ) {
786     RotAxis(MyViewReferencePoint,MyXscreenAxis,Ay,Rmatrix) ;
787     Multiply(Matrix, Rmatrix, Matrix);
788   }
789   if( Az != 0. ) {
790     RotAxis(MyViewReferencePoint,MyZscreenAxis,Az,Rmatrix) ;
791     Multiply(Matrix, Rmatrix, Matrix);
792   }
793   Vpn = TrsPoint(MyViewReferencePlane,Matrix) ;
794   MyViewOrientation.SetViewReferencePlane(Vpn) ;
795   Vup = TrsPoint(MyViewReferenceUp,Matrix) ;
796   MyViewOrientation.SetViewReferenceUp(Vup) ;
797   MyView->SetViewOrientation(MyViewOrientation) ;
798 #ifdef IMP020300
799   SetZSize(0.) ;
800 #endif
801   ImmediateUpdate();
802 }
803
804 /*----------------------------------------------------------------------*/
805
806 void V3d_View::Rotate(const Standard_Real ax, const Standard_Real ay, const Standard_Real az,
807                       const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const Standard_Boolean Start)
808 {
809   Standard_Real Ax = ax ;
810   Standard_Real Ay = ay ;
811   Standard_Real Az = az ;
812   Graphic3d_Vector Vpn,Vup ;
813   Graphic3d_Vertex Vrp ;
814   TColStd_Array2OfReal Matrix(0,3,0,3) ;
815   TColStd_Array2OfReal Rmatrix(0,3,0,3) ;
816
817   if( Ax > 0. ) while ( Ax > DEUXPI ) Ax -= DEUXPI ;
818   else if( Ax < 0. ) while ( Ax < -DEUXPI ) Ax += DEUXPI ;
819   if( Ay > 0. ) while ( Ay > DEUXPI ) Ay -= DEUXPI ;
820   else if( Ay < 0. ) while ( Ay < -DEUXPI ) Ay += DEUXPI ;
821   if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI ;
822   else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI ;
823
824   if( Start ) {
825     MyGravityReferencePoint.SetCoord(X,Y,Z) ;
826     MyViewReferencePoint = MyViewOrientation.ViewReferencePoint() ;
827     MyViewReferencePlane = MyViewOrientation.ViewReferencePlane() ;
828     MyViewReferenceUp = MyViewOrientation.ViewReferenceUp() ;
829     if (!ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
830       MyXscreenAxis,MyYscreenAxis,MyZscreenAxis))
831       Viewer_BadValue::Raise ("V3d_View::Rotate, alignment of Eye,At,Up");
832   }
833
834   InitMatrix(Matrix) ;
835
836   if( Ax != 0. ) RotAxis(MyGravityReferencePoint,MyYscreenAxis,-Ax,Matrix);
837   if( Ay != 0. ) {
838     RotAxis(MyGravityReferencePoint,MyXscreenAxis,Ay,Rmatrix) ;
839     Multiply(Matrix, Rmatrix, Matrix);
840   }
841   if( Az != 0. ) {
842     RotAxis(MyGravityReferencePoint,MyZscreenAxis,Az,Rmatrix) ;
843     Multiply(Matrix, Rmatrix, Matrix);
844   }
845   Vrp = TrsPoint(MyViewReferencePoint,Matrix) ;
846   MyViewOrientation.SetViewReferencePoint(Vrp) ;
847   Vpn = TrsPoint(MyViewReferencePlane,Matrix) ;
848   MyViewOrientation.SetViewReferencePlane(Vpn) ;
849   Vup = TrsPoint(MyViewReferenceUp,Matrix) ;
850   MyViewOrientation.SetViewReferenceUp(Vup) ;
851   MyView->SetViewOrientation(MyViewOrientation) ;
852 #ifdef IMP020300
853   SetZSize(0.) ;
854 #endif
855   ImmediateUpdate();
856 }
857
858 /*----------------------------------------------------------------------*/
859
860 void V3d_View::Rotate(const V3d_TypeOfAxe Axe, const Standard_Real angle, const Standard_Boolean Start)
861 {
862   switch (Axe) {
863   case V3d_X :
864     Rotate(angle,0.,0.,Start);
865     break ;
866   case V3d_Y :
867     Rotate(0.,angle,0.,Start);
868     break ;
869   case V3d_Z :
870     Rotate(0.,0.,angle,Start);
871     break ;
872   }
873 }
874
875 /*----------------------------------------------------------------------*/
876
877 void V3d_View::Rotate(const V3d_TypeOfAxe Axe, const Standard_Real angle,
878                       const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const Standard_Boolean Start)
879 {
880   Standard_Real Angle = angle ;
881   Graphic3d_Vector Vpn,Vup ;
882   Graphic3d_Vertex Vrp ;
883   TColStd_Array2OfReal Matrix(0,3,0,3) ;
884
885   if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
886   else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
887
888   if( Start ) {
889     MyGravityReferencePoint.SetCoord(X,Y,Z) ;
890     MyViewReferencePoint = MyViewOrientation.ViewReferencePoint() ;
891     MyViewReferencePlane = MyViewOrientation.ViewReferencePlane() ;
892     MyViewReferenceUp = MyViewOrientation.ViewReferenceUp() ;
893     switch (Axe) {
894     case V3d_X :
895       MyViewAxis.SetCoord(1.,0.,0.) ;
896       break ;
897     case V3d_Y :
898       MyViewAxis.SetCoord(0.,1.,0.) ;
899       break ;
900     case V3d_Z :
901       MyViewAxis.SetCoord(0.,0.,1.) ;
902       break ;
903     }
904   }
905
906   RotAxis(MyGravityReferencePoint,MyViewAxis,Angle,Matrix) ;
907   Vrp = TrsPoint(MyViewReferencePoint,Matrix) ;
908   MyViewOrientation.SetViewReferencePoint(Vrp) ;
909   Vpn = TrsPoint(MyViewReferencePlane,Matrix) ;
910   MyViewOrientation.SetViewReferencePlane(Vpn) ;
911   Vup = TrsPoint(MyViewReferenceUp,Matrix) ;
912   MyViewOrientation.SetViewReferenceUp(Vup) ;
913   MyView->SetViewOrientation(MyViewOrientation) ;
914 #ifdef IMP020300
915   SetZSize(0.) ;
916 #endif
917   ImmediateUpdate();
918 }
919
920 /*----------------------------------------------------------------------*/
921
922 void V3d_View::Rotate(const Standard_Real angle, const Standard_Boolean Start)
923 {
924   Standard_Real Angle = angle ;
925   Graphic3d_Vector Vpn,Vup ;
926   TColStd_Array2OfReal Matrix(0,3,0,3) ;
927
928   if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
929   else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
930
931   if( Start ) {
932     MyViewReferencePoint = MyViewOrientation.ViewReferencePoint() ;
933     MyViewReferencePlane = MyViewOrientation.ViewReferencePlane() ;
934     MyViewReferenceUp = MyViewOrientation.ViewReferenceUp() ;
935   }
936
937   RotAxis(MyDefaultViewPoint,MyDefaultViewAxis,Angle,Matrix) ;
938 #ifdef BUC60952
939   Graphic3d_Vertex Vrp ;
940   Vrp = TrsPoint(MyViewReferencePoint,Matrix) ;
941   MyViewOrientation.SetViewReferencePoint(Vrp) ;
942 #endif
943   Vpn = TrsPoint(MyViewReferencePlane,Matrix) ;
944   MyViewOrientation.SetViewReferencePlane(Vpn) ;
945   Vup = TrsPoint(MyViewReferenceUp,Matrix) ;
946   MyViewOrientation.SetViewReferenceUp(Vup) ;
947   MyView->SetViewOrientation(MyViewOrientation) ;
948 #ifdef IMP020300
949   SetZSize(0.) ;
950 #endif
951   ImmediateUpdate();
952 }
953
954 /*----------------------------------------------------------------------*/
955
956 void V3d_View::Turn(const Standard_Real ax, const Standard_Real ay, const Standard_Real az, const Standard_Boolean Start)
957 {
958   Standard_Real Ax = ax ;
959   Standard_Real Ay = ay ;
960   Standard_Real Az = az ;
961   Graphic3d_Vertex Vrp,Eye ;
962   Graphic3d_Vector Vpn,Vup ;
963   Standard_Real Xrp,Yrp,Zrp,Xpn,Ypn,Zpn,Xat,Yat,Zat,Xeye,Yeye,Zeye ;
964   TColStd_Array2OfReal Matrix(0,3,0,3) ;
965   TColStd_Array2OfReal Rmatrix(0,3,0,3) ;
966
967   if( Ax > 0. ) while ( Ax > DEUXPI ) Ax -= DEUXPI ;
968   else if( Ax < 0. ) while ( Ax < -DEUXPI ) Ax += DEUXPI ;
969   if( Ay > 0. ) while ( Ay > DEUXPI ) Ay -= DEUXPI ;
970   else if( Ay < 0. ) while ( Ay < -DEUXPI ) Ay += DEUXPI ;
971   if( Az > 0. ) while ( Az > DEUXPI ) Az -= DEUXPI ;
972   else if( Az < 0. ) while ( Az < -DEUXPI ) Az += DEUXPI ;
973
974   if( Start ) {
975     MyProjReferencePoint = MyViewMapping.ProjectionReferencePoint();
976     MyViewReferencePoint = MyViewOrientation.ViewReferencePoint() ;
977     MyViewReferencePlane = MyViewOrientation.ViewReferencePlane() ;
978     MyViewReferenceUp = MyViewOrientation.ViewReferenceUp() ;
979     if (!ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
980       MyXscreenAxis,MyYscreenAxis,MyZscreenAxis))
981       Viewer_BadValue::Raise ("V3d_View::Turn, alignment of Eye,At,Up");
982   }
983
984   InitMatrix(Matrix) ;
985   MyProjReferencePoint.Coord(Xrp,Yrp,Zrp) ;
986   MyViewReferencePoint.Coord(Xat,Yat,Zat) ;
987   MyViewReferencePlane.Coord(Xpn,Ypn,Zpn) ;
988   Xeye = Zrp*Xpn + Xat ; Yeye = Zrp*Ypn + Yat ; Zeye = Zrp*Zpn + Zat ;
989   Eye.SetCoord(Xeye,Yeye,Zeye) ;
990   if( Ax != 0. ) RotAxis(Eye,MyYscreenAxis,-Ax,Matrix) ;
991   if( Ay != 0. ) {
992     RotAxis(Eye,MyXscreenAxis,Ay,Rmatrix) ;
993     Multiply(Matrix, Rmatrix, Matrix);
994   }
995   if( Az != 0. ) {
996     RotAxis(Eye,MyZscreenAxis,Az,Rmatrix) ;
997     Multiply(Matrix, Rmatrix, Matrix);
998   }
999   Vrp = TrsPoint(MyViewReferencePoint,Matrix) ;
1000   MyViewOrientation.SetViewReferencePoint(Vrp) ;
1001   Vpn = TrsPoint(MyViewReferencePlane,Matrix) ;
1002   MyViewOrientation.SetViewReferenceUp(Vpn) ;
1003   Vup = TrsPoint(MyViewReferenceUp,Matrix) ;
1004   MyViewOrientation.SetViewReferenceUp(Vup) ;
1005   MyView->SetViewOrientation(MyViewOrientation) ;
1006 #ifdef IMP020300
1007   SetZSize(0.) ;
1008 #else
1009   // Check ZClipping planes
1010   Standard_Real Zmax ;
1011   Vrp.Coord(Xat,Yat,Zat) ;
1012   Zmax = Sqrt( Xat*Xat + Yat*Yat + Zat*Zat) ;
1013   if( Zmax > MyViewMapping.FrontPlaneDistance() &&
1014     MyProjModel == V3d_TPM_SCREEN ) {
1015       SetZSize(2.*Zmax+Zmax*Zmargin) ;
1016     }
1017 #endif
1018     ImmediateUpdate();
1019 }
1020
1021 /*----------------------------------------------------------------------*/
1022
1023 void V3d_View::Turn(const V3d_TypeOfAxe Axe, const Standard_Real angle, const Standard_Boolean Start)
1024 {
1025   switch (Axe) {
1026   case V3d_X :
1027     Turn(angle,0.,0.,Start);
1028     break ;
1029   case V3d_Y :
1030     Turn(0.,angle,0.,Start);
1031     break ;
1032   case V3d_Z :
1033     Turn(0.,0.,angle,Start);
1034     break ;
1035   }
1036 }
1037
1038 void V3d_View::Turn(const Standard_Real angle, const Standard_Boolean Start)
1039 {
1040   Standard_Real Angle = angle ;
1041   Graphic3d_Vertex Vrp,Eye ;
1042   Graphic3d_Vector Vpn,Vup ;
1043   Standard_Real Xrp,Yrp,Zrp,Xpn,Ypn,Zpn,Xat,Yat,Zat,Xeye,Yeye,Zeye ;
1044   TColStd_Array2OfReal Matrix(0,3,0,3) ;
1045
1046   if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
1047   else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
1048
1049   if( Start ) {
1050     MyProjReferencePoint = MyViewMapping.ProjectionReferencePoint();
1051     MyViewReferencePoint = MyViewOrientation.ViewReferencePoint() ;
1052     MyViewReferencePlane = MyViewOrientation.ViewReferencePlane() ;
1053     MyViewReferenceUp = MyViewOrientation.ViewReferenceUp() ;
1054   }
1055   MyProjReferencePoint.Coord(Xrp,Yrp,Zrp) ;
1056   MyViewReferencePoint.Coord(Xat,Yat,Zat) ;
1057   MyViewReferencePlane.Coord(Xpn,Ypn,Zpn) ;
1058   Xeye = Zrp*Xpn + Xat ; Yeye = Zrp*Ypn + Yat ; Zeye = Zrp*Zpn + Zat ;
1059   Eye.SetCoord(Xeye,Yeye,Zeye) ;
1060   RotAxis(Eye,MyDefaultViewAxis,Angle,Matrix) ;
1061   Vrp = TrsPoint(MyViewReferencePoint,Matrix) ;
1062   MyViewOrientation.SetViewReferencePoint(Vrp) ;
1063   Vpn = TrsPoint(MyViewReferencePlane,Matrix) ;
1064   MyViewOrientation.SetViewReferencePlane(Vpn) ;
1065   Vup = TrsPoint(MyViewReferenceUp,Matrix) ;
1066   MyViewOrientation.SetViewReferenceUp(Vup) ;
1067   MyView->SetViewOrientation(MyViewOrientation) ;
1068 #ifdef IMP020300
1069   SetZSize(0.) ;
1070 #else
1071   // Check ZClipping planes
1072   Standard_Real Zmax ;
1073   Vrp.Coord(Xat,Yat,Zat) ;
1074   Zmax = Sqrt( Xat*Xat + Yat*Yat + Zat*Zat ) ;
1075   if( Zmax > MyViewMapping.FrontPlaneDistance() &&
1076     MyProjModel == V3d_TPM_SCREEN ) {
1077       SetZSize(2.*Zmax+Zmax*Zmargin) ;
1078     }
1079 #endif
1080     ImmediateUpdate();
1081 }
1082
1083 void V3d_View::SetTwist(const Standard_Real angle)
1084 {
1085   Standard_Real Angle = angle ;
1086   TColStd_Array2OfReal Matrix(0,3,0,3) ;
1087   Standard_Boolean TheStatus ;
1088
1089   if( Angle > 0. ) while ( Angle > DEUXPI ) Angle -= DEUXPI ;
1090   else if( Angle < 0. ) while ( Angle < -DEUXPI ) Angle += DEUXPI ;
1091
1092   MyViewReferencePlane = MyViewOrientation.ViewReferencePlane() ;
1093   MyViewReferenceUp.SetCoord(0.,0.,1.) ;
1094   TheStatus = ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
1095     MyXscreenAxis,MyYscreenAxis,MyZscreenAxis) ;
1096   if( !TheStatus ) {
1097     MyViewReferenceUp.SetCoord(0.,1.,0.) ;
1098     TheStatus = ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
1099       MyXscreenAxis,MyYscreenAxis,MyZscreenAxis) ;
1100   }
1101   if( !TheStatus ) {
1102     MyViewReferenceUp.SetCoord(1.,0.,0.) ;
1103     TheStatus = ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
1104       MyXscreenAxis,MyYscreenAxis,MyZscreenAxis) ;
1105   }
1106
1107   Viewer_BadValue_Raise_if( !TheStatus,"V3d_ViewSetTwist, alignment of Eye,At,Up,");
1108
1109   MyViewReferencePoint = MyViewOrientation.ViewReferencePoint() ;
1110   RotAxis(MyViewReferencePoint,MyZscreenAxis,Angle,Matrix) ;
1111   MyViewReferenceUp = TrsPoint(MyYscreenAxis,Matrix) ;
1112   MyViewOrientation.SetViewReferenceUp(MyViewReferenceUp) ;
1113   MyView->SetViewOrientation(MyViewOrientation) ;
1114   ImmediateUpdate();
1115 }
1116
1117 #ifdef IMP240100
1118 void V3d_View::SetProjModel( const V3d_TypeOfProjectionModel aModel )
1119 {
1120   MyProjModel = aModel;
1121 }
1122
1123 V3d_TypeOfProjectionModel V3d_View::ProjModel() const
1124 {
1125   return MyProjModel;
1126 }
1127 #endif
1128
1129 void V3d_View::SetEye(const Standard_Real X,const Standard_Real Y,const Standard_Real Z)
1130 {
1131   Standard_Real Angle,Xat,Yat,Zat,Xrp,Yrp,Zrp,Xpn,Ypn,Zpn ;
1132   Angle = Twist() ;
1133   MyProjReferencePoint = MyViewMapping.ProjectionReferencePoint() ;
1134   MyProjReferencePoint.Coord(Xrp,Yrp,Zrp) ;
1135   MyViewReferencePoint = MyViewOrientation.ViewReferencePoint() ;
1136   MyViewReferencePoint.Coord(Xat,Yat,Zat) ;
1137   MyViewReferenceUp = MyViewOrientation.ViewReferenceUp() ;
1138   Xpn = X - Xat ; Ypn = Y - Yat ; Zpn = Z - Zat ;
1139   Zrp = Sqrt(Xpn*Xpn + Ypn*Ypn + Zpn*Zpn) ;
1140   Viewer_BadValue_Raise_if( Zrp <= 0. , "V3d_View::SetEye:: Eye,At are Confused");
1141
1142   Xpn /= Zrp ; Ypn /= Zrp ; Zpn /= Zrp ;
1143   MyViewReferencePlane.SetCoord(Xpn,Ypn,Zpn) ;
1144   MyViewOrientation.SetViewReferencePlane(MyViewReferencePlane) ;
1145   MyProjReferencePoint.SetCoord(Xrp,Yrp,Zrp) ;
1146   MyViewMapping.SetProjectionReferencePoint(MyProjReferencePoint);
1147
1148   Standard_Boolean update = myImmediateUpdate;
1149   myImmediateUpdate = Standard_False;
1150   if( MyProjModel == V3d_TPM_WALKTHROUGH ) {
1151     //FMN desactivation temp SetTwist(Angle) ;
1152     // Set infos.
1153     MyView->SetViewOrientation(MyViewOrientation) ;
1154     MyView->SetViewMapping(MyViewMapping) ;
1155   } else {
1156     SetTwist(Angle) ;
1157   }
1158 #ifdef IMP020300
1159   SetZSize(0.) ;
1160 #else
1161   // Check ZClipping planes
1162   Standard_Real Zmax = Sqrt( X*X + Y*Y + Z*Z ) ;
1163   if( Zmax > MyViewMapping.FrontPlaneDistance() &&
1164     MyProjModel == V3d_TPM_SCREEN ) {
1165       SetZSize(2.*Zmax+Zmax*Zmargin) ;
1166     } else {
1167       if( MyType == V3d_PERSPECTIVE ) SetFocale(focale) ;
1168       MyView->SetViewMapping(MyViewMapping);
1169     }
1170 #endif
1171     myImmediateUpdate = update;
1172     ImmediateUpdate();
1173 }
1174
1175 void V3d_View::SetDepth(const Standard_Real Depth)
1176 {
1177   Standard_Real Xrp,Yrp,Zrp ;
1178 #ifdef IMP250200
1179   Viewer_BadValue_Raise_if( Depth == 0. ,"V3d_View::SetDepth, bad depth");
1180 #else
1181   Viewer_BadValue_Raise_if( Depth <= 0. ,"V3d_View::SetDepth, bad depth");
1182 #endif
1183
1184   MyViewReferencePoint = MyViewOrientation.ViewReferencePoint() ;
1185   MyViewReferencePlane = MyViewOrientation.ViewReferencePlane() ;
1186   MyProjReferencePoint = MyViewMapping.ProjectionReferencePoint() ;
1187   MyProjReferencePoint.Coord(Xrp,Yrp,Zrp) ;
1188 #ifdef IMP250200
1189   if( Depth > 0. )
1190 #endif
1191   {
1192     Zrp = Depth;
1193     MyProjReferencePoint.SetCoord(Xrp,Yrp,Zrp) ;
1194     MyViewMapping.SetProjectionReferencePoint(MyProjReferencePoint);
1195   }
1196
1197   // Check ZClipping planes
1198   Standard_Real Xat,Yat,Zat,Xpn,Ypn,Zpn,Xeye,Yeye,Zeye ;
1199   MyViewReferencePoint.Coord(Xat,Yat,Zat) ;
1200   MyViewReferencePlane.Coord(Xpn,Ypn,Zpn) ;
1201 #ifdef IMP250200
1202   if( Depth < 0. ) {  // Move the view ref point instead of the eye.
1203     Xeye = Xpn*Zrp + Xat ; Yeye = Ypn*Zrp + Yat ; Zeye = Zpn*Zrp + Zat ;
1204     Zrp = Abs(Depth) ;
1205     Xat = Xeye - Xpn*Zrp ; Yat = Yeye - Ypn*Zrp ; Zat = Zeye - Zpn*Zrp;
1206     MyViewReferencePoint.SetCoord(Xat,Yat,Zat) ;
1207     MyProjReferencePoint.SetCoord(Xrp,Yrp,Zrp) ;
1208     MyViewOrientation.SetViewReferencePoint(MyViewReferencePoint) ;
1209     MyView->SetViewOrientation(MyViewOrientation);
1210   }
1211 #endif
1212 #ifdef IMP020300
1213   MyView->SetViewMapping(MyViewMapping) ;
1214   SetZSize(0.) ;
1215 #else
1216   Xeye = Xpn*Zrp + Xat ; Yeye = Ypn*Zrp + Yat ; Zeye = Zpn*Zrp + Zat ;
1217   Standard_Real Zmax = Sqrt( Xeye*Xeye + Yeye*Yeye + Zeye*Zeye );
1218
1219   if( Zmax > MyViewMapping.FrontPlaneDistance() &&
1220     MyProjModel == V3d_TPM_SCREEN ) {
1221       SetZSize(2.*Zmax+Zmax*Zmargin) ;
1222     } else {
1223       if( MyType == V3d_PERSPECTIVE ) SetFocale(focale) ;
1224 #ifdef IMP250200
1225       if( Depth > 0. )
1226 #endif
1227         MyView->SetViewMapping(MyViewMapping) ;
1228     }
1229 #endif
1230
1231     ImmediateUpdate();
1232 }
1233
1234
1235 void V3d_View::SetProj( const Standard_Real Vx,const Standard_Real Vy, const Standard_Real Vz )
1236 {
1237   Standard_Real Angle ;
1238
1239   Viewer_BadValue_Raise_if( Sqrt(Vx*Vx + Vy*Vy + Vz*Vz) <= 0.,
1240     "V3d_View::SetProj, null projection vector");
1241
1242   Angle = Twist() ;
1243   MyViewReferencePlane.SetCoord(Vx,Vy,Vz) ;
1244   MyViewReferencePlane.Normalize() ;
1245   MyViewOrientation.SetViewReferencePlane(MyViewReferencePlane) ;
1246   Standard_Boolean update = myImmediateUpdate;
1247   myImmediateUpdate = Standard_False;
1248   if( MyProjModel == V3d_TPM_SCREEN ) SetTwist(Angle) ;
1249 #ifdef IMP020300
1250   SetZSize(0.) ;
1251 #endif
1252   myImmediateUpdate = update;
1253   ImmediateUpdate();
1254 }
1255
1256 void V3d_View::SetProj( const V3d_TypeOfOrientation Orientation )
1257 {
1258   MyViewReferencePlane = V3d::GetProjAxis(Orientation) ;
1259   MyViewOrientation.SetViewReferencePlane(MyViewReferencePlane) ;
1260   // MSV 14.03.2007: reset ViewReferencePoint to debug LH3D14955
1261   MyViewOrientation.SetViewReferencePoint(Graphic3d_Vertex (0.0f, 0.0f, 0.0f));
1262   Standard_Real Xpn=0;
1263   Standard_Real Ypn=0;
1264   Standard_Real Zpn=0;
1265
1266   switch (Orientation) {
1267   case V3d_Zpos :
1268     Ypn = 1. ;
1269     break ;
1270   case V3d_Zneg :
1271     Ypn = -1. ;
1272     break ;
1273   default:
1274     Zpn = 1.;
1275   }
1276   SetUp(Xpn,Ypn,Zpn);
1277 #ifdef IMP020300
1278   SetZSize(0.) ;
1279 #endif
1280   ImmediateUpdate();
1281 }
1282
1283 void V3d_View::SetAt(const Standard_Real X,const Standard_Real Y,const Standard_Real Z)
1284 {
1285   Standard_Real Angle,Xrp,Yrp,Zrp,Xpn,Ypn,Zpn,Xat,Yat,Zat ;
1286   Standard_Real Xeye,Yeye,Zeye ;
1287
1288   Angle = Twist() ;
1289   MyProjReferencePoint = MyViewMapping.ProjectionReferencePoint() ;
1290   MyProjReferencePoint.Coord(Xrp,Yrp,Zrp) ;
1291   MyViewReferencePlane = MyViewOrientation.ViewReferencePlane() ;
1292   MyViewReferencePlane.Coord(Xpn,Ypn,Zpn) ;
1293   MyViewReferencePoint = MyViewOrientation.ViewReferencePoint() ;
1294   MyViewReferencePoint.Coord(Xat,Yat,Zat) ;
1295   Xeye = Zrp*Xpn + Xat ; Yeye = Zrp*Ypn + Yat ; Zeye = Zrp*Zpn + Zat ;
1296   Xpn = Xeye - X ; Ypn = Yeye - Y ; Zpn = Zeye - Z ;
1297   Zrp = Sqrt(Xpn*Xpn + Ypn*Ypn + Zpn*Zpn) ;
1298   Viewer_BadValue_Raise_if( Zrp <= 0.,
1299     "V3d_View::SetAt, Eye,At are Confused");
1300
1301   Xpn /= Zrp ; Ypn /= Zrp ; Zpn /= Zrp ;
1302   MyViewReferencePoint.SetCoord(X,Y,Z) ;
1303   MyViewOrientation.SetViewReferencePoint(MyViewReferencePoint) ;
1304   MyViewReferencePlane.SetCoord(Xpn,Ypn,Zpn) ;
1305   MyViewOrientation.SetViewReferencePlane(MyViewReferencePlane) ;
1306   MyProjReferencePoint.SetCoord(Xrp,Yrp,Zrp) ;
1307   MyViewMapping.SetProjectionReferencePoint(MyProjReferencePoint);
1308   Standard_Boolean update = myImmediateUpdate;
1309   myImmediateUpdate = Standard_False;
1310   if( MyProjModel == V3d_TPM_WALKTHROUGH ) {
1311     //FMN desactivation temp SetTwist(Angle) ;
1312     // Set infos.
1313     MyView->SetViewOrientation(MyViewOrientation) ;
1314     MyView->SetViewMapping(MyViewMapping) ;
1315   } else {
1316     SetTwist(Angle) ;
1317   }
1318 #ifdef IMP020300
1319   MyView->SetViewMapping(MyViewMapping);
1320   SetZSize(0.) ;
1321 #else
1322   ImmediateUpdate();
1323   // Check ZClipping planes
1324   Standard_Real Zmax = Sqrt( X*X + Y*Y + Z*Z ) ;
1325   if( Zmax > MyViewMapping.FrontPlaneDistance() &&
1326     MyProjModel == V3d_TPM_SCREEN ) {
1327       SetZSize(2.*Zmax+Zmax*Zmargin) ;
1328     } else {
1329       if( MyType == V3d_PERSPECTIVE ) SetFocale(focale) ;
1330       MyView->SetViewMapping(MyViewMapping);
1331     }
1332 #endif
1333     myImmediateUpdate = update;
1334     ImmediateUpdate();
1335 }
1336
1337 void V3d_View::SetUp(const Standard_Real Vx,const Standard_Real Vy,const Standard_Real Vz)
1338 {
1339   Standard_Boolean TheStatus ;
1340   Viewer_BadValue_Raise_if( Sqrt(Vx*Vx + Vy*Vy + Vz*Vz) <= 0. ,
1341     "V3d_View::SetUp, nullUp vector");
1342
1343   MyViewReferencePlane = MyViewOrientation.ViewReferencePlane() ;
1344   MyViewReferenceUp.SetCoord(Vx,Vy,Vz) ;
1345   MyViewReferenceUp.Normalize() ;
1346   TheStatus = ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
1347     MyXscreenAxis,MyYscreenAxis,MyZscreenAxis) ;
1348   if( !TheStatus ) {
1349     MyViewReferenceUp.SetCoord(0.,0.,1.) ;
1350     TheStatus = ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
1351       MyXscreenAxis,MyYscreenAxis,MyZscreenAxis) ;
1352   }
1353   if( !TheStatus ) {
1354     MyViewReferenceUp.SetCoord(0.,1.,0.) ;
1355     TheStatus = ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
1356       MyXscreenAxis,MyYscreenAxis,MyZscreenAxis) ;
1357   }
1358   if( !TheStatus ) {
1359     MyViewReferenceUp.SetCoord(1.,0.,0.) ;
1360     TheStatus = ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
1361       MyXscreenAxis,MyYscreenAxis,MyZscreenAxis) ;
1362   }
1363   Viewer_BadValue_Raise_if( !TheStatus,"V3d_View::Setup, alignment of Eye,At,Up");
1364
1365   MyViewReferenceUp = MyYscreenAxis ;
1366   MyViewOrientation.SetViewReferenceUp(MyViewReferenceUp) ;
1367   MyView->SetViewOrientation(MyViewOrientation) ;
1368   ImmediateUpdate();
1369 }
1370
1371 void V3d_View::SetUp( const V3d_TypeOfOrientation Orientation )
1372 {
1373   Standard_Boolean TheStatus ;
1374
1375   MyViewReferenceUp = V3d::GetProjAxis(Orientation) ;
1376   MyViewReferencePlane = MyViewOrientation.ViewReferencePlane() ;
1377   TheStatus = ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
1378     MyXscreenAxis,MyYscreenAxis,MyZscreenAxis) ;
1379   if( !TheStatus ) {
1380     MyViewReferenceUp.SetCoord(0.,0.,1.) ;
1381     TheStatus = ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
1382       MyXscreenAxis,MyYscreenAxis,MyZscreenAxis) ;
1383   }
1384   if( !TheStatus ) {
1385     MyViewReferenceUp.SetCoord(0.,1.,0.) ;
1386     TheStatus = ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
1387       MyXscreenAxis,MyYscreenAxis,MyZscreenAxis) ;
1388   }
1389   if( !TheStatus ) {
1390     MyViewReferenceUp.SetCoord(1.,0.,0.) ;
1391     TheStatus = ScreenAxis(MyViewReferencePlane,MyViewReferenceUp,
1392       MyXscreenAxis,MyYscreenAxis,MyZscreenAxis) ;
1393   }
1394   Viewer_BadValue_Raise_if( !TheStatus, "V3d_View::SetUp, alignment of Eye,At,Up");
1395
1396   MyViewReferenceUp = MyYscreenAxis ;
1397   MyViewOrientation.SetViewReferenceUp(MyViewReferenceUp) ;
1398   MyView->SetViewOrientation(MyViewOrientation) ;
1399   ImmediateUpdate();
1400 }
1401
1402 void V3d_View::SetViewOrientation(const Visual3d_ViewOrientation& VO)
1403 {
1404   MyViewOrientation = VO;
1405   MyView->SetViewOrientation(MyViewOrientation) ;
1406   ImmediateUpdate();
1407 }
1408
1409 void V3d_View::SetViewOrientationDefault()
1410 {
1411   MyView->SetViewOrientation(MyViewOrientation) ;
1412   MyView->SetViewOrientationDefault() ;
1413   ImmediateUpdate();
1414 }
1415
1416 void V3d_View::ResetViewOrientation()
1417 {
1418   MyView->ViewOrientationReset() ;
1419   MyViewOrientation = MyView->ViewOrientation() ;
1420   ImmediateUpdate();
1421 }
1422
1423 void V3d_View::Reset( const Standard_Boolean update )
1424 {
1425   MyView->ViewOrientationReset() ;
1426   MyViewOrientation = MyView->ViewOrientation() ;
1427   MyView->ViewMappingReset();
1428   MyViewMapping = MyView->ViewMapping() ;
1429
1430   ZFitAll (Zmargin);
1431   SwitchSetFront = Standard_False;
1432 #ifdef IMP020300
1433   if( !myImmediateUpdate && update ) Update();
1434 #else
1435   ImmediateUpdate();
1436 #endif
1437 }
1438
1439 void V3d_View::Panning(const Standard_Real Dx, const Standard_Real Dy, const Quantity_Factor aZoomFactor, const Standard_Boolean Start)
1440 {
1441   Standard_Real Umin,Vmin,Umax,Vmax,Xrp,Yrp,Zrp,Dxv,Dyv ;
1442   Viewer_BadValue_Raise_if( aZoomFactor <= 0.,"V3d_View::Panning, bad zoom factor");
1443
1444   if( Start ) {
1445     MyProjReferencePoint = MyViewMapping.ProjectionReferencePoint() ;
1446     MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax) ;
1447     MyXwindowCenter = (Umin + Umax)/2. ;
1448     MyYwindowCenter = (Vmin + Vmax)/2. ;
1449     MyWindowWidth = Abs(Umax - Umin) ;
1450     MyWindowHeight = Abs(Vmax - Vmin) ;
1451     Viewer_BadValue_Raise_if( MyWindowWidth <= 0. || MyWindowHeight <= 0. ,
1452       "V3d_View::Panning, Window Size is NULL");
1453   }
1454   MyProjReferencePoint.Coord(Xrp,Yrp,Zrp) ;
1455   Xrp = MyXwindowCenter - Dx ;
1456   Yrp = MyYwindowCenter - Dy ;
1457   Dxv = MyWindowWidth/aZoomFactor ; Dyv = MyWindowHeight/aZoomFactor ;
1458   Umin = Xrp - Dxv/2. ; Umax = Xrp + Dxv/2. ;
1459   Vmin = Yrp - Dyv/2. ; Vmax = Yrp + Dyv/2. ;
1460   MyViewMapping.SetWindowLimit(Umin,Vmin,Umax,Vmax) ;
1461   if( MyType != V3d_PERSPECTIVE ) {
1462     MyProjReferencePoint.SetCoord(Xrp,Yrp,Zrp) ;
1463     MyViewMapping.SetProjectionReferencePoint(MyProjReferencePoint) ;
1464   }
1465   MyView->SetViewMapping(MyViewMapping) ;
1466   ImmediateUpdate();
1467 }
1468
1469 void V3d_View::SetCenter(const Standard_Integer X, const Standard_Integer Y)
1470 {
1471   Standard_Real x,y;
1472   Convert(X,Y,x,y);
1473   SetCenter(x,y);
1474 }
1475
1476 void V3d_View::SetCenter(const Standard_Real Xc, const Standard_Real Yc)
1477 {
1478   Standard_Real Umin,Vmin,Umax,Vmax,Xrp,Yrp,Zrp ;
1479
1480   MyProjReferencePoint = MyViewMapping.ProjectionReferencePoint() ;
1481   MyProjReferencePoint.Coord(Xrp,Yrp,Zrp) ;
1482   MyXwindowCenter = Xrp = Xc ; MyYwindowCenter = Yrp = Yc ;
1483   MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax) ;
1484   MyWindowWidth = Abs(Umax - Umin) ; MyWindowHeight = Abs(Vmax - Vmin) ;
1485   Viewer_BadValue_Raise_if( MyWindowWidth <= 0. || MyWindowHeight <= 0. ,
1486     "V3d_View::SetCenter, Window Size is NULL");
1487
1488   Umin = Xc - MyWindowWidth/2. ; Vmin = Yc - MyWindowHeight/2. ;
1489   Umax = Xc + MyWindowWidth/2. ; Vmax = Yc + MyWindowHeight/2. ;
1490   MyViewMapping.SetWindowLimit(Umin,Vmin,Umax,Vmax) ;
1491   if( MyType != V3d_PERSPECTIVE ) {
1492     MyProjReferencePoint.SetCoord(Xrp,Yrp,Zrp) ;
1493     MyViewMapping.SetProjectionReferencePoint(MyProjReferencePoint) ;
1494   }
1495   MyView->SetViewMapping(MyViewMapping) ;
1496   ImmediateUpdate();
1497 }
1498
1499 void V3d_View::SetSize(const Standard_Real Size)
1500 {
1501   Standard_Real Umin,Vmin,Umax,Vmax,Rap ;
1502
1503   Viewer_BadValue_Raise_if(  Size  <= 0.,
1504     "V3d_View::SetSize, Window Size is NULL");
1505
1506
1507   MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax) ;
1508   MyWindowWidth = Abs(Umax - Umin) ;
1509   MyWindowHeight = Abs(Vmax - Vmin) ;
1510   MyXwindowCenter = (Umin + Umax)/2. ;
1511   MyYwindowCenter = (Vmin + Vmax)/2. ;
1512   Rap = MyWindowWidth/MyWindowHeight ;
1513   if( MyWindowWidth >= MyWindowHeight ) {
1514     MyWindowWidth = Size ; MyWindowHeight = Size/Rap ;
1515   } else {
1516     MyWindowHeight = Size ; MyWindowWidth = Size*Rap ;
1517   }
1518   Umin = MyXwindowCenter - MyWindowWidth/2. ;
1519   Vmin = MyYwindowCenter - MyWindowHeight/2. ;
1520   Umax = MyXwindowCenter + MyWindowWidth/2. ;
1521   Vmax = MyYwindowCenter + MyWindowHeight/2. ;
1522   MyViewMapping.SetWindowLimit(Umin,Vmin,Umax,Vmax) ;
1523   MyView->SetViewMapping(MyViewMapping) ;
1524   ImmediateUpdate();
1525 }
1526
1527 void V3d_View::SetZSize(const Standard_Real Size)
1528 {
1529   Standard_Real Zmax = Size/2.;
1530
1531 #ifdef IMP020300
1532   if( Size <= 0. ) {
1533     Standard_Real Xat,Yat,Zat,Xpn,Ypn,Zpn,Xrp,Yrp,Zrp,Xeye,Yeye,Zeye;
1534     MyViewReferencePoint.Coord(Xat,Yat,Zat) ;
1535     MyProjReferencePoint.Coord(Xrp,Yrp,Zrp) ;
1536     MyViewReferencePlane.Coord(Xpn,Ypn,Zpn) ;
1537     Xeye = Zrp*Xpn + Xat ; Yeye = Zrp*Ypn + Yat ; Zeye = Zrp*Zpn + Zat;
1538     Zmax = Sqrt( Xeye*Xeye + Yeye*Yeye + Zeye*Zeye );
1539     if( Zmax <= MyViewMapping.FrontPlaneDistance() ) return;
1540   }
1541 #else
1542   Viewer_BadValue_Raise_if(  Size  <= 0.,
1543     "V3d_View::SetZSize, Window ZSize is NULL");
1544 #endif
1545
1546   Standard_Real Front = MyViewContext.ZClippingFrontPlane() ;
1547   Standard_Real Back  = MyViewContext.ZClippingBackPlane() ;
1548   Standard_Real focale= Focale();
1549
1550   MyViewMapping.SetFrontPlaneDistance(Zmax) ;
1551   MyViewMapping.SetBackPlaneDistance(-Zmax) ;
1552
1553   // OCC18942
1554   if( MyProjModel != V3d_TPM_WALKTHROUGH ) {
1555     MyViewMapping.SetViewPlaneDistance(MyType == V3d_PERSPECTIVE ? 0. : Zmax) ;
1556   }
1557
1558   MyView->SetViewMapping(MyViewMapping) ;
1559   if( MyViewContext.FrontZClippingIsOn()  ||
1560       MyViewContext.BackZClippingIsOn() )
1561   {
1562     MyViewContext.SetZClippingFrontPlane(Front) ;
1563     MyViewContext.SetZClippingBackPlane(Back) ;
1564     MyView->SetContext(MyViewContext) ;
1565   }
1566 }
1567
1568 void V3d_View::SetZoom(const Standard_Real Coef,const Standard_Boolean Start)
1569 {
1570   Standard_Real Umin,Vmin,Umax,Vmax,Dxv,Dyv ;
1571   Viewer_BadValue_Raise_if( Coef <= 0.,"V3d_View::SetZoom, bad coefficient");
1572
1573   if( Start ) {
1574     MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax) ;
1575     MyXwindowCenter = (Umin + Umax)/2. ;
1576     MyYwindowCenter = (Vmin + Vmax)/2. ;
1577     MyWindowWidth = Abs(Umax - Umin) ;
1578     MyWindowHeight = Abs(Vmax - Vmin) ;
1579   }
1580
1581   // ensure that zoom will not be too small or too big
1582   Standard_Real coef = Coef;
1583   if ( MyWindowWidth < coef * Precision::Confusion() )
1584     coef = MyWindowWidth / Precision::Confusion();
1585   else if ( MyWindowWidth > coef * 1e12 )
1586     coef = MyWindowWidth / 1e12;
1587   if ( MyWindowHeight < coef * Precision::Confusion() )
1588     coef = MyWindowHeight / Precision::Confusion();
1589   else if ( MyWindowHeight > coef * 1e12 )
1590     coef = MyWindowHeight / 1e12;
1591
1592   Dxv = MyWindowWidth/coef;
1593   Dyv = MyWindowHeight/coef;
1594   Umin = MyXwindowCenter - Dxv/2. ; Umax = MyXwindowCenter + Dxv/2. ;
1595   Vmin = MyYwindowCenter - Dyv/2. ; Vmax = MyYwindowCenter + Dyv/2. ;
1596   MyViewMapping.SetWindowLimit(Umin,Vmin,Umax,Vmax) ;
1597   MyView->SetViewMapping(MyViewMapping) ;
1598   ImmediateUpdate();
1599 }
1600
1601 void V3d_View::SetScale( const Standard_Real Coef )
1602 {
1603   Standard_Real Umin,Vmin,Umax,Vmax,Xrp,Yrp,Dxv,Dyv ;
1604   Visual3d_ViewMapping VMD = MyView->ViewMappingDefault() ;
1605
1606   Viewer_BadValue_Raise_if( Coef <= 0. ,"V3d_View::SetScale, bad coefficient");
1607
1608   VMD.WindowLimit(Umin,Vmin,Umax,Vmax) ;
1609   Dxv = Abs(Umax - Umin) ; Dyv = Abs(Vmax - Vmin) ;
1610   Dxv /= Coef ; Dyv /= Coef ;
1611   MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax) ;
1612   Xrp = (Umin + Umax)/2. ; Yrp = (Vmin + Vmax)/2. ;
1613   Umin = Xrp - Dxv/2. ; Umax = Xrp + Dxv/2. ;
1614   Vmin = Yrp - Dyv/2. ; Vmax = Yrp + Dyv/2. ;
1615   MyViewMapping.SetWindowLimit(Umin,Vmin,Umax,Vmax) ;
1616   MyView->SetViewMapping(MyViewMapping) ;
1617   ImmediateUpdate();
1618 }
1619
1620 void V3d_View::SetAxialScale( const Standard_Real Sx, const Standard_Real Sy, const Standard_Real Sz )
1621 {
1622   Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax,U,V,W ;
1623   Standard_Real Umin,Vmin,Wmin,Umax,Vmax,Wmax ;
1624   Viewer_BadValue_Raise_if( Sx <= 0. || Sy <= 0. || Sz <= 0.,"V3d_View::SetAxialScale, bad coefficient");
1625
1626   MyViewOrientation.SetAxialScale( Sx, Sy, Sz );
1627   Aspect_TypeOfUpdate updateMode = MyView->ViewManager()->UpdateMode();
1628   MyView->ViewManager()->SetUpdateMode(Aspect_TOU_ASAP);
1629   MyView->SetViewOrientation(MyViewOrientation);
1630   MyView->ViewManager()->SetUpdateMode(updateMode);
1631
1632   MyView->MinMaxValues(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax) ;
1633
1634   Standard_Real LIM = ShortRealLast() -1.;
1635   if     (Abs(Xmin) > LIM || Abs(Ymin) > LIM || Abs(Zmin) > LIM
1636     ||  Abs(Xmax) > LIM || Abs(Ymax) > LIM || Abs(Zmax) > LIM ) {
1637       return;
1638     }
1639
1640     MyView->Projects(Xmin,Ymin,Zmin,Umin,Vmin,Wmin) ;
1641     MyView->Projects(Xmax,Ymax,Zmax,Umax,Vmax,Wmax) ;
1642     Umax = Max(Umin,Umax) ; Vmax = Max(Vmin,Vmax) ;
1643     MyView->Projects(Xmin,Ymin,Zmax,U,V,W) ;
1644     Umax = Max(Umax,U) ; Vmax = Max(Vmax,V) ;
1645     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1646     MyView->Projects(Xmax,Ymin,Zmax,U,V,W) ;
1647     Umax = Max(Umax,U) ; Vmax = Max(Vmax,V) ;
1648     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1649     MyView->Projects(Xmax,Ymin,Zmin,U,V,W) ;
1650     Umax = Max(Umax,U) ; Vmax = Max(Vmax,V) ;
1651     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1652     MyView->Projects(Xmax,Ymax,Zmin,U,V,W) ;
1653     Umax = Max(Umax,U) ; Vmax = Max(Vmax,V) ;
1654     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1655     MyView->Projects(Xmin,Ymax,Zmax,U,V,W) ;
1656     Umax = Max(Umax,U) ; Vmax = Max(Vmax,V) ;
1657     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1658     MyView->Projects(Xmin,Ymax,Zmin,U,V,W) ;
1659     Umax = Max(Umax,U) ; Vmax = Max(Vmax,V) ;
1660     Umax = Max(Umax,Vmax) ;
1661     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1662     Wmax = Max(Abs(Wmin),Abs(Wmax)) ;
1663     Wmax = Max(Abs(Umax),Abs(Wmax)) ;
1664
1665     if( Wmax > 0. ) {
1666       SetZSize(2.*Wmax + Wmax) ;
1667     }
1668 }
1669
1670 void V3d_View::FitAll(const Standard_Real Coef, const Standard_Boolean FitZ,
1671                       const Standard_Boolean update)
1672 {
1673   Standard_Real Umin, Umax, Vmin, Vmax, Xrp, Yrp, Zrp, U, V, W, U1, V1, W1;
1674   Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
1675   Standard_Real DxvOld, DyvOld, DxvNew, DyvNew;
1676   Standard_Integer Nstruct = MyView->NumberOfDisplayedStructures();
1677
1678   if ((Nstruct <= 0) || (Coef < 0.0) || (Coef > 1.0))
1679   {
1680 #ifndef IMP020300
1681     ImmediateUpdate();
1682 #endif
1683     return;
1684   }
1685
1686   MyProjReferencePoint = MyViewMapping.ProjectionReferencePoint();
1687   MyProjReferencePoint.Coord (Xrp, Yrp, Zrp);
1688   if (MyView->IsDefined())
1689   {
1690     Standard_Integer Xpixel, Ypixel;
1691     MyWindow->Size (Xpixel, Ypixel);
1692     DxvOld = Xpixel;
1693     DyvOld = Ypixel;
1694   }
1695   else
1696   {
1697     MyViewMapping.WindowLimit (Umin, Vmin, Umax, Vmax);
1698     DxvOld = Abs (Umax - Umin);
1699     DyvOld = Abs (Vmax - Vmin);
1700   }
1701   if ((DxvOld == 0.0) || (DyvOld == 0.0))
1702   {
1703     return;
1704   }
1705
1706   Standard_Real aWinRatio = DxvOld / DyvOld;
1707
1708   // retrieve min / max values for current displayed objects
1709   MyView->MinMaxValues (Xmin, Ymin, Zmin,
1710                         Xmax, Ymax, Zmax);
1711
1712   Standard_Real LIM = ShortRealLast() - 1.0;
1713   if  (Abs(Xmin) > LIM || Abs(Ymin) > LIM || Abs(Zmin) > LIM
1714     || Abs(Xmax) > LIM || Abs(Ymax) > LIM || Abs(Zmax) > LIM)
1715   {
1716 #ifndef IMP020300
1717     ImmediateUpdate();
1718 #endif
1719     return;
1720   }
1721
1722   // eliminate fluctuations between sequential FitAll() calls
1723   MyViewMapping.SetWindowLimit (-1.0 * aWinRatio, -1.0, 1.0 * aWinRatio, 1.0);
1724   if (MyType != V3d_PERSPECTIVE)
1725   {
1726     MyProjReferencePoint.SetCoord (0.0, 0.0, Zrp);
1727     MyViewMapping.SetProjectionReferencePoint (MyProjReferencePoint);
1728   }
1729   MyView->SetViewMapping (MyViewMapping);
1730
1731   // iterate 2 times to find optimal view plane size
1732   // (use view plane values computed on previous iteration)
1733   for (Standard_Integer aIteration = 2; aIteration > 0; --aIteration)
1734   {
1735     MyView->Projects (Xmin, Ymin, Zmin,  U,  V,  W);
1736     MyView->Projects (Xmax, Ymax, Zmax, U1, V1, W1);
1737     Umin = Min (U, U1); Umax = Max (U, U1);
1738     Vmin = Min (V, V1); Vmax = Max (V, V1);
1739
1740     MyView->Projects (Xmin, Ymin, Zmax, U, V, W);
1741     Umin = Min (U, Umin); Umax = Max (U, Umax);
1742     Vmin = Min (V, Vmin); Vmax = Max (V, Vmax);
1743
1744     MyView->Projects (Xmax, Ymin, Zmax, U, V, W);
1745     Umin = Min (U, Umin); Umax = Max (U, Umax);
1746     Vmin = Min (V, Vmin); Vmax = Max (V, Vmax);
1747
1748     MyView->Projects (Xmax, Ymin, Zmin, U, V, W);
1749     Umin = Min (U, Umin); Umax = Max (U, Umax);
1750     Vmin = Min (V, Vmin); Vmax = Max (V, Vmax);
1751
1752     MyView->Projects (Xmax, Ymax, Zmin, U, V, W);
1753     Umin = Min (U, Umin); Umax = Max (U, Umax);
1754     Vmin = Min (V, Vmin); Vmax = Max (V, Vmax);
1755
1756     MyView->Projects (Xmin, Ymax, Zmax, U, V, W);
1757     Umin = Min (U, Umin); Umax = Max (U, Umax);
1758     Vmin = Min (V, Vmin); Vmax = Max (V, Vmax);
1759
1760     MyView->Projects (Xmin, Ymax, Zmin, U, V, W);
1761     Umin = Min (U, Umin); Umax = Max (U, Umax);
1762     Vmin = Min (V, Vmin); Vmax = Max (V, Vmax);
1763
1764     DxvNew = Abs (Umax - Umin);
1765     DyvNew = Abs (Vmax - Vmin);
1766
1767     if (DyvNew < 10.0 * Precision::Confusion())
1768     {
1769       if (DxvNew < 10.0 * Precision::Confusion())
1770       {
1771         // whole scene projected to point
1772         DxvNew = Max (Abs (Zmax - Zmin), (Max (Abs (Xmax - Xmin), Abs (Ymax - Ymin))));
1773         if (DxvNew < 10.0 * Precision::Confusion())
1774         {
1775           // this is really just one (!) point and zooming has no any effect
1776           // just center the view
1777           DyvNew = DyvOld;
1778           DxvNew = DxvOld;
1779         }
1780         else
1781         {
1782           // we look along some line
1783           // fit view like that to see whole scene on rotation
1784           DxvNew += Coef * DxvNew;
1785           DyvNew = DxvNew / aWinRatio;
1786         }
1787       }
1788       else
1789       {
1790         // whole scene projected to horizontal line
1791         DxvNew += Coef * DxvNew;
1792         DyvNew = DxvNew / aWinRatio;
1793       }
1794     }
1795     else
1796     {
1797       // general case (or DxvNew == 0.0 - vertical line)
1798       // safe original ratio
1799       Standard_Real aFitRatio = DxvNew / DyvNew;
1800       if (aFitRatio >= aWinRatio)
1801       {
1802         DxvNew += Coef * DxvNew;
1803         DyvNew = DxvNew / aWinRatio;
1804       }
1805       else
1806       {
1807         DyvNew += Coef * DyvNew;
1808         DxvNew = DyvNew * aWinRatio;
1809       }
1810     }
1811
1812     // new scene center
1813     Xrp = 0.5 * (Umin + Umax);
1814     Yrp = 0.5 * (Vmin + Vmax);
1815
1816     // new window limits
1817     Umin = Xrp - 0.5 * DxvNew;
1818     Umax = Xrp + 0.5 * DxvNew;
1819     Vmin = Yrp - 0.5 * DyvNew;
1820     Vmax = Yrp + 0.5 * DyvNew;
1821     MyViewMapping.SetWindowLimit (Umin, Vmin, Umax, Vmax);
1822
1823     if (MyType != V3d_PERSPECTIVE)
1824     {
1825       // center the view
1826       MyProjReferencePoint.SetCoord (Xrp, Yrp, Zrp);
1827       MyViewMapping.SetProjectionReferencePoint (MyProjReferencePoint);
1828     }
1829     MyView->SetViewMapping (MyViewMapping);
1830   }
1831
1832   if (FitZ)
1833   {
1834     ZFitAll (Zmargin);
1835 #ifdef IMP020300
1836   }
1837   else
1838   {
1839     ImmediateUpdate();
1840 #endif
1841   }
1842 #ifdef IMP020300
1843   if (!myImmediateUpdate && update)
1844   {
1845     Update();
1846   }
1847 #endif
1848 }
1849
1850 void V3d_View::ZFitAll(const Standard_Real Coef)
1851 {
1852   Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax,U,V,W ;
1853   Standard_Real Umin,Vmin,Wmin,Umax,Vmax,Wmax ;
1854   // CAL 6/11/98
1855   Standard_Integer Nstruct = MyView->NumberOfDisplayedStructures() ;
1856
1857   if( MyType == V3d_PERSPECTIVE ) {
1858     ImmediateUpdate();
1859     return ;
1860   }
1861
1862   if( (Nstruct <= 0) || (Coef < 0.) ) {
1863     ImmediateUpdate();
1864     return ;
1865   }
1866
1867   MyView->MinMaxValues(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax) ;
1868
1869   Standard_Real LIM = ShortRealLast() -1.;
1870   if   (Abs(Xmin) > LIM || Abs(Ymin) > LIM || Abs(Zmin) > LIM
1871     ||  Abs(Xmax) > LIM || Abs(Ymax) > LIM || Abs(Zmax) > LIM ) {
1872       ImmediateUpdate();
1873       return ;
1874     }
1875
1876     // CAL 6/11/98
1877     // Case when view contains only a point
1878     if (Xmin == Xmax && Ymin == Ymax && Zmin == Zmax) {
1879       ImmediateUpdate();
1880       return ;
1881     }
1882     MyView->Projects(Xmin,Ymin,Zmin,Umin,Vmin,Wmin) ;
1883     MyView->Projects(Xmax,Ymax,Zmax,Umax,Vmax,Wmax) ;
1884     MyView->Projects(Xmin,Ymin,Zmax,U,V,W) ;
1885     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1886     MyView->Projects(Xmax,Ymin,Zmax,U,V,W) ;
1887     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1888     MyView->Projects(Xmax,Ymin,Zmin,U,V,W) ;
1889     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1890     MyView->Projects(Xmax,Ymax,Zmin,U,V,W) ;
1891     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1892     MyView->Projects(Xmin,Ymax,Zmax,U,V,W) ;
1893     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1894     MyView->Projects(Xmin,Ymax,Zmin,U,V,W) ;
1895     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1896     Wmax = Max(Abs(Wmin),Abs(Wmax)) ;
1897     if( Wmax > 0. ) {
1898       SetZSize(2.*Wmax + Coef * Wmax) ;
1899     }
1900     ImmediateUpdate();
1901 }
1902
1903 void V3d_View::DepthFitAll(const Quantity_Coefficient Aspect,
1904                            const Quantity_Coefficient Margin)
1905 {
1906   Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax,U,V,W,U1,V1,W1 ;
1907   Standard_Real Umin,Vmin,Wmin,Umax,Vmax,Wmax ;
1908   Standard_Real Xrp,Yrp,Zrp,Dx,Dy,Dz,Size;
1909
1910   Standard_Integer Nstruct = MyView->NumberOfDisplayedStructures() ;
1911
1912   if( (Nstruct <= 0) || (Aspect < 0.) || (Margin < 0.) || (Margin > 1.)) {
1913     ImmediateUpdate();
1914     return ;
1915   }
1916
1917   MyProjReferencePoint = MyViewMapping.ProjectionReferencePoint() ;
1918   MyProjReferencePoint.Coord(Xrp,Yrp,Zrp) ;
1919
1920   MyView->MinMaxValues(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax) ;
1921
1922   Standard_Real LIM = ShortRealLast() -1.;
1923   if     (Abs(Xmin) > LIM || Abs(Ymin) > LIM || Abs(Zmin) > LIM
1924     ||  Abs(Xmax) > LIM || Abs(Ymax) > LIM || Abs(Zmax) > LIM ) {
1925       ImmediateUpdate();
1926       return ;
1927     }
1928
1929     if (Xmin == Xmax && Ymin == Ymax && Zmin == Zmax) {
1930       ImmediateUpdate();
1931       return ;
1932     }
1933     MyView->Projects(Xmin,Ymin,Zmin,U,V,W) ;
1934     MyView->Projects(Xmax,Ymax,Zmax,U1,V1,W1) ;
1935     Umin = Min(U,U1) ; Umax = Max(U,U1) ;
1936     Vmin = Min(V,V1) ; Vmax = Max(V,V1) ;
1937     Wmin = Min(W,W1) ; Wmax = Max(W,W1) ;
1938     MyView->Projects(Xmin,Ymin,Zmax,U,V,W) ;
1939     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1940     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1941     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1942     MyView->Projects(Xmax,Ymin,Zmax,U,V,W) ;
1943     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1944     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1945     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1946     MyView->Projects(Xmax,Ymin,Zmin,U,V,W) ;
1947     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1948     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1949     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1950     MyView->Projects(Xmax,Ymax,Zmin,U,V,W) ;
1951     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1952     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1953     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1954     MyView->Projects(Xmin,Ymax,Zmax,U,V,W) ;
1955     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1956     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1957     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1958     MyView->Projects(Xmin,Ymax,Zmin,U,V,W) ;
1959     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
1960     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
1961     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
1962
1963     // Adjust Z size
1964     Wmax = Max(Abs(Wmin),Abs(Wmax)) ;
1965     Dz = 2.*Wmax + Margin * Wmax;
1966
1967     // Compute depth value
1968     Dx = Abs(Umax - Umin) ; Dy = Abs(Vmax - Vmin) ; // Dz = Abs(Wmax - Wmin);
1969     Dx += Margin * Dx; Dy += Margin * Dy;
1970     Size = Sqrt(Dx*Dx + Dy*Dy + Dz*Dz);
1971     if( Size > 0. ) {
1972       SetZSize(Size) ;
1973       SetDepth( Aspect * Size / 2.);
1974     }
1975
1976     ImmediateUpdate();
1977 }
1978
1979 void V3d_View::FitAll(const Standard_Real Xmin, const Standard_Real Ymin, const Standard_Real Xmax, const Standard_Real Ymax)
1980 {
1981   FitAll(MyWindow,Xmin,Ymin,Xmax,Ymax);
1982 #ifndef IMP020300
1983   ImmediateUpdate();
1984 #endif
1985 }
1986
1987 void V3d_View::WindowFitAll(const Standard_Integer Xmin, const Standard_Integer Ymin, const Standard_Integer Xmax, const Standard_Integer Ymax)
1988 {
1989   WindowFit(Xmin,Ymin,Xmax,Ymax);
1990 }
1991
1992 void V3d_View::WindowFit(const Standard_Integer Xmin, const Standard_Integer Ymin, const Standard_Integer Xmax, const Standard_Integer Ymax)
1993 {
1994   Standard_Real x1,y1,x2,y2;
1995   Convert(Xmin,Ymin,x1,y1);
1996   Convert(Xmax,Ymax,x2,y2);
1997   FitAll(x1,y1,x2,y2);
1998 }
1999
2000 void V3d_View::SetViewMapping(const Visual3d_ViewMapping& VM)
2001 {
2002   MyViewMapping = VM;
2003   // ajust view type according to mapping projection
2004   // NOTE: Might be dangerous, potentially conflicts with the C++ view class
2005   // (orthographic or perspective)! Use with care!
2006   if ( VM.Projection() == Visual3d_TOP_PERSPECTIVE )
2007     MyType = V3d_PERSPECTIVE;
2008   else MyType = V3d_ORTHOGRAPHIC;
2009
2010   MyView->SetViewMapping(MyViewMapping) ;
2011   ImmediateUpdate();
2012 }
2013
2014 void V3d_View::SetViewMappingDefault()
2015 {
2016   MyView->SetViewMapping(MyViewMapping) ;
2017   MyView->SetViewMappingDefault();
2018   ImmediateUpdate();
2019 }
2020
2021 void V3d_View::ResetViewMapping()
2022 {
2023   MyView->ViewMappingReset();
2024   MyViewMapping = MyView->ViewMapping() ;
2025 #ifdef IMP020300
2026   ZFitAll (Zmargin);
2027   Update();
2028 #else
2029   ImmediateUpdate();
2030 #endif
2031 }
2032
2033 Standard_Real V3d_View::Convert(const Standard_Integer Vp) const
2034 {
2035   Standard_Real Umin,Umax,Vmin,Vmax,Dxv,Vv ;
2036   Standard_Integer Dxw,Dyw ;
2037
2038   V3d_UnMapped_Raise_if( !MyView->IsDefined(), "view has no window");
2039
2040   MyWindow->Size(Dxw,Dyw);
2041   MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax) ;
2042   Dxv = Umax - Umin ;
2043   Vv = Dxv*(Standard_Real)Vp/(Standard_Real)Dxw ;
2044
2045   return Vv ;
2046 }
2047
2048 void V3d_View::Convert(const Standard_Integer Xp, const Standard_Integer Yp, Standard_Real& Xv, Standard_Real& Yv) const
2049 {
2050   Standard_Real Umin,Umax,Vmin,Vmax,Dxv,Dyv ;
2051   Standard_Integer Dxw,Dyw ;
2052
2053   V3d_UnMapped_Raise_if( !MyView->IsDefined(), "view has no window");
2054
2055   MyWindow->Size(Dxw,Dyw);
2056   MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax) ;
2057   Dxv = Umax - Umin ; Dyv = Vmax - Vmin ;
2058   Xv = Umin + Dxv*(Standard_Real)Xp/(Standard_Real)Dxw ;
2059   Yv = Vmin + Dyv*(Standard_Real)(Dyw-Yp)/(Standard_Real)Dyw ;
2060 }
2061
2062 Standard_Integer V3d_View::Convert(const Standard_Real Vv) const
2063 {
2064   V3d_UnMapped_Raise_if( !MyView->IsDefined(), "view has no window");
2065
2066   Standard_Integer Dxw, Dyw;
2067   MyWindow->Size(Dxw,Dyw);
2068
2069   Standard_Real Umin,Umax,Vmin,Vmax;
2070   MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax) ;
2071   Standard_Real Dxv = Umax - Umin;
2072   return RealToInt ( Dxw * Vv / Dxv );
2073 }
2074
2075 void V3d_View::Convert(const Standard_Real Xv, const Standard_Real Yv, Standard_Integer& Xp, Standard_Integer& Yp) const
2076 {
2077   V3d_UnMapped_Raise_if( !MyView->IsDefined(), "view has no window");
2078
2079   Standard_Integer Dxw, Dyw;
2080   MyWindow->Size(Dxw,Dyw);
2081
2082   Standard_Real Umin,Umax,Vmin,Vmax;
2083   MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax) ;
2084   Standard_Real Dxv = Umax - Umin;
2085   Standard_Real Dyv = Vmax - Vmin;
2086
2087   // CAL 15/12/93 warning:  double  assigned to  int
2088   Xp = RealToInt (Dxw*(Xv - Umin)/Dxv);
2089   // CAL 15/12/93 warning:  double  assigned to  int
2090   Yp = Dyw - RealToInt (Dyw*(Yv - Vmin)/Dyv);
2091 }
2092
2093 void V3d_View::Convert(const Standard_Integer Xp, const Standard_Integer Yp, Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
2094 {
2095   Graphic3d_Vertex Vrp ;
2096   Vrp = (MyViewer->Viewer())->ConvertCoord(MyWindow,Xp,Yp) ;
2097   Vrp.Coord(X,Y,Z) ;
2098 #ifndef IMP240100
2099   if( MyViewer->Grid()->IsActive() ) {
2100     Graphic3d_Vertex NewVrp = Compute(Vrp) ;
2101     NewVrp.Coord(X,Y,Z) ;
2102   }
2103 #endif
2104 }
2105
2106 void V3d_View::ConvertWithProj(const Standard_Integer Xp, const Standard_Integer Yp, Standard_Real& X, Standard_Real& Y, Standard_Real& Z, Standard_Real& Dx, Standard_Real& Dy, Standard_Real& Dz) const
2107 {
2108   Graphic3d_Vertex Vrp ;
2109   Graphic3d_Vector Proj ;
2110   (MyViewer->Viewer())->ConvertCoordWithProj(MyWindow,Xp,Yp, Vrp, Proj) ;
2111   Vrp.Coord(X,Y,Z) ;
2112   Proj.Coord(Dx,Dy,Dz) ;
2113 #ifndef IMP240100
2114   if( MyViewer->Grid()->IsActive() ) {
2115     Graphic3d_Vertex NewVrp = Compute(Vrp) ;
2116     NewVrp.Coord(X,Y,Z) ;
2117   }
2118 #endif
2119 }
2120
2121 #ifdef IMP240100
2122 void V3d_View::ConvertToGrid(const Standard_Integer Xp, const Standard_Integer Yp, Standard_Real& Xg, Standard_Real& Yg, Standard_Real& Zg) const
2123 {
2124   Graphic3d_Vertex Vrp ;
2125   Vrp = (MyViewer->Viewer())->ConvertCoord(MyWindow,Xp,Yp) ;
2126   if( MyViewer->Grid()->IsActive() ) {
2127     Graphic3d_Vertex NewVrp = Compute(Vrp) ;
2128     NewVrp.Coord(Xg,Yg,Zg) ;
2129   } else
2130     Vrp.Coord(Xg,Yg,Zg) ;
2131 }
2132
2133 void V3d_View::ConvertToGrid(const Standard_Real X, const Standard_Real Y, const Standard_Real Z, Standard_Real& Xg, Standard_Real& Yg, Standard_Real& Zg) const
2134 {
2135   if( MyViewer->Grid()->IsActive() ) {
2136     Graphic3d_Vertex Vrp(X,Y,Z) ;
2137     Graphic3d_Vertex NewVrp = Compute(Vrp) ;
2138     NewVrp.Coord(Xg,Yg,Zg) ;
2139   } else {
2140     Xg = X; Yg = Y; Zg = Z;
2141   }
2142 }
2143 #endif
2144
2145 #ifndef IMP240100
2146 void V3d_View::PixToRef(const Standard_Integer Xp, const Standard_Integer Yp, Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
2147 {
2148   Graphic3d_Vertex Vrp ;
2149   Vrp = (MyViewer->Viewer())->ConvertCoord(MyWindow,Xp,Yp) ;
2150   Vrp.Coord(X,Y,Z) ;
2151   if( MyViewer->Grid()->IsActive() ) {
2152     Graphic3d_Vertex NewVrp = Compute(Vrp) ;
2153     NewVrp.Coord(X,Y,Z) ;
2154   }
2155 }
2156 #endif
2157
2158 #ifdef IMP240100
2159 void V3d_View::Convert(const Standard_Real X, const Standard_Real Y, const Standard_Real Z, Standard_Integer& Xp, Standard_Integer& Yp) const
2160 #else
2161 void V3d_View::RefToPix(const Standard_Real X, const Standard_Real Y, const Standard_Real Z, Standard_Integer& Xp, Standard_Integer& Yp) const
2162 #endif
2163 {
2164   Graphic3d_Vertex Vrp(X,Y,Z) ;
2165   (MyViewer->Viewer())->ConvertCoord(MyWindow,Vrp,Xp,Yp) ;
2166 }
2167
2168 void V3d_View::Project(const Standard_Real X, const Standard_Real Y, const Standard_Real Z, Standard_Real &Xp, Standard_Real &Yp) const
2169 {
2170   if ( MyType != V3d_PERSPECTIVE )
2171   {
2172     // use old implementation
2173     Standard_Real Zp;
2174     MyView->Projects( X, Y, Z, Xp, Yp, Zp );
2175   }
2176   else
2177   {
2178     // NKV - Using raster projection
2179     Standard_Integer Xpx, Ypx;
2180     Convert(X, Y, Z, Xpx, Ypx);
2181     Convert(Xpx, Ypx, Xp, Yp);
2182   }
2183 }
2184
2185 void V3d_View::BackgroundColor(const Quantity_TypeOfColor Type,Standard_Real& V1, Standard_Real& V2, Standard_Real& V3) const
2186 {
2187   Quantity_Color C = BackgroundColor() ;
2188   C.Values(V1,V2,V3,Type) ;
2189 }
2190
2191 Quantity_Color V3d_View::BackgroundColor() const
2192 {
2193   return MyBackground.Color() ;
2194 }
2195
2196 void V3d_View::GradientBackgroundColors(Quantity_Color& Color1,Quantity_Color& Color2) const
2197 {
2198   MyGradientBackground.Colors(Color1, Color2);
2199 }
2200
2201 Aspect_GradientBackground V3d_View::GradientBackground() const
2202 {
2203    return MyGradientBackground;
2204 }
2205
2206 Standard_Real V3d_View::Scale() const
2207 {
2208   Standard_Real Umin,Vmin,Umax,Vmax,Dxv ;
2209   Visual3d_ViewMapping VMD = MyView->ViewMappingDefault() ;
2210   Standard_Real S = 0. ;
2211
2212   VMD.WindowLimit(Umin,Vmin,Umax,Vmax) ;
2213   Dxv = Umax - Umin ;
2214   MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax) ;
2215   if( Umax > Umin && Vmax > Vmin ) S = Dxv/(Umax - Umin) ;
2216   return S ;
2217 }
2218
2219 void V3d_View::AxialScale(Standard_Real& Sx, Standard_Real& Sy, Standard_Real& Sz) const
2220 {
2221   MyViewOrientation.AxialScale( Sx, Sy, Sz );
2222 }
2223
2224 void V3d_View::Center(Standard_Real& Xc, Standard_Real& Yc) const
2225 {
2226   Standard_Real Umin,Vmin,Umax,Vmax ;
2227
2228   MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax) ;
2229   Xc = (Umin + Umax)/2. ;
2230   Yc = (Vmin + Vmax)/2. ;
2231 }
2232
2233 void V3d_View::Size(Standard_Real& Width, Standard_Real& Height) const
2234 {
2235   Standard_Real Umin,Vmin,Umax,Vmax ;
2236
2237   MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax) ;
2238   Width = Umax - Umin ;
2239   Height = Vmax - Vmin ;
2240 }
2241
2242 Standard_Real V3d_View::ZSize() const
2243 {
2244   Standard_Real Wmin,Wmax,Depth ;
2245
2246   Wmax = MyViewMapping.FrontPlaneDistance() ;
2247   Wmin = MyViewMapping.BackPlaneDistance() ;
2248   Depth = 2. * Max(Wmin,Wmax) ;
2249   return (Depth) ;
2250 }
2251
2252 Standard_Integer V3d_View::MinMax(Standard_Real& Umin, Standard_Real& Vmin, Standard_Real& Umax, Standard_Real& Vmax) const
2253 {
2254   Standard_Real Wmin,Wmax,U,V,W ;
2255   Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax ;
2256   // CAL 6/11/98
2257   Standard_Integer Nstruct = MyView->NumberOfDisplayedStructures() ;
2258
2259   if( Nstruct ) {
2260     MyView->MinMaxValues(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax) ;
2261     MyView->Projects(Xmin,Ymin,Zmin,Umin,Vmin,Wmin) ;
2262     MyView->Projects(Xmax,Ymax,Zmax,Umax,Vmax,Wmax) ;
2263     MyView->Projects(Xmin,Ymin,Zmax,U,V,W) ;
2264     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
2265     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
2266     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
2267     MyView->Projects(Xmax,Ymin,Zmax,U,V,W) ;
2268     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
2269     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
2270     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
2271     MyView->Projects(Xmax,Ymin,Zmin,U,V,W) ;
2272     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
2273     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
2274     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
2275     MyView->Projects(Xmax,Ymax,Zmin,U,V,W) ;
2276     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
2277     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
2278     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
2279     MyView->Projects(Xmin,Ymax,Zmax,U,V,W) ;
2280     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
2281     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
2282     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
2283     MyView->Projects(Xmin,Ymax,Zmin,U,V,W) ;
2284     Umin = Min(U,Umin) ; Umax = Max(U,Umax) ;
2285     Vmin = Min(V,Vmin) ; Vmax = Max(V,Vmax) ;
2286     Wmin = Min(W,Wmin) ; Wmax = Max(W,Wmax) ;
2287   }
2288   return Nstruct ;
2289 }
2290
2291 Standard_Integer V3d_View::MinMax(Standard_Real& Xmin, Standard_Real& Ymin, Standard_Real& Zmin, Standard_Real& Xmax, Standard_Real& Ymax, Standard_Real& Zmax) const
2292 {
2293   // CAL 6/11/98
2294   // Standard_Integer Nstruct = (MyView->DisplayedStructures())->Extent() ;
2295   Standard_Integer Nstruct = MyView->NumberOfDisplayedStructures() ;
2296
2297   if( Nstruct ) {
2298     MyView->MinMaxValues(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax) ;
2299   }
2300   return Nstruct ;
2301 }
2302
2303 Standard_Integer V3d_View::Gravity(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
2304 {
2305   Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax,U,V,W ;
2306   Standard_Real Umin,Vmin,Umax,Vmax ;
2307   Standard_Integer Nstruct,Npoint ;
2308   Graphic3d_MapOfStructure MySetOfStructures;
2309
2310   MyView->DisplayedStructures (MySetOfStructures);
2311   Nstruct = MySetOfStructures.Extent() ;
2312
2313   Graphic3d_MapIteratorOfMapOfStructure MyIterator(MySetOfStructures) ;
2314
2315   MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax) ;
2316   Npoint = 0 ; X = Y = Z = 0. ;
2317   for( ; MyIterator.More(); MyIterator.Next()) {
2318     if (!(MyIterator.Key())->IsEmpty()) {
2319       (MyIterator.Key())->MinMaxValues(Xmin,Ymin,Zmin,
2320         Xmax,Ymax,Zmax) ;
2321
2322       Standard_Real LIM = ShortRealLast() -1.;
2323       if (!    (Abs(Xmin) > LIM || Abs(Ymin) > LIM || Abs(Zmin) > LIM
2324         ||  Abs(Xmax) > LIM || Abs(Ymax) > LIM || Abs(Zmax) > LIM )) {
2325
2326           MyView->Projects(Xmin,Ymin,Zmin,U,V,W) ;
2327           if( U >= Umin && U <= Umax && V >= Vmin && V <= Vmax ) {
2328             Npoint++ ; X += Xmin ; Y += Ymin ; Z += Zmin ;
2329           }
2330           MyView->Projects(Xmax,Ymin,Zmin,U,V,W) ;
2331           if( U >= Umin && U <= Umax && V >= Vmin && V <= Vmax ) {
2332             Npoint++ ; X += Xmax ; Y += Ymin ; Z += Zmin ;
2333           }
2334           MyView->Projects(Xmin,Ymax,Zmin,U,V,W) ;
2335           if( U >= Umin && U <= Umax && V >= Vmin && V <= Vmax ) {
2336             Npoint++ ; X += Xmin ; Y += Ymax ; Z += Zmin ;
2337           }
2338           MyView->Projects(Xmax,Ymax,Zmin,U,V,W) ;
2339           if( U >= Umin && U <= Umax && V >= Vmin && V <= Vmax ) {
2340             Npoint++ ; X += Xmax ; Y += Ymax ; Z += Zmin ;
2341           }
2342           MyView->Projects(Xmin,Ymin,Zmax,U,V,W) ;
2343           if( U >= Umin && U <= Umax && V >= Vmin && V <= Vmax ) {
2344             Npoint++ ; X += Xmin ; Y += Ymin ; Z += Zmax ;
2345           }
2346           MyView->Projects(Xmax,Ymin,Zmax,U,V,W) ;
2347           if( U >= Umin && U <= Umax && V >= Vmin && V <= Vmax ) {
2348             Npoint++ ; X += Xmax ; Y += Ymin ; Z += Zmax ;
2349           }
2350           MyView->Projects(Xmin,Ymax,Zmax,U,V,W) ;
2351           if( U >= Umin && U <= Umax && V >= Vmin && V <= Vmax ) {
2352             Npoint++ ; X += Xmin ; Y += Ymax ; Z += Zmax ;
2353           }
2354           MyView->Projects(Xmax,Ymax,Zmax,U,V,W) ;
2355           if( U >= Umin && U <= Umax && V >= Vmin && V <= Vmax ) {
2356             Npoint++ ; X += Xmax ; Y += Ymax ; Z += Zmax ;
2357           }
2358         }
2359     }
2360   }
2361   if( Npoint > 0 ) {
2362     X /= Npoint ; Y /= Npoint ; Z /= Npoint ;
2363   }
2364
2365   return Nstruct ;
2366 }
2367
2368 void V3d_View::Eye(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
2369 {
2370   Graphic3d_Vertex Prp ;
2371   Graphic3d_Vector Vpn ;
2372   Standard_Real Xrp,Yrp,Zrp,Xpn,Ypn,Zpn,Xat,Yat,Zat ;
2373
2374   MyViewReferencePoint = MyViewOrientation.ViewReferencePoint() ;
2375   MyViewReferencePoint.Coord(Xat,Yat,Zat) ;
2376   Prp = MyViewMapping.ProjectionReferencePoint() ;
2377   Prp.Coord(Xrp,Yrp,Zrp) ;
2378   Vpn = MyViewOrientation.ViewReferencePlane() ;
2379   Vpn.Coord(Xpn,Ypn,Zpn) ;
2380   X = Zrp*Xpn + Xat; Y = Zrp*Ypn + Yat; Z = Zrp*Zpn + Zat;
2381 }
2382
2383 void V3d_View::FocalReferencePoint(Standard_Real& X, Standard_Real& Y,Standard_Real& Z) const
2384 {
2385   Graphic3d_Vertex PRP,VRPoint;
2386   Graphic3d_Vector VRPlane;
2387   Standard_Real FPD,Xprp,Yprp,Zprp;
2388   Standard_Real Xvrp,Yvrp,Zvrp,DXvrp,DYvrp,DZvrp;
2389
2390   if ( MyType == V3d_PERSPECTIVE ) {
2391     PRP = MyViewMapping.ProjectionReferencePoint() ;
2392     FPD = MyViewMapping.FrontPlaneDistance();
2393     PRP.Coord(Xprp,Yprp,Zprp);
2394     VRPoint = MyViewOrientation.ViewReferencePoint();
2395     VRPoint.Coord(Xvrp,Yvrp,Zvrp);
2396     VRPlane = MyViewOrientation.ViewReferencePlane();
2397     VRPlane.Coord(DXvrp,DYvrp,DZvrp);
2398     X = (FPD + Zprp) * DXvrp + Xvrp;
2399     Y = (FPD + Zprp) * DYvrp + Yvrp;
2400     Z = (FPD + Zprp) * DZvrp + Zvrp;
2401   }
2402   else Eye(X,Y,Z);
2403 }
2404
2405 void V3d_View::ProjReferenceAxe(const Standard_Integer Xpix, const Standard_Integer Ypix, Standard_Real& XP, Standard_Real& YP, Standard_Real& ZP, Standard_Real& VX, Standard_Real& VY, Standard_Real& VZ) const
2406 {
2407   Standard_Real Xo,Yo,Zo;
2408
2409   Convert(Xpix,Ypix,XP,YP,ZP);
2410   if ( MyType == V3d_PERSPECTIVE ) {
2411     FocalReferencePoint(Xo,Yo,Zo);
2412     VX = Xo - XP;
2413     VY = Yo - YP;
2414     VZ = Zo - ZP;
2415   }
2416   else {
2417     Proj(VX,VY,VZ);
2418   }
2419 }
2420
2421 Standard_Real V3d_View::Depth() const
2422 {
2423   Graphic3d_Vertex Prp ;
2424   Standard_Real Xrp,Yrp,Zrp ;
2425
2426   Prp = MyViewMapping.ProjectionReferencePoint() ;
2427   Prp.Coord(Xrp,Yrp,Zrp) ;
2428   return Zrp ;
2429 }
2430
2431 void V3d_View::Proj(Standard_Real& Dx, Standard_Real& Dy, Standard_Real& Dz) const
2432 {
2433   Graphic3d_Vector Vpn ;
2434
2435   Vpn = MyViewOrientation.ViewReferencePlane() ;
2436   Vpn.Coord(Dx,Dy,Dz) ;
2437 }
2438
2439 void V3d_View::At(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
2440 {
2441   Graphic3d_Vertex Vrp ;
2442
2443   Vrp = MyViewOrientation.ViewReferencePoint() ;
2444   Vrp.Coord(X,Y,Z) ;
2445 }
2446
2447 void V3d_View::Up(Standard_Real& Vx, Standard_Real& Vy, Standard_Real& Vz) const
2448 {
2449   Graphic3d_Vector Vup ;
2450
2451   Vup = MyViewOrientation.ViewReferenceUp() ;
2452   Vup.Coord(Vx,Vy,Vz) ;
2453 }
2454
2455 Standard_Real V3d_View::Twist() const
2456 {
2457   Standard_Real Xup,Yup,Zup,Xpn,Ypn,Zpn,X0,Y0,Z0 ;
2458   Standard_Real pvx,pvy,pvz,pvn,sca,angle ;
2459   Graphic3d_Vector Vpn,Vup,Xaxis,Yaxis,Zaxis ;
2460   Standard_Boolean TheStatus ;
2461
2462   Vpn = MyViewOrientation.ViewReferencePlane() ;
2463   Vpn.Coord(Xpn,Ypn,Zpn) ;
2464   Vup.SetCoord(0.,0.,1.) ;
2465   TheStatus = ScreenAxis(Vpn,Vup,Xaxis,Yaxis,Zaxis) ;
2466   if( !TheStatus ) {
2467     Vup.SetCoord(0.,1.,0.) ;
2468     TheStatus = ScreenAxis(Vpn,Vup,Xaxis,Yaxis,Zaxis) ;
2469   }
2470   if( !TheStatus ) {
2471     Vup.SetCoord(1.,0.,0.) ;
2472     TheStatus = ScreenAxis(Vpn,Vup,Xaxis,Yaxis,Zaxis) ;
2473   }
2474   Yaxis.Coord(X0,Y0,Z0) ;
2475   Vup = MyViewOrientation.ViewReferenceUp() ;
2476   Vup.Coord(Xup,Yup,Zup) ;
2477   /* Compute Cross Vector From Up & Origin */
2478   pvx = Y0*Zup - Z0*Yup ;
2479   pvy = Z0*Xup - X0*Zup ;
2480   pvz = X0*Yup - Y0*Xup ;
2481   pvn = pvx*pvx + pvy*pvy + pvz*pvz ;
2482   sca = X0*Xup + Y0*Yup + Z0*Zup ;
2483   /* Compute Angle */
2484   angle = Sqrt(pvn) ;
2485   if( angle > 1. ) angle = 1. ;
2486   else if( angle < -1. ) angle = -1. ;
2487   angle = asin(angle) ;
2488   if( sca < 0. ) angle = M_PI - angle ;
2489   if( angle > 0. && angle < M_PI ) {
2490     sca = pvx*Xpn + pvy*Ypn + pvz*Zpn ;
2491     if( sca < 0. ) angle = DEUXPI - angle ;
2492   }
2493   return angle ;
2494 }
2495
2496 V3d_TypeOfShadingModel V3d_View::ShadingModel() const
2497 {
2498   V3d_TypeOfShadingModel SM = (V3d_TypeOfShadingModel)MyViewContext.Model() ;
2499   return SM ;
2500 }
2501
2502 V3d_TypeOfSurfaceDetail V3d_View::SurfaceDetail() const
2503 {
2504   V3d_TypeOfSurfaceDetail SM = (V3d_TypeOfSurfaceDetail)MyViewContext.SurfaceDetail() ;
2505   return SM ;
2506 }
2507
2508 Handle_Graphic3d_TextureEnv V3d_View::TextureEnv() const
2509 {
2510   Handle(Graphic3d_TextureEnv) SM = MyViewContext.TextureEnv() ;
2511   return SM ;
2512 }
2513
2514 V3d_TypeOfVisualization V3d_View::Visualization() const
2515 {
2516   V3d_TypeOfVisualization V =
2517     (V3d_TypeOfVisualization)MyViewContext.Visualization() ;
2518   return V ;
2519 }
2520
2521 Standard_Boolean V3d_View::Antialiasing() const
2522 {
2523   Standard_Boolean A = MyViewContext.AliasingIsOn() ;
2524   return A ;
2525 }
2526
2527 Handle(V3d_Viewer) V3d_View::Viewer() const
2528 {
2529   return MyViewer ;
2530 }
2531
2532 Standard_Boolean V3d_View::IfWindow() const
2533 {
2534   Standard_Boolean TheStatus = MyView->IsDefined() ;
2535   return TheStatus ;
2536 }
2537
2538 Handle(Aspect_Window) V3d_View::Window() const
2539 {
2540   return MyWindow;
2541 }
2542
2543 V3d_TypeOfView V3d_View::Type() const
2544 {
2545   return (MyType) ;
2546 }
2547
2548 void V3d_View::SetFocale( const Standard_Real focale )
2549 {
2550   Standard_TypeMismatch_Raise_if (MyType != V3d_PERSPECTIVE,
2551     "the view is not a perspective view");
2552   Standard_Real Xrp,Yrp,Zrp,ViewPlane,FrontPlane ;
2553   Graphic3d_Vertex Prp ;
2554   Prp = MyViewMapping.ProjectionReferencePoint() ;
2555   Prp.Coord(Xrp,Yrp,Zrp) ;
2556   if( MyProjModel == V3d_TPM_WALKTHROUGH ) {
2557     ViewPlane = Zrp - focale ;
2558   } else {
2559     FrontPlane = MyViewMapping.FrontPlaneDistance() ;
2560     ViewPlane = FrontPlane + Zrp - focale ;
2561   }
2562   MyViewMapping.SetViewPlaneDistance(ViewPlane) ;
2563   MyView->SetViewMapping(MyViewMapping) ;
2564
2565   ImmediateUpdate();
2566 }
2567
2568 Standard_Real V3d_View::Focale( ) const
2569 {
2570   Standard_Real Xrp,Yrp,Zrp,ViewPlane,FrontPlane ;
2571   Standard_Real focale = 0.0 ;
2572   Graphic3d_Vertex Prp ;
2573
2574   if( MyType == V3d_PERSPECTIVE ) {
2575     Prp = MyViewMapping.ProjectionReferencePoint() ;
2576     Prp.Coord(Xrp,Yrp,Zrp) ;
2577     ViewPlane = MyViewMapping.ViewPlaneDistance() ;
2578     if( MyProjModel == V3d_TPM_WALKTHROUGH ) {
2579       focale = Zrp - ViewPlane ;
2580     } else {
2581       FrontPlane = MyViewMapping.FrontPlaneDistance() ;
2582       focale = FrontPlane + Zrp - ViewPlane ;
2583     }
2584   }
2585   return (focale) ;
2586 }
2587
2588 void V3d_View::SetViewingVolume(const Standard_Real Left, const Standard_Real Right,
2589                                 const Standard_Real Bottom, const Standard_Real Top,
2590                                 const Standard_Real ZNear, const Standard_Real ZFar)
2591 {
2592   Standard_Real Umin,Vmin,Umax,Vmax,Xrp,Yrp,Zrp;
2593
2594   Viewer_BadValue_Raise_if ( ZNear <= 0. || ZFar <= 0. || ZNear >= ZFar, "V3d_View::SetVolume, bad distances");
2595
2596   MyProjReferencePoint = MyViewMapping.ProjectionReferencePoint() ;
2597   MyProjReferencePoint.Coord(Xrp,Yrp,Zrp) ;
2598   Zrp = ZNear;
2599
2600   Standard_Real size = (ZFar - ZNear) / 2.;
2601
2602   MyViewMapping.SetFrontPlaneDistance(size);
2603   MyViewMapping.SetBackPlaneDistance(-size);
2604
2605   // keep view plane at front plane distance
2606   MyViewMapping.SetViewPlaneDistance(MyType == V3d_PERSPECTIVE ? 0. : size);
2607
2608   // set window limits
2609   Umin = Left; Umax = Right;
2610   Vmin = Bottom; Vmax = Top;
2611   MyViewMapping.SetWindowLimit(Left, Bottom, Right, Top);
2612
2613   // Update window center
2614   if ( MyType == V3d_PERSPECTIVE ) {
2615     Xrp = Yrp = 0.0;
2616   }
2617   else {
2618     Xrp = (Umin + Umax)/2. ; Yrp = (Vmin + Vmax)/2. ;
2619   }
2620   MyProjReferencePoint.SetCoord(Xrp,Yrp,Zrp) ;
2621   MyViewMapping.SetProjectionReferencePoint(MyProjReferencePoint);
2622
2623   MyView->SetViewMapping(MyViewMapping) ;
2624   //  SetZSize(0.);
2625   ImmediateUpdate();
2626 }
2627
2628 Handle(Visual3d_View) V3d_View::View() const
2629 {
2630   return MyView ;
2631 }
2632
2633 Visual3d_ViewMapping V3d_View::ViewMapping() const
2634 {
2635   return MyViewMapping;
2636 }
2637
2638 Visual3d_ViewOrientation V3d_View::ViewOrientation() const
2639 {
2640   return MyViewOrientation;
2641 }
2642
2643 Standard_Boolean V3d_View::ScreenAxis( const Graphic3d_Vector &Vpn, const Graphic3d_Vector &Vup, Graphic3d_Vector &Xaxe, Graphic3d_Vector &Yaxe, Graphic3d_Vector &Zaxe)
2644 {
2645   Standard_Real Xpn,Ypn,Zpn,Xup,Yup,Zup ;
2646   Standard_Real dx1,dy1,dz1,dx2,dy2,dz2,xx,yy,zz ;
2647
2648   Vpn.Coord(Xpn,Ypn,Zpn) ;
2649   Vup.Coord(Xup,Yup,Zup) ;
2650   xx = Yup*Zpn - Zup*Ypn ;
2651   yy = Zup*Xpn - Xup*Zpn ;
2652   zz = Xup*Ypn - Yup*Xpn ;
2653   Xaxe.SetCoord(xx,yy,zz) ;
2654   if( Xaxe.LengthZero() ) return Standard_False;
2655   Xaxe.Normalize() ; Xaxe.Coord(dx1,dy1,dz1) ;
2656   xx = Ypn*dz1 - Zpn*dy1 ;
2657   yy = Zpn*dx1 - Xpn*dz1 ;
2658   zz = Xpn*dy1 - Ypn*dx1 ;
2659   Yaxe.SetCoord(xx,yy,zz) ;
2660   if( Yaxe.LengthZero() ) return Standard_False;
2661   Yaxe.Normalize() ; Yaxe.Coord(dx2,dy2,dz2) ;
2662   xx = dy1*dz2 - dz1*dy2 ;
2663   yy = dz1*dx2 - dx1*dz2 ;
2664   zz = dx1*dy2 - dy1*dx2 ;
2665   Zaxe.SetCoord(xx,yy,zz) ;
2666   if( Zaxe.LengthZero() ) return Standard_False;
2667   Zaxe.Normalize() ;
2668   return Standard_True ;
2669 }
2670
2671 void V3d_View::InitMatrix( TColStd_Array2OfReal& Matrix )
2672 {
2673   Standard_Integer LR = Matrix.LowerRow() ;
2674   Standard_Integer UR = Matrix.UpperRow() ;
2675   Standard_Integer LC = Matrix.LowerCol() ;
2676   Standard_Integer UC = Matrix.UpperCol() ;
2677   Standard_Integer I,J ;
2678
2679   for( I=LR ; I<=UR ; I++ ) {
2680     for( J=LC ; J<=UC ; J++ ) Matrix(I,J) = 0. ;
2681   }
2682   for( I=LR,J=LC ; I<=UR ; I++,J++ ) Matrix(I,J) = 1. ;
2683 }
2684
2685 Standard_Boolean V3d_View::Multiply (const TColStd_Array2OfReal& Left, const TColStd_Array2OfReal& Right, TColStd_Array2OfReal& Matrix)
2686 {
2687   Standard_Integer llr = Left.LowerRow ();
2688   Standard_Integer llc = Left.LowerCol ();
2689   Standard_Integer luc = Left.UpperCol ();
2690
2691   Standard_Integer rlr = Right.LowerRow ();
2692   Standard_Integer rur = Right.UpperRow ();
2693   Standard_Integer rlc = Right.LowerCol ();
2694
2695   Standard_Integer mlr = Matrix.LowerRow ();
2696   Standard_Integer mur = Matrix.UpperRow ();
2697   Standard_Integer mlc = Matrix.LowerCol ();
2698   Standard_Integer muc = Matrix.UpperCol ();
2699
2700   TColStd_Array2OfReal Result (mlr, mur, mlc, muc);
2701
2702   InitMatrix (Result);
2703
2704   // Left et Right incompatibles
2705   if (luc - llc + 1 != rur - rlr + 1)
2706     return Standard_False;
2707
2708   Standard_Integer i, j, k;
2709
2710   Standard_Real Som;
2711   Standard_Integer I1 = llr;
2712
2713   for (i=mlr; i<=mur; i++) {
2714     Standard_Integer J2 = rlc;
2715     for (j=mlc; j<=muc; j++) {
2716       Som = 0.0;
2717       Standard_Integer J1 = llc;
2718       Standard_Integer I2 = rlr;
2719       for (k=llc; k<=luc; k++) {
2720         Som = Som + Left (I1, J1) * Right (I2, J2);
2721         J1++;
2722         I2++;
2723       }
2724       Result (i, j) = Som;
2725       J2++;
2726     }
2727     I1++;
2728   }
2729
2730   for (i=mlr; i<=mur; i++)
2731     for (j=mlc; j<=muc; j++)
2732       Matrix (i, j) = Result (i, j);
2733
2734   return Standard_True;
2735 }
2736
2737 /*----------------------------------------------------------------------*/
2738
2739 void V3d_View::RotAxis( const Graphic3d_Vertex &Vrp, const Graphic3d_Vector &Axe, const Standard_Real angle, TColStd_Array2OfReal& Matrix )
2740 {
2741   Standard_Real Xrp,Yrp,Zrp,Xaxe,Yaxe,Zaxe ;
2742   Standard_Real sina,cosa,cos1m,terms1,terms2,terms3 ;
2743   Standard_Real termc12,termc13,termc23,vcal ;
2744   TColStd_Array2OfReal Tmatrix(0,3,0,3) ;
2745   TColStd_Array2OfReal Rmatrix(0,3,0,3) ;
2746
2747   InitMatrix(Matrix) ;
2748   InitMatrix(Tmatrix) ;
2749
2750   Vrp.Coord(Xrp,Yrp,Zrp) ;
2751   Axe.Coord(Xaxe,Yaxe,Zaxe) ;
2752   /* translation of x,y,z */
2753   Tmatrix(0,3) = Xrp ; Tmatrix(1,3) = Yrp ; Tmatrix(2,3) = Zrp ;
2754   /* rotation around an axis */
2755   cosa = cos(angle); sina = sin(angle); cos1m = 1. - cosa ;
2756   termc12 = Xaxe * Yaxe * cos1m ;
2757   termc13 = Xaxe * Zaxe * cos1m ;
2758   termc23 = Yaxe * Zaxe * cos1m ;
2759   terms1 =  Xaxe * sina ;
2760   terms2 =  Yaxe * sina ;
2761   terms3 =  Zaxe * sina ;
2762
2763   vcal =  Xaxe * Xaxe ;
2764   Rmatrix(0,0) = vcal + ( 1. - vcal ) * cosa ;
2765   Rmatrix(1,0) = termc12 + terms3 ;
2766   Rmatrix(2,0) = termc13 - terms2 ;
2767   Rmatrix(3,0) = 0. ;
2768   Rmatrix(0,1) = termc12 - terms3 ;
2769   vcal =  Yaxe * Yaxe ;
2770   Rmatrix(1,1) = vcal + ( 1. - vcal ) * cosa ;
2771   Rmatrix(2,1) = termc23 + terms1 ;
2772   Rmatrix(3,1) = 0. ;
2773   Rmatrix(0,2) = termc13 + terms2 ;
2774   Rmatrix(1,2) = termc23 - terms1 ;
2775   vcal =  Zaxe * Zaxe ;
2776   Rmatrix(2,2) = vcal + ( 1. - vcal ) * cosa ;
2777   Rmatrix(3,2) = 0. ;
2778   Rmatrix(0,3) = 0. ;
2779   Rmatrix(1,3) = 0. ;
2780   Rmatrix(2,3) = 0. ;
2781   Rmatrix(3,3) = 1. ;
2782   /* produced by two matrices */
2783   Multiply(Tmatrix, Rmatrix, Matrix);
2784   /* translation invert */
2785   Tmatrix(0,3) = -Xrp;
2786   Tmatrix(1,3) = -Yrp;
2787   Tmatrix(2,3) = -Zrp;
2788   /* product final */
2789   Multiply(Matrix, Tmatrix, Matrix);
2790 }
2791
2792 /*----------------------------------------------------------------------*/
2793
2794 Graphic3d_Vertex V3d_View::TrsPoint( const Graphic3d_Vertex &P, const TColStd_Array2OfReal &Matrix )
2795 {
2796   Graphic3d_Vertex PP ;
2797   Standard_Real X,Y,Z,XX,YY,ZZ ;
2798
2799   // CAL. S3892
2800   Standard_Integer lr, ur, lc, uc;
2801   lr    = Matrix.LowerRow ();
2802   ur    = Matrix.UpperRow ();
2803   lc    = Matrix.LowerCol ();
2804   uc    = Matrix.UpperCol ();
2805   if ( (ur - lr + 1 != 4) || (uc - lc + 1 != 4) ) {
2806     P.Coord(X,Y,Z) ;
2807     PP.SetCoord(X,Y,Z) ;
2808     return PP ;
2809   }
2810   P.Coord(X,Y,Z) ;
2811   XX = (Matrix(lr,lc+3) + X*Matrix(lr,lc) + Y*Matrix(lr,lc+1)+
2812     Z*Matrix(lr,lc+2))/Matrix(lr+3,lc+3) ;
2813
2814   YY = (Matrix(lr+1,lc+3) + X*Matrix(lr+1,lc) + Y*Matrix(lr+1,lc+1) +
2815     Z*Matrix(lr+1,lc+2))/Matrix(lr+3,lc+3) ;
2816
2817   ZZ = (Matrix(lr+2,lc+3) + X*Matrix(lr+2,lc) + Y*Matrix(lr+2,lc+1) +
2818     Z*Matrix(lr+2,lc+2))/Matrix(lr+3,lc+3) ;
2819   PP.SetCoord(XX,YY,ZZ) ;
2820   return PP ;
2821 }
2822
2823 Graphic3d_Vector V3d_View::TrsPoint( const Graphic3d_Vector& V, const TColStd_Array2OfReal& Matrix )
2824 {
2825   Graphic3d_Vector VV ;
2826   Standard_Real X,Y,Z,XX,YY,ZZ ;
2827
2828   // CAL. S3892
2829   Standard_Integer lr, ur, lc, uc;
2830   lr    = Matrix.LowerRow ();
2831   ur    = Matrix.UpperRow ();
2832   lc    = Matrix.LowerCol ();
2833   uc    = Matrix.UpperCol ();
2834   if ( (ur - lr + 1 != 4) || (uc - lc + 1 != 4) ) {
2835     V.Coord(X,Y,Z) ;
2836     VV.SetCoord(X,Y,Z) ;
2837     return VV ;
2838   }
2839   V.Coord(X,Y,Z) ;
2840   XX = X*Matrix(lr,lc)+Y*Matrix(lr,lc+1)+Z*Matrix(lr,lc+2) ;
2841   YY = X*Matrix(lr+1,lc)+Y*Matrix(lr+1,lc+1)+Z*Matrix(lr+1,lc+2) ;
2842   ZZ = X*Matrix(lr+2,lc)+Y*Matrix(lr+2,lc+1)+Z*Matrix(lr+2,lc+2) ;
2843   VV.SetCoord(XX,YY,ZZ) ; VV.Normalize() ;
2844   return VV ;
2845 }
2846
2847 void V3d_View::Pan(const Standard_Integer Dx, const Standard_Integer Dy,const Quantity_Factor aZoomFactor)
2848 {
2849   Panning(Convert(Dx),Convert(Dy),aZoomFactor,Standard_True);
2850 }
2851
2852 void V3d_View::Zoom (const Standard_Integer X1,
2853                      const Standard_Integer Y1,
2854                      const Standard_Integer X2,
2855                      const Standard_Integer Y2)
2856 {
2857
2858   Standard_Real dx = Standard_Real (X2-X1);
2859   Standard_Real dy = Standard_Real (Y2-Y1);
2860   if ( dx != 0. || dy != 0. ) {
2861     Standard_Real dzoom = Sqrt(dx*dx + dy*dy) / 100. + 1;
2862     dzoom = (dx > 0) ?  dzoom : 1./dzoom;
2863     SetZoom(dzoom, Standard_True);
2864   }
2865 }
2866
2867 void V3d_View::Zoom (const Standard_Integer X1,
2868                      const Standard_Integer Y1)
2869 {
2870   Standard_Real x,y;
2871   Center(x,y);
2872   Standard_Integer ix,iy;
2873   Convert(x,y,ix,iy);
2874   Zoom(ix,iy,X1,Y1);
2875 }
2876
2877 // Defines the point (pixel) of zooming (for the method ZoomAtPoint()).
2878 void V3d_View::StartZoomAtPoint(const Standard_Integer xpix, const Standard_Integer ypix)
2879 {
2880     MyZoomAtPointX = xpix;
2881     MyZoomAtPointY = ypix;
2882 }
2883
2884 // Zooms the model at a pixel defined by the method StartZoomAtPoint().
2885 void V3d_View::ZoomAtPoint(const Standard_Integer mouseStartX,
2886                            const Standard_Integer mouseStartY,
2887                            const Standard_Integer mouseEndX,
2888                            const Standard_Integer mouseEndY)
2889 {
2890     Standard_Boolean update;
2891     V3d_Coordinate X0, Y0, XS, YS;
2892
2893     // Forbid any update.
2894     update = SetImmediateUpdate(Standard_False);
2895
2896     // Get center point
2897     Center(X0, Y0);
2898
2899     // Pan the point to the center of window.
2900     Convert(MyZoomAtPointX, MyZoomAtPointY, XS, YS);
2901     Panning(X0-XS, Y0-YS);
2902
2903     // Zoom
2904     Standard_Real d = Standard_Real ((mouseEndX + mouseEndY) - (mouseStartX + mouseStartY));
2905
2906     Standard_Real dzoom = fabs(d) / 100.0 + 1.0;
2907     dzoom = (d > 0) ?  dzoom : 1.0 / dzoom;
2908     Viewer_BadValue_Raise_if( dzoom <= 0.,"V3d_View::ZoomAtPoint, bad coefficient");
2909
2910     Standard_Real Umin,Vmin,Umax,Vmax;
2911     MyViewMapping.WindowLimit(Umin,Vmin,Umax,Vmax);
2912
2913     MyXwindowCenter = (Umin + Umax) / 2.0;
2914     MyYwindowCenter = (Vmin + Vmax) / 2.0;
2915     MyWindowWidth = Abs(Umax - Umin);
2916     MyWindowHeight = Abs(Vmax - Vmin);
2917
2918     // Ensure that zoom will not be too small or too big.
2919     Standard_Real coef = dzoom;
2920     if (MyWindowWidth < coef * Precision::Confusion())
2921         coef = MyWindowWidth / Precision::Confusion();
2922     else if (MyWindowWidth > coef * 1e12)
2923         coef = MyWindowWidth / 1e12;
2924     if (MyWindowHeight < coef * Precision::Confusion())
2925         coef = MyWindowHeight / Precision::Confusion();
2926     else if (MyWindowHeight > coef * 1e12)
2927         coef = MyWindowHeight / 1e12;
2928
2929     Standard_Real Dxv = MyWindowWidth / coef;
2930     Standard_Real Dyv = MyWindowHeight / coef;
2931
2932     Umin = MyXwindowCenter - Dxv/2.0; Umax = MyXwindowCenter + Dxv/2.0;
2933     Vmin = MyYwindowCenter - Dyv/2.0; Vmax = MyYwindowCenter + Dyv/2.0;
2934
2935     // Pan the point backwards.
2936     Dxv = (XS - X0) / coef;
2937     Dyv = (YS - Y0) / coef;
2938
2939     MyXwindowCenter = (Umin + Umax) / 2.0;
2940     MyYwindowCenter = (Vmin + Vmax) / 2.0;
2941     MyWindowWidth = Abs(Umax - Umin);
2942     MyWindowHeight = Abs(Vmax - Vmin);
2943
2944     Standard_Real Xrp,Yrp,Zrp;
2945     MyProjReferencePoint = MyViewMapping.ProjectionReferencePoint();
2946     MyProjReferencePoint.Coord(Xrp,Yrp,Zrp);
2947
2948     Xrp = MyXwindowCenter - Dxv;
2949     Yrp = MyYwindowCenter - Dyv;
2950
2951     Umin = Xrp - MyWindowWidth / 2.0; Umax = Xrp + MyWindowWidth / 2.0;
2952     Vmin = Yrp - MyWindowHeight / 2.0; Vmax = Yrp + MyWindowHeight / 2.0;
2953
2954     // Set new reference plane coordintes of the window.
2955     MyViewMapping.SetWindowLimit(Umin,Vmin,Umax,Vmax);
2956     if (MyType != V3d_PERSPECTIVE)
2957     {
2958         MyProjReferencePoint.SetCoord(Xrp,Yrp,Zrp);
2959         MyViewMapping.SetProjectionReferencePoint(MyProjReferencePoint);
2960     }
2961     MyView->SetViewMapping(MyViewMapping);
2962
2963     // Update the view.
2964     SetImmediateUpdate(update);
2965     ImmediateUpdate();
2966 }
2967
2968 void V3d_View::AxialScale (const Standard_Integer Dx,
2969                            const Standard_Integer Dy,
2970                            const V3d_TypeOfAxe Axis)
2971 {
2972   if( Dx != 0. || Dy != 0. ) {
2973     Standard_Real Sx, Sy, Sz;
2974     AxialScale( Sx, Sy, Sz );
2975     Standard_Real dscale = Sqrt(Dx*Dx + Dy*Dy) / 100. + 1;
2976     dscale = (Dx > 0) ?  dscale : 1./dscale;
2977     if( Axis == V3d_X ) Sx = dscale;
2978     if( Axis == V3d_Y ) Sy = dscale;
2979     if( Axis == V3d_Z ) Sz = dscale;
2980     SetAxialScale( Sx, Sy, Sz );
2981   }
2982 }
2983
2984 void V3d_View::FitAll(const Handle(Aspect_Window)& aWindow,
2985                       const Standard_Real Xmin,
2986                       const Standard_Real Ymin,
2987                       const Standard_Real Xmax,
2988                       const Standard_Real Ymax)
2989 {
2990
2991   Standard_Real Umin,Umax,Vmin,Vmax,Xrp,Yrp,Zrp ;
2992   Standard_Real DxvOld,DyvOld,DxvNew,DyvNew,RapOld,RapNew ;
2993   Standard_Integer Xpixel,Ypixel;
2994   //Standard_Integer Xleft,Yup,Xright,Ylow ;
2995
2996   Viewer_BadValue_Raise_if( (Xmin == Xmax) || (Ymin == Ymax) ,
2997     "V3d_View::FitAll, Window Size is NULL");
2998
2999   MyProjReferencePoint = MyViewMapping.ProjectionReferencePoint() ;
3000   MyProjReferencePoint.Coord(Xrp,Yrp,Zrp) ;
3001   aWindow->Size(Xpixel,Ypixel);
3002   DxvOld = Xpixel; DyvOld = Ypixel;
3003
3004   if( (DxvOld == 0.) || (DyvOld == 0.) ) return ;
3005   RapOld = DxvOld/DyvOld ;
3006   DxvNew = Abs(Xmax - Xmin) ; DyvNew = Abs(Ymax - Ymin) ;
3007   RapNew = DxvNew/DyvNew ;
3008   if( RapNew >= RapOld ) {
3009     DyvNew = DxvNew/RapOld ;
3010   } else {
3011     DxvNew = DyvNew*RapOld ;
3012   }
3013   Xrp = (Xmin + Xmax)/2. ; Yrp = (Ymin + Ymax)/2. ;
3014   Umin = Xrp - DxvNew/2. ; Vmin = Yrp - DyvNew/2. ;
3015   Umax = Xrp + DxvNew/2. ; Vmax = Yrp + DyvNew/2. ;
3016   MyViewMapping.SetWindowLimit(Umin,Vmin,Umax,Vmax) ;
3017   if( MyType != V3d_PERSPECTIVE ) {
3018     MyProjReferencePoint.SetCoord(Xrp,Yrp,Zrp) ;
3019     MyViewMapping.SetProjectionReferencePoint(MyProjReferencePoint);
3020   }
3021   MyView->SetViewMapping(MyViewMapping) ;
3022 #ifdef IMP020300
3023   Update();
3024 #else
3025   ImmediateUpdate();
3026 #endif
3027 }
3028
3029 #ifdef IMP250900
3030 static Standard_Boolean zRotation = Standard_False;
3031 #endif
3032 void V3d_View::StartRotation(const Standard_Integer X,
3033                              const Standard_Integer Y,
3034                              const Quantity_Ratio zRotationThreshold)
3035 {
3036
3037   sx = X; sy = Y;
3038   Standard_Real x,y;
3039   Size(x,y);
3040   rx = Standard_Real(Convert(x));
3041   ry = Standard_Real(Convert(y));
3042   Gravity(gx,gy,gz);
3043   Rotate(0.,0.,0.,gx,gy,gz,Standard_True);
3044 #ifdef IMP250900
3045   zRotation = Standard_False;
3046   if( zRotationThreshold > 0. ) {
3047     Standard_Real dx = Abs(sx - rx/2.);
3048     Standard_Real dy = Abs(sy - ry/2.);
3049     //  if( dx > rx/3. || dy > ry/3. ) zRotation = Standard_True;
3050     Standard_Real dd = zRotationThreshold * (rx + ry)/2.;
3051     if( dx > dd || dy > dd ) zRotation = Standard_True;
3052   }
3053 #endif
3054
3055 }
3056
3057 void V3d_View::Rotation(const Standard_Integer X,
3058                         const Standard_Integer Y)
3059 {
3060
3061 #ifdef IMP210600
3062   if( rx == 0. || ry == 0. ) {
3063     StartRotation(X,Y);
3064     return;
3065   }
3066 #endif
3067 #ifdef IMP250900
3068   Standard_Real dx=0.,dy=0.,dz=0.;
3069   if( zRotation ) {
3070     dz = atan2(Standard_Real(X)-rx/2., ry/2.-Standard_Real(Y)) -
3071       atan2(sx-rx/2.,ry/2.-sy);
3072   } else {
3073     dx = (Standard_Real(X) - sx) * M_PI / rx;
3074     dy = (sy - Standard_Real(Y)) * M_PI / ry;
3075   }
3076   Rotate(dx, dy, dz, gx, gy, gz, Standard_False);
3077 #else
3078   Standard_Real dx = (Standard_Real(X - sx)) * M_PI;
3079   Standard_Real dy = (Standard_Real(sy - Y)) * M_PI;
3080   Rotate(dx/rx, dy/ry, 0., gx, gy, gz, Standard_False);
3081 #endif
3082 #ifdef IMP020300
3083   if( !myImmediateUpdate ) Update();
3084 #else
3085   myImmediateUpdate = Standard_False;
3086   Rotate(dx/rx, dy/ry, 0., gx, gy, gz, Standard_False);
3087   ZFitAll (Zmargin);    //Don't do that, perf improvment
3088   myImmediateUpdate = Standard_True;
3089   ImmediateUpdate();
3090 #endif
3091 }
3092
3093 void V3d_View::SetAnimationModeOn () {
3094 #ifdef G003
3095   if ( MyAnimationFlags & V3d_FLAG_ANIMATION ) {
3096     if( ComputedMode() ) {  // Deactivates computed mode during animation
3097       MyAnimationFlags |= V3d_FLAG_COMPUTATION;
3098       Standard_Boolean immediatUpdate = myImmediateUpdate;
3099       myImmediateUpdate = Standard_False;
3100       SetComputedMode(Standard_False);
3101       myImmediateUpdate = immediatUpdate;
3102     }
3103     MyView -> SetAnimationModeOn ( MyAnimationFlags & V3d_FLAG_DEGENERATION );
3104   }
3105 #else
3106   MyView->SetAnimationModeOn();
3107 #endif
3108 }
3109
3110 void V3d_View::SetAnimationModeOff () {
3111 #ifdef G003
3112   if ( MyAnimationFlags & V3d_FLAG_ANIMATION ) {
3113     MyView -> SetAnimationModeOff ();
3114     if ( MyAnimationFlags & V3d_FLAG_COMPUTATION ) {
3115       // Reactivates computed mode after animation
3116       MyAnimationFlags &= ~V3d_FLAG_COMPUTATION;
3117       SetComputedMode(Standard_True);
3118     } else if( MyAnimationFlags & V3d_FLAG_DEGENERATION ) {
3119       Update();
3120     }
3121   }
3122 #else
3123   MyView->SetAnimationModeOff();
3124 #endif
3125 }
3126
3127 Standard_Boolean V3d_View::AnimationModeIsOn () const
3128 {
3129   return MyView->AnimationModeIsOn();
3130 }
3131
3132 #ifdef G003
3133 void V3d_View :: SetAnimationMode
3134 (
3135  const Standard_Boolean anAnimationFlag,
3136  const Standard_Boolean aDegenerationFlag
3137  )
3138 {
3139   if ( anAnimationFlag )
3140     MyAnimationFlags |= V3d_FLAG_ANIMATION;
3141   else
3142     MyAnimationFlags &= ~V3d_FLAG_ANIMATION;
3143
3144   if ( aDegenerationFlag )
3145     MyAnimationFlags |= V3d_FLAG_DEGENERATION;
3146   else
3147     MyAnimationFlags &= ~V3d_FLAG_DEGENERATION;
3148
3149 }  // end V3d_View :: SetAnimationMode
3150
3151 Standard_Boolean V3d_View::AnimationMode( Standard_Boolean& isDegenerate ) const
3152 {
3153   isDegenerate = MyAnimationFlags & V3d_FLAG_DEGENERATION;
3154   return MyAnimationFlags & V3d_FLAG_ANIMATION;
3155 }
3156 #endif
3157
3158 void V3d_View::SetDegenerateModeOn()
3159 {
3160 #ifdef G003
3161   SetComputedMode(Standard_False);
3162 #else
3163   MyView->SetDegenerateModeOn();
3164   ImmediateUpdate();
3165 #endif
3166 }
3167
3168 void V3d_View::SetDegenerateModeOff()
3169 {
3170 #ifdef G003
3171   SetComputedMode(Standard_True);
3172 #else
3173   if(myComputedMode) {
3174     MyView->SetDegenerateModeOff();
3175     ImmediateUpdate();
3176   }
3177 #endif
3178 }
3179
3180 Standard_Boolean V3d_View::DegenerateModeIsOn() const
3181 {
3182 #ifdef G003
3183   return !ComputedMode();
3184 #else
3185   return MyView->DegenerateModeIsOn();
3186 #endif
3187 }
3188
3189 #ifdef G003
3190 void V3d_View :: SetComputedMode ( const Standard_Boolean aMode )
3191 {
3192   if( aMode ) {
3193     if( myComputedMode ) {
3194       MyView -> SetComputedMode ( Standard_True );
3195       Update ();
3196     }
3197   } else {
3198     MyView -> SetComputedMode ( Standard_False );
3199     Update ();
3200   }
3201 }
3202
3203 Standard_Boolean V3d_View :: ComputedMode () const
3204 {
3205   return MyView -> ComputedMode ();
3206 }
3207
3208 void V3d_View :: SetBackFacingModel (const V3d_TypeOfBackfacingModel aModel)
3209 {
3210   MyView -> SetBackFacingModel ( Visual3d_TypeOfBackfacingModel(aModel) );
3211   Redraw();
3212 }
3213
3214 V3d_TypeOfBackfacingModel V3d_View :: BackFacingModel () const
3215 {
3216   return V3d_TypeOfBackfacingModel(MyView -> BackFacingModel ());
3217 }
3218 #endif
3219
3220 Standard_Boolean V3d_View::TransientManagerBeginDraw(const Standard_Boolean DoubleBuffer,const Standard_Boolean RetainMode) const
3221 {
3222   return Visual3d_TransientManager::BeginDraw(MyView,DoubleBuffer,RetainMode);
3223 }
3224
3225 void V3d_View::TransientManagerClearDraw() const
3226 {
3227   Visual3d_TransientManager::ClearDraw(MyView);
3228 }
3229
3230 Standard_Boolean V3d_View::TransientManagerBeginAddDraw() const
3231 {
3232   return Visual3d_TransientManager::BeginAddDraw(MyView);
3233 }
3234
3235 void V3d_View::Init()
3236 {
3237   myComputedMode = MyViewer->ComputedMode();
3238 #ifdef G003
3239   if( !myComputedMode || !MyViewer->DefaultComputedMode() ) {
3240     SetComputedMode(Standard_False);
3241   }
3242 #else
3243   if(!myComputedMode)
3244     MyView->SetDegenerateModeOn();
3245   else
3246     if(!MyViewer->DefaultComputedMode()) MyView->SetDegenerateModeOn();
3247 #endif
3248
3249 #ifdef IMP240100
3250   OSD_Environment env_walkthrow("CSF_WALKTHROUGH");
3251   env_walkthrow.SetName( "CSF_WALKTHROUGH" );
3252   if ( env_walkthrow.Value () != "" ) MyProjModel = V3d_TPM_WALKTHROUGH;
3253   else MyProjModel = V3d_TPM_SCREEN;
3254 #endif
3255 }
3256
3257 void V3d_View::SetPlotter(const Handle(Graphic3d_Plotter)& aPlotter)
3258 {
3259   MyPlotter = aPlotter;
3260 }
3261
3262 void V3d_View::Plot()
3263 {
3264   Viewer_BadValue_Raise_if( !MyPlotter.IsNull(), "view has no plotter");
3265   MyView->Plot(MyPlotter);
3266 }
3267
3268 Standard_Real V3d_View::Tumble (const Standard_Integer NbImages, const Standard_Boolean AnimationMode)
3269 {
3270   FullTimer.Reset ();
3271   FullTimer.Start ();
3272
3273   if (AnimationMode) MyView->SetAnimationModeOn();
3274   Standard_Integer i;
3275   Standard_Real delta = 0.01;
3276   Standard_Real xangle, yangle;
3277   xangle = yangle = delta*int(NbImages/3);
3278
3279   Rotate (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Standard_True);
3280   for (i=1; i<=int(NbImages/3); i++)
3281     Rotate (delta*i, 0.0, 0.0, 0.0, 0.0, 0.0, Standard_False);
3282   for (i=1; i<=int(NbImages/3); i++)
3283     Rotate (xangle, delta*i, 0.0, 0.0, 0.0, 0.0, Standard_False);
3284   for (i=1; i<=int(NbImages/3); i++)
3285     Rotate (xangle, yangle, delta*i, 0.0, 0.0, 0.0, Standard_False);
3286   if (AnimationMode) MyView->SetAnimationModeOff();
3287
3288   FullTimer.Stop ();
3289   cout << "For " << NbImages << " Images : " << endl;
3290   FullTimer.Show (cout);
3291   cout << flush;
3292
3293   Standard_Real Seconds, CPUtime;
3294   Standard_Integer Minutes, Hours;
3295
3296   FullTimer.Show (Seconds, Minutes, Hours, CPUtime);
3297   cout << "Result " << (AnimationMode ? "with " : "without ")
3298     << "display list : " << NbImages/CPUtime << " images/Second."
3299     << endl;
3300
3301   return NbImages/CPUtime;
3302 }
3303
3304 #define SCREENCOPY_FILENAME "screencopy3d.gif"
3305 void V3d_View::ScreenCopy (const Handle(PlotMgt_PlotterDriver)& aPlotterDriver,
3306                            const Standard_Boolean fWhiteBackground,
3307                            const Quantity_Factor aPlotScale)
3308 {
3309   TCollection_AsciiString aFileToDump;
3310   Handle(Aspect_WindowDriver) aWindowDriver =
3311 #ifdef WNT
3312     new WNT_WDriver (Handle(WNT_Window)::DownCast(MyWindow));
3313 #else
3314     new Xw_Driver (Handle(Xw_Window)::DownCast(MyWindow));
3315 #endif // WNT
3316   Quantity_Factor    aScale;
3317   Quantity_Length    thePixel;
3318   Quantity_Parameter theWWidth, theWHeight;
3319   Quantity_Parameter thePWidth, thePHeight;
3320   aPlotterDriver -> WorkSpace (thePWidth, thePHeight);
3321   aWindowDriver  -> WorkSpace (theWWidth, theWHeight);
3322   thePixel = aWindowDriver -> Convert (1);
3323
3324   if (theWWidth * theWHeight != 0.) {
3325     if (aPlotScale == 0.) {
3326       aScale = Min (thePWidth / theWWidth, thePHeight / theWHeight);
3327     } else {
3328       // To be changed !!!!!!!!!!!!!!!
3329       aScale = Min (thePWidth / theWWidth, thePHeight / theWHeight);
3330     }
3331
3332     // Set default maps (color, type, etc) for plotter driver
3333     aPlotterDriver -> SetColorMap ( new Aspect_GenericColorMap () );
3334     aPlotterDriver -> SetTypeMap  ( new Aspect_TypeMap         () );
3335     aPlotterDriver -> SetWidthMap ( new Aspect_WidthMap        () );
3336     aPlotterDriver -> SetFontMap  ( new Aspect_FontMap         () );
3337     aPlotterDriver -> SetMarkMap  ( new Aspect_MarkMap         () );
3338
3339     // Set backgroung to white, unihiglight if any
3340     Quantity_Parameter theRed, theGreen, theBlue;
3341     Handle(TColStd_HSequenceOfInteger) theFlags;
3342     Graphic3d_MapOfStructure  theStructures;
3343     BackgroundColor (Quantity_TOC_RGB, theRed, theGreen, theBlue);
3344     if (fWhiteBackground)
3345       SetBackgroundColor (Quantity_NOC_WHITE);
3346     MyView -> DisplayedStructures (theStructures);
3347     theFlags      = new TColStd_HSequenceOfInteger ();
3348     Graphic3d_MapIteratorOfMapOfStructure Iterator (theStructures);
3349     while (Iterator.More ()) {
3350       Handle(Graphic3d_Structure) aStructure = Iterator.Key();
3351       if (aStructure -> IsHighlighted()) {
3352         theFlags -> Append (1);
3353         aStructure -> UnHighlight ();
3354       } else {
3355         theFlags -> Append (0);
3356       }
3357       Iterator.Next ();
3358     }
3359     Redraw ();
3360
3361     // Dump the view
3362     if (aPlotterDriver->IsKind(STANDARD_TYPE(PlotMgt_ImageDriver))) {
3363       aFileToDump  = aPlotterDriver->PlotFileName();
3364     } else {
3365       aFileToDump  = aPlotterDriver->SpoolDirectory();
3366       aFileToDump += SCREENCOPY_FILENAME;
3367     }
3368     MyWindow -> Dump (aFileToDump.ToCString());
3369
3370     Standard_Integer theCurStruct = 1;
3371     Iterator = Graphic3d_MapIteratorOfMapOfStructure (theStructures);
3372     while (Iterator.More ()) {
3373       if (theFlags -> Value(theCurStruct))
3374         Iterator.Key() -> Highlight (Aspect_TOHM_COLOR);
3375       Iterator.Next ();
3376       theCurStruct++;
3377     }
3378     if (fWhiteBackground)
3379       SetBackgroundColor (Quantity_TOC_RGB, theRed, theGreen, theBlue);
3380     Redraw ();
3381
3382     // Draw imagefile by plotter driver
3383     aPlotterDriver -> SetPixelSize (thePixel);
3384     aPlotterDriver -> BeginDraw ();
3385     aPlotterDriver -> DrawImageFile (
3386       aFileToDump.ToCString(), (float)(thePWidth / 2.), (float)(thePHeight / 2.), aScale);
3387     aPlotterDriver -> EndDraw ();
3388   }
3389 }
3390 #undef SCREENCOPY_FILENAME
3391
3392 #include <Aspect.hxx>
3393 #include <Visual3d_Layer.hxx>
3394
3395 ////////////////////////////////////////////////////////////////
3396 Standard_Boolean V3d_View::Dump (const Standard_CString      theFile,
3397                                  const Graphic3d_BufferType& theBufferType)
3398 {
3399   Standard_Integer aWinWidth, aWinHeight;
3400   MyWindow->Size (aWinWidth, aWinHeight);
3401   Image_AlienPixMap anImage;
3402   return ToPixMap (anImage, aWinWidth, aWinHeight, theBufferType) && anImage.Save (theFile);
3403 }
3404
3405 ////////////////////////////////////////////////////////////////
3406 Standard_Boolean V3d_View::Dump (const Standard_CString          theFile,
3407                                  const Aspect_FormatOfSheetPaper theFormat,
3408                                  const Graphic3d_BufferType&     theBufferType)
3409 {
3410   // convert Aspect_FormatOfSheetPaper size to pixel ...
3411   Quantity_Length anSPWidth, anSPHeight;
3412   Aspect::ValuesOfFOSP (theFormat, anSPWidth, anSPHeight);
3413
3414   // adjusting to the ratio width/height ...
3415   Quantity_Length aWinWidth, aWinHeight;
3416   MyWindow->MMSize (aWinWidth, aWinHeight);
3417   Standard_Integer aPixelWidth, aPixelHeight;
3418   MyWindow->Size (aPixelWidth, aPixelHeight);
3419
3420   Quantity_Factor aScale = Min (anSPWidth / aWinWidth, anSPHeight / aWinHeight);
3421   aPixelWidth  = Standard_Integer (aPixelWidth  * aScale);
3422   aPixelHeight = Standard_Integer (aPixelHeight * aScale);
3423
3424   Image_AlienPixMap anImage;
3425   ToPixMap (anImage, aPixelWidth, aPixelHeight, theBufferType);
3426   OSD_Environment anEnvGamma ("CSF_GAMMA_CORRECTION");
3427   TCollection_AsciiString strGamma (anEnvGamma.Value());
3428   if (!anImage.IsEmpty() && !strGamma.IsEmpty())
3429   {
3430     Standard_Real aGammaValue = strGamma.RealValue();
3431     anImage.AdjustGamma (aGammaValue);
3432   }
3433   return anImage.Save (theFile);
3434 }
3435
3436 ////////////////////////////////////////////////////////////////
3437 Standard_Boolean V3d_View::ToPixMap (Image_PixMap&               theImage,
3438                                      const Standard_Integer      theWidth,
3439                                      const Standard_Integer      theHeight,
3440                                      const Graphic3d_BufferType& theBufferType,
3441                                      const Standard_Boolean      theIsForceCentred)
3442 {
3443   // always prefer hardware accelerated offscreen buffer
3444   Graphic3d_CView* cView = (Graphic3d_CView* )MyView->CView();
3445   Graphic3d_PtrFrameBuffer aFBOPtr = NULL;
3446   Graphic3d_PtrFrameBuffer aPrevFBOPtr = (Graphic3d_PtrFrameBuffer )cView->ptrFBO;
3447   Standard_Integer aFBOVPSizeX (theWidth), aFBOVPSizeY (theHeight), aFBOSizeXMax (0), aFBOSizeYMax (0);
3448   Standard_Integer aPrevFBOVPSizeX (0), aPrevFBOVPSizeY (0), aPrevFBOSizeXMax (0), aPrevFBOSizeYMax (0);
3449   if (aPrevFBOPtr != NULL)
3450   {
3451     MyView->FBOGetDimensions (aPrevFBOPtr,
3452                               aPrevFBOVPSizeX, aPrevFBOVPSizeY,
3453                               aPrevFBOSizeXMax, aPrevFBOSizeYMax);
3454     if (aFBOVPSizeX <= aPrevFBOSizeXMax && aFBOVPSizeY <= aPrevFBOSizeYMax)
3455     {
3456       MyView->FBOChangeViewport (aPrevFBOPtr, aFBOVPSizeX, aFBOVPSizeY);
3457       aFBOPtr = aPrevFBOPtr;
3458     }
3459   }
3460
3461   if (aFBOPtr == NULL)
3462   {
3463     // Try to create hardware accelerated buffer
3464     aFBOPtr = MyView->FBOCreate (aFBOVPSizeX, aFBOVPSizeY);
3465     if (aFBOPtr != NULL)
3466     {
3467       MyView->FBOGetDimensions (aFBOPtr,
3468                                 aFBOVPSizeX,  aFBOVPSizeY,
3469                                 aFBOSizeXMax, aFBOSizeYMax);
3470       // reduce viewport in case of hardware limits
3471       if (aFBOVPSizeX > aFBOSizeXMax) aFBOVPSizeX = aFBOSizeXMax;
3472       if (aFBOVPSizeY > aFBOSizeYMax) aFBOVPSizeY = aFBOSizeYMax;
3473       MyView->FBOChangeViewport (aFBOPtr, aFBOVPSizeX, aFBOVPSizeY);
3474     }
3475   }
3476   cView->ptrFBO = aFBOPtr;
3477
3478   // If hardware accelerated buffer - try to use onscreen buffer
3479   // Results may be bad!
3480   if (aFBOPtr == NULL)
3481   {
3482     // retrieve window sizes
3483     Standard_Integer aWinWidth, aWinHeight;
3484     MyWindow->Size (aWinWidth, aWinHeight);
3485
3486     // technically we can reduce existing viewport...
3487     // but currently allow only dumping the window itself
3488     if (aFBOVPSizeX != aWinWidth || aFBOVPSizeY != aWinHeight)
3489     {
3490       return Standard_False;
3491     }
3492   }
3493
3494   //szv: save mapping
3495   Visual3d_ViewMapping prevMapping = MyView->ViewMapping();
3496   Standard_Real Umin, Vmin, Umax, Vmax;
3497
3498   if (theIsForceCentred)
3499   {
3500     //szv: get mapping frame
3501     Standard_Real PUmin, PVmin, PUmax, PVmax;
3502     prevMapping.WindowLimit (PUmin, PVmin, PUmax, PVmax);
3503
3504     //szv: calculate expansion
3505     Umin = PUmin; Vmin = PVmin; Umax = PUmax; Vmax = PVmax;
3506     Standard_Real oldWidth = (PUmax - PUmin), oldHeight = (PVmax - PVmin);
3507     Standard_Real newWidth = (oldHeight * aFBOVPSizeX) / aFBOVPSizeY;
3508     if (newWidth < oldWidth)
3509     {
3510       Standard_Real newHeight = (oldWidth * aFBOVPSizeY) / aFBOVPSizeX;
3511       // Expand height
3512       Standard_Real delta = 0.5 * (newHeight - oldHeight);
3513       Vmin = PVmin - delta;
3514       Vmax = PVmax + delta;
3515     }
3516     else
3517     {
3518       // Expand width
3519       Standard_Real delta = 0.5 * (newWidth - oldWidth);
3520       Umin = PUmin - delta;
3521       Umax = PUmax + delta;
3522     }
3523
3524     //szv: apply expanded mapping
3525     MyViewMapping.SetWindowLimit (Umin, Vmin, Umax, Vmax);
3526     MyView->SetViewMapping (MyViewMapping);
3527   }
3528
3529   //workround for rendering list of Over and Under Layers
3530   if (!MyLayerMgr.IsNull())
3531   {
3532     MyLayerMgr->Compute();
3533   }
3534   Redraw();
3535
3536   //szv: restore mapping
3537   MyViewMapping = prevMapping;
3538   MyView->SetViewMapping (prevMapping);
3539
3540   Standard_Boolean isSuccess = Standard_True;
3541
3542   // allocate image buffer for dumping
3543   if (theImage.IsEmpty()
3544    || (Standard_Size )aFBOVPSizeX != theImage.SizeX()
3545    || (Standard_Size )aFBOVPSizeY != theImage.SizeY())
3546   {
3547     bool isBigEndian = Image_PixMap::IsBigEndianHost();
3548     Image_PixMap::ImgFormat aFormat = Image_PixMap::ImgUNKNOWN;
3549     switch (theBufferType)
3550     {
3551       case Graphic3d_BT_RGB:   aFormat = isBigEndian ? Image_PixMap::ImgRGB  : Image_PixMap::ImgBGR;  break;
3552       case Graphic3d_BT_RGBA:  aFormat = isBigEndian ? Image_PixMap::ImgRGBA : Image_PixMap::ImgBGRA; break;
3553       case Graphic3d_BT_Depth: aFormat = Image_PixMap::ImgGrayF; break;
3554     }
3555
3556     isSuccess = isSuccess && theImage.InitZero (aFormat, aFBOVPSizeX, aFBOVPSizeY);
3557   }
3558   isSuccess = isSuccess && MyView->BufferDump (theImage, theBufferType);
3559
3560   // FBO now useless, free resources
3561   if (aFBOPtr != aPrevFBOPtr)
3562   {
3563     MyView->FBORelease (aFBOPtr);
3564   }
3565   else if (aPrevFBOPtr != NULL)
3566   {
3567     MyView->FBOChangeViewport (aPrevFBOPtr, aPrevFBOVPSizeX, aPrevFBOVPSizeY);
3568   }
3569   cView->ptrFBO = aPrevFBOPtr;
3570   return isSuccess;
3571 }