0024752: Visualization - inherit OpenGl_Group from Graphic3d_Group
[occt.git] / src / V3d / V3d.cxx
CommitLineData
b311480e 1// Created by: GG
2// Copyright (c) 1991-1999 Matra Datavision
973c2be1 3// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
7fd59977 16// Modified 23/02/98 : FMN ; Remplacement PI par Standard_PI
17// 02.15.100 : JR : Clutter
18
7fd59977 19
20//-Version
21
22//-Design
23
24//-Warning
25
26//-References
27
28//-Language C++ 2.1
29
30//-Declarations
31
32// for the class
33#include <V3d.ixx>
34#include <V3d_View.hxx>
35
36#include <Quantity_NameOfColor.hxx>
37#include <Aspect_Grid.hxx>
38#include <Aspect_Window.hxx>
b8ddfc2f 39#include <Graphic3d_ArrayOfPolylines.hxx>
7fd59977 40#include <Graphic3d_Group.hxx>
41#include <Graphic3d_Structure.hxx>
42#include <Graphic3d_AspectLine3d.hxx>
43#include <Graphic3d_AspectText3d.hxx>
44#include <Visual3d_HSetOfView.hxx>
45#include <Visual3d_SetIteratorOfSetOfView.hxx>
46#include <Visual3d_View.hxx>
47#include <Visual3d_ViewManager.hxx>
48
49Graphic3d_Vector V3d::GetProjAxis(const V3d_TypeOfOrientation Orientation) {
50Standard_Real Xpn=0,Ypn=0,Zpn=0 ;
51Graphic3d_Vector Vec ;
52
53 switch (Orientation) {
54 case V3d_Xpos :
55 Xpn = 1. ; Ypn = 0. ; Zpn = 0. ;
56 break ;
57 case V3d_Ypos :
58 Xpn = 0. ; Ypn = 1. ; Zpn = 0. ;
59 break ;
60 case V3d_Zpos :
61 Xpn = 0. ; Ypn = 0. ; Zpn = 1. ;
62 break ;
63 case V3d_Xneg :
64 Xpn = -1. ; Ypn = 0. ; Zpn = 0. ;
65 break ;
66 case V3d_Yneg :
67 Xpn = 0. ; Ypn = -1. ; Zpn = 0. ;
68 break ;
69 case V3d_Zneg :
70 Xpn = 0. ; Ypn = 0. ; Zpn = -1. ;
71 break ;
72 case V3d_XposYposZpos :
73 Xpn = 1. ; Ypn = 1. ; Zpn = 1. ;
74 break ;
75 case V3d_XposYposZneg :
76 Xpn = 1. ; Ypn = 1. ; Zpn = -1. ;
77 break ;
78 case V3d_XposYnegZpos :
79 Xpn = 1. ; Ypn = -1. ; Zpn = 1. ;
80 break ;
81 case V3d_XposYnegZneg :
82 Xpn = 1. ; Ypn = -1. ; Zpn = -1. ;
83 break ;
84 case V3d_XnegYposZpos :
85 Xpn = -1. ; Ypn = 1. ; Zpn = 1. ;
86 break ;
87 case V3d_XnegYposZneg :
88 Xpn = -1. ; Ypn = 1. ; Zpn = -1. ;
89 break ;
90 case V3d_XnegYnegZpos :
91 Xpn = -1. ; Ypn = -1. ; Zpn = 1. ;
92 break ;
93 case V3d_XnegYnegZneg :
94 Xpn = -1. ; Ypn = -1. ; Zpn = -1. ;
95 break ;
96 case V3d_XposYpos :
97 Xpn = 1. ; Ypn = 1. ; Zpn = 0. ;
98 break ;
99 case V3d_XposYneg :
100 Xpn = 1. ; Ypn = -1. ; Zpn = 0. ;
101 break ;
102 case V3d_XnegYpos :
103 Xpn = -1. ; Ypn = 1. ; Zpn = 0. ;
104 break ;
105 case V3d_XnegYneg :
106 Xpn = -1. ; Ypn = -1. ; Zpn = 0. ;
107 break ;
108 case V3d_XposZpos :
109 Xpn = 1. ; Ypn = 0. ; Zpn = 1. ;
110 break ;
111 case V3d_XposZneg :
112 Xpn = 1. ; Ypn = 0. ; Zpn = -1. ;
113 break ;
114 case V3d_XnegZpos :
115 Xpn = -1. ; Ypn = 0. ; Zpn = 1. ;
116 break ;
117 case V3d_XnegZneg :
118 Xpn = -1. ; Ypn = 0. ; Zpn = -1. ;
119 break ;
120 case V3d_YposZpos :
121 Xpn = 0. ; Ypn = 1. ; Zpn = 1. ;
122 break ;
123 case V3d_YposZneg :
124 Xpn = 0. ; Ypn = 1. ; Zpn = -1. ;
125 break ;
126 case V3d_YnegZpos :
127 Xpn = 0. ; Ypn = -1. ; Zpn = 1. ;
128 break ;
129 case V3d_YnegZneg :
130 Xpn = 0. ; Ypn = -1. ; Zpn = -1. ;
131 break ;
132 }
133 Vec.SetCoord(Xpn,Ypn,Zpn) ; Vec.Normalize() ;
134 return Vec ;
135}
136
b8ddfc2f 137void V3d::ArrowOfRadius(const Handle(Graphic3d_Group)& garrow,const Standard_Real X0,const Standard_Real Y0,const Standard_Real Z0,const Standard_Real Dx,const Standard_Real Dy,const Standard_Real Dz,const Standard_Real Alpha,const Standard_Real Lng)
138{
1d47d8d0 139 Standard_Real Xc, Yc, Zc, Xi, Yi, Zi, Xj, Yj, Zj;
140 Standard_Real Xn, Yn, Zn, X, Y, Z, X1 = 0., Y1 = 0., Z1 = 0., Norme;
b8ddfc2f 141 const Standard_Integer NbPoints = 10;
7fd59977 142
143// Centre du cercle base de la fleche :
b8ddfc2f 144 Xc = X0 - Dx * Lng;
145 Yc = Y0 - Dy * Lng;
146 Zc = Z0 - Dz * Lng;
7fd59977 147
148// Construction d'un repere i,j pour le cercle:
b8ddfc2f 149 Xn=0., Yn=0., Zn=0.;
150
151 if ( Abs(Dx) <= Abs(Dy) && Abs(Dx) <= Abs(Dz)) Xn=1.;
152 else if ( Abs(Dy) <= Abs(Dz) && Abs(Dy) <= Abs(Dx)) Yn=1.;
153 else Zn=1.;
154 Xi = Dy * Zn - Dz * Yn;
155 Yi = Dz * Xn - Dx * Zn;
156 Zi = Dx * Yn - Dy * Xn;
157
158 Norme = Sqrt ( Xi*Xi + Yi*Yi + Zi*Zi );
159 Xi= Xi / Norme; Yi = Yi / Norme; Zi = Zi/Norme;
160
161 Xj = Dy * Zi - Dz * Yi;
162 Yj = Dz * Xi - Dx * Zi;
163 Zj = Dx * Yi - Dy * Xi;
164
165 Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(3*NbPoints,NbPoints);
166
167 Standard_Integer i;
168 const Standard_Real Tg = Tan(Alpha);
169 for (i = 1; i <= NbPoints; i++)
170 {
171 const Standard_Real cosinus = Cos ( 2. * M_PI / NbPoints * (i-1) );
172 const Standard_Real sinus = Sin ( 2. * M_PI / NbPoints * (i-1) );
173
174 X = Xc + (cosinus * Xi + sinus * Xj) * Lng * Tg;
175 Y = Yc + (cosinus * Yi + sinus * Yj) * Lng * Tg;
176 Z = Zc + (cosinus * Zi + sinus * Zj) * Lng * Tg;
177
178 if(i==1) { X1=X, Y1=Y, Z1=Z; }
179 else aPrims->AddVertex(X,Y,Z);
180 aPrims->AddBound(3);
181 aPrims->AddVertex(X0,Y0,Z0);
182 aPrims->AddVertex(X,Y,Z);
183 }
184 aPrims->AddVertex(X1,Y1,Z1);
7fd59977 185
b8ddfc2f 186 garrow->AddPrimitiveArray(aPrims);
7fd59977 187}
188
189
b8ddfc2f 190void V3d::CircleInPlane(const Handle(Graphic3d_Group)& gcircle,const Standard_Real X0,const Standard_Real Y0,const Standard_Real Z0,const Standard_Real DX,const Standard_Real DY,const Standard_Real DZ,const Standard_Real Rayon)
191{
192 Standard_Real Norme = Sqrt ( DX*DX + DY*DY + DZ*DZ );
193 if ( Norme >= 0.0001 )
194 {
195 Standard_Real VX,VY,VZ,X,Y,Z,Xn,Yn,Zn,Xi,Yi,Zi,Xj,Yj,Zj;
7fd59977 196
b8ddfc2f 197 VX= DX/Norme; VY = DY/Norme; VZ = DZ/Norme;
7fd59977 198
81bba717 199//Construction of marker i,j for the circle:
b8ddfc2f 200 Xn=0., Yn=0., Zn=0.;
201 if ( Abs(VX) <= Abs(VY) && Abs(VX) <= Abs(VZ)) Xn=1.;
202 else if ( Abs(VY) <= Abs(VZ) && Abs(VY) <= Abs(VX)) Yn=1.;
203 else Zn=1.;
204 Xi = VY * Zn - VZ * Yn;
205 Yi = VZ * Xn - VX * Zn;
206 Zi = VX * Yn - VY * Xn;
207
208 Norme = Sqrt ( Xi*Xi + Yi*Yi + Zi*Zi );
209 Xi= Xi / Norme; Yi = Yi / Norme; Zi = Zi/Norme;
210
211 Xj = VY * Zi - VZ * Yi;
212 Yj = VZ * Xi - VX * Zi;
213 Zj = VX * Yi - VY * Xi;
214
215 const Standard_Integer NFACES = 30;
216 Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(NFACES+1);
217
218 Standard_Integer i = 0;
219 Standard_Real Alpha = 0.;
220 const Standard_Real Dalpha = 2. * M_PI / NFACES;
221 for (; i <= NFACES; i++, Alpha += Dalpha)
222 {
223 const Standard_Real cosinus = Cos(Alpha);
224 const Standard_Real sinus = Sin(Alpha);
225
226 X = X0 + (cosinus * Xi + sinus * Xj) * Rayon;
227 Y = Y0 + (cosinus * Yi + sinus * Yj) * Rayon;
228 Z = Z0 + (cosinus * Zi + sinus * Zj) * Rayon;
229
230 aPrims->AddVertex(X,Y,Z);
231 }
232 gcircle->AddPrimitiveArray(aPrims);
233 }
7fd59977 234}
235
236
237void V3d::SwitchViewsinWindow(const Handle(V3d_View)& aPreviousView,
238 const Handle(V3d_View)& aNextView) {
239 aPreviousView->Viewer()->SetViewOff(aPreviousView);
240 if(!aNextView->IfWindow())
241 aNextView->SetWindow(aPreviousView->Window());
242 aNextView->Viewer()->SetViewOn(aNextView);
243
244}
b8ddfc2f 245void V3d::DrawSphere(const Handle(V3d_Viewer)& aViewer,const Quantity_Length ray)
246{
247 const Standard_Boolean inf = ray < 0;
248 const Standard_Real aRadius = Standard_ShortReal(Abs(ray));
7fd59977 249 Handle(Graphic3d_Structure) Struct = new Graphic3d_Structure(aViewer->Viewer()) ;
b64d84be 250 Handle(Graphic3d_Group) Group = Struct->NewGroup();
7fd59977 251
252 Handle(Graphic3d_AspectLine3d) LineAttrib = new Graphic3d_AspectLine3d() ;
253 LineAttrib->SetColor(Quantity_Color(Quantity_NOC_YELLOW));
254 Struct->SetPrimitivesAspect(LineAttrib) ;
b8ddfc2f 255
256 const Standard_Integer NFACES = 30;
257 Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(NFACES*(NFACES+1),NFACES);
258
259 const Standard_Real Dbeta = 2. * M_PI / NFACES;
260 const Standard_Real Dalpha = 2. * M_PI / NFACES;
1d47d8d0 261 Standard_ShortReal X,Y,Z,X0 = 0.,Y0 = 0.,Z0 = 0.;
b8ddfc2f 262 Standard_Real R, Alpha, Beta = 0.;
7fd59977 263 Standard_Integer i,j ;
b8ddfc2f 264 for( j=0; j<NFACES/2 ; j++, Beta += Dbeta ) {
265 aPrims->AddBound(NFACES+1);
266 R = aRadius*sin(Beta);
267 Z = Standard_ShortReal(aRadius*cos(Beta));
268 for( i=0, Alpha = 0.; i<NFACES; i++, Alpha += Dalpha ) {
269 X = Standard_ShortReal(R*cos(Alpha));
270 Y = Standard_ShortReal(R*sin(Alpha));
271 aPrims->AddVertex(X,Y,Z);
272 if (i==0) { X0=X, Y0=Y, Z0=Z; }
7fd59977 273 }
b8ddfc2f 274 aPrims->AddVertex(X0,Y0,Z0);
7fd59977 275 }
b8ddfc2f 276 for( j=0; j<NFACES/2 ; j++, Beta += Dbeta ) {
277 aPrims->AddBound(NFACES+1);
278 R = aRadius*sin(Beta);
279 Y = Standard_ShortReal(aRadius*cos(Beta));
7fd59977 280 Beta += Dbeta ;
b8ddfc2f 281 for( i=0, Alpha = 0.; i<NFACES; i++, Alpha += Dalpha ) {
282 X = Standard_ShortReal(R*cos(Alpha));
283 Z = Standard_ShortReal(R*sin(Alpha));
284 aPrims->AddVertex(X,Y,Z);
285 if (i==0) { X0=X, Y0=Y, Z0=Z; }
7fd59977 286 }
b8ddfc2f 287 aPrims->AddVertex(X0,Y0,Z0);
7fd59977 288 }
b8ddfc2f 289 Group->AddPrimitiveArray(aPrims);
290 if(inf) Struct->SetInfiniteState(Standard_True);
7fd59977 291 Struct->Display();
292 aViewer->Update();
293}
b8ddfc2f 294
7fd59977 295void V3d::SetPlane(const Handle(V3d_Viewer)& aViewer,
296 const Standard_Real x1,
297 const Standard_Real y1,
298 const Standard_Real z1,
299 const Standard_Real x2,
300 const Standard_Real y2,
301 const Standard_Real z2) {
302
303 gp_Ax3 a(gp_Pnt(0.,0.,0),gp_Dir(x1,y1,z1),gp_Dir(x2,y2,z2));
304 aViewer->SetPrivilegedPlane(a);
305
306}
307void V3d::PickGrid(const Handle(V3d_Viewer)& aViewer,
308// const Quantity_Length ray) {
309 const Quantity_Length ) {
310Standard_Real x1, y1, z1;
311Standard_Real x2, y2, z2;
312 cout << "Direction ? " << flush;
313 cin >> x1; cin >> y1; cin >> z1;
314 cout << "XDirection ? " << flush;
315 cin >> x2; cin >> y2; cin >> z2;
316Standard_Integer u, v;
317 cout << "u, v ? " << flush;
318 cin >> u; cin >> v;
319 V3d::SetPlane (aViewer, x1, y1, z1, x2, y2, z2);
320
81bba717 321 // To restart the calculation on the new plane
7fd59977 322 if (aViewer->Grid ()->IsActive ()) {
323 Standard_Real xo, yo;
324 Quantity_PlaneAngle angle;
325 switch (aViewer->GridType ()) {
326 case Aspect_GT_Rectangular :
327 Standard_Real xstep, ystep;
328 aViewer->RectangularGridValues
329 (xo, yo, xstep, ystep, angle);
330 aViewer->SetRectangularGridValues
331 (xo, yo, xstep, ystep, angle);
332 break;
333 case Aspect_GT_Circular :
334 Standard_Real radiusstep;
335 Standard_Integer division;
336 aViewer->CircularGridValues
337 (xo, yo, radiusstep, division, angle);
338 aViewer->SetCircularGridValues
339 (xo, yo, radiusstep, division, angle);
340 break;
341 }
342 }
343
344 for (aViewer->InitActiveViews ();
345 aViewer->MoreActiveViews ();
346 aViewer->NextActiveViews()) {
347Standard_Real X, Y, Z;
348 aViewer->ActiveView ()->Convert (u, v, X, Y, Z);
349 }
350}