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