vtkCubeSource.cc 5.65 KB
Newer Older
1
2
/*=========================================================================

Ken Martin's avatar
Ken Martin committed
3
  Program:   Visualization Toolkit
Ken Martin's avatar
Ken Martin committed
4
  Module:    vtkCubeSource.cc
5
6
7
8
9
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


Ken Martin's avatar
Ken Martin committed
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
Copyright (c) 1993-1995 Ken Martin, Will Schroeder, Bill Lorensen.

This software is copyrighted by Ken Martin, Will Schroeder and Bill Lorensen.
The following terms apply to all files associated with the software unless
explicitly disclaimed in individual files. This copyright specifically does
not apply to the related textbook "The Visualization Toolkit" ISBN
013199837-4 published by Prentice Hall which is covered by its own copyright.

The authors hereby grant permission to use, copy, and distribute this
software and its documentation for any purpose, provided that existing
copyright notices are retained in all copies and that this notice is included
verbatim in any distributions. Additionally, the authors grant permission to
modify this software and its documentation for any purpose, provided that
such modifications are not distributed without the explicit consent of the
authors and that existing copyright notices are retained in all copies. Some
of the algorithms implemented by this software are patented, observe all
applicable patent law.

IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

39
40

=========================================================================*/
Will Schroeder's avatar
Will Schroeder committed
41
#include <math.h>
Ken Martin's avatar
Ken Martin committed
42
43
44
#include "vtkCubeSource.hh"
#include "vtkFloatPoints.hh"
#include "vtkFloatNormals.hh"
Will Schroeder's avatar
Will Schroeder committed
45

Ken Martin's avatar
Ken Martin committed
46
vtkCubeSource::vtkCubeSource(float xL, float yL, float zL)
Will Schroeder's avatar
Will Schroeder committed
47
48
49
50
{
  this->XLength = fabs(xL);
  this->YLength = fabs(yL);
  this->ZLength = fabs(zL);
Will Schroeder's avatar
Will Schroeder committed
51
52
53
54

  this->Center[0] = 0.0;
  this->Center[1] = 0.0;
  this->Center[2] = 0.0;
Will Schroeder's avatar
Will Schroeder committed
55
56
}

Ken Martin's avatar
Ken Martin committed
57
void vtkCubeSource::Execute()
Will Schroeder's avatar
Will Schroeder committed
58
59
60
61
62
{
  float x[3], n[3];
  int numPolys=6, numPts=24;
  int i, j, k;
  int pts[4];
Ken Martin's avatar
Ken Martin committed
63
64
65
  vtkFloatPoints *newPoints; 
  vtkFloatNormals *newNormals;
  vtkCellArray *newPolys;
Ken Martin's avatar
Ken Martin committed
66
67
  vtkPolyData *output = this->GetOutput();
  
Will Schroeder's avatar
Will Schroeder committed
68
69
70
71
  vtkDebugMacro(<<"Creating polygonal cube");
//
// Set things up; allocate memory
//
Ken Martin's avatar
Ken Martin committed
72
73
  newPoints = new vtkFloatPoints(numPts);
  newNormals = new vtkFloatNormals(numPts);
Will Schroeder's avatar
Will Schroeder committed
74

Ken Martin's avatar
Ken Martin committed
75
  newPolys = new vtkCellArray;
Will Schroeder's avatar
Will Schroeder committed
76
  newPolys->Allocate(newPolys->EstimateSize(numPolys,4));
Will Schroeder's avatar
Will Schroeder committed
77
78
79
80
81
//
// Generate points and normals
//
  numPts = 0;

Will Schroeder's avatar
Will Schroeder committed
82
  for (x[0]=Center[0]-this->XLength/2.0, n[0]=(-1.0), n[1]=n[2]=0.0, i=0; 
Will Schroeder's avatar
Will Schroeder committed
83
  i<2; i++, x[0]+=this->XLength, n[0]+=2.0)
Will Schroeder's avatar
Will Schroeder committed
84
    {
Will Schroeder's avatar
Will Schroeder committed
85
    for (x[1]=Center[1]-this->YLength/2.0, j=0; j<2; 
Will Schroeder's avatar
Will Schroeder committed
86
    j++, x[1]+=this->YLength)
Will Schroeder's avatar
Will Schroeder committed
87
      {
Will Schroeder's avatar
Will Schroeder committed
88
      for (x[2]=Center[2]-this->ZLength/2.0, k=0; k<2; 
Will Schroeder's avatar
Will Schroeder committed
89
      k++, x[2]+=this->ZLength)
Will Schroeder's avatar
Will Schroeder committed
90
        {
Will Schroeder's avatar
Will Schroeder committed
91
92
        newPoints->InsertNextPoint(x);
        newNormals->InsertNextNormal(n);
Will Schroeder's avatar
Will Schroeder committed
93
94
95
        }
      }
    }
Will Schroeder's avatar
Will Schroeder committed
96
97
98
99
  pts[0] = 0; pts[1] = 1; pts[2] = 3; pts[3] = 2; 
  newPolys->InsertNextCell(4,pts);
  pts[0] += 4; pts[1] +=4; pts[2] +=4; pts[3] += 4; 
  newPolys->InsertNextCell(4,pts);
Will Schroeder's avatar
Will Schroeder committed
100

Will Schroeder's avatar
Will Schroeder committed
101
  for (x[1]=Center[1]-this->YLength/2.0, n[1]=(-1.0), n[0]=n[2]=0.0, i=0; 
Will Schroeder's avatar
Will Schroeder committed
102
  i<2; i++, x[1]+=this->YLength, n[1]+=2.0)
Will Schroeder's avatar
Will Schroeder committed
103
    {
Will Schroeder's avatar
Will Schroeder committed
104
    for (x[0]=Center[0]-this->XLength/2.0, j=0; j<2; 
Will Schroeder's avatar
Will Schroeder committed
105
    j++, x[0]+=this->XLength)
Will Schroeder's avatar
Will Schroeder committed
106
      {
Will Schroeder's avatar
Will Schroeder committed
107
      for (x[2]=Center[2]-this->ZLength/2.0, k=0; k<2; 
Will Schroeder's avatar
Will Schroeder committed
108
      k++, x[2]+=this->ZLength)
Will Schroeder's avatar
Will Schroeder committed
109
        {
Will Schroeder's avatar
Will Schroeder committed
110
111
        newPoints->InsertNextPoint(x);
        newNormals->InsertNextNormal(n);
Will Schroeder's avatar
Will Schroeder committed
112
113
114
115
116
117
118
119
        }
      }
    }
  pts[0] += 4; pts[1] +=4; pts[2] +=4; pts[3] += 4; 
  newPolys->InsertNextCell(4,pts);
  pts[0] += 4; pts[1] +=4; pts[2] +=4; pts[3] += 4; 
  newPolys->InsertNextCell(4,pts);

Will Schroeder's avatar
Will Schroeder committed
120
  for (x[2]=Center[2]-this->ZLength/2.0, n[2]=(-1.0), n[0]=n[1]=0.0, i=0; 
Will Schroeder's avatar
Will Schroeder committed
121
  i<2; i++, x[2]+=this->ZLength, n[2]+=2.0)
Will Schroeder's avatar
Will Schroeder committed
122
    {
Will Schroeder's avatar
Will Schroeder committed
123
    for (x[1]=Center[1]-this->YLength/2.0, j=0; j<2; 
Will Schroeder's avatar
Will Schroeder committed
124
    j++, x[1]+=this->YLength)
Will Schroeder's avatar
Will Schroeder committed
125
      {
Will Schroeder's avatar
Will Schroeder committed
126
      for (x[0]=Center[0]-this->XLength/2.0, k=0; k<2; 
Will Schroeder's avatar
Will Schroeder committed
127
      k++, x[0]+=this->XLength)
Will Schroeder's avatar
Will Schroeder committed
128
        {
Will Schroeder's avatar
Will Schroeder committed
129
130
        newPoints->InsertNextPoint(x);
        newNormals->InsertNextNormal(n);
Will Schroeder's avatar
Will Schroeder committed
131
132
133
        }
      }
    }
Will Schroeder's avatar
Will Schroeder committed
134
135
136
137
  pts[0] += 4; pts[1] +=4; pts[2] +=4; pts[3] += 4; 
  newPolys->InsertNextCell(4,pts);
  pts[0] += 4; pts[1] +=4; pts[2] +=4; pts[3] += 4; 
  newPolys->InsertNextCell(4,pts);
Will Schroeder's avatar
Will Schroeder committed
138
//
Will Schroeder's avatar
Will Schroeder committed
139
// Update ourselves and release memory
Will Schroeder's avatar
Will Schroeder committed
140
//
Ken Martin's avatar
Ken Martin committed
141
  output->SetPoints(newPoints);
Will Schroeder's avatar
Will Schroeder committed
142
143
  newPoints->Delete();

Ken Martin's avatar
Ken Martin committed
144
  output->GetPointData()->SetNormals(newNormals);
Will Schroeder's avatar
Will Schroeder committed
145
  newNormals->Delete();
Will Schroeder's avatar
Will Schroeder committed
146
147

  newPolys->Squeeze(); // since we've estimated size; reclaim some space
Ken Martin's avatar
Ken Martin committed
148
  output->SetPolys(newPolys);
Will Schroeder's avatar
Will Schroeder committed
149
  newPolys->Delete();
Will Schroeder's avatar
Will Schroeder committed
150
}
Will Schroeder's avatar
Will Schroeder committed
151

Will Schroeder's avatar
Will Schroeder committed
152
153
// Description:
// Convenience method allows creation of cube by specifying bounding box.
Ken Martin's avatar
Ken Martin committed
154
void vtkCubeSource::SetBounds(float bounds[6])
Will Schroeder's avatar
Will Schroeder committed
155
156
157
158
159
160
161
162
163
{
  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);
}

Ken Martin's avatar
Ken Martin committed
164
void vtkCubeSource::PrintSelf(ostream& os, vtkIndent indent)
Will Schroeder's avatar
Will Schroeder committed
165
{
Ken Martin's avatar
Ken Martin committed
166
  vtkPolySource::PrintSelf(os,indent);
Will Schroeder's avatar
Will Schroeder committed
167

168
169
170
  os << indent << "X Length: " << this->XLength << "\n";
  os << indent << "Y Length: " << this->YLength << "\n";
  os << indent << "Z Length: " << this->ZLength << "\n";
Will Schroeder's avatar
Will Schroeder committed
171
172
  os << indent << "Center: (" << this->Center[0] << ", " 
               << this->Center[1] << ", " << this->Center[2] << ")\n";
Will Schroeder's avatar
Will Schroeder committed
173
}
Will Schroeder's avatar
Will Schroeder committed
174