File indexing completed on 2024-07-14 07:17:24

0001 /*
0002     SPDX-FileCopyrightText: 2005 Thomas Kabelmann <thomas.kabelmann@gmx.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "ksnumbers.h"
0010 #include "listcomponent.h"
0011 #include "skylabel.h"
0012 #include "stardata.h"
0013 #include "skyobjects/starobject.h"
0014 
0015 #include <memory>
0016 
0017 #ifdef KSTARS_LITE
0018 class StarItem;
0019 #endif
0020 
0021 class KStarsSplash;
0022 class BinFileHelper;
0023 class DeepStarComponent;
0024 class HighPMStarList;
0025 class MeshIterator;
0026 class SkyLabeler;
0027 class SkyMesh;
0028 class StarObject;
0029 class StarBlockFactory;
0030 
0031 #define MAX_LINENUMBER_MAG 90
0032 
0033 /**
0034  * @class StarComponent
0035  *
0036  * @short Represents the stars on the sky map. For optimization reasons the stars are not
0037  * separate objects and are stored in a list.
0038  *
0039  * The StarComponent class manages all stars drawn in KStars. While it handles all stars
0040  * having names using its own member methods, it shunts the responsibility of unnamed stars
0041  * to the class 'DeepStarComponent', objects of which it maintains.
0042  *
0043  * @author Thomas Kabelmann
0044  * @author Akarsh Simha
0045  * @version 1.0
0046  */
0047 class StarComponent : public ListComponent
0048 {
0049 #ifdef KSTARS_LITE
0050     friend class StarItem; //Needs access to faintMagnitude() and reindex()
0051 #endif
0052 
0053   protected:
0054     StarComponent(SkyComposite *);
0055 
0056   public:
0057     ~StarComponent() override;
0058 
0059     // TODO: Desingletonize StarComponent
0060     /** @short Create an instance of StarComponent */
0061     static StarComponent *Create(SkyComposite *);
0062 
0063     /** @return the instance of StarComponent if already created, nullptr otherwise */
0064     static StarComponent *Instance() { return pinstance; }
0065 
0066     //This function is empty; we need that so that the JiT update
0067     //is the only one being used.
0068     void update(KSNumbers *num) override;
0069 
0070     bool selected() override;
0071 
0072     void draw(SkyPainter *skyp) override;
0073 
0074     /**
0075      * @short draw all the labels in the prioritized LabelLists and then clear the LabelLists.
0076      */
0077     void drawLabels();
0078 
0079     static float zoomMagnitudeLimit();
0080 
0081     SkyObject *objectNearest(SkyPoint *p, double &maxrad) override;
0082 
0083     virtual SkyObject *findStarByGenetiveName(const QString name);
0084 
0085     /**
0086      * @short Searches the region(s) and appends the SkyObjects found to the list of sky objects
0087      *
0088      * Look for a SkyObject that is in one of the regions
0089      * If found, then append to the list of sky objects
0090      * @p list list of SkyObject to which matching list has to be appended to
0091      * @p region defines the regions in which the search for SkyObject should be done within
0092      */
0093     void objectsInArea(QList<SkyObject *> &list, const SkyRegion &region) override;
0094 
0095     /**
0096      * @short Find stars by HD catalog index
0097      * @param HDnum HD Catalog Number of the star to find
0098      * @return If the star is a static star, a pointer to the star will be returned
0099      * If it is a dynamic star, a fake copy will be created that survives till
0100      * the next findByHDIndex() call. If no match was found, returns nullptr.
0101      */
0102     StarObject *findByHDIndex(int HDnum);
0103 
0104 
0105     /**
0106      * @short Append a star to the Object List. (including genetive name)
0107      *
0108      * Overrides ListComponent::appendListObject() to include genetive names of stars as well.
0109      */
0110     void appendListObject(SkyObject * object);
0111 
0112     /**
0113      * @short Add to the given list, the stars from this component, that lie within the
0114      * specified circular aperture, and that are brighter than the limiting magnitude specified.
0115      * @p center The center point of the aperture
0116      * @p radius The radius around the center point that defines the aperture
0117      * @p maglim Optional parameter indicating the limiting magnitude.
0118      * If magnitude limit is numerically < -28, the limiting magnitude
0119      * is assumed to be the limiting magnitude of the catalog (i.e. no magnitude limit)
0120      * @p list The list to operate on
0121      */
0122     void starsInAperture(QList<StarObject *> &list, const SkyPoint &center, float radius, float maglim = -29);
0123 
0124     // TODO: Make byteSwap a template method and put it in byteorder.h
0125     // It should ideally handle 32-bit, 16-bit fields and StarData and
0126     // DeepStarData fields
0127     static void byteSwap(StarData *stardata);
0128 
0129   private:
0130     /**
0131      * @short Read data for stars which will remain static in the memory
0132      *
0133      * This method reads data for named stars (stars having names, which are stored by
0134      * default in "namedstars.dat") into memory. These stars are always kept in memory,
0135      * as against 'deep' stars which are mostly loaded dynamically (KStars treats all
0136      * unnamed stars as 'deep' stars) into memory when required, depending on region
0137      * and magnitude limit. Once loading is successful, this method sets the starsLoaded flag to true
0138      */
0139     bool loadStaticData();
0140 
0141     /** @return the magnitude of the faintest star */
0142     float faintMagnitude() const;
0143 
0144     /** true if all stars(not only high PM ones) were reindexed else false**/
0145     bool reindex(KSNumbers *num);
0146 
0147     /** Adds a label to the lists of labels to be drawn prioritized by magnitude. */
0148     void addLabel(const QPointF &p, StarObject *star);
0149 
0150     void reindexAll(KSNumbers *num);
0151 
0152     /** Load available deep star catalogs */
0153     int loadDeepStarCatalogs();
0154 
0155     bool addDeepStarCatalogIfExists(const QString &fileName, float trigMag, bool staticstars = false);
0156 
0157     SkyMesh *m_skyMesh { nullptr };
0158     std::unique_ptr<StarIndex> m_starIndex;
0159 
0160     KSNumbers m_reindexNum;
0161     double m_reindexInterval { 0 };
0162 
0163     LabelList *m_labelList[MAX_LINENUMBER_MAG + 1];
0164     bool m_hideLabels { false };
0165 
0166     float m_zoomMagLimit { 0 };
0167 
0168     /// Limiting magnitude of the catalog currently loaded
0169     float m_FaintMagnitude { -5 };
0170     bool starsLoaded { false };
0171     /// Current limiting magnitude for visible stars
0172     float magLim { 0 };
0173 
0174     StarObject m_starObject;
0175     StarObject *focusStar { nullptr }; // This object is always drawn
0176 
0177     KStarsSplash *m_reindexSplash { nullptr };
0178 
0179     StarBlockFactory *m_StarBlockFactory { nullptr };
0180 
0181     QVector<HighPMStarList *> m_highPMStars;
0182     QHash<QString, SkyObject *> m_genName;
0183     QHash<int, StarObject *> m_HDHash;
0184     QVector<DeepStarComponent *> m_DeepStarComponents;
0185 
0186     /**
0187      * @struct starName
0188      * @brief Structure that holds star name information, to be read as-is from the
0189      * corresponding binary data file
0190      */
0191     typedef struct starName
0192     {
0193         char bayerName[8];
0194         char longName[32];
0195     } starName;
0196 
0197     StarData stardata;
0198     starName starname;
0199 
0200     static StarComponent *pinstance;
0201 };