vtkTexturedSphereSource.cxx 4.75 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1 2 3
/*=========================================================================

  Program:   Visualization Toolkit
Ken Martin's avatar
Ken Martin committed
4
  Module:    vtkTexturedSphereSource.cxx
Ken Martin's avatar
Ken Martin committed
5

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
Ken Martin's avatar
Ken Martin committed
9

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
Ken Martin's avatar
Ken Martin committed
13 14

=========================================================================*/
Ken Martin's avatar
Ken Martin committed
15
#include "vtkTexturedSphereSource.h"
16

17
#include "vtkCellArray.h"
18
#include "vtkFloatArray.h"
Ken Martin's avatar
Ken Martin committed
19
#include "vtkMath.h"
20 21
#include "vtkInformation.h"
#include "vtkInformationVector.h"
22
#include "vtkObjectFactory.h"
23
#include "vtkPointData.h"
24 25
#include "vtkPoints.h"
#include "vtkPolyData.h"
26

Brad King's avatar
Brad King committed
27
vtkStandardNewMacro(vtkTexturedSphereSource);
28

Ken Martin's avatar
Ken Martin committed
29 30 31 32 33 34 35 36
// 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;
37 38
  this->Theta = 0.0;
  this->Phi = 0.0;
39
  this->OutputPointsPrecision = SINGLE_PRECISION;
40 41

  this->SetNumberOfInputPorts(0);
Ken Martin's avatar
Ken Martin committed
42 43
}

44 45 46 47
int vtkTexturedSphereSource::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **vtkNotUsed(inputVector),
  vtkInformationVector *outputVector)
Ken Martin's avatar
Ken Martin committed
48
{
49 50 51
  // get the info object
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

52
  // get the output
53 54 55
  vtkPolyData *output = vtkPolyData::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

Ken Martin's avatar
Ken Martin committed
56 57 58
  int i, j;
  int numPts;
  int numPolys;
59
  vtkPoints *newPoints;
60 61
  vtkFloatArray *newNormals;
  vtkFloatArray *newTCoords;
Ken Martin's avatar
Ken Martin committed
62
  vtkCellArray *newPolys;
Ken Martin's avatar
Ken Martin committed
63
  double x[3], deltaPhi, deltaTheta, phi, theta, radius, norm;
64
  vtkIdType pts[3];
Ken Martin's avatar
Ken Martin committed
65
  double tc[2];
66

Ken Martin's avatar
Ken Martin committed
67 68 69 70 71 72 73 74
  //
  // Set things up; allocate memory
  //

  numPts = (this->PhiResolution + 1) * (this->ThetaResolution + 1);
  // creating triangles
  numPolys = this->PhiResolution * 2 * this->ThetaResolution;

75
  newPoints = vtkPoints::New();
76 77 78

  // Set the desired precision for the points in the output.
  if(this->OutputPointsPrecision == vtkAlgorithm::DOUBLE_PRECISION)
79
  {
80
    newPoints->SetDataType(VTK_DOUBLE);
81
  }
82
  else
83
  {
84
    newPoints->SetDataType(VTK_FLOAT);
85
  }
86

Will Schroeder's avatar
Will Schroeder committed
87
  newPoints->Allocate(numPts);
88 89 90 91 92 93
  newNormals = vtkFloatArray::New();
  newNormals->SetNumberOfComponents(3);
  newNormals->Allocate(3*numPts);
  newTCoords = vtkFloatArray::New();
  newTCoords->SetNumberOfComponents(2);
  newTCoords->Allocate(2*numPts);
94
  newPolys = vtkCellArray::New();
Ken Martin's avatar
Ken Martin committed
95 96 97 98 99
  newPolys->Allocate(newPolys->EstimateSize(numPolys,3));
  //
  // Create sphere
  //
  // Create intermediate points
100 101
  deltaPhi = vtkMath::Pi() / this->PhiResolution;
  deltaTheta = 2.0 * vtkMath::Pi() / this->ThetaResolution;
Ken Martin's avatar
Ken Martin committed
102
  for (i=0; i <= this->ThetaResolution; i++)
103
  {
Ken Martin's avatar
Ken Martin committed
104
    theta = i * deltaTheta;
105
    tc[0] = theta/(2.0 * vtkMath::Pi());
Ken Martin's avatar
Ken Martin committed
106
    for (j=0; j <= this->PhiResolution; j++)
107
    {
Ken Martin's avatar
Ken Martin committed
108 109 110 111 112 113 114
      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);

Bill Lorensen's avatar
Bill Lorensen committed
115
      if ( (norm = vtkMath::Norm(x)) == 0.0 )
116
      {
117
        norm = 1.0;
118
      }
119
      x[0] /= norm; x[1] /= norm; x[2] /= norm;
120
      newNormals->InsertNextTuple(x);
Ken Martin's avatar
Ken Martin committed
121

122
      tc[1] = 1.0 - phi/vtkMath::Pi();
123
      newTCoords->InsertNextTuple(tc);
Ken Martin's avatar
Ken Martin committed
124
    }
125
  }
Ken Martin's avatar
Ken Martin committed
126 127 128
  //
  // Generate mesh connectivity
  //
Kyle Lutz's avatar
Kyle Lutz committed
129
  // bands between poles
Ken Martin's avatar
Ken Martin committed
130
  for (i=0; i < this->ThetaResolution; i++)
131
  {
Ken Martin's avatar
Ken Martin committed
132
    for (j=0; j < this->PhiResolution; j++)
133
    {
Ken Martin's avatar
Ken Martin committed
134 135 136 137 138 139 140 141 142
      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);
    }
143
  }
Ken Martin's avatar
Ken Martin committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157
//
// 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();
158 159

  return 1;
Ken Martin's avatar
Ken Martin committed
160 161
}

162
void vtkTexturedSphereSource::PrintSelf(ostream& os, vtkIndent indent)
Ken Martin's avatar
Ken Martin committed
163
{
Brad King's avatar
Brad King committed
164
  this->Superclass::PrintSelf(os,indent);
Ken Martin's avatar
Ken Martin committed
165 166 167

  os << indent << "Theta Resolution: " << this->ThetaResolution << "\n";
  os << indent << "Phi Resolution: " << this->PhiResolution << "\n";
168 169
  os << indent << "Theta: " << this->Theta << "\n";
  os << indent << "Phi: " << this->Phi << "\n";
Ken Martin's avatar
Ken Martin committed
170
  os << indent << "Radius: " << this->Radius << "\n";
171 172
  os << indent << "Output Points Precision: " << this->OutputPointsPrecision
     << "\n";
Ken Martin's avatar
Ken Martin committed
173
}