OBJToGLBUtil.java 6.7 KB

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