Salome HOME
f6d343c298e87ef9298ab216415b0e0e76382cd9
[modules/visu.git] / src / PIPELINE / VISU_DataSetMapperHolder.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  VISU OBJECT : interactive object for VISU entities implementation
24 // File:    VISU_DataSetMapperHolder.cxx
25 // Author:  Alexey PETROV
26 // Module : VISU
27 //
28 #include "VISU_DataSetMapperHolder.hxx"
29 #include "SALOME_ExtractGeometry.h"
30 #include "VISU_ElnoDisassembleFilter.hxx"
31 #include "VISU_LookupTable.hxx"
32
33 #include "VISU_PipeLineUtils.hxx"
34
35 #include <vtkDataSetMapper.h>
36 #include <vtkUnstructuredGrid.h>
37
38 #include <vtkPlane.h>
39 #include <vtkImplicitBoolean.h>
40 #include <vtkImplicitFunction.h>
41 #include <vtkImplicitFunctionCollection.h>
42 #include <vtkMath.h>
43
44 #include <cmath>
45
46 #ifdef _DEBUG_
47 static int MYDEBUG = 0;
48 #else
49 static int MYDEBUG = 0;
50 #endif
51
52
53 //----------------------------------------------------------------------------
54 vtkStandardNewMacro(VISU_DataSetMapperHolder);
55
56
57 //----------------------------------------------------------------------------
58 VISU_DataSetMapperHolder
59 ::VISU_DataSetMapperHolder():
60   myElnoDisassembleFilter( VISU_ElnoDisassembleFilter::New() ),
61   myExtractGeometry( SALOME_ExtractGeometry::New() )
62 {
63   if(MYDEBUG) MESSAGE("VISU_DataSetMapperHolder::VISU_DataSetMapperHolder - "<<this);
64
65   myElnoDisassembleFilter->Delete();
66
67   // Clipping functionality
68   myExtractGeometry->Delete();
69   myExtractGeometry->SetStoreMapping(true);
70
71   vtkImplicitBoolean* anImplicitBoolean = vtkImplicitBoolean::New();
72   myExtractGeometry->SetImplicitFunction(anImplicitBoolean);
73   anImplicitBoolean->SetOperationTypeToIntersection();
74   anImplicitBoolean->Delete();
75 }
76
77
78 //----------------------------------------------------------------------------
79 VISU_DataSetMapperHolder
80 ::~VISU_DataSetMapperHolder()
81 {
82   if(MYDEBUG)
83     MESSAGE("VISU_DataSetMapperHolder::~VISU_DataSetMapperHolder - "<<this);
84 }
85
86
87 //----------------------------------------------------------------------------
88 void 
89 VISU_DataSetMapperHolder
90 ::ShallowCopy(VISU_MapperHolder *theMapperHolder,
91               bool theIsCopyInput)
92 {
93   if(VISU_DataSetMapperHolder* aMapperHolder = dynamic_cast<VISU_DataSetMapperHolder*>(theMapperHolder)){
94     if(theIsCopyInput)
95       SetUnstructuredGridIDMapper(aMapperHolder->GetUnstructuredGridIDMapper());
96     
97     VISU::CopyDataSetMapper(GetDataSetMapper(), 
98                             aMapperHolder->GetDataSetMapper(), 
99                             theIsCopyInput);
100     myExtractGeometry->SetImplicitFunction(aMapperHolder->GetImplicitFunction());
101   }
102 }
103
104
105 //----------------------------------------------------------------------------
106 void
107 VISU_DataSetMapperHolder
108 ::SetElnoDisassembleState( bool theIsShrunk )
109 {
110   vtkFloatingPointType aShrinkFactor = std::abs( myElnoDisassembleFilter->GetShrinkFactor() );
111   if ( theIsShrunk )
112     myElnoDisassembleFilter->SetShrinkFactor( aShrinkFactor );
113   else
114     myElnoDisassembleFilter->SetShrinkFactor( -aShrinkFactor );
115 }
116
117
118 //----------------------------------------------------------------------------
119 unsigned long int
120 VISU_DataSetMapperHolder
121 ::GetMemorySize()
122 {
123   unsigned long int aSize = Superclass::GetMemorySize();
124
125   if(myExtractGeometry->GetInput())
126     if(vtkDataSet* aDataSet = myExtractGeometry->GetOutput())
127       aSize = aDataSet->GetActualMemorySize() * 1024;
128   
129   return aSize;
130 }
131
132
133 //----------------------------------------------------------------------------
134 void
135 VISU_DataSetMapperHolder
136 ::SetUnstructuredGridIDMapper(const VISU::PUnstructuredGridIDMapper& theIDMapper)
137 {
138   myElnoDisassembleFilter->SetInput( theIDMapper->GetUnstructuredGridOutput() );
139   myExtractGeometry->SetInput( myElnoDisassembleFilter->GetOutput() );
140   myUnstructuredGridIDMapper = theIDMapper;
141   SetIDMapper( theIDMapper );
142 }
143
144
145 //----------------------------------------------------------------------------
146 const VISU::PUnstructuredGridIDMapper&  
147 VISU_DataSetMapperHolder
148 ::GetUnstructuredGridIDMapper()
149 {
150   return myUnstructuredGridIDMapper;
151 }
152
153
154 //----------------------------------------------------------------------------
155 vtkUnstructuredGrid* 
156 VISU_DataSetMapperHolder
157 ::GetUnstructuredGridInput()
158 {
159   if(myUnstructuredGridIDMapper)
160     return myUnstructuredGridIDMapper->GetUnstructuredGridOutput();
161
162   return NULL;
163 }
164
165
166 //----------------------------------------------------------------------------
167 vtkPointSet* 
168 VISU_DataSetMapperHolder
169 ::GetClippedInput()
170 {
171   if(myExtractGeometry->GetInput())
172     myExtractGeometry->Update();
173   return myExtractGeometry->GetOutput();
174 }
175
176
177 //----------------------------------------------------------------------------
178 void
179 VISU_DataSetMapperHolder
180 ::OnCreateMapper()
181 {
182   myDataSetMapper = vtkDataSetMapper::New();
183   myDataSetMapper->Delete();
184   SetMapper(myDataSetMapper.GetPointer());
185 }
186
187
188 //----------------------------------------------------------------------------
189 void
190 VISU_DataSetMapperHolder
191 ::SetDataSetMapper(vtkDataSetMapper* theMapper)
192 {
193   myDataSetMapper = theMapper;
194   SetMapper(myDataSetMapper.GetPointer());
195 }
196
197
198 //----------------------------------------------------------------------------
199 vtkDataSetMapper* 
200 VISU_DataSetMapperHolder
201 ::GetDataSetMapper()
202 {
203   GetMapper();
204   return myDataSetMapper.GetPointer();
205 }
206
207
208 //----------------------------------------------------------------------------
209 void
210 VISU_DataSetMapperHolder
211 ::SetLookupTable(VISU_LookupTable* theLookupTable)
212 {
213   myDataSetMapper->SetLookupTable(theLookupTable);
214 }
215
216
217 //----------------------------------------------------------------------------
218 vtkIdType 
219 VISU_DataSetMapperHolder
220 ::GetNodeObjID(vtkIdType theID)
221 {
222   vtkIdType anID = myExtractGeometry->GetNodeObjId(theID);
223   return Superclass::GetNodeObjID(anID);
224 }
225
226 //----------------------------------------------------------------------------
227 vtkIdType 
228 VISU_DataSetMapperHolder
229 ::GetNodeVTKID(vtkIdType theID)
230 {
231   vtkIdType anID = Superclass::GetNodeVTKID(theID);
232   return myExtractGeometry->GetNodeVTKId(anID);
233 }
234
235 //----------------------------------------------------------------------------
236 vtkFloatingPointType* 
237 VISU_DataSetMapperHolder
238 ::GetNodeCoord(vtkIdType theObjID)
239 {
240   return Superclass::GetNodeCoord(theObjID);
241 }
242
243
244 //----------------------------------------------------------------------------
245 vtkIdType 
246 VISU_DataSetMapperHolder
247 ::GetElemObjID(vtkIdType theID)
248 {
249   vtkIdType anID = myExtractGeometry->GetElemObjId(theID);
250   return Superclass::GetElemObjID(anID);
251 }
252
253 //----------------------------------------------------------------------------
254 vtkIdType
255 VISU_DataSetMapperHolder
256 ::GetElemVTKID(vtkIdType theID)
257 {
258   vtkIdType anID = Superclass::GetElemVTKID(theID);
259   return myExtractGeometry->GetElemVTKId(anID);
260 }
261
262 //----------------------------------------------------------------------------
263 vtkCell* 
264 VISU_DataSetMapperHolder
265 ::GetElemCell(vtkIdType  theObjID)
266 {
267   return Superclass::GetElemCell(theObjID);
268 }
269
270
271 //----------------------------------------------------------------------------
272 void
273 VISU_DataSetMapperHolder
274 ::SetImplicitFunction(vtkImplicitFunction *theFunction)
275 {
276   myExtractGeometry->SetImplicitFunction(theFunction);
277
278
279 //----------------------------------------------------------------------------
280 vtkImplicitFunction * 
281 VISU_DataSetMapperHolder
282 ::GetImplicitFunction()
283 {
284   return myExtractGeometry->GetImplicitFunction();
285 }
286
287 //----------------------------------------------------------------------------
288 void
289 VISU_DataSetMapperHolder
290 ::RemoveAllClippingPlanes()
291 {
292   if(vtkImplicitBoolean* aBoolean = myExtractGeometry->GetImplicitBoolean()){
293     vtkImplicitFunctionCollection* aFunction = aBoolean->GetFunction();
294     aFunction->RemoveAllItems();
295     aBoolean->Modified(); // VTK bug
296   }
297 }
298
299 //----------------------------------------------------------------------------
300 vtkIdType
301 VISU_DataSetMapperHolder
302 ::GetNumberOfClippingPlanes()
303 {
304   if(vtkImplicitBoolean* aBoolean = myExtractGeometry->GetImplicitBoolean()){
305     vtkImplicitFunctionCollection* aFunction = aBoolean->GetFunction();
306     return aFunction->GetNumberOfItems();
307   }
308   return 0;
309 }
310
311 //----------------------------------------------------------------------------
312 bool 
313 VISU_DataSetMapperHolder
314 ::AddClippingPlane(vtkPlane* thePlane)
315 {
316   if (thePlane) {
317     if (vtkImplicitBoolean* aBoolean = myExtractGeometry->GetImplicitBoolean()) {
318       vtkImplicitFunctionCollection* aFunction = aBoolean->GetFunction();
319       aFunction->AddItem(thePlane);
320       aBoolean->Modified();
321       // Check, that at least one cell present after clipping.
322       // This check was introduced because of bug IPAL8849.
323       vtkDataSet* aClippedDataSet = GetClippedInput();
324       if(aClippedDataSet->GetNumberOfCells() < 1)
325         return false;
326     }
327   }
328   return true;
329 }
330
331 //----------------------------------------------------------------------------
332 vtkPlane* 
333 VISU_DataSetMapperHolder
334 ::GetClippingPlane(vtkIdType theID)
335 {
336   vtkPlane* aPlane = NULL;
337   if(theID >= 0 && theID < GetNumberOfClippingPlanes()){
338     if(vtkImplicitBoolean* aBoolean = myExtractGeometry->GetImplicitBoolean()){
339       vtkImplicitFunctionCollection* aFunction = aBoolean->GetFunction();
340       vtkImplicitFunction* aFun = NULL;
341       aFunction->InitTraversal();
342       for(vtkIdType anID = 0; anID <= theID; anID++)
343         aFun = aFunction->GetNextItem();
344       aPlane = dynamic_cast<vtkPlane*>(aFun);
345     }
346   }
347   return aPlane;
348 }
349
350 //----------------------------------------------------------------------------
351 void VISU_DataSetMapperHolder::RemoveClippingPlane(vtkIdType theID)
352 {
353   if(theID >= 0 && theID < GetNumberOfClippingPlanes()){
354     if(vtkImplicitBoolean* aBoolean = myExtractGeometry->GetImplicitBoolean()){
355       vtkImplicitFunctionCollection* aFunctions = aBoolean->GetFunction();
356       aFunctions->RemoveItem(theID);
357       aBoolean->Modified();
358     }
359   }
360 }
361
362
363 //----------------------------------------------------------------------------
364 void
365 VISU_DataSetMapperHolder
366 ::SetExtractInside(bool theMode)
367 {
368   myExtractGeometry->SetExtractInside(theMode);
369 }
370
371 //----------------------------------------------------------------------------
372 void
373 VISU_DataSetMapperHolder
374 ::SetExtractBoundaryCells(bool theMode)
375 {
376   myExtractGeometry->SetExtractBoundaryCells(theMode);
377 }
378
379
380 //----------------------------------------------------------------------------