123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QFileDialog>
- #include <QMessageBox>
- #include <iostream>
- #include "google/protobuf/io/zero_copy_stream_impl.h"
- #include "google/protobuf/text_format.h"
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- mpTimer = new QTimer();
- connect(mpTimer,SIGNAL(timeout()),this,SLOT(onTimer()));
- mpTimer->setInterval(10);
- connect(this,SIGNAL(CurState(int)),this,SLOT(onCurState(int)));
- ui->progressBar->setRange(0,100);
- ui->progressBar->setValue(0);
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- void MainWindow::on_pushButton_decode_clicked()
- {
- QString strdir = QFileDialog::getExistingDirectory(this, tr("Set Save Directory"), ".", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
- if(strdir.isEmpty())
- {
- return;
- }
- std::cout<<" directory: "<<strdir.toStdString()<<std::endl;
- QString strivd = QFileDialog::getOpenFileName(this,tr("Open file"),"",tr("Record File(*.ivd)"));
- if(strivd.isEmpty())return;
- mpthread = new std::thread(&MainWindow::threadconvert,this, strivd,strdir);
- mbRunning = true;
- ui->pushButton_decode->setEnabled(false);
- mpTimer->start();
- }
- void MainWindow::onCurState(int nstate)
- {
- if(nstate == -1)
- {
- QMessageBox::warning(this,tr("Warning"),tr("Open File Fail."),QMessageBox::YesAll);
- }
- if(nstate == 0)
- {
- ui->progressBar->setValue(100);
- }
- ui->pushButton_decode->setEnabled(true);
- mpTimer->stop();
- }
- void MainWindow::onTimer()
- {
- qint64 nSize = mnFileSize;
- if(nSize<=0)return;
- int nPos = mnPos*100/mnFileSize;
- ui->progressBar->setValue(nPos);
- }
- void MainWindow::threadconvert(QString strfilepath,QString strdir)
- {
- mFile.setFileName(strfilepath);
- if(mFile.open(QFile::ReadOnly))
- {
- mbOpen = true;
- mnFileSize = mFile.size();
- mnPos = 0;
- }
- else
- {
- mnFileSize = 0;
- mbReplay = false;
- mnPos = 0;
- mbOpen = false;
- }
- if(mbOpen == false)
- {
- emit onCurState(-1);
- return;
- }
- bool bx = true;
- while(bx == true)
- {
- int nReadSize = 0;
- int nDataSize;
- char * strData;
- char * strName;
- bx = ReadARecord(nReadSize,&strName,&nDataSize,&strData);
- mnPos = mnPos + nReadSize;
- if(bx == true)
- {
- std::cout<<" read a record."<<std::endl;
- std::cout<<" name : "<<strName<<std::endl;
- if(strncmp(strName,"lidar_pc",256)==0)
- {
- QString strpath = strdir + "/" + mdtcurpos.toString("yyyy-MM-dd-hh-mm-ss-zzz") + ".pcd";
- savepointcloud(strData,nDataSize,strpath);
- }
- if(strncmp(strName,"hcp2_gpsimu",256)==0)
- {
- QString strpath = strdir + "/" + mdtcurpos.toString("yyyy-MM-dd-hh-mm-ss-zzz") + ".txt";
- savegps(strData,nDataSize,strpath);
- }
- delete strData;
- delete strName;
- }
- }
- std::cout<<"complete."<<std::endl;
- emit onCurState(0);
- }
- inline QDateTime MainWindow::GetDateTimeFromRH(iv::RecordHead rh)
- {
- QDateTime dt;
- QDate datex;
- QTime timex;
- datex.setDate(rh.mYear,rh.mMon,rh.mDay);
- timex.setHMS(rh.mHour,rh.mMin,rh.mSec,rh.mMSec);
- dt.setDate(datex);
- dt.setTime(timex);
- return dt;
- }
- inline bool MainWindow::ReadARecord(int & nRecSize,char ** pstrName, int * pnDataSize, char ** pstrData)
- {
- char strmark[10];
- int nTotalSize,nHeadSize,nNameSize,nDataSize;
- int nRead = mFile.read(strmark,1);
- if(nRead == 0)return false;
- nRead = mFile.read((char *)&nTotalSize,sizeof(int));
- if(nRead < (int)sizeof(int))return false;
- nRead = mFile.read((char *)&nHeadSize,sizeof(int));
- if(nRead < (int)sizeof(int))return false;
- nRead = mFile.read((char *)&nNameSize,sizeof(int));
- if(nRead< (int)sizeof(int))return false;
- nRead = mFile.read((char *)&nDataSize,sizeof(int));
- if(nRead< (int)sizeof(int))return false;
- if(nTotalSize !=(nHeadSize + nNameSize + nDataSize + 4*(int)sizeof(int) ))
- {
- return false;
- }
- iv::RecordHead rh;
- char * strName = new char[1000];
- char * strData = new char[nDataSize];
- nRead = mFile.read((char *)&rh,sizeof(iv::RecordHead));
- if(nRead < (int)sizeof(iv::RecordHead))
- {
- delete strData;
- delete strName;
- return false;
- }
- mdtcurpos = GetDateTimeFromRH(rh);
- nRead = mFile.read(strName,nNameSize);
- if(nRead < nNameSize)
- {
- delete strData;
- delete strName;
- return false;
- }
- strName[nNameSize] = 0;
- // qDebug(strName);
- // qDebug("file pos is %d ms is %d",mFile.pos(),rh.mMSec);
- nRead = mFile.read(strData,nDataSize);
- if(nRead < nDataSize)
- {
- delete strData;
- delete strName;
- return false;
- }
- //Share Data
- *pnDataSize = nDataSize;
- *pstrName = strName;
- *pstrData = strData;
- // delete strData;
- nRecSize = nTotalSize + 1;
- return true;
- }
- void MainWindow::savepointcloud(const char *strdata, const unsigned int nSize, QString strfilepath)
- {
- if(nSize <=16)return;
- unsigned int * pHeadSize = (unsigned int *)strdata;
- if(*pHeadSize > nSize)
- {
- std::cout<<"ListenPointCloud data is small headsize ="<<*pHeadSize<<" data size is"<<nSize<<std::endl;
- }
- pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
- new pcl::PointCloud<pcl::PointXYZI>());
- int nNameSize;
- nNameSize = *pHeadSize - 4-4-8;
- char * strName = new char[nNameSize+1];strName[nNameSize] = 0;
- memcpy(strName,(char *)((char *)strdata +4),nNameSize);
- point_cloud->header.frame_id = strName;
- memcpy(&point_cloud->header.seq,(char *)strdata+4+nNameSize,4);
- memcpy(&point_cloud->header.stamp,(char *)strdata+4+nNameSize+4,8);
- int nPCount = (nSize - *pHeadSize)/sizeof(pcl::PointXYZI);
- int i;
- pcl::PointXYZI * p;
- p = (pcl::PointXYZI *)((char *)strdata + *pHeadSize);
- for(i=0;i<nPCount;i++)
- {
- pcl::PointXYZI xp;
- xp.x = p->x;
- xp.y = p->y;
- xp.z = p->z;
- xp.intensity = p->intensity;
- point_cloud->push_back(xp);
- p++;
- // std::cout<<" x is "<<xp.x<<" y is "<<xp.y<<std::endl;
- }
- if(0 == pcl::io::savePCDFile(strfilepath.toLatin1().data(),*point_cloud))
- {
- }
- else
- {
- std::cout<<" save pcd file fail. "<<std::endl;
- }
- }
- void MainWindow::savegps(const char *strdata, const unsigned int nSize, QString strfilepath)
- {
- iv::gps::gpsimu xgpsimu;
- if(xgpsimu.ParseFromArray(strdata,nSize))
- {
- using google::protobuf::TextFormat;
- using google::protobuf::io::FileOutputStream;
- using google::protobuf::io::ZeroCopyOutputStream;
- std::string strout;
- ZeroCopyOutputStream *output = new google::protobuf::io::StringOutputStream(&strout);//new FileOutputStream(file_descriptor);
- bool success = TextFormat::Print(xgpsimu, output);
- if(success)
- {
- QFile xFile;
- xFile.setFileName(strfilepath);
- if(xFile.open(QIODevice::ReadWrite))
- {
- xFile.write(strout.data(),strout.size());
- xFile.close();
- }
- // std::cout<<strout<<std::endl;
- // qDebug(strout.data());
- }
- }
- }
|