CreateObjUtil.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package com.fdkankan.modeldemo.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.fdkankan.modeldemo.proto.BigSceneProto;
  6. import com.fdkankan.modeldemo.proto.Visionmodeldata;
  7. import com.fdkankan.modeldemo.proto.format.JsonFormat;
  8. import com.google.protobuf.TextFormat;
  9. import lombok.extern.slf4j.Slf4j;
  10. import java.io.*;
  11. import java.util.*;
  12. @Slf4j
  13. public class CreateObjUtil {
  14. public static void main(String[] args) throws Exception {
  15. // Scanner scanner = new Scanner(System.in);
  16. // System.out.println("输入需要调用的转换方法:");
  17. // System.out.println("convertTxtToVisionmodeldata:输入1");
  18. // System.out.println("convertTxtToDam:输入2");
  19. // int type = scanner.nextInt();
  20. // scanner.reset();
  21. // System.out.println("输入源文件路径:");
  22. // String srcPath = scanner.next();
  23. // System.out.println("输入目标文件路径:");
  24. // String targetPath = scanner.next();
  25. // String type = args[0];//类型(1-vision.txt转为vision.modeldata, 2-modeldata.txt转为tieta.dam)
  26. // String srcPath = args[1];//源文件目录(需要提前源文件放到此目录中再调用方法)
  27. // String targetPath = args[2];//目标文件路径(目录,转换后文件会放到此目录中,文件名固定为vision.modeldata或tieta.dam)
  28. //
  29. // if(type.equals("1")){
  30. // String srcFileName = "vision.txt";
  31. // String targetFileName = "vision.modeldata";
  32. // CreateObjUtil.convertTxtToVisionmodeldata(srcPath.concat(File.separator).concat(srcFileName),targetPath.concat(File.separator).concat(targetFileName));
  33. // }
  34. // if(type.equals("2")){
  35. // String srcFileName = "modeldata.txt";
  36. // String targetFileName = "tieta.dam";
  37. // CreateObjUtil.convertTxtToDam(srcPath.concat(File.separator).concat(srcFileName),targetPath.concat(File.separator).concat(targetFileName));
  38. // }
  39. // scanner.close();
  40. // String srcFileName1 = "D:\\test\\tieta\\110106908000001067_001102050200000038732428\\modeldata.txt";
  41. // String srcFileName2 = "D:\\test\\tieta\\110115908000001104_000102050000000000079651\\modeldata.txt";
  42. // String srcFileName3 = "D:\\test\\tieta\\111401500010001724_500102050000000000285799\\modeldata.txt";
  43. // String targetFileName1 = "D:\\test\\tieta\\110106908000001067_001102050200000038732428\\tieta.dam";
  44. // String targetFileName2 = "D:\\test\\tieta\\110115908000001104_000102050000000000079651\\tieta.dam";
  45. // String targetFileName3 = "D:\\test\\tieta\\111401500010001724_500102050000000000285799\\tieta.dam";
  46. // CreateObjUtil.convertTxtToDam(srcFileName1,targetFileName1);
  47. // CreateObjUtil.convertTxtToDam(srcFileName2,targetFileName2);
  48. // CreateObjUtil.convertTxtToDam(srcFileName3,targetFileName3);
  49. CreateObjUtil.convertTxtToVisionmodeldata("C:\\Users\\dsx\\Desktop\\vsion.txt", "C:\\Users\\dsx\\Desktop\\vsion.modeldata");
  50. }
  51. /**
  52. * modeldata.txt 转 dam
  53. * @param srcpath 源文件全路径,如D:\test\modeldata.txt
  54. * @param despath 目标文件全路径,如D:\test\tieta.dam
  55. * @throws Exception
  56. */
  57. public static void convertTxtToDam(String srcpath,String despath)throws Exception{
  58. InputStream inputStream = null;
  59. InputStreamReader reader = null;
  60. ByteArrayInputStream stream = null;
  61. BufferedOutputStream bos = null;
  62. BufferedInputStream bis = null;
  63. try{
  64. BigSceneProto.binary_mesh.Builder builder = BigSceneProto.binary_mesh.newBuilder();
  65. inputStream = new FileInputStream(srcpath);
  66. reader = new InputStreamReader(inputStream, "ASCII");
  67. TextFormat.merge(reader, builder);
  68. byte[] buf = builder.build().toByteArray();
  69. //把序列化后的数据写入本地磁盘
  70. stream = new ByteArrayInputStream(buf);
  71. bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
  72. bis = new BufferedInputStream(stream);
  73. int b = -1;
  74. while ((b = bis.read()) != -1) {
  75. bos.write(b);
  76. }
  77. }catch (Exception e){
  78. log.error("txt转dam出错,srcpath:{}, despath:{}",srcpath, despath);
  79. throw e;
  80. }finally {
  81. if(inputStream != null){
  82. inputStream.close();
  83. }
  84. if(reader != null){
  85. reader.close();
  86. }
  87. if(stream != null){
  88. stream.close();
  89. }
  90. if(bos != null){
  91. bos.close();
  92. }
  93. if(bis != null){
  94. bis.close();
  95. }
  96. }
  97. }
  98. /**
  99. * vision.txt 转 vision.modeldata
  100. * @param srcpath 源文件全路径,如D:\test\vision.txt
  101. * @param despath 目标文件全路径,如D:\test\vision.modeldata
  102. * @throws Exception
  103. */
  104. public static void convertTxtToVisionmodeldata(String srcpath,String despath)throws Exception {
  105. Visionmodeldata.NavigationInfo.Builder builder = Visionmodeldata.NavigationInfo.newBuilder();
  106. String jsonFormat = transferVisionUuidToIndex(srcpath);
  107. JsonFormat.merge(jsonFormat, builder);
  108. byte[] buf= builder.build().toByteArray();
  109. //把序列化后的数据写入本地磁盘
  110. try(ByteArrayInputStream stream = new ByteArrayInputStream(buf);
  111. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
  112. BufferedInputStream bis = new BufferedInputStream(stream)){
  113. int b = -1;
  114. while ((b = bis.read()) != -1) {
  115. bos.write(b);
  116. }
  117. }
  118. }
  119. public static String readTxtFileToJson(String filePath){
  120. try {
  121. String encoding="UTF-8";
  122. File file=new File(filePath);
  123. if(file.isFile() && file.exists()){
  124. InputStreamReader read = new InputStreamReader(
  125. new FileInputStream(file),encoding);
  126. BufferedReader bufferedReader = new BufferedReader(read);
  127. String lineTxt = null;
  128. String result="";
  129. while((lineTxt = bufferedReader.readLine()) != null){
  130. result+=lineTxt;
  131. //log.info(lineTxt);
  132. }
  133. read.close();
  134. return result;
  135. }else{
  136. return null;
  137. }
  138. } catch (Exception e) {
  139. e.printStackTrace();
  140. return null;
  141. }
  142. }
  143. public static String transferVisionUuidToIndex(String srcPath){
  144. String visionJsonStr = readTxtFileToJson(srcPath);
  145. JSONObject visionJson = JSON.parseObject(visionJsonStr);
  146. JSONArray sweepLocationArr = visionJson.getJSONArray("sweepLocations");
  147. Map<String, Integer> uuidIndexMap = new HashMap<>();
  148. for (int i = 0; i < sweepLocationArr.size(); i++) {
  149. JSONObject sweepLocationObj = (JSONObject)sweepLocationArr.get(i);
  150. uuidIndexMap.put(sweepLocationObj.getString("uuid"), i);
  151. }
  152. for (Object o : sweepLocationArr) {
  153. JSONObject sweepLocationObj = (JSONObject)o;
  154. JSONArray visibles = sweepLocationObj.getJSONArray("visibles");
  155. // int[] visiblesArr = new int[visibles.size()];
  156. List<Integer> visiblesList = new ArrayList<>();
  157. for(int i = 0; i < visibles.size(); i++){
  158. Integer index = uuidIndexMap.get(visibles.get(i));
  159. if(Objects.isNull(index)){
  160. continue;
  161. }
  162. visiblesList.add(index);
  163. }
  164. sweepLocationObj.put("visibles", visiblesList);
  165. if(sweepLocationObj.containsKey("visibles2")){
  166. JSONArray visibles2 = sweepLocationObj.getJSONArray("visibles2");
  167. // int[] visiblesArr2 = new int[visibles2.size()];
  168. List<Integer> visibles2List = new ArrayList<>();
  169. for(int i = 0; i < visibles2.size(); i++){
  170. Integer index = uuidIndexMap.get(visibles2.get(i));
  171. if(Objects.isNull(index)){
  172. continue;
  173. }
  174. visibles2List.add(index);
  175. }
  176. sweepLocationObj.put("visibles2", visibles2List);
  177. }
  178. if(sweepLocationObj.containsKey("visibles3")){
  179. JSONArray visibles3 = sweepLocationObj.getJSONArray("visibles3");
  180. // int[] visiblesArr3 = new int[visibles3.size()];
  181. List<Integer> visibles3List = new ArrayList<>();
  182. for(int i = 0; i < visibles3.size(); i++){
  183. Integer index = uuidIndexMap.get(visibles3.get(i));
  184. if(Objects.isNull(index)){
  185. continue;
  186. }
  187. visibles3List.add(index);
  188. }
  189. sweepLocationObj.put("visibles3", visibles3List);
  190. }
  191. }
  192. return JSON.toJSONString(visionJson);
  193. }
  194. }