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