OBJToGLBUtil.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. log.info("执行obj转换glb命令路径-{}", command);
  39. ShellUtil.execCmd(command);
  40. log.info("obj转换b3dm完毕:" + command);
  41. return glbPath;
  42. }
  43. private static Integer getLodNum(String objPath) {
  44. // long length = new File(objPath).length();
  45. // long mb = length / 1024 /1024;
  46. // if(mb >100){
  47. // return (mb /10) >10 ?10:Integer.parseInt(String.valueOf(mb /10));
  48. // }
  49. return 3;
  50. }
  51. public static boolean checkObj(String objPath) {
  52. File file = new File(objPath);
  53. File file1 = file.getParentFile();
  54. File[] files = file1.listFiles();
  55. if(files == null || files.length <=0){
  56. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  57. }
  58. File mtlFile = null;
  59. File objFile = null;
  60. for (File file2 : files) {
  61. if(file2.isDirectory()){
  62. return checkObj(file2.getPath());
  63. }
  64. if(file2.getName().contains(".obj") ){
  65. objFile = file2;
  66. }
  67. if(file2.getName().contains(".mtl") ){
  68. mtlFile = file2;
  69. }
  70. }
  71. if(mtlFile == null || objFile == null ){
  72. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  73. }
  74. return checkMtl(file1,mtlFile);
  75. }
  76. private static boolean checkMtl(File allFile,File file) {
  77. if(!file.getName().contains("mtl")){
  78. return false;
  79. }
  80. LinkedHashSet<String> imgName = new LinkedHashSet<>();
  81. if(allFile == null || allFile.length()<=0 ){
  82. return false;
  83. }
  84. File[] files = allFile.listFiles();
  85. if(files == null || files.length<=0 ){
  86. return false;
  87. }
  88. for (File listFile : files) {
  89. String modelName = listFile.getName();
  90. imgName.add(modelName);
  91. }
  92. LinkedHashSet<String> imgMtl = readMtlFile(file.getPath());
  93. for (String mtlName : imgMtl) {
  94. if(!imgName.contains(mtlName)){
  95. throw new BusinessException(-1,mtlName +".jpg 图片缺失!");
  96. }
  97. }
  98. return true;
  99. }
  100. public static LinkedHashSet<String> readMtlFile(String mtlPath) {
  101. LinkedHashSet<String> imgName = new LinkedHashSet<>();
  102. FileInputStream fis = null;
  103. BufferedReader br = null;
  104. try {
  105. fis = new FileInputStream(new File(mtlPath));
  106. br = new BufferedReader(new InputStreamReader(fis));
  107. String line = null;
  108. while ((line = br.readLine()) != null) {
  109. String[] tempsa = line.split("[ ]+");
  110. if (tempsa[0].trim().equals("map_Ka")) {
  111. String mtlName = tempsa[1];
  112. if(mtlName.contains("/")){
  113. String[] split = mtlName.split("/");
  114. mtlName = split[split.length-1];
  115. }
  116. imgName.add(mtlName);
  117. }
  118. if (tempsa[0].trim().equals("map_Kd")) {
  119. String mtlName = tempsa[1];
  120. if(mtlName.contains("/")){
  121. String[] split = mtlName.split("/");
  122. mtlName = split[split.length-1];
  123. }
  124. imgName.add(mtlName);
  125. }
  126. }
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. }
  130. return imgName;
  131. }
  132. public static File lasOrPlyToBin(File srcFile) throws IOException {
  133. if(!srcFile.exists()){
  134. srcFile.mkdirs();
  135. }
  136. String cmd = ShellUtil.LAS_TO_BIN;
  137. cmd = cmd.replace("@inPath",srcFile.getPath());
  138. cmd = cmd.replace("@outPath",srcFile.getParentFile().getPath());
  139. ShellUtil.execCmd(cmd);
  140. log.info("lasOrPlyToBin---------cmd-over");
  141. String cloudJs = srcFile.getParentFile().getPath() +"/webcloud/"+ "cloud.js";
  142. JSONObject jsonObject = ShellUtil.fixCloud(cloudJs);
  143. FileWriterUtil.writerJson(srcFile.getParentFile().getPath() +"/webcloud/","cloud.js",jsonObject.toJSONString());
  144. return srcFile.getParentFile();
  145. }
  146. public static String OsgbToB3dm(File objPathFile) {
  147. String targetPath = FilePath.MNT_BASE_PATH +"osgb" +"/" + objPathFile.getName();
  148. String sourcePath = FilePath.MNT_BASE_PATH +"b3dm" +"/" + objPathFile.getName();
  149. //ShellUtil.execCmd("cp -r " + objPath + " " + dockerPath+ objPathFile.getName() );
  150. File file = new File(targetPath);
  151. FileUtil.copyContent(objPathFile,file,true);
  152. String cmd = ShellCmd.osgbTob3dmCmd.replaceAll("@path",FilePath.MNT_BASE_PATH);
  153. cmd =cmd.replaceAll("@inputFile",targetPath);
  154. cmd =cmd.replaceAll("@outputFile",sourcePath);
  155. ShellUtil.execCmd(cmd);
  156. return sourcePath;
  157. }
  158. }