浏览代码

change xodr2rd5.

yuchuli 2 年之前
父节点
当前提交
c747b7b51d

+ 23 - 3
src/test/xodr2rd5_grpc/main.cpp

@@ -115,11 +115,30 @@ std::string GetFileName(char * strpath)
     return strrtn;
 }
 
+std::string GetServerstr()
+{
+    std::string strserverstr = "111.33.136.149:50091";
+    QFile xFile;
+    xFile.setFileName("carmakerservice.txt");
+    if(xFile.open(QIODevice::ReadOnly))
+    {
+        char str[256];
+        xFile.readLine(str,256);
+        std::cout<<" open file. server : "<<str<<std::endl;
+        strserverstr = str;
+        xFile.close();
+    }
+    return  strserverstr;
+}
+
 
 int main(int argc, char *argv[])
 {
     QCoreApplication a(argc, argv);
 
+    snprintf(gstr_inputpath,256," ");
+    snprintf(gstr_outputpath,256," ");
+
     int nRtn = GetOptLong(argc,argv);
     if(nRtn == 1)  //show help,so exit.
     {
@@ -132,7 +151,7 @@ int main(int argc, char *argv[])
     snprintf(gstr_outputpath,255,"/home/yuchuli/demodata/lk1.rd5");
 #endif
 
-    if(strncmp(gstr_inputpath , "",255) == 0)
+    if(strncmp(gstr_inputpath , " ",255) == 0)
     {
         std::cout<<"Please use -i set input file path."<<std::endl;
         print_useage();
@@ -143,7 +162,7 @@ int main(int argc, char *argv[])
     snprintf(strout,1000,"Input File Path: %s",gstr_inputpath);
     std::cout<<strout<<std::endl;
 
-    if(strncmp(gstr_outputpath , "",255) == 0)
+    if(strncmp(gstr_outputpath , " ",255) == 0)
     {
         std::cout<<"Please use -o set output file path."<<std::endl;
         print_useage();
@@ -193,7 +212,8 @@ int main(int argc, char *argv[])
     int noutputsize = 0;
     int nres;
 
-    carmakercvt * pcvt = new carmakercvt();
+    std::string strserverstr = GetServerstr();
+    carmakercvt * pcvt = new carmakercvt(strserverstr);
     nres = pcvt->GetRes(strinputname,stroutputname,strcmd,pstr_ptr,ninputsize,pout_ptr,noutputsize);
 
     if(nres != 1)

+ 297 - 0
src/test/xodr2rd5_grpc/rd5route.cpp

@@ -0,0 +1,297 @@
+#include <QCoreApplication>
+
+#include <QFile>
+
+#include <iostream>
+#include <memory>
+
+#include <getopt.h>
+
+#include "carmakercvt.h"
+
+static char gstr_inputpath[256];
+static char gstr_outputpath[256];
+
+static char gstr_fromroad[256];
+static char gstr_fromlane[256];
+
+static char gstr_toroad[256];
+static char gstr_tolane[256];
+
+void print_useage()
+{
+    std::cout<<" -i --input $xodrfile : set  input file path. eq.  -i d:/lk.rd5"<<std::endl;
+    std::cout<<" -o --output $outputfile : set output file. eq.  -o d:/lk2.rd5"<<std::endl;
+    std::cout<<" -a --from road.  eq. -a 1"<<std::endl;
+    std::cout<<" -b --from lane . eq. -b -1"<<std::endl;
+    std::cout<<" -c --to road.  eq. -c 3"<<std::endl;
+    std::cout<<" -d --to lane . eq. -d -1"<<std::endl;
+    std::cout<<" -h --help print help"<<std::endl;
+}
+
+int  GetOptLong(int argc, char *argv[]) {
+    int nRtn = 0;
+    int opt; // getopt_long() 的返回值
+    int digit_optind = 0; // 设置短参数类型及是否需要参数
+    (void)digit_optind;
+
+    // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
+    // 第几个元素的描述,即是long_opts的下标值
+    int option_index = 0;
+    // 设置短参数类型及是否需要参数
+    const char *optstring = "i:o:a:b:c:d:h";
+
+    // 设置长参数类型及其简写,比如 --reqarg <==>-r
+    /*
+    struct option {
+             const char * name;  // 参数的名称
+             int has_arg; // 是否带参数值,有三种:no_argument, required_argument,optional_argument
+             int * flag; // 为空时,函数直接将 val 的数值从getopt_long的返回值返回出去,
+                     // 当非空时,val的值会被赋到 flag 指向的整型数中,而函数返回值为0
+             int val; // 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值
+        };
+    其中:
+        no_argument(即0),表明这个长参数不带参数(即不带数值,如:--name)
+            required_argument(即1),表明这个长参数必须带参数(即必须带数值,如:--name Bob)
+            optional_argument(即2),表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
+     */
+    static struct option long_options[] = {
+        {"input", required_argument, NULL, 'i'},
+        {"output", required_argument, NULL, 'o'},
+        {"fromroad", required_argument, NULL, 'a'},
+        {"fromlane", required_argument, NULL, 'b'},
+        {"toroad", required_argument, NULL, 'c'},
+        {"tolane", required_argument, NULL, 'd'},
+        {"help",  no_argument,       NULL, 'h'},
+ //       {"optarg", optional_argument, NULL, 'o'},
+        {0, 0, 0, 0}  // 添加 {0, 0, 0, 0} 是为了防止输入空值
+    };
+
+    while ( (opt = getopt_long(argc,
+                               argv,
+                               optstring,
+                               long_options,
+                               &option_index)) != -1) {
+//        printf("opt = %c\n", opt); // 命令参数,亦即 -a -b -n -r
+//        printf("optarg = %s\n", optarg); // 参数内容
+//        printf("optind = %d\n", optind); // 下一个被处理的下标值
+//        printf("argv[optind - 1] = %s\n",  argv[optind - 1]); // 参数内容
+//        printf("option_index = %d\n", option_index);  // 当前打印参数的下标值
+//        printf("\n");
+        switch(opt)
+        {
+        case 'i':
+            strncpy(gstr_inputpath,optarg,255);
+            break;
+        case 'o':
+            strncpy(gstr_outputpath,optarg,255);
+            break;
+        case 'a':
+            strncpy(gstr_fromroad,optarg,255);
+            break;
+        case 'b':
+            strncpy(gstr_fromlane,optarg,255);
+            break;
+        case 'c':
+            strncpy(gstr_toroad,optarg,255);
+            break;
+        case 'd':
+            strncpy(gstr_tolane,optarg,255);
+            break;
+        case 'h':
+            print_useage();
+            nRtn = 1; //because use -h
+            break;
+        default:
+            break;
+        }
+
+    }
+
+    return nRtn;
+}
+
+
+
+std::string GetFileName(char * strpath)
+{
+    int npos = -1;
+    int nsize = static_cast<int>(strnlen(strpath,256));
+    int i;
+    for(i=(nsize-2);i>=0;i--)
+    {
+        if((strpath[i] == '/')||(strpath[i] == '\\'))
+        {
+            npos = i+1;
+            break;
+        }
+    }
+
+    std::string strrtn;
+    if(npos  == -1)
+    {
+        strrtn = strpath;
+    }
+    else
+    {
+        char strname[256];
+        strncpy(strname,strpath+npos,256);
+        strrtn = strname;
+    }
+    return strrtn;
+}
+
+std::string GetServerstr()
+{
+    std::string strserverstr = "111.33.136.149:50091";
+    QFile xFile;
+    xFile.setFileName("carmakerservice.txt");
+    if(xFile.open(QIODevice::ReadOnly))
+    {
+        char str[256];
+        xFile.readLine(str,256);
+        std::cout<<" open file. server : "<<str<<std::endl;
+        strserverstr = str;
+        xFile.close();
+    }
+    return  strserverstr;
+}
+
+int main(int argc, char *argv[])
+{
+    QCoreApplication a(argc, argv);
+
+    snprintf(gstr_inputpath,256," ");
+    snprintf(gstr_outputpath,256," ");
+
+    snprintf(gstr_fromroad,256,"1");
+    snprintf(gstr_toroad,256,"3");
+    snprintf(gstr_fromlane,256,"-1");
+    snprintf(gstr_tolane,256,"-1");
+
+    int nRtn = GetOptLong(argc,argv);
+    if(nRtn == 1)  //show help,so exit.
+    {
+        return 0;
+    }
+
+
+#ifdef TESTC
+    snprintf(gstr_inputpath,255,"/home/yuchuli/demodata/lk.xodr");
+    snprintf(gstr_outputpath,255,"/home/yuchuli/demodata/lk1.rd5");
+#endif
+
+    if(strncmp(gstr_inputpath , " ",255) == 0)
+    {
+        std::cout<<"Please use -i set input file path."<<std::endl;
+        print_useage();
+        return 0;
+    }
+
+    char strout[1000];
+    snprintf(strout,1000,"Input File Path: %s",gstr_inputpath);
+    std::cout<<strout<<std::endl;
+
+    if(strncmp(gstr_outputpath , " ",255) == 0)
+    {
+        std::cout<<"Please use -o set output file path."<<std::endl;
+        print_useage();
+        return 0;
+    }
+
+    snprintf(strout,1000,"Output File Path: %s",gstr_outputpath);
+    std::cout<<strout<<std::endl;
+
+    std::string strinputname = GetFileName(gstr_inputpath);
+    std::string stroutputname = GetFileName(gstr_outputpath);
+
+//    int nfromroad = atoi(gstr_fromroad);
+//    int ntoroad = atoi(gstr_toroad);
+//    int nfromlane = atoi(gstr_fromlane);
+//    int ntolane = atoi(gstr_tolane);
+
+    std::cout<<"input name: "<<strinputname<<std::endl;
+
+    std::string strcmd = "rd5route.exe ";
+    strcmd = strcmd + "-i "+strinputname + " -o "+stroutputname + " -a "+gstr_fromroad+" -b "+gstr_fromlane
+            +" -c "+gstr_toroad+" -d "+gstr_tolane;
+    std::cout<<"cmd: "<<strcmd<<std::endl;
+
+    std::shared_ptr<char> pstr_ptr = nullptr;
+    int ninputsize = 0;
+    QFile xFile;
+    xFile.setFileName(gstr_inputpath);
+    if(xFile.open(QIODevice::ReadOnly))
+    {
+        QByteArray ba= xFile.readAll();
+        ninputsize = ba.size();
+        if(ninputsize > 0)
+        {
+            pstr_ptr = std::shared_ptr<char>(new char[ninputsize]);
+            memcpy(pstr_ptr.get(),ba.data(),static_cast<size_t>(ninputsize));
+        }
+        xFile.close();
+    }
+    else
+    {
+        std::cout<<"FATAL Errror. Can't Open input File. Please Check Input File."<<std::endl;
+        return -1;
+    }
+
+    if(ninputsize == 0)
+    {
+        std::cout<<"FATAL Error. Input File is empty."<<std::endl;
+        return  -2;
+    }
+
+    std::shared_ptr<char> pout_ptr;
+    int noutputsize = 0;
+    int nres;
+
+    std::string strserverstr = GetServerstr();
+    carmakercvt * pcvt = new carmakercvt(strserverstr);
+    nres = pcvt->GetRes(strinputname,stroutputname,strcmd,pstr_ptr,ninputsize,pout_ptr,noutputsize);
+
+    if(nres != 1)
+    {
+        std::cout<<" GetRes Fail.Fail Code is: "<<nres<<std::endl;
+        if(nres == -1)
+        {
+            std::cout<<" Convert Fail. Please Check Input File"<<std::endl;
+        }
+        if(nres == -2)
+        {
+            std::cout<<" NO Carmaker Service."<<std::endl;
+        }
+        if(nres == -3)
+        {
+            std::cout<<" Server Can't Connect."<<std::endl;
+        }
+        return -3;
+    }
+
+    if(noutputsize == 0)
+    {
+        std::cout<<" Get File size is 0,please check"<<std::endl;
+        return -4;
+    }
+
+    QFile xFileOut;
+    xFileOut.setFileName(gstr_outputpath);
+    if(xFileOut.open(QIODevice::ReadWrite))
+    {
+        xFileOut.write(pout_ptr.get(),noutputsize);
+        xFileOut.close();
+    }
+    else
+    {
+        std::cout<<" Can't Output data to File. "<<" Output Path: "<<gstr_outputpath<<std::endl;
+        return -5;
+    }
+
+    return 0;
+
+
+
+    return a.exec();
+}

+ 38 - 0
src/test/xodr2rd5_grpc/rd5route_grpc.pro

@@ -0,0 +1,38 @@
+QT -= gui
+
+CONFIG += c++11 console
+CONFIG -= app_bundle
+
+# The following define makes your compiler emit warnings if you use
+# any feature of Qt which as been marked deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if you use deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+SOURCES += rd5route.cpp \
+    ../carmakerservice/carmaker.grpc.pb.cc \
+    ../../include/msgtype/carmaker.pb.cc \
+    carmakercvt.cpp
+
+INCLUDEPATH += $$PWD/../../../src/include/msgtype
+INCLUDEPATH += $$PWD/../carmakerservice
+
+#DEFINES += TESTC
+
+!include(../../../include/ivprotobuf.pri ) {
+    error( "Couldn't find the ivprotobuf.pri file!" )
+}
+
+!include(../../../include/ivgrpc.pri ) {
+    error( "Couldn't find the ivgrpc.pri file!" )
+}
+
+HEADERS += \
+    ../carmakerservice/carmaker.grpc.pb.h \
+    ../../include/msgtype/carmaker.pb.h \
+    carmakercvt.h

+ 1 - 1
src/test/xodr2rd5_grpc/xodr2rd5_grpc.pro

@@ -22,7 +22,7 @@ SOURCES += main.cpp \
 INCLUDEPATH += $$PWD/../../../src/include/msgtype
 INCLUDEPATH += $$PWD/../carmakerservice
 
-DEFINES += TESTC
+#DEFINES += TESTC
 
 !include(../../../include/ivprotobuf.pri ) {
     error( "Couldn't find the ivprotobuf.pri file!" )