~ubuntu-branches/debian/squeeze/stellarium/squeeze

« back to all changes in this revision

Viewing changes to src/Navigator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2008-05-19 21:28:23 UTC
  • mfrom: (3.1.5 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080519212823-m5nfiuntxstxzxj7
Tags: 0.9.1-4
Add libxcursor-dev, libxfixes-dev, libxinerama-dev, libqt4-opengl-dev to
build-deps (Closes: #479906)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Stellarium
 
3
 * Copyright (C) 2003 Fabien Chereau
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
 */
 
19
 
 
20
#include "Navigator.hpp"
 
21
#include "StelUtils.hpp"
 
22
#include "SolarSystem.hpp"
 
23
#include "InitParser.hpp"
 
24
#include "Observer.hpp"
 
25
 
 
26
////////////////////////////////////////////////////////////////////////////////
 
27
Navigator::Navigator(Observer* obs) : time_speed(JD_SECOND), JDay(0.), position(obs)
 
28
{
 
29
        if (!position)
 
30
        {
 
31
                printf("ERROR : Can't create a Navigator without a valid Observator\n");
 
32
                exit(-1);
 
33
        }
 
34
        local_vision=Vec3d(1.,0.,0.);
 
35
        equ_vision=Vec3d(1.,0.,0.);
 
36
        prec_equ_vision=Vec3d(1.,0.,0.);  // not correct yet...
 
37
        viewing_mode = VIEW_HORIZON;  // default
 
38
 
 
39
}
 
40
 
 
41
Navigator::~Navigator()
 
42
{}
 
43
 
 
44
const Planet *Navigator::getHomePlanet(void) const
 
45
{
 
46
        return position->getHomePlanet();
 
47
}
 
48
 
 
49
void Navigator::init(const InitParser& conf)
 
50
{
 
51
        setTimeNow();
 
52
        setLocalVision(Vec3f(1,1e-05,0.2));
 
53
        // Compute transform matrices between coordinates systems
 
54
        updateTransformMatrices();
 
55
        updateModelViewMat();
 
56
        string tmpstr = conf.get_str("navigation:viewing_mode");
 
57
        if (tmpstr=="equator")
 
58
                setViewingMode(Navigator::VIEW_EQUATOR);
 
59
        else
 
60
        {
 
61
                if (tmpstr=="horizon")
 
62
                        setViewingMode(Navigator::VIEW_HORIZON);
 
63
                else
 
64
                {
 
65
                        cerr << "ERROR : Unknown viewing mode type : " << tmpstr << endl;
 
66
                        assert(0);
 
67
                }
 
68
        }
 
69
        initViewPos = StelUtils::str_to_vec3f(conf.get_str("navigation:init_view_pos"));
 
70
        setLocalVision(initViewPos);
 
71
        
 
72
        // Navigation section
 
73
        PresetSkyTime           = conf.get_double ("navigation","preset_sky_time",2451545.);
 
74
        StartupTimeMode         = conf.get_str("navigation:startup_time_mode"); // Can be "now" or "preset"
 
75
        if (StartupTimeMode=="preset" || StartupTimeMode=="Preset")
 
76
                setJDay(PresetSkyTime - get_GMT_shift_from_system(PresetSkyTime) * JD_HOUR);
 
77
        else setTimeNow();
 
78
}
 
79
 
 
80
//! Set stellarium time to current real world time
 
81
void Navigator::setTimeNow()
 
82
{
 
83
        setJDay(StelUtils::getJDFromSystem());
 
84
}
 
85
 
 
86
//! Get whether the current stellarium time is the real world time
 
87
bool Navigator::getIsTimeNow(void) const
 
88
{
 
89
        // cache last time to prevent to much slow system call
 
90
        static double lastJD = getJDay();
 
91
        static bool previousResult = (fabs(getJDay()-StelUtils::getJDFromSystem())<JD_SECOND);
 
92
        if (fabs(lastJD-getJDay())>JD_SECOND/4)
 
93
        {
 
94
                lastJD = getJDay();
 
95
                previousResult = (fabs(getJDay()-StelUtils::getJDFromSystem())<JD_SECOND);
 
96
        }
 
97
        return previousResult;
 
98
}
 
99
 
 
100
 
 
101
////////////////////////////////////////////////////////////////////////////////
 
102
void Navigator::setLocalVision(const Vec3d& _pos)
 
103
{
 
104
        local_vision = _pos;
 
105
        equ_vision=local_to_earth_equ(local_vision);
 
106
        prec_equ_vision = mat_earth_equ_to_j2000*equ_vision;
 
107
}
 
108
 
 
109
////////////////////////////////////////////////////////////////////////////////
 
110
void Navigator::setEquVision(const Vec3d& _pos)
 
111
{
 
112
        equ_vision = _pos;
 
113
        prec_equ_vision = mat_earth_equ_to_j2000*equ_vision;
 
114
        local_vision = earth_equ_to_local(equ_vision);
 
115
}
 
116
 
 
117
////////////////////////////////////////////////////////////////////////////////
 
118
void Navigator::setPrecEquVision(const Vec3d& _pos)
 
119
{
 
120
        prec_equ_vision = _pos;
 
121
        equ_vision = mat_j2000_to_earth_equ*prec_equ_vision;
 
122
        local_vision = earth_equ_to_local(equ_vision);
 
123
}
 
124
 
 
125
////////////////////////////////////////////////////////////////////////////////
 
126
// Increment time
 
127
void Navigator::updateTime(int delta_time)
 
128
{
 
129
        JDay+=time_speed*(double)delta_time/1000.;
 
130
 
 
131
        // Fix time limits to -100000 to +100000 to prevent bugs
 
132
        if (JDay>38245309.499988) JDay = 38245309.499988;
 
133
        if (JDay<-34803211.500012) JDay = -34803211.500012;
 
134
}
 
135
 
 
136
////////////////////////////////////////////////////////////////////////////////
 
137
// The non optimized (more clear version is available on the CVS : before date 25/07/2003)
 
138
 
 
139
  // see vsop87.doc:
 
140
 
 
141
const Mat4d mat_j2000_to_vsop87(
 
142
              Mat4d::xrotation(-23.4392803055555555556*(M_PI/180)) *
 
143
              Mat4d::zrotation(0.0000275*(M_PI/180)));
 
144
 
 
145
const Mat4d mat_vsop87_to_j2000(mat_j2000_to_vsop87.transpose());
 
146
 
 
147
 
 
148
void Navigator::updateTransformMatrices(void)
 
149
{
 
150
        mat_local_to_earth_equ = position->getRotLocalToEquatorial(JDay);
 
151
        mat_earth_equ_to_local = mat_local_to_earth_equ.transpose();
 
152
 
 
153
        mat_earth_equ_to_j2000 = mat_vsop87_to_j2000
 
154
                           * position->getRotEquatorialToVsop87();
 
155
        mat_j2000_to_earth_equ = mat_earth_equ_to_j2000.transpose();
 
156
 
 
157
        mat_helio_to_earth_equ =
 
158
            mat_j2000_to_earth_equ *
 
159
        mat_vsop87_to_j2000 *
 
160
            Mat4d::translation(-position->getCenterVsop87Pos());
 
161
 
 
162
 
 
163
        // These two next have to take into account the position of the observer on the earth
 
164
        Mat4d tmp =
 
165
            mat_j2000_to_vsop87 *
 
166
            mat_earth_equ_to_j2000 *
 
167
        mat_local_to_earth_equ;
 
168
 
 
169
        mat_local_to_helio =  Mat4d::translation(position->getCenterVsop87Pos()) *
 
170
                              tmp *
 
171
                              Mat4d::translation(Vec3d(0.,0., position->getDistanceFromCenter()));
 
172
 
 
173
        mat_helio_to_local =  Mat4d::translation(Vec3d(0.,0.,-position->getDistanceFromCenter())) *
 
174
                              tmp.transpose() *
 
175
                              Mat4d::translation(-position->getCenterVsop87Pos());
 
176
}
 
177
 
 
178
////////////////////////////////////////////////////////////////////////////////
 
179
// Update the modelview matrices
 
180
void Navigator::updateModelViewMat(void)
 
181
{
 
182
 
 
183
        Vec3d f;
 
184
 
 
185
        if( viewing_mode == VIEW_EQUATOR)
 
186
        {
 
187
                // view will use equatorial coordinates, so that north is always up
 
188
                f = equ_vision;
 
189
        }
 
190
        else
 
191
        {
 
192
                // view will correct for horizon (always down)
 
193
                f = local_vision;
 
194
        }
 
195
 
 
196
 
 
197
        f.normalize();
 
198
        Vec3d s(f[1],-f[0],0.);
 
199
 
 
200
 
 
201
        if( viewing_mode == VIEW_EQUATOR)
 
202
        {
 
203
                // convert everything back to local coord
 
204
                f = local_vision;
 
205
                f.normalize();
 
206
                s = earth_equ_to_local( s );
 
207
        }
 
208
 
 
209
        Vec3d u(s^f);
 
210
        s.normalize();
 
211
        u.normalize();
 
212
 
 
213
        mat_local_to_eye.set(s[0],u[0],-f[0],0.,
 
214
                             s[1],u[1],-f[1],0.,
 
215
                             s[2],u[2],-f[2],0.,
 
216
                             0.,0.,0.,1.);
 
217
 
 
218
//johannes
 
219
//    mat_local_to_eye =  Mat4d::zrotation(0.5*M_PI) * mat_local_to_eye;
 
220
 
 
221
        mat_earth_equ_to_eye = mat_local_to_eye*mat_earth_equ_to_local;
 
222
        mat_helio_to_eye = mat_local_to_eye*mat_helio_to_local;
 
223
        mat_j2000_to_eye = mat_earth_equ_to_eye*mat_j2000_to_earth_equ;
 
224
}
 
225
 
 
226
////////////////////////////////////////////////////////////////////////////////
 
227
// Return the observer heliocentric position
 
228
Vec3d Navigator::getObserverHelioPos(void) const
 
229
{
 
230
        Vec3d v(0.,0.,0.);
 
231
        return mat_local_to_helio*v;
 
232
}
 
233
 
 
234
 
 
235
 
 
236
////////////////////////////////////////////////////////////////////////////////
 
237
// Set type of viewing mode (align with horizon or equatorial coordinates)
 
238
void Navigator::setViewingMode(ViewingModeType view_mode)
 
239
{
 
240
        viewing_mode = view_mode;
 
241
 
 
242
        // TODO: include some nice smoothing function trigger here to rotate between
 
243
        // the two modes
 
244
 
 
245
}