Salome HOME
Merge from BR_WIN_INDUS_514 branch 21/03/2011 (Windows industrialization)
[modules/visu.git] / src / PIPELINE / VISU_PipeLine.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_PipeLine.cxx
25 // Author:  Alexey PETROV
26 // Module : VISU
27 //
28 #include "VISU_PipeLine.hxx"
29 #include "VISU_MapperHolder.hxx"
30
31 #include "VISU_PipeLineUtils.hxx"
32
33 #include <vtkObjectFactory.h>
34 #include <vtkPlane.h>
35
36 #include <float.h>
37 #include <algorithm>
38 #include <climits>
39 #include <limits>
40
41 #ifdef _DEBUG_
42 static int MYDEBUG = 0;
43 #else
44 static int MYDEBUG = 0;
45 #endif
46
47
48 //----------------------------------------------------------------------------
49 VISU_PipeLine
50 ::VISU_PipeLine():
51   myIsShrinkable(true),
52   myIsFeatureEdgesAllowed(true)
53 {
54   if(MYDEBUG) MESSAGE("VISU_PipeLine::VISU_PipeLine - "<<this);
55   
56   vtkMath::UninitializeBounds(myVisibleBounds);
57 }
58
59
60 //----------------------------------------------------------------------------
61 VISU_PipeLine
62 ::~VISU_PipeLine()
63 {
64   if(MYDEBUG) MESSAGE("VISU_PipeLine::~VISU_PipeLine - "<<this);
65 }
66
67
68 //----------------------------------------------------------------------------
69 unsigned long int 
70 VISU_PipeLine
71 ::GetMTime()
72 {
73   unsigned long int aTime = Superclass::GetMTime();
74
75   if(myMapperHolder.GetPointer())
76     aTime = std::max(aTime, myMapperHolder->GetMTime());
77
78   return aTime;
79 }
80
81
82 //----------------------------------------------------------------------------
83 unsigned long int
84 VISU_PipeLine
85 ::GetMemorySize()
86 {
87   unsigned long int aSize = 0;
88
89   if(myMapperHolder.GetPointer())
90     aSize += myMapperHolder->GetMemorySize();
91   
92   return aSize;
93 }
94
95
96 //----------------------------------------------------------------------------
97 void 
98 VISU_PipeLine
99 ::ShallowCopy(VISU_PipeLine *thePipeLine,
100               bool theIsCopyInput)
101 {
102   SetImplicitFunction(thePipeLine->GetImplicitFunction());
103   DoShallowCopy(thePipeLine, theIsCopyInput);
104   Update();
105 }
106
107
108 //----------------------------------------------------------------------------
109 void 
110 VISU_PipeLine
111 ::DoShallowCopy(VISU_PipeLine *thePipeLine,
112                 bool theIsCopyInput)
113 {
114   GetMapperHolder()->ShallowCopy(thePipeLine->GetMapperHolder(),
115                                  theIsCopyInput);
116   for( int i =0 ; i < 6 ; i++)
117     myVisibleBounds[i] = thePipeLine->myVisibleBounds[i];
118   
119   myVisibleComputeTime = thePipeLine->myVisibleComputeTime;
120 }
121
122
123 //----------------------------------------------------------------------------
124 void
125 VISU_PipeLine
126 ::SameAs(VISU_PipeLine *thePipeLine)
127 {
128   DoShallowCopy(thePipeLine, false);
129   Update();
130 }
131
132
133 //----------------------------------------------------------------------------
134 VISU_MapperHolder* 
135 VISU_PipeLine
136 ::GetMapperHolder()
137 {
138   if(!myMapperHolder.GetPointer())
139     OnCreateMapperHolder();
140
141   return myMapperHolder.GetPointer();
142 }
143
144
145 //----------------------------------------------------------------------------
146 const VISU::PIDMapper&  
147 VISU_PipeLine
148 ::GetIDMapper()
149 {
150   return GetMapperHolder()->GetIDMapper();
151 }
152
153
154 //----------------------------------------------------------------------------
155 vtkDataSet* 
156 VISU_PipeLine
157 ::GetInput()
158 {
159   return GetMapperHolder()->GetInput();
160 }
161
162
163 //----------------------------------------------------------------------------
164 vtkMapper* 
165 VISU_PipeLine
166 ::GetMapper()
167 {
168   return GetMapperHolder()->GetMapper();
169 }
170
171
172 //----------------------------------------------------------------------------
173 vtkDataSet* 
174 VISU_PipeLine
175 ::GetOutput()
176 {
177   return GetMapperHolder()->GetOutput();
178 }
179
180
181 //----------------------------------------------------------------------------
182 bool
183 VISU_PipeLine
184 ::IsPlanarInput()
185 {
186   vtkFloatingPointType aBounds[6];
187   GetInput()->GetBounds( aBounds ); // xmin,xmax, ymin,ymax, zmin,zmax
188   if (fabs( aBounds[0] - aBounds[1] ) <= FLT_MIN ||
189       fabs( aBounds[2] - aBounds[3] ) <= FLT_MIN ||
190       fabs( aBounds[4] - aBounds[5] ) <= FLT_MIN )
191     return true;
192
193   return false;
194 }
195
196
197 //----------------------------------------------------------------------------
198 void 
199 VISU_PipeLine
200 ::SetMapperHolder(VISU_MapperHolder* theHolder)
201 {
202   myMapperHolder = theHolder;
203   theHolder->SetPipeLine(this);
204 }
205
206
207 //----------------------------------------------------------------------------
208 void 
209 VISU_PipeLine
210 ::Init()
211 {}
212
213
214 //----------------------------------------------------------------------------
215 void 
216 VISU_PipeLine
217 ::Update()
218 {
219   GetMapperHolder()->Update();
220 }
221
222
223 //----------------------------------------------------------------------------
224 vtkIdType 
225 VISU_PipeLine
226 ::GetNodeObjID(vtkIdType theID)
227 {
228   return GetMapperHolder()->GetNodeObjID(theID);
229 }
230
231 //----------------------------------------------------------------------------
232 vtkIdType 
233 VISU_PipeLine
234 ::GetNodeVTKID(vtkIdType theID)
235 {
236   return GetMapperHolder()->GetNodeVTKID(theID);
237 }
238
239 //----------------------------------------------------------------------------
240 vtkFloatingPointType* 
241 VISU_PipeLine
242 ::GetNodeCoord(vtkIdType theObjID)
243 {
244   return GetMapperHolder()->GetNodeCoord(theObjID);
245 }
246
247
248 //----------------------------------------------------------------------------
249 vtkIdType 
250 VISU_PipeLine
251 ::GetElemObjID(vtkIdType theID)
252 {
253   return GetMapperHolder()->GetElemObjID(theID);
254 }
255
256 //----------------------------------------------------------------------------
257 vtkIdType
258 VISU_PipeLine
259 ::GetElemVTKID(vtkIdType theID)
260 {
261   return GetMapperHolder()->GetElemVTKID(theID);
262 }
263
264 //----------------------------------------------------------------------------
265 vtkCell* 
266 VISU_PipeLine
267 ::GetElemCell(vtkIdType  theObjID)
268 {
269   return GetMapperHolder()->GetElemCell(theObjID);
270 }
271
272
273 //----------------------------------------------------------------------------
274 bool 
275 VISU_PipeLine
276 ::IsShrinkable()
277 {
278   return myIsShrinkable;
279 }
280
281 void 
282 VISU_PipeLine
283 ::SetIsShrinkable(bool theIsShrinkable)
284 {
285   if(myIsShrinkable == theIsShrinkable)
286     return;
287
288   myIsShrinkable = theIsShrinkable;
289   Modified();
290 }
291
292
293 //----------------------------------------------------------------------------
294 bool 
295 VISU_PipeLine
296 ::IsFeatureEdgesAllowed()
297 {
298   return myIsFeatureEdgesAllowed;
299 }
300
301 void 
302 VISU_PipeLine
303 ::SetIsFeatureEdgesAllowed(bool theIsFeatureEdgesAllowed)
304 {
305   if(myIsFeatureEdgesAllowed == theIsFeatureEdgesAllowed)
306     return;
307
308   myIsFeatureEdgesAllowed = theIsFeatureEdgesAllowed;
309   Modified();
310 }
311
312
313 //----------------------------------------------------------------------------
314 void
315 VISU_PipeLine
316 ::SetImplicitFunction(vtkImplicitFunction *theFunction)
317 {
318   GetMapperHolder()->SetImplicitFunction(theFunction);
319
320
321 //----------------------------------------------------------------------------
322 vtkImplicitFunction * 
323 VISU_PipeLine
324 ::GetImplicitFunction()
325 {
326   return GetMapperHolder()->GetImplicitFunction();
327 }
328
329 //----------------------------------------------------------------------------
330 void
331 VISU_PipeLine
332 ::SetExtractInside(bool theMode)
333 {
334   GetMapperHolder()->SetExtractInside(theMode);
335 }
336
337 //----------------------------------------------------------------------------
338 void
339 VISU_PipeLine
340 ::SetExtractBoundaryCells(bool theMode)
341 {
342   GetMapperHolder()->SetExtractBoundaryCells(theMode);
343 }
344
345
346 //----------------------------------------------------------------------------
347 void
348 VISU_PipeLine
349 ::RemoveAllClippingPlanes()
350 {
351   GetMapperHolder()->RemoveAllClippingPlanes();
352 }
353
354 //----------------------------------------------------------------------------
355 vtkIdType
356 VISU_PipeLine
357 ::GetNumberOfClippingPlanes()
358 {
359   return GetMapperHolder()->GetNumberOfClippingPlanes();
360 }
361
362 //----------------------------------------------------------------------------
363 bool 
364 VISU_PipeLine
365 ::AddClippingPlane(vtkPlane* thePlane)
366 {
367   return GetMapperHolder()->AddClippingPlane(thePlane);
368 }
369
370 //----------------------------------------------------------------------------
371 vtkPlane* 
372 VISU_PipeLine
373 ::GetClippingPlane(vtkIdType theID)
374 {
375   return GetMapperHolder()->GetClippingPlane(theID);
376 }
377
378 //----------------------------------------------------------------------------
379 void VISU_PipeLine::RemoveClippingPlane(vtkIdType theID)
380 {
381   return GetMapperHolder()->RemoveClippingPlane(theID);
382 }
383
384 //----------------------------------------------------------------------------
385 vtkDataSet* 
386 VISU_PipeLine
387 ::GetClippedInput()
388 {
389   return GetMapperHolder()->GetClippedInput();
390 }
391
392
393 //----------------------------------------------------------------------------
394 void
395 VISU_PipeLine
396 ::SetPlaneParam(vtkFloatingPointType theDir[3], 
397                 vtkFloatingPointType theDist, 
398                 vtkPlane* thePlane)
399 {
400   thePlane->SetNormal(theDir);
401
402   vtkFloatingPointType anOrigin[3];
403
404   //Make sure that bounds are calculated
405   ComputeVisibleBounds();
406   VISU::DistanceToPosition(myVisibleBounds,
407                            theDir,
408                            theDist,
409                            anOrigin);
410
411   thePlane->SetOrigin(anOrigin);
412 }
413
414
415 //----------------------------------------------------------------------------
416 void
417 VISU_PipeLine
418 ::GetPlaneParam(vtkFloatingPointType theDir[3], 
419                 vtkFloatingPointType& theDist, 
420                 vtkPlane* thePlane)
421 {
422   thePlane->GetNormal(theDir);
423   
424   vtkFloatingPointType anOrigin[3];
425   thePlane->GetOrigin(anOrigin);
426   
427   //Make sure that bounds are calculated
428   ComputeVisibleBounds();
429   VISU::PositionToDistance(myVisibleBounds,
430                            theDir,
431                            anOrigin,
432                            theDist);
433 }
434
435
436 //----------------------------------------------------------------------------
437 size_t
438 VISU_PipeLine
439 ::CheckAvailableMemory(double theSize)
440 {
441   if(theSize < ULONG_MAX){
442     try{
443       size_t aSize = size_t(theSize);
444       if(char *aCheck = new char[aSize]){
445         delete [] aCheck;
446         return aSize;
447       }
448     }catch(std::bad_alloc& exc){
449     }catch(...){
450     }
451   }
452   return 0;
453 }
454
455
456 //----------------------------------------------------------------------------
457 size_t
458 VISU_PipeLine
459 ::GetAvailableMemory(double theSize, 
460                      double theMinSize)
461 {
462   // Finds acceptable memory size by half-deflection methods
463   static double EPSILON = 2 * 1024;
464   double aMax = std::max(theSize, theMinSize);
465   double aMin = std::min(theSize, theMinSize);
466   //cout<<"GetAvailableMemory - "<<aMax<<"; "<<aMin;
467   while(!CheckAvailableMemory(aMax) && CheckAvailableMemory(aMin) && (aMax - aMin) > EPSILON){
468     double aRoot = (aMax + aMin) / 2.;
469     if(CheckAvailableMemory(aRoot))
470       aMin = aRoot;
471     else
472       aMax = aRoot;
473   }
474   //cout<<"; "<<aMax<<endl;
475   return (size_t)aMax;
476 }
477
478
479 //----------------------------------------------------------------------------
480 // Re-compute visible bounds if need
481 void VISU_PipeLine::ComputeVisibleBounds() {
482   if(GetMTime() > myVisibleComputeTime) {
483     VISU::ComputeVisibleBounds(GetMapperHolder()->GetOutput(), myVisibleBounds);
484     myVisibleComputeTime.Modified();
485   }
486 }
487
488 //----------------------------------------------------------------------------
489 void VISU_PipeLine::GetVisibleBounds(vtkFloatingPointType theBounds[6]) {
490   // Compute or get cached bounds
491   ComputeVisibleBounds(); 
492   for (int i=0; i<6; i++) {
493     theBounds[i] = myVisibleBounds[i];
494   }
495 }
496
497 //----------------------------------------------------------------------------