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"
42 #include <vtkDataSet.h>
45 static int MYDEBUG = 0;
47 static int MYDEBUG = 0;
50 static vtkFloatingPointType EPS = 1.0e-7;
51 static vtkFloatingPointType aMinNbOfSteps = 1.0E+2;
52 //static vtkFloatingPointType aMaxNbOfSteps = 1.0E+3;
53 static vtkFloatingPointType aCoeffOfIntStep = 1.0E+1;
56 //----------------------------------------------------------------------------
57 vtkStandardNewMacro(VISU_StreamLinesPL);
60 //----------------------------------------------------------------------------
62 ::VISU_StreamLinesPL()
64 SetIsShrinkable(false);
65 SetIsFeatureEdgesAllowed(false);
67 myStream = VISU_StreamLine::New();
68 myCenters = vtkCellCenters::New();
69 myGeomFilter = VTKViewer_GeometryFilter::New();
70 myPointsFilter = VISU_MaskPointsFilter::New();
76 //----------------------------------------------------------------------------
78 ::~VISU_StreamLinesPL()
80 myPointsFilter->Delete();
81 myPointsFilter = NULL;
86 myGeomFilter->Delete();
94 //----------------------------------------------------------------------------
99 unsigned long int aTime = Superclass::GetMTime();
101 aTime = std::max(aTime, myStream->GetMTime());
102 aTime = std::max(aTime, myCenters->GetMTime());
103 aTime = std::max(aTime, myGeomFilter->GetMTime());
104 aTime = std::max(aTime, myPointsFilter->GetMTime());
107 aTime = std::max(aTime, mySource->GetMTime());
113 //----------------------------------------------------------------------------
116 ::DoShallowCopy(VISU_PipeLine *thePipeLine,
119 Superclass::DoShallowCopy(thePipeLine, theIsCopyInput);
121 if(VISU_StreamLinesPL *aPipeLine = dynamic_cast<VISU_StreamLinesPL*>(thePipeLine)){
122 SetParams(aPipeLine->GetIntegrationStep(),
123 aPipeLine->GetPropagationTime(),
124 aPipeLine->GetStepLength(),
125 aPipeLine->GetSource(),
126 aPipeLine->GetUsedPoints(),
127 aPipeLine->GetDirection());
132 //----------------------------------------------------------------------------
135 ::GetNecasseryMemorySize(vtkIdType theNbOfPoints,
136 vtkFloatingPointType theStepLength,
137 vtkFloatingPointType thePropogationTime,
138 vtkFloatingPointType thePercents)
140 static vtkFloatingPointType aStreamPointSize = sizeof(vtkFloatingPointType)*15 + sizeof(vtkIdType)*2;
141 static vtkFloatingPointType aStreamArraySize = aStreamPointSize*1024; // == 69632
143 vtkFloatingPointType aNbCells = thePercents*theNbOfPoints*2.0;
144 vtkFloatingPointType aNbPointsPerCell = thePropogationTime/theStepLength;
145 vtkFloatingPointType aCellsSize = aNbCells*(1+aNbPointsPerCell);
146 vtkFloatingPointType aPointsSize = aCellsSize*3.0*sizeof(vtkFloatingPointType);
148 vtkFloatingPointType aConnectivitySize = aCellsSize*sizeof(vtkIdType);
149 vtkFloatingPointType aTypesSize = aNbCells*sizeof(char);
150 vtkFloatingPointType aLocationsSize = aNbCells*sizeof(int);
151 //vtkFloatingPointType aNbCellsPerPoint = aCellsSize / aNbCells - 1;
152 vtkFloatingPointType aMeshSize = aPointsSize + aConnectivitySize + aTypesSize + aLocationsSize;
154 vtkFloatingPointType anAssignedDataSize = aCellsSize*4.0*sizeof(vtkFloatingPointType);
155 vtkFloatingPointType anOutputDataSetSize = aMeshSize + anAssignedDataSize;
157 vtkFloatingPointType aResult = aStreamArraySize*aNbCells + anOutputDataSetSize;
162 //----------------------------------------------------------------------------
165 ::FindPossibleParams(vtkDataSet* theDataSet,
166 vtkFloatingPointType& theStepLength,
167 vtkFloatingPointType& thePropogationTime,
168 vtkFloatingPointType& thePercents)
170 static vtkFloatingPointType aPercentsDecrease = 3.0, aStepLengthIncrease = 9.0;
171 vtkIdType aNbOfPoints = theDataSet->GetNumberOfPoints();
172 vtkFloatingPointType aSize = GetNecasseryMemorySize(aNbOfPoints,theStepLength,thePropogationTime,thePercents);
173 size_t anIsPossible = CheckAvailableMemory(aSize);
175 vtkFloatingPointType aMaxStepLength = std::max(GetMaxStepLength(theDataSet),thePropogationTime);
176 vtkFloatingPointType aMinStepLength = GetMinStepLength(theDataSet);
177 vtkFloatingPointType aDeltaStepLength = (aMaxStepLength - aMinStepLength)/aStepLengthIncrease;
178 for(int i = 2, aStepChanged = 1, aPerecentsChanged = 1; aStepChanged || aPerecentsChanged; i++){
179 vtkFloatingPointType aStepLength = theStepLength + aDeltaStepLength;
180 if(aStepLength < aMaxStepLength) theStepLength = aStepLength;
181 else if(aStepChanged){
182 aStepLength = aMaxStepLength;
185 vtkFloatingPointType aPercents = thePercents /= aPercentsDecrease;
186 if(aPercents*aNbOfPoints > 1) thePercents = aPercents;
187 else if(aPerecentsChanged) {
188 thePercents = 1.1 / aNbOfPoints;
189 aPerecentsChanged = 0;
191 aSize = GetNecasseryMemorySize(aNbOfPoints,theStepLength,thePropogationTime,thePercents);
192 if(CheckAvailableMemory(aSize)){
198 if(MYDEBUG) MESSAGE("FindPossibleParams - aSize = "<<aSize<<"; anIsPossible = "<<anIsPossible);
203 //----------------------------------------------------------------------------
206 ::SetParams(vtkFloatingPointType theIntStep,
207 vtkFloatingPointType thePropogationTime,
208 vtkFloatingPointType theStepLength,
209 vtkPointSet* theSource,
210 vtkFloatingPointType thePercents,
213 vtkPointSet* aDataSet = theSource? theSource: GetMergedInput();
216 vtkIdType aNbOfPoints = aDataSet->GetNumberOfPoints();
217 vtkDataSet* aPointSet = GetExtractorFilter()->GetOutput();
218 if (thePercents * aNbOfPoints < 1)
219 thePercents = 2.0 / aNbOfPoints;
221 theIntStep = CorrectIntegrationStep(theIntStep,
225 thePropogationTime = CorrectPropagationTime(thePropogationTime,
228 theStepLength = CorrectStepLength(theStepLength,
231 size_t anIsAccepted = FindPossibleParams(aPointSet,
237 mySource = theSource;
238 myPercents = thePercents;
239 if(VISU::IsDataOnCells(GetMergedInput())){
240 myCenters->SetInput(aDataSet);
241 myCenters->VertexCellsOn();
242 aDataSet = myCenters->GetOutput();
244 myPointsFilter->SetInput(aDataSet);
245 myPointsFilter->SetPercentsOfUsedPoints(thePercents);
246 aDataSet = myPointsFilter->GetOutput();
247 myStream->SetSource(aDataSet);
248 myStream->SetIntegrationStepLength(theIntStep);
249 myStream->SetMaximumPropagationTime(thePropogationTime);
250 myStream->SetStepLength(theStepLength);
251 myStream->SetSavePointInterval(theIntStep*aMinNbOfSteps);
252 myStream->SetIntegrationDirection(theDirection);
253 myStream->Modified();
260 //----------------------------------------------------------------------------
269 //----------------------------------------------------------------------------
278 //----------------------------------------------------------------------------
281 ::GetStreamerSource()
283 return myStream->GetSource();
287 //----------------------------------------------------------------------------
292 return GetVelocityCoeff(GetExtractorFilter()->GetOutput());
296 //----------------------------------------------------------------------------
299 ::GetVelocityCoeff(vtkDataSet* theDataSet)
301 vtkFloatingPointType* aScalarRange = theDataSet->GetScalarRange();
302 vtkFloatingPointType aVelocity = (fabs(aScalarRange[1]) + fabs(aScalarRange[0]))/2.0;
310 //----------------------------------------------------------------------------
313 ::IsPossible(vtkPointSet* theDataSet,
314 vtkFloatingPointType thePercents)
316 vtkFloatingPointType aStepLength = GetBaseStepLength(theDataSet);
317 vtkFloatingPointType aBasePropTime = GetBasePropagationTime(theDataSet);
318 VISU_MaskPointsFilter *aPointsFilter = VISU_MaskPointsFilter::New();
319 aPointsFilter->SetInput(theDataSet);
320 vtkDataSet* aDataSet = aPointsFilter->GetOutput();
322 size_t aRes = FindPossibleParams(aDataSet,
326 aPointsFilter->Delete();
331 //----------------------------------------------------------------------------
334 ::GetIntegrationStep()
336 return myStream->GetIntegrationStepLength();
340 //----------------------------------------------------------------------------
345 return myStream->GetStepLength();
349 //----------------------------------------------------------------------------
352 ::GetPropagationTime()
354 return myStream->GetMaximumPropagationTime();
358 //----------------------------------------------------------------------------
363 return myStream->GetIntegrationDirection();
367 //----------------------------------------------------------------------------
370 ::GetMinIntegrationStep(vtkDataSet* theDataSet,
371 vtkFloatingPointType thePercents)
376 theDataSet->Update();
379 vtkFloatingPointType aVolume = 1.0;
380 vtkFloatingPointType* aBounds = theDataSet->GetBounds();
381 for(int i = 0, j = 0; i < 3; ++i, j = 2*i){
382 vtkFloatingPointType tmp = aBounds[j+1] - aBounds[j];
390 return 0.0; // absolutely empty object
392 vtkFloatingPointType anStepLength = GetMaxIntegrationStep(theDataSet)/aCoeffOfIntStep;
393 // 0020724: last division has been commented, seems to be a logical mistake (to discuss with APO)
394 vtkFloatingPointType aBasePropTime = GetBasePropagationTime(theDataSet); // /GetVelocityCoeff(theDataSet)
396 vtkIdType aNbOfPoints = theDataSet->GetNumberOfPoints();
397 vtkFloatingPointType aSize = GetNecasseryMemorySize(aNbOfPoints,anStepLength,aBasePropTime,thePercents);
398 size_t aRealSize = GetAvailableMemory(aSize);
399 vtkFloatingPointType anAverageVolume = aVolume / aRealSize;
400 vtkFloatingPointType aStep = pow(double(anAverageVolume), double(1.0/double(degree)));
405 //----------------------------------------------------------------------------
408 ::GetMinIntegrationStep()
410 return GetMinIntegrationStep(GetExtractorFilter()->GetOutput(), GetUsedPoints());
414 //----------------------------------------------------------------------------
417 ::GetMaxIntegrationStep(vtkDataSet* theDataSet)
422 theDataSet->Update();
424 vtkFloatingPointType aLength = theDataSet->GetLength();
425 vtkFloatingPointType* aBounds = theDataSet->GetBounds();
426 vtkFloatingPointType aMaxSizeY = (aBounds[3]-aBounds[2])/aLength;
427 vtkFloatingPointType aMaxSizeZ = (aBounds[5]-aBounds[4])/aLength;
428 vtkFloatingPointType aMinMax = (aBounds[1] - aBounds[0])/aLength;
429 if (aMinMax < EPS || (aMaxSizeY < aMinMax && aMaxSizeY > EPS))
431 if (aMinMax < EPS || (aMaxSizeZ < aMinMax && aMaxSizeZ > EPS))
433 return aMinMax*aLength/2.0;
437 //----------------------------------------------------------------------------
440 ::GetMaxIntegrationStep()
442 return GetMaxIntegrationStep(GetExtractorFilter()->GetOutput());
446 //----------------------------------------------------------------------------
449 ::GetBaseIntegrationStep(vtkDataSet* theDataSet,
450 vtkFloatingPointType thePercents)
452 theDataSet->Update();
454 vtkFloatingPointType aMaxIntegrationStep = GetMaxIntegrationStep(theDataSet);
455 vtkFloatingPointType anIntegrationStep = aMaxIntegrationStep / aCoeffOfIntStep;
456 vtkFloatingPointType aMinMax = theDataSet->GetLength() / theDataSet->GetNumberOfPoints();
457 if(aMinMax > anIntegrationStep)
458 anIntegrationStep = (anIntegrationStep*aCoeffOfIntStep*0.9+aMinMax)/aCoeffOfIntStep;
460 vtkFloatingPointType aMinIntegrationStep = GetMinIntegrationStep(theDataSet, thePercents);
461 if(aMinIntegrationStep > anIntegrationStep)
462 anIntegrationStep = aMinIntegrationStep;
464 return anIntegrationStep;
468 //----------------------------------------------------------------------------
471 ::CorrectIntegrationStep(vtkFloatingPointType theStep,
472 vtkDataSet* theDataSet,
473 vtkFloatingPointType thePercents)
475 theDataSet->Update();
477 vtkFloatingPointType aMinIntegrationStep = GetMinIntegrationStep(theDataSet, thePercents);
478 if(aMinIntegrationStep > theStep)
479 theStep = aMinIntegrationStep;
481 vtkFloatingPointType aMaxIntegrationStep = GetMaxIntegrationStep(theDataSet);
482 if(aMaxIntegrationStep < theStep)
483 theStep = aMaxIntegrationStep;
489 //----------------------------------------------------------------------------
492 ::GetMinPropagationTime(vtkDataSet* theDataSet)
497 return GetMinStepLength(theDataSet);
501 //----------------------------------------------------------------------------
504 ::GetMinPropagationTime()
506 return GetMinPropagationTime(GetExtractorFilter()->GetOutput());
510 //----------------------------------------------------------------------------
513 ::GetMaxPropagationTime(vtkDataSet* theDataSet)
518 return GetBasePropagationTime(theDataSet)*aMinNbOfSteps;
522 //----------------------------------------------------------------------------
525 ::GetMaxPropagationTime()
527 return GetMaxPropagationTime(GetExtractorFilter()->GetOutput());
531 //----------------------------------------------------------------------------
534 ::CorrectPropagationTime(vtkFloatingPointType thePropagationTime,
535 vtkDataSet* theDataSet)
537 vtkFloatingPointType aMinPropagationTime = GetMinPropagationTime(theDataSet);
538 if(aMinPropagationTime > thePropagationTime)
539 thePropagationTime = aMinPropagationTime;
541 vtkFloatingPointType aMaxPropagationTime = GetMaxPropagationTime(theDataSet);
542 if(aMaxPropagationTime < thePropagationTime)
543 thePropagationTime = aMaxPropagationTime;
545 return thePropagationTime;
549 //----------------------------------------------------------------------------
552 ::GetBasePropagationTime(vtkDataSet* theDataSet)
557 theDataSet->Update();
558 vtkFloatingPointType aPropagationTime = theDataSet->GetLength() / GetVelocityCoeff(theDataSet);
560 return aPropagationTime;
564 //----------------------------------------------------------------------------
567 ::GetBasePropagationTime()
569 return GetBasePropagationTime(GetExtractorFilter()->GetOutput());
573 //----------------------------------------------------------------------------
576 ::GetMinStepLength(vtkDataSet* theDataSet)
578 static vtkFloatingPointType aNbOfStepsOfIntStep = 1.0E+1;
579 vtkFloatingPointType anIntStep = GetMinIntegrationStep(theDataSet);
580 vtkFloatingPointType aStepLength = anIntStep * aNbOfStepsOfIntStep / GetVelocityCoeff(theDataSet);
585 //----------------------------------------------------------------------------
590 return GetMinStepLength(GetExtractorFilter()->GetOutput());
594 //----------------------------------------------------------------------------
597 ::GetMaxStepLength(vtkDataSet* theDataSet)
599 vtkFloatingPointType aStepLength = GetBasePropagationTime(theDataSet);
604 //----------------------------------------------------------------------------
609 return GetMaxStepLength(GetExtractorFilter()->GetOutput());
613 //----------------------------------------------------------------------------
616 ::CorrectStepLength(vtkFloatingPointType theStep,
617 vtkDataSet* theDataSet)
619 vtkFloatingPointType aMinStep = GetMinStepLength(theDataSet);
620 if(theStep < aMinStep)
623 vtkFloatingPointType aMaxStep = GetMaxStepLength(theDataSet);
624 if(theStep > aMaxStep)
631 //----------------------------------------------------------------------------
634 ::GetBaseStepLength(vtkDataSet* theDataSet)
636 static vtkFloatingPointType anAvgNbOfSteps = 1.0E+2;
637 vtkFloatingPointType aPropagationTime = GetBasePropagationTime(theDataSet);
638 vtkFloatingPointType aStepLength = aPropagationTime/anAvgNbOfSteps;
639 aStepLength = CorrectStepLength(aStepLength,theDataSet);
645 //----------------------------------------------------------------------------
652 vtkDataSet* aDataSet = GetExtractorFilter()->GetOutput();
653 vtkFloatingPointType anIntStep = GetBaseIntegrationStep(aDataSet);
654 vtkFloatingPointType aPropagationTime = GetBasePropagationTime(aDataSet);
655 vtkFloatingPointType aStepLength = GetBaseStepLength(aDataSet);
662 //----------------------------------------------------------------------------
669 VISU::CellDataToPoint(myStream,
670 myCellDataToPointData,
673 myGeomFilter->SetInput(myStream->GetOutput());
674 myGeomFilter->ExtentClippingOn();
678 //----------------------------------------------------------------------------
683 return myGeomFilter->GetOutput();
687 //----------------------------------------------------------------------------
693 Superclass::Update();
695 vtkFloatingPointType aBounds[6];
696 GetMergedInput()->GetBounds(aBounds);
697 myGeomFilter->SetExtent(aBounds);
699 // std::string aFileName = std::string(getenv("HOME"))+"/"+getenv("USER")+"-myStream.vtk";
700 // VISU::WriteToFile(myStream->GetOutput(), aFileName);
702 }catch(std::exception& exc){
703 MSG(true, "Follow exception was occured :\n"<<exc.what());
705 MSG(MYDEBUG,"Unknown exception was occured\n");
710 //----------------------------------------------------------------------------
715 unsigned long int aSize = Superclass::GetMemorySize();
717 if(vtkDataSet* aDataSet = myStream->GetOutput())
718 aSize += aDataSet->GetActualMemorySize() * 1024;
720 if(vtkDataSet* aDataSet = myGeomFilter->GetOutput())
721 aSize += aDataSet->GetActualMemorySize() * 1024;
723 if(myCellDataToPointData->GetInput())
724 if(vtkDataSet* aDataSet = myCellDataToPointData->GetOutput())
725 aSize += aDataSet->GetActualMemorySize() * 1024;
731 //----------------------------------------------------------------------------
734 ::SetMapScale(vtkFloatingPointType theMapScale)
736 Superclass::SetMapScale(theMapScale);
740 //----------------------------------------------------------------------------