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

0001 /*
0002     SPDX-FileCopyrightText: 2008 Akarsh Simha <akarshsimha@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "starblocklist.h"
0008 
0009 #include "binfilehelper.h"
0010 #include "deepstarcomponent.h"
0011 #include "starblock.h"
0012 #include "starcomponent.h"
0013 
0014 #ifdef KSTARS_LITE
0015 #include "skymaplite.h"
0016 #include "kstarslite/skyitems/skynodes/pointsourcenode.h"
0017 #endif
0018 
0019 #include <QDebug>
0020 
0021 StarBlockList::StarBlockList(const Trixel &tr, DeepStarComponent *parent)
0022 {
0023     trixel       = tr;
0024     this->parent = parent;
0025     staticStars  = parent->hasStaticStars();
0026 }
0027 
0028 int StarBlockList::releaseBlock(StarBlock *block)
0029 {
0030     if (block != blocks[nBlocks - 1].get())
0031         qDebug() << Q_FUNC_INFO << "ERROR: Trying to release a block which is not the last block! Trixel = " << trixel;
0032 
0033     else if (blocks.size() > 0)
0034     {
0035 #ifdef KSTARS_LITE
0036         for (int i = 0; i < block->getStarCount(); ++i)
0037         {
0038             PointSourceNode *node = block->star(i)->starNode;
0039             if (node)
0040             {
0041                 SkyMapLite::Instance()->deleteSkyNode(node);
0042                 block->star(i)->starNode = 0;
0043             }
0044         }
0045 #endif
0046         blocks.removeLast();
0047         nBlocks--;
0048         nStars -= block->getStarCount();
0049 
0050         readOffset -= parent->getStarReader()->guessRecordSize() * block->getStarCount();
0051         if (nBlocks <= 0)
0052             faintMag = -5.0;
0053         else
0054             faintMag = blocks[nBlocks - 1]->faintMag;
0055 
0056         return 1;
0057     }
0058 
0059     return 0;
0060 }
0061 
0062 bool StarBlockList::fillToMag(float maglim)
0063 {
0064     // TODO: Remove staticity of BinFileHelper
0065     BinFileHelper *dSReader;
0066     StarBlockFactory *SBFactory;
0067     StarData stardata;
0068     DeepStarData deepstardata;
0069     FILE *dataFile;
0070 
0071     dSReader  = parent->getStarReader();
0072     dataFile  = dSReader->getFileHandle();
0073     SBFactory = StarBlockFactory::Instance();
0074 
0075     if (staticStars)
0076         return false;
0077 
0078     if (faintMag >= maglim)
0079         return true;
0080 
0081     if (!dataFile)
0082     {
0083         qDebug() << Q_FUNC_INFO << "dataFile not opened!";
0084         return false;
0085     }
0086 
0087     Trixel trixelId =
0088         trixel; //( ( trixel < 256 ) ? ( trixel + 256 ) : ( trixel - 256 ) ); // Trixel ID on datafile is assigned differently
0089 
0090     if (readOffset <= 0)
0091         readOffset = dSReader->getOffset(trixelId);
0092 
0093     Q_ASSERT(nBlocks == (unsigned int)blocks.size());
0094 
0095     BinFileHelper::unsigned_KDE_fseek(dataFile, readOffset, SEEK_SET);
0096 
0097     /*
0098     qDebug() << Q_FUNC_INFO << "Reading trixel" << trixel << ", id on disk =" << trixelId << ", currently nStars =" << nStars
0099              << ", record count =" << dSReader->getRecordCount( trixelId ) << ", first block = " << blocks[0]->getStarCount()
0100              << "to maglim =" << maglim << "with current faintMag =" << faintMag;
0101     */
0102 
0103     while (maglim >= faintMag && nStars < dSReader->getRecordCount(trixelId))
0104     {
0105         int ret = 0;
0106 
0107         if (nBlocks == 0 || blocks[nBlocks - 1]->isFull())
0108         {
0109             std::shared_ptr<StarBlock> newBlock = SBFactory->getBlock();
0110 
0111             if (!newBlock.get())
0112             {
0113                 qWarning() << "ERROR: Could not get a new block from StarBlockFactory::getBlock() in trixel " << trixel
0114                            << ", while trying to create block #" << nBlocks + 1;
0115                 return false;
0116             }
0117             blocks.append(newBlock);
0118             blocks[nBlocks]->parent = this;
0119             if (nBlocks == 0)
0120                 SBFactory->markFirst(blocks[0]);
0121             else if (!SBFactory->markNext(blocks[nBlocks - 1], blocks[nBlocks]))
0122                 qWarning() << "ERROR: markNext() failed on block #" << nBlocks + 1 << "in trixel" << trixel;
0123 
0124             ++nBlocks;
0125         }
0126         // TODO: Make this more general
0127         if (dSReader->guessRecordSize() == 32)
0128         {
0129             ret = fread(&stardata, sizeof(StarData), 1, dataFile);
0130             if (dSReader->getByteSwap())
0131                 DeepStarComponent::byteSwap(&stardata);
0132             readOffset += sizeof(StarData);
0133             blocks[nBlocks - 1]->addStar(stardata);
0134         }
0135         else
0136         {
0137             ret = fread(&deepstardata, sizeof(DeepStarData), 1, dataFile);
0138             if (dSReader->getByteSwap())
0139                 DeepStarComponent::byteSwap(&deepstardata);
0140             readOffset += sizeof(DeepStarData);
0141             blocks[nBlocks - 1]->addStar(deepstardata);
0142         }
0143 
0144         /*
0145           if( faintMag > -5.0 && fabs(faintMag - blocks[nBlocks - 1]->getFaintMag()) > 0.2 ) {
0146           qDebug() << Q_FUNC_INFO << "Encountered a jump from mag" << faintMag << "to mag"
0147           << blocks[nBlocks - 1]->getFaintMag() << "in trixel" << trixel;
0148           }
0149         */
0150         faintMag = blocks[nBlocks - 1]->getFaintMag();
0151         nStars++;
0152     }
0153 
0154     return ((maglim < faintMag) ? true : false);
0155 }
0156 
0157 void StarBlockList::setStaticBlock(std::shared_ptr<StarBlock> &block)
0158 {
0159     if (!block)
0160         return;
0161     if (nBlocks == 0)
0162     {
0163         blocks.append(block);
0164     }
0165     else
0166         blocks[0] = block;
0167 
0168     blocks[0]->parent = this;
0169     faintMag          = blocks[0]->faintMag;
0170     nBlocks           = 1;
0171     staticStars       = true;
0172 }