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