OBJToGLBUtil.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package com.fdkankan.fusion.common.util;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.core.io.file.FileReader;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.fdkankan.fusion.common.FilePath;
  6. import com.fdkankan.fusion.common.ResultCode;
  7. import com.fdkankan.fusion.exception.BusinessException;
  8. import lombok.extern.slf4j.Slf4j;
  9. import java.io.*;
  10. import java.nio.charset.StandardCharsets;
  11. import java.util.Date;
  12. import java.util.LinkedHashSet;
  13. @Slf4j
  14. public class OBJToGLBUtil {
  15. public static String objToGlb(String objPath, String glbPath) {
  16. log.info("obj转换glb开始,{}",objPath);
  17. if(!checkObj(objPath)){
  18. throw new BusinessException(-1,"obj文件错误");
  19. }
  20. log.info("obj转换glb开始");
  21. String command = String.format(ShellCmd.obj2glb_cmd,objPath,glbPath);
  22. log.info("执行obj转换glb命令路径-{}", command);
  23. ShellUtil.execCmd(command);
  24. log.info("obj转换glb完毕:" + command);
  25. return glbPath;
  26. }
  27. public static void objToGlb2(String objPath,String glbPath) {
  28. log.info("obj转换glb开始,{}",objPath);
  29. log.info("obj转换glb开始");
  30. String command = String.format(ShellCmd.obj2glb_cmd,objPath,glbPath);
  31. log.info("执行obj转换glb命令路径-{}", command);
  32. ShellUtil.execCmd(command);
  33. log.info("obj转换glb完毕:" + command);
  34. }
  35. public static String objToB3dm(String objPath, String glbPath) {
  36. Integer lodNum = getLodNum(objPath);
  37. log.info("obj转换b3dm开始,{}",objPath);
  38. log.info("obj转换b3dm开始");
  39. //String command = "Obj2Tiles --lods "+lodNum+" --divisions 3 " + objPath + " " + glbPath;
  40. String command = "Obj2Tiles " + objPath + " " + glbPath;
  41. log.info("执行obj转换glb命令路径-{}", command);
  42. ShellUtil.execCmd(command);
  43. log.info("obj转换b3dm完毕:" + command);
  44. return glbPath;
  45. }
  46. private static Integer getLodNum(String objPath) {
  47. // long length = new File(objPath).length();
  48. // long mb = length / 1024 /1024;
  49. // if(mb >100){
  50. // return (mb /10) >10 ?10:Integer.parseInt(String.valueOf(mb /10));
  51. // }
  52. return 3;
  53. }
  54. public static boolean checkObj(String objPath) {
  55. File file = new File(objPath);
  56. File file1 = file.getParentFile();
  57. File[] files = file1.listFiles();
  58. if(files == null || files.length <=0){
  59. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  60. }
  61. File mtlFile = null;
  62. File objFile = null;
  63. for (File file2 : files) {
  64. if(file2.isDirectory()){
  65. return checkObj(file2.getPath());
  66. }
  67. if(file2.getName().contains(".obj") ){
  68. objFile = file2;
  69. }
  70. if(file2.getName().contains(".mtl") ){
  71. mtlFile = file2;
  72. }
  73. }
  74. if(mtlFile == null || objFile == null ){
  75. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  76. }
  77. return checkMtl(file1,mtlFile);
  78. }
  79. private static boolean checkMtl(File allFile,File file) {
  80. if(!file.getName().contains("mtl")){
  81. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  82. }
  83. LinkedHashSet<String> imgName = new LinkedHashSet<>();
  84. if(allFile == null ){
  85. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  86. }
  87. File[] files = allFile.listFiles();
  88. if(files == null || files.length<=0 ){
  89. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  90. }
  91. for (File listFile : files) {
  92. String modelName = listFile.getName();
  93. imgName.add(modelName);
  94. }
  95. LinkedHashSet<String> imgMtl = readMtlFile(file.getPath());
  96. for (String mtlName : imgMtl) {
  97. if(!imgName.contains(mtlName)){
  98. throw new BusinessException(ResultCode.UPLOAD_FILE_MSG_ERROR.code,mtlName +".jpg 图片缺失!");
  99. }
  100. }
  101. updateMtlFile(file);
  102. return true;
  103. }
  104. private static void updateMtlFile(File file) {
  105. String mtlStr = FileUtil.readUtf8String(file);
  106. String[] split = mtlStr.split("\n");
  107. StringBuilder newMtlStr = new StringBuilder();
  108. for (String s : split) {
  109. if(s.toLowerCase().contains("tr")){
  110. String[] split1 = s.split(" ");
  111. if(split1.length <=1){
  112. continue;
  113. }
  114. System.out.println(split1[1]);
  115. System.out.println(isNumeric2(split1[1]));
  116. if(isNumeric2(split1[1])){
  117. String[] split2 = split1[1].split("\\.");
  118. int number = Integer.parseInt(split2[0]);
  119. if(number >=1){
  120. number = 1 - number;
  121. }
  122. String numStr = number + (split2.length <=1 ?"": "."+split2[1]);
  123. s = s.replace(split1[1],numStr);
  124. }
  125. }
  126. newMtlStr.append(s).append("\n");
  127. }
  128. FileUtil.copyContent(file,new File(file.getPath()+new Date().getTime()+"back"),true);
  129. FileUtil.writeString(newMtlStr.toString(),file, StandardCharsets.UTF_8);
  130. }
  131. public static boolean isNumeric2(String str) {
  132. try {
  133. double i = Double.valueOf(str);
  134. return true;
  135. }catch (Exception e){
  136. e.printStackTrace();
  137. }
  138. return false;
  139. }
  140. public static LinkedHashSet<String> readMtlFile(String mtlPath) {
  141. LinkedHashSet<String> imgName = new LinkedHashSet<>();
  142. FileInputStream fis = null;
  143. BufferedReader br = null;
  144. try {
  145. fis = new FileInputStream(new File(mtlPath));
  146. br = new BufferedReader(new InputStreamReader(fis));
  147. String line = null;
  148. while ((line = br.readLine()) != null) {
  149. String[] tempsa = line.split("[ ]+");
  150. if (tempsa[0].trim().equals("map_Ka")) {
  151. String mtlName = tempsa[1];
  152. if(mtlName.contains("/")){
  153. String[] split = mtlName.split("/");
  154. mtlName = split[split.length-1];
  155. }
  156. imgName.add(mtlName);
  157. }
  158. if (tempsa[0].trim().equals("map_Kd")) {
  159. String mtlName = tempsa[1];
  160. if(mtlName.contains("/")){
  161. String[] split = mtlName.split("/");
  162. mtlName = split[split.length-1];
  163. }
  164. imgName.add(mtlName);
  165. }
  166. }
  167. } catch (Exception e) {
  168. e.printStackTrace();
  169. }
  170. return imgName;
  171. }
  172. public static File lasOrPlyToBin(File srcFile){
  173. if(!srcFile.exists()){
  174. srcFile.mkdirs();
  175. }
  176. String cmd = String.format(ShellCmd.LAS_TO_BIN,srcFile.getPath(),srcFile.getParentFile().getPath());
  177. ShellUtil.execCmd(cmd);
  178. log.info("lasOrPlyToBin---------cmd-over");
  179. String cloudJs = srcFile.getParentFile().getPath() +"/webcloud/"+ "cloud.js";
  180. JSONObject jsonObject = fixCloud(cloudJs);
  181. FileWriterUtil.writerJson(srcFile.getParentFile().getPath() +"/webcloud/","cloud.js",jsonObject.toJSONString());
  182. return srcFile.getParentFile();
  183. }
  184. public static JSONObject fixCloud(String path) {
  185. cn.hutool.core.io.file.FileReader fileReader = new FileReader(path);
  186. String str = fileReader.readString();
  187. JSONObject json = JSONObject.parseObject(str);
  188. String[] pointAttributes = {
  189. "POSITION_CARTESIAN",
  190. "COLOR_PACKED",
  191. "NORMAL_OCT16"
  192. };
  193. json.put("pointAttributes", pointAttributes);
  194. return json;
  195. }
  196. public static String OsgbToB3dm(File objPathFile) {
  197. String targetPath = FilePath.MNT_BASE_PATH +"osgb" +"/" + objPathFile.getName();
  198. String sourcePath = FilePath.MNT_BASE_PATH +"b3dm" +"/" + objPathFile.getName();
  199. //ShellUtil.execCmd("cp -r " + objPath + " " + dockerPath+ objPathFile.getName() );
  200. File file = new File(targetPath);
  201. FileUtil.copyContent(objPathFile,file,true);
  202. String cmd = String.format(ShellCmd.osgbTob3dmCmd,targetPath,sourcePath);
  203. ShellUtil.execCmd(cmd);
  204. return sourcePath;
  205. }
  206. public static String OsgbToB3dm(String targetPath,String sourcePath) {
  207. String cmd = String.format(ShellCmd.osgbTob3dmCmd,targetPath,sourcePath);
  208. ShellUtil.execCmd(cmd);
  209. return sourcePath;
  210. }
  211. }