1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkBoundedPointSource.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkBoundedPointSource.h"
#include "vtkCellArray.h"
#include "vtkMath.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkFloatArray.h"
#include "vtkPointData.h"
vtkStandardNewMacro(vtkBoundedPointSource);
//----------------------------------------------------------------------------
vtkBoundedPointSource::vtkBoundedPointSource()
{
this->NumberOfPoints = 100;
this->Bounds[0] = this->Bounds[2] = this->Bounds[4] = -1.0;
this->Bounds[1] = this->Bounds[3] = this->Bounds[5] = 1.0;
this->OutputPointsPrecision = SINGLE_PRECISION;
this->ProduceCellOutput = false;
this->ProduceRandomScalars = false;
this->ScalarRange[0] = 0.0;
this->ScalarRange[1] = 1.0;
this->SetNumberOfInputPorts(0);
}
//----------------------------------------------------------------------------
int vtkBoundedPointSource::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *outputVector)
{
// get the info object
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the ouptut
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkIdType ptId;
double x[3];
vtkPoints *newPoints = vtkPoints::New();
// Set the desired precision for the points in the output.
if(this->OutputPointsPrecision == vtkAlgorithm::DOUBLE_PRECISION)
{
newPoints->SetDataType(VTK_DOUBLE);
}
else
{
newPoints->SetDataType(VTK_FLOAT);
}
// Generate the points
newPoints->SetNumberOfPoints(this->NumberOfPoints);
double xmin = (this->Bounds[0] < this->Bounds[1] ? this->Bounds[0] : this->Bounds[1]);
double xmax = (this->Bounds[0] < this->Bounds[1] ? this->Bounds[1] : this->Bounds[0]);
double ymin = (this->Bounds[2] < this->Bounds[3] ? this->Bounds[2] : this->Bounds[3]);
double ymax = (this->Bounds[2] < this->Bounds[3] ? this->Bounds[3] : this->Bounds[2]);
double zmin = (this->Bounds[4] < this->Bounds[5] ? this->Bounds[4] : this->Bounds[5]);
double zmax = (this->Bounds[4] < this->Bounds[5] ? this->Bounds[5] : this->Bounds[4]);
vtkMath *math = vtkMath::New();
for (ptId=0; ptId<this->NumberOfPoints; ptId++)
{
x[0] = math->Random(xmin,xmax);
x[1] = math->Random(ymin,ymax);
x[2] = math->Random(zmin,zmax);
newPoints->SetPoint(ptId,x);
}
output->SetPoints(newPoints);
newPoints->Delete();
// Generate the scalars if requested
if ( this->ProduceRandomScalars )
{
vtkFloatArray *scalars = vtkFloatArray::New();
scalars->SetName("RandomScalars");
scalars->SetNumberOfTuples(this->NumberOfPoints);
float *s = static_cast<float*>(scalars->GetVoidPointer(0));
double sMin = (this->ScalarRange[0] < this->ScalarRange[1] ?
this->ScalarRange[0] : this->ScalarRange[1]);
double sMax = (this->ScalarRange[0] < this->ScalarRange[1] ?
this->ScalarRange[1] : this->ScalarRange[0]);
for (ptId=0; ptId<this->NumberOfPoints; ptId++)
{
*s++ = math->Random(sMin,sMax);
}
output->GetPointData()->SetScalars(scalars);
scalars->Delete();
}
// Generate the vertices if requested
if ( this->ProduceCellOutput )
{
vtkCellArray *newVerts = vtkCellArray::New();
newVerts->Allocate(newVerts->EstimateSize(1,this->NumberOfPoints));
newVerts->InsertNextCell(this->NumberOfPoints);
for (ptId=0; ptId<this->NumberOfPoints; ptId++)
{
newVerts->InsertCellPoint(ptId);
}
output->SetVerts(newVerts);
newVerts->Delete();
}
math->Delete();
return 1;
}
//----------------------------------------------------------------------------
void vtkBoundedPointSource::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Number Of Points: " << this->NumberOfPoints << "\n";
for(int i=0;i<6;i++)
{
os << indent << "Bounds[" << i << "]: " << this->Bounds[i] << "\n";
}
os << indent << "Output Points Precision: " << this->OutputPointsPrecision << "\n";
os << indent << "Produce Cell Output: "
<< (this->ProduceCellOutput ? "On\n" : "Off\n");
os << indent << "Produce Random Scalars: "
<< (this->ProduceRandomScalars ? "On\n" : "Off\n");
os << indent << "Scalar Range (" << this->ScalarRange[0] << ","
<< this->ScalarRange[1] << ")\n";
}
|