1 // Created on: 2011-10-14
2 // Created by: Roman KOZLOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <IVtkTools_ShapeDataSource.hxx>
18 #include <IVtkOCC_ShapeMesher.hxx>
19 #include <IVtkTools_ShapeObject.hxx>
22 #include <vtkObjectFactory.h>
23 #include <vtkCellData.h>
24 #include <vtkIdTypeArray.h>
25 #include <vtkInformation.h>
26 #include <vtkPoints.h>
27 #include <vtkPolyData.h>
28 #include <vtkTransform.h>
29 #include <vtkTransformPolyDataFilter.h>
31 vtkStandardNewMacro(IVtkTools_ShapeDataSource)
33 //================================================================
34 // Function : Constructor
36 //================================================================
37 IVtkTools_ShapeDataSource::IVtkTools_ShapeDataSource()
38 : myPolyData (new IVtkVTK_ShapeData),
39 myIsFastTransformMode (Standard_False),
40 myIsTransformOnly (Standard_False)
42 this->SetNumberOfInputPorts(0);
43 this->SetNumberOfOutputPorts(1);
46 //================================================================
47 // Function : Destructor
49 //================================================================
50 IVtkTools_ShapeDataSource::~IVtkTools_ShapeDataSource()
53 //================================================================
54 // Function : SetShape
56 //================================================================
57 void IVtkTools_ShapeDataSource::SetShape (const IVtkOCC_Shape::Handle& theOccShape)
59 if (myIsFastTransformMode && !myOccShape.IsNull() &&
60 theOccShape->GetShape().IsPartner (myOccShape->GetShape() ) )
62 myIsTransformOnly = Standard_True;
66 myIsTransformOnly = Standard_False;
69 myOccShape = theOccShape;
73 //================================================================
74 // Function : GetShape
76 //================================================================
77 IVtkOCC_Shape::Handle IVtkTools_ShapeDataSource::GetShape()
82 //================================================================
83 // Function : RequestData
85 //================================================================
86 int IVtkTools_ShapeDataSource::RequestData(vtkInformation *vtkNotUsed(theRequest),
87 vtkInformationVector **vtkNotUsed(theInputVector),
88 vtkInformationVector *theOutputVector)
90 vtkSmartPointer<vtkPolyData> aPolyData = vtkPolyData::GetData (theOutputVector);
91 if (aPolyData.GetPointer() != NULL)
93 aPolyData->Allocate();
94 vtkSmartPointer<vtkPoints> aPts = vtkSmartPointer<vtkPoints>::New();
95 aPolyData->SetPoints (aPts);
97 vtkSmartPointer<vtkPolyData> aTransformedData;
98 TopoDS_Shape aShape = myOccShape->GetShape();
99 TopLoc_Location aShapeLoc = aShape.Location();
101 if (myIsTransformOnly)
103 vtkSmartPointer<vtkPolyData> aPrevData = myPolyData->getVtkPolyData();
104 if ( !aShapeLoc.IsIdentity() )
106 aTransformedData = this->transform (aPrevData, aShapeLoc);
110 aTransformedData = aPrevData;
115 IVtkOCC_Shape::Handle aShapeWrapperCopy;
116 if ( myIsFastTransformMode && !aShapeLoc.IsIdentity() )
118 // Reset location before meshing
119 aShape.Location (TopLoc_Location());
120 aShapeWrapperCopy = new IVtkOCC_Shape (aShape);
121 aShapeWrapperCopy->SetId (myOccShape->GetId());
125 aShapeWrapperCopy = myOccShape;
128 myPolyData = new IVtkVTK_ShapeData;
129 IVtkOCC_ShapeMesher::Handle aMesher = new IVtkOCC_ShapeMesher;
130 aMesher->Build (aShapeWrapperCopy, myPolyData);
131 vtkSmartPointer<vtkPolyData> aMeshData = myPolyData->getVtkPolyData();
133 if ( myIsFastTransformMode && !aShapeLoc.IsIdentity() )
135 aTransformedData = this->transform (aMeshData, aShapeLoc);
139 aTransformedData = aMeshData;
143 aPolyData->CopyStructure (aTransformedData); // Copy points and cells
144 aPolyData->CopyAttributes (aTransformedData); // Copy data arrays (sub-shapes IDs)
146 // We store the OccShape instance in a IVtkTools_ShapeObject
147 // wrapper in vtkInformation object of vtkDataObject, then pass it
148 // to the actors through pipelines, so selection logic can access
149 // OccShape easily given the actor instance.
150 IVtkTools_ShapeObject::SetShapeSource (this, aPolyData);
151 aPolyData->GetAttributes (vtkDataObject::CELL)->SetPedigreeIds (SubShapeIDs());
157 //================================================================
158 // Function : SubShapeIDs
160 //================================================================
161 vtkSmartPointer<vtkIdTypeArray> IVtkTools_ShapeDataSource::SubShapeIDs()
163 vtkSmartPointer<vtkDataArray> arr =
164 GetOutput()->GetCellData()->GetArray(IVtkVTK_ShapeData::ARRNAME_SUBSHAPE_IDS);
165 return vtkSmartPointer<vtkIdTypeArray>(
166 vtkIdTypeArray::SafeDownCast(arr.GetPointer()) );
169 //================================================================
172 //================================================================
173 IVtk_IdType IVtkTools_ShapeDataSource::GetId() const
175 return myOccShape.IsNull() ? -1 : myOccShape->GetId();
178 //================================================================
179 // Function : Contains
181 //================================================================
182 Standard_Boolean IVtkTools_ShapeDataSource::Contains (const IVtkOCC_Shape::Handle& shape) const
184 return ((myOccShape == shape) ? Standard_True : Standard_False);
187 //================================================================
188 // Function : transform
190 //================================================================
191 vtkSmartPointer<vtkPolyData> IVtkTools_ShapeDataSource::transform (vtkPolyData* theSource,
192 const gp_Trsf& theTrsf) const
194 vtkSmartPointer<vtkPolyData> aResult = vtkSmartPointer<vtkPolyData>::New();
196 vtkSmartPointer<vtkPoints> aPts = vtkSmartPointer<vtkPoints>::New();
197 aResult->SetPoints (aPts);
199 vtkSmartPointer<vtkTransform> aTransform = vtkSmartPointer<vtkTransform>::New();
200 vtkSmartPointer<vtkMatrix4x4> aMx = vtkSmartPointer<vtkMatrix4x4>::New();
201 for (Standard_Integer aRow = 0; aRow < 3; ++aRow)
202 for (Standard_Integer aCol = 0; aCol < 4; ++aCol)
204 aMx->SetElement (aRow, aCol, theTrsf.Value (aRow + 1, aCol + 1) );
207 aTransform->SetMatrix (aMx);
208 vtkSmartPointer<vtkTransformPolyDataFilter> aTrsfFilter
209 = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
211 aTrsfFilter->SetTransform (aTransform);
212 aTrsfFilter->SetInputData (theSource);
213 aTrsfFilter->Update();
215 vtkSmartPointer<vtkPolyData> aTransformed = aTrsfFilter->GetOutput();
216 aResult->CopyStructure (aTransformed); // Copy points and cells
217 aResult->CopyAttributes (aTransformed); // Copy data arrays (sub-shapes ids)