123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- 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 com.fdkankan.geo.GeoQueryUtil;
- import com.fdkankan.geo.GeoTransformUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.locationtech.proj4j.ProjCoordinate;
- import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
- 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 = "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")){
- throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
- }
- LinkedHashSet<String> 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<String> 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;
- }
- 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) {
- return str != null && str.matches("-?\\d+(\\.\\d+)?");
- }
- 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;
- }
- public static String OsgbToB3dm(String targetPath,String sourcePath) {
- 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;
- }
- }
|