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