mainwindow.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QFileDialog>
  4. #include <QMessageBox>
  5. #include <iostream>
  6. #include "google/protobuf/io/zero_copy_stream_impl.h"
  7. #include "google/protobuf/text_format.h"
  8. MainWindow::MainWindow(QWidget *parent) :
  9. QMainWindow(parent),
  10. ui(new Ui::MainWindow)
  11. {
  12. ui->setupUi(this);
  13. mpTimer = new QTimer();
  14. connect(mpTimer,SIGNAL(timeout()),this,SLOT(onTimer()));
  15. mpTimer->setInterval(10);
  16. connect(this,SIGNAL(CurState(int)),this,SLOT(onCurState(int)));
  17. ui->progressBar->setRange(0,100);
  18. ui->progressBar->setValue(0);
  19. }
  20. MainWindow::~MainWindow()
  21. {
  22. delete ui;
  23. }
  24. void MainWindow::on_pushButton_decode_clicked()
  25. {
  26. QString strdir = QFileDialog::getExistingDirectory(this, tr("Set Save Directory"), ".", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  27. if(strdir.isEmpty())
  28. {
  29. return;
  30. }
  31. std::cout<<" directory: "<<strdir.toStdString()<<std::endl;
  32. QString strivd = QFileDialog::getOpenFileName(this,tr("Open file"),"",tr("Record File(*.ivd)"));
  33. if(strivd.isEmpty())return;
  34. mpthread = new std::thread(&MainWindow::threadconvert,this, strivd,strdir);
  35. mbRunning = true;
  36. ui->pushButton_decode->setEnabled(false);
  37. mpTimer->start();
  38. }
  39. void MainWindow::onCurState(int nstate)
  40. {
  41. if(nstate == -1)
  42. {
  43. QMessageBox::warning(this,tr("Warning"),tr("Open File Fail."),QMessageBox::YesAll);
  44. }
  45. if(nstate == 0)
  46. {
  47. ui->progressBar->setValue(100);
  48. }
  49. ui->pushButton_decode->setEnabled(true);
  50. mpTimer->stop();
  51. }
  52. void MainWindow::onTimer()
  53. {
  54. qint64 nSize = mnFileSize;
  55. if(nSize<=0)return;
  56. int nPos = mnPos*100/mnFileSize;
  57. ui->progressBar->setValue(nPos);
  58. }
  59. void MainWindow::threadconvert(QString strfilepath,QString strdir)
  60. {
  61. mFile.setFileName(strfilepath);
  62. if(mFile.open(QFile::ReadOnly))
  63. {
  64. mbOpen = true;
  65. mnFileSize = mFile.size();
  66. mnPos = 0;
  67. }
  68. else
  69. {
  70. mnFileSize = 0;
  71. mbReplay = false;
  72. mnPos = 0;
  73. mbOpen = false;
  74. }
  75. if(mbOpen == false)
  76. {
  77. emit onCurState(-1);
  78. return;
  79. }
  80. bool bx = true;
  81. while(bx == true)
  82. {
  83. int nReadSize = 0;
  84. int nDataSize;
  85. char * strData;
  86. char * strName;
  87. bx = ReadARecord(nReadSize,&strName,&nDataSize,&strData);
  88. mnPos = mnPos + nReadSize;
  89. if(bx == true)
  90. {
  91. std::cout<<" read a record."<<std::endl;
  92. std::cout<<" name : "<<strName<<std::endl;
  93. if(strncmp(strName,"lidar_pc",256)==0)
  94. {
  95. QString strpath = strdir + "/" + mdtcurpos.toString("yyyy-MM-dd-hh-mm-ss-zzz") + ".pcd";
  96. savepointcloud(strData,nDataSize,strpath);
  97. }
  98. if(strncmp(strName,"hcp2_gpsimu",256)==0)
  99. {
  100. QString strpath = strdir + "/" + mdtcurpos.toString("yyyy-MM-dd-hh-mm-ss-zzz") + ".txt";
  101. savegps(strData,nDataSize,strpath);
  102. }
  103. delete strData;
  104. delete strName;
  105. }
  106. }
  107. std::cout<<"complete."<<std::endl;
  108. emit onCurState(0);
  109. }
  110. inline QDateTime MainWindow::GetDateTimeFromRH(iv::RecordHead rh)
  111. {
  112. QDateTime dt;
  113. QDate datex;
  114. QTime timex;
  115. datex.setDate(rh.mYear,rh.mMon,rh.mDay);
  116. timex.setHMS(rh.mHour,rh.mMin,rh.mSec,rh.mMSec);
  117. dt.setDate(datex);
  118. dt.setTime(timex);
  119. return dt;
  120. }
  121. inline bool MainWindow::ReadARecord(int & nRecSize,char ** pstrName, int * pnDataSize, char ** pstrData)
  122. {
  123. char strmark[10];
  124. int nTotalSize,nHeadSize,nNameSize,nDataSize;
  125. int nRead = mFile.read(strmark,1);
  126. if(nRead == 0)return false;
  127. nRead = mFile.read((char *)&nTotalSize,sizeof(int));
  128. if(nRead < (int)sizeof(int))return false;
  129. nRead = mFile.read((char *)&nHeadSize,sizeof(int));
  130. if(nRead < (int)sizeof(int))return false;
  131. nRead = mFile.read((char *)&nNameSize,sizeof(int));
  132. if(nRead< (int)sizeof(int))return false;
  133. nRead = mFile.read((char *)&nDataSize,sizeof(int));
  134. if(nRead< (int)sizeof(int))return false;
  135. if(nTotalSize !=(nHeadSize + nNameSize + nDataSize + 4*(int)sizeof(int) ))
  136. {
  137. return false;
  138. }
  139. iv::RecordHead rh;
  140. char * strName = new char[1000];
  141. char * strData = new char[nDataSize];
  142. nRead = mFile.read((char *)&rh,sizeof(iv::RecordHead));
  143. if(nRead < (int)sizeof(iv::RecordHead))
  144. {
  145. delete strData;
  146. delete strName;
  147. return false;
  148. }
  149. mdtcurpos = GetDateTimeFromRH(rh);
  150. nRead = mFile.read(strName,nNameSize);
  151. if(nRead < nNameSize)
  152. {
  153. delete strData;
  154. delete strName;
  155. return false;
  156. }
  157. strName[nNameSize] = 0;
  158. // qDebug(strName);
  159. // qDebug("file pos is %d ms is %d",mFile.pos(),rh.mMSec);
  160. nRead = mFile.read(strData,nDataSize);
  161. if(nRead < nDataSize)
  162. {
  163. delete strData;
  164. delete strName;
  165. return false;
  166. }
  167. //Share Data
  168. *pnDataSize = nDataSize;
  169. *pstrName = strName;
  170. *pstrData = strData;
  171. // delete strData;
  172. nRecSize = nTotalSize + 1;
  173. return true;
  174. }
  175. void MainWindow::savepointcloud(const char *strdata, const unsigned int nSize, QString strfilepath)
  176. {
  177. if(nSize <=16)return;
  178. unsigned int * pHeadSize = (unsigned int *)strdata;
  179. if(*pHeadSize > nSize)
  180. {
  181. std::cout<<"ListenPointCloud data is small headsize ="<<*pHeadSize<<" data size is"<<nSize<<std::endl;
  182. }
  183. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  184. new pcl::PointCloud<pcl::PointXYZI>());
  185. int nNameSize;
  186. nNameSize = *pHeadSize - 4-4-8;
  187. char * strName = new char[nNameSize+1];strName[nNameSize] = 0;
  188. memcpy(strName,(char *)((char *)strdata +4),nNameSize);
  189. point_cloud->header.frame_id = strName;
  190. memcpy(&point_cloud->header.seq,(char *)strdata+4+nNameSize,4);
  191. memcpy(&point_cloud->header.stamp,(char *)strdata+4+nNameSize+4,8);
  192. int nPCount = (nSize - *pHeadSize)/sizeof(pcl::PointXYZI);
  193. int i;
  194. pcl::PointXYZI * p;
  195. p = (pcl::PointXYZI *)((char *)strdata + *pHeadSize);
  196. for(i=0;i<nPCount;i++)
  197. {
  198. pcl::PointXYZI xp;
  199. xp.x = p->x;
  200. xp.y = p->y;
  201. xp.z = p->z;
  202. xp.intensity = p->intensity;
  203. point_cloud->push_back(xp);
  204. p++;
  205. // std::cout<<" x is "<<xp.x<<" y is "<<xp.y<<std::endl;
  206. }
  207. if(0 == pcl::io::savePCDFile(strfilepath.toLatin1().data(),*point_cloud))
  208. {
  209. }
  210. else
  211. {
  212. std::cout<<" save pcd file fail. "<<std::endl;
  213. }
  214. }
  215. void MainWindow::savegps(const char *strdata, const unsigned int nSize, QString strfilepath)
  216. {
  217. iv::gps::gpsimu xgpsimu;
  218. if(xgpsimu.ParseFromArray(strdata,nSize))
  219. {
  220. using google::protobuf::TextFormat;
  221. using google::protobuf::io::FileOutputStream;
  222. using google::protobuf::io::ZeroCopyOutputStream;
  223. std::string strout;
  224. ZeroCopyOutputStream *output = new google::protobuf::io::StringOutputStream(&strout);//new FileOutputStream(file_descriptor);
  225. bool success = TextFormat::Print(xgpsimu, output);
  226. if(success)
  227. {
  228. QFile xFile;
  229. xFile.setFileName(strfilepath);
  230. if(xFile.open(QIODevice::ReadWrite))
  231. {
  232. xFile.write(strout.data(),strout.size());
  233. xFile.close();
  234. }
  235. // std::cout<<strout<<std::endl;
  236. // qDebug(strout.data());
  237. }
  238. }
  239. }