0024624: Lost word in license statement in source files
[occt.git] / src / V3d / V3d_CircularGrid.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  
16      FONCTION :
17      ----------
18         Classe V3d_CircularGrid :
19  
20      VERSION HISTORY  :
21      --------------------------------
22       IMP230300: GG The color fields and methods have moved in Aspect_Grid
23
24
25 ************************************************************************/
26
27 #define IMP200100       //GG
28 //                      -> Compute the case Aspect_GDM_None
29 //                      -> Initialize the grid size according to the
30 //                        viewer default size.
31
32 #define IMP200300       //GG
33 //                      -> Recompute the grid when any graphic parameter is
34 //                        modified.
35
36 #define IMP300300       //GG
37 //                      -> Draw circles with 2 colors (tenth and normal colors)
38
39 /*----------------------------------------------------------------------*/
40 /*
41  * Includes
42  */
43
44 #include <V3d_CircularGrid.ixx>
45
46 #include <TColStd_Array2OfReal.hxx>
47 #include <Graphic3d_AspectLine3d.hxx>
48 #include <Graphic3d_AspectMarker3d.hxx>
49 #include <Graphic3d_Vertex.hxx>
50 #include <Graphic3d_Array1OfVertex.hxx>
51 #include <Graphic3d_ArrayOfSegments.hxx>
52 #include <Graphic3d_ArrayOfPoints.hxx>
53 #include <Graphic3d_ArrayOfPolylines.hxx>
54 #include <Visual3d_ViewManager.hxx>
55 #include <V3d_Viewer.hxx>
56 #include <TColgp_SequenceOfPnt.hxx>
57
58 /*----------------------------------------------------------------------*/
59 /*
60  * Constant
61  */
62
63 #define DIVISION 8
64 #define MYMINMAX 25.
65 #define MYFACTOR 50.
66
67 /*----------------------------------------------------------------------*/
68
69 V3d_CircularGrid::V3d_CircularGrid (const V3d_ViewerPointer& aViewer, const Quantity_Color& aColor, const Quantity_Color& aTenthColor)
70 : Aspect_CircularGrid (1.,8),
71   myStructure (new Graphic3d_Structure (aViewer->Viewer ())),
72   myGroup (new Graphic3d_Group (myStructure)),
73   myViewer (aViewer),
74   myCurAreDefined (Standard_False)
75 {
76   myColor = aColor;
77   myTenthColor = aTenthColor;
78
79   myStructure->SetInfiniteState (Standard_True);
80
81   const Standard_Real step = 10.;
82   const Standard_Real size = 0.5*myViewer->DefaultViewSize();
83   SetGraphicValues (size, step/MYFACTOR);
84   SetRadiusStep (step);
85 }
86
87 void V3d_CircularGrid::SetColors (const Quantity_Color& aColor, const Quantity_Color& aTenthColor)
88 {
89   if( myColor != aColor || myTenthColor != aTenthColor ) {
90     myColor = aColor;
91     myTenthColor = aTenthColor;
92     myCurAreDefined = Standard_False;
93     UpdateDisplay();
94   }
95 }
96
97 void V3d_CircularGrid::Display ()
98 {
99   myStructure->Display (1);
100 }
101
102 void V3d_CircularGrid::Erase () const
103 {
104   myStructure->Erase ();
105 }
106
107 Standard_Boolean V3d_CircularGrid::IsDisplayed () const
108 {
109   return myStructure->IsDisplayed ();
110 }
111
112 void V3d_CircularGrid::UpdateDisplay ()
113 {
114   gp_Ax3 ThePlane = myViewer->PrivilegedPlane();
115
116   Standard_Real xl, yl, zl;
117   Standard_Real xdx, xdy, xdz;
118   Standard_Real ydx, ydy, ydz;
119   Standard_Real dx, dy, dz;
120   ThePlane.Location ().Coord (xl, yl, zl);
121   ThePlane.XDirection ().Coord (xdx, xdy, xdz);
122   ThePlane.YDirection ().Coord (ydx, ydy, ydz);
123   ThePlane.Direction ().Coord (dx, dy, dz);
124
125   Standard_Boolean MakeTransform = !myCurAreDefined;
126   if (!MakeTransform)
127   {
128     MakeTransform = (RotationAngle() != myCurAngle || XOrigin() != myCurXo || YOrigin() != myCurYo);
129     if (!MakeTransform)
130     {
131       Standard_Real curxl, curyl, curzl;
132       Standard_Real curxdx, curxdy, curxdz;
133       Standard_Real curydx, curydy, curydz;
134       Standard_Real curdx, curdy, curdz;
135       myCurViewPlane.Location ().Coord (curxl, curyl, curzl);
136       myCurViewPlane.XDirection ().Coord (curxdx, curxdy, curxdz);
137       myCurViewPlane.YDirection ().Coord (curydx, curydy, curydz);
138       myCurViewPlane.Direction ().Coord (curdx, curdy, curdz);
139       if (xl != curxl || yl != curyl || zl != curzl ||
140           xdx != curxdx || xdy != curxdy || xdz != curxdz ||
141           ydx != curydx || ydy != curydy || ydz != curydz ||
142           dx != curdx || dy != curdy || dz != curdz)
143         MakeTransform = Standard_True;
144     }
145   }
146
147   if (MakeTransform)
148   {
149     const Standard_Real CosAlpha = Cos (RotationAngle ());
150     const Standard_Real SinAlpha = Sin (RotationAngle ());
151     TColStd_Array2OfReal Trsf (1, 4, 1, 4);
152     Trsf (4, 4) = 1.0;
153     Trsf (4, 1) = Trsf (4, 2) = Trsf (4, 3) = 0.0;
154     // Translation
155     Trsf (1, 4) = xl,
156     Trsf (2, 4) = yl,
157     Trsf (3, 4) = zl;
158     // Transformation  change of marker
159     Trsf (1, 1) = xdx,
160     Trsf (2, 1) = xdy,
161     Trsf (3, 1) = xdz,
162     Trsf (1, 2) = ydx,
163     Trsf (2, 2) = ydy,
164     Trsf (3, 2) = ydz,
165     Trsf (1, 3) = dx,
166     Trsf (2, 3) = dy,
167     Trsf (3, 3) = dz;
168     myStructure->SetTransform (Trsf, Graphic3d_TOC_REPLACE);
169
170     // Translation of the origin
171     Trsf (1, 4) = -XOrigin (),
172     Trsf (2, 4) = -YOrigin (),
173     Trsf (3, 4) = 0.0;
174     // Rotation Alpha around axis -Z
175     Trsf (1, 1) = CosAlpha,
176     Trsf (2, 1) = -SinAlpha,
177     Trsf (3, 1) = 0.0,
178     Trsf (1, 2) = SinAlpha,
179     Trsf (2, 2) = CosAlpha,
180     Trsf (3, 2) = 0.0,
181     Trsf (1, 3) = 0.0,
182     Trsf (2, 3) = 0.0,
183     Trsf (3, 3) = 1.0;
184     myStructure->SetTransform (Trsf,Graphic3d_TOC_POSTCONCATENATE);
185
186     myCurAngle = RotationAngle ();
187     myCurXo = XOrigin (), myCurYo = YOrigin ();
188     myCurViewPlane = ThePlane;
189   }
190
191   switch (DrawMode())
192   {
193     default:
194     //case Aspect_GDM_Points:
195       DefinePoints ();
196       myCurDrawMode = Aspect_GDM_Points;
197       break;
198     case Aspect_GDM_Lines:
199       DefineLines ();
200       myCurDrawMode = Aspect_GDM_Lines;
201       break;
202 #ifdef IMP210100
203     case Aspect_GDM_None:
204       myCurDrawMode = Aspect_GDM_None;
205       break;
206 #endif
207   }
208   myCurAreDefined = Standard_True;
209 }
210
211 void V3d_CircularGrid::DefineLines ()
212 {
213   const Standard_Real    aStep     = RadiusStep ();
214   const Standard_Real    aDivision = DivisionNumber ();
215   const Standard_Boolean toUpdate  = !myCurAreDefined
216                                   || myCurDrawMode != Aspect_GDM_Lines
217                                   || aDivision != myCurDivi
218                                   || aStep     != myCurStep;
219   if (!toUpdate)
220   {
221     return;
222   }
223
224   myGroup->Clear ();
225
226   Handle(Graphic3d_AspectLine3d) LineAttrib = new Graphic3d_AspectLine3d ();
227   LineAttrib->SetColor (myColor);
228   LineAttrib->SetType (Aspect_TOL_SOLID);
229   LineAttrib->SetWidth (1.0);
230
231   const Standard_Integer Division = (Standard_Integer )( (aDivision >= DIVISION ? aDivision : DIVISION));
232
233   Standard_Integer nbpnts = 2 * Division;
234   // diametres
235   Standard_Real alpha = M_PI / aDivision;
236   LineAttrib->SetColor (myTenthColor);
237   myGroup->SetGroupPrimitivesAspect (LineAttrib);
238   Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2*nbpnts);
239   const gp_Pnt p0(0., 0., -myOffSet);
240   for (Standard_Integer i=1; i<=nbpnts; i++) {
241     aPrims->AddVertex(p0);
242     aPrims->AddVertex(Cos(alpha*i)*myRadius, Sin(alpha*i)*myRadius, -myOffSet);
243   }
244   myGroup->AddPrimitiveArray(aPrims, Standard_False);
245
246   // circles
247   nbpnts = 2 * Division + 1;
248   alpha = M_PI / Division;
249   Standard_Integer nblines = 0;
250   TColgp_SequenceOfPnt aSeqLines, aSeqTenth;
251   for (Standard_Real r=aStep; r<=myRadius; r+=aStep, nblines++) {
252     const Standard_Boolean isTenth = (Modulus(nblines, 10) == 0);
253     for (Standard_Integer i=0; i<nbpnts; i++) {
254       const gp_Pnt pt(Cos(alpha*i)*r,Sin(alpha*i)*r,-myOffSet);
255       (isTenth? aSeqTenth : aSeqLines).Append(pt);
256     }
257   }
258   if (aSeqTenth.Length())
259   {
260     LineAttrib->SetColor (myTenthColor);
261     myGroup->SetGroupPrimitivesAspect (LineAttrib);
262     Standard_Integer n, np;
263     const Standard_Integer nbl = aSeqTenth.Length() / nbpnts;
264     Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(aSeqTenth.Length(),nbl);
265     for (np = 1, n=0; n<nbl; n++) {
266       aPrims->AddBound(nbpnts);
267       for (Standard_Integer i=0; i<nbpnts; i++, np++)
268         aPrims->AddVertex(aSeqTenth(np));
269   }
270     myGroup->AddPrimitiveArray(aPrims, Standard_False);
271   }
272   if (aSeqLines.Length())
273   {
274     LineAttrib->SetColor (myColor);
275     myGroup->SetPrimitivesAspect (LineAttrib);
276     Standard_Integer n, np;
277     const Standard_Integer nbl = aSeqLines.Length() / nbpnts;
278     Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(aSeqLines.Length(),nbl);
279     for (np = 1, n=0; n<nbl; n++) {
280       aPrims->AddBound(nbpnts);
281       for (Standard_Integer i=0; i<nbpnts; i++, np++)
282         aPrims->AddVertex(aSeqLines(np));
283   }
284     myGroup->AddPrimitiveArray(aPrims, Standard_False);
285   }
286
287   myGroup->SetMinMaxValues(-myRadius, -myRadius, 0.0, myRadius, myRadius, 0.0);
288   myCurStep = aStep, myCurDivi = (Standard_Integer ) aDivision;
289 }
290
291 void V3d_CircularGrid::DefinePoints ()
292 {
293   const Standard_Real    aStep     = RadiusStep();
294   const Standard_Real    aDivision = DivisionNumber();
295   const Standard_Boolean toUpdate  = !myCurAreDefined
296                                   || myCurDrawMode != Aspect_GDM_Points
297                                   || aDivision != myCurDivi
298                                   || aStep     != myCurStep;
299   if (!toUpdate)
300   {
301     return;
302   }
303
304   myGroup->Clear ();
305
306   Handle(Graphic3d_AspectMarker3d) MarkerAttrib = new Graphic3d_AspectMarker3d ();
307   MarkerAttrib->SetColor (myColor);
308   MarkerAttrib->SetType (Aspect_TOM_POINT);
309   MarkerAttrib->SetScale (3.);
310
311   const Standard_Integer nbpnts = Standard_Integer (2*aDivision);
312   Standard_Real r, alpha = M_PI / aDivision;
313
314   // diameters
315   TColgp_SequenceOfPnt aSeqPnts;
316   aSeqPnts.Append(gp_Pnt(0.0, 0.0, -myOffSet));
317   for (r=aStep; r<=myRadius; r+=aStep) {
318     for (Standard_Integer i=0; i<nbpnts; i++)
319       aSeqPnts.Append(gp_Pnt(Cos(alpha*i)*r, Sin(alpha*i)*r, -myOffSet));
320   }
321   myGroup->SetGroupPrimitivesAspect (MarkerAttrib);
322   if (aSeqPnts.Length())
323   {
324     Standard_Real X,Y,Z;
325     const Standard_Integer nbv = aSeqPnts.Length();
326     Handle(Graphic3d_ArrayOfPoints) Cercle = new Graphic3d_ArrayOfPoints (nbv);
327     for (Standard_Integer i=1; i<=nbv; i++)
328     {
329       aSeqPnts(i).Coord(X,Y,Z);
330       Cercle->AddVertex (X,Y,Z);
331     }
332     myGroup->AddPrimitiveArray (Cercle, Standard_False);
333   }
334   myGroup->SetMinMaxValues(-myRadius, -myRadius, 0.0, myRadius, myRadius, 0.0);
335
336   myCurStep = aStep, myCurDivi = (Standard_Integer ) aDivision;
337 }
338
339 void V3d_CircularGrid::GraphicValues (Standard_Real& theRadius, Standard_Real& theOffSet) const
340 {
341   theRadius = myRadius;
342   theOffSet = myOffSet;
343 }
344
345 void V3d_CircularGrid::SetGraphicValues (const Standard_Real theRadius, const Standard_Real theOffSet)
346 {
347   if (! myCurAreDefined) {
348     myRadius = theRadius;
349     myOffSet = theOffSet;
350   }
351   if (myRadius != theRadius) {
352     myRadius = theRadius;
353     myCurAreDefined = Standard_False;
354   }
355   if (myOffSet != theOffSet) {
356     myOffSet = theOffSet;
357     myCurAreDefined = Standard_False;
358   }
359   if( !myCurAreDefined ) UpdateDisplay();
360 }