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

« back to all changes in this revision

Viewing changes to src/xplanet.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 <clocale>
 
2
#include <cstdio>
 
3
#include <iostream>
 
4
#include <map>
 
5
#include <sstream>
 
6
#include <vector>
 
7
using namespace std;
 
8
 
 
9
#include <sys/time.h>
 
10
#include <unistd.h>
 
11
 
 
12
#include "config.h"
 
13
 
 
14
#include "buildPlanetMap.h"
 
15
#include "keywords.h"
 
16
#include "Options.h"
 
17
#include "PlanetProperties.h"
 
18
#include "readOriginFile.h"
 
19
#include "xpUtil.h"
 
20
 
 
21
#include "libannotate/libannotate.h"
 
22
#include "libdisplay/libdisplay.h"
 
23
#include "libdisplay/libtimer.h"
 
24
#include "libplanet/Planet.h"
 
25
 
 
26
extern void
 
27
drawMultipleBodies(DisplayBase *display, Planet *target,
 
28
                   map<double, Planet *> &planetsFromSunMap,
 
29
                   PlanetProperties *planetProperties[]);
 
30
 
 
31
extern void
 
32
drawProjection(DisplayBase *display, Planet *target,
 
33
               map<double, Planet *> &planetsFromSunMap,
 
34
               PlanetProperties *planetProperties);
 
35
 
 
36
extern void
 
37
readConfigFile(string configFile, PlanetProperties *planetProperties[]);
 
38
 
 
39
int
 
40
main(int argc, char **argv)
 
41
{
 
42
    if (setlocale(LC_ALL, "") == NULL)
 
43
    {
 
44
        ostringstream errMsg;
 
45
        errMsg << "Warning: setlocale(LC_ALL, \"\") failed! "
 
46
               << "Check your LANG environment variable "
 
47
               << "(currently ";
 
48
        char *lang = getenv("LANG");
 
49
        if (lang == NULL)
 
50
        {
 
51
            errMsg << "NULL";
 
52
        }
 
53
        else
 
54
        {
 
55
            errMsg << "\"" << lang << "\"";
 
56
        }
 
57
        errMsg << "). Setting to \"C\".\n";
 
58
        setlocale(LC_ALL, "C");
 
59
        cerr << errMsg.str();
 
60
    }
 
61
 
 
62
    Options *options = Options::getInstance();
 
63
    options->parseArgs(argc, argv);
 
64
 
 
65
    PlanetProperties *planetProperties[RANDOM_BODY];
 
66
    for (int i = 0; i < RANDOM_BODY; i++)
 
67
        planetProperties[i] = new PlanetProperties((body) i);
 
68
 
 
69
    // Load the drawing info for each planet
 
70
    readConfigFile(options->ConfigFile(), planetProperties);
 
71
 
 
72
#ifdef HAVE_CSPICE
 
73
    // Load any SPICE kernels
 
74
    loadSpiceKernels();
 
75
#endif
 
76
 
 
77
    // Load artificial satellite orbital elements
 
78
    if (!planetProperties[EARTH]->SatelliteFiles().empty())
 
79
        loadSatelliteVector(planetProperties[EARTH]);
 
80
 
 
81
    // If an origin file has been specified, read it
 
82
    const bool origin_file = !options->OriginFile().empty();
 
83
    vector<LBRPoint> originVector;
 
84
    if (origin_file) 
 
85
    {
 
86
        readOriginFile(options->OriginFile(), originVector);
 
87
        if (!options->InterpolateOriginFile())
 
88
            options->NumTimes(originVector.size());
 
89
    }
 
90
 
 
91
    vector<LBRPoint>::iterator iterOriginVector = originVector.begin();
 
92
 
 
93
    // Initialize the timer
 
94
    Timer *timer = getTimer(options->getWait(), options->Hibernate(),
 
95
                            options->IdleWait());
 
96
 
 
97
    int times_run = 0;
 
98
 
 
99
    while (1)
 
100
    {
 
101
        // Set the time for the next update
 
102
        timer->Update();
 
103
 
 
104
        if (!options->PrevCommand().empty())
 
105
        {
 
106
            if (system(options->PrevCommand().c_str()) != 0)
 
107
            {
 
108
                ostringstream errStr;
 
109
                errStr << "Can't execute " << options->PrevCommand() 
 
110
                       << "\n";
 
111
                xpWarn(errStr.str(), __FILE__, __LINE__);
 
112
            }
 
113
        }
 
114
 
 
115
        // Set the time to the current time, if desired
 
116
        if (options->UseCurrentTime())
 
117
        {
 
118
            struct timeval time;
 
119
            gettimeofday(&time, NULL);
 
120
            
 
121
            time_t t = time.tv_sec;
 
122
            const double julianDay = toJulian(gmtime(static_cast<time_t *> (&t))->tm_year + 1900,
 
123
                                              gmtime(static_cast<time_t *> (&t))->tm_mon + 1,
 
124
                                              gmtime(static_cast<time_t *> (&t))->tm_mday,
 
125
                                              gmtime(static_cast<time_t *> (&t))->tm_hour,
 
126
                                              gmtime(static_cast<time_t *> (&t))->tm_min,
 
127
                                              gmtime(static_cast<time_t *> (&t))->tm_sec);
 
128
            options->setTime(julianDay);
 
129
        }
 
130
        
 
131
        if (origin_file)
 
132
        {
 
133
            if (options->InterpolateOriginFile())
 
134
            {
 
135
                // interpolate positions in the origin file to the
 
136
                // current time
 
137
                double thisRad, thisLat, thisLon, thisLocalTime = -1;
 
138
                interpolateOriginFile(options->getJulianDay(), 
 
139
                                      originVector, thisRad, 
 
140
                                      thisLat, thisLon, thisLocalTime);
 
141
                options->setRange(thisRad);
 
142
                options->Latitude(thisLat);
 
143
                options->Longitude(thisLon);
 
144
                options->LocalTime(thisLocalTime);
 
145
            }
 
146
            else
 
147
            {
 
148
                // Use the next time and position in the origin file
 
149
                options->setTime(iterOriginVector->time);
 
150
                options->setRange(iterOriginVector->radius);
 
151
                options->Latitude(iterOriginVector->latitude);
 
152
                options->Longitude(iterOriginVector->longitude);
 
153
                options->LocalTime(iterOriginVector->localTime);
 
154
            }
 
155
        }
 
156
 
 
157
        if (!options->DynamicOrigin().empty())
 
158
        {
 
159
            LBRPoint originPoint;
 
160
            readDynamicOrigin(options->DynamicOrigin(), originPoint);
 
161
            options->setRange(originPoint.radius);
 
162
            options->Latitude(originPoint.latitude);
 
163
            options->Longitude(originPoint.longitude);
 
164
            options->LocalTime(originPoint.localTime);
 
165
        }
 
166
    
 
167
        options->setTarget(planetProperties);
 
168
        options->setOrigin(planetProperties);
 
169
 
 
170
        // Rectangular coordinates of the observer
 
171
        double oX, oY, oZ;
 
172
        options->getOrigin(oX, oY, oZ);
 
173
 
 
174
        // Calculate the positions of the planets & moons.  The map
 
175
        // container sorts on the key, so the bodies will be ordered
 
176
        // by heliocentric distance.  This makes calculating shadows
 
177
        // easier.
 
178
        map<double, Planet *> planetsFromSunMap;
 
179
        buildPlanetMap(options->getJulianDay(), oX, oY, oZ, 
 
180
                       options->LightTime(), planetsFromSunMap);
 
181
 
 
182
        // Find the target body in the list
 
183
        body target_body = options->getTarget();
 
184
        Planet *target = findPlanetinMap(planetsFromSunMap, target_body);
 
185
 
 
186
        // LOOKAT mode is where the target isn't a planetary body.
 
187
        // (e.g. the Cassini spacecraft)
 
188
        if (options->TargetMode() == LOOKAT)
 
189
        {
 
190
            if (options->LightTime()) options->setTarget(planetProperties);
 
191
        }
 
192
        else
 
193
        {
 
194
            if (target == NULL)
 
195
                xpExit("Can't find target body?\n", __FILE__, __LINE__);
 
196
 
 
197
            if (options->LightTime())
 
198
            {
 
199
                double tX, tY, tZ;
 
200
                target->getPosition(tX, tY, tZ);
 
201
                options->setTarget(tX, tY, tZ);
 
202
            }
 
203
 
 
204
            // Find the sub-observer lat & lon
 
205
            double obs_lat, obs_lon;
 
206
            target->XYZToPlanetographic(oX, oY, oZ, obs_lat, obs_lon);
 
207
            options->Latitude(obs_lat);
 
208
            options->Longitude(obs_lon);
 
209
 
 
210
            // Find the sub-solar lat & lon.  This is used for the image
 
211
            // label
 
212
            double sunLat, sunLon;
 
213
            target->XYZToPlanetographic(0, 0, 0, sunLat, sunLon);
 
214
            options->SunLat(sunLat);
 
215
            options->SunLon(sunLon);
 
216
        }
 
217
 
 
218
        // delete the markerbounds file, since we'll create a new one
 
219
        string markerBounds(options->MarkerBounds());
 
220
        if (!markerBounds.empty())
 
221
            unlinkFile(markerBounds.c_str());
 
222
 
 
223
        // Initialize display device
 
224
        DisplayBase *display = getDisplay(times_run);
 
225
 
 
226
        if (options->ProjectionMode() == MULTIPLE)
 
227
        {
 
228
            drawMultipleBodies(display, target, planetsFromSunMap,
 
229
                               planetProperties);
 
230
        }
 
231
        else
 
232
        {
 
233
            drawProjection(display, target, planetsFromSunMap, 
 
234
                           planetProperties[target->Index()]);
 
235
        }
 
236
 
 
237
        display->renderImage(planetProperties);
 
238
        delete display;
 
239
 
 
240
        destroyPlanetMap();
 
241
 
 
242
        times_run++;
 
243
 
 
244
        if (!options->PostCommand().empty())
 
245
        {
 
246
            if (system(options->PostCommand().c_str()) != 0)
 
247
            {
 
248
                ostringstream errStr;
 
249
                errStr << "Can't execute " << options->PostCommand()
 
250
                       << "\n";
 
251
                xpWarn(errStr.str(), __FILE__, __LINE__);
 
252
            }
 
253
        }
 
254
 
 
255
        if (options->NumTimes() > 0 && times_run >= options->NumTimes())
 
256
            break;
 
257
 
 
258
        if (origin_file && !options->InterpolateOriginFile())
 
259
        {
 
260
            // If we've run through the origin file, break out of the
 
261
            // while(1) loop.
 
262
            iterOriginVector++;
 
263
            if (iterOriginVector == originVector.end()) break;
 
264
        }
 
265
 
 
266
        if (!options->UseCurrentTime())
 
267
        {
 
268
            // Set the time to the next update
 
269
            options->incrementTime((time_t) (options->getTimeWarp() 
 
270
                                             * options->getWait()));
 
271
        }
 
272
 
 
273
        // Sleep until the next update.  If Sleep() returns false,
 
274
        // then quit.
 
275
        if (!timer->Sleep()) break;
 
276
    }
 
277
 
 
278
    delete timer;
 
279
 
 
280
    for (int i = 0; i < RANDOM_BODY; i++) delete planetProperties[i];
 
281
 
 
282
    return(EXIT_SUCCESS);
 
283
}