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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTexturedSphereSource.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 "vtkTexturedSphereSource.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkMath.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
vtkStandardNewMacro(vtkTexturedSphereSource);
// Construct sphere with radius=0.5 and default resolution 8 in both Phi
// and Theta directions.
vtkTexturedSphereSource::vtkTexturedSphereSource(int res)
{
res = res < 4 ? 4 : res;
this->Radius = 0.5;
this->ThetaResolution = res;
this->PhiResolution = res;
this->Theta = 0.0;
this->Phi = 0.0;
this->OutputPointsPrecision = SINGLE_PRECISION;
this->SetNumberOfInputPorts(0);
}
int vtkTexturedSphereSource::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *outputVector)
{
// get the info object
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the output
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
int i, j;
int numPts;
int numPolys;
vtkPoints *newPoints;
vtkFloatArray *newNormals;
vtkFloatArray *newTCoords;
vtkCellArray *newPolys;
double x[3], deltaPhi, deltaTheta, phi, theta, radius, norm;
vtkIdType pts[3];
double tc[2];
//
// Set things up; allocate memory
//
numPts = (this->PhiResolution + 1) * (this->ThetaResolution + 1);
// creating triangles
numPolys = this->PhiResolution * 2 * this->ThetaResolution;
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(3*numPts);
newTCoords = vtkFloatArray::New();
newTCoords->SetNumberOfComponents(2);
newTCoords->Allocate(2*numPts);
newPolys = vtkCellArray::New();
newPolys->Allocate(newPolys->EstimateSize(numPolys,3));
//
// Create sphere
//
// Create intermediate points
deltaPhi = vtkMath::Pi() / this->PhiResolution;
deltaTheta = 2.0 * vtkMath::Pi() / this->ThetaResolution;
for (i=0; i <= this->ThetaResolution; i++)
{
theta = i * deltaTheta;
tc[0] = 2.0 * theta/vtkMath::Pi();
for (j=0; j <= this->PhiResolution; j++)
{
phi = j * deltaPhi;
radius = this->Radius * sin((double)phi);
x[0] = radius * cos((double)theta);
x[1] = radius * sin((double)theta);
x[2] = this->Radius * cos((double)phi);
newPoints->InsertNextPoint(x);
if ( (norm = vtkMath::Norm(x)) == 0.0 )
{
norm = 1.0;
}
x[0] /= norm; x[1] /= norm; x[2] /= norm;
newNormals->InsertNextTuple(x);
tc[1] = 1.0 - phi/vtkMath::Pi();
newTCoords->InsertNextTuple(tc);
}
}
//
// Generate mesh connectivity
//
// bands between poles
for (i=0; i < this->ThetaResolution; i++)
{
for (j=0; j < this->PhiResolution; j++)
{
pts[0] = (this->PhiResolution+1)*i + j;
pts[1] = pts[0] + 1;
pts[2] = ((this->PhiResolution+1)*(i+1)+j) + 1;
newPolys->InsertNextCell(3,pts);
pts[1] = pts[2];
pts[2] = pts[1] - 1;
newPolys->InsertNextCell(3,pts);
}
}
//
// Update ourselves and release memeory
//
output->SetPoints(newPoints);
newPoints->Delete();
output->GetPointData()->SetNormals(newNormals);
newNormals->Delete();
output->GetPointData()->SetTCoords(newTCoords);
newTCoords->Delete();
output->SetPolys(newPolys);
newPolys->Delete();
return 1;
}
void vtkTexturedSphereSource::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Theta Resolution: " << this->ThetaResolution << "\n";
os << indent << "Phi Resolution: " << this->PhiResolution << "\n";
os << indent << "Theta: " << this->Theta << "\n";
os << indent << "Phi: " << this->Phi << "\n";
os << indent << "Radius: " << this->Radius << "\n";
os << indent << "Output Points Precision: " << this->OutputPointsPrecision
<< "\n";
}
|