package com.fdkankan.fusion.common.util; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.file.FileReader; import com.alibaba.fastjson.JSONObject; import com.fdkankan.fusion.common.FilePath; import com.fdkankan.fusion.common.ResultCode; import com.fdkankan.fusion.exception.BusinessException; import lombok.extern.slf4j.Slf4j; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.Date; import java.util.LinkedHashSet; @Slf4j public class OBJToGLBUtil { public static String objToGlb(String objPath, String glbPath) { log.info("obj转换glb开始,{}",objPath); if(!checkObj(objPath)){ throw new BusinessException(-1,"obj文件错误"); } log.info("obj转换glb开始"); String command = String.format(ShellCmd.obj2glb_cmd,objPath,glbPath); log.info("执行obj转换glb命令路径-{}", command); ShellUtil.execCmd(command); log.info("obj转换glb完毕:" + command); return glbPath; } public static void objToGlb2(String objPath,String glbPath) { log.info("obj转换glb开始,{}",objPath); log.info("obj转换glb开始"); String command = String.format(ShellCmd.obj2glb_cmd,objPath,glbPath); log.info("执行obj转换glb命令路径-{}", command); ShellUtil.execCmd(command); log.info("obj转换glb完毕:" + command); } public static String objToB3dm(String objPath, String glbPath) { Integer lodNum = getLodNum(objPath); log.info("obj转换b3dm开始,{}",objPath); log.info("obj转换b3dm开始"); //String command = "Obj2Tiles --lods "+lodNum+" --divisions 3 " + objPath + " " + glbPath; String command = "Obj2Tiles " + objPath + " " + glbPath; log.info("执行obj转换glb命令路径-{}", command); ShellUtil.execCmd(command); log.info("obj转换b3dm完毕:" + command); return glbPath; } private static Integer getLodNum(String objPath) { // long length = new File(objPath).length(); // long mb = length / 1024 /1024; // if(mb >100){ // return (mb /10) >10 ?10:Integer.parseInt(String.valueOf(mb /10)); // } return 3; } public static boolean checkObj(String objPath) { File file = new File(objPath); File file1 = file.getParentFile(); File[] files = file1.listFiles(); if(files == null || files.length <=0){ throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST); } File mtlFile = null; File objFile = null; for (File file2 : files) { if(file2.isDirectory()){ return checkObj(file2.getPath()); } if(file2.getName().contains(".obj") ){ objFile = file2; } if(file2.getName().contains(".mtl") ){ mtlFile = file2; } } if(mtlFile == null || objFile == null ){ throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR); } return checkMtl(file1,mtlFile); } private static boolean checkMtl(File allFile,File file) { if(!file.getName().contains("mtl")){ throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR); } LinkedHashSet imgName = new LinkedHashSet<>(); if(allFile == null ){ throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR); } File[] files = allFile.listFiles(); if(files == null || files.length<=0 ){ throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR); } for (File listFile : files) { String modelName = listFile.getName(); imgName.add(modelName); } LinkedHashSet imgMtl = readMtlFile(file.getPath()); for (String mtlName : imgMtl) { if(!imgName.contains(mtlName)){ throw new BusinessException(ResultCode.UPLOAD_FILE_MSG_ERROR.code,mtlName +".jpg 图片缺失!"); } } updateMtlFile(file); return true; } private static void updateMtlFile(File file) { String mtlStr = FileUtil.readUtf8String(file); String[] split = mtlStr.split("\n"); StringBuilder newMtlStr = new StringBuilder(); for (String s : split) { if(s.toLowerCase().contains("tr")){ String[] split1 = s.split(" "); if(split1.length <=1){ continue; } System.out.println(split1[1]); System.out.println(isNumeric2(split1[1])); if(isNumeric2(split1[1])){ String[] split2 = split1[1].split("\\."); int number = Integer.parseInt(split2[0]); if(number >=1){ number = 1 - number; } String numStr = number + (split2.length <=1 ?"": "."+split2[1]); s = s.replace(split1[1],numStr); } } newMtlStr.append(s).append("\n"); } FileUtil.copyContent(file,new File(file.getPath()+new Date().getTime()+"back"),true); FileUtil.writeString(newMtlStr.toString(),file, StandardCharsets.UTF_8); } public static boolean isNumeric2(String str) { try { double i = Double.valueOf(str); return true; }catch (Exception e){ e.printStackTrace(); } return false; } public static LinkedHashSet readMtlFile(String mtlPath) { LinkedHashSet imgName = new LinkedHashSet<>(); FileInputStream fis = null; BufferedReader br = null; try { fis = new FileInputStream(new File(mtlPath)); br = new BufferedReader(new InputStreamReader(fis)); String line = null; while ((line = br.readLine()) != null) { String[] tempsa = line.split("[ ]+"); if (tempsa[0].trim().equals("map_Ka")) { String mtlName = tempsa[1]; if(mtlName.contains("/")){ String[] split = mtlName.split("/"); mtlName = split[split.length-1]; } imgName.add(mtlName); } if (tempsa[0].trim().equals("map_Kd")) { String mtlName = tempsa[1]; if(mtlName.contains("/")){ String[] split = mtlName.split("/"); mtlName = split[split.length-1]; } imgName.add(mtlName); } } } catch (Exception e) { e.printStackTrace(); } return imgName; } public static File lasOrPlyToBin(File srcFile){ if(!srcFile.exists()){ srcFile.mkdirs(); } String cmd = String.format(ShellCmd.LAS_TO_BIN,srcFile.getPath(),srcFile.getParentFile().getPath()); ShellUtil.execCmd(cmd); log.info("lasOrPlyToBin---------cmd-over"); String cloudJs = srcFile.getParentFile().getPath() +"/webcloud/"+ "cloud.js"; JSONObject jsonObject = fixCloud(cloudJs); FileWriterUtil.writerJson(srcFile.getParentFile().getPath() +"/webcloud/","cloud.js",jsonObject.toJSONString()); return srcFile.getParentFile(); } public static JSONObject fixCloud(String path) { cn.hutool.core.io.file.FileReader fileReader = new FileReader(path); String str = fileReader.readString(); JSONObject json = JSONObject.parseObject(str); String[] pointAttributes = { "POSITION_CARTESIAN", "COLOR_PACKED", "NORMAL_OCT16" }; json.put("pointAttributes", pointAttributes); return json; } public static String OsgbToB3dm(File objPathFile) { String targetPath = FilePath.MNT_BASE_PATH +"osgb" +"/" + objPathFile.getName(); String sourcePath = FilePath.MNT_BASE_PATH +"b3dm" +"/" + objPathFile.getName(); //ShellUtil.execCmd("cp -r " + objPath + " " + dockerPath+ objPathFile.getName() ); File file = new File(targetPath); FileUtil.copyContent(objPathFile,file,true); String cmd = String.format(ShellCmd.osgbTob3dmCmd,targetPath,sourcePath); ShellUtil.execCmd(cmd); return sourcePath; } public static String OsgbToB3dm(String targetPath,String sourcePath) { String cmd = String.format(ShellCmd.osgbTob3dmCmd,targetPath,sourcePath); ShellUtil.execCmd(cmd); return sourcePath; } }