0023634: Eliminate Polyline and Polygon usage in drawers
[occt.git] / src / V3d / V3d_DirectionalLight.cxx
CommitLineData
b311480e 1// Copyright (c) 1999-2012 OPEN CASCADE SAS
2//
3// The content of this file is subject to the Open CASCADE Technology Public
4// License Version 6.5 (the "License"). You may not use the content of this file
5// except in compliance with the License. Please obtain a copy of the License
6// at http://www.opencascade.org and read it completely before using this file.
7//
8// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10//
11// The Original Code and all software distributed under the License is
12// distributed on an "AS IS" basis, without warranty of any kind, and the
13// Initial Developer hereby disclaims all such warranties, including without
14// limitation, any warranties of merchantability, fitness for a particular
15// purpose or non-infringement. Please see the License for the specific terms
16// and conditions governing the rights and limitations under the License.
17
7fd59977 18/***********************************************************************
19
20 FONCTION :
21 ----------
22 Classe V3d_DirectionalLight :
23
24 HISTORIQUE DES MODIFICATIONS :
25 --------------------------------
26 00-09-92 : GG ; Creation.
27 18-06-96 : FMN ; Ajout MyGraphicStructure1 pour sauvegarder snopick
28 24-12-97 : FMN ; Remplacement de math par MathGra
29 31-12-97 : CAL ; Suppression de MathGra
30 21-01-98 : CAL ; Window de Xw et WNT remplacee par Aspect_Window
31 23-02-98 : FMN ; Remplacement PI par Standard_PI
32 30-03-98 : ZOV ; PRO6774 (reconstruction of the class hierarchy and suppressing useless methods)
33
34************************************************************************/
35
36/*----------------------------------------------------------------------*/
37/*
38 * Includes
39 */
40
41#include <V3d.hxx>
42#include <V3d_DirectionalLight.ixx>
43#include <Graphic3d_Vector.hxx>
44#include <Graphic3d_Vertex.hxx>
45#include <Graphic3d_Structure.hxx>
46#include <Graphic3d_Group.hxx>
b8ddfc2f 47#include <Graphic3d_ArrayOfSegments.hxx>
7fd59977 48#include <Graphic3d_AspectMarker3d.hxx>
49#include <Graphic3d_AspectLine3d.hxx>
50#include <Graphic3d_AspectText3d.hxx>
51#include <Visual3d_Light.hxx>
52#include <Visual3d_ViewManager.hxx>
53#include <Visual3d_ContextPick.hxx>
54#include <Visual3d_PickDescriptor.hxx>
55#include <Visual3d_HSequenceOfPickPath.hxx>
56#include <Visual3d_PickPath.hxx>
57#include <Viewer_BadValue.hxx>
58#include <gp_Dir.hxx>
59#include <gp_Ax1.hxx>
60#include <gp_Vec.hxx>
61#include <gp_Pnt.hxx>
62#include <gp_Trsf.hxx>
63#include <TColStd_Array2OfReal.hxx>
64#include <Aspect_Window.hxx>
65
66//-Constructors
67
68V3d_DirectionalLight::V3d_DirectionalLight(const Handle(V3d_Viewer)& VM, const
69V3d_TypeOfOrientation Direction,const Quantity_NameOfColor Name,const Standard_Boolean Headlight):V3d_PositionLight(VM) {
70 Quantity_Color C(Name) ;
71 Graphic3d_Vertex T(0.,0.,0.) ;
72 Graphic3d_Vertex P ;
73 Graphic3d_Vector V = V3d::GetProjAxis(Direction) ;
74
75 MyType = V3d_DIRECTIONAL ;
76 MyLight = new Visual3d_Light(C,V, Headlight) ;
81bba717 77 // The initial target is chosen at random
7fd59977 78 MyTarget = T;
81bba717 79// Position is found
7fd59977 80 P.SetCoord(-V.X(),-V.Y(),-V.Z());
81 MyDisplayPosition = P;
82
83}
84
85V3d_DirectionalLight::V3d_DirectionalLight(const Handle(V3d_Viewer)& VM,const Standard_Real Xt,const Standard_Real Yt,const Standard_Real Zt,const Standard_Real Xp,const Standard_Real Yp,const Standard_Real Zp,const Quantity_NameOfColor Name,const Standard_Boolean Headlight):V3d_PositionLight(VM) {
86 Quantity_Color C(Name) ;
87 Graphic3d_Vertex T(Xt,Yt,Zt) ;
88 Graphic3d_Vertex P(Xp,Yp,Zp) ;
89 Graphic3d_Vector V(P,T);
90
91 MyType = V3d_DIRECTIONAL ;
92 V.Normalize();
93 MyLight = new Visual3d_Light(C,V, Headlight) ;
94 MyTarget = T;
95 MyDisplayPosition = P;
96}
97
98//-Methods, in order
99
100void V3d_DirectionalLight::SetDirection(const V3d_TypeOfOrientation Direction) {
101
102 Graphic3d_Vector V = V3d::GetProjAxis(Direction) ;
103 MyLight->SetDirection(V) ;
104}
105
106void V3d_DirectionalLight::SetDirection(const Standard_Real Vx, const Standard_Real Vy, const Standard_Real Vz) {
107
108 Viewer_BadValue_Raise_if( sqrt( Vx*Vx + Vy*Vy + Vz*Vz ) <= 0.,"V3d_DirectionalLight::SetDirection, null vector" );
109
110 Graphic3d_Vector V(Vx,Vy,Vz) ;
111 V.Normalize() ;
112 MyLight->SetDirection(V) ;
113}
114
115void V3d_DirectionalLight::SetDisplayPosition(const Standard_Real X, const Standard_Real Y, const Standard_Real Z) {
116
117 Standard_Real Xt,Yt,Zt;
118
119 MyDisplayPosition.SetCoord(X,Y,Z) ;
120 MyTarget.Coord(Xt,Yt,Zt);
121 SetDirection(Xt-X,Yt-Y,Zt-Z);
122}
123
124void V3d_DirectionalLight::SetPosition(const Standard_Real Xp, const Standard_Real Yp, const Standard_Real Zp) {
125
126 SetDisplayPosition (Xp,Yp,Zp);
127}
128
129void V3d_DirectionalLight::Position(Standard_Real& Xp, Standard_Real& Yp, Standard_Real& Zp)const {
130
131 DisplayPosition (Xp,Yp,Zp) ;
132}
133
134void V3d_DirectionalLight::DisplayPosition(Standard_Real& Xp, Standard_Real& Yp, Standard_Real& Zp)const {
135
136 MyDisplayPosition.Coord(Xp,Yp,Zp) ;
137}
138
b8ddfc2f 139void V3d_DirectionalLight::Symbol (const Handle(Graphic3d_Group)& gsymbol, const Handle(V3d_View)& aView) const
140{
7fd59977 141 Standard_Real Xi,Yi,Zi,Xf,Yf,Zf,Rayon,PXT,PYT,X,Y,Z,XT,YT,ZT;
142 Standard_Real A,B,C,Dist,Beta,CosBeta,SinBeta,Coef,X1,Y1,Z1;
143 Standard_Real DX,DY,DZ,VX,VY,VZ;
144 Standard_Integer IXP,IYP,j;
145 TColStd_Array2OfReal MatRot(0,2,0,2);
7fd59977 146
147 aView->Proj(VX,VY,VZ);
148 this->DisplayPosition(Xi,Yi,Zi);
149 Rayon = this->Radius();
150 aView->Project(Xi,Yi,Zi,PXT,PYT);
151 aView->Convert(PXT,PYT,IXP,IYP);
81bba717 152// Coordinated 3d in the plane of projection of the source.
7fd59977 153 aView->Convert(IXP,IYP,XT,YT,ZT);
154 aView->Convert(PXT,PYT+Rayon,IXP,IYP);
155 aView->Convert(IXP,IYP,X,Y,Z);
156 X = X+Xi-XT; Y = Y+Yi-YT; Z = Z+Zi-ZT;
157 Dist = Sqrt( Square(X-Xi) + Square(Y-Yi) + Square(Z-Zi) );
81bba717 158// Axis of rotation.
7fd59977 159 A = (X-Xi)/Dist;
160 B = (Y-Yi)/Dist;
161 C = (Z-Zi)/Dist;
162
81bba717 163// A sphere is drawn
7fd59977 164 V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
165 for( j=1 ; j<=3 ; j++ ) {
c6541a0c 166 Beta = j * M_PI / 4.;
7fd59977 167 CosBeta = Cos(Beta);
168 SinBeta = Sin(Beta);
169 Coef = 1. - CosBeta;
170 MatRot(0,0) = A * A + (1. - A * A) * CosBeta;
171 MatRot(0,1) = -C * SinBeta + Coef * A * B;
172 MatRot(0,2) = B * SinBeta + Coef * A * C;
173 MatRot(1,0) = C * SinBeta + Coef * A * B;
174 MatRot(1,1) = B * B + (1. - B * B) * CosBeta;
175 MatRot(1,2) = -A * SinBeta + Coef * B * C;
176 MatRot(2,0) = -B * SinBeta + Coef * A * C;
177 MatRot(2,1) = A * SinBeta + Coef * B * C;
178 MatRot(2,2) = C * C + (1. - C * C) * CosBeta;
179 Xf = Xi * MatRot(0,0) + Yi * MatRot(0,1) + Zi * MatRot(0,2);
180 Yf = Xi * MatRot(1,0) + Yi * MatRot(1,1) + Zi * MatRot(1,2);
181 Zf = Xi * MatRot(2,0) + Yi * MatRot(2,1) + Zi * MatRot(2,2);
81bba717 182// Rotation of the normal
7fd59977 183 X1 = VX * MatRot(0,0) + VY * MatRot(0,1) + VZ * MatRot(0,2);
184 Y1 = VX * MatRot(1,0) + VY * MatRot(1,1) + VZ * MatRot(1,2);
185 Z1 = VX * MatRot(2,0) + VY * MatRot(2,1) + VZ * MatRot(2,2);
186 VX = X1 + Xi - Xf ; VY = Y1 + Yi - Yf ; VZ = Z1 + Zi - Zf;
187 V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
188 }
189
81bba717 190// The arrow is drawn
7fd59977 191 Rayon = this->Radius();
192 this->Direction(DX,DY,DZ);
193 X = Xi + DX*Rayon/10.; Y = Yi + DY*Rayon/10.; Z = Zi + DZ*Rayon/10.;
b8ddfc2f 194
195 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
196 aPrims->AddVertex(Standard_ShortReal(Xi),Standard_ShortReal(Yi),Standard_ShortReal(Zi));
197 aPrims->AddVertex(Standard_ShortReal(X),Standard_ShortReal(Y),Standard_ShortReal(Z));
198 gsymbol->AddPrimitiveArray(aPrims);
199
c6541a0c 200 V3d::ArrowOfRadius(gsymbol, X, Y, Z, DX, DY, DZ, M_PI / 15., Rayon / 20.);
7fd59977 201}
202
203void V3d_DirectionalLight::Display( const Handle(V3d_View)& aView,
204 const V3d_TypeOfRepresentation TPres) {
205
206 Standard_Real X,Y,Z,Rayon;
207 Standard_Real X0,Y0,Z0,VX,VY,VZ;
208 Standard_Real X1,Y1,Z1;
209 Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
210 Standard_Real R1,G1,B1;
211 V3d_TypeOfRepresentation Pres;
212 V3d_TypeOfUpdate UpdSov;
213
81bba717 214// Creation of a structure of markable elements (position of the
215// light, and the domain of lighting represented by a circle)
216// Creation of a structure of non-markable elements (target, meridian and
217// parallel).
7fd59977 218
219 Pres = TPres;
220 Handle(V3d_Viewer) TheViewer = aView->Viewer();
221 UpdSov = TheViewer->UpdateMode();
222 TheViewer->SetUpdateMode(V3d_WAIT);
223 if (!MyGraphicStructure.IsNull()) {
224 MyGraphicStructure->Disconnect(MyGraphicStructure1);
225 MyGraphicStructure->Clear();
226 MyGraphicStructure1->Clear();
227 if (Pres == V3d_SAMELAST) Pres = MyTypeOfRepresentation;
228 }
229 else {
230 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
231 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->Viewer());
232 MyGraphicStructure = slight;
233 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->Viewer());
234 MyGraphicStructure1 = snopick;
235 }
236
237 Handle(Graphic3d_Group) glight = new Graphic3d_Group(MyGraphicStructure);
238 Handle(Graphic3d_Group) gsphere;
239 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) gsphere = new Graphic3d_Group(MyGraphicStructure);
240
241 Handle(Graphic3d_Group) gnopick = new Graphic3d_Group(MyGraphicStructure1);
242 MyGraphicStructure1->SetPick(Standard_False);
243
244 X0 = MyTarget.X();
245 Y0 = MyTarget.Y();
246 Z0 = MyTarget.Z();
247
81bba717 248//Display of the position of the light.
7fd59977 249
7fd59977 250 this->Color(Quantity_TOC_RGB,R1,G1,B1);
251 Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
252 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
253 Asp1->SetColor(Col1);
254 glight->SetPrimitivesAspect(Asp1);
255 this->Symbol(glight,aView);
256
81bba717 257 // Display of the markable sphere (limit at the circle).
7fd59977 258
259 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
260
261 Rayon = this->Radius();
262 aView->Proj(VX,VY,VZ);
7fd59977 263 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
264
81bba717 265//Display of the meridian
7fd59977 266
267 Quantity_Color Col2(Quantity_NOC_GREEN);
268 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d
269 (Col2,Aspect_TOL_SOLID,1.);
270 gnopick->SetPrimitivesAspect(Asp2);
271
81bba717 272// Definition of the axis of circle
7fd59977 273 aView->Up(DXRef,DYRef,DZRef);
274 this->DisplayPosition(X,Y,Z);
275 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
276 VX = DYRef*DZini - DZRef*DYini;
277 VY = DZRef*DXini - DXRef*DZini;
278 VZ = DXRef*DYini - DYRef*DXini;
279
280 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
281
81bba717 282// Display of the parallel
7fd59977 283
81bba717 284// Definition of the axis of circle
7fd59977 285 aView->Proj(VX,VY,VZ);
286 aView->Up(X1,Y1,Z1);
287 DXRef = VY * Z1 - VZ * Y1;
288 DYRef = VZ * X1 - VX * Z1;
289 DZRef = VX * Y1 - VY * X1;
290 this->DisplayPosition(X,Y,Z);
291 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
292 VX = DYRef*DZini - DZRef*DYini;
293 VY = DZRef*DXini - DXRef*DZini;
294 VZ = DXRef*DYini - DYRef*DXini;
295
296 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
297
298 }
299
300 MyGraphicStructure->Connect(MyGraphicStructure1,Graphic3d_TOC_DESCENDANT);
301// cout << "MyGraphicStructure exploration \n" << flush; MyGraphicStructure->Exploration();
302 MyTypeOfRepresentation = Pres;
303 MyGraphicStructure->Display();
304 TheViewer->SetUpdateMode(UpdSov);
305}
306
307void V3d_DirectionalLight::Direction(Standard_Real& Vx, Standard_Real& Vy, Standard_Real& Vz)const {
308
309 Quantity_Color C ;
310 Graphic3d_Vector V ;
311
312 MyLight->Values(C,V) ;
313 V.Coord(Vx,Vy,Vz) ;
314}