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