0024637: Visualization - clean up implementation of rendering in immediate mode
[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 #include <V3d_View.jxx>
15
16 #include <TColStd_Array2OfReal.hxx>
17
18 #include <gp_Dir2d.hxx>
19
20 #include <Aspect.hxx>
21
22 #include <V3d_RectangularGrid.hxx>
23 #include <V3d_CircularGrid.hxx>
24
25 #define MYEPSILON1 0.0001               // Comparison with 0.0
26 #define MYEPSILON2 M_PI / 180.  // Delta between 2 angles
27
28 /*----------------------------------------------------------------------*/
29
30 void V3d_View::SetGrid (const gp_Ax3& aPlane, const Handle(Aspect_Grid)& aGrid)
31 {
32   MyPlane       = aPlane;
33   MyGrid        = aGrid;
34
35   Standard_Real xl, yl, zl;
36   Standard_Real xdx, xdy, xdz;
37   Standard_Real ydx, ydy, ydz;
38   Standard_Real dx, dy, dz;
39   aPlane.Location ().Coord (xl, yl, zl);
40   aPlane.XDirection ().Coord (xdx, xdy, xdz);
41   aPlane.YDirection ().Coord (ydx, ydy, ydz);
42   aPlane.Direction ().Coord (dx, dy, dz);
43
44   Standard_Real CosAlpha = Cos (MyGrid->RotationAngle ());
45   Standard_Real SinAlpha = Sin (MyGrid->RotationAngle ());
46
47   TColStd_Array2OfReal Trsf1 (1, 4, 1, 4);
48   Trsf1 (4, 4) = 1.0;
49   Trsf1 (4, 1) = Trsf1 (4, 2) = Trsf1 (4, 3) = 0.0;
50   // Translation
51   Trsf1 (1, 4) = xl,
52   Trsf1 (2, 4) = yl,
53   Trsf1 (3, 4) = zl;
54   // Transformation change of marker
55   Trsf1 (1, 1) = xdx,
56   Trsf1 (2, 1) = xdy,
57   Trsf1 (3, 1) = xdz,
58   Trsf1 (1, 2) = ydx,
59   Trsf1 (2, 2) = ydy,
60   Trsf1 (3, 2) = ydz,
61   Trsf1 (1, 3) = dx,
62   Trsf1 (2, 3) = dy,
63   Trsf1 (3, 3) = dz;
64
65   TColStd_Array2OfReal Trsf2 (1, 4, 1, 4);
66   Trsf2 (4, 4) = 1.0;
67   Trsf2 (4, 1) = Trsf2 (4, 2) = Trsf2 (4, 3) = 0.0;
68   // Translation of the origin
69   Trsf2 (1, 4) = -MyGrid->XOrigin (),
70   Trsf2 (2, 4) = -MyGrid->YOrigin (),
71   Trsf2 (3, 4) = 0.0;
72   // Rotation Alpha around axis -Z
73   Trsf2 (1, 1) = CosAlpha,
74   Trsf2 (2, 1) = -SinAlpha,
75   Trsf2 (3, 1) = 0.0,
76   Trsf2 (1, 2) = SinAlpha,
77   Trsf2 (2, 2) = CosAlpha,
78   Trsf2 (3, 2) = 0.0,
79   Trsf2 (1, 3) = 0.0,
80   Trsf2 (2, 3) = 0.0,
81   Trsf2 (3, 3) = 1.0;
82
83   Standard_Real valuetrsf;
84   Standard_Real valueoldtrsf;
85   Standard_Real valuenewtrsf;
86   Standard_Integer i, j, k;
87   // Calculation of the product of matrices
88   for (i=1; i<=4; i++)
89       for (j=1; j<=4; j++) {
90     MyTrsf (i, j) = 0.0;
91     for (k=1; k<=4; k++) {
92         valueoldtrsf = Trsf1 (i, k);
93         valuetrsf        = Trsf2 (k, j);
94         valuenewtrsf = MyTrsf (i, j) + valueoldtrsf * valuetrsf;
95         MyTrsf (i, j) = valuenewtrsf;
96     }
97      }
98 }
99
100 void V3d_View::SetGridActivity (const Standard_Boolean AFlag)
101 {
102   if (AFlag) MyGrid->Activate ();
103   else MyGrid->Deactivate ();
104 }
105
106 void V3d_View::SetGridGraphicValues (const Handle(Aspect_Grid)& )
107 {
108 }
109
110
111 void toPolarCoords (const Standard_Real theX, const Standard_Real theY, 
112                           Standard_Real& theR, Standard_Real& thePhi)
113 {
114   theR = Sqrt (theX * theX + theY * theY);
115   thePhi = ATan2 (theY, theX);  
116 }
117
118 void toCartesianCoords (const Standard_Real theR, const Standard_Real thePhi, 
119                               Standard_Real& theX, Standard_Real& theY)
120 {
121   theX = theR * Cos (thePhi);
122   theY = theR * Sin (thePhi);
123 }
124
125 Graphic3d_Vertex V3d_View::Compute (const Graphic3d_Vertex & AVertex) const
126 {
127   Graphic3d_Vertex CurPoint, NewPoint;
128   Standard_Real X1, Y1, Z1, X2, Y2, Z2;
129   Standard_Real XPp, YPp;
130
131   gp_Dir aRefPlane = myCamera->Direction().Reversed();
132   X1 = aRefPlane.X(); Y1 = aRefPlane.Y(); Z1 = aRefPlane.Z();
133   MyPlane.Direction ().Coord (X2, Y2, Z2);
134
135   gp_Dir VPN (X1, Y1, Z1);
136   gp_Dir GPN (X2, Y2, Z2);
137
138   AVertex.Coord (X1, Y1, Z1);
139   Project (X1, Y1, Z1, XPp, YPp);
140
141   // Casw when the plane of the grid and the plane of the view
142   // are perpendicular to MYEPSILON2 close radians
143   if (Abs (VPN.Angle (GPN) - M_PI / 2.) < MYEPSILON2) {
144     NewPoint.SetCoord (X1, Y1, Z1);
145     return NewPoint;
146   }
147
148   Standard_Boolean IsRectangular = 
149     MyGrid->IsKind (STANDARD_TYPE (Aspect_RectangularGrid));
150
151   Graphic3d_Vertex P1;
152
153   Standard_Real x0, y0, z0, x1, y1, z1, x2, y2, z2;
154     
155   P1.SetCoord (0.0, 0.0, 0.0);
156   CurPoint = V3d_View::TrsPoint (P1, MyTrsf);
157   CurPoint.Coord (x0, y0, z0);
158     
159   // get grid axes in world space
160   P1.SetCoord (1.0, 0.0, 0.0);
161   CurPoint = V3d_View::TrsPoint (P1, MyTrsf);
162   CurPoint.Coord (x1, y1, z1);
163   gp_Vec aGridX (gp_Pnt (x0, y0, z0), gp_Pnt (x1, y1, z1));
164   aGridX.Normalize();
165
166   P1.SetCoord (0.0, 1.0, 0.0);
167   CurPoint = V3d_View::TrsPoint (P1, MyTrsf);
168   CurPoint.Coord (x2, y2, z2);
169   gp_Vec aGridY (gp_Pnt (x0, y0, z0), gp_Pnt (x2, y2, z2));
170   aGridY.Normalize();
171
172   // get grid normal
173   MyPlane.Direction().Coord (x2, y2, z2);
174   gp_Vec aPlaneNormal (x2, y2, z2);
175
176   gp_Vec aPointOnPlane = gp_Vec (0.0, 0.0, 0.0);
177
178   AVertex.Coord (x1, y1, z1);
179     
180   // project ray from camera onto grid plane
181   if (!myCamera->IsOrthographic())
182   {
183     gp_Vec aPointFromCamera = gp_Vec (myCamera->Eye(), gp_Pnt (x1, y1, z1));
184     aPointFromCamera.Normalize();
185
186     Standard_Real aT = - gp_Vec (myCamera->Eye().XYZ()).Dot (aPlaneNormal) / 
187                                   aPointFromCamera.Dot (aPlaneNormal);
188     aPointOnPlane = gp_Vec (myCamera->Eye().XYZ()) + aPointFromCamera * aT;
189   } else
190   {
191     gp_Vec aPointFromCamera (myCamera->Direction());
192     gp_Vec aPointOnCamera (gp_Vec (x1, y1, z1) - aPointFromCamera);
193
194     Standard_Real aT = - aPointOnCamera.Dot (aPlaneNormal) / 
195                           aPointFromCamera.Dot (aPlaneNormal);
196     aPointOnPlane = aPointOnCamera + aPointFromCamera * aT;
197   }
198
199   if (IsRectangular) {
200     Standard_Real XS, YS;
201     Handle(Aspect_RectangularGrid) theGrid =
202       *(Handle(Aspect_RectangularGrid) *) &MyGrid;
203     XS = theGrid->XStep (), YS = theGrid->YStep ();
204
205     // project point on plane to grid local space
206     gp_Vec aToPoint (gp_Pnt (x0, y0, z0), 
207                      gp_Pnt (aPointOnPlane.X(), aPointOnPlane.Y(), aPointOnPlane.Z()));
208     Standard_Real anXSteps = Round (aGridX.Dot (aToPoint) / XS);
209     Standard_Real anYSteps = Round (aGridY.Dot (aToPoint) / YS);
210
211     // clamp point to grid
212     gp_Vec aResult = aGridX * anXSteps * XS + aGridY * anYSteps * YS + gp_Vec (x0, y0, z0);
213     NewPoint.SetCoord (aResult.X(), aResult.Y(), aResult.Z());
214
215   } 
216   else // IsCircular
217   {
218     Standard_Real RS;
219     Standard_Integer DN;
220     Standard_Real Alpha;
221     Handle(Aspect_CircularGrid) theGrid =
222       *(Handle(Aspect_CircularGrid) *) &MyGrid;
223     RS = theGrid->RadiusStep ();
224     DN = theGrid->DivisionNumber ();
225     Alpha = M_PI / Standard_Real (DN);
226
227     // project point on plane to grid local space
228     gp_Vec aToPoint (gp_Pnt (x0, y0, z0), 
229                      gp_Pnt (aPointOnPlane.X(), aPointOnPlane.Y(), aPointOnPlane.Z()));
230
231     Standard_Real anR = 0.0, aPhi = 0.0;
232     Standard_Real aLocalX = aGridX.Dot (aToPoint);
233     Standard_Real aLocalY = aGridY.Dot (aToPoint);
234     toPolarCoords (aLocalX, aLocalY, anR, aPhi);
235
236     // clamp point to grid
237     Standard_Real anRSteps  = Round (anR / RS);
238     Standard_Real aPhiSteps = Round (aPhi / Alpha);
239     toCartesianCoords (anRSteps * RS, aPhiSteps * Alpha, aLocalX, aLocalY);
240
241     gp_Vec aResult = aGridX * aLocalX + aGridY * aLocalY + gp_Vec (x0, y0, z0);
242     NewPoint.SetCoord (aResult.X(), aResult.Y(), aResult.Z());
243   }
244
245   return NewPoint;
246 }
247
248 // Triedron methods : the Triedron is a non-zoomable object.
249
250 void V3d_View::ZBufferTriedronSetup(const Quantity_NameOfColor XColor,
251                                     const Quantity_NameOfColor YColor,
252                                     const Quantity_NameOfColor ZColor,
253                                     const Standard_Real        SizeRatio,
254                                     const Standard_Real        AxisDiametr,
255                                     const Standard_Integer     NbFacettes)
256 {
257   MyView->ZBufferTriedronSetup(XColor, YColor, ZColor, SizeRatio, AxisDiametr, NbFacettes);
258 }
259
260 void V3d_View::TriedronDisplay (const Aspect_TypeOfTriedronPosition APosition,
261  const Quantity_NameOfColor AColor, const Standard_Real AScale, const V3d_TypeOfVisualization AMode )
262 {
263   MyView->TriedronDisplay (APosition, AColor, AScale, (AMode == V3d_WIREFRAME));
264 }
265
266 void V3d_View::TriedronErase ( )
267 {
268   MyView->TriedronErase ( );
269 }
270
271 void V3d_View::TriedronEcho (const Aspect_TypeOfTriedronEcho AType )
272 {
273   MyView->TriedronEcho (AType);
274 }
275
276 void V3d_View::GetGraduatedTrihedron(/* Names of axes */
277                                      TCollection_ExtendedString &xname,
278                                      TCollection_ExtendedString &yname,
279                                      TCollection_ExtendedString &zname,
280                                      /* Draw names */
281                                      Standard_Boolean& xdrawname, 
282                                      Standard_Boolean& ydrawname, 
283                                      Standard_Boolean& zdrawname,
284                                      /* Draw values */
285                                      Standard_Boolean& xdrawvalues, 
286                                      Standard_Boolean& ydrawvalues, 
287                                      Standard_Boolean& zdrawvalues,
288                                      /* Draw grid */
289                                      Standard_Boolean& drawgrid,
290                                      /* Draw axes */
291                                      Standard_Boolean& drawaxes,
292                                      /* Number of splits along axes */
293                                      Standard_Integer& nbx, 
294                                      Standard_Integer& nby, 
295                                      Standard_Integer& nbz,
296                                      /* Offset for drawing values */
297                                      Standard_Integer& xoffset, 
298                                      Standard_Integer& yoffset, 
299                                      Standard_Integer& zoffset,
300                                      /* Offset for drawing names of axes */
301                                      Standard_Integer& xaxisoffset, 
302                                      Standard_Integer& yaxisoffset, 
303                                      Standard_Integer& zaxisoffset,
304                                      /* Draw tickmarks */
305                                      Standard_Boolean& xdrawtickmarks, 
306                                      Standard_Boolean& ydrawtickmarks, 
307                                      Standard_Boolean& zdrawtickmarks,
308                                      /* Length of tickmarks */
309                                      Standard_Integer& xtickmarklength, 
310                                      Standard_Integer& ytickmarklength, 
311                                      Standard_Integer& ztickmarklength,
312                                      /* Grid color */
313                                      Quantity_Color& gridcolor,
314                                      /* X name color */
315                                      Quantity_Color& xnamecolor,
316                                      /* Y name color */
317                                      Quantity_Color& ynamecolor,
318                                      /* Z name color */
319                                      Quantity_Color& znamecolor,
320                                      /* X color of axis and values */
321                                      Quantity_Color& xcolor,
322                                      /* Y color of axis and values */
323                                      Quantity_Color& ycolor,
324                                      /* Z color of axis and values */
325                                      Quantity_Color& zcolor,
326                                      /* Name of font for names of axes */
327                                      TCollection_AsciiString &fontOfNames,
328                                      /* Style of names of axes */
329                                      Font_FontAspect& styleOfNames,
330                                      /* Size of names of axes */
331                                      Standard_Integer& sizeOfNames,
332                                      /* Name of font for values */
333                                      TCollection_AsciiString &fontOfValues,
334                                      /* Style of values */
335                                      Font_FontAspect& styleOfValues,
336                                      /* Size of values */
337                                      Standard_Integer& sizeOfValues) const
338 {
339     MyView->GetGraduatedTrihedron(/* Names of axes */
340                                   xname, 
341                                   yname, 
342                                   zname,
343                                   /* Draw names */
344                                   xdrawname, 
345                                   ydrawname, 
346                                   zdrawname,
347                                   /* Draw values */
348                                   xdrawvalues, 
349                                   ydrawvalues, 
350                                   zdrawvalues,
351                                   /* Draw grid */
352                                   drawgrid,
353                                   /* Draw axes */
354                                   drawaxes,
355                                   /* Number of splits along axes */
356                                   nbx, 
357                                   nby, 
358                                   nbz,
359                                   /* Offset for drawing values */
360                                   xoffset, 
361                                   yoffset, 
362                                   zoffset,
363                                   /* Offset for drawing names of axes */
364                                   xaxisoffset, 
365                                   yaxisoffset, 
366                                   zaxisoffset,
367                                   /* Draw tickmarks */
368                                   xdrawtickmarks, 
369                                   ydrawtickmarks, 
370                                   zdrawtickmarks,
371                                   /* Length of tickmarks */
372                                   xtickmarklength, 
373                                   ytickmarklength, 
374                                   ztickmarklength,
375                                   /* Grid color */
376                                   gridcolor,
377                                   /* X name color */
378                                   xnamecolor,
379                                   /* Y name color */
380                                   ynamecolor,
381                                   /* Z name color */
382                                   znamecolor,
383                                   /* X color of axis and values */
384                                   xcolor,
385                                   /* Y color of axis and values */
386                                   ycolor,
387                                   /* Z color of axis and values */
388                                   zcolor,
389                                   /* Name of font for names of axes */
390                                   fontOfNames,
391                                   /* Style of names of axes */
392                                   styleOfNames,
393                                   /* Size of names of axes */
394                                   sizeOfNames,
395                                   /* Name of font for values */
396                                   fontOfValues,
397                                   /* Style of values */
398                                   styleOfValues,
399                                   /* Size of values */
400                                   sizeOfValues);
401 }
402
403 void V3d_View::GraduatedTrihedronDisplay(/* Names of axes */
404                                          const TCollection_ExtendedString &xname, 
405                                          const TCollection_ExtendedString &yname, 
406                                          const TCollection_ExtendedString &zname,
407                                          /* Draw names */
408                                          const Standard_Boolean xdrawname, 
409                                          const Standard_Boolean ydrawname, 
410                                          const Standard_Boolean zdrawname,
411                                          /* Draw values */
412                                          const Standard_Boolean xdrawvalues, 
413                                          const Standard_Boolean ydrawvalues, 
414                                          const Standard_Boolean zdrawvalues,
415                                          /* Draw grid */
416                                          const Standard_Boolean drawgrid,
417                                          /* Draw axes */
418                                          const Standard_Boolean drawaxes,
419                                          /* Number of splits along axes */
420                                          const Standard_Integer nbx, 
421                                          const Standard_Integer nby, 
422                                          const Standard_Integer nbz,
423                                          /* Offset for drawing values */
424                                          const Standard_Integer xoffset, 
425                                          const Standard_Integer yoffset, 
426                                          const Standard_Integer zoffset,
427                                          /* Offset for drawing names of axes */
428                                          const Standard_Integer xaxisoffset, 
429                                          const Standard_Integer yaxisoffset, 
430                                          const Standard_Integer zaxisoffset,
431                                          /* Draw tickmarks */
432                                          const Standard_Boolean xdrawtickmarks, 
433                                          const Standard_Boolean ydrawtickmarks, 
434                                          const Standard_Boolean zdrawtickmarks,
435                                          /* Length of tickmarks */
436                                          const Standard_Integer xtickmarklength, 
437                                          const Standard_Integer ytickmarklength, 
438                                          const Standard_Integer ztickmarklength,
439                                          /* Grid color */
440                                          const Quantity_Color& gridcolor,
441                                          /* X name color */
442                                          const Quantity_Color& xnamecolor,
443                                          /* Y name color */
444                                          const Quantity_Color& ynamecolor,
445                                          /* Z name color */
446                                          const Quantity_Color& znamecolor,
447                                          /* X color of axis and values */
448                                          const Quantity_Color& xcolor,
449                                          /* Y color of axis and values */
450                                          const Quantity_Color& ycolor,
451                                          /* Z color of axis and values */
452                                          const Quantity_Color& zcolor,
453                                          /* Name of font for names of axes */
454                                          const TCollection_AsciiString &fontOfNames,
455                                          /* Style of names of axes */
456                                          const Font_FontAspect styleOfNames,
457                                          /* Size of names of axes */
458                                          const Standard_Integer sizeOfNames,
459                                          /* Name of font for values */
460                                          const TCollection_AsciiString &fontOfValues,
461                                          /* Style of values */
462                                          const Font_FontAspect styleOfValues,
463                                          /* Size of values */
464                                          const Standard_Integer sizeOfValues)
465 {
466     MyView->GraduatedTrihedronDisplay(/* Names of axes */
467                                       xname, 
468                                       yname, 
469                                       zname,
470                                       /* Draw names */
471                                       xdrawname, 
472                                       ydrawname, 
473                                       zdrawname,
474                                       /* Draw values */
475                                       xdrawvalues, 
476                                       ydrawvalues, 
477                                       zdrawvalues,
478                                       /* Draw grid */
479                                       drawgrid,
480                                       /* Draw axes */
481                                       drawaxes,
482                                       /* Number of splits along axes */
483                                       nbx, 
484                                       nby, 
485                                       nbz,
486                                       /* Offset for drawing values */
487                                       xoffset, 
488                                       yoffset, 
489                                       zoffset,
490                                       /* Offset for drawing names of axes */
491                                       xaxisoffset, 
492                                       yaxisoffset, 
493                                       zaxisoffset,
494                                       /* Draw tickmarks */
495                                       xdrawtickmarks, 
496                                       ydrawtickmarks, 
497                                       zdrawtickmarks,
498                                       /* Length of tickmarks */
499                                       xtickmarklength, 
500                                       ytickmarklength, 
501                                       ztickmarklength,
502                                       /* Grid color */
503                                       gridcolor,
504                                       /* X name color */
505                                       xnamecolor,
506                                       /* Y name color */
507                                       ynamecolor,
508                                       /* Z name color */
509                                       znamecolor,
510                                       /* X color of axis and values */
511                                       xcolor,
512                                       /* Y color of axis and values */
513                                       ycolor,
514                                       /* Z color of axis and values */
515                                       zcolor,
516                                       /* Name of font for names of axes */
517                                       fontOfNames,
518                                       /* Style of names of axes */
519                                       styleOfNames,
520                                       /* Size of names of axes */
521                                       sizeOfNames,
522                                       /* Name of font for values */
523                                       fontOfValues,
524                                       /* Style of values */
525                                       styleOfValues,
526                                       /* Size of values */
527                                       sizeOfValues);
528 }
529
530 void V3d_View::GraduatedTrihedronErase()
531 {
532     MyView->GraduatedTrihedronErase();
533 }