ConvertUtil.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package com.fdkankan.modeldemo.utils;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.fdkankan.modeldemo.bean.SceneEditControlsBean;
  7. import com.fdkankan.modeldemo.bean.SceneJsonBean;
  8. import com.fdkankan.modeldemo.constant.Constant;
  9. import com.fdkankan.modeldemo.entity.Scene;
  10. import com.fdkankan.modeldemo.service.SceneService;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Component;
  14. import java.io.File;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.Objects;
  19. @Component
  20. public class ConvertUtil {
  21. @Autowired
  22. private SceneService sceneService;
  23. public static void main(String[] args) throws Exception {
  24. String srcPath = args[0];
  25. System.out.println(srcPath);
  26. String targetPath = args[1];
  27. System.out.println(targetPath);
  28. // convert(srcPath, targetPath);
  29. // scanner.close();
  30. }
  31. /**
  32. *
  33. * @param sourcePath 原始资源目录
  34. * @param targetPath 目标资源目录
  35. * @return boolean true-转换成功 false-转换失败
  36. */
  37. public Map<String, String> convert(String sourcePath, String num) throws Exception {
  38. Map<String, String> map = new HashMap<>();
  39. String dataViewPath = String.format(Constant.DATA_VIEW_PATH, num);
  40. String imgViewPath = String.format(Constant.IMG_VIEW_PATH, num);
  41. String obj2TxtPath = null;
  42. if(!sourcePath.endsWith(File.separator)){
  43. sourcePath = sourcePath + File.separator;
  44. }
  45. obj2TxtPath = sourcePath.substring(0, sourcePath.length() - 1) + "_obj2txt";
  46. FileUtil.mkdir(obj2TxtPath);
  47. //调用算法生成modeldata.txt
  48. String extrasPath = obj2TxtPath + File.separator + "extras";
  49. FileUtil.mkdir(extrasPath);
  50. FileUtil.copyContent(new File(sourcePath + "data" + File.separator + "mesh"), new File(extrasPath), true);
  51. //写data.json
  52. writeDataJson(obj2TxtPath);
  53. //调用算法建模
  54. build3dModel(obj2TxtPath);
  55. //校验算法是否正常结束
  56. String uploadJsonPath = obj2TxtPath + File.separator + "results" +File.separator+"upload.json";
  57. boolean success = checkComputeCompleted(uploadJsonPath, 5, 300);
  58. if(!success){
  59. throw new RuntimeException("计算失败,obj2TxtPath:" + obj2TxtPath);
  60. }
  61. String uploadData = FileUtil.readUtf8String(uploadJsonPath);
  62. JSONObject uploadJson = null;
  63. JSONArray array = null;
  64. if(uploadData!=null) {
  65. uploadJson = JSONObject.parseObject(uploadData);
  66. array = uploadJson.getJSONArray("upload");
  67. }
  68. JSONObject fileJson = null;
  69. String fileName = "";
  70. for(int i = 0, len = array.size(); i < len; i++) {
  71. fileJson = array.getJSONObject(i);
  72. fileName = fileJson.getString("file");
  73. //文件不存在抛出异常
  74. if (!new File(obj2TxtPath + File.separator + "results" + File.separator + fileName).exists()) {
  75. throw new Exception(obj2TxtPath + File.separator + "results" + File.separator + fileName + "文件不存在");
  76. }
  77. //tex文件夹
  78. if (fileJson.getIntValue("clazz") == 15) {
  79. // FileUtil.copy(obj2TxtPath + File.separator + "results" + File.separator + fileName,
  80. // targetImagePath + File.separator + "tieta_texture/" + fileName.replace("tex/", ""),true);
  81. map.put(imgViewPath + "tieta_texture/" + fileName.replace("tex/", ""), obj2TxtPath + File.separator + "results" + File.separator + fileName);
  82. }
  83. }
  84. //压缩成dam
  85. String damKey = imgViewPath + "tieta.dam";
  86. String damPath = obj2TxtPath + File.separator + "results" +File.separator + "tieta.dam";
  87. CreateObjUtil.convertTxtToDam( obj2TxtPath + File.separator + "results" +File.separator+"modeldata.txt", damPath);
  88. map.put(damKey, damPath);
  89. //复制文件
  90. if(FileUtil.exist(sourcePath + "data/" + "floorplan.json")){
  91. String floorplanJson = FileUtil.readUtf8String(sourcePath + "data/" + "floorplan.json");
  92. JSONObject jsonObject = JSON.parseObject(floorplanJson);
  93. Integer currentId = jsonObject.getInteger("currentId");
  94. if(Objects.nonNull(currentId) && currentId == 0){
  95. FileUtil.del(sourcePath + "data/" + "floorplan.json");
  96. }
  97. }
  98. List<File> dataFiles = FileUtil.loopFiles(sourcePath + "data/");
  99. for (File dataFile : dataFiles) {
  100. map.put(dataFile.getAbsolutePath().replace(sourcePath + "data/", dataViewPath) , dataFile.getAbsolutePath());
  101. }
  102. String finalSourcePath = sourcePath;
  103. if(FileUtil.exist(finalSourcePath + "images/4k/")){
  104. FileUtil.listFileNames(sourcePath + "images/4k/").stream().forEach(v->{
  105. map.put(imgViewPath + "pan/high/" + v.replaceAll("-", ""), finalSourcePath + "images/4k/" + v);
  106. // FileUtil.copy(finalSourcePath + "images/4k/" + v, targetImagePath + "/pan/high/" + v.replaceAll("-", ""), true);
  107. });
  108. }
  109. if(FileUtil.exist(finalSourcePath + "images/512/")){
  110. FileUtil.listFileNames(finalSourcePath + "images/512/").stream().forEach(v->{
  111. map.put(imgViewPath + "pan/low/" + v.replaceAll("-", ""), finalSourcePath + "images/512/" + v);
  112. // FileUtil.copy(finalSourcePath + "images/512/" + v, targetImagePath + "/pan/low/" + v.replaceAll("-", ""), true);
  113. });
  114. }
  115. if(FileUtil.exist(finalSourcePath + "images/8k/")) {
  116. FileUtil.listFileNames(finalSourcePath + "images/8k/").stream().forEach(v -> {
  117. map.put(imgViewPath + "pan/8k/" + v.replaceAll("-", ""), finalSourcePath + "images/8k/" + v);
  118. // FileUtil.copy(finalSourcePath + "images/8k/" + v, targetImagePath + "/pan/8k/" + v.replaceAll("-", ""), true);
  119. });
  120. }
  121. map.put(imgViewPath + "vision.txt", sourcePath + "images/vision.txt");
  122. // FileUtil.copy(sourcePath + "images/vision.txt", targetImagePath + "/vision.txt", true);
  123. //生成vison.modeldata
  124. CreateObjUtil.convertTxtToVisionmodeldata(sourcePath + "images/vision.txt", sourcePath + "images/vision.modeldata");
  125. map.put(imgViewPath + "vision.modeldata", sourcePath + "images/vision.modeldata");
  126. //生成场景标题
  127. String title = this.getTitle(num, sourcePath);
  128. //生成scene.json
  129. SceneJsonBean sceneJsonBean = this.genSceneJson(num, title);
  130. FileUtil.writeUtf8String(JSON.toJSONString(sceneJsonBean), sourcePath + "data/scene.json");
  131. map.put(dataViewPath + "scene.json", sourcePath + "data/scene.json");
  132. return map;
  133. }
  134. private SceneJsonBean genSceneJson(String roomId, String title){
  135. Scene scene = sceneService.getByNum(roomId);
  136. if(Objects.isNull(scene)){
  137. scene = new Scene();
  138. }
  139. scene.setTitle(title);
  140. scene.setNum(roomId);
  141. scene.setFloorlogosize(100);
  142. scene.setScenekind("pano");
  143. scene.setSceneresolution("4k");
  144. scene.setScenefrom("realsee");
  145. scene.setModelkind("dam");
  146. scene.setFloorplanangle(0);
  147. sceneService.saveOrUpdate(scene);
  148. SceneEditControlsBean sceneEditControlsBean = SceneEditControlsBean.builder()
  149. .showDollhouse(1).showMap(1).showPanorama(1).showVR(1).showTitle(1).showFloorplan(1).build();
  150. SceneJsonBean sceneJsonBean = SceneJsonBean.builder()
  151. .title(title).description(null)
  152. .num(roomId).floorLogoSize(100).sceneKind("pano")
  153. .sceneResolution("4k").sceneFrom("realsee")
  154. .modelKind("dam").floorPlanAngle(0).controls(sceneEditControlsBean).build();
  155. return sceneJsonBean;
  156. }
  157. private String getTitle(String roomId, String sourcePath){
  158. String sceneJsonPath = sourcePath + "data/scene.json";
  159. if(!FileUtil.exist(sceneJsonPath)){
  160. return roomId;
  161. }
  162. String sceneJsonStr = FileUtil.readUtf8String(sceneJsonPath);
  163. JSONObject jsonObject = JSON.parseObject(sceneJsonStr);
  164. String title = jsonObject.getString("title");
  165. return Objects.isNull(title) ? roomId : title;
  166. }
  167. public static void writeDataJson(String path){
  168. JSONObject dataJson = new JSONObject();
  169. dataJson.put("obj2txt", true);
  170. dataJson.put("split_type", "SPLIT_V6");
  171. dataJson.put("data_describe", "double spherical");
  172. dataJson.put("skybox_type", "SKYBOX_V5");
  173. FileUtil.writeUtf8String(dataJson.toString(), path + File.separator + "data.json");
  174. }
  175. public static void build3dModel(String folderName) throws Exception {
  176. System.out.println("开始建模");
  177. String command = "sudo bash /home/ubuntu/bin/Launcher.sh " + folderName;
  178. CmdUtils.callLineSh(command);
  179. System.out.println("计算完毕:" + command);
  180. }
  181. public static boolean checkComputeCompleted(String uploadJsonPath, int maxCheckTimes, long waitTime) throws Exception {
  182. int checkTimes = 1;
  183. boolean exist = false;
  184. do {
  185. if ((new File(uploadJsonPath)).exists()) {
  186. exist = true;
  187. break;
  188. }
  189. Thread.sleep(waitTime);
  190. ++checkTimes;
  191. } while(checkTimes <= maxCheckTimes);
  192. return exist;
  193. }
  194. }