0024393: Visualization - objects position with enchanced precision
[occt.git] / src / V3d / V3d_PositionalLight.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 /***********************************************************************
15      FONCTION :
16      ----------
17         Classe V3d_PositionalLight :
18      HISTORIQUE DES MODIFICATIONS   :
19      --------------------------------
20       00-09-92 : GG  ; Creation.
21       18-06-96 : FMN ; Ajout MyGraphicStructure1 pour sauvegarder snopick
22       24-12-97 : FMN ; Remplacement de math par MathGra
23       31-12-97 : CAL ; Suppression de MathGra
24       21-01-98 : CAL ; Window de Xw et WNT remplacee par Aspect_Window
25       23-02-98 : FMN ; Remplacement PI par Standard_PI
26       30-03-98 : ZOV ; PRO6774 (reconstruction of the class hierarchy and suppressing useless methods)
27 ************************************************************************/
28 /*----------------------------------------------------------------------*/
29 /*
30  * Includes
31  */
32
33 #include <Aspect_Window.hxx>
34 #include <gp_Ax1.hxx>
35 #include <gp_Dir.hxx>
36 #include <gp_Pnt.hxx>
37 #include <gp_Trsf.hxx>
38 #include <gp_Vec.hxx>
39 #include <Graphic3d_ArrayOfSegments.hxx>
40 #include <Graphic3d_AspectLine3d.hxx>
41 #include <Graphic3d_AspectMarker3d.hxx>
42 #include <Graphic3d_AspectText3d.hxx>
43 #include <Graphic3d_Group.hxx>
44 #include <Graphic3d_Structure.hxx>
45 #include <Graphic3d_Vector.hxx>
46 #include <Graphic3d_Vertex.hxx>
47 #include <Standard_Type.hxx>
48 #include <TCollection_AsciiString.hxx>
49 #include <TColStd_Array2OfReal.hxx>
50 #include <V3d.hxx>
51 #include <V3d_BadValue.hxx>
52 #include <V3d_PositionalLight.hxx>
53 #include <V3d_View.hxx>
54 #include <V3d_Viewer.hxx>
55
56 IMPLEMENT_STANDARD_RTTIEXT(V3d_PositionalLight,V3d_PositionLight)
57
58 // =======================================================================
59 // function : V3d_PositionalLight
60 // purpose  :
61 // =======================================================================
62 V3d_PositionalLight::V3d_PositionalLight (const Handle(V3d_Viewer)& theViewer,
63                                           const Standard_Real theX,
64                                           const Standard_Real theY,
65                                           const Standard_Real theZ,
66                                           const Quantity_NameOfColor theColor,
67                                           const Standard_Real theConstAttenuation,
68                                           const Standard_Real theLinearAttenuation)
69 : V3d_PositionLight (theViewer)
70 {
71   SetType (V3d_POSITIONAL);
72   SetColor (Quantity_Color (theColor));
73   SetTarget (0., 0., 0.);
74   SetPosition (theX, theY, theZ);
75   SetAttenuation (theConstAttenuation, theLinearAttenuation);
76 }
77
78 // =======================================================================
79 // function : V3d_PositionalLight
80 // purpose  :
81 // =======================================================================
82 V3d_PositionalLight::V3d_PositionalLight (const Handle(V3d_Viewer)& theViewer,
83                                           const Standard_Real theXt,
84                                           const Standard_Real theYt,
85                                           const Standard_Real theZt,
86                                           const Standard_Real theXp,
87                                           const Standard_Real theYp,
88                                           const Standard_Real theZp,
89                                           const Quantity_NameOfColor theColor,
90                                           const Standard_Real theConstAttenuation,
91                                           const Standard_Real theLinearAttenuation)
92 : V3d_PositionLight (theViewer)
93 {
94   SetType (V3d_POSITIONAL);
95   SetColor (Quantity_Color (theColor));
96   SetTarget (theXt, theYt, theZt);
97   SetPosition (theXp, theYp, theZp);
98   SetAttenuation (theConstAttenuation, theLinearAttenuation);
99 }
100
101 // =======================================================================
102 // function : SetSmoothRadius
103 // purpose  :
104 // =======================================================================
105 void V3d_PositionalLight::SetSmoothRadius (const Standard_Real theValue)
106 {
107   V3d_BadValue_Raise_if (theValue < 0.0,
108     "V3d_PositionalLight::SetSmoothRadius,"
109     "Bad value for smoothing radius");
110
111   myLight.Smoothness = static_cast<Standard_ShortReal> (theValue);
112 }
113
114 // =======================================================================
115 // function : SetPosition
116 // purpose  :
117 // =======================================================================
118 void V3d_PositionalLight::SetPosition (const Standard_Real theXp,
119                                        const Standard_Real theYp,
120                                        const Standard_Real theZp)
121 {
122   myLight.Position.x() = theXp;
123   myLight.Position.y() = theYp;
124   myLight.Position.z() = theZp;
125 }
126
127 // =======================================================================
128 // function : SetAttenuation
129 // purpose  :
130 // =======================================================================
131 void V3d_PositionalLight::SetAttenuation (const Standard_Real theConstAttenuation,
132                                           const Standard_Real theLinearAttenuation)
133 {
134   V3d_BadValue_Raise_if (theConstAttenuation < 0.
135                       || theConstAttenuation > 1.
136                       || theLinearAttenuation < 0.
137                       || theLinearAttenuation > 1.,
138     "V3d_PositionalLight::SetAttenuation, bad coefficients");
139
140   myLight.ChangeConstAttenuation()  = static_cast<Standard_ShortReal> (theConstAttenuation);
141   myLight.ChangeLinearAttenuation() = static_cast<Standard_ShortReal> (theLinearAttenuation);
142 }
143
144 // =======================================================================
145 // function : Position
146 // purpose  :
147 // =======================================================================
148 void V3d_PositionalLight::Position (Standard_Real& theX, Standard_Real& theY, Standard_Real& theZ) const
149 {
150   theX = myLight.Position.x();
151   theY = myLight.Position.y();
152   theZ = myLight.Position.z();
153 }
154
155 // =======================================================================
156 // function : Attenuation
157 // purpose  :
158 // =======================================================================
159 void V3d_PositionalLight::Attenuation (Standard_Real& theConstAttenuation,
160                                        Standard_Real& theLinearAttenuation) const
161 {
162   theConstAttenuation  = myLight.ConstAttenuation();
163   theLinearAttenuation = myLight.LinearAttenuation();
164 }
165
166 // =======================================================================
167 // function : Symbol
168 // purpose  :
169 // =======================================================================
170 void V3d_PositionalLight::Symbol (const Handle(Graphic3d_Group)& theSymbol, const Handle(V3d_View)& theView) const
171 {
172   Standard_Real Xi,Yi,Zi,Xf,Yf,Zf,Rayon,PXT,PYT,X,Y,Z,XT,YT,ZT;
173   Standard_Real A,B,C,Dist,Beta,CosBeta,SinBeta,Coef,X1,Y1,Z1;
174   Standard_Real VX,VY,VZ;
175   Standard_Integer IXP,IYP,j;
176   TColStd_Array2OfReal MatRot(0,2,0,2);
177
178   theView->Proj(VX,VY,VZ);
179   this->Position(Xi,Yi,Zi);
180   Rayon = this->Radius();
181   theView->Project(Xi,Yi,Zi,PXT,PYT); 
182   theView->Convert(PXT,PYT,IXP,IYP);
183 //  3D Coordinate in the plane of projection of the source.
184   theView->Convert(IXP,IYP,XT,YT,ZT);
185   theView->Convert(PXT,PYT+Rayon,IXP,IYP);
186   theView->Convert(IXP,IYP,X,Y,Z);
187   X = X+Xi-XT; Y = Y+Yi-YT; Z = Z+Zi-ZT;
188   Dist = Sqrt( Square(X-Xi) + Square(Y-Yi) + Square(Z-Zi) );
189 //  Axis of rotation.
190   A = (X-Xi)/Dist;
191   B = (Y-Yi)/Dist;
192   C = (Z-Zi)/Dist;
193
194 //  A sphere is drawn
195   V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
196   for( j=1 ; j<=3 ; j++ ) {
197     Beta = j * M_PI / 4.;
198     CosBeta = Cos(Beta);
199     SinBeta = Sin(Beta);
200     Coef = 1. - CosBeta;
201     MatRot(0,0) =  A * A + (1. - A * A) * CosBeta;
202     MatRot(0,1) = -C * SinBeta + Coef * A * B;
203     MatRot(0,2) =  B * SinBeta + Coef * A * C;
204     MatRot(1,0) =  C * SinBeta + Coef * A * B; 
205     MatRot(1,1) =  B * B + (1. - B * B) * CosBeta;
206     MatRot(1,2) = -A * SinBeta + Coef * B * C;
207     MatRot(2,0) = -B * SinBeta + Coef * A * C;
208     MatRot(2,1) =  A * SinBeta + Coef * B * C;
209     MatRot(2,2) =  C * C + (1. - C * C) * CosBeta;
210     Xf = Xi * MatRot(0,0) + Yi * MatRot(0,1) + Zi * MatRot(0,2);
211     Yf = Xi * MatRot(1,0) + Yi * MatRot(1,1) + Zi * MatRot(1,2);
212     Zf = Xi * MatRot(2,0) + Yi * MatRot(2,1) + Zi * MatRot(2,2);
213 //    Rotation of the normal
214     X1 = VX * MatRot(0,0) + VY * MatRot(0,1) + VZ * MatRot(0,2);
215     Y1 = VX * MatRot(1,0) + VY * MatRot(1,1) + VZ * MatRot(1,2);
216     Z1 = VX * MatRot(2,0) + VY * MatRot(2,1) + VZ * MatRot(2,2);
217     VX = X1 + Xi - Xf ; VY = Y1 + Yi - Yf ; VZ = Z1 + Zi - Zf;
218     V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
219   }
220 }
221
222 // =======================================================================
223 // function : Display
224 // purpose  :
225 // =======================================================================
226 void V3d_PositionalLight::Display (const Handle(V3d_View)& theView,
227                                    const V3d_TypeOfRepresentation theRepresentation)
228 {
229   Graphic3d_Vertex PText ;
230   Standard_Real X,Y,Z,Rayon;
231   Standard_Real X0,Y0,Z0,VX,VY,VZ;
232   Standard_Real X1,Y1,Z1;
233   Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
234   Standard_Real R1,G1,B1;
235   V3d_TypeOfRepresentation Pres;
236   V3d_TypeOfUpdate UpdSov;
237
238 //  Creation of a structure slight of markable elements (position of the
239 //  light, and the domain of lighting represented by a circle)
240 //  Creation of a structure snopick of non-markable elements (target, meridian and 
241 //  parallel).
242
243   Pres = theRepresentation;
244   Handle(V3d_Viewer) TheViewer = theView->Viewer();
245   UpdSov = TheViewer->UpdateMode();
246   TheViewer->SetUpdateMode(V3d_WAIT);
247   if (!myGraphicStructure.IsNull()) {
248     myGraphicStructure->Disconnect(myGraphicStructure1);
249     myGraphicStructure->Clear();
250     myGraphicStructure1->Clear();
251     if (Pres == V3d_SAMELAST) Pres = myTypeOfRepresentation;
252   }
253   else {
254     if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
255     Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->StructureManager());
256     myGraphicStructure = slight;
257     Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->StructureManager()); 
258     myGraphicStructure1 = snopick;
259   }
260
261   Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow;
262   if (Pres == V3d_COMPLETE)
263   {
264     gradius   = myGraphicStructure->NewGroup();
265     gExtArrow = myGraphicStructure->NewGroup();
266     gIntArrow = myGraphicStructure->NewGroup();
267   }
268   Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup();
269   Handle(Graphic3d_Group) gsphere;
270   if (Pres == V3d_COMPLETE
271    || Pres == V3d_PARTIAL)
272   {
273     gsphere = myGraphicStructure->NewGroup();
274   }
275   
276   Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup();
277   
278   X0 = myTarget.X();
279   Y0 = myTarget.Y();
280   Z0 = myTarget.Z();
281   
282 // Display of the position of the light.
283
284   this->Color(Quantity_TOC_RGB,R1,G1,B1);
285   Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
286   Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
287   Asp1->SetColor(Col1);
288   glight->SetPrimitivesAspect(Asp1);
289   this->Symbol(glight,theView);
290
291 // Display of the markable sphere (limit at the cercle).
292
293   if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
294       
295     Rayon = this->Radius();
296     theView->Proj(VX,VY,VZ);
297     V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
298
299 // Display of the radius of the sphere (line + text)
300
301     if (Pres == V3d_COMPLETE) {
302       this->Position(X,Y,Z);
303       Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
304       aPrims->AddVertex(X0,Y0,Z0);
305       aPrims->AddVertex(X,Y,Z);
306       gnopick->AddPrimitiveArray(aPrims);
307       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.);
308       V3d::ArrowOfRadius(gIntArrow, X0, Y0, Z0, X0-X, Y0-Y, Z0-Z, M_PI / 15., Rayon / 20.);
309       TCollection_AsciiString ValOfRadius(Rayon);
310       PText.SetCoord( 0.5*(X0+X), 0.5*(Y0+Y), 0.5*(Z0+Z) );
311       gradius->Text(ValOfRadius.ToCString(),PText,0.01);
312     }
313     
314 // Display of the meridian
315
316     Quantity_Color Col2(Quantity_NOC_GREEN);
317     Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
318     gnopick->SetPrimitivesAspect(Asp2);
319     
320 //    Definition of the axis of circle
321     theView->Up(DXRef,DYRef,DZRef);
322     this->Position(X,Y,Z);
323     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
324     VX = DYRef*DZini - DZRef*DYini;
325     VY = DZRef*DXini - DXRef*DZini;
326     VZ = DXRef*DYini - DYRef*DXini;
327     
328     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
329       
330 //  Display of the parallel
331
332 //  Definition of the axis of circle
333     theView->Proj(VX,VY,VZ);
334     theView->Up(X1,Y1,Z1);
335     DXRef = VY * Z1 - VZ * Y1;
336     DYRef = VZ * X1 - VX * Z1;
337     DZRef = VX * Y1 - VY * X1;
338     this->Position(X,Y,Z);
339     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
340     VX = DYRef*DZini - DZRef*DYini;
341     VY = DZRef*DXini - DXRef*DZini;
342     VZ = DXRef*DYini - DYRef*DXini;
343     
344     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
345   }
346
347   myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT);
348   myTypeOfRepresentation = Pres;
349   myGraphicStructure->Display();
350   TheViewer->SetUpdateMode(UpdSov);
351 }