/* -*- c++ -*- * Copyright ©2008 Hugo Mills * * This software is distributed under the terms of the GNU GPL v3 * For more information on the GPL, see the file COPYING or * visit http://www.gnu.org/ * * This software is distributed without warranty */ #include "xforbit_sunpos.h" #include "magellan/xfsphere.h" extern "C" PluginInfo* getmodules(int i) { if(i==0) { PluginInfo_Orbit* pi = new PluginInfo_Orbit(); pi->name = "Sun Position Orbit"; pi->author = "Hugo Mills"; pi->copyright = "©2008 Hugo Mills"; pi->version = 0x00001; pi->parser = XfOrbit_Sunpos::parser; return pi; } else return NULL; } XfOrbit* XfOrbit_Sunpos::parser( ConfigLexer* lex, const std::string& type, const std::string& subtype ) { if(type != "sunpos") return NULL; long timeorigin = 0; while(!lex->eos()) { if(lex->type() == TOKEN && lex->value() == "time-origin") { lex->next(); if(lex->type() == TOKEN && lex->value() == "now") { timeorigin = time(NULL); lex->next(); if(lex->type() != CHAR || lex->value() != ";") // Syntax error -- missing semicolon? throw(MissingSemicolon(lex)); } /* else if(lex->type() == STRING) { // Parse date and set timeorigin lex->next(); } */ else if(lex->type() == INTEGER) { // Parse date and set timeorigin timeorigin = time(NULL); int offset = atoi(lex->value().c_str()); timeorigin += offset; lex->next(); if(lex->type() != CHAR || lex->value() != ";") // Syntax error -- missing semicolon? throw(MissingSemicolon(lex)); } else throw(UnknownConfigToken(lex)); } else throw(UnknownConfigToken(lex)); } return new XfOrbit_Sunpos(timeorigin); } XfOrbit_Sunpos::XfOrbit_Sunpos(long t) : XfOrbit(), timeorigin(t) { reset(); } void XfOrbit_Sunpos::reset(void) { time_t tm = time(NULL); struct tm now; gmtime_r(&tm, &now); // One day is 86400 seconds double theta = -(now.tm_hour * 3600.0 + now.tm_min * 60.0 + now.tm_sec) / 43200.0 * M_PI; theta += M_PI; // Really awful approximation. // Offset by 80 days, as that's March 21st // Assume 365.25 days in the year double phi = sin( (now.tm_yday - 80) / 365.25) * (degrad(23.5)); setmatrices(theta, phi); }