1 // Copyright (C) 2010-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "vtkTableTo3D.h"
22 #include "vtkSmartPointer.h"
23 #include "vtkDoubleArray.h"
24 #include "vtkVariantArray.h"
25 #include "vtkObjectFactory.h"
26 #include "vtkPointData.h"
27 #include "vtkPoints.h"
28 #include "vtkPolyData.h"
30 #include "vtkInformation.h"
31 #include "vtkStructuredGrid.h"
32 #include "vtkStructuredGridGeometryFilter.h"
33 #include "vtkWarpScalar.h"
34 #include "vtkContourFilter.h"
36 vtkStandardNewMacro(vtkTableTo3D);
37 //vtkCxxRevisionMacro(vtkTableTo3D, "$Revision$");
40 vtkTableTo3D::vtkTableTo3D()
42 this->ScaleFactor = 1.0;
43 this->UseOptimusScale = true;
44 this->PresentationType = TABLETO3D_SURFACE;
45 this->NumberOfContours = 32;
48 vtkTableTo3D::~vtkTableTo3D()
52 int vtkTableTo3D::FillInputPortInformation(
53 int vtkNotUsed(port), vtkInformation* info)
55 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTable");
59 int vtkTableTo3D::RequestData(vtkInformation* vtkNotUsed(request),
60 vtkInformationVector** inputVector, vtkInformationVector* outputVector)
62 vtkTable* input = vtkTable::GetData(inputVector[0], 0);
63 vtkPolyData* output = vtkPolyData::GetData(outputVector, 0);
65 if (input->GetNumberOfRows() == 0 ||input->GetNumberOfColumns() < 2)
70 vtkIdType xSize = input->GetNumberOfRows();
71 vtkIdType ySize = input->GetNumberOfColumns() - 1;
72 vtkIdType nbPoints = xSize * ySize;
74 vtkDataArray* xAxis = vtkDataArray::SafeDownCast(input->GetColumn(0));
77 vtkErrorMacro("The first column is not numeric.");
81 double xRange = xAxis->GetTuple1(xSize - 1) - xAxis->GetTuple1(0);
82 double yDelta = xRange / ySize;
84 vtkSmartPointer<vtkDoubleArray> yAxis =
85 vtkSmartPointer<vtkDoubleArray>::New();
86 yAxis->SetNumberOfValues(ySize);
87 for (vtkIdType i = 0; i < ySize; i++ )
89 yAxis->SetValue(i, i*yDelta);
92 vtkSmartPointer<vtkPoints> points =
93 vtkSmartPointer<vtkPoints>::New();
94 points->SetNumberOfPoints(nbPoints);
96 vtkSmartPointer<vtkIntArray> pointsIdMapper =
97 vtkSmartPointer<vtkIntArray>::New();
98 pointsIdMapper->SetName("POINTS_ID_MAPPER");
99 pointsIdMapper->SetNumberOfComponents(2);
100 pointsIdMapper->SetNumberOfTuples(nbPoints);
101 int *pointsIdMapperPtr = pointsIdMapper->GetPointer(0);
103 for (vtkIdType i = 0, pntId = 0; i < ySize; i++)
105 for (vtkIdType j = 0; j < xSize; j++, pntId++)
107 points->SetPoint(pntId, xAxis->GetTuple1(j),
111 *pointsIdMapperPtr++ = pntId;
112 *pointsIdMapperPtr++ = 0;
116 vtkSmartPointer<vtkDoubleArray> scalars =
117 vtkSmartPointer<vtkDoubleArray>::New();
118 scalars->SetNumberOfComponents(1);
119 scalars->SetNumberOfTuples(nbPoints);
120 double *scalarsPtr = scalars->GetPointer(0);
121 for (vtkIdType i = 0; i < ySize; i++)
124 vtkDataArray::SafeDownCast(input->GetColumn(i + 1));
128 vtkErrorMacro("Column "<< i <<"is not numeric.");
132 for ( vtkIdType j = 0; j < xSize; j++ )
134 double value = col->GetTuple1(j);
135 *scalarsPtr++ = value;
139 vtkSmartPointer<vtkStructuredGrid> structuredGrid =
140 vtkSmartPointer<vtkStructuredGrid>::New();
141 structuredGrid->SetPoints(points);
143 structuredGrid->SetDimensions(xSize, ySize, 1);
145 // structuredGrid->GetPointData()->AddArray(pointsIdMapper);
146 if (input->GetInformation()->Has(vtkDataObject::FIELD_NAME()))
148 scalars->SetName(input->GetInformation()->Get(vtkDataObject::FIELD_NAME()));
152 scalars->SetName("Table");
154 structuredGrid->GetPointData()->SetScalars(scalars);
156 vtkSmartPointer<vtkStructuredGridGeometryFilter> geomFilter =
157 vtkSmartPointer<vtkStructuredGridGeometryFilter>::New();
158 geomFilter->SetInputData(structuredGrid);
159 geomFilter->Update();
161 vtkSmartPointer<vtkWarpScalar> warpScalar =
162 vtkSmartPointer<vtkWarpScalar>::New();
164 double scaleFactor = this->ScaleFactor;
165 if (this->UseOptimusScale)
168 geomFilter->GetOutput()->GetScalarRange(range);
169 double length = geomFilter->GetOutput()->GetLength();
172 scaleFactor = length / range[1] * 0.3;
180 if (this->PresentationType == TABLETO3D_SURFACE)
182 warpScalar->SetInputConnection(geomFilter->GetOutputPort(0));
183 warpScalar->SetScaleFactor(scaleFactor);
187 vtkSmartPointer<vtkContourFilter> contourFilter =
188 vtkSmartPointer<vtkContourFilter>::New();
189 contourFilter->SetInputConnection(geomFilter->GetOutputPort(0));
190 contourFilter->GenerateValues(this->NumberOfContours,
191 geomFilter->GetOutput()->GetScalarRange());
192 warpScalar->SetInputConnection(contourFilter->GetOutputPort(0));
193 warpScalar->SetScaleFactor(scaleFactor);
196 warpScalar->Update();
197 output->ShallowCopy(warpScalar->GetPolyDataOutput());
202 //----------------------------------------------------------------------------
203 void vtkTableTo3D::PrintSelf(ostream& os, vtkIndent indent)
205 this->Superclass::PrintSelf(os, indent);
207 os << indent << "ScaleFactor: " << this->ScaleFactor << endl;
208 os << indent << "UseOptimusScale: "
209 << (this->UseOptimusScale? "true" : "false") << endl;
210 os << indent << "PresentationType: "
211 << ((this->PresentationType == TABLETO3D_SURFACE)? "Surface" : "Contour")
213 os << indent << "NumberOfContours: " << this->NumberOfContours << endl;