0027667: OCAF binary persistence hangs on reading truncated CBF file
[occt.git] / src / V3d / V3d_SpotLight.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_SpotLight :
18      HISTORIQUE DES MODIFICATIONS   :
19      --------------------------------
20       00-09-92 : GG  ; Creation.
21       18-06-96 : FMN ; Ajout MyGraphicStructure1 pour sauvegarder snopick
22       30-03-98 : ZOV ; PRO6774 (reconstruction of the class hierarchy and suppressing useless methods)
23       02.15.100 : JR : Clutter
24 ************************************************************************/
25 /*----------------------------------------------------------------------*/
26 /*
27  * Includes
28  */
29
30 #include <gp_Ax1.hxx>
31 #include <gp_Dir.hxx>
32 #include <gp_Pnt.hxx>
33 #include <gp_Trsf.hxx>
34 #include <gp_Vec.hxx>
35 #include <Graphic3d_ArrayOfSegments.hxx>
36 #include <Graphic3d_AspectLine3d.hxx>
37 #include <Graphic3d_AspectMarker3d.hxx>
38 #include <Graphic3d_AspectText3d.hxx>
39 #include <Graphic3d_Group.hxx>
40 #include <Graphic3d_Structure.hxx>
41 #include <Graphic3d_Vector.hxx>
42 #include <Graphic3d_Vertex.hxx>
43 #include <Standard_Type.hxx>
44 #include <TCollection_AsciiString.hxx>
45 #include <V3d.hxx>
46 #include <V3d_BadValue.hxx>
47 #include <V3d_SpotLight.hxx>
48 #include <V3d_View.hxx>
49 #include <V3d_Viewer.hxx>
50
51 IMPLEMENT_STANDARD_RTTIEXT(V3d_SpotLight,V3d_PositionLight)
52
53 // =======================================================================
54 // function : V3d_SpotLight
55 // purpose  :
56 // =======================================================================
57 V3d_SpotLight::V3d_SpotLight (const Handle(V3d_Viewer)& theViewer,
58                               const Standard_Real theX,
59                               const Standard_Real theY,
60                               const Standard_Real theZ,
61                               const V3d_TypeOfOrientation theDirection,
62                               const Quantity_NameOfColor theColor,
63                               const Standard_Real theConstAttenuation,
64                               const Standard_Real theLinearAttenuation,
65                               const Standard_Real theConcentration,
66                               const Standard_Real theAngle)
67 : V3d_PositionLight (theViewer)
68 {
69   Graphic3d_Vector aDir = V3d::GetProjAxis (theDirection);
70   SetType (V3d_SPOT);
71   SetColor (theColor);
72   SetTarget (theX + aDir.X(), theY + aDir.Y(), theZ + aDir.Z());
73   SetPosition (theX, theY, theZ);
74   SetDirection (aDir.X(), aDir.Y(), aDir.Z());
75   SetAttenuation (theConstAttenuation, theLinearAttenuation);
76   SetConcentration (theConcentration);
77   SetAngle (theAngle);
78 }
79
80 // =======================================================================
81 // function : V3d_SpotLight
82 // purpose  :
83 // =======================================================================
84 V3d_SpotLight::V3d_SpotLight (const Handle(V3d_Viewer)& theViewer,
85                               const Standard_Real theXt,
86                               const Standard_Real theYt,
87                               const Standard_Real theZt,
88                               const Standard_Real theXp,
89                               const Standard_Real theYp,
90                               const Standard_Real theZp,
91                               const Quantity_NameOfColor theColor,
92                               const Standard_Real theConstAttenuation,
93                               const Standard_Real theLinearAttenuation,
94                               const Standard_Real theConcentration,
95                               const Standard_Real theAngle)
96 : V3d_PositionLight (theViewer)
97 {
98   SetType (V3d_SPOT);
99   SetColor (theColor);
100   SetTarget (theXt, theYt, theZt);
101   SetPosition (theXp, theYp, theZp);
102   SetDirection (theXt - theXp, theYt - theYp, theZt - theZp);
103   SetAttenuation (theConstAttenuation, theLinearAttenuation);
104   SetConcentration (theConcentration);
105   SetAngle (theAngle);
106 }
107
108 // =======================================================================
109 // function : SetPosition
110 // purpose  :
111 // =======================================================================
112 void V3d_SpotLight::SetPosition (const Standard_Real theXp,
113                                  const Standard_Real theYp,
114                                  const Standard_Real theZp)
115 {
116   myLight.Position.x() = theXp;
117   myLight.Position.y() = theYp;
118   myLight.Position.z() = theZp;
119 }
120
121 // =======================================================================
122 // function : SetDirection
123 // purpose  :
124 // =======================================================================
125 void V3d_SpotLight::SetDirection (const Standard_Real theVx,
126                                   const Standard_Real theVy,
127                                   const Standard_Real theVz)
128 {
129   myLight.Direction.x() = static_cast<Standard_ShortReal> (theVx);
130   myLight.Direction.y() = static_cast<Standard_ShortReal> (theVy);
131   myLight.Direction.z() = static_cast<Standard_ShortReal> (theVz);
132 }
133
134 // =======================================================================
135 // function : SetDirection
136 // purpose  :
137 // =======================================================================
138 void V3d_SpotLight::SetDirection (const V3d_TypeOfOrientation theDirection)
139 {
140   Graphic3d_Vector aDir = V3d::GetProjAxis (theDirection);
141   SetDirection (aDir.X(), aDir.Y(), aDir.Z());
142 }
143
144 // =======================================================================
145 // function : SetAttenuation
146 // purpose  :
147 // =======================================================================
148 void V3d_SpotLight::SetAttenuation (const Standard_Real theConstAttenuation,
149                                     const Standard_Real theLinearAttenuation)
150 {
151   V3d_BadValue_Raise_if (theConstAttenuation  < 0. ||
152                          theConstAttenuation  > 1. ||
153                          theLinearAttenuation < 0. ||
154                          theLinearAttenuation > 1 ,
155                          "V3d_SpotLight::SetAttenuation, "
156                          "bad coefficients");
157
158   myLight.ChangeConstAttenuation()  = static_cast<Standard_ShortReal> (theConstAttenuation);
159   myLight.ChangeLinearAttenuation() = static_cast<Standard_ShortReal> (theLinearAttenuation);
160 }
161
162 // =======================================================================
163 // function : SetConcentration
164 // purpose  :
165 // =======================================================================
166 void V3d_SpotLight::SetConcentration (const Standard_Real theConcentration)
167 {
168   V3d_BadValue_Raise_if (theConcentration < 0. ||
169                          theConcentration > 1.,
170                          "V3d_SpotLight::SetConcentration, "
171                          "bad coefficient");
172
173   myLight.ChangeConcentration() = static_cast<Standard_ShortReal> (theConcentration);
174 }
175
176 // =======================================================================
177 // function : SetAngle
178 // purpose  :
179 // =======================================================================
180 void V3d_SpotLight::SetAngle (const Standard_Real theAngle)
181 {
182   V3d_BadValue_Raise_if (theAngle <= 0. || 
183                          theAngle >= M_PI,
184                          "V3d_SpotLight::SetAngle, "
185                          "bad angle");
186
187   myLight.ChangeAngle() = static_cast<Standard_ShortReal> (theAngle);
188 }
189
190 // =======================================================================
191 // function : Direction
192 // purpose  :
193 // =======================================================================
194 void V3d_SpotLight::Direction (Standard_Real& theVx,
195                                Standard_Real& theVy,
196                                Standard_Real& theVz) const
197 {
198   theVx = myLight.Direction.x();
199   theVy = myLight.Direction.y();
200   theVz = myLight.Direction.z();
201 }
202
203 // =======================================================================
204 // function : Direction
205 // purpose  :
206 // =======================================================================
207 void V3d_SpotLight::Position (Standard_Real& theXp,
208                               Standard_Real& theYp,
209                               Standard_Real& theZp) const
210 {
211   theXp = myLight.Position.x();
212   theYp = myLight.Position.y();
213   theZp = myLight.Position.z();
214 }
215
216 // =======================================================================
217 // function : Attenuation
218 // purpose  :
219 // =======================================================================
220 void V3d_SpotLight::Attenuation (Standard_Real& theConstAttenuation,
221                                  Standard_Real& theLinearAttenuation) const
222 {
223   theConstAttenuation  = myLight.ConstAttenuation();
224   theLinearAttenuation = myLight.LinearAttenuation();
225 }
226
227 // =======================================================================
228 // function : Concentration
229 // purpose  :
230 // =======================================================================
231 Standard_Real V3d_SpotLight::Concentration ()const
232 {
233   return myLight.Concentration();
234 }
235
236 // =======================================================================
237 // function : Concentration
238 // purpose  :
239 // =======================================================================
240 Standard_Real V3d_SpotLight::Angle()const
241 {
242   return myLight.Angle();
243 }
244
245 // =======================================================================
246 // function : Symbol
247 // purpose  :
248 // =======================================================================
249 void V3d_SpotLight::Symbol (const Handle(Graphic3d_Group)& theSymbol,
250                             const Handle(V3d_View)& ) const
251 {
252   Standard_Real X,Y,Z;
253   Standard_Real DX,DY,DZ;
254   this->Position(X,Y,Z);
255   this->Direction(DX,DY,DZ);
256
257   V3d::ArrowOfRadius(theSymbol,X,Y,Z,-DX,-DY,-DZ,M_PI/8.,this->Radius()/15.);
258 }
259
260 // =======================================================================
261 // function : Display
262 // purpose  :
263 // =======================================================================
264 void V3d_SpotLight::Display (const Handle(V3d_View)& theView,
265                              const V3d_TypeOfRepresentation theTPres)
266 {
267   Graphic3d_Vertex PText ;
268   Standard_Real X,Y,Z,Rayon;
269   Standard_Real X0,Y0,Z0,VX,VY,VZ;
270   Standard_Real X1,Y1,Z1;
271   Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
272   Standard_Real R1,G1,B1;
273   V3d_TypeOfRepresentation Pres;
274
275 //  Creation of a structure slight of markable elements (position of the
276 //  light, and the domain of lighting represented by a circle)
277 //  Creation of a structure snopick of non-markable elements (target, meridian and 
278 //  parallel).// 
279
280   Pres = theTPres;
281   Handle(V3d_Viewer) TheViewer = theView->Viewer();
282   if (!myGraphicStructure.IsNull()) {
283     myGraphicStructure->Disconnect(myGraphicStructure1);
284     myGraphicStructure->Clear();
285     myGraphicStructure1->Clear();
286     if (Pres == V3d_SAMELAST) Pres = myTypeOfRepresentation;
287   }
288   else {
289     if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
290     Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->StructureManager());
291     myGraphicStructure = slight;
292     Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->StructureManager()); 
293     myGraphicStructure1 = snopick;
294   }
295
296   Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow;
297   if (Pres == V3d_COMPLETE)
298   {
299     gradius   = myGraphicStructure->NewGroup();
300     gExtArrow = myGraphicStructure->NewGroup();
301     gIntArrow = myGraphicStructure->NewGroup();
302   }
303   Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup();
304   Handle(Graphic3d_Group) gsphere;
305   if (Pres == V3d_COMPLETE
306    || Pres == V3d_PARTIAL)
307   {
308     gsphere = myGraphicStructure->NewGroup();
309   }
310   
311   Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup();
312   
313   X0 = myTarget.X();
314   Y0 = myTarget.Y();
315   Z0 = myTarget.Z();
316   
317 //Display of the position of the light.
318
319   this->Color(Quantity_TOC_RGB,R1,G1,B1);
320   Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
321   Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
322   Asp1->SetColor(Col1);
323   glight->SetPrimitivesAspect(Asp1);
324   this->Symbol(glight,theView);
325   
326 // Display of the reference sphere (limited by circle).
327
328   if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
329     
330     Rayon = this->Radius(); 
331     theView->Proj(VX,VY,VZ);
332     V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
333
334 // Display of the radius of the sphere (line + text)
335
336     if (Pres == V3d_COMPLETE) {
337       this->Position(X,Y,Z);
338       Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
339       aPrims->AddVertex(X0,Y0,Z0);
340       aPrims->AddVertex(X,Y,Z);
341       gnopick->AddPrimitiveArray(aPrims);
342       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.);
343       V3d::ArrowOfRadius(gIntArrow,X0,Y0,Z0,X0-X,Y0-Y,Z0-Z,M_PI/15.,Rayon/20.);
344       TCollection_AsciiString ValOfRadius(Rayon);
345       PText.SetCoord( .5*(X0+X), .5*(Y0+Y), .5*(Z0+Z) );
346       gradius->Text(ValOfRadius.ToCString(),PText,0.01);
347     }
348     
349 // Display of the meridian
350
351     Quantity_Color Col2(Quantity_NOC_GREEN);
352     Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
353     gnopick->SetPrimitivesAspect(Asp2);
354     
355 //    Definition of the axis of the circle
356     theView->Up(DXRef,DYRef,DZRef);
357     this->Position(X,Y,Z);
358     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
359     VX = DYRef*DZini - DZRef*DYini;
360     VY = DZRef*DXini - DXRef*DZini;
361     VZ = DXRef*DYini - DYRef*DXini;
362     
363     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
364
365 //    Display of the parallel
366
367 //    Definition of the axis of the circle
368     theView->Proj(VX,VY,VZ);
369     theView->Up(X1,Y1,Z1);
370     DXRef = VY * Z1 - VZ * Y1;
371     DYRef = VZ * X1 - VX * Z1;
372     DZRef = VX * Y1 - VY * X1;
373     this->Position(X,Y,Z);
374     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
375     VX = DYRef*DZini - DZRef*DYini;
376     VY = DZRef*DXini - DXRef*DZini;
377     VZ = DXRef*DYini - DYRef*DXini;
378     
379     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
380   }
381   
382   myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT);
383   myTypeOfRepresentation = Pres;
384   myGraphicStructure->Display();
385 }