0030692: Data Exchange - introduce base framework RWMesh for importing mesh data...
[occt.git] / src / V3d / V3d_View_4.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 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
973c2be1 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.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
7fd59977 14
42cf5bc1 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>
7fd59977 22#include <gp_Dir2d.hxx>
42cf5bc1 23#include <Graphic3d_Group.hxx>
24#include <Graphic3d_Structure.hxx>
25#include <Graphic3d_TextureEnv.hxx>
42cf5bc1 26#include <Quantity_Color.hxx>
27#include <Standard_MultiplyDefined.hxx>
28#include <Standard_TypeMismatch.hxx>
29#include <TColStd_Array2OfReal.hxx>
30#include <V3d_BadValue.hxx>
7fd59977 31#include <V3d_CircularGrid.hxx>
42cf5bc1 32#include <V3d_Light.hxx>
33#include <V3d_RectangularGrid.hxx>
34#include <V3d_UnMapped.hxx>
35#include <V3d_View.hxx>
36#include <V3d_Viewer.hxx>
92efcf78 37#include <Aspect_CircularGrid.hxx>
7fd59977 38
81bba717 39#define MYEPSILON1 0.0001 // Comparison with 0.0
c6541a0c 40#define MYEPSILON2 M_PI / 180. // Delta between 2 angles
7fd59977 41
c357e426 42//=============================================================================
43//function : SetGrid
44//purpose :
45//=============================================================================
b8ddfc2f 46void V3d_View::SetGrid (const gp_Ax3& aPlane, const Handle(Aspect_Grid)& aGrid)
47{
b5ac8292 48 MyPlane = aPlane;
49 MyGrid = aGrid;
50
51 Standard_Real xl, yl, zl;
52 Standard_Real xdx, xdy, xdz;
53 Standard_Real ydx, ydy, ydz;
54 Standard_Real dx, dy, dz;
55 aPlane.Location ().Coord (xl, yl, zl);
56 aPlane.XDirection ().Coord (xdx, xdy, xdz);
57 aPlane.YDirection ().Coord (ydx, ydy, ydz);
58 aPlane.Direction ().Coord (dx, dy, dz);
59
60 Standard_Real CosAlpha = Cos (MyGrid->RotationAngle ());
61 Standard_Real SinAlpha = Sin (MyGrid->RotationAngle ());
62
63 TColStd_Array2OfReal Trsf1 (1, 4, 1, 4);
64 Trsf1 (4, 4) = 1.0;
65 Trsf1 (4, 1) = Trsf1 (4, 2) = Trsf1 (4, 3) = 0.0;
66 // Translation
67 Trsf1 (1, 4) = xl,
68 Trsf1 (2, 4) = yl,
69 Trsf1 (3, 4) = zl;
70 // Transformation change of marker
71 Trsf1 (1, 1) = xdx,
72 Trsf1 (2, 1) = xdy,
73 Trsf1 (3, 1) = xdz,
74 Trsf1 (1, 2) = ydx,
75 Trsf1 (2, 2) = ydy,
76 Trsf1 (3, 2) = ydz,
77 Trsf1 (1, 3) = dx,
78 Trsf1 (2, 3) = dy,
79 Trsf1 (3, 3) = dz;
80
81 TColStd_Array2OfReal Trsf2 (1, 4, 1, 4);
82 Trsf2 (4, 4) = 1.0;
83 Trsf2 (4, 1) = Trsf2 (4, 2) = Trsf2 (4, 3) = 0.0;
84 // Translation of the origin
85 Trsf2 (1, 4) = -MyGrid->XOrigin (),
86 Trsf2 (2, 4) = -MyGrid->YOrigin (),
87 Trsf2 (3, 4) = 0.0;
88 // Rotation Alpha around axis -Z
89 Trsf2 (1, 1) = CosAlpha,
90 Trsf2 (2, 1) = -SinAlpha,
91 Trsf2 (3, 1) = 0.0,
92 Trsf2 (1, 2) = SinAlpha,
93 Trsf2 (2, 2) = CosAlpha,
94 Trsf2 (3, 2) = 0.0,
95 Trsf2 (1, 3) = 0.0,
96 Trsf2 (2, 3) = 0.0,
97 Trsf2 (3, 3) = 1.0;
98
99 Standard_Real valuetrsf;
100 Standard_Real valueoldtrsf;
101 Standard_Real valuenewtrsf;
102 Standard_Integer i, j, k;
103 // Calculation of the product of matrices
104 for (i=1; i<=4; i++)
105 for (j=1; j<=4; j++) {
106 MyTrsf (i, j) = 0.0;
107 for (k=1; k<=4; k++) {
108 valueoldtrsf = Trsf1 (i, k);
109 valuetrsf = Trsf2 (k, j);
110 valuenewtrsf = MyTrsf (i, j) + valueoldtrsf * valuetrsf;
111 MyTrsf (i, j) = valuenewtrsf;
112 }
113 }
7fd59977 114}
115
c357e426 116//=============================================================================
117//function : SetGridActivity
118//purpose :
119//=============================================================================
b8ddfc2f 120void V3d_View::SetGridActivity (const Standard_Boolean AFlag)
121{
b5ac8292 122 if (AFlag) MyGrid->Activate ();
123 else MyGrid->Deactivate ();
7fd59977 124}
125
c357e426 126//=============================================================================
127//function : toPolarCoords
128//purpose :
129//=============================================================================
b5ac8292 130void toPolarCoords (const Standard_Real theX, const Standard_Real theY,
131 Standard_Real& theR, Standard_Real& thePhi)
132{
133 theR = Sqrt (theX * theX + theY * theY);
134 thePhi = ATan2 (theY, theX);
135}
136
c357e426 137//=============================================================================
138//function : toCartesianCoords
139//purpose :
140//=============================================================================
b5ac8292 141void toCartesianCoords (const Standard_Real theR, const Standard_Real thePhi,
142 Standard_Real& theX, Standard_Real& theY)
143{
144 theX = theR * Cos (thePhi);
145 theY = theR * Sin (thePhi);
146}
147
c357e426 148//=============================================================================
149//function : Compute
150//purpose :
151//=============================================================================
475c2302 152Graphic3d_Vertex V3d_View::Compute (const Graphic3d_Vertex& theVertex) const
b8ddfc2f 153{
475c2302 154 const Handle(Graphic3d_Camera)& aCamera = Camera();
155 gp_Dir VPN = aCamera->Direction().Reversed(); // RefPlane
156 gp_Dir GPN = MyPlane.Direction();
7fd59977 157
475c2302 158 Standard_Real XPp = 0.0, YPp = 0.0;
159 Project (theVertex.X(), theVertex.Y(), theVertex.Z(), XPp, YPp);
7fd59977 160
b5ac8292 161 // Casw when the plane of the grid and the plane of the view
162 // are perpendicular to MYEPSILON2 close radians
475c2302 163 if (Abs (VPN.Angle (GPN) - M_PI / 2.) < MYEPSILON2)
164 {
165 return theVertex;
b5ac8292 166 }
167
475c2302 168 const gp_XYZ aPnt0 = V3d_View::TrsPoint (Graphic3d_Vertex (0.0, 0.0, 0.0), MyTrsf);
b5ac8292 169
170 // get grid axes in world space
475c2302 171 const gp_XYZ aPnt1 = V3d_View::TrsPoint (Graphic3d_Vertex (1.0, 0.0, 0.0), MyTrsf);
172 gp_Vec aGridX (aPnt0, aPnt1);
b5ac8292 173 aGridX.Normalize();
174
475c2302 175 const gp_XYZ aPnt2 = V3d_View::TrsPoint (Graphic3d_Vertex (0.0, 1.0, 0.0), MyTrsf);
176 gp_Vec aGridY (aPnt0, aPnt2);
b5ac8292 177 aGridY.Normalize();
178
b5ac8292 179 // project ray from camera onto grid plane
475c2302 180 const gp_Vec aProjection = aCamera->IsOrthographic()
181 ? gp_Vec (aCamera->Direction())
182 : gp_Vec (aCamera->Eye(), gp_Pnt (theVertex.X(), theVertex.Y(), theVertex.Z())).Normalized();
183 const gp_Vec aPointOrigin = gp_Vec (gp_Pnt (theVertex.X(), theVertex.Y(), theVertex.Z()), aPnt0);
184 const Standard_Real aT = aPointOrigin.Dot (MyPlane.Direction()) / aProjection.Dot (MyPlane.Direction());
185 const gp_XYZ aPointOnPlane = gp_XYZ (theVertex.X(), theVertex.Y(), theVertex.Z()) + aProjection.XYZ() * aT;
186
187 if (Handle(Aspect_RectangularGrid) aRectGrid = Handle(Aspect_RectangularGrid)::DownCast (MyGrid))
188 {
b5ac8292 189 // project point on plane to grid local space
475c2302 190 const gp_Vec aToPoint (aPnt0, aPointOnPlane);
191 const Standard_Real anXSteps = Round (aGridX.Dot (aToPoint) / aRectGrid->XStep());
192 const Standard_Real anYSteps = Round (aGridY.Dot (aToPoint) / aRectGrid->YStep());
b5ac8292 193
194 // clamp point to grid
475c2302 195 const gp_Vec aResult = aGridX * anXSteps * aRectGrid->XStep()
196 + aGridY * anYSteps * aRectGrid->YStep()
197 + gp_Vec (aPnt0);
198 return Graphic3d_Vertex (aResult.X(), aResult.Y(), aResult.Z());
b5ac8292 199 }
200 else // IsCircular
201 {
475c2302 202 Handle(Aspect_CircularGrid) aCircleGrid = Handle(Aspect_CircularGrid)::DownCast (MyGrid);
203 const Standard_Real anAlpha = M_PI / Standard_Real (aCircleGrid->DivisionNumber());
b5ac8292 204
205 // project point on plane to grid local space
475c2302 206 const gp_Vec aToPoint (aPnt0, aPointOnPlane);
b5ac8292 207 Standard_Real aLocalX = aGridX.Dot (aToPoint);
208 Standard_Real aLocalY = aGridY.Dot (aToPoint);
475c2302 209 Standard_Real anR = 0.0, aPhi = 0.0;
b5ac8292 210 toPolarCoords (aLocalX, aLocalY, anR, aPhi);
211
212 // clamp point to grid
475c2302 213 const Standard_Real anRSteps = Round (anR / aCircleGrid->RadiusStep());
214 const Standard_Real aPhiSteps = Round (aPhi / anAlpha);
215 toCartesianCoords (anRSteps * aCircleGrid->RadiusStep(), aPhiSteps * anAlpha, aLocalX, aLocalY);
b5ac8292 216
475c2302 217 const gp_Vec aResult = aGridX * aLocalX + aGridY * aLocalY + gp_Vec (aPnt0);
218 return Graphic3d_Vertex (aResult.X(), aResult.Y(), aResult.Z());
b5ac8292 219 }
7fd59977 220}
221
c357e426 222//=============================================================================
223//function : ZBufferTriedronSetup
224//purpose :
225//=============================================================================
87432b82 226void V3d_View::ZBufferTriedronSetup(const Quantity_Color& theXColor,
227 const Quantity_Color& theYColor,
228 const Quantity_Color& theZColor,
229 const Standard_Real theSizeRatio,
230 const Standard_Real theAxisDiametr,
231 const Standard_Integer theNbFacettes)
7fd59977 232{
caf231b0 233 myTrihedron->SetArrowsColor (theXColor, theYColor, theZColor);
234 myTrihedron->SetSizeRatio (theSizeRatio);
235 myTrihedron->SetNbFacets (theNbFacettes);
236 myTrihedron->SetArrowDiameter (theAxisDiametr);
7fd59977 237}
238
c357e426 239//=============================================================================
240//function : TriedronDisplay
241//purpose :
242//=============================================================================
243void V3d_View::TriedronDisplay (const Aspect_TypeOfTriedronPosition thePosition,
87432b82 244 const Quantity_Color& theColor,
c357e426 245 const Standard_Real theScale,
246 const V3d_TypeOfVisualization theMode)
b8ddfc2f 247{
caf231b0 248 myTrihedron->SetLabelsColor (theColor);
249 myTrihedron->SetScale (theScale);
250 myTrihedron->SetPosition (thePosition);
251 myTrihedron->SetWireframe (theMode == V3d_WIREFRAME);
252
253 myTrihedron->Display (*this);
7fd59977 254}
255
c357e426 256//=============================================================================
257//function : TriedronErase
258//purpose :
259//=============================================================================
260void V3d_View::TriedronErase()
b8ddfc2f 261{
caf231b0 262 myTrihedron->Erase();
7fd59977 263}
264
c357e426 265//=============================================================================
266//function : GetGraduatedTrihedron
267//purpose :
268//=============================================================================
536d98e2 269const Graphic3d_GraduatedTrihedron& V3d_View::GetGraduatedTrihedron() const
7fd59977 270{
c357e426 271 return myView->GetGraduatedTrihedron();
7fd59977 272}
273
c357e426 274//=============================================================================
275//function : GraduatedTrihedronDisplay
276//purpose :
277//=============================================================================
a79f67f8 278void V3d_View::GraduatedTrihedronDisplay(const Graphic3d_GraduatedTrihedron& theTrihedronData)
7fd59977 279{
c357e426 280 myView->GraduatedTrihedronDisplay (theTrihedronData);
7fd59977 281}
282
c357e426 283//=============================================================================
284//function : GraduatedTrihedronErase
285//purpose :
286//=============================================================================
7fd59977 287void V3d_View::GraduatedTrihedronErase()
288{
c357e426 289 myView->GraduatedTrihedronErase();
7fd59977 290}