1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkEarthSource.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15 #include "vtkEarthSource.h"
16
17 #include "vtkCellArray.h"
18 #include "vtkFloatArray.h"
19 #include "vtkInformation.h"
20 #include "vtkInformationVector.h"
21 #include "vtkMath.h"
22 #include "vtkObjectFactory.h"
23 #include "vtkPointData.h"
24 #include "vtkPoints.h"
25 #include "vtkPolyData.h"
26 #include "vtkStreamingDemandDrivenPipeline.h"
27
28 #include <cmath>
29
30 vtkStandardNewMacro(vtkEarthSource);
31
32 // Description:
33 // Construct an Earth with radius = 1.0 and OnRatio set at 10. The outlines are drawn
34 // in wireframe as default.
vtkEarthSource()35 vtkEarthSource::vtkEarthSource()
36 {
37 this->Radius = 1.0;
38 this->OnRatio = 10;
39 this->Outline = 1;
40
41 this->SetNumberOfInputPorts(0);
42 }
43
PrintSelf(ostream & os,vtkIndent indent)44 void vtkEarthSource::PrintSelf(ostream& os, vtkIndent indent)
45 {
46 this->Superclass::PrintSelf(os, indent);
47
48 os << indent << "Radius: " << this->Radius << "\n";
49 os << indent << "OnRatio: " << this->OnRatio << "\n";
50 os << indent << "Outline: " << (this->Outline ? "On\n" : "Off\n");
51 }
52
53 // NOLINTNEXTLINE(bugprone-suspicious-include)
54 #include "vtkEarthSourceData.cxx"
55
RequestData(vtkInformation * vtkNotUsed (request),vtkInformationVector ** vtkNotUsed (inputVector),vtkInformationVector * outputVector)56 int vtkEarthSource::RequestData(vtkInformation* vtkNotUsed(request),
57 vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* outputVector)
58 {
59 // get the info object
60 vtkInformation* outInfo = outputVector->GetInformationObject(0);
61
62 // get the output
63 vtkPolyData* output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
64
65 int i;
66 int maxPts;
67 int maxPolys;
68 vtkPoints* newPoints;
69 vtkFloatArray* newNormals;
70 vtkCellArray* newPolys;
71 double x[3], base[3];
72 vtkIdType Pts[4000];
73 int npts, land, offset;
74 int actualpts, actualpolys;
75 double scale = 1.0 / 30000.0;
76
77 //
78 // Set things up; allocate memory
79 //
80 maxPts = 12000 / this->OnRatio;
81 maxPolys = 16;
82 actualpts = actualpolys = 0;
83
84 newPoints = vtkPoints::New();
85 newPoints->Allocate(maxPts);
86 newNormals = vtkFloatArray::New();
87 newNormals->SetNumberOfComponents(3);
88 newNormals->Allocate(3 * maxPts);
89 newPolys = vtkCellArray::New();
90 newPolys->AllocateEstimate(maxPolys, 4000 / this->OnRatio);
91
92 //
93 // Create points
94 //
95 offset = 0;
96 while (true)
97 {
98 // read a polygon
99 npts = vtkEarthData[offset++];
100 if ((npts == 0) || (actualpolys > maxPolys))
101 {
102 break;
103 }
104
105 land = vtkEarthData[offset++];
106
107 base[0] = 0;
108 base[1] = 0;
109 base[2] = 0;
110
111 for (i = 1; i <= npts; i++)
112 {
113 base[0] += vtkEarthData[offset++] * scale;
114 base[1] += vtkEarthData[offset++] * scale;
115 base[2] += vtkEarthData[offset++] * scale;
116
117 x[0] = base[2] * this->Radius;
118 x[1] = base[0] * this->Radius;
119 x[2] = base[1] * this->Radius;
120
121 if ((land == 1) && (npts > this->OnRatio * 3))
122 {
123 // use only every OnRatioth point in the polygon
124 if ((i % this->OnRatio) == 0)
125 {
126 newPoints->InsertNextPoint(x);
127 vtkMath::Normalize(x);
128 newNormals->InsertNextTuple(x);
129 actualpts++;
130 }
131 }
132 }
133
134 if ((land == 1) && (npts > this->OnRatio * 3))
135 {
136 //
137 // Generate mesh connectivity for this polygon
138 //
139
140 for (i = 0; i < (npts / this->OnRatio); i++)
141 {
142 Pts[i] = (actualpts - npts / this->OnRatio) + i;
143 }
144
145 if (this->Outline) // close the loop in the line
146 {
147 Pts[i] = (actualpts - npts / this->OnRatio);
148 newPolys->InsertNextCell(i + 1, Pts);
149 }
150 else
151 {
152 newPolys->InsertNextCell(i, Pts);
153 }
154
155 actualpolys++;
156 }
157 }
158
159 //
160 // Update ourselves and release memory
161 //
162 output->SetPoints(newPoints);
163 newPoints->Delete();
164
165 output->GetPointData()->SetNormals(newNormals);
166 newNormals->Delete();
167
168 if (this->Outline) // lines or polygons
169 {
170 output->SetLines(newPolys);
171 }
172 else
173 {
174 output->SetPolys(newPolys);
175 }
176 newPolys->Delete();
177
178 output->Squeeze();
179
180 return 1;
181 }
182