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