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