0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / V3d / V3d.cxx
1 // Created by: GG
2 // Copyright (c) 1991-1999 Matra Datavision
3 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <V3d.hxx>
17
18 #include <Aspect_Grid.hxx>
19 #include <Aspect_Window.hxx>
20 #include <Graphic3d_ArrayOfPolylines.hxx>
21 #include <Graphic3d_AspectLine3d.hxx>
22 #include <Graphic3d_AspectText3d.hxx>
23 #include <Graphic3d_Group.hxx>
24 #include <Graphic3d_Structure.hxx>
25 #include <Quantity_NameOfColor.hxx>
26 #include <V3d_View.hxx>
27 #include <V3d_Viewer.hxx>
28
29 namespace
30 {
31   static Standard_CString V3d_Table_PrintTypeOfOrientation[26] =
32   {
33     "XPOS", "YPOS", "ZPOS", "XNEG", "YNEG", "ZNEG", "XPOSYPOS", "XPOSZPOS", "XPOSZPOS", "XNEGYNEG",
34     "XNEGYPOS", "XNEGZNEG", "XNEGZPOS", "YNEGZNEG", "YNEGZPOS", "XPOSYNEG", "XPOSZNEG", "YPOSZNEG",
35     "XPOSYPOSZPOS", "XPOSYNEGZPOS", "XPOSYPOSZNEG", "XNEGYPOSZPOS", "XPOSYNEGZNEG", "XNEGYPOSZNEG",
36     "XNEGYNEGZPOS", "XNEGYNEGZNEG"
37   };
38 }
39
40 void 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)
41 {
42   Standard_Real Xc, Yc, Zc, Xi, Yi, Zi, Xj, Yj, Zj;
43   Standard_Real Xn, Yn, Zn, X, Y, Z, X1 = 0., Y1 = 0., Z1 = 0., Norme;
44   const Standard_Integer NbPoints = 10;
45
46 //      Centre du cercle base de la fleche :
47   Xc = X0 - Dx * Lng;
48   Yc = Y0 - Dy * Lng;
49   Zc = Z0 - Dz * Lng;
50
51 //      Construction d'un repere i,j pour le cercle:
52   Xn=0., Yn=0., Zn=0.;
53
54   if ( Abs(Dx) <= Abs(Dy) && Abs(Dx) <= Abs(Dz)) Xn=1.;
55   else if ( Abs(Dy) <= Abs(Dz) && Abs(Dy) <= Abs(Dx)) Yn=1.;
56   else Zn=1.;
57   Xi = Dy * Zn - Dz * Yn;
58   Yi = Dz * Xn - Dx * Zn;
59   Zi = Dx * Yn - Dy * Xn;
60
61   Norme = Sqrt ( Xi*Xi + Yi*Yi + Zi*Zi );
62   Xi= Xi / Norme; Yi = Yi / Norme; Zi = Zi/Norme;
63
64   Xj = Dy * Zi - Dz * Yi;
65   Yj = Dz * Xi - Dx * Zi;
66   Zj = Dx * Yi - Dy * Xi;
67
68   Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(3*NbPoints,NbPoints);
69
70   Standard_Integer i;
71   const Standard_Real Tg = Tan(Alpha);
72   for (i = 1; i <= NbPoints; i++)
73   {
74     const Standard_Real cosinus = Cos ( 2. * M_PI / NbPoints * (i-1) );
75     const Standard_Real sinus = Sin ( 2. * M_PI / NbPoints * (i-1) );
76
77     X = Xc + (cosinus * Xi + sinus * Xj) * Lng * Tg;
78     Y = Yc + (cosinus * Yi + sinus * Yj) * Lng * Tg;
79     Z = Zc + (cosinus * Zi + sinus * Zj) * Lng * Tg;
80
81     if(i==1) { X1=X, Y1=Y, Z1=Z; }
82     else aPrims->AddVertex(X,Y,Z);
83     aPrims->AddBound(3);
84     aPrims->AddVertex(X0,Y0,Z0);
85     aPrims->AddVertex(X,Y,Z);
86   }
87   aPrims->AddVertex(X1,Y1,Z1);
88
89   garrow->AddPrimitiveArray(aPrims);
90 }
91
92
93 void 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)
94 {
95   Standard_Real Norme = Sqrt ( DX*DX + DY*DY + DZ*DZ );
96   if ( Norme >= 0.0001 )
97   {
98     Standard_Real VX,VY,VZ,X,Y,Z,Xn,Yn,Zn,Xi,Yi,Zi,Xj,Yj,Zj;
99
100     VX= DX/Norme; VY = DY/Norme; VZ = DZ/Norme;
101
102 //Construction of marker i,j for the circle:
103     Xn=0., Yn=0., Zn=0.;   
104     if ( Abs(VX) <= Abs(VY) && Abs(VX) <= Abs(VZ)) Xn=1.;
105     else if ( Abs(VY) <= Abs(VZ) && Abs(VY) <= Abs(VX)) Yn=1.;
106     else Zn=1.;
107     Xi = VY * Zn - VZ * Yn;
108     Yi = VZ * Xn - VX * Zn;
109     Zi = VX * Yn - VY * Xn;
110
111     Norme = Sqrt ( Xi*Xi + Yi*Yi + Zi*Zi );
112     Xi= Xi / Norme; Yi = Yi / Norme; Zi = Zi/Norme;
113
114     Xj = VY * Zi - VZ * Yi;
115     Yj = VZ * Xi - VX * Zi;
116     Zj = VX * Yi - VY * Xi;      
117
118     const Standard_Integer NFACES = 30;
119     Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(NFACES+1);
120
121     Standard_Integer i = 0;
122     Standard_Real Alpha = 0.;
123     const Standard_Real Dalpha = 2. * M_PI / NFACES;
124     for (; i <= NFACES; i++, Alpha += Dalpha)
125     {
126       const Standard_Real cosinus = Cos(Alpha);
127       const Standard_Real sinus = Sin(Alpha);
128
129       X = X0 + (cosinus * Xi + sinus * Xj) * Rayon;
130       Y = Y0 + (cosinus * Yi + sinus * Yj) * Rayon;
131       Z = Z0 + (cosinus * Zi + sinus * Zj) * Rayon;
132
133       aPrims->AddVertex(X,Y,Z);
134     }
135     gcircle->AddPrimitiveArray(aPrims);
136   }
137 }
138
139
140 void V3d::SwitchViewsinWindow(const Handle(V3d_View)& aPreviousView,
141                               const Handle(V3d_View)& aNextView) {
142   aPreviousView->Viewer()->SetViewOff(aPreviousView);
143   if(!aNextView->IfWindow())
144     aNextView->SetWindow(aPreviousView->Window());
145   aNextView->Viewer()->SetViewOn(aNextView);
146     
147 }
148
149 //=======================================================================
150 //function : TypeOfOrientationToString
151 //purpose  :
152 //=======================================================================
153 Standard_CString V3d::TypeOfOrientationToString (V3d_TypeOfOrientation theType)
154 {
155   return V3d_Table_PrintTypeOfOrientation[theType];
156 }
157
158 //=======================================================================
159 //function : TypeOfOrientationFromString
160 //purpose  :
161 //=======================================================================
162 Standard_Boolean V3d::TypeOfOrientationFromString (Standard_CString theTypeString,
163                                                    V3d_TypeOfOrientation& theType)
164 {
165   TCollection_AsciiString aName (theTypeString);
166   aName.UpperCase();
167   for (Standard_Integer aTypeIter = 0; aTypeIter <= V3d_XnegYnegZneg; ++aTypeIter)
168   {
169     Standard_CString aTypeName = V3d_Table_PrintTypeOfOrientation[aTypeIter];
170     if (aName == aTypeName)
171     {
172       theType = V3d_TypeOfOrientation (aTypeIter);
173       return Standard_True;
174     }
175   }
176   return Standard_False;
177 }