File indexing completed on 2024-07-14 14:25:18

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2006-2007 Torsten Rahn <tackat@kde.org>
0004 // SPDX-FileCopyrightText: 2007 Inge Wallin <ingwa@kde.org>
0005 //
0006 
0007 
0008 #include <cmath>
0009 
0010 #include <QCoreApplication>
0011 #include <QDebug>
0012 #include <QFile>
0013 #include <QVector>
0014 #include <QStringList>
0015 #include <QDataStream>
0016 #include "Quaternion.h"
0017 
0018 using namespace Marble;
0019 
0020 int getHeader( int count, int size )
0021 {
0022     int header = 0;
0023     if ( size > 14 ) {
0024         if ( count % 9 == 0 ) 
0025             header = 5;
0026         else if ( count % 5 == 0 )
0027             header = 3;
0028         else if ( count % 2 == 0 )
0029             header = 2;
0030         else
0031             header = 1;
0032     }
0033     else if ( size > 6 ) {
0034         if ( count % 2 == 0 )
0035             header = 3;
0036         else
0037             header = 1;
0038     }
0039     else {
0040         header = 2;
0041     }
0042     if ( count == size - 1 )
0043         header = 5;
0044 
0045     return header;
0046 }
0047 
0048 double deg2rad = M_PI / 180.0;
0049 
0050 int main(int argc, char *argv[])
0051 {
0052 
0053     QString  sourcefilename;
0054     QString  targetfilename;
0055 
0056     QCoreApplication  app( argc, argv );
0057 
0058 
0059     if ( argc != 4 || strcmp( argv[ 1 ], "-o" ) != 0 )
0060     {
0061         qDebug(" dateline -o targetfile sourcefile");
0062         return 0;
0063     }
0064 
0065     targetfilename = QString(argv[2]);
0066     sourcefilename = QString(argv[3]);
0067 
0068     QVector<float> idlPosList;
0069 
0070     QFile  sourcefile( sourcefilename );
0071     sourcefile.open( QIODevice::ReadOnly );
0072 
0073     qDebug() << "Source: " << sourcefilename;
0074     qDebug() << "Target: " << targetfilename;
0075 
0076     // Read the data serialized from the file.
0077     QTextStream  sourcestream( &sourcefile );
0078     sourcestream.setCodec("UTF-8");
0079 
0080     QString  rawline;
0081     QString  lonstring;
0082     QString  latstring;
0083 
0084     QStringList  splitline;
0085 
0086     int line = 0;
0087 
0088     while ( !sourcestream.atEnd() )
0089     {
0090         rawline = sourcestream.readLine();
0091 
0092         if (!rawline.contains(QLatin1Char(' '))) {
0093             qDebug() << "Line " << line << " does not contain a space separator.";
0094             continue;
0095         } 
0096         splitline = rawline.split(QLatin1Char(' '));
0097 
0098         lonstring = splitline[0];
0099         latstring = splitline[1];
0100 
0101 //        qDebug() << "Point read at: " << lonstring << ", " << latstring;
0102 
0103         float lon = lonstring.toFloat();
0104         float lat = latstring.toFloat();
0105 
0106         idlPosList.append(lon);
0107         idlPosList.append(lat);
0108         ++line;
0109     }
0110 
0111     QFile    targetfile( targetfilename );
0112 
0113     // Read the data serialized from the file.
0114     targetfile.open( QIODevice::WriteOnly );
0115     QDataStream stream( &targetfile );
0116     stream.setByteOrder( QDataStream::LittleEndian );
0117 
0118     int count = 0;
0119     bool firstheader = true;
0120 
0121     QVector<float>::iterator i = idlPosList.begin();
0122 
0123     float lastlon = 99999.0f;
0124     float lastlat = 99999.0f;
0125 
0126     const float step = 50.0f;
0127 
0128     while ( i != idlPosList.end() ) {
0129         float lonf = *i++;
0130         float latf = *i++;
0131  //       qDebug() << "Writing point" << lonf << ", " << latf;  
0132 
0133         float  header;
0134         float  lat;
0135         float  lon; 
0136 
0137         header = 5;
0138         if ( firstheader ) {
0139              header      = 19000;
0140              firstheader = false;
0141         }
0142 
0143         lon = ( lonf * 60.0f );
0144         lat = ( latf * 60.0f );
0145 
0146         if ( lastlon != 99999.0f || lastlat != 99999.0f )
0147         {
0148             Quaternion lastPos = Quaternion::fromSpherical((lastlon/60.0) * deg2rad , (lastlat/60.0) * -deg2rad  );
0149             Quaternion currentPos = Quaternion::fromSpherical((lon/60.0) * deg2rad , (lat/60.0) * -deg2rad  );
0150 //      qDebug() << "lastPos: " << lastPos << "currentPos: " << currentPos;
0151 
0152             float distance =   sqrt( ( lon - lastlon ) * ( lon - lastlon ) 
0153                              + ( lat - lastlat ) * ( lat - lastlat ) );
0154             int numsteps = (int)( distance / step );
0155 
0156             for ( int i = 1; i < numsteps; ++i )
0157             {
0158                 const Quaternion itPos = Quaternion::slerp( lastPos, currentPos, (double)(i)/(double)(numsteps) );
0159 //      qDebug() << "itPos: " << itPos; 
0160                 double alpha = 0;
0161                 double beta = 0;
0162                 itPos.getSpherical( alpha, beta);
0163 //      qDebug() << "alpha: " << alpha << " beta: " << beta;
0164                 float ipLon = alpha * 180.0 / M_PI * 60;
0165                 float ipLat = - beta * 180.0 / M_PI * 60;
0166 
0167                 short ipHeader = getHeader( i, numsteps );
0168 
0169                 qDebug() << "numsteps: " << numsteps << "ipLon:" << (short)ipLon << "ipLat:" << (short)ipLat << "ipHeader:" << (short)ipHeader << "node#:" << count;
0170         stream << (short)(ipHeader) << (short)(ipLat) << (short)(ipLon); 
0171 
0172         count ++;
0173             } 
0174 
0175 
0176         }
0177 
0178           qDebug() << "lng:" << (short)(lon) << "lat:" << (short)(lat) << "header:"
0179                      << (short)(header) << "node#:" << count;
0180 
0181         stream << (short)(header) << (short)(lat) << (short)(lon); 
0182 
0183         lastlon = lon;
0184         lastlat = lat;
0185         count++;
0186     }
0187     targetfile.close();
0188 
0189     qDebug("Finished!");
0190 
0191     app.exit();
0192 }