0025180: Visualization - Homogeneous transformation API in TKV3d
[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 ());
1f7f5a90 125
126 gp_Trsf aTrsf;
b8ddfc2f 127 // Translation
1f7f5a90 128 // Transformation of change of marker
129 aTrsf.SetValues (xdx, ydx, dx, xl,
130 xdy, ydy, dy, yl,
131 xdz, ydz, dz, zl);
b8ddfc2f 132
133 // Translation of the origin
b8ddfc2f 134 // Rotation Alpha around axis -Z
1f7f5a90 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));
b8ddfc2f 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;
7fd59977 158#ifdef IMP210100
b8ddfc2f 159 case Aspect_GDM_None:
160 myCurDrawMode = Aspect_GDM_None;
161 break;
7fd59977 162#endif
b8ddfc2f 163 }
164 myCurAreDefined = Standard_True;
7fd59977 165}
166
b8ddfc2f 167void 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
b8ddfc2f 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;
b6472664 187
188 myGroup->SetGroupPrimitivesAspect (new Graphic3d_AspectLine3d (myTenthColor, Aspect_TOL_SOLID, 1.0));
51740958 189 Handle(Graphic3d_ArrayOfSegments) aPrims1 = new Graphic3d_ArrayOfSegments(2*nbpnts);
b8ddfc2f 190 const gp_Pnt p0(0., 0., -myOffSet);
191 for (Standard_Integer i=1; i<=nbpnts; i++) {
51740958 192 aPrims1->AddVertex(p0);
193 aPrims1->AddVertex(Cos(alpha*i)*myRadius, Sin(alpha*i)*myRadius, -myOffSet);
b8ddfc2f 194 }
51740958 195 myGroup->AddPrimitiveArray(aPrims1, Standard_False);
b8ddfc2f 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 {
b6472664 211 myGroup->SetGroupPrimitivesAspect (new Graphic3d_AspectLine3d (myTenthColor, Aspect_TOL_SOLID, 1.0));
b8ddfc2f 212 Standard_Integer n, np;
213 const Standard_Integer nbl = aSeqTenth.Length() / nbpnts;
51740958 214 Handle(Graphic3d_ArrayOfPolylines) aPrims2 = new Graphic3d_ArrayOfPolylines(aSeqTenth.Length(),nbl);
b8ddfc2f 215 for (np = 1, n=0; n<nbl; n++) {
51740958 216 aPrims2->AddBound(nbpnts);
b8ddfc2f 217 for (Standard_Integer i=0; i<nbpnts; i++, np++)
51740958 218 aPrims2->AddVertex(aSeqTenth(np));
b8ddfc2f 219 }
51740958 220 myGroup->AddPrimitiveArray(aPrims2, Standard_False);
b8ddfc2f 221 }
222 if (aSeqLines.Length())
223 {
b6472664 224 myGroup->SetPrimitivesAspect (new Graphic3d_AspectLine3d (myColor, Aspect_TOL_SOLID, 1.0));
b8ddfc2f 225 Standard_Integer n, np;
226 const Standard_Integer nbl = aSeqLines.Length() / nbpnts;
51740958 227 Handle(Graphic3d_ArrayOfPolylines) aPrims3 = new Graphic3d_ArrayOfPolylines(aSeqLines.Length(),nbl);
b8ddfc2f 228 for (np = 1, n=0; n<nbl; n++) {
51740958 229 aPrims3->AddBound(nbpnts);
b8ddfc2f 230 for (Standard_Integer i=0; i<nbpnts; i++, np++)
51740958 231 aPrims3->AddVertex(aSeqLines(np));
b8ddfc2f 232 }
51740958 233 myGroup->AddPrimitiveArray(aPrims3, Standard_False);
b8ddfc2f 234 }
235
236 myGroup->SetMinMaxValues(-myRadius, -myRadius, 0.0, myRadius, myRadius, 0.0);
237 myCurStep = aStep, myCurDivi = (Standard_Integer ) aDivision;
7fd59977 238}
239
b8ddfc2f 240void 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();
a577aaab 275 Handle(Graphic3d_ArrayOfPoints) Cercle = new Graphic3d_ArrayOfPoints (nbv);
b8ddfc2f 276 for (Standard_Integer i=1; i<=nbv; i++)
277 {
278 aSeqPnts(i).Coord(X,Y,Z);
a577aaab 279 Cercle->AddVertex (X,Y,Z);
b8ddfc2f 280 }
a577aaab 281 myGroup->AddPrimitiveArray (Cercle, Standard_False);
b8ddfc2f 282 }
283 myGroup->SetMinMaxValues(-myRadius, -myRadius, 0.0, myRadius, myRadius, 0.0);
284
285 myCurStep = aStep, myCurDivi = (Standard_Integer ) aDivision;
7fd59977 286}
287
b8ddfc2f 288void V3d_CircularGrid::GraphicValues (Standard_Real& theRadius, Standard_Real& theOffSet) const
289{
290 theRadius = myRadius;
291 theOffSet = myOffSet;
7fd59977 292}
293
b8ddfc2f 294void 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();
7fd59977 309}