0025765: Coding rules - clean up code from obsolete macro checks
[occt.git] / src / V3d / V3d_RectangularGrid.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      HISTORIQUE DES MODIFICATIONS   :
21      --------------------------------
22       16-08-98 : CAL ; S3892. Ajout grilles 3d.
23       13-10-98 : CAL ; S3892. Ajout de la gestion de la taille des grilles 3d.
24       23-10-98 : CAL ; PRO 15885. Patch K4403 et K4404
25       03-11-98 : CAL ; PRO 16161. Patch K4418 et K4419
26
27 ************************************************************************/
28
29 /*----------------------------------------------------------------------*/
30 /*
31  * Includes
32  */
33
34 #include <V3d_RectangularGrid.ixx>
35
36 #include <TColStd_Array2OfReal.hxx>
37 #include <Graphic3d_AspectLine3d.hxx>
38 #include <Graphic3d_AspectMarker3d.hxx>
39 #include <Graphic3d_AspectText3d.hxx>
40 #include <Graphic3d_ArrayOfPoints.hxx>
41 #include <Visual3d_ViewManager.hxx>
42 #include <V3d_Viewer.hxx>
43 #include <TColgp_SequenceOfPnt.hxx>
44 #include <Graphic3d_ArrayOfSegments.hxx>
45
46 /*----------------------------------------------------------------------*/
47 /*
48  * Constant
49  */
50
51 #define MYMINMAX 25.
52 #define MYFACTOR 50.
53
54 /*----------------------------------------------------------------------*/
55
56 V3d_RectangularGrid::V3d_RectangularGrid (const V3d_ViewerPointer& aViewer, const Quantity_Color& aColor, const Quantity_Color& aTenthColor)
57 : Aspect_RectangularGrid (1.,1.),
58   myStructure (new Graphic3d_Structure (aViewer->Viewer ())),
59   myGroup (myStructure->NewGroup()),
60   myViewer (aViewer),
61   myCurAreDefined (Standard_False)
62 {
63   myColor = aColor;
64   myTenthColor = aTenthColor;
65
66   myStructure->SetInfiniteState (Standard_True);
67
68   const Standard_Real step = 10.;
69   const Standard_Real gstep = step/MYFACTOR;
70   const Standard_Real size = 0.5*myViewer->DefaultViewSize();
71   SetGraphicValues (size, size, gstep);
72   SetXStep (step);
73   SetYStep (step);
74 }
75
76 void V3d_RectangularGrid::SetColors (const Quantity_Color& aColor, const Quantity_Color& aTenthColor)
77 {
78   if( myColor != aColor || myTenthColor != aTenthColor ) {
79     myColor = aColor;
80     myTenthColor = aTenthColor;
81     myCurAreDefined = Standard_False;
82     UpdateDisplay();
83   }
84 }
85
86 void V3d_RectangularGrid::Display ()
87 {
88   myStructure->SetDisplayPriority (1);
89   myStructure->Display();
90 }
91
92 void V3d_RectangularGrid::Erase () const
93 {
94   myStructure->Erase ();
95 }
96
97 Standard_Boolean V3d_RectangularGrid::IsDisplayed () const
98 {
99   return myStructure->IsDisplayed ();
100 }
101
102 void V3d_RectangularGrid::UpdateDisplay ()
103 {
104   gp_Ax3 ThePlane = myViewer->PrivilegedPlane ();
105
106   Standard_Boolean MakeTransform = Standard_False;
107   Standard_Real xl, yl, zl;
108   Standard_Real xdx, xdy, xdz;
109   Standard_Real ydx, ydy, ydz;
110   Standard_Real dx, dy, dz;
111   ThePlane.Location ().Coord (xl, yl, zl);
112   ThePlane.XDirection ().Coord (xdx, xdy, xdz);
113   ThePlane.YDirection ().Coord (ydx, ydy, ydz);
114   ThePlane.Direction ().Coord (dx, dy, dz);
115   if (! myCurAreDefined)
116     MakeTransform = Standard_True;
117   else {
118     if (RotationAngle() != myCurAngle || XOrigin() != myCurXo || YOrigin() != myCurYo)
119       MakeTransform = Standard_True;
120     if (! MakeTransform) {
121       Standard_Real curxl, curyl, curzl;
122       Standard_Real curxdx, curxdy, curxdz;
123       Standard_Real curydx, curydy, curydz;
124       Standard_Real curdx, curdy, curdz;
125       myCurViewPlane.Location ().Coord (curxl, curyl, curzl);
126       myCurViewPlane.XDirection ().Coord (curxdx, curxdy, curxdz);
127       myCurViewPlane.YDirection ().Coord (curydx, curydy, curydz);
128       myCurViewPlane.Direction ().Coord (curdx, curdy, curdz);
129       if (xl != curxl || yl != curyl || zl != curzl ||
130           xdx != curxdx || xdy != curxdy || xdz != curxdz ||
131           ydx != curydx || ydy != curydy || ydz != curydz ||
132           dx != curdx || dy != curdy || dz != curdz)
133         MakeTransform = Standard_True;
134     }
135   }
136
137   if (MakeTransform) {
138     const Standard_Real CosAlpha = Cos (RotationAngle ());
139     const Standard_Real SinAlpha = Sin (RotationAngle ());
140     TColStd_Array2OfReal Trsf (1, 4, 1, 4);
141     Trsf (4, 4) = 1.0;
142     Trsf (4, 1) = Trsf (4, 2) = Trsf (4, 3) = 0.0;
143     // Translation
144     Trsf (1, 4) = xl,
145     Trsf (2, 4) = yl,
146     Trsf (3, 4) = zl;
147     // Transformation of change of marker
148     Trsf (1, 1) = xdx,
149     Trsf (2, 1) = xdy,
150     Trsf (3, 1) = xdz,
151     Trsf (1, 2) = ydx,
152     Trsf (2, 2) = ydy,
153     Trsf (3, 2) = ydz,
154     Trsf (1, 3) = dx,
155     Trsf (2, 3) = dy,
156     Trsf (3, 3) = dz;
157     myStructure->SetTransform (Trsf, Graphic3d_TOC_REPLACE);
158
159     // Translation of the origin
160     Trsf (1, 4) = -XOrigin (),
161     Trsf (2, 4) = -YOrigin (),
162     Trsf (3, 4) = 0.0;
163     // Rotation Alpha around axis -Z
164     Trsf (1, 1) = CosAlpha,
165     Trsf (2, 1) = -SinAlpha,
166     Trsf (3, 1) = 0.0,
167     Trsf (1, 2) = SinAlpha,
168     Trsf (2, 2) = CosAlpha,
169     Trsf (3, 2) = 0.0,
170     Trsf (1, 3) = 0.0,
171     Trsf (2, 3) = 0.0,
172     Trsf (3, 3) = 1.0;
173     myStructure->SetTransform (Trsf,Graphic3d_TOC_POSTCONCATENATE);
174
175     myCurAngle = RotationAngle ();
176     myCurXo = XOrigin (), myCurYo = YOrigin ();
177     myCurViewPlane = ThePlane;
178   }
179
180   switch (DrawMode ())
181   {
182     default:
183     //case Aspect_GDM_Points:
184       DefinePoints ();
185       myCurDrawMode = Aspect_GDM_Points;
186       break;
187     case Aspect_GDM_Lines:
188       DefineLines ();
189       myCurDrawMode = Aspect_GDM_Lines;
190       break;
191 #ifdef IMP210100
192     case Aspect_GDM_None:
193       myCurDrawMode = Aspect_GDM_None;
194       break;
195 #endif
196         }
197         myCurAreDefined = Standard_True;
198 }
199
200 void V3d_RectangularGrid::DefineLines ()
201 {
202   const Standard_Real aXStep = XStep();
203   const Standard_Real aYStep = YStep();
204   const Standard_Boolean toUpdate = !myCurAreDefined
205                                  || myCurDrawMode != Aspect_GDM_Lines
206                                  || aXStep != myCurXStep
207                                  || aYStep != myCurYStep;
208   if (!toUpdate)
209   {
210     return;
211   }
212
213   myGroup->Clear();
214
215   Handle(Graphic3d_AspectLine3d) LineAttrib = new Graphic3d_AspectLine3d ();
216   LineAttrib->SetColor (myColor);
217   LineAttrib->SetType (Aspect_TOL_SOLID);
218   LineAttrib->SetWidth (1.0);
219
220   Standard_Integer nblines;
221   Standard_Real xl, yl, zl = myOffSet;
222
223   TColgp_SequenceOfPnt aSeqLines, aSeqTenth;
224
225   // verticals
226   aSeqTenth.Append(gp_Pnt(0., -myYSize, -zl));
227   aSeqTenth.Append(gp_Pnt(0.,  myYSize, -zl));
228   for (nblines = 1, xl = aXStep; xl < myXSize; xl += aXStep, nblines++)
229   {
230     TColgp_SequenceOfPnt &aSeq = (Modulus(nblines, 10) != 0)? aSeqLines : aSeqTenth;
231     aSeq.Append(gp_Pnt( xl, -myYSize, -zl));
232     aSeq.Append(gp_Pnt( xl,  myYSize, -zl));
233     aSeq.Append(gp_Pnt(-xl, -myYSize, -zl));
234     aSeq.Append(gp_Pnt(-xl,  myYSize, -zl));
235   }
236
237   // horizontals
238   aSeqTenth.Append(gp_Pnt(-myXSize, 0., -zl));
239   aSeqTenth.Append(gp_Pnt( myXSize, 0., -zl));
240   for (nblines = 1, yl = aYStep; yl < myYSize; yl += aYStep, nblines++)
241   {
242     TColgp_SequenceOfPnt &aSeq = (Modulus(nblines, 10) != 0)? aSeqLines : aSeqTenth;
243     aSeq.Append(gp_Pnt(-myXSize,  yl, -zl));
244     aSeq.Append(gp_Pnt( myXSize,  yl, -zl));
245     aSeq.Append(gp_Pnt(-myXSize, -yl, -zl));
246     aSeq.Append(gp_Pnt( myXSize, -yl, -zl));
247   }
248
249   if (aSeqLines.Length())
250   {
251     LineAttrib->SetColor (myColor);
252     myGroup->SetPrimitivesAspect (LineAttrib);
253     const Standard_Integer nbv = aSeqLines.Length();
254     Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(nbv);
255     Standard_Integer n = 1;
256     while (n<=nbv)
257       aPrims->AddVertex(aSeqLines(n++));
258     myGroup->AddPrimitiveArray(aPrims, Standard_False);
259   }
260   if (aSeqTenth.Length())
261   {
262     LineAttrib->SetColor (myTenthColor);
263     myGroup->SetPrimitivesAspect (LineAttrib);
264     const Standard_Integer nbv = aSeqTenth.Length();
265     Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(nbv);
266     Standard_Integer n = 1;
267     while (n<=nbv)
268       aPrims->AddVertex(aSeqTenth(n++));
269     myGroup->AddPrimitiveArray(aPrims, Standard_False);
270   }
271
272   myGroup->SetMinMaxValues(-myXSize, -myYSize, 0.0, myXSize, myYSize, 0.0);
273   myCurXStep = aXStep, myCurYStep = aYStep;
274 }
275
276 void V3d_RectangularGrid::DefinePoints ()
277 {
278   const Standard_Real aXStep = XStep();
279   const Standard_Real aYStep = YStep();
280   const Standard_Boolean toUpdate = !myCurAreDefined
281                                   || myCurDrawMode != Aspect_GDM_Points
282                                   || aXStep != myCurXStep
283                                   || aYStep != myCurYStep;
284   if (!toUpdate)
285   {
286     return;
287   }
288
289   myGroup->Clear ();
290
291   Handle(Graphic3d_AspectMarker3d) MarkerAttrib = new Graphic3d_AspectMarker3d ();
292   MarkerAttrib->SetColor (myColor);
293   MarkerAttrib->SetType (Aspect_TOM_POINT);
294   MarkerAttrib->SetScale (3.);
295
296   // horizontals
297   Standard_Real xl, yl;
298   TColgp_SequenceOfPnt aSeqPnts;
299   for (xl = 0.0; xl <= myXSize; xl += aXStep) {
300     aSeqPnts.Append(gp_Pnt( xl, 0.0, -myOffSet));
301     aSeqPnts.Append(gp_Pnt(-xl, 0.0, -myOffSet));
302     for (yl = aYStep; yl <= myYSize; yl += aYStep) {
303       aSeqPnts.Append(gp_Pnt( xl,  yl, -myOffSet));
304       aSeqPnts.Append(gp_Pnt( xl, -yl, -myOffSet));
305       aSeqPnts.Append(gp_Pnt(-xl,  yl, -myOffSet));
306       aSeqPnts.Append(gp_Pnt(-xl, -yl, -myOffSet));
307     }
308   }
309   if (aSeqPnts.Length())
310   {
311     Standard_Integer i;
312     Standard_Real X,Y,Z;
313     const Standard_Integer nbv = aSeqPnts.Length();
314     Handle(Graphic3d_ArrayOfPoints) Vertical = new Graphic3d_ArrayOfPoints (nbv);
315     for (i=1; i<=nbv; i++)
316     {
317       aSeqPnts(i).Coord(X,Y,Z);
318       Vertical->AddVertex (X,Y,Z);
319     }
320     myGroup->SetGroupPrimitivesAspect (MarkerAttrib);
321     myGroup->AddPrimitiveArray (Vertical, Standard_False);
322   }
323
324   myGroup->SetMinMaxValues(-myXSize, -myYSize, 0.0, myXSize, myYSize, 0.0);
325   myCurXStep = aXStep, myCurYStep = aYStep;
326 }
327
328 void V3d_RectangularGrid::GraphicValues (Standard_Real& theXSize, Standard_Real& theYSize, Standard_Real& theOffSet) const
329 {
330   theXSize = myXSize;
331   theYSize = myYSize;
332   theOffSet = myOffSet;
333 }
334
335 void V3d_RectangularGrid::SetGraphicValues (const Standard_Real theXSize, const Standard_Real theYSize, const Standard_Real theOffSet)
336 {
337   if (! myCurAreDefined) {
338     myXSize = theXSize;
339     myYSize = theYSize;
340     myOffSet = theOffSet;
341   }
342   if (myXSize != theXSize) {
343     myXSize = theXSize;
344     myCurAreDefined = Standard_False;
345   }
346   if (myYSize != theYSize) {
347     myYSize = theYSize;
348     myCurAreDefined = Standard_False;
349   }
350   if (myOffSet != theOffSet) {
351     myOffSet = theOffSet;
352     myCurAreDefined = Standard_False;
353   }
354   if( !myCurAreDefined ) UpdateDisplay();
355 }