forked from Kitware/VTK
/
vtkGeoCamera.cxx
438 lines (379 loc) · 13.5 KB
/
vtkGeoCamera.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*=============================================================================
Program: Visualization Toolkit
Module: vtkGeoCamera.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.
=============================================================================*/
/*-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
#include "vtkObjectFactory.h"
#include "vtkGeoCamera.h"
#include "vtkGeoMath.h"
#include "vtkGeoTerrainNode.h"
#include "vtkMath.h"
#include "vtkCamera.h"
#include "vtkTransform.h"
#include <cfloat>
vtkStandardNewMacro(vtkGeoCamera);
//----------------------------------------------------------------------------
vtkGeoCamera::vtkGeoCamera()
{
this->VTKCamera = vtkSmartPointer<vtkCamera>::New();
this->Transform = vtkSmartPointer<vtkTransform>::New();
this->OriginLongitude = 0.0;
this->OriginLatitude = 0.0;
// Initial state will be looking at earth over the American continent.
//this->Longitude = -90.0;
this->Longitude = 0.0;
this->Latitude = 0.0;
this->Distance = vtkGeoMath::EarthRadiusMeters() * 5.0;
this->Heading = 0.0;
this->Tilt = 90.0;
this->LockHeading = true;
this->ComputeRectilinearOrigin();
this->UpdateAngleRanges();
this->UpdateVTKCamera();
this->Position[0] = this->Position[1] = this->Position[2] = 0.0;
this->Origin[0] = this->Origin[1] = this->Origin[2] = 0.0;
}
//-----------------------------------------------------------------------------
vtkGeoCamera::~vtkGeoCamera()
{
}
//-----------------------------------------------------------------------------
void vtkGeoCamera::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Distance: " << this->Distance << endl;
os << indent << "Tilt: " << this->Tilt << endl;
os << indent << "Heading: " << this->Heading << endl;
os << indent << "Latitude: " << this->Latitude << endl;
os << indent << "Longitude: " << this->Longitude << endl;
os << indent << "LockHeading: " << (this->LockHeading ? "on" : "off");
os << indent << "Origin: {" << this->Origin[0] << ", " << this->Origin[1]
<< ", " << this->Origin[2] << "}" << endl;
os << indent << "OriginLatitude: " << this->OriginLatitude << endl;
os << indent << "OriginLongitude: " << this->OriginLongitude << endl;
os << indent << "Position: {" << this->Position[0] << ", " << this->Position[1]
<< ", " << this->Position[2] << "}" << endl;
os << indent << "VTKCamera: " << endl;
this->VTKCamera->PrintSelf(os, indent.GetNextIndent());
}
//-----------------------------------------------------------------------------
void vtkGeoCamera::SetOriginLatitude(double oLat)
{
if (this->OriginLatitude == oLat)
{
return;
}
this->OriginLatitude = oLat;
this->Modified();
this->ComputeRectilinearOrigin();
}
//-----------------------------------------------------------------------------
void vtkGeoCamera::SetOriginLongitude(double oLon)
{
if (this->OriginLongitude == oLon)
{
return;
}
this->OriginLongitude = oLon;
this->Modified();
this->ComputeRectilinearOrigin();
}
//-----------------------------------------------------------------------------
void vtkGeoCamera::ComputeRectilinearOrigin()
{
double tmp = cos(this->OriginLatitude * vtkMath::Pi() / 180.0);
this->Origin[1] = tmp * cos(this->OriginLongitude * vtkMath::Pi() / 180.0);
this->Origin[0] = -tmp * sin(this->OriginLongitude * vtkMath::Pi() / 180.0);
this->Origin[2] = sin(this->OriginLatitude * vtkMath::Pi() / 180.0);
this->Origin[0] *= vtkGeoMath::EarthRadiusMeters();
this->Origin[1] *= vtkGeoMath::EarthRadiusMeters();
this->Origin[2] *= vtkGeoMath::EarthRadiusMeters();
this->UpdateVTKCamera();
}
//-----------------------------------------------------------------------------
void vtkGeoCamera::SetLongitude(double longitude)
{
if (this->Longitude == longitude)
{
return;
}
this->Modified();
this->Longitude = longitude;
this->UpdateAngleRanges();
this->UpdateVTKCamera();
}
//-----------------------------------------------------------------------------
void vtkGeoCamera::SetLatitude(double latitude)
{
if (this->Latitude == latitude)
{
return;
}
this->Modified();
this->Latitude = latitude;
this->UpdateAngleRanges();
this->UpdateVTKCamera();
}
//-----------------------------------------------------------------------------
void vtkGeoCamera::SetDistance(double altitude)
{
if (this->Distance == altitude)
{
return;
}
this->Modified();
this->Distance = altitude;
this->UpdateVTKCamera();
}
//-----------------------------------------------------------------------------
void vtkGeoCamera::SetHeading(double heading)
{
if (this->Heading == heading)
{
return;
}
while (heading < -180)
{
heading += 360;
}
while (heading > 180)
{
heading -= 360;
}
this->Modified();
this->Heading = heading;
this->UpdateAngleRanges();
this->UpdateVTKCamera();
}
//-----------------------------------------------------------------------------
void vtkGeoCamera::SetTilt(double tilt)
{
if (this->Tilt == tilt)
{
return;
}
this->Modified();
this->Tilt = tilt;
this->UpdateVTKCamera();
}
//-----------------------------------------------------------------------------
void vtkGeoCamera::UpdateAngleRanges()
{
while (this->Heading > 180.0)
{
this->Heading -= 360.0;
}
while (this->Heading < -180.0)
{
this->Heading += 360.0;
}
while (this->Longitude > 180.0)
{
this->Longitude -= 360.0;
}
while (this->Longitude < -180.0)
{
this->Longitude += 360.0;
}
if (this->Latitude > 90.0)
{
this->Latitude = 180.0 - this->Latitude;
}
if (this->Latitude < -90.0)
{
this->Latitude = -180.0 - this->Latitude;
}
}
//-----------------------------------------------------------------------------
void vtkGeoCamera::UpdateVTKCamera()
{
this->Transform->PostMultiply();
this->Transform->Identity();
// Tilt
this->Transform->RotateX(this->Tilt);
// Heading
this->Transform->RotateY(-this->Heading);
// to surface of earth
this->Transform->Translate(0.0, vtkGeoMath::EarthRadiusMeters(), 0.0);
// Latitude
this->Transform->RotateX(this->Latitude);
// Longitude
this->Transform->RotateZ(this->Longitude);
// Consider origin.
this->Transform->Translate(-this->Origin[0],
-this->Origin[1],
-this->Origin[2]);
double* pt;
double tmp[3];
// Find focal point of the camera.
pt = this->Transform->TransformDoublePoint(0.0, 0.0, 0.0);
this->VTKCamera->SetFocalPoint(pt);
// Find position
this->VTKCamera->GetFocalPoint(tmp);
// Use arbitrary magnitude.
pt = this->Transform->TransformDoublePoint(0.0, 0.0, -this->Distance);
this->VTKCamera->SetPosition(pt);
// Save the position with out the origin shift so the evaluation works.
this->Position[0] = pt[0] + this->Origin[0];
this->Position[1] = pt[1] + this->Origin[1];
this->Position[2] = pt[2] + this->Origin[2];
if (this->LockHeading)
{
// Now find view up using heading.
pt = this->Transform->TransformDoublePoint(0.0, 1.0, 0.0);
tmp[0] = pt[0] - tmp[0];
tmp[1] = pt[1] - tmp[1];
tmp[2] = pt[2] - tmp[2];
this->VTKCamera->SetViewUp(tmp);
}
else
{
// Find heading using view up.
this->VTKCamera->OrthogonalizeViewUp();
double up[3];
this->VTKCamera->GetViewUp(up);
// Project vector to north pole and view up to the following plane:
// Normal = dir. out of center of earth from focal point, Point = origin.
// Compute the plane normal (center of earth at -origin).
double dir[3];
dir[0] = -tmp[0] - this->Origin[0];
dir[1] = -tmp[1] - this->Origin[1];
dir[2] = -tmp[2] - this->Origin[2];
vtkMath::Normalize(dir);
// Compute direction to north pole (at -origin + (0,0,earth_radius))
// from focal point.
double north[3];
north[0] = -this->Origin[0] - tmp[0];
north[1] = -this->Origin[1] - tmp[1];
north[2] = vtkGeoMath::EarthRadiusMeters() - this->Origin[2] - tmp[2];
double northDot = vtkMath::Dot(north, dir);
// Project direction to north pole to our plane.
double northProj[3];
northProj[0] = north[0] - northDot*dir[0];
northProj[1] = north[1] - northDot*dir[1];
northProj[2] = north[2] - northDot*dir[2];
vtkMath::Normalize(northProj);
// Project view up vector to the same plane.
double upDot = vtkMath::Dot(up, dir);
double upProj[3];
upProj[0] = up[0] - upDot*dir[0];
upProj[1] = up[1] - upDot*dir[1];
upProj[2] = up[2] - upDot*dir[2];
vtkMath::Normalize(upProj);
// Determine the angle between the vectors.
// Do some sin/cos trickery to get it to the full range
// [-180, 180].
double dotProd = vtkMath::Dot(northProj, upProj);
double crossProd[3];
vtkMath::Cross(northProj, upProj, crossProd);
this->Heading = vtkMath::DegreesFromRadians( asin( vtkMath::Norm( crossProd ) ) );
if (dotProd < 0)
{
this->Heading = 180.0 - this->Heading;
}
if (vtkMath::Dot(crossProd, dir) < 0)
{
this->Heading = -this->Heading;
}
}
}
//-----------------------------------------------------------------------------
void vtkGeoCamera::InitializeNodeAnalysis(int rendererSize[2])
{
this->Aspect[1] = tan( vtkMath::RadiansFromDegrees( this->VTKCamera->GetViewAngle() ) * 0.5 );
this->Aspect[0] = this->Aspect[1] * rendererSize[0]
/ static_cast<double>( rendererSize[1] );
this->VTKCamera->GetViewPlaneNormal(this->ForwardNormal);
this->ForwardNormal[0] = - this->ForwardNormal[0];
this->ForwardNormal[1] = - this->ForwardNormal[1];
this->ForwardNormal[2] = - this->ForwardNormal[2];
this->VTKCamera->GetViewUp(this->UpNormal);
vtkMath::Normalize(this->UpNormal);
vtkMath::Cross(this->ForwardNormal, this->UpNormal, this->RightNormal);
// It may not be necessary to keep the above as instance variables.
for (int ii = 0; ii < 3; ++ii)
{
this->LeftPlaneNormal[ii] = - this->ForwardNormal[ii]*this->Aspect[0]
- this->RightNormal[ii];
this->RightPlaneNormal[ii] = - this->ForwardNormal[ii]*this->Aspect[0]
+ this->RightNormal[ii];
this->DownPlaneNormal[ii] = - this->ForwardNormal[ii]*this->Aspect[1]
- this->UpNormal[ii];
this->UpPlaneNormal[ii] = - this->ForwardNormal[ii]*this->Aspect[1]
+ this->UpNormal[ii];
}
vtkMath::Normalize(this->LeftPlaneNormal);
vtkMath::Normalize(this->RightPlaneNormal);
vtkMath::Normalize(this->DownPlaneNormal);
vtkMath::Normalize(this->UpPlaneNormal);
}
//-----------------------------------------------------------------------------
// This has to be as fast as possible.
// It is called for each node every render.
// These comparisons in rectilinear coordinates are not perfect.
// I look at a viewplain containing the sphere center.
// I compate a circle with the view bounds.
// Another option is to save 4 frustum plane normals in the camera.
// This would require 5 dot products (15 multiplications).
double vtkGeoCamera::GetNodeCoverage(vtkGeoTerrainNode* node)
{
double sphereCenter[3];
double sphereRadius;
double camPosition[3];
this->GetPosition(camPosition);
// Lets take care of nodes on the opposite side of the earth.
// I think there should be a better way of figuring this out.
if (vtkMath::Dot(this->ForwardNormal,node->GetCornerNormal00())>0.0 &&
vtkMath::Dot(this->ForwardNormal,node->GetCornerNormal01())>0.0 &&
vtkMath::Dot(this->ForwardNormal,node->GetCornerNormal10())>0.0 &&
vtkMath::Dot(this->ForwardNormal,node->GetCornerNormal11())>0.0)
{ // Node is hidden by the earth.
return 0.0;
}
sphereRadius = node->GetBoundingSphereRadius();
node->GetBoundingSphereCenter(sphereCenter);
// Put the camera's position at the origin.
sphereCenter[0] = sphereCenter[0] - camPosition[0];
sphereCenter[1] = sphereCenter[1] - camPosition[1];
sphereCenter[2] = sphereCenter[2] - camPosition[2];
double left = vtkMath::Dot(this->LeftPlaneNormal,sphereCenter);
double right = vtkMath::Dot(this->RightPlaneNormal,sphereCenter);
double down = vtkMath::Dot(this->DownPlaneNormal,sphereCenter);
double up = vtkMath::Dot(this->UpPlaneNormal,sphereCenter);
double forward = vtkMath::Dot(this->ForwardNormal,sphereCenter);
if ( left > sphereRadius || right > sphereRadius ||
down > sphereRadius || up > sphereRadius ||
forward < -sphereRadius)
{
return 0.0;
}
if (forward < sphereRadius)
{ // Camera is probably in the sphere.
return 1.0;
}
left = -left;
if (left > sphereRadius) { left = sphereRadius;}
up = -up;
if (up > sphereRadius) { up = sphereRadius;}
right = -right;
if (right > sphereRadius) { right = sphereRadius;}
down = -down;
if (down > sphereRadius) { down = sphereRadius;}
double coverage = (left+right)*(up+down) / (4.0 * forward*forward*this->Aspect[0]*this->Aspect[1]);
return coverage;
}
//-----------------------------------------------------------------------------
vtkCamera* vtkGeoCamera::GetVTKCamera()
{
return this->VTKCamera;
}