ZoneArray.cpp   ZoneArray.cpp 
skipping to change at line 20 skipping to change at line 20
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA.
*/ */
#include "config.h"
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
#ifdef WIN32 #ifdef Q_OS_WIN
#include <io.h> #include <io.h>
#include <windows.h> #include <windows.h>
#endif #endif
#include "ZoneArray.hpp" #include "ZoneArray.hpp"
#include "StelApp.hpp" #include "StelApp.hpp"
#include "StelFileMgr.hpp" #include "StelFileMgr.hpp"
#include "StelGeodesicGrid.hpp" #include "StelGeodesicGrid.hpp"
#include "StelObject.hpp" #include "StelObject.hpp"
#include "StelNavigator.hpp" #include "StelNavigator.hpp"
#ifdef HAVE_BYTESWAP_H
#include <byteswap.h>
#else
static unsigned int bswap_32(unsigned int val) {
return (((val) & 0xff000000) >> 24) | (((val) & 0x00ff0000) >> 8) |
(((val) & 0x0000ff00) << 8) | (((val) & 0x000000ff) << 24);
}
#endif
namespace BigStarCatalogExtension namespace BigStarCatalogExtension
{ {
static const Vec3d north(0,0,1); static const Vec3d north(0,0,1);
void ZoneArray::initTriangle(int index, const Vec3d &c0, const Vec3d &c1, void ZoneArray::initTriangle(int index, const Vec3d &c0, const Vec3d &c1,
const Vec3d &c2) const Vec3d &c2)
{ {
// initialize center,axis0,axis1: // initialize center,axis0,axis1:
ZoneData &z(zones[index]); ZoneData &z(zones[index]);
z.center = c0+c1+c2; z.center = c0+c1+c2;
z.center.normalize(); z.center.normalize();
z.axis0 = north ^ z.center; z.axis0 = north ^ z.center;
z.axis0.normalize(); z.axis0.normalize();
z.axis1 = z.center ^ z.axis0; z.axis1 = z.center ^ z.axis0;
// initialize star_position_scale: // initialize star_position_scale:
double mu0,mu1,f,h; double mu0,mu1,f,h;
skipping to change at line 75 skipping to change at line 85
if (star_position_scale < h) star_position_scale = h; if (star_position_scale < h) star_position_scale = h;
mu0 = (c2-z.center)*z.axis0; mu0 = (c2-z.center)*z.axis0;
mu1 = (c2-z.center)*z.axis1; mu1 = (c2-z.center)*z.axis1;
f = 1.0/sqrt(1.0-mu0*mu0-mu1*mu1); f = 1.0/sqrt(1.0-mu0*mu0-mu1*mu1);
h = fabs(mu0)*f; h = fabs(mu0)*f;
if (star_position_scale < h) star_position_scale = h; if (star_position_scale < h) star_position_scale = h;
h = fabs(mu1)*f; h = fabs(mu1)*f;
if (star_position_scale < h) star_position_scale = h; if (star_position_scale < h) star_position_scale = h;
} }
static inline static inline int ReadInt(QFile& file, unsigned int &x)
int ReadInt(QFile& file, unsigned int &x)
{ {
const int rval = (4 == file.read((char*)&x, 4)) const int rval = (4 == file.read((char*)&x, 4)) ? 0 : -1;
? 0 : -1;
return rval; return rval;
} }
#if (!defined(__GNUC__)) #if (!defined(__GNUC__))
#warning Star catalogue loading has only been tested with gcc #warning Star catalogue loading has only been tested with gcc
#endif #endif
ZoneArray *ZoneArray::create(const QString& extended_file_name, bool use_mm ZoneArray* ZoneArray::create(const QString& catalogFilePath, bool use_mmap,
ap, StelLoadingBar* lb)
StelLoadingBar &lb)
{ {
QString fname(extended_file_name);
QString dbStr; // for debugging output. QString dbStr; // for debugging output.
try QFile* file = new QFile(catalogFilePath);
{ if (!file->open(QIODevice::ReadOnly))
fname = StelApp::getInstance().getFileMgr().findFile("stars/
default/"+fname);
}
catch (std::runtime_error &e)
{
qDebug() << "Loading" << extended_file_name << e.what();
return 0;
}
QFile* file = new QFile(fname);
if(!file->open(QIODevice::ReadOnly))
{ {
qDebug() << "Loading" << extended_file_name << "failed to op en file."; qWarning() << "Error while loading " << catalogFilePath << " : failed to open file.";
return 0; return 0;
} }
dbStr = "Loading \"" + extended_file_name + "\": "; dbStr = "Loading \"" + catalogFilePath + "\": ";
unsigned int magic,major,minor,type,level,mag_min,mag_range,mag_step s; unsigned int magic,major,minor,type,level,mag_min,mag_range,mag_step s;
if (ReadInt(*file,magic) < 0 || if (ReadInt(*file,magic) < 0 ||
ReadInt(*file,type) < 0 || ReadInt(*file,type) < 0 ||
ReadInt(*file,major) < 0 || ReadInt(*file,major) < 0 ||
ReadInt(*file,minor) < 0 || ReadInt(*file,minor) < 0 ||
ReadInt(*file,level) < 0 || ReadInt(*file,level) < 0 ||
ReadInt(*file,mag_min) < 0 || ReadInt(*file,mag_min) < 0 ||
ReadInt(*file,mag_range) < 0 || ReadInt(*file,mag_range) < 0 ||
ReadInt(*file,mag_steps) < 0) ReadInt(*file,mag_steps) < 0)
{ {
dbStr += "error - file format is bad."; dbStr += "error - file format is bad.";
qDebug() << dbStr; qDebug() << dbStr;
return 0; return 0;
} }
const bool byte_swap = (magic == FILE_MAGIC_OTHER_ENDIAN); const bool byte_swap = (magic == FILE_MAGIC_OTHER_ENDIAN);
if (byte_swap) if (byte_swap)
{ {
// ok, FILE_MAGIC_OTHER_ENDIAN, must swap // ok, FILE_MAGIC_OTHER_ENDIAN, must swap
if (use_mmap) if (use_mmap)
{ {
dbStr += "warning - must convert catalogue "; dbStr += "warning - must convert catalogue ";
#if (!defined(__GNUC__)) #if (!defined(__GNUC__))
dbStr += "to native format "; dbStr += "to native format ";
#endif #endif
dbStr += "before mmap loading"; dbStr += "before mmap loading";
qDebug() << dbStr; qWarning() << dbStr;
return 0; use_mmap = false;
qWarning() << "Revert to not using mmmap";
//return 0;
} }
dbStr += "byteswap "; dbStr += "byteswap ";
type = bswap_32(type); type = bswap_32(type);
major = bswap_32(major); major = bswap_32(major);
minor = bswap_32(minor); minor = bswap_32(minor);
level = bswap_32(level); level = bswap_32(level);
mag_min = bswap_32(mag_min); mag_min = bswap_32(mag_min);
mag_range = bswap_32(mag_range); mag_range = bswap_32(mag_range);
mag_steps = bswap_32(mag_steps); mag_steps = bswap_32(mag_steps);
} }
else if (magic == FILE_MAGIC) else if (magic == FILE_MAGIC)
{ {
// ok, FILE_MAGIC // ok, FILE_MAGIC
#if (!defined(__GNUC__)) #if (!defined(__GNUC__))
if (use_mmap) if (use_mmap)
{ {
// mmap only with gcc: // mmap only with gcc:
dbStr += "warning - you must convert catalogue " dbStr += "warning - you must convert catalogue "
+= "to native format before mmap loading"; += "to native format before mmap loading";
qDebug(qPrintable(dbStr)); qDebug(qPrintable(dbStr));
return 0; return 0;
} }
#endif #endif
} }
else if (magic == FILE_MAGIC_NATIVE) else if (magic == FILE_MAGIC_NATIVE)
{ {
// ok, will work for any architecture and any compiler // ok, will work for any architecture and any compiler
} }
else else
{ {
dbStr += "error - not a catalogue file."; dbStr += "error - not a catalogue file.";
qDebug() << dbStr; qDebug() << dbStr;
return 0; return 0;
} }
ZoneArray *rval = 0; ZoneArray *rval = 0;
dbStr += QString("%1_%2v%3_%4; ").arg(level) dbStr += QString("%1_%2v%3_%4; ").arg(level).arg(type).arg(major).ar
.arg(type) g(minor);
.arg(major)
.arg(minor);
switch (type) switch (type)
{ {
case 0: case 0:
if (major > MAX_MAJOR_FILE_VERSION) if (major > MAX_MAJOR_FILE_VERSION)
{ {
dbStr += "warning - unsupported version "; dbStr += "warning - unsupported version ";
} }
else else
{ {
skipping to change at line 246 skipping to change at line 242
} }
} }
break; break;
default: default:
dbStr += "error - bad file type "; dbStr += "error - bad file type ";
break; break;
} }
if (rval && rval->isInitialized()) if (rval && rval->isInitialized())
{ {
dbStr += QString("%1").arg(rval->getNrOfStars()); dbStr += QString("%1").arg(rval->getNrOfStars());
qDebug() << qPrintable(dbStr); qDebug() << dbStr;
} }
else else
{ {
dbStr += " - initialization failed"; dbStr += " - initialization failed";
qDebug() << dbStr; qDebug() << dbStr;
if (rval) if (rval)
{ {
delete rval; delete rval;
rval = 0; rval = 0;
} }
} }
return rval; return rval;
} }
ZoneArray::ZoneArray(const QString& fname, QFile* file, int level, int mag_ min, ZoneArray::ZoneArray(const QString& fname, QFile* file, int level, int mag_ min,
int mag_range, int mag_steps) int mag_range, int mag_steps)
: fname(fname), level(level), mag_min(mag_min), : fname(fname), level(level), mag_min(mag_min),
mag_range(mag_range), mag_steps(mag_steps), mag_range(mag_range), mag_steps(mag_steps),
star_position_scale(0.0), zones(0), file(file) star_position_scale(0.0), zones(0), file(file)
{ {
nr_of_zones = StelGeodesicGrid::nrOfZones(level); nr_of_zones = StelGeodesicGrid::nrOfZones(level);
nr_of_stars = 0; nr_of_stars = 0;
} }
bool ZoneArray::readFileWithStelLoadingBar(QFile& file, void *data, qint64 bool ZoneArray::readFileWithStelLoadingBar(QFile& file, void *data, qint64
size, size, StelLoadingBar*lb)
StelLoadingBar &lb)
{ {
int parts = 256; int parts = 256;
int part_size = (size + (parts>>1)) / parts; int part_size = (size + (parts>>1)) / parts;
if (part_size < 64*1024) if (part_size < 64*1024)
{ {
part_size = 64*1024; part_size = 64*1024;
parts = (size + (part_size>>1)) / part_size; parts = (size + (part_size>>1)) / part_size;
} }
float i = 0.f; float i = 0.f;
lb.Draw(i / parts); if (lb)
lb->Draw(i / parts);
i += 1.f; i += 1.f;
while (size > 0) while (size > 0)
{ {
const int to_read = (part_size < size) ? part_size : size; const int to_read = (part_size < size) ? part_size : size;
const int read_rc = file.read((char*)data, to_read); const int read_rc = file.read((char*)data, to_read);
if (read_rc != to_read) return false; if (read_rc != to_read) return false;
size -= read_rc; size -= read_rc;
data = ((char*)data) + read_rc; data = ((char*)data) + read_rc;
lb.Draw(i / parts); if (lb)
lb->Draw(i / parts);
i += 1.f; i += 1.f;
} }
return true; return true;
} }
void HipZoneArray::updateHipIndex(HipIndexStruct hipIndex[]) const void HipZoneArray::updateHipIndex(HipIndexStruct hipIndex[]) const
{ {
for (const SpecialZoneData<Star1> *z=getZones()+(nr_of_zones-1);z>=g etZones();z--) for (const SpecialZoneData<Star1> *z=getZones()+(nr_of_zones-1);z>=g etZones();z--)
{ {
for (const Star1 *s = z->getStars()+z->size-1;s>=z->getStars ();s--) for (const Star1 *s = z->getStars()+z->size-1;s>=z->getStars ();s--)
skipping to change at line 332 skipping to change at line 329
star_position_scale /= Star::MaxPosVal; star_position_scale /= Star::MaxPosVal;
for (ZoneData *z=zones+(nr_of_zones-1);z>=zones;z--) for (ZoneData *z=zones+(nr_of_zones-1);z>=zones;z--)
{ {
z->axis0 *= star_position_scale; z->axis0 *= star_position_scale;
z->axis1 *= star_position_scale; z->axis1 *= star_position_scale;
} }
} }
template<class Star> template<class Star>
SpecialZoneArray<Star>::SpecialZoneArray(QFile* file, bool byte_swap,bool u se_mmap, SpecialZoneArray<Star>::SpecialZoneArray(QFile* file, bool byte_swap,bool u se_mmap,
StelLoadingBar &lb, int level, int mag_min, StelLoadingBar*lb, int level, int m ag_min,
int mag_range, int mag_steps) int mag_range, int mag_steps)
: ZoneArray(file->fileName(), file, level, mag_min, mag_rang e, mag_steps), : ZoneArray(file->fileName(), file, level, mag_min, mag_rang e, mag_steps),
stars(0), mmap_start(0) stars(0), mmap_start(0)
{ {
if (nr_of_zones > 0) if (nr_of_zones > 0)
{ {
lb.Draw(0.f); if (lb)
lb->Draw(0.f);
zones = new SpecialZoneData<Star>[nr_of_zones]; zones = new SpecialZoneData<Star>[nr_of_zones];
if (zones == 0) if (zones == 0)
{ {
qDebug() << "ERROR: SpecialZoneArray(" << level qDebug() << "ERROR: SpecialZoneArray(" << level
<< ")::SpecialZoneArray: no memory (1)"; << ")::SpecialZoneArray: no memory (1)";
exit(1); exit(1);
} }
unsigned int *zone_size = new unsigned int[nr_of_zones]; unsigned int *zone_size = new unsigned int[nr_of_zones];
if (zone_size == 0) if (zone_size == 0)
skipping to change at line 443 skipping to change at line 441
else else
{ {
Star *s = stars; Star *s = stars;
for (unsigned int z=0;z<nr_of_zones; z++) for (unsigned int z=0;z<nr_of_zones; z++)
{ {
getZones()[z].stars = s; getZones()[z].stars = s;
s += getZones()[z].size; s += getZones()[z].size;
} }
if ( if (
#if (!defined(__GNUC__)) #if (!defined(__GNUC__))
true true
#else #else
byte_swap byte_swap
#endif #endif
) )
{ {
s = stars; s = stars;
for (unsigned int i=0;i<nr_o f_stars;i++,s++) for (unsigned int i=0;i<nr_o f_stars;i++,s++)
{ {
s->repack( s->repack(
#ifdef WORDS_BIGENDIAN #if Q_BYTE_ORDER == Q_BIG_ENDIAN
// need for byte // need for
_swap on a BE machine means that catalog is LE byte_swap on a BE machine means that catalog is LE
!byte_swap !byte_swap
#else #else
// need for byte // need for
_swap on a LE machine means that catalog is BE byte_swap on a LE machine means that catalog is BE
byte_swap byte_swap
#endif #endif
); );
} }
} }
} }
file->close(); file->close();
} }
} }
lb.Draw(1.f); if (lb)
lb->Draw(1.f);
} }
} }
template<class Star> template<class Star>
SpecialZoneArray<Star>::~SpecialZoneArray(void) SpecialZoneArray<Star>::~SpecialZoneArray(void)
{ {
if (stars) if (stars)
{ {
if (mmap_start != 0) if (mmap_start != 0)
{ {
skipping to change at line 497 skipping to change at line 496
if (zones) if (zones)
{ {
delete[] getZones(); delete[] getZones();
zones = NULL; zones = NULL;
} }
nr_of_zones = 0; nr_of_zones = 0;
nr_of_stars = 0; nr_of_stars = 0;
} }
template<class Star> template<class Star>
void SpecialZoneArray<Star>::draw(int index, bool is_inside, const float *r void SpecialZoneArray<Star>::draw(StelPainter* sPainter, int index, bool is
cmag_table, StelCore* core, unsigned int maxMagStarName, _inside, const float *rcmag_table, StelCore* core, unsigned int maxMagStarN
float names_brightness, StelFont *starFont ame,
) const float names_brightness) const
{ {
StelSkyDrawer* drawer = core->getSkyDrawer(); StelSkyDrawer* drawer = core->getSkyDrawer();
const StelProjectorP& prj = core->getProjection(StelCore::FrameJ2000 );
SpecialZoneData<Star> *const z = getZones() + index; SpecialZoneData<Star> *const z = getZones() + index;
Vec3d xy; Vec3d v;
const Star *const end = z->getStars() + z->size; const Star *const end = z->getStars() + z->size;
const double d2000 = 2451545.0; static const double d2000 = 2451545.0;
const double movementFactor = (M_PI/180)*(0.0001/3600) * ((core->get Navigator()->getJDay()-d2000)/365.25) / star_position_scale; const double movementFactor = (M_PI/180)*(0.0001/3600) * ((core->get Navigator()->getJDay()-d2000)/365.25) / star_position_scale;
const float* tmpRcmag;
for (const Star *s=z->getStars();s<end;++s) for (const Star *s=z->getStars();s<end;++s)
{ {
if (is_inside ? prj->project(s->getJ2000Pos(z,movementFactor tmpRcmag = rcmag_table+2*s->mag;
),xy) : prj->projectCheck(s->getJ2000Pos(z,movementFactor),xy)) if (*tmpRcmag<=0.)
{ break;
if (drawer->drawPointSource(xy[0],xy[1],rcmag_table s->getJ2000Pos(z,movementFactor, v);
+ 2*(s->mag),s->bV)==false) if (drawer->drawPointSource(sPainter, v,tmpRcmag,s->bV, !is_
{ inside) && s->hasName() && s->mag < maxMagStarName)
break; {
} const float offset = *tmpRcmag*0.7;
if (s->mag < maxMagStarName) const Vec3f& colorr = (StelApp::getInstance().getVis
{ ionModeNight() ? Vec3f(0.8, 0.2, 0.2) : StelSkyDrawer::indexToColor(s->bV))
const QString& starname = s->getNameI18n(); *0.75;
if (!starname.isEmpty()) sPainter->setColor(colorr[0], colorr[1], colorr[2],n
{ ames_brightness);
const float offset = (rcmag_table + sPainter->drawText(v, s->getNameI18n(), 0, offset, o
2*(s->mag))[0]*0.7; ffset, false);
const Vec3f& colorr = (StelApp::getI
nstance().getVisionModeNight() ? Vec3f(0.8, 0.2, 0.2) : StelSkyDrawer::inde
xToColor(s->bV))*0.75;
glColor4f(colorr[0], colorr[1], colo
rr[2],names_brightness);
drawer->getPainter()->drawText(starF
ont,xy[0],xy[1], starname, 0, offset, offset, false);
}
}
} }
} }
} }
template<class Star> template<class Star>
void SpecialZoneArray<Star>::searchAround(const StelNavigator* nav, int ind ex, const Vec3d &v, double cosLimFov, void SpecialZoneArray<Star>::searchAround(const StelNavigator* nav, int ind ex, const Vec3d &v, double cosLimFov,
QList<StelObjectP > &result) QList<StelObjectP > &result)
{ {
const double d2000 = 2451545.0; static const double d2000 = 2451545.0;
const double movementFactor = (M_PI/180.)*(0.0001/3600.) * ((nav->ge tJDay()-d2000)/365.25)/ star_position_scale; const double movementFactor = (M_PI/180.)*(0.0001/3600.) * ((nav->ge tJDay()-d2000)/365.25)/ star_position_scale;
const SpecialZoneData<Star> *const z = getZones()+index; const SpecialZoneData<Star> *const z = getZones()+index;
Vec3d tmp; Vec3d tmp;
for (int i=0;i<z->size;i++) for (const Star* s=z->getStars();s<z->getStars()+z->size;++s)
{ {
tmp = z->getStars()[i].getJ2000Pos(z,movementFactor); s->getJ2000Pos(z,movementFactor, tmp);
tmp.normalize(); tmp.normalize();
if (tmp*v >= cosLimFov) if (tmp*v >= cosLimFov)
{ {
// TODO: do not select stars that are too faint to d isplay // TODO: do not select stars that are too faint to d isplay
result.push_back(z->getStars()[i].createStelObject(t his,z)); result.push_back(s->createStelObject(this,z));
} }
} }
} }
} // namespace BigStarCatalogExtension } // namespace BigStarCatalogExtension
 End of changes. 38 change blocks. 
93 lines changed or deleted 83 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/