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