~ubuntu-branches/debian/jessie/stellarium/jessie

« back to all changes in this revision

Viewing changes to src/modules/StarMgr.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2009-03-13 20:07:22 UTC
  • mfrom: (1.1.8 upstream)
  • mto: (11.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20090313200722-gbgujsmzsa8a02ty
Import upstream version 0.10.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include <QRegExp>
31
31
#include <QDebug>
32
32
 
33
 
#include "Projector.hpp"
 
33
#include "StelProjector.hpp"
34
34
#include "StarMgr.hpp"
35
35
#include "StelObject.hpp"
36
 
#include "STexture.hpp"
37
 
#include "Navigator.hpp"
 
36
#include "StelTexture.hpp"
 
37
#include "StelNavigator.hpp"
38
38
#include "StelUtils.hpp"
39
 
#include "ToneReproducer.hpp"
40
 
#include "Translator.hpp"
41
 
#include "GeodesicGrid.hpp"
42
 
#include "LoadingBar.hpp"
43
 
#include "Translator.hpp"
 
39
#include "StelToneReproducer.hpp"
 
40
#include "StelTranslator.hpp"
 
41
#include "StelGeodesicGrid.hpp"
 
42
#include "StelLoadingBar.hpp"
 
43
#include "StelTranslator.hpp"
44
44
#include "StelApp.hpp"
45
45
#include "StelTextureMgr.hpp"
46
46
#include "StelObjectMgr.hpp"
53
53
#include "StelCore.hpp"
54
54
#include "StelIniParser.hpp"
55
55
#include "StelStyle.hpp"
 
56
#include "StelPainter.hpp"
56
57
 
57
58
#include "ZoneArray.hpp"
58
 
#include "StringArray.hpp"
59
59
 
60
60
#include <list>
61
61
 
64
64
 
65
65
using namespace BigStarCatalogExtension;
66
66
 
67
 
static StringArray spectral_array;
68
 
static StringArray component_array;
69
 
 
70
 
QString StarMgr::convertToSpectralType(int index) {
71
 
  if (index < 0 || index >= spectral_array.getSize()) {
72
 
    qDebug() << "convertToSpectralType: bad index: " << index
73
 
             << ", max: " << spectral_array.getSize();
74
 
    return "";
75
 
  }
76
 
  return spectral_array[index];
77
 
}
78
 
 
79
 
QString StarMgr::convertToComponentIds(int index) {
80
 
  if (index < 0 || index >= component_array.getSize()) {
81
 
    qDebug() << "convertToComponentIds: bad index: " << index
82
 
             << ", max: " << component_array.getSize();
83
 
    return "";
84
 
  }
85
 
  return component_array[index];
86
 
}
87
 
 
88
 
 
89
 
 
 
67
static QStringList spectral_array;
 
68
static QStringList component_array;
 
69
 
 
70
// Initialise statics
 
71
bool StarMgr::flagSciNames = true;
 
72
std::map<int,QString> StarMgr::commonNamesMap;
 
73
std::map<int,QString> StarMgr::commonNamesMapI18n;
 
74
std::map<QString,int> StarMgr::commonNamesIndex;
 
75
std::map<QString,int> StarMgr::commonNamesIndexI18n;
 
76
std::map<int,QString> StarMgr::sciNamesMapI18n;
 
77
std::map<QString,int> StarMgr::sciNamesIndexI18n;
 
78
 
 
79
QStringList initStringListFromFile(const QString& file_name)
 
80
{
 
81
        QStringList list;
 
82
        QFile f(file_name);
 
83
        if (f.open(QIODevice::ReadOnly | QIODevice::Text))
 
84
        {
 
85
                while (!f.atEnd())
 
86
                {
 
87
                        QString s = QString::fromUtf8(f.readLine());
 
88
                        s.chop(1);
 
89
                        list << s;
 
90
                }
 
91
                f.close();
 
92
        }
 
93
        return list;
 
94
}
 
95
 
 
96
QString StarMgr::convertToSpectralType(int index)
 
97
{
 
98
        if (index < 0 || index >= spectral_array.size())
 
99
        {
 
100
                qDebug() << "convertToSpectralType: bad index: " << index << ", max: " << spectral_array.size();
 
101
        return "";
 
102
        }
 
103
        return spectral_array.at(index);
 
104
}
 
105
 
 
106
QString StarMgr::convertToComponentIds(int index)
 
107
{
 
108
        if (index < 0 || index >= component_array.size())
 
109
        {
 
110
                qDebug() << "convertToComponentIds: bad index: " << index << ", max: " << component_array.size();
 
111
                return "";
 
112
        }
 
113
        return component_array.at(index);
 
114
}
90
115
 
91
116
 
92
117
void StarMgr::initTriangle(int lev,int index,
98
123
}
99
124
 
100
125
 
101
 
StarMgr::StarMgr(void) :
102
 
    hipIndex(new HipIndexStruct[NR_OF_HIP+1]),
103
 
        fontSize(13.),
104
 
    starFont(0)
 
126
StarMgr::StarMgr(void) : hipIndex(new HipIndexStruct[NR_OF_HIP+1]), fontSize(13.), starFont(NULL)
105
127
{
106
128
        setObjectName("StarMgr");
107
 
  if (hipIndex == 0) {
108
 
    qWarning() << "ERROR: StarMgr::StarMgr: no memory";
109
 
    exit(1);
110
 
  }
111
 
  maxGeodesicGridLevel = -1;
112
 
  lastMaxSearchLevel = -1;
 
129
        if (hipIndex == 0)
 
130
        {
 
131
                qFatal("ERROR: StarMgr::StarMgr: no memory");
 
132
        }
 
133
        maxGeodesicGridLevel = -1;
 
134
        lastMaxSearchLevel = -1;
 
135
        objectMgr = GETSTELMODULE(StelObjectMgr);
 
136
        Q_ASSERT(objectMgr);
113
137
}
114
138
 
115
139
/*************************************************************************
123
147
}
124
148
 
125
149
 
126
 
StarMgr::~StarMgr(void) {
127
 
  ZoneArrayMap::iterator it(zoneArrays.end());
128
 
  while (it!=zoneArrays.begin()) {
129
 
    --it;
130
 
    delete it->second;
131
 
    it->second = NULL;
132
 
  }
133
 
  zoneArrays.clear();
134
 
  if (hipIndex) delete[] hipIndex;
135
 
}
136
 
 
137
 
bool StarMgr::flagSciNames = true;
138
 
double StarMgr::currentJDay = 0;
139
 
std::map<int,QString> StarMgr::commonNamesMap;
140
 
std::map<int,QString> StarMgr::commonNamesMapI18n;
141
 
std::map<QString,int> StarMgr::commonNamesIndex;
142
 
std::map<QString,int> StarMgr::commonNamesIndexI18n;
143
 
 
144
 
std::map<int,QString> StarMgr::sciNamesMapI18n;
145
 
std::map<QString,int> StarMgr::sciNamesIndexI18n;
146
 
 
147
 
QString StarMgr::getCommonName(int hip) {
148
 
  std::map<int,QString>::const_iterator it(commonNamesMapI18n.find(hip));
149
 
  if (it!=commonNamesMapI18n.end()) return it->second;
150
 
  return "";
151
 
}
152
 
 
153
 
QString StarMgr::getSciName(int hip) {
154
 
  std::map<int,QString>::const_iterator it(sciNamesMapI18n.find(hip));
155
 
  if (it!=sciNamesMapI18n.end()) return it->second;
156
 
  return "";
157
 
}
158
 
 
159
 
 
160
 
 
161
 
 
162
 
void StarMgr::init() {
 
150
StarMgr::~StarMgr(void)
 
151
{
 
152
        delete starSettings;
 
153
        starSettings=NULL;
 
154
        ZoneArrayMap::iterator it(zoneArrays.end());
 
155
        while (it!=zoneArrays.begin())
 
156
        {
 
157
                --it;
 
158
                delete it->second;
 
159
                it->second = NULL;
 
160
        }
 
161
        zoneArrays.clear();
 
162
        if (hipIndex)
 
163
                delete[] hipIndex;
 
164
}
 
165
 
 
166
QString StarMgr::getCommonName(int hip)
 
167
{
 
168
        std::map<int,QString>::const_iterator it(commonNamesMapI18n.find(hip));
 
169
        if (it!=commonNamesMapI18n.end())
 
170
                return it->second;
 
171
        return QString();
 
172
}
 
173
 
 
174
QString StarMgr::getSciName(int hip)
 
175
{
 
176
        std::map<int,QString>::const_iterator it(sciNamesMapI18n.find(hip));
 
177
        if (it!=sciNamesMapI18n.end())
 
178
                return it->second;
 
179
        return QString();
 
180
}
 
181
 
 
182
void StarMgr::init()
 
183
{
163
184
        QSettings* conf = StelApp::getInstance().getSettings();
164
 
        assert(conf);
 
185
        Q_ASSERT(conf);
165
186
 
 
187
        loadStarSettings();
166
188
        loadData();
167
189
        double fontSize = 12;
168
190
        starFont = &StelApp::getInstance().getFontManager().getStandardFont(StelApp::getInstance().getLocaleMgr().getSkyLanguage(), fontSize);
171
193
        setFlagLabels(conf->value("astro/flag_star_name",true).toBool());
172
194
        setLabelsAmount(conf->value("stars/labels_amount",3).toDouble());
173
195
        
174
 
        StelApp::getInstance().getStelObjectMgr().registerStelObjectMgr(this);
 
196
        objectMgr->registerStelObjectMgr(this);
175
197
 
176
198
        StelApp::getInstance().getTextureManager().setDefaultParams();
 
199
        StelApp::getInstance().getTextureManager().setMinFilter(GL_LINEAR);
177
200
        texPointer = StelApp::getInstance().getTextureManager().createTexture("pointeur2.png");   // Load pointer texture
178
 
}
179
 
 
180
 
void StarMgr::setGrid(GeodesicGrid* geodesicGrid) {
181
 
  geodesicGrid->visitTriangles(maxGeodesicGridLevel,initTriangleFunc,this);
182
 
  for (ZoneArrayMap::const_iterator it(zoneArrays.begin());
183
 
       it!=zoneArrays.end();it++) {
184
 
    it->second->scaleAxis();
185
 
  }
186
 
}
187
 
 
188
 
 
189
 
void StarMgr::drawPointer(const Projector* prj, const Navigator * nav)
 
201
        
 
202
        StelApp::getInstance().getCore()->getGeodesicGrid(maxGeodesicGridLevel)->visitTriangles(maxGeodesicGridLevel,initTriangleFunc,this);
 
203
        for (ZoneArrayMap::const_iterator it(zoneArrays.begin()); it!=zoneArrays.end();it++)
 
204
        {
 
205
                it->second->scaleAxis();
 
206
        }
 
207
}
 
208
 
 
209
 
 
210
void StarMgr::drawPointer(const StelProjectorP& prj, const StelNavigator * nav)
190
211
{
191
 
        const QList<StelObjectP> newSelected = StelApp::getInstance().getStelObjectMgr().getSelectedObject("Star");
 
212
        const QList<StelObjectP> newSelected = objectMgr->getSelectedObject("Star");
192
213
        if (!newSelected.empty())
193
214
        {
194
215
                const StelObjectP obj = newSelected[0];
195
 
                Vec3d pos=obj->getObsJ2000Pos(nav);
 
216
                Vec3d pos=obj->getJ2000EquatorialPos(nav);
196
217
                Vec3d screenpos;
197
218
                // Compute 2D pos and return if outside screen
198
 
                if (!prj->project(pos, screenpos)) return;
 
219
                if (!prj->project(pos, screenpos))
 
220
                        return;
199
221
        
 
222
                StelPainter sPainter(prj);
200
223
                glColor3fv(obj->getInfoColor());
201
224
                float diameter = 26.f;
202
225
                texPointer->bind();
203
226
        glEnable(GL_TEXTURE_2D);
204
227
        glEnable(GL_BLEND);
205
228
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Normal transparency mode
206
 
        prj->drawSprite2dMode(screenpos[0], screenpos[1], diameter, StelApp::getInstance().getTotalRunTime()*40.);
 
229
                sPainter.drawSprite2dMode(screenpos[0], screenpos[1], diameter, StelApp::getInstance().getTotalRunTime()*40.);
207
230
        }
208
231
}
209
232
 
217
240
        setLabelColor(StelUtils::strToVec3f(conf->value(section+"/star_label_color", defaultColor).toString()));
218
241
}
219
242
 
 
243
void StarMgr::loadStarSettings()
 
244
{
 
245
        QString iniFile;
 
246
        try
 
247
        {
 
248
                iniFile = StelApp::getInstance().getFileMgr().findFile("stars/default/stars.ini");
 
249
        }
 
250
        catch (std::runtime_error& e)
 
251
        {
 
252
                qWarning() << "ERROR - could not find stars/default/stars.ini : " << e.what() << iniFile;
 
253
                return;
 
254
        }
 
255
 
 
256
        starSettings = new QSettings(iniFile, StelIniFormat);
 
257
        if (starSettings->status() != QSettings::NoError)
 
258
        {
 
259
                qWarning() << "ERROR while parsing " << iniFile;
 
260
                return;
 
261
        }
 
262
        starSettings->beginGroup("stars");
 
263
}
 
264
 
220
265
/***************************************************************************
221
266
 Load star catalogue data from files.
222
267
 If a file is not found, it will be skipped.
223
268
***************************************************************************/
224
269
void StarMgr::loadData()
225
270
{
226
 
        LoadingBar& lb = *StelApp::getInstance().getLoadingBar();
 
271
        StelLoadingBar& lb = *StelApp::getInstance().getStelLoadingBar();
227
272
                        
228
273
        // Please do not init twice:
229
 
        assert(maxGeodesicGridLevel < 0);
 
274
        Q_ASSERT(maxGeodesicGridLevel < 0);
230
275
 
231
276
        qDebug() << "Loading star data ...";
232
 
 
233
 
        QString iniFile;
234
 
        try
235
 
        {
236
 
                iniFile = StelApp::getInstance().getFileMgr().findFile("stars/default/stars.ini");
237
 
        }
238
 
        catch (std::runtime_error& e)
239
 
        {
240
 
                qWarning() << "ERROR - could not find stars/default/stars.ini : " << e.what() << iniFile;
241
 
                return;
242
 
        }
243
 
 
244
 
        QSettings conf(iniFile, StelIniFormat);
245
 
        if (conf.status() != QSettings::NoError)
246
 
        {
247
 
                qWarning() << "ERROR while parsing " << iniFile;
248
 
                return;
249
 
        }
250
 
                                         
251
 
        for (int i=0; i<100; i++)
252
 
        {
253
 
                //sprintf(key_name,"cat_file_name_%02d",i);
254
 
                QString keyName = QString("cat_file_name_%1").arg(i,2,10,QChar('0'));
255
 
                const QString cat_file_name = conf.value(QString("stars/")+keyName,"").toString();
256
 
                if (!cat_file_name.isEmpty()) {
257
 
                        lb.SetMessage(q_("Loading catalog %1").arg(cat_file_name));
258
 
                        ZoneArray *const z = ZoneArray::create(*this,cat_file_name,lb);
259
 
                        if (z)
260
 
                        {
261
 
                                if (maxGeodesicGridLevel < z->level)
262
 
                                {
263
 
                                        maxGeodesicGridLevel = z->level;
264
 
                                }
265
 
                                ZoneArray *&pos(zoneArrays[z->level]);
266
 
                                if (pos)
267
 
                                {
268
 
                                        qDebug() << cat_file_name << ", " << z->level << ": duplicate level";
269
 
                                        delete z;
270
 
                                }
271
 
                                else
272
 
                                {
273
 
                                        pos = z;
274
 
                                }
 
277
        
 
278
        qulonglong memoryUsed = 0;
 
279
        const qulonglong maxMemory = StelApp::getInstance().getSettings()->value("stars/max_memory", 128).toULongLong() * 1024*1024;
 
280
        
 
281
        QStringList cats = starSettings->childGroups();
 
282
        QListIterator<QString> it(cats);
 
283
        StelFileMgr& fileMgr = StelApp::getInstance().getFileMgr();
 
284
        while(it.hasNext())
 
285
        {
 
286
                QString cat = it.next();
 
287
                QString cat_file_name = starSettings->value(cat+"/path").toString();
 
288
                QString cat_file_path = fileMgr.findFile("stars/default/"+cat_file_name);
 
289
                
 
290
                lb.SetMessage(q_("Loading catalog %1 from file %2").arg(cat, cat_file_name));
 
291
                memoryUsed += fileMgr.size(cat_file_path);
 
292
                ZoneArray *const z = ZoneArray::create(cat_file_name, memoryUsed > maxMemory, lb);
 
293
                if (z)
 
294
                {
 
295
                        if (maxGeodesicGridLevel < z->level)
 
296
                        {
 
297
                                maxGeodesicGridLevel = z->level;
 
298
                        }
 
299
                        ZoneArray *&pos(zoneArrays[z->level]);
 
300
                        if (pos)
 
301
                        {
 
302
                                qDebug() << cat_file_name << ", " << z->level << ": duplicate level";
 
303
                                delete z;
 
304
                        }
 
305
                        else
 
306
                        {
 
307
                                pos = z;
275
308
                        }
276
309
                }
277
310
        }
288
321
                it->second->updateHipIndex(hipIndex);
289
322
        }
290
323
 
291
 
        const QString cat_hip_sp_file_name = conf.value("stars/cat_hip_sp_file_name","").toString();
 
324
        const QString cat_hip_sp_file_name = starSettings->value("cat_hip_sp_file_name","").toString();
292
325
        if (cat_hip_sp_file_name.isEmpty())
293
326
        {
294
327
                qWarning() << "ERROR: stars:cat_hip_sp_file_name not found";
297
330
        {
298
331
                try
299
332
                {
300
 
                        spectral_array.initFromFile(StelApp::getInstance().getFileMgr().findFile("stars/default/" + cat_hip_sp_file_name));
 
333
                        spectral_array = initStringListFromFile(fileMgr.findFile("stars/default/" + cat_hip_sp_file_name));
301
334
                }
302
335
                catch (std::runtime_error& e)
303
336
                {
307
340
                }
308
341
        }
309
342
 
310
 
        const QString cat_hip_cids_file_name = conf.value("stars/cat_hip_cids_file_name","").toString();
 
343
        const QString cat_hip_cids_file_name = starSettings->value("cat_hip_cids_file_name","").toString();
311
344
        if (cat_hip_cids_file_name.isEmpty())
312
345
        {
313
346
                qWarning() << "ERROR: stars:cat_hip_cids_file_name not found";
316
349
        {
317
350
                try
318
351
                {
319
 
                        component_array.initFromFile(StelApp::getInstance().getFileMgr()
320
 
                                .findFile("stars/default/" + cat_hip_cids_file_name));
 
352
                        component_array = initStringListFromFile(fileMgr.findFile("stars/default/" + cat_hip_cids_file_name));
321
353
                }
322
354
                catch (std::runtime_error& e)
323
355
                {
332
364
}
333
365
 
334
366
// Load common names from file 
335
 
int StarMgr::loadCommonNames(const QString& commonNameFile) {
 
367
int StarMgr::loadCommonNames(const QString& commonNameFile)
 
368
{
336
369
        commonNamesMap.clear();
337
370
        commonNamesMapI18n.clear();
338
371
        commonNamesIndex.clear();
491
524
 
492
525
int StarMgr::getMaxSearchLevel() const
493
526
{
494
 
  int rval = -1;
495
 
  for (ZoneArrayMap::const_iterator it(zoneArrays.begin());
496
 
       it!=zoneArrays.end();it++) {
497
 
    const float mag_min = 0.001f*it->second->mag_min;
498
 
    float rcmag[2];
499
 
    if (StelApp::getInstance().getCore()->getSkyDrawer()->computeRCMag(mag_min,rcmag)==false)
500
 
                break;
501
 
    rval = it->first;
502
 
  }
503
 
  return rval;
 
527
        int rval = -1;
 
528
        for (ZoneArrayMap::const_iterator it(zoneArrays.begin());it!=zoneArrays.end();++it)
 
529
        {
 
530
                const float mag_min = 0.001f*it->second->mag_min;
 
531
                float rcmag[2];
 
532
                if (StelApp::getInstance().getCore()->getSkyDrawer()->computeRCMag(mag_min,rcmag)==false)
 
533
                        break;
 
534
                rval = it->first;
 
535
        }
 
536
        return rval;
504
537
}
505
538
 
506
539
 
507
540
// Draw all the stars
508
541
void StarMgr::draw(StelCore* core)
509
542
{
510
 
        Navigator* nav = core->getNavigation();
511
 
        Projector* prj = core->getProjection();
512
 
        SkyDrawer* skyDrawer = core->getSkyDrawer();
513
 
        
514
 
    currentJDay = nav->getJDay();
 
543
        StelNavigator* nav = core->getNavigator();
 
544
        const StelProjectorP prj = core->getProjection(StelCore::FrameJ2000);
 
545
        StelSkyDrawer* skyDrawer = core->getSkyDrawer();
515
546
 
516
 
    // If stars are turned off don't waste time below
517
 
    // projecting all stars just to draw disembodied labels
518
 
    if (!starsFader.getInterstate())
 
547
        // If stars are turned off don't waste time below
 
548
        // projecting all stars just to draw disembodied labels
 
549
        if (!starsFader.getInterstate())
519
550
                return;
520
551
 
521
552
        int maxSearchLevel = getMaxSearchLevel();
522
 
        const GeodesicSearchResult* geodesic_search_result = core->getGeodesicGrid()->search(prj->unprojectViewport(),maxSearchLevel);
 
553
        const GeodesicSearchResult* geodesic_search_result = core->getGeodesicGrid(maxSearchLevel)->search(prj->getViewportConvexPolygon(),maxSearchLevel);
523
554
 
524
 
    // Set temporary static variable for optimization
525
 
    const float names_brightness = labelsFader.getInterstate() * starsFader.getInterstate();
526
 
    
527
 
    prj->setCurrentFrame(Projector::FrameJ2000);
 
555
        // Set temporary static variable for optimization
 
556
        const float names_brightness = labelsFader.getInterstate() * starsFader.getInterstate();
528
557
 
529
558
        // Prepare openGL for drawing many stars
530
 
        skyDrawer->preDrawPointSource();
 
559
        StelPainter* sPainter = new StelPainter(prj);
 
560
        skyDrawer->preDrawPointSource(sPainter);
 
561
        Q_ASSERT(sPainter);
531
562
 
532
 
    // draw all the stars of all the selected zones
533
 
    float rcmag_table[2*256];
 
563
        // draw all the stars of all the selected zones
 
564
        float rcmag_table[2*256];
534
565
        
535
 
    for (ZoneArrayMap::const_iterator it(zoneArrays.begin()); it!=zoneArrays.end();it++)
 
566
        for (ZoneArrayMap::const_iterator it(zoneArrays.begin()); it!=zoneArrays.end();++it)
536
567
        {
537
568
                const float mag_min = 0.001f*it->second->mag_min;
538
569
                const float k = (0.001f*it->second->mag_range)/it->second->mag_steps;
539
 
                for (int i=it->second->mag_steps-1;i>=0;i--)
 
570
                for (int i=it->second->mag_steps-1;i>=0;--i)
540
571
                {
541
572
                        const float mag = mag_min+k*i;
542
573
                        if (skyDrawer->computeRCMag(mag,rcmag_table + 2*i)==false)
565
596
                }
566
597
                int zone;
567
598
                for (GeodesicSearchInsideIterator it1(*geodesic_search_result,it->first);(zone = it1.next()) >= 0;)
568
 
                        it->second->draw(zone, true, rcmag_table, prj, maxMagStarName, names_brightness, starFont);
 
599
                        it->second->draw(zone, true, rcmag_table, core, maxMagStarName, names_brightness, starFont);
569
600
                for (GeodesicSearchBorderIterator it1(*geodesic_search_result,it->first);(zone = it1.next()) >= 0;)
570
 
                        it->second->draw(zone, false, rcmag_table, prj, maxMagStarName,names_brightness, starFont);
571
 
    }
572
 
    exit_loop:
 
601
                        it->second->draw(zone, false, rcmag_table, core, maxMagStarName,names_brightness, starFont);
 
602
        }
 
603
        exit_loop:
573
604
        // Finish drawing many stars
574
605
        skyDrawer->postDrawPointSource();
575
606
        
576
 
        drawPointer(prj, nav);
577
 
}
578
 
 
579
 
 
580
 
 
581
 
 
582
 
 
583
 
 
584
 
// Look for a star by XYZ coords
585
 
StelObjectP StarMgr::search(Vec3d pos) const {
586
 
assert(0);
587
 
  pos.normalize();
588
 
  QList<StelObjectP > v = searchAround(pos,
589
 
                                        0.8, // just an arbitrary number
590
 
                                        NULL);
591
 
  StelObjectP nearest;
592
 
  double cos_angle_nearest = -10.0;
593
 
  for (QList<StelObjectP >::const_iterator it(v.begin());it!=v.end();it++) {
594
 
    const double c = (*it)->getObsJ2000Pos(0)*pos;
595
 
    if (c > cos_angle_nearest) {
596
 
      cos_angle_nearest = c;
597
 
      nearest = *it;
598
 
    }
599
 
  }
600
 
  return nearest;
601
 
}
 
607
        delete sPainter;
 
608
        sPainter = NULL;
 
609
        
 
610
        if (objectMgr->getFlagSelectedObjectPointer())
 
611
                drawPointer(prj, nav);
 
612
}
 
613
 
602
614
 
603
615
// Return a stl vector containing the stars located
604
616
// inside the limFov circle around position v
605
 
QList<StelObjectP > StarMgr::searchAround(const Vec3d& vv,
606
 
                                           double limFov, // degrees
607
 
                                                                                   const StelCore* core) const {
608
 
  QList<StelObjectP > result;
609
 
  if (!getFlagStars())
610
 
        return result;
611
 
        
612
 
  Vec3d v(vv);
613
 
  v.normalize();
614
 
    // find any vectors h0 and h1 (length 1), so that h0*v=h1*v=h0*h1=0
615
 
  int i;
616
 
  {
617
 
    const double a0 = fabs(v[0]);
618
 
    const double a1 = fabs(v[1]);
619
 
    const double a2 = fabs(v[2]);
620
 
    if (a0 <= a1) {
621
 
      if (a0 <= a2) i = 0;
622
 
      else i = 2;
623
 
    } else {
624
 
      if (a1 <= a2) i = 1;
625
 
      else i = 2;
626
 
    }
627
 
  }
628
 
  Vec3d h0(0.0,0.0,0.0);
629
 
  h0[i] = 1.0;
630
 
  Vec3d h1 = h0 ^ v;
631
 
  h1.normalize();
632
 
  h0 = h1 ^ v;
633
 
  h0.normalize();
634
 
    // now we have h0*v=h1*v=h0*h1=0.
635
 
    // construct a region with 4 corners e0,e1,e2,e3 inside which
636
 
    // all desired stars must be:
637
 
  double f = 1.4142136 * tan(limFov * M_PI/180.0);
638
 
  h0 *= f;
639
 
  h1 *= f;
640
 
  Vec3d e0 = v + h0;
641
 
  Vec3d e1 = v + h1;
642
 
  Vec3d e2 = v - h0;
643
 
  Vec3d e3 = v - h1;
644
 
  f = 1.0/e0.length();
645
 
  e0 *= f;
646
 
  e1 *= f;
647
 
  e2 *= f;
648
 
  e3 *= f;
649
 
    // search the triangles
650
 
        const GeodesicSearchResult* geodesic_search_result = core->getGeodesicGrid()->search(e3,e2,e1,e0,lastMaxSearchLevel);
651
 
    // iterate over the stars inside the triangles:
652
 
  f = cos(limFov * M_PI/180.);
653
 
  for (ZoneArrayMap::const_iterator it(zoneArrays.begin());
654
 
       it!=zoneArrays.end();it++) {
655
 
//qDebug() << "search inside(" << it->first << "):";
656
 
    int zone;
657
 
    for (GeodesicSearchInsideIterator it1(*geodesic_search_result,it->first);
658
 
         (zone = it1.next()) >= 0;) {
659
 
      it->second->searchAround(zone,v,f,result);
660
 
//qDebug() << " " << zone;
661
 
    }
662
 
//qDebug() << endl << "search border(" << it->first << "):";
663
 
    for (GeodesicSearchBorderIterator it1(*geodesic_search_result,it->first);
664
 
         (zone = it1.next()) >= 0;) {
665
 
      it->second->searchAround(zone,v,f,result);
666
 
//qDebug() << " " << zone;
667
 
    }
668
 
  }
669
 
  return result;
 
617
QList<StelObjectP > StarMgr::searchAround(const Vec3d& vv, double limFov, const StelCore* core) const
 
618
{
 
619
        QList<StelObjectP > result;
 
620
        if (!getFlagStars())
 
621
                return result;
 
622
                
 
623
        Vec3d v(vv);
 
624
        v.normalize();
 
625
        
 
626
        // find any vectors h0 and h1 (length 1), so that h0*v=h1*v=h0*h1=0
 
627
        int i;
 
628
        {
 
629
                const double a0 = fabs(v[0]);
 
630
                const double a1 = fabs(v[1]);
 
631
                const double a2 = fabs(v[2]);
 
632
                if (a0 <= a1)
 
633
                {
 
634
                        if (a0 <= a2) i = 0;
 
635
                        else i = 2;
 
636
                } else
 
637
                {
 
638
                        if (a1 <= a2) i = 1;
 
639
                        else i = 2;
 
640
                }
 
641
        }
 
642
        Vec3d h0(0.0,0.0,0.0);
 
643
        h0[i] = 1.0;
 
644
        Vec3d h1 = h0 ^ v;
 
645
        h1.normalize();
 
646
        h0 = h1 ^ v;
 
647
        h0.normalize();
 
648
        
 
649
        // Now we have h0*v=h1*v=h0*h1=0.
 
650
        // Construct a region with 4 corners e0,e1,e2,e3 inside which all desired stars must be:
 
651
        double f = 1.4142136 * tan(limFov * M_PI/180.0);
 
652
        h0 *= f;
 
653
        h1 *= f;
 
654
        Vec3d e0 = v + h0;
 
655
        Vec3d e1 = v + h1;
 
656
        Vec3d e2 = v - h0;
 
657
        Vec3d e3 = v - h1;
 
658
        f = 1.0/e0.length();
 
659
        e0 *= f;
 
660
        e1 *= f;
 
661
        e2 *= f;
 
662
        e3 *= f;
 
663
        // Search the triangles
 
664
        const GeodesicSearchResult* geodesic_search_result = core->getGeodesicGrid(lastMaxSearchLevel)->search(e3,e2,e1,e0,lastMaxSearchLevel);
 
665
        
 
666
        // Iterate over the stars inside the triangles
 
667
        f = cos(limFov * M_PI/180.);
 
668
        for (ZoneArrayMap::const_iterator it(zoneArrays.begin());it!=zoneArrays.end();it++)
 
669
        {
 
670
                //qDebug() << "search inside(" << it->first << "):";
 
671
                int zone;
 
672
                for (GeodesicSearchInsideIterator it1(*geodesic_search_result,it->first);(zone = it1.next()) >= 0;)
 
673
                {
 
674
                        it->second->searchAround(core->getNavigator(), zone,v,f,result);
 
675
                        //qDebug() << " " << zone;
 
676
                }
 
677
                //qDebug() << endl << "search border(" << it->first << "):";
 
678
                for (GeodesicSearchBorderIterator it1(*geodesic_search_result,it->first); (zone = it1.next()) >= 0;)
 
679
                {
 
680
                        it->second->searchAround(core->getNavigator(), zone,v,f,result);
 
681
                        //qDebug() << " " << zone;
 
682
                }
 
683
        }
 
684
        return result;
670
685
}
671
686
 
672
687
 
673
 
 
674
 
 
675
 
 
676
 
 
677
688
//! Update i18 names from english names according to passed translator.
678
689
//! The translation is done using gettext with translated strings defined in translations.h
679
 
void StarMgr::updateI18n() {
680
 
  Translator trans = StelApp::getInstance().getLocaleMgr().getSkyTranslator();
681
 
  commonNamesMapI18n.clear();
682
 
  commonNamesIndexI18n.clear();
683
 
  for (std::map<int,QString>::iterator it(commonNamesMap.begin());
684
 
       it!=commonNamesMap.end();it++) {
685
 
    const int i = it->first;
686
 
    const QString t(trans.qtranslate(it->second));
687
 
    commonNamesMapI18n[i] = t;
688
 
    commonNamesIndexI18n[t.toUpper()] = i;
689
 
  }
690
 
  starFont = &StelApp::getInstance().getFontManager().getStandardFont(trans.getTrueLocaleName(), fontSize);
 
690
void StarMgr::updateI18n()
 
691
{
 
692
        StelTranslator trans = StelApp::getInstance().getLocaleMgr().getSkyTranslator();
 
693
        commonNamesMapI18n.clear();
 
694
        commonNamesIndexI18n.clear();
 
695
        for (std::map<int,QString>::iterator it(commonNamesMap.begin());it!=commonNamesMap.end();it++)
 
696
        {
 
697
                const int i = it->first;
 
698
                const QString t(trans.qtranslate(it->second));
 
699
                commonNamesMapI18n[i] = t;
 
700
                commonNamesIndexI18n[t.toUpper()] = i;
 
701
        }
 
702
        starFont = &StelApp::getInstance().getFontManager().getStandardFont(trans.getTrueLocaleName(), fontSize);
691
703
}
692
704
 
693
 
 
694
 
StelObjectP StarMgr::search(const QString& name) const
695
 
{
696
 
        // Use this QRegExp to extract the catalogue number and prefix
697
 
        QRegExp catRx("^(HP|HD|SAO)\\s*(\\d+)$");
698
 
        QString n = name.toUpper();
699
 
        n.replace('_', ' ');
700
 
 
701
 
        if (catRx.exactMatch(n))
702
 
        {
703
 
                QString cat = catRx.capturedTexts().at(1);
704
 
                if (cat=="HP")
705
 
                        return searchHP(catRx.capturedTexts().at(2).toInt());
706
 
                else // currently we only support searching by string for HP catalogue
707
 
                        return NULL;
708
 
        }
709
 
        else 
710
 
        {
711
 
                // Maybe the HP prefix is missing and we just have a number...
712
 
                bool ok;
713
 
                int num = n.toInt(&ok);
714
 
                if (!ok)
715
 
                        return NULL;
716
 
                else
717
 
                        return searchHP(num);
718
 
        }
719
 
}    
720
 
 
721
705
// Search the star by HP number
722
 
StelObjectP StarMgr::searchHP(int _HP) const {
723
 
  if (0 < _HP && _HP <= NR_OF_HIP) {
724
 
    const Star1 *const s = hipIndex[_HP].s;
725
 
    if (s) {
726
 
      const SpecialZoneArray<Star1> *const a = hipIndex[_HP].a;
727
 
      const SpecialZoneData<Star1> *const z = hipIndex[_HP].z;
728
 
      return s->createStelObject(a,z);
729
 
    }
730
 
  }
731
 
  return StelObjectP();
 
706
StelObjectP StarMgr::searchHP(int hp) const
 
707
{
 
708
        if (0 < hp && hp <= NR_OF_HIP)
 
709
        {
 
710
                const Star1 *const s = hipIndex[hp].s;
 
711
                if (s)
 
712
                {
 
713
                        const SpecialZoneArray<Star1> *const a = hipIndex[hp].a;
 
714
                        const SpecialZoneData<Star1> *const z = hipIndex[hp].z;
 
715
                        return s->createStelObject(a,z);
 
716
                }
 
717
        }
 
718
        return StelObjectP();
732
719
}
733
720
 
734
721
StelObjectP StarMgr::searchByNameI18n(const QString& nameI18n) const
736
723
        QString objw = nameI18n.toUpper();
737
724
 
738
725
        // Search by HP number if it's an HP formated number
739
 
        QRegExp rx("^\\s*HP\\s*(\\d+)\\s*$", Qt::CaseInsensitive);
 
726
        QRegExp rx("^\\s*(HIP|HP)\\s*(\\d+)\\s*$", Qt::CaseInsensitive);
740
727
        if (rx.exactMatch(objw))
741
728
        {
742
 
                return searchHP(rx.capturedTexts().at(1).toInt());
 
729
                return searchHP(rx.capturedTexts().at(2).toInt());
743
730
        }
744
731
 
745
732
        // Search by I18n common name
765
752
        QString objw = name.toUpper();
766
753
 
767
754
        // Search by HP number if it's an HP formated number
768
 
        QRegExp rx("^\\s*HP\\s*(\\d+)\\s*$", Qt::CaseInsensitive);
 
755
        QRegExp rx("^\\s*(HP|HIP)\\s*(\\d+)\\s*$", Qt::CaseInsensitive);
769
756
        if (rx.exactMatch(objw))
770
757
        {
771
 
                return searchHP(rx.capturedTexts().at(1).toInt());
 
758
                return searchHP(rx.capturedTexts().at(2).toInt());
772
759
        }
773
760
 
774
 
 
775
 
        /* Should we try this anyway?
776
 
        // Search by common name
777
 
        std::map<QString,int>::const_iterator it(commonNamesIndexI18n.find(objw));
778
 
 
779
 
        if (it!=commonNamesIndexI18n.end()) {
780
 
                return searchHP(it->second);
781
 
        } */
782
 
 
783
761
        // Search by sci name
784
762
        std::map<QString,int>::const_iterator it = sciNamesIndexI18n.find(objw);
785
 
        if (it!=sciNamesIndexI18n.end()) {
 
763
        if (it!=sciNamesIndexI18n.end())
 
764
        {
786
765
                return searchHP(it->second);
787
766
        }
788
767
 
790
769
}
791
770
 
792
771
//! Find and return the list of at most maxNbItem objects auto-completing
793
 
//! the passed object I18n name
 
772
//! the passed object I18n name.
794
773
QStringList StarMgr::listMatchingObjectsI18n(const QString& objPrefix, int maxNbItem) const 
795
774
{
796
775
        QStringList result;
799
778
        QString objw = objPrefix.toUpper();
800
779
 
801
780
        // Search for common names
802
 
        for (std::map<QString,int>::const_iterator it(commonNamesIndexI18n.lower_bound(objw));
803
 
             it!=commonNamesIndexI18n.end();
804
 
             it++) 
 
781
        for (std::map<QString,int>::const_iterator it(commonNamesIndexI18n.lower_bound(objw)); it!=commonNamesIndexI18n.end(); ++it) 
805
782
        {
806
 
                const QString constw(it->first.mid(0,objw.size()));
807
 
                if (constw==objw) {
808
 
                        if (maxNbItem==0) break;
 
783
                if (it->first.startsWith(objw))
 
784
                {
 
785
                        if (maxNbItem==0)
 
786
                                break;
809
787
                        result << getCommonName(it->second);
810
 
                        maxNbItem--;
 
788
                        --maxNbItem;
811
789
                } 
812
790
                else 
813
791
                        break;
814
792
        }
815
793
 
816
794
        // Search for sci names
817
 
        for (std::map<QString,int>::const_iterator it(sciNamesIndexI18n.lower_bound(objw));
818
 
             it!=sciNamesIndexI18n.end();
819
 
             it++) 
 
795
        QString bayerPattern = objw;
 
796
        QRegExp bayerRegEx(bayerPattern);
 
797
        
 
798
        // if the first character is a Greek letter, check if there's an index
 
799
        // after it, such as "alpha1 Cen".
 
800
        if (objw.at(0).unicode() >= 0x0391 && objw.at(0).unicode() <= 0x03A9)
 
801
                bayerRegEx.setPattern(bayerPattern.insert(1,"\\d?"));
 
802
        
 
803
        for (std::map<QString,int>::const_iterator it(sciNamesIndexI18n.lower_bound(objw)); it!=sciNamesIndexI18n.end(); ++it) 
820
804
        {
821
 
                const QString constw(it->first.mid(0,objw.size()));
822
 
                if (constw==objw) {
823
 
                        if (maxNbItem==0) break;
 
805
                if (it->first.indexOf(bayerRegEx)==0)
 
806
                {
 
807
                        if (maxNbItem==0)
 
808
                                break;
824
809
                        result << getSciName(it->second);
825
 
                        maxNbItem--;
826
 
                } 
827
 
                else 
 
810
                        --maxNbItem;
 
811
                }
 
812
                else if (it->first.at(0) != objw.at(0))
828
813
                        break;
829
814
        }
830
815
 
 
816
        // Add exact Hp catalogue numbers
 
817
        QRegExp hpRx("^(HIP|HP)\\s*(\\d+)\\s*$");
 
818
        hpRx.setCaseSensitivity(Qt::CaseInsensitive);
 
819
        if (hpRx.exactMatch(objw))
 
820
        {
 
821
                bool ok;
 
822
                int hpNum = hpRx.capturedTexts().at(2).toInt(&ok);
 
823
                if (ok)
 
824
                {
 
825
                        StelObjectP s = searchHP(hpNum);
 
826
                        if (s && maxNbItem>0)
 
827
                        {
 
828
                                result << QString("HIP%1").arg(hpNum);
 
829
                                maxNbItem--;
 
830
                        }
 
831
                }
 
832
        }
 
833
 
831
834
        result.sort();
832
835
        return result;
833
836
}
841
844
                StelApp::getInstance().getLocaleMgr().getSkyLanguage(),fontSize);
842
845
}
843
846
 
844
 
void StarMgr::updateSkyCulture()
 
847
void StarMgr::updateSkyCulture(const QString& skyCultureDir)
845
848
{
846
 
        QString skyCultureDir = StelApp::getInstance().getSkyCultureMgr().getCurrentSkyCultureID();
847
 
        
848
849
        // Load culture star names in english
849
850
        try
850
851
        {