1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <V3d_View.jxx>
16 #include <TColStd_Array2OfReal.hxx>
18 #include <gp_Dir2d.hxx>
22 #include <V3d_RectangularGrid.hxx>
23 #include <V3d_CircularGrid.hxx>
25 #define MYEPSILON1 0.0001 // Comparison with 0.0
26 #define MYEPSILON2 M_PI / 180. // Delta between 2 angles
28 /*----------------------------------------------------------------------*/
30 void V3d_View::SetGrid (const gp_Ax3& aPlane, const Handle(Aspect_Grid)& aGrid)
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);
44 Standard_Real CosAlpha = Cos (MyGrid->RotationAngle ());
45 Standard_Real SinAlpha = Sin (MyGrid->RotationAngle ());
47 TColStd_Array2OfReal Trsf1 (1, 4, 1, 4);
49 Trsf1 (4, 1) = Trsf1 (4, 2) = Trsf1 (4, 3) = 0.0;
54 // Transformation change of marker
65 TColStd_Array2OfReal Trsf2 (1, 4, 1, 4);
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 (),
72 // Rotation Alpha around axis -Z
73 Trsf2 (1, 1) = CosAlpha,
74 Trsf2 (2, 1) = -SinAlpha,
76 Trsf2 (1, 2) = SinAlpha,
77 Trsf2 (2, 2) = CosAlpha,
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
89 for (j=1; j<=4; j++) {
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;
100 void V3d_View::SetGridActivity (const Standard_Boolean AFlag)
102 if (AFlag) MyGrid->Activate ();
103 else MyGrid->Deactivate ();
106 void V3d_View::SetGridGraphicValues (const Handle(Aspect_Grid)& )
111 void toPolarCoords (const Standard_Real theX, const Standard_Real theY,
112 Standard_Real& theR, Standard_Real& thePhi)
114 theR = Sqrt (theX * theX + theY * theY);
115 thePhi = ATan2 (theY, theX);
118 void toCartesianCoords (const Standard_Real theR, const Standard_Real thePhi,
119 Standard_Real& theX, Standard_Real& theY)
121 theX = theR * Cos (thePhi);
122 theY = theR * Sin (thePhi);
125 Graphic3d_Vertex V3d_View::Compute (const Graphic3d_Vertex & AVertex) const
127 Graphic3d_Vertex CurPoint, NewPoint;
128 Standard_Real X1, Y1, Z1, X2, Y2, Z2;
129 Standard_Real XPp, YPp;
131 gp_Dir aRefPlane = myCamera->Direction().Reversed();
132 X1 = aRefPlane.X(); Y1 = aRefPlane.Y(); Z1 = aRefPlane.Z();
133 MyPlane.Direction ().Coord (X2, Y2, Z2);
135 gp_Dir VPN (X1, Y1, Z1);
136 gp_Dir GPN (X2, Y2, Z2);
138 AVertex.Coord (X1, Y1, Z1);
139 Project (X1, Y1, Z1, XPp, YPp);
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);
148 Standard_Boolean IsRectangular =
149 MyGrid->IsKind (STANDARD_TYPE (Aspect_RectangularGrid));
153 Standard_Real x0, y0, z0, x1, y1, z1, x2, y2, z2;
155 P1.SetCoord (0.0, 0.0, 0.0);
156 CurPoint = V3d_View::TrsPoint (P1, MyTrsf);
157 CurPoint.Coord (x0, y0, z0);
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));
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));
173 MyPlane.Direction().Coord (x2, y2, z2);
174 gp_Vec aPlaneNormal (x2, y2, z2);
176 gp_Vec aPointOnPlane = gp_Vec (0.0, 0.0, 0.0);
178 AVertex.Coord (x1, y1, z1);
180 // project ray from camera onto grid plane
181 if (!myCamera->IsOrthographic())
183 gp_Vec aPointFromCamera = gp_Vec (myCamera->Eye(), gp_Pnt (x1, y1, z1));
184 aPointFromCamera.Normalize();
186 Standard_Real aT = - gp_Vec (myCamera->Eye().XYZ()).Dot (aPlaneNormal) /
187 aPointFromCamera.Dot (aPlaneNormal);
188 aPointOnPlane = gp_Vec (myCamera->Eye().XYZ()) + aPointFromCamera * aT;
191 gp_Vec aPointFromCamera (myCamera->Direction());
192 gp_Vec aPointOnCamera (gp_Vec (x1, y1, z1) - aPointFromCamera);
194 Standard_Real aT = - aPointOnCamera.Dot (aPlaneNormal) /
195 aPointFromCamera.Dot (aPlaneNormal);
196 aPointOnPlane = aPointOnCamera + aPointFromCamera * aT;
200 Standard_Real XS, YS;
201 Handle(Aspect_RectangularGrid) theGrid =
202 *(Handle(Aspect_RectangularGrid) *) &MyGrid;
203 XS = theGrid->XStep (), YS = theGrid->YStep ();
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);
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());
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);
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()));
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);
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);
241 gp_Vec aResult = aGridX * aLocalX + aGridY * aLocalY + gp_Vec (x0, y0, z0);
242 NewPoint.SetCoord (aResult.X(), aResult.Y(), aResult.Z());
248 // Triedron methods : the Triedron is a non-zoomable object.
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)
257 MyView->ZBufferTriedronSetup(XColor, YColor, ZColor, SizeRatio, AxisDiametr, NbFacettes);
260 void V3d_View::TriedronDisplay (const Aspect_TypeOfTriedronPosition APosition,
261 const Quantity_NameOfColor AColor, const Standard_Real AScale, const V3d_TypeOfVisualization AMode )
263 MyView->TriedronDisplay (APosition, AColor, AScale, (AMode == V3d_WIREFRAME));
266 void V3d_View::TriedronErase ( )
268 MyView->TriedronErase ( );
271 void V3d_View::TriedronEcho (const Aspect_TypeOfTriedronEcho AType )
273 MyView->TriedronEcho (AType);
276 void V3d_View::GetGraduatedTrihedron(/* Names of axes */
277 TCollection_ExtendedString &xname,
278 TCollection_ExtendedString &yname,
279 TCollection_ExtendedString &zname,
281 Standard_Boolean& xdrawname,
282 Standard_Boolean& ydrawname,
283 Standard_Boolean& zdrawname,
285 Standard_Boolean& xdrawvalues,
286 Standard_Boolean& ydrawvalues,
287 Standard_Boolean& zdrawvalues,
289 Standard_Boolean& drawgrid,
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,
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,
313 Quantity_Color& gridcolor,
315 Quantity_Color& xnamecolor,
317 Quantity_Color& ynamecolor,
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,
337 Standard_Integer& sizeOfValues) const
339 MyView->GetGraduatedTrihedron(/* Names of axes */
355 /* Number of splits along axes */
359 /* Offset for drawing values */
363 /* Offset for drawing names of axes */
371 /* Length of tickmarks */
383 /* X color of axis and values */
385 /* Y color of axis and values */
387 /* Z color of axis and values */
389 /* Name of font for names of axes */
391 /* Style of names of axes */
393 /* Size of names of axes */
395 /* Name of font for values */
397 /* Style of values */
403 void V3d_View::GraduatedTrihedronDisplay(/* Names of axes */
404 const TCollection_ExtendedString &xname,
405 const TCollection_ExtendedString &yname,
406 const TCollection_ExtendedString &zname,
408 const Standard_Boolean xdrawname,
409 const Standard_Boolean ydrawname,
410 const Standard_Boolean zdrawname,
412 const Standard_Boolean xdrawvalues,
413 const Standard_Boolean ydrawvalues,
414 const Standard_Boolean zdrawvalues,
416 const Standard_Boolean drawgrid,
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,
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,
440 const Quantity_Color& gridcolor,
442 const Quantity_Color& xnamecolor,
444 const Quantity_Color& ynamecolor,
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,
464 const Standard_Integer sizeOfValues)
466 MyView->GraduatedTrihedronDisplay(/* Names of axes */
482 /* Number of splits along axes */
486 /* Offset for drawing values */
490 /* Offset for drawing names of axes */
498 /* Length of tickmarks */
510 /* X color of axis and values */
512 /* Y color of axis and values */
514 /* Z color of axis and values */
516 /* Name of font for names of axes */
518 /* Style of names of axes */
520 /* Size of names of axes */
522 /* Name of font for values */
524 /* Style of values */
530 void V3d_View::GraduatedTrihedronErase()
532 MyView->GraduatedTrihedronErase();