~ubuntu-branches/ubuntu/warty/xplanet/warty

« back to all changes in this revision

Viewing changes to src/libprojection/ProjectionOrthographic.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:14:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040824071400-2dr4qnjbjmm8z3ia
Tags: 1.0.6-1ubuntu1
Build-depend: libtiff4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cmath>
 
2
using namespace std;
 
3
 
 
4
#include "Options.h"
 
5
#include "xpUtil.h"
 
6
 
 
7
#include "ProjectionOrthographic.h"
 
8
 
 
9
ProjectionOrthographic::ProjectionOrthographic(const int f, const int w, const int h)
 
10
    : ProjectionBase(f, w, h)
 
11
{
 
12
    isWrapAround_ = false;
 
13
 
 
14
    Options *options = Options::getInstance();
 
15
 
 
16
    radius_ = options->Radius() * height_;
 
17
    setRange(options->getRange());
 
18
 
 
19
    buildPhotoTable();
 
20
}
 
21
 
 
22
ProjectionOrthographic::~ProjectionOrthographic() 
 
23
{
 
24
    destroyPhotoTable();
 
25
}
 
26
 
 
27
void
 
28
ProjectionOrthographic::setRange(const double range)
 
29
{
 
30
    P = range;
 
31
    Psq = P*P;
 
32
    Pp1 = (P + 1);
 
33
    Pm1 = (P - 1);
 
34
    PPm1 = P * Pm1;
 
35
    Pm1sq = Pm1 * Pm1;
 
36
 
 
37
    radius_ *= sqrt(Pp1/Pm1);
 
38
}
 
39
 
 
40
bool
 
41
ProjectionOrthographic::pixelToSpherical(const double x, const double y, 
 
42
                                         double &lon, double &lat)
 
43
{
 
44
    const double X = (x - centerX_)/radius_;
 
45
    const double Y = (centerY_ - y)/radius_;
 
46
 
 
47
    const double rho2 = X*X + Y*Y;
 
48
    if (rho2 > 1) return(false);
 
49
 
 
50
    const double rho = sqrt(rho2);
 
51
 
 
52
    if (rho == 0)
 
53
    {
 
54
        lat = 0; 
 
55
        lon = 0;
 
56
    }
 
57
    else
 
58
    {
 
59
        double arg = Pm1*(Pm1 - rho2 * Pp1);
 
60
        if (arg < 0) return(false);
 
61
 
 
62
        const double N = rho * (PPm1 - sqrt(arg));
 
63
        const double D = (Pm1sq + rho2);
 
64
 
 
65
        const double sinc = N/D;
 
66
        const double cosc = sqrt(1 - sinc*sinc);
 
67
 
 
68
        arg = Y * sinc / rho;
 
69
        if (fabs(arg) > 1) return(false);
 
70
        
 
71
        lat = asin(arg);
 
72
        lon = atan2(X * sinc, rho * cosc);
 
73
    }
 
74
 
 
75
    // This is the cosine of the observer-planet center-normal angle
 
76
    const double cosa = cos(lat) * cos(lon);
 
77
    const double sina_sq = 1 - cosa*cosa;
 
78
    
 
79
    // This is the distance from the observer to the point on the surface
 
80
    const double dist_sq = Psq - 2 * P * cosa + 1;
 
81
    
 
82
    // This is the angle we want: observer-surface-normal
 
83
    const double sinb_sq = Psq / dist_sq * sina_sq;
 
84
    const double cosb = sqrt(1 - sinb_sq);
 
85
    
 
86
    darkening_ = getPhotoFunction(fabs(cosb)); 
 
87
 
 
88
    if (rotate_) RotateXYZ(lat, lon);
 
89
 
 
90
    if (lon > M_PI) lon -= 2*M_PI;
 
91
    else if (lon < -M_PI) lon += 2*M_PI;
 
92
 
 
93
    return(true);
 
94
}
 
95
 
 
96
bool
 
97
ProjectionOrthographic::sphericalToPixel(double lon, double lat,
 
98
                                         double &x, double &y) const
 
99
{
 
100
    if (rotate_) RotateZYX(lat, lon);
 
101
 
 
102
    const double cosc = cos(lat) * cos(lon);
 
103
    if (cosc < 0) return(false);
 
104
 
 
105
    const double k = (P - 1) / (P - cosc);
 
106
 
 
107
    const double X = k * cos(lat) * sin(lon);
 
108
    const double Y = k * sin(lat);
 
109
 
 
110
    x = X * radius_ + centerX_;
 
111
    if (x < 0 || x >= width_) return(false);
 
112
 
 
113
    y = centerY_ - Y * radius_;
 
114
    if (y < 0 || y >= height_) return(false);
 
115
 
 
116
    if (P*cosc < 1) 
 
117
    {
 
118
        double dist = sqrt(x*x + y*y);
 
119
        if (dist < radius_) return(false);
 
120
    }
 
121
 
 
122
    return(true);
 
123
}