123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- package com.fdkankan.modeldemo.utils;
- import cn.hutool.core.io.FileUtil;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.fdkankan.modeldemo.bean.SceneEditControlsBean;
- import com.fdkankan.modeldemo.bean.SceneJsonBean;
- import com.fdkankan.modeldemo.constant.Constant;
- import com.fdkankan.modeldemo.entity.Scene;
- import com.fdkankan.modeldemo.service.SceneService;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import java.io.File;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Objects;
- @Component
- public class ConvertUtil {
- @Autowired
- private SceneService sceneService;
- public static void main(String[] args) throws Exception {
- String srcPath = args[0];
- System.out.println(srcPath);
- String targetPath = args[1];
- System.out.println(targetPath);
- // convert(srcPath, targetPath);
- // scanner.close();
- }
- /**
- *
- * @param sourcePath 原始资源目录
- * @param targetPath 目标资源目录
- * @return boolean true-转换成功 false-转换失败
- */
- public Map<String, String> convert(String sourcePath, String num) throws Exception {
- Map<String, String> map = new HashMap<>();
- String dataViewPath = String.format(Constant.DATA_VIEW_PATH, num);
- String imgViewPath = String.format(Constant.IMG_VIEW_PATH, num);
- String obj2TxtPath = null;
- if(!sourcePath.endsWith(File.separator)){
- sourcePath = sourcePath + File.separator;
- }
- obj2TxtPath = sourcePath.substring(0, sourcePath.length() - 1) + "_obj2txt";
- FileUtil.mkdir(obj2TxtPath);
- //调用算法生成modeldata.txt
- String extrasPath = obj2TxtPath + File.separator + "extras";
- FileUtil.mkdir(extrasPath);
- FileUtil.copyContent(new File(sourcePath + "data" + File.separator + "mesh"), new File(extrasPath), true);
- //写data.json
- writeDataJson(obj2TxtPath);
- //调用算法建模
- build3dModel(obj2TxtPath);
- //校验算法是否正常结束
- String uploadJsonPath = obj2TxtPath + File.separator + "results" +File.separator+"upload.json";
- boolean success = checkComputeCompleted(uploadJsonPath, 5, 300);
- if(!success){
- throw new RuntimeException("计算失败,obj2TxtPath:" + obj2TxtPath);
- }
- String uploadData = FileUtil.readUtf8String(uploadJsonPath);
- JSONObject uploadJson = null;
- JSONArray array = null;
- if(uploadData!=null) {
- uploadJson = JSONObject.parseObject(uploadData);
- array = uploadJson.getJSONArray("upload");
- }
- JSONObject fileJson = null;
- String fileName = "";
- for(int i = 0, len = array.size(); i < len; i++) {
- fileJson = array.getJSONObject(i);
- fileName = fileJson.getString("file");
- //文件不存在抛出异常
- if (!new File(obj2TxtPath + File.separator + "results" + File.separator + fileName).exists()) {
- throw new Exception(obj2TxtPath + File.separator + "results" + File.separator + fileName + "文件不存在");
- }
- //tex文件夹
- if (fileJson.getIntValue("clazz") == 15) {
- // FileUtil.copy(obj2TxtPath + File.separator + "results" + File.separator + fileName,
- // targetImagePath + File.separator + "tieta_texture/" + fileName.replace("tex/", ""),true);
- map.put(imgViewPath + "tieta_texture/" + fileName.replace("tex/", ""), obj2TxtPath + File.separator + "results" + File.separator + fileName);
- }
- }
- //压缩成dam
- String damKey = imgViewPath + "tieta.dam";
- String damPath = obj2TxtPath + File.separator + "results" +File.separator + "tieta.dam";
- CreateObjUtil.convertTxtToDam( obj2TxtPath + File.separator + "results" +File.separator+"modeldata.txt", damPath);
- map.put(damKey, damPath);
- //复制文件
- if(FileUtil.exist(sourcePath + "data/" + "floorplan.json")){
- String floorplanJson = FileUtil.readUtf8String(sourcePath + "data/" + "floorplan.json");
- JSONObject jsonObject = JSON.parseObject(floorplanJson);
- Integer currentId = jsonObject.getInteger("currentId");
- if(Objects.nonNull(currentId) && currentId == 0){
- FileUtil.del(sourcePath + "data/" + "floorplan.json");
- }
- }
- List<File> dataFiles = FileUtil.loopFiles(sourcePath + "data/");
- for (File dataFile : dataFiles) {
- map.put(dataFile.getAbsolutePath().replace(sourcePath + "data/", dataViewPath) , dataFile.getAbsolutePath());
- }
- String finalSourcePath = sourcePath;
- if(FileUtil.exist(finalSourcePath + "images/4k/")){
- FileUtil.listFileNames(sourcePath + "images/4k/").stream().forEach(v->{
- map.put(imgViewPath + "pan/high/" + v.replaceAll("-", ""), finalSourcePath + "images/4k/" + v);
- // FileUtil.copy(finalSourcePath + "images/4k/" + v, targetImagePath + "/pan/high/" + v.replaceAll("-", ""), true);
- });
- }
- if(FileUtil.exist(finalSourcePath + "images/512/")){
- FileUtil.listFileNames(finalSourcePath + "images/512/").stream().forEach(v->{
- map.put(imgViewPath + "pan/low/" + v.replaceAll("-", ""), finalSourcePath + "images/512/" + v);
- // FileUtil.copy(finalSourcePath + "images/512/" + v, targetImagePath + "/pan/low/" + v.replaceAll("-", ""), true);
- });
- }
- if(FileUtil.exist(finalSourcePath + "images/8k/")) {
- FileUtil.listFileNames(finalSourcePath + "images/8k/").stream().forEach(v -> {
- map.put(imgViewPath + "pan/8k/" + v.replaceAll("-", ""), finalSourcePath + "images/8k/" + v);
- // FileUtil.copy(finalSourcePath + "images/8k/" + v, targetImagePath + "/pan/8k/" + v.replaceAll("-", ""), true);
- });
- }
- map.put(imgViewPath + "vision.txt", sourcePath + "images/vision.txt");
- // FileUtil.copy(sourcePath + "images/vision.txt", targetImagePath + "/vision.txt", true);
- //生成vison.modeldata
- CreateObjUtil.convertTxtToVisionmodeldata(sourcePath + "images/vision.txt", sourcePath + "images/vision.modeldata");
- map.put(imgViewPath + "vision.modeldata", sourcePath + "images/vision.modeldata");
- //生成场景标题
- String title = this.getTitle(num, sourcePath);
- //生成scene.json
- SceneJsonBean sceneJsonBean = this.genSceneJson(num, title);
- FileUtil.writeUtf8String(JSON.toJSONString(sceneJsonBean), sourcePath + "data/scene.json");
- map.put(dataViewPath + "scene.json", sourcePath + "data/scene.json");
- return map;
- }
- private SceneJsonBean genSceneJson(String roomId, String title){
- Scene scene = sceneService.getByNum(roomId);
- if(Objects.isNull(scene)){
- scene = new Scene();
- }
- scene.setTitle(title);
- scene.setNum(roomId);
- scene.setFloorlogosize(100);
- scene.setScenekind("pano");
- scene.setSceneresolution("4k");
- scene.setScenefrom("realsee");
- scene.setModelkind("dam");
- scene.setFloorplanangle(0);
- sceneService.saveOrUpdate(scene);
- SceneEditControlsBean sceneEditControlsBean = SceneEditControlsBean.builder()
- .showDollhouse(1).showMap(1).showPanorama(1).showVR(1).showTitle(1).showFloorplan(1).build();
- SceneJsonBean sceneJsonBean = SceneJsonBean.builder()
- .title(title).description(null)
- .num(roomId).floorLogoSize(100).sceneKind("pano")
- .sceneResolution("4k").sceneFrom("realsee")
- .modelKind("dam").floorPlanAngle(0).controls(sceneEditControlsBean).build();
- return sceneJsonBean;
- }
- private String getTitle(String roomId, String sourcePath){
- String sceneJsonPath = sourcePath + "data/scene.json";
- if(!FileUtil.exist(sceneJsonPath)){
- return roomId;
- }
- String sceneJsonStr = FileUtil.readUtf8String(sceneJsonPath);
- JSONObject jsonObject = JSON.parseObject(sceneJsonStr);
- String title = jsonObject.getString("title");
- return Objects.isNull(title) ? roomId : title;
- }
- public static void writeDataJson(String path){
- JSONObject dataJson = new JSONObject();
- dataJson.put("obj2txt", true);
- dataJson.put("split_type", "SPLIT_V6");
- dataJson.put("data_describe", "double spherical");
- dataJson.put("skybox_type", "SKYBOX_V5");
- FileUtil.writeUtf8String(dataJson.toString(), path + File.separator + "data.json");
- }
- public static void build3dModel(String folderName) throws Exception {
- System.out.println("开始建模");
- String command = "sudo bash /home/ubuntu/bin/Launcher.sh " + folderName;
- CmdUtils.callLineSh(command);
- System.out.println("计算完毕:" + command);
- }
- public static boolean checkComputeCompleted(String uploadJsonPath, int maxCheckTimes, long waitTime) throws Exception {
- int checkTimes = 1;
- boolean exist = false;
- do {
- if ((new File(uploadJsonPath)).exists()) {
- exist = true;
- break;
- }
- Thread.sleep(waitTime);
- ++checkTimes;
- } while(checkTimes <= maxCheckTimes);
- return exist;
- }
- }
|