1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <V3d_RectangularGrid.hxx>
16 #include <Graphic3d_ArrayOfPoints.hxx>
17 #include <Graphic3d_ArrayOfSegments.hxx>
18 #include <Graphic3d_AspectLine3d.hxx>
19 #include <Graphic3d_AspectMarker3d.hxx>
20 #include <Graphic3d_AspectText3d.hxx>
21 #include <Graphic3d_Group.hxx>
22 #include <Graphic3d_Structure.hxx>
23 #include <Quantity_Color.hxx>
24 #include <Standard_Type.hxx>
25 #include <TColgp_SequenceOfPnt.hxx>
26 #include <TColStd_Array2OfReal.hxx>
27 #include <V3d_Viewer.hxx>
29 IMPLEMENT_STANDARD_RTTIEXT(V3d_RectangularGrid,Aspect_RectangularGrid)
33 static const Standard_Real THE_DEFAULT_GRID_STEP = 10.0;
37 //! Dummy implementation of Graphic3d_Structure overriding ::Compute() method for handling Device Lost.
38 class V3d_RectangularGrid::RectangularGridStructure : public Graphic3d_Structure
42 RectangularGridStructure (const Handle(Graphic3d_StructureManager)& theManager, V3d_RectangularGrid* theGrid)
43 : Graphic3d_Structure (theManager), myGrid (theGrid) {}
45 //! Override method initiating recomputing in V3d_RectangularGrid.
46 virtual void Compute() Standard_OVERRIDE
48 GraphicClear (Standard_False);
49 myGrid->myGroup = NewGroup();
50 myGrid->myCurAreDefined = Standard_False;
51 myGrid->UpdateDisplay();
55 V3d_RectangularGrid* myGrid;
58 /*----------------------------------------------------------------------*/
60 V3d_RectangularGrid::V3d_RectangularGrid (const V3d_ViewerPointer& aViewer, const Quantity_Color& aColor, const Quantity_Color& aTenthColor)
61 : Aspect_RectangularGrid (1.,1.),
63 myCurAreDefined (Standard_False),
64 myToComputePrs (Standard_True),
65 myCurDrawMode (Aspect_GDM_Lines),
71 myXSize (0.5 * aViewer->DefaultViewSize()),
72 myYSize (0.5 * aViewer->DefaultViewSize()),
73 myOffSet (THE_DEFAULT_GRID_STEP / MYFACTOR)
76 myTenthColor = aTenthColor;
78 myStructure = new RectangularGridStructure (aViewer->StructureManager(), this);
79 myGroup = myStructure->NewGroup();
80 myStructure->SetInfiniteState (Standard_True);
82 SetXStep (THE_DEFAULT_GRID_STEP);
83 SetYStep (THE_DEFAULT_GRID_STEP);
86 V3d_RectangularGrid::~V3d_RectangularGrid()
89 if (!myStructure.IsNull())
95 void V3d_RectangularGrid::SetColors (const Quantity_Color& aColor, const Quantity_Color& aTenthColor)
97 if( myColor != aColor || myTenthColor != aTenthColor ) {
99 myTenthColor = aTenthColor;
100 myCurAreDefined = Standard_False;
105 void V3d_RectangularGrid::Display ()
107 myStructure->SetDisplayPriority (1);
108 myStructure->Display();
112 void V3d_RectangularGrid::Erase () const
114 myStructure->Erase ();
117 Standard_Boolean V3d_RectangularGrid::IsDisplayed () const
119 return myStructure->IsDisplayed ();
122 void V3d_RectangularGrid::UpdateDisplay ()
124 gp_Ax3 ThePlane = myViewer->PrivilegedPlane ();
126 Standard_Boolean MakeTransform = Standard_False;
127 Standard_Real xl, yl, zl;
128 Standard_Real xdx, xdy, xdz;
129 Standard_Real ydx, ydy, ydz;
130 Standard_Real dx, dy, dz;
131 ThePlane.Location ().Coord (xl, yl, zl);
132 ThePlane.XDirection ().Coord (xdx, xdy, xdz);
133 ThePlane.YDirection ().Coord (ydx, ydy, ydz);
134 ThePlane.Direction ().Coord (dx, dy, dz);
135 if (! myCurAreDefined)
136 MakeTransform = Standard_True;
138 if (RotationAngle() != myCurAngle || XOrigin() != myCurXo || YOrigin() != myCurYo)
139 MakeTransform = Standard_True;
140 if (! MakeTransform) {
141 Standard_Real curxl, curyl, curzl;
142 Standard_Real curxdx, curxdy, curxdz;
143 Standard_Real curydx, curydy, curydz;
144 Standard_Real curdx, curdy, curdz;
145 myCurViewPlane.Location ().Coord (curxl, curyl, curzl);
146 myCurViewPlane.XDirection ().Coord (curxdx, curxdy, curxdz);
147 myCurViewPlane.YDirection ().Coord (curydx, curydy, curydz);
148 myCurViewPlane.Direction ().Coord (curdx, curdy, curdz);
149 if (xl != curxl || yl != curyl || zl != curzl ||
150 xdx != curxdx || xdy != curxdy || xdz != curxdz ||
151 ydx != curydx || ydy != curydy || ydz != curydz ||
152 dx != curdx || dy != curdy || dz != curdz)
153 MakeTransform = Standard_True;
158 const Standard_Real CosAlpha = Cos (RotationAngle ());
159 const Standard_Real SinAlpha = Sin (RotationAngle ());
163 // Transformation of change of marker
164 aTrsf.SetValues (xdx, ydx, dx, xl,
168 // Translation of the origin
169 // Rotation Alpha around axis -Z
171 aTrsf2.SetValues ( CosAlpha, SinAlpha, 0.0, -XOrigin(),
172 -SinAlpha, CosAlpha, 0.0, -YOrigin(),
174 aTrsf.Multiply (aTrsf2);
175 myStructure->SetTransformation (new TopLoc_Datum3D (aTrsf));
177 myCurAngle = RotationAngle ();
178 myCurXo = XOrigin (), myCurYo = YOrigin ();
179 myCurViewPlane = ThePlane;
184 case Aspect_GDM_Points:
186 myCurDrawMode = Aspect_GDM_Points;
188 case Aspect_GDM_Lines:
190 myCurDrawMode = Aspect_GDM_Lines;
192 case Aspect_GDM_None:
193 myCurDrawMode = Aspect_GDM_None;
196 myCurAreDefined = Standard_True;
199 void V3d_RectangularGrid::DefineLines ()
201 const Standard_Real aXStep = XStep();
202 const Standard_Real aYStep = YStep();
203 const Standard_Boolean toUpdate = !myCurAreDefined
204 || myCurDrawMode != Aspect_GDM_Lines
205 || aXStep != myCurXStep
206 || aYStep != myCurYStep;
212 else if (!myStructure->IsDisplayed())
214 myToComputePrs = Standard_True;
218 myToComputePrs = Standard_False;
221 Standard_Integer nblines;
222 Standard_Real xl, yl, zl = myOffSet;
224 TColgp_SequenceOfPnt aSeqLines, aSeqTenth;
227 aSeqTenth.Append(gp_Pnt(0., -myYSize, -zl));
228 aSeqTenth.Append(gp_Pnt(0., myYSize, -zl));
229 for (nblines = 1, xl = aXStep; xl < myXSize; xl += aXStep, nblines++)
231 TColgp_SequenceOfPnt &aSeq = (Modulus(nblines, 10) != 0)? aSeqLines : aSeqTenth;
232 aSeq.Append(gp_Pnt( xl, -myYSize, -zl));
233 aSeq.Append(gp_Pnt( xl, myYSize, -zl));
234 aSeq.Append(gp_Pnt(-xl, -myYSize, -zl));
235 aSeq.Append(gp_Pnt(-xl, myYSize, -zl));
239 aSeqTenth.Append(gp_Pnt(-myXSize, 0., -zl));
240 aSeqTenth.Append(gp_Pnt( myXSize, 0., -zl));
241 for (nblines = 1, yl = aYStep; yl < myYSize; yl += aYStep, nblines++)
243 TColgp_SequenceOfPnt &aSeq = (Modulus(nblines, 10) != 0)? aSeqLines : aSeqTenth;
244 aSeq.Append(gp_Pnt(-myXSize, yl, -zl));
245 aSeq.Append(gp_Pnt( myXSize, yl, -zl));
246 aSeq.Append(gp_Pnt(-myXSize, -yl, -zl));
247 aSeq.Append(gp_Pnt( myXSize, -yl, -zl));
250 if (aSeqLines.Length())
252 Handle(Graphic3d_AspectLine3d) aLineAspect = new Graphic3d_AspectLine3d (myColor, Aspect_TOL_SOLID, 1.0);
253 myGroup->SetPrimitivesAspect (aLineAspect);
254 const Standard_Integer nbv = aSeqLines.Length();
255 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(nbv);
256 Standard_Integer n = 1;
258 aPrims->AddVertex(aSeqLines(n++));
259 myGroup->AddPrimitiveArray(aPrims, Standard_False);
261 if (aSeqTenth.Length())
263 Handle(Graphic3d_AspectLine3d) aLineAspect = new Graphic3d_AspectLine3d (myTenthColor, Aspect_TOL_SOLID, 1.0);
264 myGroup->SetPrimitivesAspect (aLineAspect);
265 const Standard_Integer nbv = aSeqTenth.Length();
266 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(nbv);
267 Standard_Integer n = 1;
269 aPrims->AddVertex(aSeqTenth(n++));
270 myGroup->AddPrimitiveArray(aPrims, Standard_False);
273 myGroup->SetMinMaxValues(-myXSize, -myYSize, -myOffSet, myXSize, myYSize, -myOffSet);
274 myCurXStep = aXStep, myCurYStep = aYStep;
276 // update bounding box
277 myStructure->CalculateBoundBox();
278 myViewer->StructureManager()->Update (myStructure->GetZLayer());
281 void V3d_RectangularGrid::DefinePoints ()
283 const Standard_Real aXStep = XStep();
284 const Standard_Real aYStep = YStep();
285 const Standard_Boolean toUpdate = !myCurAreDefined
286 || myCurDrawMode != Aspect_GDM_Points
287 || aXStep != myCurXStep
288 || aYStep != myCurYStep;
294 else if (!myStructure->IsDisplayed())
296 myToComputePrs = Standard_True;
300 myToComputePrs = Standard_False;
304 Standard_Real xl, yl;
305 TColgp_SequenceOfPnt aSeqPnts;
306 for (xl = 0.0; xl <= myXSize; xl += aXStep) {
307 aSeqPnts.Append(gp_Pnt( xl, 0.0, -myOffSet));
308 aSeqPnts.Append(gp_Pnt(-xl, 0.0, -myOffSet));
309 for (yl = aYStep; yl <= myYSize; yl += aYStep) {
310 aSeqPnts.Append(gp_Pnt( xl, yl, -myOffSet));
311 aSeqPnts.Append(gp_Pnt( xl, -yl, -myOffSet));
312 aSeqPnts.Append(gp_Pnt(-xl, yl, -myOffSet));
313 aSeqPnts.Append(gp_Pnt(-xl, -yl, -myOffSet));
316 if (aSeqPnts.Length())
320 const Standard_Integer nbv = aSeqPnts.Length();
321 Handle(Graphic3d_ArrayOfPoints) Vertical = new Graphic3d_ArrayOfPoints (nbv);
322 for (i=1; i<=nbv; i++)
324 aSeqPnts(i).Coord(X,Y,Z);
325 Vertical->AddVertex (X,Y,Z);
328 Handle(Graphic3d_AspectMarker3d) aMarkerAspect = new Graphic3d_AspectMarker3d (Aspect_TOM_POINT, myColor, 3.0);
329 myGroup->SetGroupPrimitivesAspect (aMarkerAspect);
330 myGroup->AddPrimitiveArray (Vertical, Standard_False);
333 myGroup->SetMinMaxValues(-myXSize, -myYSize, -myOffSet, myXSize, myYSize, -myOffSet);
334 myCurXStep = aXStep, myCurYStep = aYStep;
336 // update bounding box
337 myStructure->CalculateBoundBox();
338 myViewer->StructureManager()->Update (myStructure->GetZLayer());
341 void V3d_RectangularGrid::GraphicValues (Standard_Real& theXSize, Standard_Real& theYSize, Standard_Real& theOffSet) const
345 theOffSet = myOffSet;
348 void V3d_RectangularGrid::SetGraphicValues (const Standard_Real theXSize, const Standard_Real theYSize, const Standard_Real theOffSet)
350 if (! myCurAreDefined) {
353 myOffSet = theOffSet;
355 if (myXSize != theXSize) {
357 myCurAreDefined = Standard_False;
359 if (myYSize != theYSize) {
361 myCurAreDefined = Standard_False;
363 if (myOffSet != theOffSet) {
364 myOffSet = theOffSet;
365 myCurAreDefined = Standard_False;
367 if( !myCurAreDefined ) UpdateDisplay();