/
vtkCubeSource.cxx
203 lines (176 loc) · 5.89 KB
/
vtkCubeSource.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCubeSource.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 "vtkCubeSource.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include <math.h>
vtkStandardNewMacro(vtkCubeSource);
vtkCubeSource::vtkCubeSource(double xL, double yL, double zL)
{
this->XLength = fabs(xL);
this->YLength = fabs(yL);
this->ZLength = fabs(zL);
this->Center[0] = 0.0;
this->Center[1] = 0.0;
this->Center[2] = 0.0;
this->SetNumberOfInputPorts(0);
}
int vtkCubeSource::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 x[3], n[3], tc[3];
int numPolys=6, numPts=24;
int i, j, k;
vtkIdType pts[4];
vtkPoints *newPoints;
vtkFloatArray *newNormals;
vtkFloatArray *newTCoords; // CCS 7/27/98 Added for Texture Mapping
vtkCellArray *newPolys;
//
// Set things up; allocate memory
//
newPoints = vtkPoints::New();
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,4));
//
// Generate points and normals
//
for (x[0]=this->Center[0]-this->XLength/2.0, n[0]=(-1.0), n[1]=n[2]=0.0, i=0;
i<2; i++, x[0]+=this->XLength, n[0]+=2.0)
{
for (x[1]=this->Center[1]-this->YLength/2.0, j=0; j<2;
j++, x[1]+=this->YLength)
{
tc[1] = x[1] + 0.5;
for (x[2]=this->Center[2]-this->ZLength/2.0, k=0; k<2;
k++, x[2]+=this->ZLength)
{
tc[0] = (x[2] + 0.5) * ( 1 - 2*i );
newPoints->InsertNextPoint(x);
newTCoords->InsertNextTuple(tc);
newNormals->InsertNextTuple(n);
}
}
}
pts[0] = 0; pts[1] = 1; pts[2] = 3; pts[3] = 2;
newPolys->InsertNextCell(4,pts);
pts[0] = 4; pts[1] = 6; pts[2] = 7; pts[3] = 5;
newPolys->InsertNextCell(4,pts);
for (x[1]=this->Center[1]-this->YLength/2.0, n[1]=(-1.0), n[0]=n[2]=0.0, i=0;
i<2; i++, x[1]+=this->YLength, n[1]+=2.0)
{
for (x[0]=this->Center[0]-this->XLength/2.0, j=0; j<2;
j++, x[0]+=this->XLength)
{
tc[0] = ( x[0] + 0.5 ) * ( 2*i - 1 );
for (x[2]=this->Center[2]-this->ZLength/2.0, k=0; k<2;
k++, x[2]+=this->ZLength)
{
tc[1] = ( x[2] + 0.5 ) * -1;
newPoints->InsertNextPoint(x);
newTCoords->InsertNextTuple(tc);
newNormals->InsertNextTuple(n);
}
}
}
pts[0] = 8; pts[1] = 10; pts[2] = 11; pts[3] = 9;
newPolys->InsertNextCell(4,pts);
pts[0] = 12; pts[1] = 13; pts[2] = 15; pts[3] = 14;
newPolys->InsertNextCell(4,pts);
for (x[2]=this->Center[2]-this->ZLength/2.0, n[2]=(-1.0), n[0]=n[1]=0.0, i=0;
i<2; i++, x[2]+=this->ZLength, n[2]+=2.0)
{
for (x[1]=this->Center[1]-this->YLength/2.0, j=0; j<2;
j++, x[1]+=this->YLength)
{
tc[1] = x[1] + 0.5;
for (x[0]=this->Center[0]-this->XLength/2.0, k=0; k<2;
k++, x[0]+=this->XLength)
{
tc[0] = ( x[0] + 0.5 ) * ( 2*i - 1 );
newPoints->InsertNextPoint(x);
newTCoords->InsertNextTuple(tc);
newNormals->InsertNextTuple(n);
}
}
}
pts[0] = 16; pts[1] = 18; pts[2] = 19; pts[3] = 17;
newPolys->InsertNextCell(4,pts);
pts[0] = 20; pts[1] = 21; pts[2] = 23; pts[3] = 22;
newPolys->InsertNextCell(4,pts);
//
// 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;
}
// Convenience method allows creation of cube by specifying bounding box.
void vtkCubeSource::SetBounds(double xMin, double xMax,
double yMin, double yMax,
double zMin, double zMax)
{
double bounds[6];
bounds[0] = xMin;
bounds[1] = xMax;
bounds[2] = yMin;
bounds[3] = yMax;
bounds[4] = zMin;
bounds[5] = zMax;
this->SetBounds (bounds);
}
void vtkCubeSource::SetBounds(double bounds[6])
{
this->SetXLength(bounds[1]-bounds[0]);
this->SetYLength(bounds[3]-bounds[2]);
this->SetZLength(bounds[5]-bounds[4]);
this->SetCenter((bounds[1]+bounds[0])/2.0, (bounds[3]+bounds[2])/2.0,
(bounds[5]+bounds[4])/2.0);
}
void vtkCubeSource::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "X Length: " << this->XLength << "\n";
os << indent << "Y Length: " << this->YLength << "\n";
os << indent << "Z Length: " << this->ZLength << "\n";
os << indent << "Center: (" << this->Center[0] << ", "
<< this->Center[1] << ", " << this->Center[2] << ")\n";
}