123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- package com.fdkankan.modeldemo.utils;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.fdkankan.modeldemo.proto.BigSceneProto;
- import com.fdkankan.modeldemo.proto.Visionmodeldata;
- import com.fdkankan.modeldemo.proto.format.JsonFormat;
- import com.google.protobuf.TextFormat;
- import lombok.extern.slf4j.Slf4j;
- import java.io.*;
- import java.util.*;
- @Slf4j
- public class CreateObjUtil {
- public static void main(String[] args) throws Exception {
- // Scanner scanner = new Scanner(System.in);
- // System.out.println("输入需要调用的转换方法:");
- // System.out.println("convertTxtToVisionmodeldata:输入1");
- // System.out.println("convertTxtToDam:输入2");
- // int type = scanner.nextInt();
- // scanner.reset();
- // System.out.println("输入源文件路径:");
- // String srcPath = scanner.next();
- // System.out.println("输入目标文件路径:");
- // String targetPath = scanner.next();
- // String type = args[0];//类型(1-vision.txt转为vision.modeldata, 2-modeldata.txt转为tieta.dam)
- // String srcPath = args[1];//源文件目录(需要提前源文件放到此目录中再调用方法)
- // String targetPath = args[2];//目标文件路径(目录,转换后文件会放到此目录中,文件名固定为vision.modeldata或tieta.dam)
- //
- // if(type.equals("1")){
- // String srcFileName = "vision.txt";
- // String targetFileName = "vision.modeldata";
- // CreateObjUtil.convertTxtToVisionmodeldata(srcPath.concat(File.separator).concat(srcFileName),targetPath.concat(File.separator).concat(targetFileName));
- // }
- // if(type.equals("2")){
- // String srcFileName = "modeldata.txt";
- // String targetFileName = "tieta.dam";
- // CreateObjUtil.convertTxtToDam(srcPath.concat(File.separator).concat(srcFileName),targetPath.concat(File.separator).concat(targetFileName));
- // }
- // scanner.close();
- // String srcFileName1 = "D:\\test\\tieta\\110106908000001067_001102050200000038732428\\modeldata.txt";
- // String srcFileName2 = "D:\\test\\tieta\\110115908000001104_000102050000000000079651\\modeldata.txt";
- // String srcFileName3 = "D:\\test\\tieta\\111401500010001724_500102050000000000285799\\modeldata.txt";
- // String targetFileName1 = "D:\\test\\tieta\\110106908000001067_001102050200000038732428\\tieta.dam";
- // String targetFileName2 = "D:\\test\\tieta\\110115908000001104_000102050000000000079651\\tieta.dam";
- // String targetFileName3 = "D:\\test\\tieta\\111401500010001724_500102050000000000285799\\tieta.dam";
- // CreateObjUtil.convertTxtToDam(srcFileName1,targetFileName1);
- // CreateObjUtil.convertTxtToDam(srcFileName2,targetFileName2);
- // CreateObjUtil.convertTxtToDam(srcFileName3,targetFileName3);
- CreateObjUtil.convertTxtToVisionmodeldata("C:\\Users\\dsx\\Desktop\\vsion.txt", "C:\\Users\\dsx\\Desktop\\vsion.modeldata");
- }
- /**
- * modeldata.txt 转 dam
- * @param srcpath 源文件全路径,如D:\test\modeldata.txt
- * @param despath 目标文件全路径,如D:\test\tieta.dam
- * @throws Exception
- */
- public static void convertTxtToDam(String srcpath,String despath)throws Exception{
- InputStream inputStream = null;
- InputStreamReader reader = null;
- ByteArrayInputStream stream = null;
- BufferedOutputStream bos = null;
- BufferedInputStream bis = null;
- try{
- BigSceneProto.binary_mesh.Builder builder = BigSceneProto.binary_mesh.newBuilder();
- inputStream = new FileInputStream(srcpath);
- reader = new InputStreamReader(inputStream, "ASCII");
- TextFormat.merge(reader, builder);
- byte[] buf = builder.build().toByteArray();
- //把序列化后的数据写入本地磁盘
- stream = new ByteArrayInputStream(buf);
- bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
- bis = new BufferedInputStream(stream);
- int b = -1;
- while ((b = bis.read()) != -1) {
- bos.write(b);
- }
- }catch (Exception e){
- log.error("txt转dam出错,srcpath:{}, despath:{}",srcpath, despath);
- throw e;
- }finally {
- if(inputStream != null){
- inputStream.close();
- }
- if(reader != null){
- reader.close();
- }
- if(stream != null){
- stream.close();
- }
- if(bos != null){
- bos.close();
- }
- if(bis != null){
- bis.close();
- }
- }
- }
- /**
- * vision.txt 转 vision.modeldata
- * @param srcpath 源文件全路径,如D:\test\vision.txt
- * @param despath 目标文件全路径,如D:\test\vision.modeldata
- * @throws Exception
- */
- public static void convertTxtToVisionmodeldata(String srcpath,String despath)throws Exception {
- Visionmodeldata.NavigationInfo.Builder builder = Visionmodeldata.NavigationInfo.newBuilder();
- String jsonFormat = transferVisionUuidToIndex(srcpath);
- JsonFormat.merge(jsonFormat, builder);
- byte[] buf= builder.build().toByteArray();
- //把序列化后的数据写入本地磁盘
- try(ByteArrayInputStream stream = new ByteArrayInputStream(buf);
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
- BufferedInputStream bis = new BufferedInputStream(stream)){
- int b = -1;
- while ((b = bis.read()) != -1) {
- bos.write(b);
- }
- }
- }
- public static String readTxtFileToJson(String filePath){
- try {
- String encoding="UTF-8";
- File file=new File(filePath);
- if(file.isFile() && file.exists()){
- InputStreamReader read = new InputStreamReader(
- new FileInputStream(file),encoding);
- BufferedReader bufferedReader = new BufferedReader(read);
- String lineTxt = null;
- String result="";
- while((lineTxt = bufferedReader.readLine()) != null){
- result+=lineTxt;
- //log.info(lineTxt);
- }
- read.close();
- return result;
- }else{
- return null;
- }
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- public static String transferVisionUuidToIndex(String srcPath){
- String visionJsonStr = readTxtFileToJson(srcPath);
- JSONObject visionJson = JSON.parseObject(visionJsonStr);
- JSONArray sweepLocationArr = visionJson.getJSONArray("sweepLocations");
- Map<String, Integer> uuidIndexMap = new HashMap<>();
- for (int i = 0; i < sweepLocationArr.size(); i++) {
- JSONObject sweepLocationObj = (JSONObject)sweepLocationArr.get(i);
- uuidIndexMap.put(sweepLocationObj.getString("uuid"), i);
- }
- for (Object o : sweepLocationArr) {
- JSONObject sweepLocationObj = (JSONObject)o;
- JSONArray visibles = sweepLocationObj.getJSONArray("visibles");
- // int[] visiblesArr = new int[visibles.size()];
- List<Integer> visiblesList = new ArrayList<>();
- for(int i = 0; i < visibles.size(); i++){
- Integer index = uuidIndexMap.get(visibles.get(i));
- if(Objects.isNull(index)){
- continue;
- }
- visiblesList.add(index);
- }
- sweepLocationObj.put("visibles", visiblesList);
- if(sweepLocationObj.containsKey("visibles2")){
- JSONArray visibles2 = sweepLocationObj.getJSONArray("visibles2");
- // int[] visiblesArr2 = new int[visibles2.size()];
- List<Integer> visibles2List = new ArrayList<>();
- for(int i = 0; i < visibles2.size(); i++){
- Integer index = uuidIndexMap.get(visibles2.get(i));
- if(Objects.isNull(index)){
- continue;
- }
- visibles2List.add(index);
- }
- sweepLocationObj.put("visibles2", visibles2List);
- }
- if(sweepLocationObj.containsKey("visibles3")){
- JSONArray visibles3 = sweepLocationObj.getJSONArray("visibles3");
- // int[] visiblesArr3 = new int[visibles3.size()];
- List<Integer> visibles3List = new ArrayList<>();
- for(int i = 0; i < visibles3.size(); i++){
- Integer index = uuidIndexMap.get(visibles3.get(i));
- if(Objects.isNull(index)){
- continue;
- }
- visibles3List.add(index);
- }
- sweepLocationObj.put("visibles3", visibles3List);
- }
- }
- return JSON.toJSONString(visionJson);
- }
- }
|