0027860: Visualization - clean up Transformation Persistence API
[occt.git] / src / V3d / V3d_CircularGrid.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 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
973c2be1 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.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
59ec40f8 14#include <V3d_CircularGrid.hxx>
7fd59977 15
a577aaab 16#include <Graphic3d_ArrayOfPoints.hxx>
b8ddfc2f 17#include <Graphic3d_ArrayOfPolylines.hxx>
42cf5bc1 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>
b8ddfc2f 26#include <TColgp_SequenceOfPnt.hxx>
42cf5bc1 27#include <TColStd_Array2OfReal.hxx>
42cf5bc1 28#include <V3d_Viewer.hxx>
7fd59977 29
92efcf78 30IMPLEMENT_STANDARD_RTTIEXT(V3d_CircularGrid,Aspect_CircularGrid)
31
7fd59977 32/*----------------------------------------------------------------------*/
33/*
34 * Constant
35 */
7fd59977 36#define DIVISION 8
37#define MYMINMAX 25.
38#define MYFACTOR 50.
39
40/*----------------------------------------------------------------------*/
41
b8ddfc2f 42V3d_CircularGrid::V3d_CircularGrid (const V3d_ViewerPointer& aViewer, const Quantity_Color& aColor, const Quantity_Color& aTenthColor)
43: Aspect_CircularGrid (1.,8),
c357e426 44 myStructure (new Graphic3d_Structure (aViewer->StructureManager())),
b64d84be 45 myGroup (myStructure->NewGroup()),
b8ddfc2f 46 myViewer (aViewer),
47 myCurAreDefined (Standard_False)
7fd59977 48{
b8ddfc2f 49 myColor = aColor;
50 myTenthColor = aTenthColor;
7fd59977 51
b8ddfc2f 52 myStructure->SetInfiniteState (Standard_True);
7fd59977 53
b8ddfc2f 54 const Standard_Real step = 10.;
55 const Standard_Real size = 0.5*myViewer->DefaultViewSize();
56 SetGraphicValues (size, step/MYFACTOR);
57 SetRadiusStep (step);
7fd59977 58}
59
b8ddfc2f 60void 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 }
7fd59977 68}
69
b8ddfc2f 70void V3d_CircularGrid::Display ()
71{
a1954302 72 myStructure->SetDisplayPriority (1);
73 myStructure->Display();
7fd59977 74}
75
b8ddfc2f 76void V3d_CircularGrid::Erase () const
77{
78 myStructure->Erase ();
7fd59977 79}
80
b8ddfc2f 81Standard_Boolean V3d_CircularGrid::IsDisplayed () const
82{
83 return myStructure->IsDisplayed ();
7fd59977 84}
85
b8ddfc2f 86void 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 TColStd_Array2OfReal Trsf (1, 4, 1, 4);
126 Trsf (4, 4) = 1.0;
127 Trsf (4, 1) = Trsf (4, 2) = Trsf (4, 3) = 0.0;
128 // Translation
129 Trsf (1, 4) = xl,
130 Trsf (2, 4) = yl,
131 Trsf (3, 4) = zl;
132 // Transformation change of marker
133 Trsf (1, 1) = xdx,
134 Trsf (2, 1) = xdy,
135 Trsf (3, 1) = xdz,
136 Trsf (1, 2) = ydx,
137 Trsf (2, 2) = ydy,
138 Trsf (3, 2) = ydz,
139 Trsf (1, 3) = dx,
140 Trsf (2, 3) = dy,
141 Trsf (3, 3) = dz;
142 myStructure->SetTransform (Trsf, Graphic3d_TOC_REPLACE);
143
144 // Translation of the origin
145 Trsf (1, 4) = -XOrigin (),
146 Trsf (2, 4) = -YOrigin (),
147 Trsf (3, 4) = 0.0;
148 // Rotation Alpha around axis -Z
149 Trsf (1, 1) = CosAlpha,
150 Trsf (2, 1) = -SinAlpha,
151 Trsf (3, 1) = 0.0,
152 Trsf (1, 2) = SinAlpha,
153 Trsf (2, 2) = CosAlpha,
154 Trsf (3, 2) = 0.0,
155 Trsf (1, 3) = 0.0,
156 Trsf (2, 3) = 0.0,
157 Trsf (3, 3) = 1.0;
158 myStructure->SetTransform (Trsf,Graphic3d_TOC_POSTCONCATENATE);
159
160 myCurAngle = RotationAngle ();
161 myCurXo = XOrigin (), myCurYo = YOrigin ();
162 myCurViewPlane = ThePlane;
163 }
164
165 switch (DrawMode())
166 {
167 default:
168 //case Aspect_GDM_Points:
169 DefinePoints ();
170 myCurDrawMode = Aspect_GDM_Points;
171 break;
172 case Aspect_GDM_Lines:
173 DefineLines ();
174 myCurDrawMode = Aspect_GDM_Lines;
175 break;
7fd59977 176#ifdef IMP210100
b8ddfc2f 177 case Aspect_GDM_None:
178 myCurDrawMode = Aspect_GDM_None;
179 break;
7fd59977 180#endif
b8ddfc2f 181 }
182 myCurAreDefined = Standard_True;
7fd59977 183}
184
b8ddfc2f 185void V3d_CircularGrid::DefineLines ()
186{
187 const Standard_Real aStep = RadiusStep ();
188 const Standard_Real aDivision = DivisionNumber ();
189 const Standard_Boolean toUpdate = !myCurAreDefined
190 || myCurDrawMode != Aspect_GDM_Lines
191 || aDivision != myCurDivi
192 || aStep != myCurStep;
193 if (!toUpdate)
194 {
195 return;
196 }
197
198 myGroup->Clear ();
199
b8ddfc2f 200 const Standard_Integer Division = (Standard_Integer )( (aDivision >= DIVISION ? aDivision : DIVISION));
201
202 Standard_Integer nbpnts = 2 * Division;
203 // diametres
204 Standard_Real alpha = M_PI / aDivision;
b6472664 205
206 myGroup->SetGroupPrimitivesAspect (new Graphic3d_AspectLine3d (myTenthColor, Aspect_TOL_SOLID, 1.0));
51740958 207 Handle(Graphic3d_ArrayOfSegments) aPrims1 = new Graphic3d_ArrayOfSegments(2*nbpnts);
b8ddfc2f 208 const gp_Pnt p0(0., 0., -myOffSet);
209 for (Standard_Integer i=1; i<=nbpnts; i++) {
51740958 210 aPrims1->AddVertex(p0);
211 aPrims1->AddVertex(Cos(alpha*i)*myRadius, Sin(alpha*i)*myRadius, -myOffSet);
b8ddfc2f 212 }
51740958 213 myGroup->AddPrimitiveArray(aPrims1, Standard_False);
b8ddfc2f 214
215 // circles
216 nbpnts = 2 * Division + 1;
217 alpha = M_PI / Division;
218 Standard_Integer nblines = 0;
219 TColgp_SequenceOfPnt aSeqLines, aSeqTenth;
220 for (Standard_Real r=aStep; r<=myRadius; r+=aStep, nblines++) {
221 const Standard_Boolean isTenth = (Modulus(nblines, 10) == 0);
222 for (Standard_Integer i=0; i<nbpnts; i++) {
223 const gp_Pnt pt(Cos(alpha*i)*r,Sin(alpha*i)*r,-myOffSet);
224 (isTenth? aSeqTenth : aSeqLines).Append(pt);
225 }
226 }
227 if (aSeqTenth.Length())
228 {
b6472664 229 myGroup->SetGroupPrimitivesAspect (new Graphic3d_AspectLine3d (myTenthColor, Aspect_TOL_SOLID, 1.0));
b8ddfc2f 230 Standard_Integer n, np;
231 const Standard_Integer nbl = aSeqTenth.Length() / nbpnts;
51740958 232 Handle(Graphic3d_ArrayOfPolylines) aPrims2 = new Graphic3d_ArrayOfPolylines(aSeqTenth.Length(),nbl);
b8ddfc2f 233 for (np = 1, n=0; n<nbl; n++) {
51740958 234 aPrims2->AddBound(nbpnts);
b8ddfc2f 235 for (Standard_Integer i=0; i<nbpnts; i++, np++)
51740958 236 aPrims2->AddVertex(aSeqTenth(np));
b8ddfc2f 237 }
51740958 238 myGroup->AddPrimitiveArray(aPrims2, Standard_False);
b8ddfc2f 239 }
240 if (aSeqLines.Length())
241 {
b6472664 242 myGroup->SetPrimitivesAspect (new Graphic3d_AspectLine3d (myColor, Aspect_TOL_SOLID, 1.0));
b8ddfc2f 243 Standard_Integer n, np;
244 const Standard_Integer nbl = aSeqLines.Length() / nbpnts;
51740958 245 Handle(Graphic3d_ArrayOfPolylines) aPrims3 = new Graphic3d_ArrayOfPolylines(aSeqLines.Length(),nbl);
b8ddfc2f 246 for (np = 1, n=0; n<nbl; n++) {
51740958 247 aPrims3->AddBound(nbpnts);
b8ddfc2f 248 for (Standard_Integer i=0; i<nbpnts; i++, np++)
51740958 249 aPrims3->AddVertex(aSeqLines(np));
b8ddfc2f 250 }
51740958 251 myGroup->AddPrimitiveArray(aPrims3, Standard_False);
b8ddfc2f 252 }
253
254 myGroup->SetMinMaxValues(-myRadius, -myRadius, 0.0, myRadius, myRadius, 0.0);
255 myCurStep = aStep, myCurDivi = (Standard_Integer ) aDivision;
7fd59977 256}
257
b8ddfc2f 258void V3d_CircularGrid::DefinePoints ()
259{
260 const Standard_Real aStep = RadiusStep();
261 const Standard_Real aDivision = DivisionNumber();
262 const Standard_Boolean toUpdate = !myCurAreDefined
263 || myCurDrawMode != Aspect_GDM_Points
264 || aDivision != myCurDivi
265 || aStep != myCurStep;
266 if (!toUpdate)
267 {
268 return;
269 }
270
271 myGroup->Clear ();
272
273 Handle(Graphic3d_AspectMarker3d) MarkerAttrib = new Graphic3d_AspectMarker3d ();
274 MarkerAttrib->SetColor (myColor);
275 MarkerAttrib->SetType (Aspect_TOM_POINT);
276 MarkerAttrib->SetScale (3.);
277
278 const Standard_Integer nbpnts = Standard_Integer (2*aDivision);
279 Standard_Real r, alpha = M_PI / aDivision;
280
281 // diameters
282 TColgp_SequenceOfPnt aSeqPnts;
283 aSeqPnts.Append(gp_Pnt(0.0, 0.0, -myOffSet));
284 for (r=aStep; r<=myRadius; r+=aStep) {
285 for (Standard_Integer i=0; i<nbpnts; i++)
286 aSeqPnts.Append(gp_Pnt(Cos(alpha*i)*r, Sin(alpha*i)*r, -myOffSet));
287 }
288 myGroup->SetGroupPrimitivesAspect (MarkerAttrib);
289 if (aSeqPnts.Length())
290 {
291 Standard_Real X,Y,Z;
292 const Standard_Integer nbv = aSeqPnts.Length();
a577aaab 293 Handle(Graphic3d_ArrayOfPoints) Cercle = new Graphic3d_ArrayOfPoints (nbv);
b8ddfc2f 294 for (Standard_Integer i=1; i<=nbv; i++)
295 {
296 aSeqPnts(i).Coord(X,Y,Z);
a577aaab 297 Cercle->AddVertex (X,Y,Z);
b8ddfc2f 298 }
a577aaab 299 myGroup->AddPrimitiveArray (Cercle, Standard_False);
b8ddfc2f 300 }
301 myGroup->SetMinMaxValues(-myRadius, -myRadius, 0.0, myRadius, myRadius, 0.0);
302
303 myCurStep = aStep, myCurDivi = (Standard_Integer ) aDivision;
7fd59977 304}
305
b8ddfc2f 306void V3d_CircularGrid::GraphicValues (Standard_Real& theRadius, Standard_Real& theOffSet) const
307{
308 theRadius = myRadius;
309 theOffSet = myOffSet;
7fd59977 310}
311
b8ddfc2f 312void V3d_CircularGrid::SetGraphicValues (const Standard_Real theRadius, const Standard_Real theOffSet)
313{
314 if (! myCurAreDefined) {
315 myRadius = theRadius;
316 myOffSet = theOffSet;
317 }
318 if (myRadius != theRadius) {
319 myRadius = theRadius;
320 myCurAreDefined = Standard_False;
321 }
322 if (myOffSet != theOffSet) {
323 myOffSet = theOffSet;
324 myCurAreDefined = Standard_False;
325 }
326 if( !myCurAreDefined ) UpdateDisplay();
7fd59977 327}