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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCylinderSource.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 "vtkCylinderSource.h"
#include "vtkMath.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include <cmath>
vtkStandardNewMacro(vtkCylinderSource);
vtkCylinderSource::vtkCylinderSource (int res)
{
this->Resolution = res;
this->Height = 1.0;
this->Radius = 0.5;
this->Capping = 1;
this->Center[0] = this->Center[1] = this->Center[2] = 0.0;
this->OutputPointsPrecision = SINGLE_PRECISION;
this->SetNumberOfInputPorts(0);
}
int vtkCylinderSource::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()));
double angle= 2.0 * vtkMath::Pi()/this->Resolution;
int numPolys, numPts;
double xbot[3], tcbot[2], nbot[3];
double xtop[3], tctop[2], ntop[3];
double *center = this->Center;
int i, idx;
vtkIdType pts[VTK_CELL_SIZE];
vtkPoints *newPoints;
vtkFloatArray *newNormals;
vtkFloatArray *newTCoords;
vtkCellArray *newPolys;
//
// Set things up; allocate memory
//
if ( this->Capping )
{
numPts = 4*this->Resolution;
numPolys = this->Resolution + 2;
}
else
{
numPts = 2*this->Resolution;
numPolys = this->Resolution;
}
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);
}
newPoints->Allocate(numPts);
newNormals = vtkFloatArray::New();
newNormals->SetNumberOfComponents(3);
newNormals->Allocate(numPts);
newNormals->SetName("Normals");
newTCoords = vtkFloatArray::New();
newTCoords->SetNumberOfComponents(2);
newTCoords->Allocate(numPts);
newTCoords->SetName("TCoords");
newPolys = vtkCellArray::New();
newPolys->Allocate(newPolys->EstimateSize(numPolys,this->Resolution));
//
// Generate points and point data for sides
//
for (i=0; i<this->Resolution; i++)
{
// x coordinate
nbot[0] = ntop[0] = cos(i*angle);
xbot[0] = (nbot[0] * this->Radius) + center[0];
xtop[0] = (ntop[0] * this->Radius) + center[0];
tcbot[0] = tctop[0] = fabs(2.0*i/this->Resolution - 1.0);
// y coordinate
xbot[1] = 0.5 * this->Height + center[1];
xtop[1] = -0.5 * this->Height + center[1];
nbot[1] = ntop[1] = 0.0;
tcbot[1] = 0.0;
tctop[1] = 1.0;
// z coordinate
nbot[2] = ntop[2] = -sin(i*angle);
xbot[2] = (nbot[2] * this->Radius) + center[2];
xtop[2] = (ntop[2] * this->Radius) + center[2];
idx = 2*i;
newPoints->InsertPoint(idx,xbot);
newPoints->InsertPoint(idx+1,xtop);
newTCoords->InsertTuple(idx,tcbot);
newTCoords->InsertTuple(idx+1,tctop);
newNormals->InsertTuple(idx,nbot);
newNormals->InsertTuple(idx+1,ntop);
}
//
// Generate polygons for sides
//
for (i=0; i<this->Resolution; i++)
{
pts[0] = 2*i;
pts[1] = pts[0] + 1;
pts[2] = (pts[1] + 2) % (2*this->Resolution);
pts[3] = pts[2] - 1;
newPolys->InsertNextCell(4,pts);
}
//
// Generate points and point data for top/bottom polygons
//
if ( this->Capping )
{
for (i=0; i<this->Resolution; i++)
{
// x coordinate
xbot[0] = xtop[0] = this->Radius * cos(i*angle);
nbot[0] = ntop[0] = 0.0;
tcbot[0] = tctop[0] = xbot[0];
xbot[0] += center[0]; xtop[0] += center[0];
// y coordinate
xbot[1] = 0.5 * this->Height;
xtop[1] = -0.5 * this->Height;
nbot[1] = 1.0;
ntop[1] = -1.0;
xbot[1] += center[1]; xtop[1] += center[1];
// z coordinate
xbot[2] = xtop[2] = -this->Radius * sin(i*angle);
tcbot[1] = tctop[1] = xbot[2];
xbot[2] += center[2]; xtop[2] += center[2];
nbot[2] = 0.0;
ntop[2] = 0.0;
idx = 2*this->Resolution;
newPoints->InsertPoint(idx+i,xbot);
newTCoords->InsertTuple(idx+i,tcbot);
newNormals->InsertTuple(idx+i,nbot);
idx = 3*this->Resolution;
newPoints->InsertPoint(idx+this->Resolution-i-1,xtop);
newTCoords->InsertTuple(idx+this->Resolution-i-1,tctop);
newNormals->InsertTuple(idx+this->Resolution-i-1,ntop);
}
//
// Generate polygons for top/bottom polygons
//
for (i=0; i<this->Resolution; i++)
{
pts[i] = 2*this->Resolution + i;
}
newPolys->InsertNextCell(this->Resolution,pts);
for (i=0; i<this->Resolution; i++)
{
pts[i] = 3*this->Resolution + i;
}
newPolys->InsertNextCell(this->Resolution,pts);
} // if capping
//
// Update ourselves and release memory
//
output->SetPoints(newPoints);
newPoints->Delete();
output->GetPointData()->SetNormals(newNormals);
newNormals->Delete();
output->GetPointData()->SetTCoords(newTCoords);
newTCoords->Delete();
newPolys->Squeeze(); // since we've estimated size; reclaim some space
output->SetPolys(newPolys);
newPolys->Delete();
return 1;
}
void vtkCylinderSource::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Resolution: " << this->Resolution << "\n";
os << indent << "Height: " << this->Height << "\n";
os << indent << "Radius: " << this->Radius << "\n";
os << indent << "Center: (" << this->Center[0] << ", "
<< this->Center[1] << ", " << this->Center[2] << " )\n";
os << indent << "Capping: " << (this->Capping ? "On\n" : "Off\n");
os << indent << "Output Points Precision: " << this->OutputPointsPrecision << "\n";
}
|