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>
20 #include <V3d_RectangularGrid.hxx>
21 #include <V3d_CircularGrid.hxx>
23 #define MYEPSILON1 0.0001 // Comparison with 0.0
24 #define MYEPSILON2 M_PI / 180. // Delta between 2 angles
26 /*----------------------------------------------------------------------*/
28 void V3d_View::SetGrid (const gp_Ax3& aPlane, const Handle(Aspect_Grid)& aGrid)
33 Standard_Real xl, yl, zl;
34 Standard_Real xdx, xdy, xdz;
35 Standard_Real ydx, ydy, ydz;
36 Standard_Real dx, dy, dz;
37 aPlane.Location ().Coord (xl, yl, zl);
38 aPlane.XDirection ().Coord (xdx, xdy, xdz);
39 aPlane.YDirection ().Coord (ydx, ydy, ydz);
40 aPlane.Direction ().Coord (dx, dy, dz);
42 Standard_Real CosAlpha = Cos (MyGrid->RotationAngle ());
43 Standard_Real SinAlpha = Sin (MyGrid->RotationAngle ());
45 TColStd_Array2OfReal Trsf1 (1, 4, 1, 4);
47 Trsf1 (4, 1) = Trsf1 (4, 2) = Trsf1 (4, 3) = 0.0;
52 // Transformation change of marker
63 TColStd_Array2OfReal Trsf2 (1, 4, 1, 4);
65 Trsf2 (4, 1) = Trsf2 (4, 2) = Trsf2 (4, 3) = 0.0;
66 // Translation of the origin
67 Trsf2 (1, 4) = -MyGrid->XOrigin (),
68 Trsf2 (2, 4) = -MyGrid->YOrigin (),
70 // Rotation Alpha around axis -Z
71 Trsf2 (1, 1) = CosAlpha,
72 Trsf2 (2, 1) = -SinAlpha,
74 Trsf2 (1, 2) = SinAlpha,
75 Trsf2 (2, 2) = CosAlpha,
81 Standard_Real valuetrsf;
82 Standard_Real valueoldtrsf;
83 Standard_Real valuenewtrsf;
84 Standard_Integer i, j, k;
85 // Calculation of the product of matrices
87 for (j=1; j<=4; j++) {
89 for (k=1; k<=4; k++) {
90 valueoldtrsf = Trsf1 (i, k);
91 valuetrsf = Trsf2 (k, j);
92 valuenewtrsf = MyTrsf (i, j) + valueoldtrsf * valuetrsf;
93 MyTrsf (i, j) = valuenewtrsf;
98 void V3d_View::SetGridActivity (const Standard_Boolean AFlag)
100 if (AFlag) MyGrid->Activate ();
101 else MyGrid->Deactivate ();
104 void V3d_View::SetGridGraphicValues (const Handle(Aspect_Grid)& )
109 void toPolarCoords (const Standard_Real theX, const Standard_Real theY,
110 Standard_Real& theR, Standard_Real& thePhi)
112 theR = Sqrt (theX * theX + theY * theY);
113 thePhi = ATan2 (theY, theX);
116 void toCartesianCoords (const Standard_Real theR, const Standard_Real thePhi,
117 Standard_Real& theX, Standard_Real& theY)
119 theX = theR * Cos (thePhi);
120 theY = theR * Sin (thePhi);
123 Graphic3d_Vertex V3d_View::Compute (const Graphic3d_Vertex & AVertex) const
125 Graphic3d_Vertex CurPoint, NewPoint;
126 Standard_Real X1, Y1, Z1, X2, Y2, Z2;
127 Standard_Real XPp, YPp;
129 gp_Dir aRefPlane = myCamera->Direction().Reversed();
130 X1 = aRefPlane.X(); Y1 = aRefPlane.Y(); Z1 = aRefPlane.Z();
131 MyPlane.Direction ().Coord (X2, Y2, Z2);
133 gp_Dir VPN (X1, Y1, Z1);
134 gp_Dir GPN (X2, Y2, Z2);
136 AVertex.Coord (X1, Y1, Z1);
137 Project (X1, Y1, Z1, XPp, YPp);
139 // Casw when the plane of the grid and the plane of the view
140 // are perpendicular to MYEPSILON2 close radians
141 if (Abs (VPN.Angle (GPN) - M_PI / 2.) < MYEPSILON2) {
142 NewPoint.SetCoord (X1, Y1, Z1);
146 Standard_Boolean IsRectangular =
147 MyGrid->IsKind (STANDARD_TYPE (Aspect_RectangularGrid));
151 Standard_Real x0, y0, z0, x1, y1, z1, x2, y2, z2;
153 P1.SetCoord (0.0, 0.0, 0.0);
154 CurPoint = V3d_View::TrsPoint (P1, MyTrsf);
155 CurPoint.Coord (x0, y0, z0);
157 // get grid axes in world space
158 P1.SetCoord (1.0, 0.0, 0.0);
159 CurPoint = V3d_View::TrsPoint (P1, MyTrsf);
160 CurPoint.Coord (x1, y1, z1);
161 gp_Vec aGridX (gp_Pnt (x0, y0, z0), gp_Pnt (x1, y1, z1));
164 P1.SetCoord (0.0, 1.0, 0.0);
165 CurPoint = V3d_View::TrsPoint (P1, MyTrsf);
166 CurPoint.Coord (x2, y2, z2);
167 gp_Vec aGridY (gp_Pnt (x0, y0, z0), gp_Pnt (x2, y2, z2));
171 MyPlane.Direction().Coord (x2, y2, z2);
172 gp_Vec aPlaneNormal (x2, y2, z2);
174 gp_Vec aPointOnPlane = gp_Vec (0.0, 0.0, 0.0);
176 AVertex.Coord (x1, y1, z1);
178 // project ray from camera onto grid plane
179 if (!myCamera->IsOrthographic())
181 gp_Vec aDirCamera2Point = gp_Vec (myCamera->Eye(), gp_Pnt (x1, y1, z1)).Normalized();
182 gp_Vec aVecCamera2Orig = gp_Vec (myCamera->Eye(), gp_Pnt (x0, y0, z0));
183 Standard_Real aDistPointPlane = aVecCamera2Orig.Dot (aPlaneNormal) / aDirCamera2Point.Dot (aPlaneNormal);
184 aPointOnPlane = gp_Vec (myCamera->Eye().XYZ()) + aDirCamera2Point * aDistPointPlane;
188 gp_Vec aDirCamera = myCamera->Direction();
189 gp_Vec aVecOrig2Point = gp_Vec (gp_Pnt (x0, y0, z0), gp_Pnt (x1, y1, z1));
190 Standard_Real aDistPointPlane = aVecOrig2Point.Dot (aPlaneNormal) / aDirCamera.Dot (aPlaneNormal);
191 aPointOnPlane = gp_Vec (x1, y1, z1) + aDirCamera * aDistPointPlane;
195 Standard_Real XS, YS;
196 Handle(Aspect_RectangularGrid) theGrid =
197 *(Handle(Aspect_RectangularGrid) *) &MyGrid;
198 XS = theGrid->XStep (), YS = theGrid->YStep ();
200 // project point on plane to grid local space
201 gp_Vec aToPoint (gp_Pnt (x0, y0, z0),
202 gp_Pnt (aPointOnPlane.X(), aPointOnPlane.Y(), aPointOnPlane.Z()));
203 Standard_Real anXSteps = Round (aGridX.Dot (aToPoint) / XS);
204 Standard_Real anYSteps = Round (aGridY.Dot (aToPoint) / YS);
206 // clamp point to grid
207 gp_Vec aResult = aGridX * anXSteps * XS + aGridY * anYSteps * YS + gp_Vec (x0, y0, z0);
208 NewPoint.SetCoord (aResult.X(), aResult.Y(), aResult.Z());
216 Handle(Aspect_CircularGrid) theGrid =
217 *(Handle(Aspect_CircularGrid) *) &MyGrid;
218 RS = theGrid->RadiusStep ();
219 DN = theGrid->DivisionNumber ();
220 Alpha = M_PI / Standard_Real (DN);
222 // project point on plane to grid local space
223 gp_Vec aToPoint (gp_Pnt (x0, y0, z0),
224 gp_Pnt (aPointOnPlane.X(), aPointOnPlane.Y(), aPointOnPlane.Z()));
226 Standard_Real anR = 0.0, aPhi = 0.0;
227 Standard_Real aLocalX = aGridX.Dot (aToPoint);
228 Standard_Real aLocalY = aGridY.Dot (aToPoint);
229 toPolarCoords (aLocalX, aLocalY, anR, aPhi);
231 // clamp point to grid
232 Standard_Real anRSteps = Round (anR / RS);
233 Standard_Real aPhiSteps = Round (aPhi / Alpha);
234 toCartesianCoords (anRSteps * RS, aPhiSteps * Alpha, aLocalX, aLocalY);
236 gp_Vec aResult = aGridX * aLocalX + aGridY * aLocalY + gp_Vec (x0, y0, z0);
237 NewPoint.SetCoord (aResult.X(), aResult.Y(), aResult.Z());
243 // Triedron methods : the Triedron is a non-zoomable object.
245 void V3d_View::ZBufferTriedronSetup(const Quantity_NameOfColor XColor,
246 const Quantity_NameOfColor YColor,
247 const Quantity_NameOfColor ZColor,
248 const Standard_Real SizeRatio,
249 const Standard_Real AxisDiametr,
250 const Standard_Integer NbFacettes)
252 MyView->ZBufferTriedronSetup(XColor, YColor, ZColor, SizeRatio, AxisDiametr, NbFacettes);
255 void V3d_View::TriedronDisplay (const Aspect_TypeOfTriedronPosition APosition,
256 const Quantity_NameOfColor AColor, const Standard_Real AScale, const V3d_TypeOfVisualization AMode )
258 MyView->TriedronDisplay (APosition, AColor, AScale, (AMode == V3d_WIREFRAME));
261 void V3d_View::TriedronErase ( )
263 MyView->TriedronErase ( );
266 void V3d_View::TriedronEcho (const Aspect_TypeOfTriedronEcho AType )
268 MyView->TriedronEcho (AType);
271 const Graphic3d_GraduatedTrihedron& V3d_View::GetGraduatedTrihedron() const
273 return MyView->GetGraduatedTrihedron();
276 void V3d_View::GraduatedTrihedronDisplay(const Graphic3d_GraduatedTrihedron& theTrihedronData)
278 MyView->GraduatedTrihedronDisplay(theTrihedronData);
281 void V3d_View::GraduatedTrihedronErase()
283 MyView->GraduatedTrihedronErase();