OBJToGLBUtil.java 8.4 KB

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