123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- package com.fdkankan.fusion.common.util;
- import cn.hutool.core.io.FileUtil;
- 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 org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
- import java.io.*;
- 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 = "obj2gltf -i " + objPath + " -o " + 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 = "obj2gltf -i " + objPath + " -o " + 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")){
- return false;
- }
- LinkedHashSet<String> imgName = new LinkedHashSet<>();
- if(allFile == null || allFile.length()<=0 ){
- return false;
- }
- File[] files = allFile.listFiles();
- if(files == null || files.length<=0 ){
- return false;
- }
- for (File listFile : files) {
- String modelName = listFile.getName();
- imgName.add(modelName);
- }
- LinkedHashSet<String> imgMtl = readMtlFile(file.getPath());
- for (String mtlName : imgMtl) {
- if(!imgName.contains(mtlName)){
- throw new BusinessException(-1,mtlName +".jpg 图片缺失!");
- }
- }
- return true;
- }
- public static LinkedHashSet<String> readMtlFile(String mtlPath) {
- LinkedHashSet<String> 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) throws IOException {
- if(!srcFile.exists()){
- srcFile.mkdirs();
- }
- String cmd = ShellUtil.LAS_TO_BIN;
- cmd = cmd.replace("@inPath",srcFile.getPath());
- cmd = cmd.replace("@outPath",srcFile.getParentFile().getPath());
- ShellUtil.execCmd(cmd);
- log.info("lasOrPlyToBin---------cmd-over");
- String cloudJs = srcFile.getParentFile().getPath() +"/webcloud/"+ "cloud.js";
- JSONObject jsonObject = ShellUtil.fixCloud(cloudJs);
- FileWriterUtil.writerJson(srcFile.getParentFile().getPath() +"/webcloud/","cloud.js",jsonObject.toJSONString());
- return srcFile.getParentFile();
- }
- 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 = ShellCmd.osgbTob3dmCmd.replaceAll("@path",FilePath.MNT_BASE_PATH);
- cmd =cmd.replaceAll("@inputFile",targetPath);
- cmd =cmd.replaceAll("@outputFile",sourcePath);
- ShellUtil.execCmd(cmd);
- return sourcePath;
- }
- }
|