Warnings on vc14 were eliminated
[occt.git] / src / IVtkDraw / IVtkDraw_HighlightAndSelectionPipeline.cxx
1 // Created on: 2012-04-02 
2
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS 
4 // 
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <IVtkDraw_HighlightAndSelectionPipeline.hxx>
17
18 #include <vtkRenderer.h>
19 #include <vtkActor.h>
20 #include <vtkPolyDataMapper.h>
21 #include <vtkPolyData.h>
22 #include <vtkAppendPolyData.h>
23 #include <vtkProperty.h>
24 #include <vtkRenderWindow.h>
25
26 #include <IVtkOCC_Shape.hxx>
27 #include <IVtkTools_DisplayModeFilter.hxx>
28 #include <IVtkTools_ShapeDataSource.hxx>
29 #include <IVtkTools_ShapeObject.hxx>
30
31
32 IMPLEMENT_STANDARD_RTTIEXT(IVtkDraw_HighlightAndSelectionPipeline,Standard_Transient)
33
34 //===========================================================
35 // Function : Constructor
36 // Purpose  :
37 //===========================================================
38
39 IVtkDraw_HighlightAndSelectionPipeline::IVtkDraw_HighlightAndSelectionPipeline (const TopoDS_Shape& theShape,
40                                                                                 const Standard_Integer theShapeID)
41 : Standard_Transient()
42 {
43   /* ===========================
44    *  Allocate involved filters
45    * =========================== */
46
47   myFilterMap.Bind (Filter_DM_Shape, vtkSmartPointer<IVtkTools_DisplayModeFilter>::New());
48   myFilterMap.Bind (Filter_DM_Hili,  vtkSmartPointer<IVtkTools_DisplayModeFilter>::New());
49   myFilterMap.Bind (Filter_DM_Sel,   vtkSmartPointer<IVtkTools_DisplayModeFilter>::New());
50   myFilterMap.Bind (Filter_SUB_Hili, vtkSmartPointer<IVtkTools_SubPolyDataFilter>::New());
51   myFilterMap.Bind (Filter_SUB_Sel,  vtkSmartPointer<IVtkTools_SubPolyDataFilter>::New());
52
53   /* ========================
54    *  Build primary pipeline
55    * ======================== */
56
57   myActor = vtkSmartPointer<vtkActor>::New();
58   IVtkOCC_Shape::Handle anIVtkShape = new IVtkOCC_Shape (theShape);
59   anIVtkShape->SetId (theShapeID);
60   vtkSmartPointer<IVtkTools_ShapeDataSource> aDataSource = vtkSmartPointer<IVtkTools_ShapeDataSource>::New();
61   aDataSource->SetShape (anIVtkShape);
62
63   IVtkTools_DisplayModeFilter*
64     aDMFilter = IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find(Filter_DM_Shape));
65
66   aDMFilter->AddInputConnection (aDataSource->GetOutputPort());
67   aDMFilter->SetDisplayMode (DM_Wireframe);
68
69   myMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
70   myMapper->AddInputConnection(aDMFilter->GetOutputPort());
71   myActor->SetMapper(myMapper);
72   IVtkTools_ShapeObject::SetShapeSource (aDataSource, myActor);
73
74   myMapper->ScalarVisibilityOn();
75   myMapper->SetScalarModeToUseCellFieldData();
76   IVtkTools::InitShapeMapper (myMapper);
77   myMapper->Update();
78
79   /* =================================
80    *  Build pipeline for highlighting
81    * ================================= */
82
83   IVtkTools_DisplayModeFilter*
84     aDMFilterH = IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find(Filter_DM_Hili) );
85   IVtkTools_SubPolyDataFilter*
86     aSUBFilterH = IVtkTools_SubPolyDataFilter::SafeDownCast (myFilterMap.Find(Filter_SUB_Hili) );
87
88   // No highligthing exists initially
89   aSUBFilterH->SetInputConnection (aDataSource->GetOutputPort() );
90   aDMFilterH->SetInputConnection(aSUBFilterH->GetOutputPort());
91
92   myHiliMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
93   myHiliMapper->SetInputConnection (aDMFilterH->GetOutputPort() );
94
95   // Create non-pickable actor
96   myHiliActor = vtkSmartPointer<vtkActor>::New();
97   myHiliActor->SetPickable(0);
98   myHiliActor->SetVisibility(1);
99   myHiliActor->GetProperty()->SetColor(0, 1, 1);
100   myHiliActor->GetProperty()->SetOpacity(1);
101   myHiliActor->GetProperty()->SetPointSize (myHiliActor->GetProperty()->GetPointSize() + 4 );
102   myHiliActor->GetProperty()->SetLineWidth (myHiliActor->GetProperty()->GetLineWidth() + 2 );
103
104   // Set maper for actor
105   myHiliActor->SetMapper (myHiliMapper);
106   myHiliMapper->ScalarVisibilityOff();
107
108   /* ==============================
109    *  Build pipeline for selection
110    * ============================== */
111
112   IVtkTools_DisplayModeFilter*
113     aDMFilterS = IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find(Filter_DM_Sel) );
114   IVtkTools_SubPolyDataFilter*
115     aSUBFilterS = IVtkTools_SubPolyDataFilter::SafeDownCast (myFilterMap.Find(Filter_SUB_Sel) );
116
117   // No highligthing exists initially
118   aSUBFilterS->SetInputConnection (aDataSource->GetOutputPort() );
119   aDMFilterS->SetInputConnection(aSUBFilterS->GetOutputPort());
120
121   mySelMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
122   mySelMapper->SetInputConnection (aDMFilterS->GetOutputPort() );
123
124   // Create non-pickable actor
125   mySelActor = vtkSmartPointer<vtkActor>::New();
126   mySelActor->SetPickable (0);
127   mySelActor->SetVisibility (1);
128   mySelActor->GetProperty()->SetColor (1, 1, 1);
129   mySelActor->GetProperty()->SetOpacity (1);
130   mySelActor->GetProperty()->SetPointSize (myHiliActor->GetProperty()->GetPointSize() + 4 );
131   mySelActor->GetProperty()->SetLineWidth (myHiliActor->GetProperty()->GetLineWidth() + 2 );
132
133   // Set maper for actor
134   mySelActor->SetMapper (mySelMapper);
135   mySelMapper->ScalarVisibilityOff();
136 }
137
138 //===========================================================
139 // Function : AddToRenderer
140 // Purpose  :
141 //===========================================================
142 void IVtkDraw_HighlightAndSelectionPipeline::AddToRenderer (vtkRenderer* theRenderer)
143 {
144   theRenderer->AddActor (myActor);
145   theRenderer->AddActor (myHiliActor);
146   theRenderer->AddActor (mySelActor);
147 }
148
149 //===========================================================
150 // Function : RemoveFromRenderer
151 // Purpose  :
152 //===========================================================
153 void IVtkDraw_HighlightAndSelectionPipeline::RemoveFromRenderer (vtkRenderer* theRenderer)
154 {
155   theRenderer->RemoveActor (myActor);
156   theRenderer->RemoveActor (myHiliActor);
157   theRenderer->RemoveActor (mySelActor);
158
159   vtkSmartPointer<vtkRenderWindow> aWin = theRenderer->GetRenderWindow();
160   if (aWin != NULL)
161   {
162     myActor->ReleaseGraphicsResources(aWin);
163     myHiliActor->ReleaseGraphicsResources(aWin);
164     mySelActor->ReleaseGraphicsResources(aWin);
165   }
166 }
167
168 //===========================================================
169 // Function : ClearHighlightFilters
170 // Purpose  :
171 //===========================================================
172 void IVtkDraw_HighlightAndSelectionPipeline::ClearHighlightFilters()
173 {
174   this->GetHighlightFilter()->Clear();
175   this->GetHighlightFilter()->SetDoFiltering (true);
176   this->GetHighlightFilter()->Modified();
177 }
178
179 //===========================================================
180 // Function : ClearSelectionFilters
181 // Purpose  :
182 //===========================================================
183 void IVtkDraw_HighlightAndSelectionPipeline::ClearSelectionFilters()
184 {
185   this->GetSelectionFilter()->Clear();
186   this->GetSelectionFilter()->SetDoFiltering (true);
187   this->GetSelectionFilter()->Modified();
188 }
189
190 //===========================================================
191 // Function : GetDisplayModeFilter
192 // Purpose  :
193 //===========================================================
194 IVtkTools_DisplayModeFilter* IVtkDraw_HighlightAndSelectionPipeline::GetDisplayModeFilter()
195 {
196   return IVtkTools_DisplayModeFilter::SafeDownCast(myFilterMap.Find(Filter_DM_Shape));
197 }
198
199 //===========================================================
200 // Function : GetHighlightFilter
201 // Purpose  :
202 //===========================================================
203 IVtkTools_SubPolyDataFilter* IVtkDraw_HighlightAndSelectionPipeline::GetHighlightFilter()
204 {
205   return IVtkTools_SubPolyDataFilter::SafeDownCast(myFilterMap.Find(Filter_SUB_Hili));
206 }
207
208 //===========================================================
209 // Function : GetSelectionFilter
210 // Purpose  :
211 //===========================================================
212 IVtkTools_SubPolyDataFilter* IVtkDraw_HighlightAndSelectionPipeline::GetSelectionFilter()
213 {
214   return IVtkTools_SubPolyDataFilter::SafeDownCast(myFilterMap.Find(Filter_SUB_Sel));
215 }
216
217 //===========================================================
218 // Function : GetHighlightDMFilter
219 // Purpose  :
220 //===========================================================
221 IVtkTools_DisplayModeFilter* IVtkDraw_HighlightAndSelectionPipeline::GetHighlightDMFilter()
222 {
223   return IVtkTools_DisplayModeFilter::SafeDownCast(myFilterMap.Find(Filter_DM_Hili));
224 }
225
226 //===========================================================
227 // Function : GetSelectionDMFilter
228 // Purpose  :
229 //===========================================================
230 IVtkTools_DisplayModeFilter* IVtkDraw_HighlightAndSelectionPipeline::GetSelectionDMFilter()
231 {
232   return IVtkTools_DisplayModeFilter::SafeDownCast(myFilterMap.Find(Filter_DM_Sel));
233 }
234
235 //===========================================================
236 // Function : SharedVerticesSelectionOn
237 // Purpose  :
238 //===========================================================
239 void IVtkDraw_HighlightAndSelectionPipeline::SharedVerticesSelectionOn()
240 {
241   this->GetHighlightDMFilter()->SetDisplaySharedVertices (Standard_True);
242   this->GetSelectionDMFilter()->SetDisplaySharedVertices (Standard_True);
243 }
244
245 //===========================================================
246 // Function : SharedVerticesSelectionOff
247 // Purpose  :
248 //===========================================================
249 void IVtkDraw_HighlightAndSelectionPipeline::SharedVerticesSelectionOff()
250 {
251   this->GetHighlightDMFilter()->SetDisplaySharedVertices (Standard_False);
252   this->GetSelectionDMFilter()->SetDisplaySharedVertices (Standard_False);
253 }