1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // VISU OBJECT : interactive object for VISU entities implementation
24 // File: VISU_StreamLinesPL.cxx
25 // Author: Alexey PETROV
28 #include "VISU_StreamLinesPL.hxx"
30 #include "VISU_Extractor.hxx"
31 //#include "VISU_FieldTransform.hxx"
32 //#include "VISU_UsedPointsFilter.hxx"
33 #include "VISU_MaskPointsFilter.hxx"
34 #include "VISU_PipeLineUtils.hxx"
35 #include "VISU_StreamLine.hxx"
37 #include "VTKViewer_GeometryFilter.h"
39 #include <SUIT_Session.h>
40 #include <SUIT_ResourceMgr.h>
45 #include <vtkDataSet.h>
48 static int MYDEBUG = 0;
50 static int MYDEBUG = 0;
53 static vtkFloatingPointType EPS = 1.0e-7;
54 static vtkFloatingPointType aMinNbOfSteps = 1.0E+2;
55 //static vtkFloatingPointType aMaxNbOfSteps = 1.0E+3;
56 static vtkFloatingPointType aCoeffOfIntStep = 1.0E+1;
59 //----------------------------------------------------------------------------
60 vtkStandardNewMacro(VISU_StreamLinesPL);
63 //----------------------------------------------------------------------------
65 ::VISU_StreamLinesPL()
67 SetIsShrinkable(false);
68 SetIsFeatureEdgesAllowed(false);
70 myStream = VISU_StreamLine::New();
71 myCenters = vtkCellCenters::New();
72 myGeomFilter = VTKViewer_GeometryFilter::New();
73 myPointsFilter = VISU_MaskPointsFilter::New();
76 myPercents = GetUsedPointsDefault();
80 //----------------------------------------------------------------------------
82 ::~VISU_StreamLinesPL()
84 myPointsFilter->Delete();
85 myPointsFilter = NULL;
90 myGeomFilter->Delete();
98 //----------------------------------------------------------------------------
103 unsigned long int aTime = Superclass::GetMTime();
105 aTime = std::max(aTime, myStream->GetMTime());
106 aTime = std::max(aTime, myCenters->GetMTime());
107 aTime = std::max(aTime, myGeomFilter->GetMTime());
108 aTime = std::max(aTime, myPointsFilter->GetMTime());
111 aTime = std::max(aTime, mySource->GetMTime());
117 //----------------------------------------------------------------------------
120 ::DoShallowCopy(VISU_PipeLine *thePipeLine,
123 Superclass::DoShallowCopy(thePipeLine, theIsCopyInput);
125 if(VISU_StreamLinesPL *aPipeLine = dynamic_cast<VISU_StreamLinesPL*>(thePipeLine)){
126 SetParams(aPipeLine->GetIntegrationStep(),
127 aPipeLine->GetPropagationTime(),
128 aPipeLine->GetStepLength(),
129 aPipeLine->GetSource(),
130 aPipeLine->GetUsedPoints(),
131 aPipeLine->GetDirection());
136 //----------------------------------------------------------------------------
139 ::GetNecasseryMemorySize(vtkIdType theNbOfPoints,
140 vtkFloatingPointType theStepLength,
141 vtkFloatingPointType thePropogationTime,
142 vtkFloatingPointType thePercents)
144 static vtkFloatingPointType aStreamPointSize = sizeof(vtkFloatingPointType)*15 + sizeof(vtkIdType)*2;
145 static vtkFloatingPointType aStreamArraySize = aStreamPointSize*1024; // == 69632
147 vtkFloatingPointType aNbCells = thePercents*theNbOfPoints*2.0;
148 vtkFloatingPointType aNbPointsPerCell = thePropogationTime/theStepLength;
149 vtkFloatingPointType aCellsSize = aNbCells*(1+aNbPointsPerCell);
150 vtkFloatingPointType aPointsSize = aCellsSize*3.0*sizeof(vtkFloatingPointType);
152 vtkFloatingPointType aConnectivitySize = aCellsSize*sizeof(vtkIdType);
153 vtkFloatingPointType aTypesSize = aNbCells*sizeof(char);
154 vtkFloatingPointType aLocationsSize = aNbCells*sizeof(int);
155 //vtkFloatingPointType aNbCellsPerPoint = aCellsSize / aNbCells - 1;
156 vtkFloatingPointType aMeshSize = aPointsSize + aConnectivitySize + aTypesSize + aLocationsSize;
158 vtkFloatingPointType anAssignedDataSize = aCellsSize*4.0*sizeof(vtkFloatingPointType);
159 vtkFloatingPointType anOutputDataSetSize = aMeshSize + anAssignedDataSize;
161 vtkFloatingPointType aResult = aStreamArraySize*aNbCells + anOutputDataSetSize;
166 //----------------------------------------------------------------------------
169 ::FindPossibleParams(vtkDataSet* theDataSet,
170 vtkFloatingPointType& theStepLength,
171 vtkFloatingPointType& thePropogationTime,
172 vtkFloatingPointType& thePercents)
174 static vtkFloatingPointType aPercentsDecrease = 3.0, aStepLengthIncrease = 9.0;
175 vtkIdType aNbOfPoints = theDataSet->GetNumberOfPoints();
176 vtkFloatingPointType aSize = GetNecasseryMemorySize(aNbOfPoints,theStepLength,thePropogationTime,thePercents);
177 size_t anIsPossible = CheckAvailableMemory(aSize);
179 vtkFloatingPointType aMaxStepLength = std::max(GetMaxStepLength(theDataSet),thePropogationTime);
180 vtkFloatingPointType aMinStepLength = GetMinStepLength(theDataSet,thePercents);
181 vtkFloatingPointType aDeltaStepLength = (aMaxStepLength - aMinStepLength)/aStepLengthIncrease;
182 for(int i = 2, aStepChanged = 1, aPerecentsChanged = 1; aStepChanged || aPerecentsChanged; i++){
183 vtkFloatingPointType aStepLength = theStepLength + aDeltaStepLength;
184 if(aStepLength < aMaxStepLength) theStepLength = aStepLength;
185 else if(aStepChanged){
186 aStepLength = aMaxStepLength;
189 vtkFloatingPointType aPercents = thePercents /= aPercentsDecrease;
190 if(aPercents*aNbOfPoints > 1) thePercents = aPercents;
191 else if(aPerecentsChanged) {
192 thePercents = 1.1 / aNbOfPoints;
193 aPerecentsChanged = 0;
195 aSize = GetNecasseryMemorySize(aNbOfPoints,theStepLength,thePropogationTime,thePercents);
196 if(CheckAvailableMemory(aSize)){
202 if(MYDEBUG) MESSAGE("FindPossibleParams - aSize = "<<aSize<<"; anIsPossible = "<<anIsPossible);
207 //----------------------------------------------------------------------------
210 ::SetParams(vtkFloatingPointType theIntStep,
211 vtkFloatingPointType thePropogationTime,
212 vtkFloatingPointType theStepLength,
213 vtkPointSet* theSource,
214 vtkFloatingPointType thePercents,
217 vtkPointSet* aDataSet = theSource? theSource: GetMergedInput();
220 vtkIdType aNbOfPoints = aDataSet->GetNumberOfPoints();
221 vtkDataSet* aPointSet = GetExtractorFilter()->GetOutput();
222 if (thePercents * aNbOfPoints < 1)
223 thePercents = 2.0 / aNbOfPoints;
225 theIntStep = CorrectIntegrationStep(theIntStep,
229 thePropogationTime = CorrectPropagationTime(thePropogationTime,
233 theStepLength = CorrectStepLength(theStepLength,
237 size_t anIsAccepted = FindPossibleParams(aPointSet,
243 mySource = theSource;
244 myPercents = thePercents;
245 if(VISU::IsDataOnCells(GetMergedInput())){
246 myCenters->SetInput(aDataSet);
247 myCenters->VertexCellsOn();
248 aDataSet = myCenters->GetOutput();
250 myPointsFilter->SetInput(aDataSet);
251 myPointsFilter->SetPercentsOfUsedPoints(thePercents);
252 aDataSet = myPointsFilter->GetOutput();
253 myStream->SetSource(aDataSet);
254 myStream->SetIntegrationStepLength(theIntStep);
255 myStream->SetMaximumPropagationTime(thePropogationTime);
256 myStream->SetStepLength(theStepLength);
257 myStream->SetSavePointInterval(theIntStep*aMinNbOfSteps);
258 myStream->SetIntegrationDirection(theDirection);
259 myStream->Modified();
266 //----------------------------------------------------------------------------
275 //----------------------------------------------------------------------------
284 //----------------------------------------------------------------------------
287 ::GetStreamerSource()
289 return myStream->GetSource();
293 //----------------------------------------------------------------------------
298 return GetVelocityCoeff(GetExtractorFilter()->GetOutput());
302 //----------------------------------------------------------------------------
305 ::GetVelocityCoeff(vtkDataSet* theDataSet)
307 vtkFloatingPointType* aScalarRange = theDataSet->GetScalarRange();
308 vtkFloatingPointType aVelocity = (fabs(aScalarRange[1]) + fabs(aScalarRange[0]))/2.0;
316 //----------------------------------------------------------------------------
319 ::IsPossible(vtkPointSet* theDataSet)
321 vtkFloatingPointType aPercents = GetUsedPointsDefault();
322 vtkFloatingPointType aStepLength = GetBaseStepLength(theDataSet,
324 vtkFloatingPointType aBasePropTime = GetBasePropagationTime(theDataSet);
325 VISU_MaskPointsFilter *aPointsFilter = VISU_MaskPointsFilter::New();
326 aPointsFilter->SetInput(theDataSet);
327 vtkDataSet* aDataSet = aPointsFilter->GetOutput();
329 size_t aRes = FindPossibleParams(aDataSet,
333 aPointsFilter->Delete();
338 //----------------------------------------------------------------------------
341 ::GetIntegrationStep()
343 return myStream->GetIntegrationStepLength();
347 //----------------------------------------------------------------------------
352 return myStream->GetStepLength();
356 //----------------------------------------------------------------------------
359 ::GetPropagationTime()
361 return myStream->GetMaximumPropagationTime();
365 //----------------------------------------------------------------------------
370 return myStream->GetIntegrationDirection();
374 //----------------------------------------------------------------------------
377 ::GetMinIntegrationStep(vtkDataSet* theDataSet,
378 vtkFloatingPointType thePercents)
383 theDataSet->Update();
386 vtkFloatingPointType aVolume = 1.0;
387 vtkFloatingPointType* aBounds = theDataSet->GetBounds();
388 for(int i = 0, j = 0; i < 3; ++i, j = 2*i){
389 vtkFloatingPointType tmp = aBounds[j+1] - aBounds[j];
397 return 0.0; // absolutely empty object
399 vtkFloatingPointType anStepLength = GetMaxIntegrationStep(theDataSet)/aCoeffOfIntStep;
400 // 0020724: last division has been commented, seems to be a logical mistake (to discuss with APO)
401 vtkFloatingPointType aBasePropTime = GetBasePropagationTime(theDataSet); // /GetVelocityCoeff(theDataSet)
403 vtkIdType aNbOfPoints = theDataSet->GetNumberOfPoints();
404 vtkFloatingPointType aSize = GetNecasseryMemorySize(aNbOfPoints,anStepLength,aBasePropTime,thePercents);
405 size_t aRealSize = GetAvailableMemory(aSize);
406 vtkFloatingPointType anAverageVolume = aVolume / aRealSize;
407 vtkFloatingPointType aStep = pow(double(anAverageVolume), double(1.0/double(degree)));
412 //----------------------------------------------------------------------------
415 ::GetMinIntegrationStep()
417 return GetMinIntegrationStep(GetExtractorFilter()->GetOutput(), GetUsedPoints());
421 //----------------------------------------------------------------------------
424 ::GetMaxIntegrationStep(vtkDataSet* theDataSet)
429 theDataSet->Update();
431 vtkFloatingPointType aLength = theDataSet->GetLength();
432 vtkFloatingPointType* aBounds = theDataSet->GetBounds();
433 vtkFloatingPointType aMaxSizeY = (aBounds[3]-aBounds[2])/aLength;
434 vtkFloatingPointType aMaxSizeZ = (aBounds[5]-aBounds[4])/aLength;
435 vtkFloatingPointType aMinMax = (aBounds[1] - aBounds[0])/aLength;
436 if (aMinMax < EPS || (aMaxSizeY < aMinMax && aMaxSizeY > EPS))
438 if (aMinMax < EPS || (aMaxSizeZ < aMinMax && aMaxSizeZ > EPS))
440 return aMinMax*aLength/2.0;
444 //----------------------------------------------------------------------------
447 ::GetMaxIntegrationStep()
449 return GetMaxIntegrationStep(GetExtractorFilter()->GetOutput());
453 //----------------------------------------------------------------------------
456 ::GetBaseIntegrationStep(vtkDataSet* theDataSet,
457 vtkFloatingPointType thePercents)
459 theDataSet->Update();
461 vtkFloatingPointType aMaxIntegrationStep = GetMaxIntegrationStep(theDataSet);
462 vtkFloatingPointType anIntegrationStep = aMaxIntegrationStep / aCoeffOfIntStep;
463 vtkFloatingPointType aMinMax = theDataSet->GetLength() / theDataSet->GetNumberOfPoints();
464 if(aMinMax > anIntegrationStep)
465 anIntegrationStep = (anIntegrationStep*aCoeffOfIntStep*0.9+aMinMax)/aCoeffOfIntStep;
467 vtkFloatingPointType aMinIntegrationStep = GetMinIntegrationStep(theDataSet, thePercents);
468 if(aMinIntegrationStep > anIntegrationStep)
469 anIntegrationStep = aMinIntegrationStep;
471 return anIntegrationStep;
475 //----------------------------------------------------------------------------
478 ::CorrectIntegrationStep(vtkFloatingPointType theStep,
479 vtkDataSet* theDataSet,
480 vtkFloatingPointType thePercents)
482 theDataSet->Update();
484 vtkFloatingPointType aMinIntegrationStep = GetMinIntegrationStep(theDataSet, thePercents);
485 if(aMinIntegrationStep > theStep)
486 theStep = aMinIntegrationStep;
488 vtkFloatingPointType aMaxIntegrationStep = GetMaxIntegrationStep(theDataSet);
489 if(aMaxIntegrationStep < theStep)
490 theStep = aMaxIntegrationStep;
496 //----------------------------------------------------------------------------
499 ::GetMinPropagationTime(vtkDataSet* theDataSet,
500 vtkFloatingPointType thePercents)
505 return GetMinStepLength(theDataSet, thePercents);
509 //----------------------------------------------------------------------------
512 ::GetMinPropagationTime()
514 return GetMinPropagationTime(GetExtractorFilter()->GetOutput(), GetUsedPoints());
518 //----------------------------------------------------------------------------
521 ::GetMaxPropagationTime(vtkDataSet* theDataSet)
526 return GetBasePropagationTime(theDataSet)*aMinNbOfSteps;
530 //----------------------------------------------------------------------------
533 ::GetMaxPropagationTime()
535 return GetMaxPropagationTime(GetExtractorFilter()->GetOutput());
539 //----------------------------------------------------------------------------
542 ::CorrectPropagationTime(vtkFloatingPointType thePropagationTime,
543 vtkDataSet* theDataSet,
544 vtkFloatingPointType thePercents)
546 vtkFloatingPointType aMinPropagationTime = GetMinPropagationTime(theDataSet, thePercents);
547 if(aMinPropagationTime > thePropagationTime)
548 thePropagationTime = aMinPropagationTime;
550 vtkFloatingPointType aMaxPropagationTime = GetMaxPropagationTime(theDataSet);
551 if(aMaxPropagationTime < thePropagationTime)
552 thePropagationTime = aMaxPropagationTime;
554 return thePropagationTime;
558 //----------------------------------------------------------------------------
561 ::GetBasePropagationTime(vtkDataSet* theDataSet)
566 theDataSet->Update();
567 vtkFloatingPointType aPropagationTime = theDataSet->GetLength() / GetVelocityCoeff(theDataSet);
569 return aPropagationTime;
573 //----------------------------------------------------------------------------
576 ::GetBasePropagationTime()
578 return GetBasePropagationTime(GetExtractorFilter()->GetOutput());
582 //----------------------------------------------------------------------------
585 ::GetMinStepLength(vtkDataSet* theDataSet,
586 vtkFloatingPointType thePercents)
588 static vtkFloatingPointType aNbOfStepsOfIntStep = 1.0E+1;
589 vtkFloatingPointType anIntStep = GetMinIntegrationStep(theDataSet, thePercents);
590 vtkFloatingPointType aStepLength = anIntStep * aNbOfStepsOfIntStep / GetVelocityCoeff(theDataSet);
595 //----------------------------------------------------------------------------
600 return GetMinStepLength(GetExtractorFilter()->GetOutput(), GetUsedPoints());
604 //----------------------------------------------------------------------------
607 ::GetMaxStepLength(vtkDataSet* theDataSet)
609 vtkFloatingPointType aStepLength = GetBasePropagationTime(theDataSet);
614 //----------------------------------------------------------------------------
619 return GetMaxStepLength(GetExtractorFilter()->GetOutput());
623 //----------------------------------------------------------------------------
626 ::CorrectStepLength(vtkFloatingPointType theStep,
627 vtkDataSet* theDataSet,
628 vtkFloatingPointType thePercents)
630 vtkFloatingPointType aMinStep = GetMinStepLength(theDataSet, thePercents);
631 if(theStep < aMinStep)
634 vtkFloatingPointType aMaxStep = GetMaxStepLength(theDataSet);
635 if(theStep > aMaxStep)
642 //----------------------------------------------------------------------------
645 ::GetBaseStepLength(vtkDataSet* theDataSet,
646 vtkFloatingPointType thePercents)
648 static vtkFloatingPointType anAvgNbOfSteps = 1.0E+2;
649 vtkFloatingPointType aPropagationTime = GetBasePropagationTime(theDataSet);
650 vtkFloatingPointType aStepLength = aPropagationTime/anAvgNbOfSteps;
651 aStepLength = CorrectStepLength(aStepLength,theDataSet,thePercents);
657 //----------------------------------------------------------------------------
660 ::GetUsedPointsDefault()
662 SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
663 return aResourceMgr->doubleValue("VISU", "stream_lines_used_points", 0.01);
667 //----------------------------------------------------------------------------
674 vtkDataSet* aDataSet = GetExtractorFilter()->GetOutput();
675 vtkFloatingPointType anIntStep = GetBaseIntegrationStep(aDataSet, GetUsedPoints());
676 vtkFloatingPointType aPropagationTime = GetBasePropagationTime(aDataSet);
677 vtkFloatingPointType aStepLength = GetBaseStepLength(aDataSet, GetUsedPoints());
686 //----------------------------------------------------------------------------
693 VISU::CellDataToPoint(myStream,
694 myCellDataToPointData,
697 myGeomFilter->SetInput(myStream->GetOutput());
698 myGeomFilter->ExtentClippingOn();
702 //----------------------------------------------------------------------------
707 return myGeomFilter->GetOutput();
711 //----------------------------------------------------------------------------
717 Superclass::Update();
719 vtkFloatingPointType aBounds[6];
720 GetMergedInput()->GetBounds(aBounds);
721 myGeomFilter->SetExtent(aBounds);
723 // std::string aFileName = std::string(getenv("HOME"))+"/"+getenv("USER")+"-myStream.vtk";
724 // VISU::WriteToFile(myStream->GetOutput(), aFileName);
726 }catch(std::exception& exc){
727 MSG(true, "Follow exception was occured :\n"<<exc.what());
729 MSG(MYDEBUG,"Unknown exception was occured\n");
734 //----------------------------------------------------------------------------
739 unsigned long int aSize = Superclass::GetMemorySize();
741 if(vtkDataSet* aDataSet = myStream->GetOutput())
742 aSize += aDataSet->GetActualMemorySize() * 1024;
744 if(vtkDataSet* aDataSet = myGeomFilter->GetOutput())
745 aSize += aDataSet->GetActualMemorySize() * 1024;
747 if(myCellDataToPointData->GetInput())
748 if(vtkDataSet* aDataSet = myCellDataToPointData->GetOutput())
749 aSize += aDataSet->GetActualMemorySize() * 1024;
755 //----------------------------------------------------------------------------
758 ::SetMapScale(vtkFloatingPointType theMapScale)
760 Superclass::SetMapScale(theMapScale);
764 //----------------------------------------------------------------------------