OBJToGLBUtil.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.config.CacheUtil;
  8. import com.fdkankan.fusion.exception.BusinessException;
  9. import lombok.extern.slf4j.Slf4j;
  10. import java.io.*;
  11. import java.nio.charset.StandardCharsets;
  12. import java.util.Date;
  13. import java.util.LinkedHashSet;
  14. @Slf4j
  15. public class OBJToGLBUtil {
  16. public static String objToGlb(String objPath, String glbPath) {
  17. log.info("obj转换glb开始,{}",objPath);
  18. if(!checkObj(objPath)){
  19. throw new BusinessException(-1,"obj文件错误");
  20. }
  21. log.info("obj转换glb开始");
  22. String command = String.format(ShellCmd.obj2glb_cmd,objPath,glbPath);
  23. log.info("执行obj转换glb命令路径-{}", command);
  24. ShellUtil.execCmd(command);
  25. log.info("obj转换glb完毕:" + command);
  26. return glbPath;
  27. }
  28. public static void objToGlb2(String objPath,String glbPath) {
  29. log.info("obj转换glb开始,{}",objPath);
  30. log.info("obj转换glb开始");
  31. String command = String.format(ShellCmd.obj2glb_cmd,objPath,glbPath);
  32. log.info("执行obj转换glb命令路径-{}", command);
  33. ShellUtil.execCmd(command);
  34. log.info("obj转换glb完毕:" + command);
  35. }
  36. public static String objToB3dm(String objPath, String glbPath) {
  37. Integer lodNum = getLodNum(objPath);
  38. log.info("obj转换b3dm开始,{}",objPath);
  39. log.info("obj转换b3dm开始");
  40. //String command = "Obj2Tiles --lods "+lodNum+" --divisions 3 " + objPath + " " + glbPath;
  41. String command = "Obj2Tiles " + objPath + " " + glbPath;
  42. log.info("执行obj转换glb命令路径-{}", command);
  43. ShellUtil.execCmd(command);
  44. log.info("obj转换b3dm完毕:" + command);
  45. return glbPath;
  46. }
  47. private static Integer getLodNum(String objPath) {
  48. // long length = new File(objPath).length();
  49. // long mb = length / 1024 /1024;
  50. // if(mb >100){
  51. // return (mb /10) >10 ?10:Integer.parseInt(String.valueOf(mb /10));
  52. // }
  53. return 3;
  54. }
  55. public static boolean checkObj(String objPath) {
  56. File file = new File(objPath);
  57. File file1 = file.getParentFile();
  58. File[] files = file1.listFiles();
  59. if(files == null || files.length <=0){
  60. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  61. }
  62. File mtlFile = null;
  63. File objFile = null;
  64. for (File file2 : files) {
  65. if(file2.isDirectory()){
  66. //return checkObj(file2.getPath());
  67. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  68. }
  69. if(file2.getName().contains(".obj") ){
  70. objFile = file2;
  71. }
  72. if(file2.getName().contains(".mtl") ){
  73. mtlFile = file2;
  74. }
  75. }
  76. if(mtlFile == null || objFile == null ){
  77. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  78. }
  79. return checkMtl(file1,mtlFile);
  80. }
  81. private static boolean checkMtl(File allFile,File file) {
  82. if(!file.getName().contains("mtl")){
  83. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  84. }
  85. LinkedHashSet<String> imgName = new LinkedHashSet<>();
  86. if(allFile == null ){
  87. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  88. }
  89. File[] files = allFile.listFiles();
  90. if(files == null || files.length<=0 ){
  91. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  92. }
  93. for (File listFile : files) {
  94. String modelName = listFile.getName();
  95. imgName.add(modelName);
  96. }
  97. LinkedHashSet<String> imgMtl = readMtlFile(file.getPath());
  98. for (String mtlName : imgMtl) {
  99. if(!imgName.contains(mtlName)){
  100. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  101. }
  102. }
  103. updateMtlFile(file);
  104. return true;
  105. }
  106. private static void updateMtlFile(File file) {
  107. String mtlStr = FileUtil.readUtf8String(file);
  108. String[] split = mtlStr.split("\n");
  109. StringBuilder newMtlStr = new StringBuilder();
  110. for (String s : split) {
  111. if(s.toLowerCase().contains("tr")){
  112. String[] split1 = s.split(" ");
  113. if(split1.length <=1){
  114. continue;
  115. }
  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(String targetPath,String sourcePath) {
  197. String cmd = String.format(ShellCmd.osgbTob3dmCmd2,targetPath,sourcePath);
  198. ShellUtil.execCmd(cmd);
  199. return sourcePath;
  200. }
  201. }