0027667: OCAF binary persistence hangs on reading truncated CBF file
[occt.git] / src / V3d / V3d_View_4.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <Aspect_GradientBackground.hxx>
16 #include <Aspect_Grid.hxx>
17 #include <Aspect_RectangularGrid.hxx>
18 #include <Aspect_Window.hxx>
19 #include <Bnd_Box.hxx>
20 #include <gp_Ax3.hxx>
21 #include <gp_Dir.hxx>
22 #include <gp_Dir2d.hxx>
23 #include <Graphic3d_Group.hxx>
24 #include <Graphic3d_Structure.hxx>
25 #include <Graphic3d_TextureEnv.hxx>
26 #include <Graphic3d_Vector.hxx>
27 #include <Quantity_Color.hxx>
28 #include <Standard_MultiplyDefined.hxx>
29 #include <Standard_TypeMismatch.hxx>
30 #include <TColStd_Array2OfReal.hxx>
31 #include <V3d_BadValue.hxx>
32 #include <V3d_CircularGrid.hxx>
33 #include <V3d_Light.hxx>
34 #include <V3d_RectangularGrid.hxx>
35 #include <V3d_UnMapped.hxx>
36 #include <V3d_View.hxx>
37 #include <V3d_Viewer.hxx>
38 #include <Aspect_CircularGrid.hxx>
39
40 #define MYEPSILON1 0.0001               // Comparison with 0.0
41 #define MYEPSILON2 M_PI / 180.  // Delta between 2 angles
42
43 //=============================================================================
44 //function : SetGrid
45 //purpose  :
46 //=============================================================================
47 void V3d_View::SetGrid (const gp_Ax3& aPlane, const Handle(Aspect_Grid)& aGrid)
48 {
49   MyPlane       = aPlane;
50   MyGrid        = aGrid;
51
52   Standard_Real xl, yl, zl;
53   Standard_Real xdx, xdy, xdz;
54   Standard_Real ydx, ydy, ydz;
55   Standard_Real dx, dy, dz;
56   aPlane.Location ().Coord (xl, yl, zl);
57   aPlane.XDirection ().Coord (xdx, xdy, xdz);
58   aPlane.YDirection ().Coord (ydx, ydy, ydz);
59   aPlane.Direction ().Coord (dx, dy, dz);
60
61   Standard_Real CosAlpha = Cos (MyGrid->RotationAngle ());
62   Standard_Real SinAlpha = Sin (MyGrid->RotationAngle ());
63
64   TColStd_Array2OfReal Trsf1 (1, 4, 1, 4);
65   Trsf1 (4, 4) = 1.0;
66   Trsf1 (4, 1) = Trsf1 (4, 2) = Trsf1 (4, 3) = 0.0;
67   // Translation
68   Trsf1 (1, 4) = xl,
69   Trsf1 (2, 4) = yl,
70   Trsf1 (3, 4) = zl;
71   // Transformation change of marker
72   Trsf1 (1, 1) = xdx,
73   Trsf1 (2, 1) = xdy,
74   Trsf1 (3, 1) = xdz,
75   Trsf1 (1, 2) = ydx,
76   Trsf1 (2, 2) = ydy,
77   Trsf1 (3, 2) = ydz,
78   Trsf1 (1, 3) = dx,
79   Trsf1 (2, 3) = dy,
80   Trsf1 (3, 3) = dz;
81
82   TColStd_Array2OfReal Trsf2 (1, 4, 1, 4);
83   Trsf2 (4, 4) = 1.0;
84   Trsf2 (4, 1) = Trsf2 (4, 2) = Trsf2 (4, 3) = 0.0;
85   // Translation of the origin
86   Trsf2 (1, 4) = -MyGrid->XOrigin (),
87   Trsf2 (2, 4) = -MyGrid->YOrigin (),
88   Trsf2 (3, 4) = 0.0;
89   // Rotation Alpha around axis -Z
90   Trsf2 (1, 1) = CosAlpha,
91   Trsf2 (2, 1) = -SinAlpha,
92   Trsf2 (3, 1) = 0.0,
93   Trsf2 (1, 2) = SinAlpha,
94   Trsf2 (2, 2) = CosAlpha,
95   Trsf2 (3, 2) = 0.0,
96   Trsf2 (1, 3) = 0.0,
97   Trsf2 (2, 3) = 0.0,
98   Trsf2 (3, 3) = 1.0;
99
100   Standard_Real valuetrsf;
101   Standard_Real valueoldtrsf;
102   Standard_Real valuenewtrsf;
103   Standard_Integer i, j, k;
104   // Calculation of the product of matrices
105   for (i=1; i<=4; i++)
106       for (j=1; j<=4; j++) {
107     MyTrsf (i, j) = 0.0;
108     for (k=1; k<=4; k++) {
109         valueoldtrsf = Trsf1 (i, k);
110         valuetrsf        = Trsf2 (k, j);
111         valuenewtrsf = MyTrsf (i, j) + valueoldtrsf * valuetrsf;
112         MyTrsf (i, j) = valuenewtrsf;
113     }
114      }
115 }
116
117 //=============================================================================
118 //function : SetGridActivity
119 //purpose  :
120 //=============================================================================
121 void V3d_View::SetGridActivity (const Standard_Boolean AFlag)
122 {
123   if (AFlag) MyGrid->Activate ();
124   else MyGrid->Deactivate ();
125 }
126
127 //=============================================================================
128 //function : toPolarCoords
129 //purpose  :
130 //=============================================================================
131 void toPolarCoords (const Standard_Real theX, const Standard_Real theY, 
132                           Standard_Real& theR, Standard_Real& thePhi)
133 {
134   theR = Sqrt (theX * theX + theY * theY);
135   thePhi = ATan2 (theY, theX);  
136 }
137
138 //=============================================================================
139 //function : toCartesianCoords
140 //purpose  :
141 //=============================================================================
142 void toCartesianCoords (const Standard_Real theR, const Standard_Real thePhi, 
143                               Standard_Real& theX, Standard_Real& theY)
144 {
145   theX = theR * Cos (thePhi);
146   theY = theR * Sin (thePhi);
147 }
148
149 //=============================================================================
150 //function : Compute
151 //purpose  :
152 //=============================================================================
153 Graphic3d_Vertex V3d_View::Compute (const Graphic3d_Vertex & AVertex) const
154 {
155   Graphic3d_Vertex CurPoint, NewPoint;
156   Standard_Real X1, Y1, Z1, X2, Y2, Z2;
157   Standard_Real XPp, YPp;
158   Handle(Graphic3d_Camera) aCamera = Camera();
159
160   gp_Dir aRefPlane = aCamera->Direction().Reversed();
161   X1 = aRefPlane.X(); Y1 = aRefPlane.Y(); Z1 = aRefPlane.Z();
162   MyPlane.Direction ().Coord (X2, Y2, Z2);
163
164   gp_Dir VPN (X1, Y1, Z1);
165   gp_Dir GPN (X2, Y2, Z2);
166
167   AVertex.Coord (X1, Y1, Z1);
168   Project (X1, Y1, Z1, XPp, YPp);
169
170   // Casw when the plane of the grid and the plane of the view
171   // are perpendicular to MYEPSILON2 close radians
172   if (Abs (VPN.Angle (GPN) - M_PI / 2.) < MYEPSILON2) {
173     NewPoint.SetCoord (X1, Y1, Z1);
174     return NewPoint;
175   }
176
177   Standard_Boolean IsRectangular = 
178     MyGrid->IsKind (STANDARD_TYPE (Aspect_RectangularGrid));
179
180   Graphic3d_Vertex P1;
181
182   Standard_Real x0, y0, z0, x1, y1, z1, x2, y2, z2;
183     
184   P1.SetCoord (0.0, 0.0, 0.0);
185   CurPoint = V3d_View::TrsPoint (P1, MyTrsf);
186   CurPoint.Coord (x0, y0, z0);
187     
188   // get grid axes in world space
189   P1.SetCoord (1.0, 0.0, 0.0);
190   CurPoint = V3d_View::TrsPoint (P1, MyTrsf);
191   CurPoint.Coord (x1, y1, z1);
192   gp_Vec aGridX (gp_Pnt (x0, y0, z0), gp_Pnt (x1, y1, z1));
193   aGridX.Normalize();
194
195   P1.SetCoord (0.0, 1.0, 0.0);
196   CurPoint = V3d_View::TrsPoint (P1, MyTrsf);
197   CurPoint.Coord (x2, y2, z2);
198   gp_Vec aGridY (gp_Pnt (x0, y0, z0), gp_Pnt (x2, y2, z2));
199   aGridY.Normalize();
200
201   // get grid normal
202   MyPlane.Direction().Coord (x2, y2, z2);
203   gp_Vec aPlaneNormal (x2, y2, z2);
204
205   gp_Vec aPointOnPlane = gp_Vec (0.0, 0.0, 0.0);
206
207   AVertex.Coord (x1, y1, z1);
208     
209   // project ray from camera onto grid plane
210   gp_Vec aProjection  = aCamera->IsOrthographic()
211                       ? gp_Vec (aCamera->Direction())
212                       : gp_Vec (aCamera->Eye(), gp_Pnt (x1, y1, z1)).Normalized();
213   gp_Vec aPointOrigin = gp_Vec (gp_Pnt (x1, y1, z1), gp_Pnt (x0, y0, z0));
214   Standard_Real aT    = aPointOrigin.Dot (aPlaneNormal) / aProjection.Dot (aPlaneNormal);
215   aPointOnPlane       = gp_Vec (x1, y1, z1) + aProjection * aT;
216
217   if (IsRectangular) {
218     Standard_Real XS, YS;
219     Handle(Aspect_RectangularGrid) theGrid =
220       Handle(Aspect_RectangularGrid)::DownCast (MyGrid);
221     XS = theGrid->XStep (), YS = theGrid->YStep ();
222
223     // project point on plane to grid local space
224     gp_Vec aToPoint (gp_Pnt (x0, y0, z0), 
225                      gp_Pnt (aPointOnPlane.X(), aPointOnPlane.Y(), aPointOnPlane.Z()));
226     Standard_Real anXSteps = Round (aGridX.Dot (aToPoint) / XS);
227     Standard_Real anYSteps = Round (aGridY.Dot (aToPoint) / YS);
228
229     // clamp point to grid
230     gp_Vec aResult = aGridX * anXSteps * XS + aGridY * anYSteps * YS + gp_Vec (x0, y0, z0);
231     NewPoint.SetCoord (aResult.X(), aResult.Y(), aResult.Z());
232
233   } 
234   else // IsCircular
235   {
236     Standard_Real RS;
237     Standard_Integer DN;
238     Standard_Real Alpha;
239     Handle(Aspect_CircularGrid) theGrid =
240       Handle(Aspect_CircularGrid)::DownCast (MyGrid);
241     RS = theGrid->RadiusStep ();
242     DN = theGrid->DivisionNumber ();
243     Alpha = M_PI / Standard_Real (DN);
244
245     // project point on plane to grid local space
246     gp_Vec aToPoint (gp_Pnt (x0, y0, z0), 
247                      gp_Pnt (aPointOnPlane.X(), aPointOnPlane.Y(), aPointOnPlane.Z()));
248
249     Standard_Real anR = 0.0, aPhi = 0.0;
250     Standard_Real aLocalX = aGridX.Dot (aToPoint);
251     Standard_Real aLocalY = aGridY.Dot (aToPoint);
252     toPolarCoords (aLocalX, aLocalY, anR, aPhi);
253
254     // clamp point to grid
255     Standard_Real anRSteps  = Round (anR / RS);
256     Standard_Real aPhiSteps = Round (aPhi / Alpha);
257     toCartesianCoords (anRSteps * RS, aPhiSteps * Alpha, aLocalX, aLocalY);
258
259     gp_Vec aResult = aGridX * aLocalX + aGridY * aLocalY + gp_Vec (x0, y0, z0);
260     NewPoint.SetCoord (aResult.X(), aResult.Y(), aResult.Z());
261   }
262
263   return NewPoint;
264 }
265
266 //=============================================================================
267 //function : ZBufferTriedronSetup
268 //purpose  :
269 //=============================================================================
270 void V3d_View::ZBufferTriedronSetup(const Quantity_NameOfColor theXColor,
271                                     const Quantity_NameOfColor theYColor,
272                                     const Quantity_NameOfColor theZColor,
273                                     const Standard_Real        theSizeRatio,
274                                     const Standard_Real        theAxisDiametr,
275                                     const Standard_Integer     theNbFacettes)
276 {
277   myTrihedron->SetArrowsColor   (theXColor, theYColor, theZColor);
278   myTrihedron->SetSizeRatio     (theSizeRatio);
279   myTrihedron->SetNbFacets      (theNbFacettes);
280   myTrihedron->SetArrowDiameter (theAxisDiametr);
281 }
282
283 //=============================================================================
284 //function : TriedronDisplay
285 //purpose  :
286 //=============================================================================
287 void V3d_View::TriedronDisplay (const Aspect_TypeOfTriedronPosition thePosition,
288                                 const Quantity_NameOfColor theColor,
289                                 const Standard_Real theScale,
290                                 const V3d_TypeOfVisualization theMode)
291 {
292   myTrihedron->SetLabelsColor (theColor);
293   myTrihedron->SetScale       (theScale);
294   myTrihedron->SetPosition    (thePosition);
295   myTrihedron->SetWireframe   (theMode == V3d_WIREFRAME);
296
297   myTrihedron->Display (*this);
298 }
299
300 //=============================================================================
301 //function : TriedronErase
302 //purpose  :
303 //=============================================================================
304 void V3d_View::TriedronErase()
305 {
306   myTrihedron->Erase();
307 }
308
309 //=============================================================================
310 //function : GetGraduatedTrihedron
311 //purpose  :
312 //=============================================================================
313 const Graphic3d_GraduatedTrihedron& V3d_View::GetGraduatedTrihedron() const
314 {
315   return myView->GetGraduatedTrihedron();
316 }
317
318 //=============================================================================
319 //function : GraduatedTrihedronDisplay
320 //purpose  :
321 //=============================================================================
322 void V3d_View::GraduatedTrihedronDisplay(const Graphic3d_GraduatedTrihedron& theTrihedronData)
323 {
324   myView->GraduatedTrihedronDisplay (theTrihedronData);
325 }
326
327 //=============================================================================
328 //function : GraduatedTrihedronErase
329 //purpose  :
330 //=============================================================================
331 void V3d_View::GraduatedTrihedronErase()
332 {
333   myView->GraduatedTrihedronErase();
334 }