0023634: Eliminate Polyline and Polygon usage in drawers
[occt.git] / src / V3d / V3d_PositionalLight.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_PositionalLight :
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_PositionalLight.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 <TCollection_AsciiString.hxx>
65#include <Aspect_Window.hxx>
66
67//-Declarations
68
69//-Constructors
70
71V3d_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) {
72
73 Quantity_Color C(Name) ;
74 Graphic3d_Vertex P(X,Y,Z) ;
75 Graphic3d_Vertex T(0.,0.,0.);
76
77 Viewer_BadValue_Raise_if ( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1,
78 "V3d_PositionalLight, bad coefficients");
79
80 MyType = V3d_POSITIONAL ;
81 MyLight = new Visual3d_Light(C,P,A1,A2) ;
82 MyTarget = T;
83
84}
85
86V3d_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) {
87
88 Quantity_Color C(Name) ;
89 Graphic3d_Vertex T(Xt,Yt,Zt) ;
90 Graphic3d_Vertex P(Xp,Yp,Zp) ;
91
92 Viewer_BadValue_Raise_if ( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1,
93 "V3d_PositionalLight, bad coefficients");
94
95 MyType = V3d_POSITIONAL ;
96 MyLight = new Visual3d_Light(C,P,A1,A2) ;
97 MyTarget = T;
81bba717 98 // Graphic structure is initialized during the display.
7fd59977 99
100}
101
102//-Methods, in order
103
104void V3d_PositionalLight::SetPosition(const Standard_Real Xp, const Standard_Real Yp, const Standard_Real Zp) {
105 MyLight->SetPosition (Graphic3d_Vertex (Xp,Yp,Zp));
106}
107
108void V3d_PositionalLight::SetAttenuation(const Standard_Real A1, const Standard_Real A2) {
109
110
111 Viewer_BadValue_Raise_if ( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1,
112 "V3d_PositionalLight::SetAttenuation, bad coefficients");
113
114 MyLight->SetAttenuation1(A1) ;
115 MyLight->SetAttenuation2(A2) ;
116}
117
118void V3d_PositionalLight::Position(Standard_Real& X, Standard_Real& Y, Standard_Real& Z)const {
119 Quantity_Color C ;
120 Graphic3d_Vertex P ;
121 Standard_Real A1,A2 ;
122
123 MyLight->Values(C,P,A1,A2) ;
124 P.Coord(X,Y,Z) ;
125}
126
127void V3d_PositionalLight::Attenuation(Standard_Real& A1, Standard_Real& A2)const {
128 Quantity_Color C ;
129 Graphic3d_Vertex P ;
130
131 MyLight->Values(C,P,A1,A2) ;
132}
133
134void V3d_PositionalLight::Symbol (const Handle(Graphic3d_Group)& gsymbol, const Handle(V3d_View)& aView) const {
135
136 Standard_Real Xi,Yi,Zi,Xf,Yf,Zf,Rayon,PXT,PYT,X,Y,Z,XT,YT,ZT;
137 Standard_Real A,B,C,Dist,Beta,CosBeta,SinBeta,Coef,X1,Y1,Z1;
138 Standard_Real VX,VY,VZ;
139 Standard_Integer IXP,IYP,j;
140 TColStd_Array2OfReal MatRot(0,2,0,2);
141
142 aView->Proj(VX,VY,VZ);
143 this->Position(Xi,Yi,Zi);
144 Rayon = this->Radius();
145 aView->Project(Xi,Yi,Zi,PXT,PYT);
146 aView->Convert(PXT,PYT,IXP,IYP);
81bba717 147// 3D Coordinate in the plane of projection of the source.
7fd59977 148 aView->Convert(IXP,IYP,XT,YT,ZT);
149 aView->Convert(PXT,PYT+Rayon,IXP,IYP);
150 aView->Convert(IXP,IYP,X,Y,Z);
151 X = X+Xi-XT; Y = Y+Yi-YT; Z = Z+Zi-ZT;
152 Dist = Sqrt( Square(X-Xi) + Square(Y-Yi) + Square(Z-Zi) );
81bba717 153// Axis of rotation.
7fd59977 154 A = (X-Xi)/Dist;
155 B = (Y-Yi)/Dist;
156 C = (Z-Zi)/Dist;
157
81bba717 158// A sphere is drawn
7fd59977 159 V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
160 for( j=1 ; j<=3 ; j++ ) {
c6541a0c 161 Beta = j * M_PI / 4.;
7fd59977 162 CosBeta = Cos(Beta);
163 SinBeta = Sin(Beta);
164 Coef = 1. - CosBeta;
165 MatRot(0,0) = A * A + (1. - A * A) * CosBeta;
166 MatRot(0,1) = -C * SinBeta + Coef * A * B;
167 MatRot(0,2) = B * SinBeta + Coef * A * C;
168 MatRot(1,0) = C * SinBeta + Coef * A * B;
169 MatRot(1,1) = B * B + (1. - B * B) * CosBeta;
170 MatRot(1,2) = -A * SinBeta + Coef * B * C;
171 MatRot(2,0) = -B * SinBeta + Coef * A * C;
172 MatRot(2,1) = A * SinBeta + Coef * B * C;
173 MatRot(2,2) = C * C + (1. - C * C) * CosBeta;
174 Xf = Xi * MatRot(0,0) + Yi * MatRot(0,1) + Zi * MatRot(0,2);
175 Yf = Xi * MatRot(1,0) + Yi * MatRot(1,1) + Zi * MatRot(1,2);
176 Zf = Xi * MatRot(2,0) + Yi * MatRot(2,1) + Zi * MatRot(2,2);
81bba717 177// Rotation of the normal
7fd59977 178 X1 = VX * MatRot(0,0) + VY * MatRot(0,1) + VZ * MatRot(0,2);
179 Y1 = VX * MatRot(1,0) + VY * MatRot(1,1) + VZ * MatRot(1,2);
180 Z1 = VX * MatRot(2,0) + VY * MatRot(2,1) + VZ * MatRot(2,2);
181 VX = X1 + Xi - Xf ; VY = Y1 + Yi - Yf ; VZ = Z1 + Zi - Zf;
182 V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
183 }
184}
185
186
187void V3d_PositionalLight::Display( const Handle(V3d_View)& aView,
b8ddfc2f 188 const V3d_TypeOfRepresentation TPres )
189{
7fd59977 190 Graphic3d_Vertex PText ;
191 Standard_Real X,Y,Z,Rayon;
192 Standard_Real X0,Y0,Z0,VX,VY,VZ;
193 Standard_Real X1,Y1,Z1;
194 Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
195 Standard_Real R1,G1,B1;
196 V3d_TypeOfRepresentation Pres;
197 V3d_TypeOfUpdate UpdSov;
198
81bba717 199// Creation of a structure slight of markable elements (position of the
200// light, and the domain of lighting represented by a circle)
201// Creation of a structure snopick of non-markable elements (target, meridian and
202// parallel).
7fd59977 203
b8ddfc2f 204 Pres = TPres;
205 Handle(V3d_Viewer) TheViewer = aView->Viewer();
206 UpdSov = TheViewer->UpdateMode();
207 TheViewer->SetUpdateMode(V3d_WAIT);
208 if (!MyGraphicStructure.IsNull()) {
209 MyGraphicStructure->Disconnect(MyGraphicStructure1);
210 MyGraphicStructure->Clear();
211 MyGraphicStructure1->Clear();
212 if (Pres == V3d_SAMELAST) Pres = MyTypeOfRepresentation;
213 }
214 else {
215 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
216 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->Viewer());
217 MyGraphicStructure = slight;
218 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->Viewer());
219 MyGraphicStructure1 = snopick;
220 }
221
7fd59977 222 Handle(Graphic3d_Group) gradius;
223 Handle(Graphic3d_Group) gExtArrow;
224 Handle(Graphic3d_Group) gIntArrow;
225 if (Pres == V3d_COMPLETE) {
226 gradius = new Graphic3d_Group(MyGraphicStructure);
227 gExtArrow = new Graphic3d_Group(MyGraphicStructure);
228 gIntArrow = new Graphic3d_Group(MyGraphicStructure);
229 }
230 Handle(Graphic3d_Group) glight = new Graphic3d_Group(MyGraphicStructure);
231 Handle(Graphic3d_Group) gsphere;
232 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) gsphere = new Graphic3d_Group(MyGraphicStructure);
233
234 Handle(Graphic3d_Group) gnopick = new Graphic3d_Group(MyGraphicStructure1);
235 MyGraphicStructure1->SetPick(Standard_False);
236
237 X0 = MyTarget.X();
238 Y0 = MyTarget.Y();
239 Z0 = MyTarget.Z();
240
81bba717 241// Display of the position of the light.
7fd59977 242
7fd59977 243 this->Color(Quantity_TOC_RGB,R1,G1,B1);
244 Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
245 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
246 Asp1->SetColor(Col1);
247 glight->SetPrimitivesAspect(Asp1);
248 this->Symbol(glight,aView);
249
81bba717 250// Display of the markable sphere (limit at the cercle).
7fd59977 251
252 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
253
254 Rayon = this->Radius();
255 aView->Proj(VX,VY,VZ);
7fd59977 256 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
257
81bba717 258// Display of the radius of the sphere (line + text)
7fd59977 259
260 if (Pres == V3d_COMPLETE) {
7fd59977 261 this->Position(X,Y,Z);
b8ddfc2f 262 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
263 aPrims->AddVertex(X0,Y0,Z0);
264 aPrims->AddVertex(X,Y,Z);
265 gnopick->AddPrimitiveArray(aPrims);
266 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 267 V3d::ArrowOfRadius(gIntArrow, X0, Y0, Z0, X0-X, Y0-Y, Z0-Z, M_PI / 15., Rayon / 20.);
7fd59977 268 TCollection_AsciiString ValOfRadius(Rayon);
b8ddfc2f 269 PText.SetCoord( 0.5*(X0+X), 0.5*(Y0+Y), 0.5*(Z0+Z) );
7fd59977 270 gradius->Text(ValOfRadius.ToCString(),PText,0.01);
271 }
272
81bba717 273// Display of the meridian
7fd59977 274
275 Quantity_Color Col2(Quantity_NOC_GREEN);
b8ddfc2f 276 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
7fd59977 277 gnopick->SetPrimitivesAspect(Asp2);
278
81bba717 279// Definition of the axis of circle
7fd59977 280 aView->Up(DXRef,DYRef,DZRef);
281 this->Position(X,Y,Z);
282 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
283 VX = DYRef*DZini - DZRef*DYini;
284 VY = DZRef*DXini - DXRef*DZini;
285 VZ = DXRef*DYini - DYRef*DXini;
286
287 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
288
81bba717 289// Display of the parallel
7fd59977 290
81bba717 291// Definition of the axis of circle
7fd59977 292 aView->Proj(VX,VY,VZ);
293 aView->Up(X1,Y1,Z1);
294 DXRef = VY * Z1 - VZ * Y1;
295 DYRef = VZ * X1 - VX * Z1;
296 DZRef = VX * Y1 - VY * X1;
297 this->Position(X,Y,Z);
298 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
299 VX = DYRef*DZini - DZRef*DYini;
300 VY = DZRef*DXini - DXRef*DZini;
301 VZ = DXRef*DYini - DYRef*DXini;
302
303 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
7fd59977 304 }
305
306 MyGraphicStructure->Connect(MyGraphicStructure1,Graphic3d_TOC_DESCENDANT);
307 MyTypeOfRepresentation = Pres;
308 MyGraphicStructure->Display();
309 TheViewer->SetUpdateMode(UpdSov);
310}