0029395: Visualization, V3d_View - Grid disappears forever after enabling RayTracing
[occt.git] / src / V3d / V3d_Viewer_4.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <Aspect_Background.hxx>
16 #include <Aspect_GradientBackground.hxx>
17 #include <Aspect_Grid.hxx>
18 #include <gp_Ax3.hxx>
19 #include <Graphic3d_ArrayOfPoints.hxx>
20 #include <Graphic3d_AspectMarker3d.hxx>
21 #include <Graphic3d_GraphicDriver.hxx>
22 #include <Graphic3d_Group.hxx>
23 #include <Graphic3d_Structure.hxx>
24 #include <Quantity_Color.hxx>
25 #include <V3d_BadValue.hxx>
26 #include <V3d_CircularGrid.hxx>
27 #include <V3d_Light.hxx>
28 #include <V3d_RectangularGrid.hxx>
29 #include <V3d_View.hxx>
30 #include <V3d_Viewer.hxx>
31
32 // =======================================================================
33 // function : Grid
34 // purpose  :
35 // =======================================================================
36 Handle(Aspect_Grid) V3d_Viewer::Grid() const
37 {
38   switch (myGridType)
39   {
40   case Aspect_GT_Circular:    return Handle(Aspect_Grid) (myCGrid);
41   case Aspect_GT_Rectangular: return Handle(Aspect_Grid) (myRGrid);
42   }
43   return Handle(Aspect_Grid) (myRGrid);
44 }
45
46 // =======================================================================
47 // function : GridDrawMode
48 // purpose  :
49 // =======================================================================
50 Aspect_GridDrawMode V3d_Viewer::GridDrawMode() const
51 {
52   return Grid()->DrawMode();
53 }
54
55 // =======================================================================
56 // function : ActivateGrid
57 // purpose  :
58 // =======================================================================
59 void V3d_Viewer::ActivateGrid (const Aspect_GridType     theType,
60                                const Aspect_GridDrawMode theMode)
61 {
62   Grid()->Erase();
63   myGridType = theType;
64   Grid()->SetDrawMode (theMode);
65   if (theMode != Aspect_GDM_None)
66   {
67     Grid()->Display();
68   }
69   Grid()->Activate();
70   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
71   {
72     anActiveViewIter.Value()->SetGrid (myPrivilegedPlane, Grid());
73   }
74 }
75
76 // =======================================================================
77 // function : DeactivateGrid
78 // purpose  :
79 // =======================================================================
80 void V3d_Viewer::DeactivateGrid()
81 {
82   Grid()->Erase();
83   myGridType = Aspect_GT_Rectangular;
84   Grid()->Deactivate();
85   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
86   {
87     anActiveViewIter.Value()->SetGridActivity (Standard_False);
88     if (myGridEcho
89     && !myGridEchoStructure.IsNull())
90     {
91       myGridEchoStructure->Erase();
92     }
93   }
94 }
95
96 // =======================================================================
97 // function : IsActive
98 // purpose  :
99 // =======================================================================
100 Standard_Boolean V3d_Viewer::IsActive() const
101 {
102   return Grid()->IsActive();
103 }
104
105 // =======================================================================
106 // function : RectangularGridValues
107 // purpose  :
108 // =======================================================================
109 void V3d_Viewer::RectangularGridValues (Standard_Real& theXOrigin,
110                                         Standard_Real& theYOrigin,
111                                         Standard_Real& theXStep,
112                                         Standard_Real& theYStep,
113                                         Standard_Real& theRotationAngle) const
114 {
115   theXOrigin       = myRGrid->XOrigin();
116   theYOrigin       = myRGrid->YOrigin();
117   theXStep         = myRGrid->XStep();
118   theYStep         = myRGrid->YStep();
119   theRotationAngle = myRGrid->RotationAngle();
120 }
121
122 // =======================================================================
123 // function : SetRectangularGridValues
124 // purpose  :
125 // =======================================================================
126 void V3d_Viewer::SetRectangularGridValues (const Standard_Real theXOrigin,
127                                            const Standard_Real theYOrigin,
128                                            const Standard_Real theXStep,
129                                            const Standard_Real theYStep,
130                                            const Standard_Real theRotationAngle)
131 {
132   myRGrid->SetGridValues (theXOrigin, theYOrigin, theXStep, theYStep, theRotationAngle);
133   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
134   {
135     anActiveViewIter.Value()->SetGrid (myPrivilegedPlane, myRGrid);
136   }
137 }
138
139 // =======================================================================
140 // function : CircularGridValues
141 // purpose  :
142 // =======================================================================
143 void V3d_Viewer::CircularGridValues (Standard_Real& theXOrigin,
144                                      Standard_Real& theYOrigin,
145                                      Standard_Real& theRadiusStep,
146                                      Standard_Integer& theDivisionNumber,
147                                      Standard_Real& theRotationAngle) const
148 {
149   theXOrigin        = myCGrid->XOrigin();
150   theYOrigin        = myCGrid->YOrigin();
151   theRadiusStep     = myCGrid->RadiusStep();
152   theDivisionNumber = myCGrid->DivisionNumber();
153   theRotationAngle  = myCGrid->RotationAngle();
154 }
155
156 // =======================================================================
157 // function : SetCircularGridValues
158 // purpose  :
159 // =======================================================================
160 void V3d_Viewer::SetCircularGridValues (const Standard_Real theXOrigin,
161                                         const Standard_Real theYOrigin,
162                                         const Standard_Real theRadiusStep,
163                                         const Standard_Integer theDivisionNumber,
164                                         const Standard_Real theRotationAngle)
165 {
166   myCGrid->SetGridValues (theXOrigin, theYOrigin, theRadiusStep,
167                           theDivisionNumber, theRotationAngle);
168   for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
169   {
170     anActiveViewIter.Value()->SetGrid (myPrivilegedPlane, myCGrid);
171   }
172 }
173
174 // =======================================================================
175 // function : RectangularGridGraphicValues
176 // purpose  :
177 // =======================================================================
178 void V3d_Viewer::RectangularGridGraphicValues (Standard_Real& theXSize,
179                                                Standard_Real& theYSize,
180                                                Standard_Real& theOffSet) const
181 {
182   myRGrid->GraphicValues (theXSize, theYSize, theOffSet);
183 }
184
185 // =======================================================================
186 // function : SetRectangularGridGraphicValues
187 // purpose  :
188 // =======================================================================
189 void V3d_Viewer::SetRectangularGridGraphicValues (const Standard_Real theXSize,
190                                                   const Standard_Real theYSize,
191                                                   const Standard_Real theOffSet)
192 {
193   myRGrid->SetGraphicValues (theXSize, theYSize, theOffSet);
194 }
195
196 // =======================================================================
197 // function : CircularGridGraphicValues
198 // purpose  :
199 // =======================================================================
200 void V3d_Viewer::CircularGridGraphicValues (Standard_Real& theRadius,
201                                             Standard_Real& theOffSet) const
202 {
203   myCGrid->GraphicValues (theRadius, theOffSet);
204 }
205
206 // =======================================================================
207 // function : SetCircularGridGraphicValues
208 // purpose  :
209 // =======================================================================
210 void V3d_Viewer::SetCircularGridGraphicValues (const Standard_Real theRadius,
211                                                const Standard_Real theOffSet)
212 {
213   myCGrid->SetGraphicValues (theRadius, theOffSet);
214 }
215
216 // =======================================================================
217 // function : SetGridEcho
218 // purpose  :
219 // =======================================================================
220 void V3d_Viewer::SetGridEcho (const Standard_Boolean theToShowGrid)
221 {
222   if (myGridEcho == theToShowGrid)
223   {
224     return;
225   }
226
227   myGridEcho = theToShowGrid;
228   if (theToShowGrid
229    || myGridEchoStructure.IsNull())
230   {
231     return;
232   }
233
234   myGridEchoStructure->Erase();
235 }
236
237 // =======================================================================
238 // function : SetGridEcho
239 // purpose  :
240 // =======================================================================
241 void V3d_Viewer::SetGridEcho (const Handle(Graphic3d_AspectMarker3d)& theMarker)
242 {
243   if (myGridEchoStructure.IsNull())
244   {
245     myGridEchoStructure = new Graphic3d_Structure (StructureManager());
246     myGridEchoGroup     = myGridEchoStructure->NewGroup();
247   }
248
249   myGridEchoAspect = theMarker;
250   myGridEchoGroup->SetPrimitivesAspect (theMarker);
251 }
252
253 // =======================================================================
254 // function : ShowGridEcho
255 // purpose  :
256 // =======================================================================
257 void V3d_Viewer::ShowGridEcho (const Handle(V3d_View)& theView,
258                                const Graphic3d_Vertex& theVertex)
259 {
260   if (!myGridEcho)
261   {
262     return;
263   }
264
265   if (myGridEchoStructure.IsNull())
266   {
267     myGridEchoStructure = new Graphic3d_Structure (StructureManager());
268     myGridEchoGroup     = myGridEchoStructure->NewGroup();
269
270     myGridEchoAspect    = new Graphic3d_AspectMarker3d (Aspect_TOM_STAR, Quantity_Color (Quantity_NOC_GRAY90), 3.0);
271     myGridEchoGroup->SetPrimitivesAspect (myGridEchoAspect);
272   }
273
274   if (theVertex.X() == myGridEchoLastVert.X()
275    && theVertex.Y() == myGridEchoLastVert.Y()
276    && theVertex.Z() == myGridEchoLastVert.Z())
277   {
278     return;
279   }
280
281   myGridEchoLastVert = theVertex;
282   myGridEchoGroup->Clear();
283   myGridEchoGroup->SetPrimitivesAspect (myGridEchoAspect);
284
285   Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
286   anArrayOfPoints->AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z());
287   myGridEchoGroup->AddPrimitiveArray (anArrayOfPoints);
288
289   myGridEchoStructure->SetZLayer (Graphic3d_ZLayerId_Topmost);
290   myGridEchoStructure->SetInfiniteState (Standard_True);
291   myGridEchoStructure->CStructure()->ViewAffinity = new Graphic3d_ViewAffinity();
292   myGridEchoStructure->CStructure()->ViewAffinity->SetVisible (Standard_False);
293   myGridEchoStructure->CStructure()->ViewAffinity->SetVisible (theView->View()->Identification(), true);
294   myGridEchoStructure->Display();
295 }
296
297 // =======================================================================
298 // function : HideGridEcho
299 // purpose  :
300 // =======================================================================
301 void V3d_Viewer::HideGridEcho (const Handle(V3d_View)& theView)
302 {
303   if (myGridEchoStructure.IsNull())
304   {
305     return;
306   }
307
308   myGridEchoLastVert.SetCoord (ShortRealLast(), ShortRealLast(), ShortRealLast());
309   const Handle(Graphic3d_ViewAffinity)& anAffinity = myGridEchoStructure->CStructure()->ViewAffinity;
310   if (!anAffinity.IsNull() && anAffinity->IsVisible (theView->View()->Identification()))
311     myGridEchoStructure->Erase();
312 }