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_CellCenters.h"
38 #include "VTKViewer_GeometryFilter.h"
40 #include <SUIT_Session.h>
41 #include <SUIT_ResourceMgr.h>
46 #include <vtkDataSet.h>
49 static int MYDEBUG = 0;
51 static int MYDEBUG = 0;
54 static vtkFloatingPointType EPS = 1.0e-7;
55 static vtkFloatingPointType aMinNbOfSteps = 1.0E+2;
56 //static vtkFloatingPointType aMaxNbOfSteps = 1.0E+3;
57 static vtkFloatingPointType aCoeffOfIntStep = 1.0E+1;
60 //----------------------------------------------------------------------------
61 vtkStandardNewMacro(VISU_StreamLinesPL);
64 //----------------------------------------------------------------------------
66 ::VISU_StreamLinesPL()
68 SetIsShrinkable(false);
69 SetIsFeatureEdgesAllowed(false);
71 myStream = VISU_StreamLine::New();
72 myCenters = VTKViewer_CellCenters::New();
73 myGeomFilter = VTKViewer_GeometryFilter::New();
74 myPointsFilter = VISU_MaskPointsFilter::New();
77 myPercents = GetUsedPointsDefault();
81 //----------------------------------------------------------------------------
83 ::~VISU_StreamLinesPL()
85 myPointsFilter->Delete();
86 myPointsFilter = NULL;
91 myGeomFilter->Delete();
99 //----------------------------------------------------------------------------
104 unsigned long int aTime = Superclass::GetMTime();
106 aTime = std::max(aTime, myStream->GetMTime());
107 aTime = std::max(aTime, myCenters->GetMTime());
108 aTime = std::max(aTime, myGeomFilter->GetMTime());
109 aTime = std::max(aTime, myPointsFilter->GetMTime());
112 aTime = std::max(aTime, mySource->GetMTime());
118 //----------------------------------------------------------------------------
121 ::DoShallowCopy(VISU_PipeLine *thePipeLine,
124 Superclass::DoShallowCopy(thePipeLine, theIsCopyInput);
126 if(VISU_StreamLinesPL *aPipeLine = dynamic_cast<VISU_StreamLinesPL*>(thePipeLine)){
127 SetParams(aPipeLine->GetIntegrationStep(),
128 aPipeLine->GetPropagationTime(),
129 aPipeLine->GetStepLength(),
130 aPipeLine->GetSource(),
131 aPipeLine->GetUsedPoints(),
132 aPipeLine->GetDirection());
137 //----------------------------------------------------------------------------
140 ::GetNecasseryMemorySize(vtkIdType theNbOfPoints,
141 vtkFloatingPointType theStepLength,
142 vtkFloatingPointType thePropogationTime,
143 vtkFloatingPointType thePercents)
145 static vtkFloatingPointType aStreamPointSize = sizeof(vtkFloatingPointType)*15 + sizeof(vtkIdType)*2;
146 static vtkFloatingPointType aStreamArraySize = aStreamPointSize*1024; // == 69632
148 vtkFloatingPointType aNbCells = thePercents*theNbOfPoints*2.0;
149 vtkFloatingPointType aNbPointsPerCell = thePropogationTime/theStepLength;
150 vtkFloatingPointType aCellsSize = aNbCells*(1+aNbPointsPerCell);
151 vtkFloatingPointType aPointsSize = aCellsSize*3.0*sizeof(vtkFloatingPointType);
153 vtkFloatingPointType aConnectivitySize = aCellsSize*sizeof(vtkIdType);
154 vtkFloatingPointType aTypesSize = aNbCells*sizeof(char);
155 vtkFloatingPointType aLocationsSize = aNbCells*sizeof(int);
156 //vtkFloatingPointType aNbCellsPerPoint = aCellsSize / aNbCells - 1;
157 vtkFloatingPointType aMeshSize = aPointsSize + aConnectivitySize + aTypesSize + aLocationsSize;
159 vtkFloatingPointType anAssignedDataSize = aCellsSize*4.0*sizeof(vtkFloatingPointType);
160 vtkFloatingPointType anOutputDataSetSize = aMeshSize + anAssignedDataSize;
162 vtkFloatingPointType aResult = aStreamArraySize*aNbCells + anOutputDataSetSize;
167 //----------------------------------------------------------------------------
170 ::FindPossibleParams(vtkDataSet* theDataSet,
171 vtkFloatingPointType& theStepLength,
172 vtkFloatingPointType& thePropogationTime,
173 vtkFloatingPointType& thePercents)
175 static vtkFloatingPointType aPercentsDecrease = 3.0, aStepLengthIncrease = 9.0;
176 vtkIdType aNbOfPoints = theDataSet->GetNumberOfPoints();
177 vtkFloatingPointType aSize = GetNecasseryMemorySize(aNbOfPoints,theStepLength,thePropogationTime,thePercents);
178 size_t anIsPossible = CheckAvailableMemory(aSize);
180 vtkFloatingPointType aMaxStepLength = std::max(GetMaxStepLength(theDataSet),thePropogationTime);
181 vtkFloatingPointType aMinStepLength = GetMinStepLength(theDataSet,thePercents);
182 vtkFloatingPointType aDeltaStepLength = (aMaxStepLength - aMinStepLength)/aStepLengthIncrease;
183 for(int i = 2, aStepChanged = 1, aPerecentsChanged = 1; aStepChanged || aPerecentsChanged; i++){
184 vtkFloatingPointType aStepLength = theStepLength + aDeltaStepLength;
185 if(aStepLength < aMaxStepLength) theStepLength = aStepLength;
186 else if(aStepChanged){
187 aStepLength = aMaxStepLength;
190 vtkFloatingPointType aPercents = thePercents /= aPercentsDecrease;
191 if(aPercents*aNbOfPoints > 1) thePercents = aPercents;
192 else if(aPerecentsChanged) {
193 thePercents = 1.1 / aNbOfPoints;
194 aPerecentsChanged = 0;
196 aSize = GetNecasseryMemorySize(aNbOfPoints,theStepLength,thePropogationTime,thePercents);
197 if(CheckAvailableMemory(aSize)){
203 if(MYDEBUG) MESSAGE("FindPossibleParams - aSize = "<<aSize<<"; anIsPossible = "<<anIsPossible);
208 //----------------------------------------------------------------------------
211 ::SetParams(vtkFloatingPointType theIntStep,
212 vtkFloatingPointType thePropogationTime,
213 vtkFloatingPointType theStepLength,
214 vtkPointSet* theSource,
215 vtkFloatingPointType thePercents,
218 vtkPointSet* aDataSet = theSource? theSource: GetMergedInput();
221 vtkIdType aNbOfPoints = aDataSet->GetNumberOfPoints();
222 vtkDataSet* aPointSet = GetExtractorFilter()->GetOutput();
223 if (thePercents * aNbOfPoints < 1)
224 thePercents = 2.0 / aNbOfPoints;
226 theIntStep = CorrectIntegrationStep(theIntStep,
230 thePropogationTime = CorrectPropagationTime(thePropogationTime,
234 theStepLength = CorrectStepLength(theStepLength,
238 size_t anIsAccepted = FindPossibleParams(aPointSet,
244 mySource = theSource;
245 myPercents = thePercents;
246 if(VISU::IsDataOnCells(GetMergedInput())){
247 myCenters->SetInput(aDataSet);
248 myCenters->VertexCellsOn();
249 aDataSet = myCenters->GetOutput();
251 myPointsFilter->SetInput(aDataSet);
252 myPointsFilter->SetPercentsOfUsedPoints(thePercents);
253 aDataSet = myPointsFilter->GetOutput();
254 myStream->SetSource(aDataSet);
255 myStream->SetIntegrationStepLength(theIntStep);
256 myStream->SetMaximumPropagationTime(thePropogationTime);
257 myStream->SetStepLength(theStepLength);
258 myStream->SetSavePointInterval(theIntStep*aMinNbOfSteps);
259 myStream->SetIntegrationDirection(theDirection);
260 myStream->Modified();
267 //----------------------------------------------------------------------------
276 //----------------------------------------------------------------------------
285 //----------------------------------------------------------------------------
288 ::GetStreamerSource()
290 return myStream->GetSource();
294 //----------------------------------------------------------------------------
299 return GetVelocityCoeff(GetExtractorFilter()->GetOutput());
303 //----------------------------------------------------------------------------
306 ::GetVelocityCoeff(vtkDataSet* theDataSet)
308 vtkFloatingPointType* aScalarRange = theDataSet->GetScalarRange();
309 vtkFloatingPointType aVelocity = (fabs(aScalarRange[1]) + fabs(aScalarRange[0]))/2.0;
317 //----------------------------------------------------------------------------
320 ::IsPossible(vtkPointSet* theDataSet)
322 vtkFloatingPointType aPercents = GetUsedPointsDefault();
323 vtkFloatingPointType aStepLength = GetBaseStepLength(theDataSet,
325 vtkFloatingPointType aBasePropTime = GetBasePropagationTime(theDataSet);
326 VISU_MaskPointsFilter *aPointsFilter = VISU_MaskPointsFilter::New();
327 aPointsFilter->SetInput(theDataSet);
328 vtkDataSet* aDataSet = aPointsFilter->GetOutput();
330 size_t aRes = FindPossibleParams(aDataSet,
334 aPointsFilter->Delete();
339 //----------------------------------------------------------------------------
342 ::GetIntegrationStep()
344 return myStream->GetIntegrationStepLength();
348 //----------------------------------------------------------------------------
353 return myStream->GetStepLength();
357 //----------------------------------------------------------------------------
360 ::GetPropagationTime()
362 return myStream->GetMaximumPropagationTime();
366 //----------------------------------------------------------------------------
371 return myStream->GetIntegrationDirection();
375 //----------------------------------------------------------------------------
378 ::GetMinIntegrationStep(vtkDataSet* theDataSet,
379 vtkFloatingPointType thePercents)
384 theDataSet->Update();
387 vtkFloatingPointType aVolume = 1.0;
388 vtkFloatingPointType* aBounds = theDataSet->GetBounds();
389 for(int i = 0, j = 0; i < 3; ++i, j = 2*i){
390 vtkFloatingPointType tmp = aBounds[j+1] - aBounds[j];
398 return 0.0; // absolutely empty object
400 vtkFloatingPointType anStepLength = GetMaxIntegrationStep(theDataSet)/aCoeffOfIntStep;
401 // 0020724: last division has been commented, seems to be a logical mistake (to discuss with APO)
402 vtkFloatingPointType aBasePropTime = GetBasePropagationTime(theDataSet); // /GetVelocityCoeff(theDataSet)
404 vtkIdType aNbOfPoints = theDataSet->GetNumberOfPoints();
405 vtkFloatingPointType aSize = GetNecasseryMemorySize(aNbOfPoints,anStepLength,aBasePropTime,thePercents);
406 size_t aRealSize = GetAvailableMemory(aSize);
407 vtkFloatingPointType anAverageVolume = aVolume / aRealSize;
408 vtkFloatingPointType aStep = pow(double(anAverageVolume), double(1.0/double(degree)));
413 //----------------------------------------------------------------------------
416 ::GetMinIntegrationStep()
418 return GetMinIntegrationStep(GetExtractorFilter()->GetOutput(), GetUsedPoints());
422 //----------------------------------------------------------------------------
425 ::GetMaxIntegrationStep(vtkDataSet* theDataSet)
430 theDataSet->Update();
432 vtkFloatingPointType aLength = theDataSet->GetLength();
433 vtkFloatingPointType* aBounds = theDataSet->GetBounds();
434 vtkFloatingPointType aMaxSizeY = (aBounds[3]-aBounds[2])/aLength;
435 vtkFloatingPointType aMaxSizeZ = (aBounds[5]-aBounds[4])/aLength;
436 vtkFloatingPointType aMinMax = (aBounds[1] - aBounds[0])/aLength;
437 if (aMinMax < EPS || (aMaxSizeY < aMinMax && aMaxSizeY > EPS))
439 if (aMinMax < EPS || (aMaxSizeZ < aMinMax && aMaxSizeZ > EPS))
441 return aMinMax*aLength/2.0;
445 //----------------------------------------------------------------------------
448 ::GetMaxIntegrationStep()
450 return GetMaxIntegrationStep(GetExtractorFilter()->GetOutput());
454 //----------------------------------------------------------------------------
457 ::GetBaseIntegrationStep(vtkDataSet* theDataSet,
458 vtkFloatingPointType thePercents)
460 theDataSet->Update();
462 vtkFloatingPointType aMaxIntegrationStep = GetMaxIntegrationStep(theDataSet);
463 vtkFloatingPointType anIntegrationStep = aMaxIntegrationStep / aCoeffOfIntStep;
464 vtkFloatingPointType aMinMax = theDataSet->GetLength() / theDataSet->GetNumberOfPoints();
465 if(aMinMax > anIntegrationStep)
466 anIntegrationStep = (anIntegrationStep*aCoeffOfIntStep*0.9+aMinMax)/aCoeffOfIntStep;
468 vtkFloatingPointType aMinIntegrationStep = GetMinIntegrationStep(theDataSet, thePercents);
469 if(aMinIntegrationStep > anIntegrationStep)
470 anIntegrationStep = aMinIntegrationStep;
472 return anIntegrationStep;
476 //----------------------------------------------------------------------------
479 ::CorrectIntegrationStep(vtkFloatingPointType theStep,
480 vtkDataSet* theDataSet,
481 vtkFloatingPointType thePercents)
483 theDataSet->Update();
485 vtkFloatingPointType aMinIntegrationStep = GetMinIntegrationStep(theDataSet, thePercents);
486 if(aMinIntegrationStep > theStep)
487 theStep = aMinIntegrationStep;
489 vtkFloatingPointType aMaxIntegrationStep = GetMaxIntegrationStep(theDataSet);
490 if(aMaxIntegrationStep < theStep)
491 theStep = aMaxIntegrationStep;
497 //----------------------------------------------------------------------------
500 ::GetMinPropagationTime(vtkDataSet* theDataSet,
501 vtkFloatingPointType thePercents)
506 return GetMinStepLength(theDataSet, thePercents);
510 //----------------------------------------------------------------------------
513 ::GetMinPropagationTime()
515 return GetMinPropagationTime(GetExtractorFilter()->GetOutput(), GetUsedPoints());
519 //----------------------------------------------------------------------------
522 ::GetMaxPropagationTime(vtkDataSet* theDataSet)
527 return GetBasePropagationTime(theDataSet)*aMinNbOfSteps;
531 //----------------------------------------------------------------------------
534 ::GetMaxPropagationTime()
536 return GetMaxPropagationTime(GetExtractorFilter()->GetOutput());
540 //----------------------------------------------------------------------------
543 ::CorrectPropagationTime(vtkFloatingPointType thePropagationTime,
544 vtkDataSet* theDataSet,
545 vtkFloatingPointType thePercents)
547 vtkFloatingPointType aMinPropagationTime = GetMinPropagationTime(theDataSet, thePercents);
548 if(aMinPropagationTime > thePropagationTime)
549 thePropagationTime = aMinPropagationTime;
551 vtkFloatingPointType aMaxPropagationTime = GetMaxPropagationTime(theDataSet);
552 if(aMaxPropagationTime < thePropagationTime)
553 thePropagationTime = aMaxPropagationTime;
555 return thePropagationTime;
559 //----------------------------------------------------------------------------
562 ::GetBasePropagationTime(vtkDataSet* theDataSet)
567 theDataSet->Update();
568 vtkFloatingPointType aPropagationTime = theDataSet->GetLength() / GetVelocityCoeff(theDataSet);
570 return aPropagationTime;
574 //----------------------------------------------------------------------------
577 ::GetBasePropagationTime()
579 return GetBasePropagationTime(GetExtractorFilter()->GetOutput());
583 //----------------------------------------------------------------------------
586 ::GetMinStepLength(vtkDataSet* theDataSet,
587 vtkFloatingPointType thePercents)
589 static vtkFloatingPointType aNbOfStepsOfIntStep = 1.0E+1;
590 vtkFloatingPointType anIntStep = GetMinIntegrationStep(theDataSet, thePercents);
591 vtkFloatingPointType aStepLength = anIntStep * aNbOfStepsOfIntStep / GetVelocityCoeff(theDataSet);
596 //----------------------------------------------------------------------------
601 return GetMinStepLength(GetExtractorFilter()->GetOutput(), GetUsedPoints());
605 //----------------------------------------------------------------------------
608 ::GetMaxStepLength(vtkDataSet* theDataSet)
610 vtkFloatingPointType aStepLength = GetBasePropagationTime(theDataSet);
615 //----------------------------------------------------------------------------
620 return GetMaxStepLength(GetExtractorFilter()->GetOutput());
624 //----------------------------------------------------------------------------
627 ::CorrectStepLength(vtkFloatingPointType theStep,
628 vtkDataSet* theDataSet,
629 vtkFloatingPointType thePercents)
631 vtkFloatingPointType aMinStep = GetMinStepLength(theDataSet, thePercents);
632 if(theStep < aMinStep)
635 vtkFloatingPointType aMaxStep = GetMaxStepLength(theDataSet);
636 if(theStep > aMaxStep)
643 //----------------------------------------------------------------------------
646 ::GetBaseStepLength(vtkDataSet* theDataSet,
647 vtkFloatingPointType thePercents)
649 static vtkFloatingPointType anAvgNbOfSteps = 1.0E+2;
650 vtkFloatingPointType aPropagationTime = GetBasePropagationTime(theDataSet);
651 vtkFloatingPointType aStepLength = aPropagationTime/anAvgNbOfSteps;
652 aStepLength = CorrectStepLength(aStepLength,theDataSet,thePercents);
658 //----------------------------------------------------------------------------
661 ::GetUsedPointsDefault()
663 SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
664 return aResourceMgr->doubleValue("VISU", "stream_lines_used_points", 0.01);
668 //----------------------------------------------------------------------------
675 vtkDataSet* aDataSet = GetExtractorFilter()->GetOutput();
676 vtkFloatingPointType anIntStep = GetBaseIntegrationStep(aDataSet, GetUsedPoints());
677 vtkFloatingPointType aPropagationTime = GetBasePropagationTime(aDataSet);
678 vtkFloatingPointType aStepLength = GetBaseStepLength(aDataSet, GetUsedPoints());
687 //----------------------------------------------------------------------------
694 VISU::CellDataToPoint(myStream,
695 myCellDataToPointData,
698 myGeomFilter->SetInput(myStream->GetOutput());
699 myGeomFilter->ExtentClippingOn();
703 //----------------------------------------------------------------------------
708 return myGeomFilter->GetOutput();
712 //----------------------------------------------------------------------------
718 Superclass::Update();
720 vtkFloatingPointType aBounds[6];
721 GetMergedInput()->GetBounds(aBounds);
722 myGeomFilter->SetExtent(aBounds);
724 // std::string aFileName = std::string(getenv("HOME"))+"/"+getenv("USER")+"-myStream.vtk";
725 // VISU::WriteToFile(myStream->GetOutput(), aFileName);
727 }catch(std::exception& exc){
728 MSG(true, "Follow exception was occured :\n"<<exc.what());
730 MSG(MYDEBUG,"Unknown exception was occured\n");
735 //----------------------------------------------------------------------------
740 unsigned long int aSize = Superclass::GetMemorySize();
742 if(vtkDataSet* aDataSet = myStream->GetOutput())
743 aSize += aDataSet->GetActualMemorySize() * 1024;
745 if(vtkDataSet* aDataSet = myGeomFilter->GetOutput())
746 aSize += aDataSet->GetActualMemorySize() * 1024;
748 if(myCellDataToPointData->GetInput())
749 if(vtkDataSet* aDataSet = myCellDataToPointData->GetOutput())
750 aSize += aDataSet->GetActualMemorySize() * 1024;
756 //----------------------------------------------------------------------------
759 ::SetMapScale(vtkFloatingPointType theMapScale)
761 Superclass::SetMapScale(theMapScale);
765 //----------------------------------------------------------------------------