OBJToGLBUtil.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.fdkankan.fusion.common.util;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.fdkankan.fusion.common.FilePath;
  5. import com.fdkankan.fusion.common.ResultCode;
  6. import com.fdkankan.fusion.exception.BusinessException;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
  9. import java.io.*;
  10. import java.util.LinkedHashSet;
  11. @Slf4j
  12. public class OBJToGLBUtil {
  13. public static String objToGlb(String objPath, String glbPath) {
  14. log.info("obj转换glb开始,{}",objPath);
  15. if(!checkObj(objPath)){
  16. throw new BusinessException(-1,"obj文件错误");
  17. }
  18. log.info("obj转换glb开始");
  19. String command = "obj2gltf -i " + objPath + " -o " + glbPath;
  20. log.info("执行obj转换glb命令路径-{}", command);
  21. ShellUtil.execCmd(command);
  22. log.info("obj转换glb完毕:" + command);
  23. return glbPath;
  24. }
  25. public static void objToGlb2(String objPath,String glbPath) {
  26. log.info("obj转换glb开始,{}",objPath);
  27. log.info("obj转换glb开始");
  28. String command = "obj2gltf -i " + objPath + " -o " + glbPath;
  29. log.info("执行obj转换glb命令路径-{}", command);
  30. ShellUtil.execCmd(command);
  31. log.info("obj转换glb完毕:" + command);
  32. }
  33. public static String objToB3dm(String objPath, String glbPath) {
  34. Integer lodNum = getLodNum(objPath);
  35. log.info("obj转换b3dm开始,{}",objPath);
  36. log.info("obj转换b3dm开始");
  37. //String command = "Obj2Tiles --lods "+lodNum+" --divisions 3 " + objPath + " " + glbPath;
  38. String command = "Obj2Tiles " + objPath + " " + glbPath;
  39. log.info("执行obj转换glb命令路径-{}", command);
  40. ShellUtil.execCmd(command);
  41. log.info("obj转换b3dm完毕:" + command);
  42. return glbPath;
  43. }
  44. private static Integer getLodNum(String objPath) {
  45. // long length = new File(objPath).length();
  46. // long mb = length / 1024 /1024;
  47. // if(mb >100){
  48. // return (mb /10) >10 ?10:Integer.parseInt(String.valueOf(mb /10));
  49. // }
  50. return 3;
  51. }
  52. public static boolean checkObj(String objPath) {
  53. File file = new File(objPath);
  54. File file1 = file.getParentFile();
  55. File[] files = file1.listFiles();
  56. if(files == null || files.length <=0){
  57. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  58. }
  59. File mtlFile = null;
  60. File objFile = null;
  61. for (File file2 : files) {
  62. if(file2.isDirectory()){
  63. return checkObj(file2.getPath());
  64. }
  65. if(file2.getName().contains(".obj") ){
  66. objFile = file2;
  67. }
  68. if(file2.getName().contains(".mtl") ){
  69. mtlFile = file2;
  70. }
  71. }
  72. if(mtlFile == null || objFile == null ){
  73. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  74. }
  75. return checkMtl(file1,mtlFile);
  76. }
  77. private static boolean checkMtl(File allFile,File file) {
  78. if(!file.getName().contains("mtl")){
  79. return false;
  80. }
  81. LinkedHashSet<String> imgName = new LinkedHashSet<>();
  82. if(allFile == null || allFile.length()<=0 ){
  83. return false;
  84. }
  85. File[] files = allFile.listFiles();
  86. if(files == null || files.length<=0 ){
  87. return false;
  88. }
  89. for (File listFile : files) {
  90. String modelName = listFile.getName();
  91. imgName.add(modelName);
  92. }
  93. LinkedHashSet<String> imgMtl = readMtlFile(file.getPath());
  94. for (String mtlName : imgMtl) {
  95. if(!imgName.contains(mtlName)){
  96. throw new BusinessException(-1,mtlName +".jpg 图片缺失!");
  97. }
  98. }
  99. return true;
  100. }
  101. public static LinkedHashSet<String> readMtlFile(String mtlPath) {
  102. LinkedHashSet<String> imgName = new LinkedHashSet<>();
  103. FileInputStream fis = null;
  104. BufferedReader br = null;
  105. try {
  106. fis = new FileInputStream(new File(mtlPath));
  107. br = new BufferedReader(new InputStreamReader(fis));
  108. String line = null;
  109. while ((line = br.readLine()) != null) {
  110. String[] tempsa = line.split("[ ]+");
  111. if (tempsa[0].trim().equals("map_Ka")) {
  112. String mtlName = tempsa[1];
  113. if(mtlName.contains("/")){
  114. String[] split = mtlName.split("/");
  115. mtlName = split[split.length-1];
  116. }
  117. imgName.add(mtlName);
  118. }
  119. if (tempsa[0].trim().equals("map_Kd")) {
  120. String mtlName = tempsa[1];
  121. if(mtlName.contains("/")){
  122. String[] split = mtlName.split("/");
  123. mtlName = split[split.length-1];
  124. }
  125. imgName.add(mtlName);
  126. }
  127. }
  128. } catch (Exception e) {
  129. e.printStackTrace();
  130. }
  131. return imgName;
  132. }
  133. public static File lasOrPlyToBin(File srcFile) throws IOException {
  134. if(!srcFile.exists()){
  135. srcFile.mkdirs();
  136. }
  137. String cmd = ShellUtil.LAS_TO_BIN;
  138. cmd = cmd.replace("@inPath",srcFile.getPath());
  139. cmd = cmd.replace("@outPath",srcFile.getParentFile().getPath());
  140. ShellUtil.execCmd(cmd);
  141. log.info("lasOrPlyToBin---------cmd-over");
  142. String cloudJs = srcFile.getParentFile().getPath() +"/webcloud/"+ "cloud.js";
  143. JSONObject jsonObject = ShellUtil.fixCloud(cloudJs);
  144. FileWriterUtil.writerJson(srcFile.getParentFile().getPath() +"/webcloud/","cloud.js",jsonObject.toJSONString());
  145. return srcFile.getParentFile();
  146. }
  147. public static String OsgbToB3dm(File objPathFile) {
  148. String targetPath = FilePath.MNT_BASE_PATH +"osgb" +"/" + objPathFile.getName();
  149. String sourcePath = FilePath.MNT_BASE_PATH +"b3dm" +"/" + objPathFile.getName();
  150. //ShellUtil.execCmd("cp -r " + objPath + " " + dockerPath+ objPathFile.getName() );
  151. File file = new File(targetPath);
  152. FileUtil.copyContent(objPathFile,file,true);
  153. String cmd = ShellCmd.osgbTob3dmCmd.replaceAll("@path",FilePath.MNT_BASE_PATH);
  154. cmd =cmd.replaceAll("@inputFile",targetPath);
  155. cmd =cmd.replaceAll("@outputFile",sourcePath);
  156. ShellUtil.execCmd(cmd);
  157. return sourcePath;
  158. }
  159. }