0024752: Visualization - inherit OpenGl_Group from Graphic3d_Group
[occt.git] / src / V3d / V3d_PositionalLight.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/***********************************************************************
15
16 FONCTION :
17 ----------
18 Classe V3d_PositionalLight :
19
20 HISTORIQUE DES MODIFICATIONS :
21 --------------------------------
22 00-09-92 : GG ; Creation.
23 18-06-96 : FMN ; Ajout MyGraphicStructure1 pour sauvegarder snopick
24 24-12-97 : FMN ; Remplacement de math par MathGra
25 31-12-97 : CAL ; Suppression de MathGra
26 21-01-98 : CAL ; Window de Xw et WNT remplacee par Aspect_Window
27 23-02-98 : FMN ; Remplacement PI par Standard_PI
28 30-03-98 : ZOV ; PRO6774 (reconstruction of the class hierarchy and suppressing useless methods)
29
30************************************************************************/
31
32/*----------------------------------------------------------------------*/
33/*
34 * Includes
35 */
36
37#include <V3d.hxx>
38#include <V3d_PositionalLight.ixx>
39#include <Graphic3d_Vector.hxx>
40#include <Graphic3d_Vertex.hxx>
41#include <Graphic3d_Structure.hxx>
42#include <Graphic3d_Group.hxx>
b8ddfc2f 43#include <Graphic3d_ArrayOfSegments.hxx>
7fd59977 44#include <Graphic3d_AspectMarker3d.hxx>
45#include <Graphic3d_AspectLine3d.hxx>
46#include <Graphic3d_AspectText3d.hxx>
47#include <Visual3d_Light.hxx>
48#include <Visual3d_ViewManager.hxx>
49#include <Visual3d_ContextPick.hxx>
50#include <Visual3d_PickDescriptor.hxx>
51#include <Visual3d_HSequenceOfPickPath.hxx>
52#include <Visual3d_PickPath.hxx>
6942f04a 53#include <V3d_BadValue.hxx>
7fd59977 54#include <gp_Dir.hxx>
55#include <gp_Ax1.hxx>
56#include <gp_Vec.hxx>
57#include <gp_Pnt.hxx>
58#include <gp_Trsf.hxx>
59#include <TColStd_Array2OfReal.hxx>
60#include <TCollection_AsciiString.hxx>
61#include <Aspect_Window.hxx>
62
63//-Declarations
64
65//-Constructors
66
67V3d_PositionalLight::V3d_PositionalLight(const Handle(V3d_Viewer)& VM, const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const Quantity_NameOfColor Name, const Standard_Real A1, const Standard_Real A2):V3d_PositionLight(VM) {
68
69 Quantity_Color C(Name) ;
70 Graphic3d_Vertex P(X,Y,Z) ;
71 Graphic3d_Vertex T(0.,0.,0.);
72
6942f04a 73 V3d_BadValue_Raise_if ( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1,
7fd59977 74 "V3d_PositionalLight, bad coefficients");
75
76 MyType = V3d_POSITIONAL ;
77 MyLight = new Visual3d_Light(C,P,A1,A2) ;
78 MyTarget = T;
79
80}
81
82V3d_PositionalLight::V3d_PositionalLight(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_Real A1, const Standard_Real A2):V3d_PositionLight(VM) {
83
84 Quantity_Color C(Name) ;
85 Graphic3d_Vertex T(Xt,Yt,Zt) ;
86 Graphic3d_Vertex P(Xp,Yp,Zp) ;
87
6942f04a 88 V3d_BadValue_Raise_if ( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1,
7fd59977 89 "V3d_PositionalLight, bad coefficients");
90
91 MyType = V3d_POSITIONAL ;
92 MyLight = new Visual3d_Light(C,P,A1,A2) ;
93 MyTarget = T;
81bba717 94 // Graphic structure is initialized during the display.
7fd59977 95
96}
97
98//-Methods, in order
99
100void V3d_PositionalLight::SetPosition(const Standard_Real Xp, const Standard_Real Yp, const Standard_Real Zp) {
101 MyLight->SetPosition (Graphic3d_Vertex (Xp,Yp,Zp));
102}
103
104void V3d_PositionalLight::SetAttenuation(const Standard_Real A1, const Standard_Real A2) {
105
106
6942f04a 107 V3d_BadValue_Raise_if ( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1,
7fd59977 108 "V3d_PositionalLight::SetAttenuation, bad coefficients");
109
110 MyLight->SetAttenuation1(A1) ;
111 MyLight->SetAttenuation2(A2) ;
112}
113
114void V3d_PositionalLight::Position(Standard_Real& X, Standard_Real& Y, Standard_Real& Z)const {
115 Quantity_Color C ;
116 Graphic3d_Vertex P ;
117 Standard_Real A1,A2 ;
118
119 MyLight->Values(C,P,A1,A2) ;
120 P.Coord(X,Y,Z) ;
121}
122
123void V3d_PositionalLight::Attenuation(Standard_Real& A1, Standard_Real& A2)const {
124 Quantity_Color C ;
125 Graphic3d_Vertex P ;
126
127 MyLight->Values(C,P,A1,A2) ;
128}
129
130void V3d_PositionalLight::Symbol (const Handle(Graphic3d_Group)& gsymbol, const Handle(V3d_View)& aView) const {
131
132 Standard_Real Xi,Yi,Zi,Xf,Yf,Zf,Rayon,PXT,PYT,X,Y,Z,XT,YT,ZT;
133 Standard_Real A,B,C,Dist,Beta,CosBeta,SinBeta,Coef,X1,Y1,Z1;
134 Standard_Real VX,VY,VZ;
135 Standard_Integer IXP,IYP,j;
136 TColStd_Array2OfReal MatRot(0,2,0,2);
137
138 aView->Proj(VX,VY,VZ);
139 this->Position(Xi,Yi,Zi);
140 Rayon = this->Radius();
141 aView->Project(Xi,Yi,Zi,PXT,PYT);
142 aView->Convert(PXT,PYT,IXP,IYP);
81bba717 143// 3D Coordinate in the plane of projection of the source.
7fd59977 144 aView->Convert(IXP,IYP,XT,YT,ZT);
145 aView->Convert(PXT,PYT+Rayon,IXP,IYP);
146 aView->Convert(IXP,IYP,X,Y,Z);
147 X = X+Xi-XT; Y = Y+Yi-YT; Z = Z+Zi-ZT;
148 Dist = Sqrt( Square(X-Xi) + Square(Y-Yi) + Square(Z-Zi) );
81bba717 149// Axis of rotation.
7fd59977 150 A = (X-Xi)/Dist;
151 B = (Y-Yi)/Dist;
152 C = (Z-Zi)/Dist;
153
81bba717 154// A sphere is drawn
7fd59977 155 V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
156 for( j=1 ; j<=3 ; j++ ) {
c6541a0c 157 Beta = j * M_PI / 4.;
7fd59977 158 CosBeta = Cos(Beta);
159 SinBeta = Sin(Beta);
160 Coef = 1. - CosBeta;
161 MatRot(0,0) = A * A + (1. - A * A) * CosBeta;
162 MatRot(0,1) = -C * SinBeta + Coef * A * B;
163 MatRot(0,2) = B * SinBeta + Coef * A * C;
164 MatRot(1,0) = C * SinBeta + Coef * A * B;
165 MatRot(1,1) = B * B + (1. - B * B) * CosBeta;
166 MatRot(1,2) = -A * SinBeta + Coef * B * C;
167 MatRot(2,0) = -B * SinBeta + Coef * A * C;
168 MatRot(2,1) = A * SinBeta + Coef * B * C;
169 MatRot(2,2) = C * C + (1. - C * C) * CosBeta;
170 Xf = Xi * MatRot(0,0) + Yi * MatRot(0,1) + Zi * MatRot(0,2);
171 Yf = Xi * MatRot(1,0) + Yi * MatRot(1,1) + Zi * MatRot(1,2);
172 Zf = Xi * MatRot(2,0) + Yi * MatRot(2,1) + Zi * MatRot(2,2);
81bba717 173// Rotation of the normal
7fd59977 174 X1 = VX * MatRot(0,0) + VY * MatRot(0,1) + VZ * MatRot(0,2);
175 Y1 = VX * MatRot(1,0) + VY * MatRot(1,1) + VZ * MatRot(1,2);
176 Z1 = VX * MatRot(2,0) + VY * MatRot(2,1) + VZ * MatRot(2,2);
177 VX = X1 + Xi - Xf ; VY = Y1 + Yi - Yf ; VZ = Z1 + Zi - Zf;
178 V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
179 }
180}
181
182
183void V3d_PositionalLight::Display( const Handle(V3d_View)& aView,
b8ddfc2f 184 const V3d_TypeOfRepresentation TPres )
185{
7fd59977 186 Graphic3d_Vertex PText ;
187 Standard_Real X,Y,Z,Rayon;
188 Standard_Real X0,Y0,Z0,VX,VY,VZ;
189 Standard_Real X1,Y1,Z1;
190 Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
191 Standard_Real R1,G1,B1;
192 V3d_TypeOfRepresentation Pres;
193 V3d_TypeOfUpdate UpdSov;
194
81bba717 195// Creation of a structure slight of markable elements (position of the
196// light, and the domain of lighting represented by a circle)
197// Creation of a structure snopick of non-markable elements (target, meridian and
198// parallel).
7fd59977 199
b8ddfc2f 200 Pres = TPres;
201 Handle(V3d_Viewer) TheViewer = aView->Viewer();
202 UpdSov = TheViewer->UpdateMode();
203 TheViewer->SetUpdateMode(V3d_WAIT);
204 if (!MyGraphicStructure.IsNull()) {
205 MyGraphicStructure->Disconnect(MyGraphicStructure1);
206 MyGraphicStructure->Clear();
207 MyGraphicStructure1->Clear();
208 if (Pres == V3d_SAMELAST) Pres = MyTypeOfRepresentation;
209 }
210 else {
211 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
212 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->Viewer());
213 MyGraphicStructure = slight;
214 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->Viewer());
215 MyGraphicStructure1 = snopick;
216 }
217
b64d84be 218 Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow;
219 if (Pres == V3d_COMPLETE)
220 {
221 gradius = MyGraphicStructure->NewGroup();
222 gExtArrow = MyGraphicStructure->NewGroup();
223 gIntArrow = MyGraphicStructure->NewGroup();
7fd59977 224 }
b64d84be 225 Handle(Graphic3d_Group) glight = MyGraphicStructure->NewGroup();
7fd59977 226 Handle(Graphic3d_Group) gsphere;
b64d84be 227 if (Pres == V3d_COMPLETE
228 || Pres == V3d_PARTIAL)
229 {
230 gsphere = MyGraphicStructure->NewGroup();
231 }
7fd59977 232
b64d84be 233 Handle(Graphic3d_Group) gnopick = MyGraphicStructure1->NewGroup();
7fd59977 234 MyGraphicStructure1->SetPick(Standard_False);
235
236 X0 = MyTarget.X();
237 Y0 = MyTarget.Y();
238 Z0 = MyTarget.Z();
239
81bba717 240// Display of the position of the light.
7fd59977 241
7fd59977 242 this->Color(Quantity_TOC_RGB,R1,G1,B1);
243 Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
244 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
245 Asp1->SetColor(Col1);
246 glight->SetPrimitivesAspect(Asp1);
247 this->Symbol(glight,aView);
248
81bba717 249// Display of the markable sphere (limit at the cercle).
7fd59977 250
251 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
252
253 Rayon = this->Radius();
254 aView->Proj(VX,VY,VZ);
7fd59977 255 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
256
81bba717 257// Display of the radius of the sphere (line + text)
7fd59977 258
259 if (Pres == V3d_COMPLETE) {
7fd59977 260 this->Position(X,Y,Z);
b8ddfc2f 261 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
262 aPrims->AddVertex(X0,Y0,Z0);
263 aPrims->AddVertex(X,Y,Z);
264 gnopick->AddPrimitiveArray(aPrims);
265 V3d::ArrowOfRadius(gExtArrow,X-.1*(X-X0),Y-.1*(Y-Y0),Z-.1*(Z-Z0),X-X0,Y-Y0,Z-Z0,M_PI/15.,Rayon/20.);
c6541a0c 266 V3d::ArrowOfRadius(gIntArrow, X0, Y0, Z0, X0-X, Y0-Y, Z0-Z, M_PI / 15., Rayon / 20.);
7fd59977 267 TCollection_AsciiString ValOfRadius(Rayon);
b8ddfc2f 268 PText.SetCoord( 0.5*(X0+X), 0.5*(Y0+Y), 0.5*(Z0+Z) );
7fd59977 269 gradius->Text(ValOfRadius.ToCString(),PText,0.01);
270 }
271
81bba717 272// Display of the meridian
7fd59977 273
274 Quantity_Color Col2(Quantity_NOC_GREEN);
b8ddfc2f 275 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
7fd59977 276 gnopick->SetPrimitivesAspect(Asp2);
277
81bba717 278// Definition of the axis of circle
7fd59977 279 aView->Up(DXRef,DYRef,DZRef);
280 this->Position(X,Y,Z);
281 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
282 VX = DYRef*DZini - DZRef*DYini;
283 VY = DZRef*DXini - DXRef*DZini;
284 VZ = DXRef*DYini - DYRef*DXini;
285
286 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
287
81bba717 288// Display of the parallel
7fd59977 289
81bba717 290// Definition of the axis of circle
7fd59977 291 aView->Proj(VX,VY,VZ);
292 aView->Up(X1,Y1,Z1);
293 DXRef = VY * Z1 - VZ * Y1;
294 DYRef = VZ * X1 - VX * Z1;
295 DZRef = VX * Y1 - VY * X1;
296 this->Position(X,Y,Z);
297 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
298 VX = DYRef*DZini - DZRef*DYini;
299 VY = DZRef*DXini - DXRef*DZini;
300 VZ = DXRef*DYini - DYRef*DXini;
301
302 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
7fd59977 303 }
304
305 MyGraphicStructure->Connect(MyGraphicStructure1,Graphic3d_TOC_DESCENDANT);
306 MyTypeOfRepresentation = Pres;
307 MyGraphicStructure->Display();
308 TheViewer->SetUpdateMode(UpdSov);
309}