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.
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>
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>
40 #define MYEPSILON1 0.0001 // Comparison with 0.0
41 #define MYEPSILON2 M_PI / 180. // Delta between 2 angles
43 //=============================================================================
46 //=============================================================================
47 void V3d_View::SetGrid (const gp_Ax3& aPlane, const Handle(Aspect_Grid)& aGrid)
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);
61 Standard_Real CosAlpha = Cos (MyGrid->RotationAngle ());
62 Standard_Real SinAlpha = Sin (MyGrid->RotationAngle ());
64 TColStd_Array2OfReal Trsf1 (1, 4, 1, 4);
66 Trsf1 (4, 1) = Trsf1 (4, 2) = Trsf1 (4, 3) = 0.0;
71 // Transformation change of marker
82 TColStd_Array2OfReal Trsf2 (1, 4, 1, 4);
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 (),
89 // Rotation Alpha around axis -Z
90 Trsf2 (1, 1) = CosAlpha,
91 Trsf2 (2, 1) = -SinAlpha,
93 Trsf2 (1, 2) = SinAlpha,
94 Trsf2 (2, 2) = CosAlpha,
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
106 for (j=1; j<=4; j++) {
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;
117 //=============================================================================
118 //function : SetGridActivity
120 //=============================================================================
121 void V3d_View::SetGridActivity (const Standard_Boolean AFlag)
123 if (AFlag) MyGrid->Activate ();
124 else MyGrid->Deactivate ();
127 //=============================================================================
128 //function : toPolarCoords
130 //=============================================================================
131 void toPolarCoords (const Standard_Real theX, const Standard_Real theY,
132 Standard_Real& theR, Standard_Real& thePhi)
134 theR = Sqrt (theX * theX + theY * theY);
135 thePhi = ATan2 (theY, theX);
138 //=============================================================================
139 //function : toCartesianCoords
141 //=============================================================================
142 void toCartesianCoords (const Standard_Real theR, const Standard_Real thePhi,
143 Standard_Real& theX, Standard_Real& theY)
145 theX = theR * Cos (thePhi);
146 theY = theR * Sin (thePhi);
149 //=============================================================================
152 //=============================================================================
153 Graphic3d_Vertex V3d_View::Compute (const Graphic3d_Vertex& theVertex) const
155 const Handle(Graphic3d_Camera)& aCamera = Camera();
156 gp_Dir VPN = aCamera->Direction().Reversed(); // RefPlane
157 gp_Dir GPN = MyPlane.Direction();
159 Standard_Real XPp = 0.0, YPp = 0.0;
160 Project (theVertex.X(), theVertex.Y(), theVertex.Z(), XPp, YPp);
162 // Casw when the plane of the grid and the plane of the view
163 // are perpendicular to MYEPSILON2 close radians
164 if (Abs (VPN.Angle (GPN) - M_PI / 2.) < MYEPSILON2)
169 const gp_XYZ aPnt0 = V3d_View::TrsPoint (Graphic3d_Vertex (0.0, 0.0, 0.0), MyTrsf);
171 // get grid axes in world space
172 const gp_XYZ aPnt1 = V3d_View::TrsPoint (Graphic3d_Vertex (1.0, 0.0, 0.0), MyTrsf);
173 gp_Vec aGridX (aPnt0, aPnt1);
176 const gp_XYZ aPnt2 = V3d_View::TrsPoint (Graphic3d_Vertex (0.0, 1.0, 0.0), MyTrsf);
177 gp_Vec aGridY (aPnt0, aPnt2);
180 // project ray from camera onto grid plane
181 const gp_Vec aProjection = aCamera->IsOrthographic()
182 ? gp_Vec (aCamera->Direction())
183 : gp_Vec (aCamera->Eye(), gp_Pnt (theVertex.X(), theVertex.Y(), theVertex.Z())).Normalized();
184 const gp_Vec aPointOrigin = gp_Vec (gp_Pnt (theVertex.X(), theVertex.Y(), theVertex.Z()), aPnt0);
185 const Standard_Real aT = aPointOrigin.Dot (MyPlane.Direction()) / aProjection.Dot (MyPlane.Direction());
186 const gp_XYZ aPointOnPlane = gp_XYZ (theVertex.X(), theVertex.Y(), theVertex.Z()) + aProjection.XYZ() * aT;
188 if (Handle(Aspect_RectangularGrid) aRectGrid = Handle(Aspect_RectangularGrid)::DownCast (MyGrid))
190 // project point on plane to grid local space
191 const gp_Vec aToPoint (aPnt0, aPointOnPlane);
192 const Standard_Real anXSteps = Round (aGridX.Dot (aToPoint) / aRectGrid->XStep());
193 const Standard_Real anYSteps = Round (aGridY.Dot (aToPoint) / aRectGrid->YStep());
195 // clamp point to grid
196 const gp_Vec aResult = aGridX * anXSteps * aRectGrid->XStep()
197 + aGridY * anYSteps * aRectGrid->YStep()
199 return Graphic3d_Vertex (aResult.X(), aResult.Y(), aResult.Z());
203 Handle(Aspect_CircularGrid) aCircleGrid = Handle(Aspect_CircularGrid)::DownCast (MyGrid);
204 const Standard_Real anAlpha = M_PI / Standard_Real (aCircleGrid->DivisionNumber());
206 // project point on plane to grid local space
207 const gp_Vec aToPoint (aPnt0, aPointOnPlane);
208 Standard_Real aLocalX = aGridX.Dot (aToPoint);
209 Standard_Real aLocalY = aGridY.Dot (aToPoint);
210 Standard_Real anR = 0.0, aPhi = 0.0;
211 toPolarCoords (aLocalX, aLocalY, anR, aPhi);
213 // clamp point to grid
214 const Standard_Real anRSteps = Round (anR / aCircleGrid->RadiusStep());
215 const Standard_Real aPhiSteps = Round (aPhi / anAlpha);
216 toCartesianCoords (anRSteps * aCircleGrid->RadiusStep(), aPhiSteps * anAlpha, aLocalX, aLocalY);
218 const gp_Vec aResult = aGridX * aLocalX + aGridY * aLocalY + gp_Vec (aPnt0);
219 return Graphic3d_Vertex (aResult.X(), aResult.Y(), aResult.Z());
223 //=============================================================================
224 //function : ZBufferTriedronSetup
226 //=============================================================================
227 void V3d_View::ZBufferTriedronSetup(const Quantity_Color& theXColor,
228 const Quantity_Color& theYColor,
229 const Quantity_Color& theZColor,
230 const Standard_Real theSizeRatio,
231 const Standard_Real theAxisDiametr,
232 const Standard_Integer theNbFacettes)
234 myTrihedron->SetArrowsColor (theXColor, theYColor, theZColor);
235 myTrihedron->SetSizeRatio (theSizeRatio);
236 myTrihedron->SetNbFacets (theNbFacettes);
237 myTrihedron->SetArrowDiameter (theAxisDiametr);
240 //=============================================================================
241 //function : TriedronDisplay
243 //=============================================================================
244 void V3d_View::TriedronDisplay (const Aspect_TypeOfTriedronPosition thePosition,
245 const Quantity_Color& theColor,
246 const Standard_Real theScale,
247 const V3d_TypeOfVisualization theMode)
249 myTrihedron->SetLabelsColor (theColor);
250 myTrihedron->SetScale (theScale);
251 myTrihedron->SetPosition (thePosition);
252 myTrihedron->SetWireframe (theMode == V3d_WIREFRAME);
254 myTrihedron->Display (*this);
257 //=============================================================================
258 //function : TriedronErase
260 //=============================================================================
261 void V3d_View::TriedronErase()
263 myTrihedron->Erase();
266 //=============================================================================
267 //function : GetGraduatedTrihedron
269 //=============================================================================
270 const Graphic3d_GraduatedTrihedron& V3d_View::GetGraduatedTrihedron() const
272 return myView->GetGraduatedTrihedron();
275 //=============================================================================
276 //function : GraduatedTrihedronDisplay
278 //=============================================================================
279 void V3d_View::GraduatedTrihedronDisplay(const Graphic3d_GraduatedTrihedron& theTrihedronData)
281 myView->GraduatedTrihedronDisplay (theTrihedronData);
284 //=============================================================================
285 //function : GraduatedTrihedronErase
287 //=============================================================================
288 void V3d_View::GraduatedTrihedronErase()
290 myView->GraduatedTrihedronErase();