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_PipeLine.cxx
25 // Author: Alexey PETROV
28 #include "VISU_CutPlanesPL.hxx"
29 #include "VISU_FieldTransform.hxx"
30 #include "VISU_PipeLineUtils.hxx"
31 #include "VTKViewer_GeometryFilter.h"
32 #include "VISU_MapperHolder.hxx"
33 #include "VISU_DeformationPL.hxx"
35 #include <vtkAppendPolyData.h>
36 #include <vtkCutter.h>
39 //#include <vtkUnstructuredGrid.h>
41 static vtkFloatingPointType EPS = 1.0E-3;
44 static int MYDEBUG = 0;
46 static int MYDEBUG = 0;
50 //----------------------------------------------------------------------------
51 vtkStandardNewMacro(VISU_CutPlanesPL);
54 //----------------------------------------------------------------------------
57 VISU_OptionalDeformationPL()
59 if(MYDEBUG) MESSAGE("VISU_CutPlanesPL()::VISU_CutPlanesPL() - "<<this);
61 SetIsShrinkable(false);
62 SetIsFeatureEdgesAllowed(false);
64 SetElnoDisassembleState( true );
66 myAppendPolyData = vtkAppendPolyData::New();
73 myDisplacement[0] = myDisplacement[1] = 0.5;
75 myAng[0][0] = myAng[0][1] = myAng[0][2] = 0.0;
76 myAng[1][0] = myAng[1][1] = myAng[1][2] = 0.0;
77 UseDeformation(false);
81 //----------------------------------------------------------------------------
85 if(MYDEBUG) MESSAGE("VISU_CutPlanesPL()::~VISU_CutPlanesPL() - "<<this);
86 myAppendPolyData->Delete();
87 myAppendPolyData = NULL;
91 //----------------------------------------------------------------------------
96 unsigned long int aTime = Superclass::GetMTime();
99 aTime = std::max(aTime, VISU_OptionalDeformationPL::GetMTime());
102 aTime = std::max(aTime, myAppendPolyData->GetMTime());
108 //----------------------------------------------------------------------------
111 ::DoShallowCopy(VISU_PipeLine *thePipeLine,
114 Superclass::DoShallowCopy(thePipeLine, theIsCopyInput);
116 if(VISU_CutPlanesPL *aPipeLine = dynamic_cast<VISU_CutPlanesPL*>(thePipeLine)){
118 SetOrientation(aPipeLine->GetPlaneOrientation(),
119 aPipeLine->GetRotateX(),
120 aPipeLine->GetRotateY());
122 SetDisplacement(aPipeLine->GetDisplacement());
124 SetNbParts(aPipeLine->GetNbParts());
125 for (int i = 0, iEnd = GetNbParts(); i < iEnd; i++)
126 if(!aPipeLine->IsPartDefault(i))
127 SetPartPosition(i, aPipeLine->GetPartPosition(i));
132 //----------------------------------------------------------------------------
140 myDisplacement[0] = 0.5;
141 myAng[0][0] = myAng[0][1] = myAng[0][2] = 0.0;
142 SetScale(VISU_DeformationPL::GetDefaultScaleFactor(this));
146 //----------------------------------------------------------------------------
151 return GetWarpVectorOutput();
155 //----------------------------------------------------------------------------
160 vtkDataSet* aMergedInput = GetMergedInput();
161 if(VISU::IsQuadraticData(aMergedInput)) // Bug 0020123, note 0005343
162 throw std::runtime_error("Impossible to build presentation");
164 ClearAppendPolyData(myAppendPolyData);
167 if(!myVectorialField || !IsDeformed()){
168 SetMergeFilterInput(aMergedInput,aMergedInput);
172 if(VISU::IsDataOnCells(aMergedInput))
173 GetMapper()->SetScalarModeToUseCellData();
175 GetMapper()->SetScalarModeToUsePointData();
179 vtkFloatingPointType aDir[3];
184 vtkFloatingPointType aBounds[6];
186 vtkDataSet* aFilterOutput = GetMergeFilterOutput();
188 aFilterOutput->GetBounds(aBounds);
190 CutWithPlanes(myAppendPolyData,
201 SetWarpVectorInput(myAppendPolyData->GetOutput());
202 Superclass::Update();
206 //----------------------------------------------------------------------------
211 unsigned long int aSize = Superclass::GetMemorySize();
213 if(vtkDataSet* aDataSet = myAppendPolyData->GetOutput())
214 aSize += aDataSet->GetActualMemorySize() * 1024;
216 int anEnd = myAppendPolyData->GetNumberOfInputConnections(0);
217 for(int anId = 0; anId < anEnd; anId++)
218 if(vtkDataSet* aDataSet = myAppendPolyData->GetInput(anId))
219 aSize += aDataSet->GetActualMemorySize() * 1024;
225 //----------------------------------------------------------------------------
228 ::SetPartPosition(int theNum)
230 for(int i = 0; i < myNbParts; i++)
231 myPartPosition[i] = GetPartPosition(i,theNum);
236 ::ClearAppendPolyData(vtkAppendPolyData *theAppendPolyData)
238 theAppendPolyData->RemoveAllInputs();
242 //----------------------------------------------------------------------------
243 vtkFloatingPointType*
245 GetRx(vtkFloatingPointType theRx[3][3],
246 vtkFloatingPointType thaAng)
248 theRx[0][0] = 1.0; theRx[0][1] = 0.0; theRx[0][2] = 0.0;
249 theRx[1][0] = 0.0; theRx[1][1] = cos(thaAng); theRx[1][2] = -sin(thaAng);
250 theRx[2][0] = 0.0; theRx[2][1] = sin(thaAng); theRx[2][2] = cos(thaAng);
256 //----------------------------------------------------------------------------
257 vtkFloatingPointType*
259 ::GetRy(vtkFloatingPointType theRy[3][3],
260 vtkFloatingPointType thaAng)
262 theRy[0][0] = cos(thaAng); theRy[0][1] = 0.0; theRy[0][2] = sin(thaAng);
263 theRy[1][0] = 0.0; theRy[1][1] = 1.0; theRy[1][2] = 0.0;
264 theRy[2][0] = -sin(thaAng); theRy[2][1] = 0.0; theRy[2][2] = cos(thaAng);
269 //----------------------------------------------------------------------------
270 vtkFloatingPointType*
272 ::GetRz(vtkFloatingPointType theRz[3][3],
273 vtkFloatingPointType thaAng)
275 theRz[0][0] = cos(thaAng); theRz[0][1] = -sin(thaAng); theRz[0][2] = 0.0;
276 theRz[1][0] = sin(thaAng); theRz[1][1] = cos(thaAng); theRz[1][2] = 0.0;
277 theRz[2][0] = 0.0; theRz[2][1] = 0.0; theRz[2][2] = 1.0;
282 //----------------------------------------------------------------------------
285 ::CorrectPnt(vtkFloatingPointType thePnt[3],
286 const vtkFloatingPointType BoundPrj[6])
288 for(int i = 0, j = 0; i < 3; ++i, j=2*i){
289 if(thePnt[i] < BoundPrj[j]) thePnt[i] = BoundPrj[j];
290 if(thePnt[i] > BoundPrj[j+1]) thePnt[i] = BoundPrj[j+1];
295 //----------------------------------------------------------------------------
298 ::GetBoundProject(vtkFloatingPointType BoundPrj[3],
299 const vtkFloatingPointType BoundBox[6],
300 const vtkFloatingPointType Dir[3])
302 vtkFloatingPointType BoundPoints[8][3] = { {BoundBox[0],BoundBox[2],BoundBox[4]},
303 {BoundBox[1],BoundBox[2],BoundBox[4]},
304 {BoundBox[0],BoundBox[3],BoundBox[4]},
305 {BoundBox[1],BoundBox[3],BoundBox[4]},
306 {BoundBox[0],BoundBox[2],BoundBox[5]},
307 {BoundBox[1],BoundBox[2],BoundBox[5]},
308 {BoundBox[0],BoundBox[3],BoundBox[5]},
309 {BoundBox[1],BoundBox[3],BoundBox[5]}};
310 BoundPrj[0] = vtkMath::Dot(Dir,BoundPoints[0]), BoundPrj[1] = BoundPrj[0];
311 for(int i = 1; i < 8; i++){
312 vtkFloatingPointType tmp = vtkMath::Dot(Dir,BoundPoints[i]);
313 if(BoundPrj[1] < tmp) BoundPrj[1] = tmp;
314 if(BoundPrj[0] > tmp) BoundPrj[0] = tmp;
316 BoundPrj[2] = BoundPrj[1] - BoundPrj[0];
317 BoundPrj[1] = BoundPrj[0] + (1.0 - EPS)*BoundPrj[2];
318 BoundPrj[0] = BoundPrj[0] + EPS*BoundPrj[2];
319 BoundPrj[2] = BoundPrj[1] - BoundPrj[0];
323 //----------------------------------------------------------------------------
326 ::SetOrientation(const VISU_CutPlanesPL::PlaneOrientation& theOrient,
327 vtkFloatingPointType theXAng,
328 vtkFloatingPointType theYAng,
331 myBasePlane[theNum] = theOrient;
332 switch(myBasePlane[theNum]){
333 case XY: myAng[theNum][0] = theXAng; break;
334 case YZ: myAng[theNum][1] = theXAng; break;
335 case ZX: myAng[theNum][2] = theXAng; break;
337 switch(myBasePlane[theNum]){
338 case XY: myAng[theNum][1] = theYAng; break;
339 case YZ: myAng[theNum][2] = theYAng; break;
340 case ZX: myAng[theNum][0] = theYAng; break;
345 //----------------------------------------------------------------------------
346 const VISU_CutPlanesPL::PlaneOrientation&
348 ::GetPlaneOrientation(int theNum)
350 return myBasePlane[theNum];
355 ::GetRotateX(int theNum)
357 switch(myBasePlane[theNum]){
358 case XY: return myAng[theNum][0];
359 case YZ: return myAng[theNum][1];
360 case ZX: return myAng[theNum][2];
366 //----------------------------------------------------------------------------
369 ::GetRotateY(int theNum)
371 switch(myBasePlane[theNum]){
372 case XY: return myAng[theNum][1];
373 case YZ: return myAng[theNum][2];
374 case ZX: return myAng[theNum][0];
380 //----------------------------------------------------------------------------
383 ::GetDisplacement(int theNum)
385 return myDisplacement[theNum];
389 //----------------------------------------------------------------------------
392 ::SetDisplacement(vtkFloatingPointType theDisp,
395 if(VISU::CheckIsSameValue(myDisplacement[theNum], theDisp))
398 myDisplacement[theNum] = theDisp;
403 //----------------------------------------------------------------------------
406 ::SetNbParts(int theNbParts)
408 if(theNbParts > 0 && GetNbParts() != theNbParts){
409 myPartPosition.resize(theNbParts);
410 myPartCondition.resize(theNbParts, 1);
411 myNbParts = theNbParts;
417 //----------------------------------------------------------------------------
422 return myPartPosition.size();
426 //----------------------------------------------------------------------------
429 ::SetPartPosition(int thePartNumber,
430 vtkFloatingPointType thePartPosition)
432 if(thePartNumber >= myNbParts)
435 bool anIsSameValue = VISU::CheckIsSameValue(myPartPosition[thePartNumber], thePartPosition);
436 anIsSameValue &= VISU::CheckIsSameValue(myPartCondition[thePartNumber], 0);
440 myPartPosition[thePartNumber] = thePartPosition;
441 myPartCondition[thePartNumber] = 0;
446 //----------------------------------------------------------------------------
449 ::GetPartPosition(int thePartNumber,
452 if(thePartNumber >= myNbParts)
455 vtkFloatingPointType aPosition = myPartPosition[thePartNumber];
456 if(myPartCondition[thePartNumber]){
457 vtkFloatingPointType aDir[3], aBounds[6], aBoundPrj[3];
459 GetMergedInput()->GetBounds(aBounds);
461 GetMergeFilterOutput()->GetBounds(aBounds);
466 myBasePlane[theNum]);
468 GetBoundProject(aBoundPrj,
473 vtkFloatingPointType aDBoundPrj = aBoundPrj[2]/(myNbParts - 1);
474 vtkFloatingPointType aDisplacement = aDBoundPrj * myDisplacement[theNum];
475 vtkFloatingPointType aStartPosition = aBoundPrj[0] - 0.5*aDBoundPrj + aDisplacement;
476 aPosition = aStartPosition + thePartNumber*aDBoundPrj;
478 aPosition = aBoundPrj[0] + aBoundPrj[2]*myDisplacement[theNum];
485 //----------------------------------------------------------------------------
488 ::SetPartDefault(int thePartNumber)
490 if(thePartNumber >= myNbParts)
493 bool anIsSameValue = VISU::CheckIsSameValue(myPartPosition[thePartNumber], GetPartPosition(thePartNumber));
494 anIsSameValue &= VISU::CheckIsSameValue(myPartCondition[thePartNumber], 1);
498 myPartPosition[thePartNumber] = GetPartPosition(thePartNumber);
499 myPartCondition[thePartNumber] = 1;
504 //----------------------------------------------------------------------------
507 ::IsPartDefault(int thePartNumber)
509 if(thePartNumber >= myNbParts)
512 return myPartCondition[thePartNumber];
516 //----------------------------------------------------------------------------
519 ::GetDir(vtkFloatingPointType theDir[3],
520 const vtkFloatingPointType theAng[3],
521 const PlaneOrientation& theBasePlane)
524 vtkFloatingPointType aRx[3][3], aRy[3][3], aRz[3][3], aRotation[3][3];
525 switch(theBasePlane){
527 if(fabs(theAng[0]) > EPS) GetRx(aRx,theAng[0]); else vtkMath::Identity3x3(aRx);
528 if(fabs(theAng[1]) > EPS) GetRy(aRy,theAng[1]); else vtkMath::Identity3x3(aRy);
529 vtkMath::Multiply3x3(aRx,aRy,aRotation);
533 if(fabs(theAng[1]) > EPS) GetRy(aRy,theAng[1]); else vtkMath::Identity3x3(aRy);
534 if(fabs(theAng[2]) > EPS) GetRz(aRz,theAng[2]); else vtkMath::Identity3x3(aRz);
535 vtkMath::Multiply3x3(aRy,aRz,aRotation);
539 if(fabs(theAng[2]) > EPS) GetRz(aRz,theAng[2]); else vtkMath::Identity3x3(aRz);
540 if(fabs(theAng[0]) > EPS) GetRx(aRx,theAng[0]); else vtkMath::Identity3x3(aRx);
541 vtkMath::Multiply3x3(aRz,aRx,aRotation);
546 for(int i = 0; i < 3; i++)
547 theDir[i] = aRotation[i][iPlane];
551 //----------------------------------------------------------------------------
554 ::CutWithPlane(vtkAppendPolyData* theAppendPolyData,
555 vtkDataSet* theDataSet,
556 vtkFloatingPointType theDir[3],
557 vtkFloatingPointType theOrig[3])
559 vtkCutter *aCutPlane = vtkCutter::New();
560 aCutPlane->SetInput(theDataSet);
561 vtkPlane *aPlane = vtkPlane::New();
562 aPlane->SetOrigin(theOrig);
564 aPlane->SetNormal(theDir);
565 aCutPlane->SetCutFunction(aPlane);
567 theAppendPolyData->AddInput(aCutPlane->GetOutput());
572 //----------------------------------------------------------------------------
575 ::CutWithPlanes(vtkAppendPolyData* theAppendPolyData,
576 vtkDataSet* theDataSet,
578 vtkFloatingPointType theDir[3],
579 vtkFloatingPointType theBounds[6],
580 const std::vector<vtkFloatingPointType>& thePlanePosition,
581 const std::vector<int>& thePlaneCondition,
582 vtkFloatingPointType theDisplacement)
584 vtkFloatingPointType aBoundPrj[3], aOrig[3], aPosition;
585 GetBoundProject(aBoundPrj, theBounds, theDir);
587 vtkFloatingPointType aDBoundPrj = aBoundPrj[2]/(theNbPlanes - 1);
588 vtkFloatingPointType aDisplacement = aDBoundPrj*theDisplacement;
589 vtkFloatingPointType aStartPosition = aBoundPrj[0] - 0.5*aDBoundPrj + aDisplacement;
590 for (int i = 0; i < theNbPlanes; i++){
591 aPosition = aStartPosition + i*aDBoundPrj;
592 if(thePlaneCondition[i]){
593 aPosition = aStartPosition + i*aDBoundPrj;
595 aPosition = thePlanePosition[i];
596 VISU::Mul(theDir,aPosition,aOrig);
597 CutWithPlane(theAppendPolyData,theDataSet,theDir,aOrig);
600 if(thePlaneCondition[0])
601 aPosition = aBoundPrj[0] + aBoundPrj[2]*theDisplacement;
603 aPosition = thePlanePosition[0];
604 VISU::Mul(theDir,aPosition,aOrig);
605 CutWithPlane(theAppendPolyData,theDataSet,theDir,aOrig);
607 vtkPolyData *aPolyData = theAppendPolyData->GetOutput();
609 theAppendPolyData->Update();
613 //----------------------------------------------------------------------------
615 VISU_CutPlanesPL::SetVectorialField(VISU::PUnstructuredGridIDMapper theMapper)
617 if(myVectorialField == theMapper)
620 if(CheckCanDeformate(theMapper->GetOutput())){
621 myVectorialField = theMapper;
623 SetMergeFilterInput(GetMergedInput(),theMapper->GetOutput());
626 UseDeformation(false);
631 //----------------------------------------------------------------------------
632 VISU::PUnstructuredGridIDMapper VISU_CutPlanesPL::
635 return myVectorialField;
638 //----------------------------------------------------------------------------
639 void VISU_CutPlanesPL::SetMapScale(vtkFloatingPointType theMapScale){
640 Superclass::SetMapScale(theMapScale);
642 VISU_OptionalDeformationPL::SetMapScale(theMapScale);