0022734: Memory allocation error in OpenGl
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_4.cxx
1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 #include <OpenGl_GraphicDriver.hxx>
22
23 #include <NCollection_DataMap.hxx>
24 #include <OpenGl_Structure.hxx>
25 #include <OpenGl_CView.hxx>
26
27 void OpenGl_GraphicDriver::ClearStructure (const Graphic3d_CStructure& theCStructure)
28 {
29   OpenGl_Structure* aStructure = (OpenGl_Structure* )theCStructure.ptrStructure;
30   if (aStructure == NULL)
31     return;
32
33   aStructure->Clear();
34   InvalidateAllWorkspaces();
35 }
36
37 void OpenGl_GraphicDriver::ContextStructure (const Graphic3d_CStructure& theCStructure)
38 {
39   OpenGl_Structure* aStructure = (OpenGl_Structure* )theCStructure.ptrStructure;
40   if (aStructure == NULL)
41     return;
42
43   aStructure->SetTransformPersistence (theCStructure.TransformPersistence);
44
45   if (theCStructure.ContextLine.IsDef)
46     aStructure->SetAspectLine (theCStructure.ContextLine);
47
48   if (theCStructure.ContextFillArea.IsDef)
49     aStructure->SetAspectFace (theCStructure.ContextFillArea);
50
51   if (theCStructure.ContextMarker.IsDef)
52     aStructure->SetAspectMarker (theCStructure.ContextMarker);
53
54   if (theCStructure.ContextText.IsDef)
55     aStructure->SetAspectText (theCStructure.ContextText);
56
57   InvalidateAllWorkspaces();
58 }
59
60 void OpenGl_GraphicDriver::Connect (const Graphic3d_CStructure& theFather,
61                                     const Graphic3d_CStructure& theSon)
62 {
63   OpenGl_Structure* aFather = (OpenGl_Structure* )theFather.ptrStructure;
64   OpenGl_Structure* aSon = (OpenGl_Structure* )theSon.ptrStructure;
65   if (aFather && aSon)
66     aFather->Connect(aSon);
67 }
68
69 void OpenGl_GraphicDriver::Disconnect (const Graphic3d_CStructure& theFather,
70                                        const Graphic3d_CStructure& theSon)
71 {
72   OpenGl_Structure* aFather = (OpenGl_Structure* )theFather.ptrStructure;
73   OpenGl_Structure* aSon = (OpenGl_Structure* )theSon.ptrStructure;
74   if (aFather && aSon)
75     aFather->Disconnect(aSon);
76 }
77
78 void OpenGl_GraphicDriver::DisplayStructure (const Graphic3d_CView&      theCView,
79                                              const Graphic3d_CStructure& theCStructure,
80                                              const Standard_Integer      thePriority)
81 {
82   const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
83   OpenGl_Structure* aStructure = (OpenGl_Structure* )theCStructure.ptrStructure;
84   if (aCView == NULL || aStructure == NULL)
85     return;
86
87   aCView->View->DisplayStructure (aStructure, thePriority);
88   aCView->WS->Invalidate();
89 }
90
91 void OpenGl_GraphicDriver::EraseStructure (const Graphic3d_CView&      theCView,
92                                            const Graphic3d_CStructure& theCStructure)
93 {
94   const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
95   OpenGl_Structure* aStructure = (OpenGl_Structure* )theCStructure.ptrStructure;
96   if (aCView == NULL || aStructure == NULL)
97     return;
98
99   aCView->View->EraseStructure (aStructure);
100   aCView->WS->Invalidate();
101 }
102
103 void OpenGl_GraphicDriver::RemoveStructure (const Graphic3d_CStructure& theCStructure)
104 {
105   if (!GetMapOfStructures().IsBound (theCStructure.Id))
106     return;
107
108   OpenGl_Structure* aStructure = OpenGl_GraphicDriver::GetMapOfStructures().Find (theCStructure.Id);
109   OpenGl_GraphicDriver::GetMapOfStructures().UnBind (theCStructure.Id);
110   delete aStructure;
111   InvalidateAllWorkspaces();
112 }
113
114 void OpenGl_GraphicDriver::Structure (Graphic3d_CStructure& theCStructure)
115 {
116   RemoveStructure (theCStructure);
117
118   OpenGl_Structure* aStructure = new OpenGl_Structure();
119
120   aStructure->SetDegenerateModel (theCStructure.ContextFillArea.DegenerationMode,
121                                   theCStructure.ContextFillArea.SkipRatio);
122
123   Standard_Integer aStatus = 0;
124   if (theCStructure.highlight) aStatus |= OPENGL_NS_HIGHLIGHT;
125   if (!theCStructure.visible)  aStatus |= OPENGL_NS_HIDE;
126   aStructure->SetNamedStatus (aStatus);
127
128   theCStructure.ptrStructure = aStructure;
129   OpenGl_GraphicDriver::GetMapOfStructures().Bind (theCStructure.Id, aStructure);
130   InvalidateAllWorkspaces();
131 }
132
133 //=======================================================================
134 //function : ChangeZLayer
135 //purpose  :
136 //=======================================================================
137
138 void OpenGl_GraphicDriver::ChangeZLayer (const Graphic3d_CStructure& theCStructure,
139                                          const Standard_Integer theLayer)
140 {
141   if (!GetMapOfStructures().IsBound (theCStructure.Id))
142     return;
143
144   OpenGl_Structure* aStructure =
145     OpenGl_GraphicDriver::GetMapOfStructures().Find (theCStructure.Id);
146
147   aStructure->SetZLayer (theLayer);
148 }
149
150 //=======================================================================
151 //function : ChangeZLayer
152 //purpose  :
153 //=======================================================================
154
155 void OpenGl_GraphicDriver::ChangeZLayer (const Graphic3d_CStructure& theCStructure,
156                                          const Graphic3d_CView& theCView,
157                                          const Standard_Integer theNewLayerId)
158 {
159   const OpenGl_CView *aCView = (const OpenGl_CView *)theCView.ptrView;
160
161   if (!GetMapOfStructures().IsBound (theCStructure.Id) || !aCView)
162     return;
163
164   OpenGl_Structure* aStructure =
165     OpenGl_GraphicDriver::GetMapOfStructures().Find (theCStructure.Id);
166
167   aCView->View->ChangeZLayer (aStructure, theNewLayerId);
168 }
169
170 //=======================================================================
171 //function : GetZLayer
172 //purpose  :
173 //=======================================================================
174
175 Standard_Integer OpenGl_GraphicDriver::GetZLayer (const Graphic3d_CStructure& theCStructure) const
176 {
177   if (!GetMapOfStructures().IsBound (theCStructure.Id))
178     return -1;
179
180   OpenGl_Structure* aStructure = 
181     OpenGl_GraphicDriver::GetMapOfStructures().Find (theCStructure.Id);
182
183   return aStructure->GetZLayer();
184 }
185
186 //=======================================================================
187 //function : UnsetZLayer
188 //purpose  :
189 //=======================================================================
190
191 void OpenGl_GraphicDriver::UnsetZLayer (const Standard_Integer theLayerId)
192 {
193   NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator
194     aStructIt (GetMapOfStructures ());
195   
196   for( ; aStructIt.More (); aStructIt.Next ())
197   {
198     OpenGl_Structure* aStruct = aStructIt.ChangeValue ();
199     if (aStruct->GetZLayer () == theLayerId)
200       aStruct->SetZLayer (0);
201   }
202 }