|
@@ -1,12 +1,15 @@
|
|
|
package com.css.simulation.resource.scheduler.service;
|
|
|
|
|
|
-import api.common.pojo.param.model.VehicleParam;
|
|
|
import api.common.pojo.po.scene.VehicleTypePO;
|
|
|
import api.common.pojo.vo.model.VehicleVO;
|
|
|
+import api.common.util.FileUtil;
|
|
|
import api.common.util.LinuxUtil;
|
|
|
import com.css.simulation.resource.scheduler.mapper.ConfigMapper;
|
|
|
import com.css.simulation.resource.scheduler.mapper.SimulationAutomaticProjectMapper;
|
|
|
import com.css.simulation.resource.scheduler.mapper.VehicleMapper;
|
|
|
+import com.css.simulation.resource.scheduler.util.MinioUtil;
|
|
|
+import io.minio.MinioClient;
|
|
|
+import lombok.NoArgsConstructor;
|
|
|
import lombok.SneakyThrows;
|
|
|
import org.dom4j.Attribute;
|
|
|
import org.dom4j.Document;
|
|
@@ -14,153 +17,113 @@ import org.dom4j.Element;
|
|
|
import org.dom4j.io.OutputFormat;
|
|
|
import org.dom4j.io.SAXReader;
|
|
|
import org.dom4j.io.XMLWriter;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.jvnet.hk2.annotations.Service;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
import java.io.OutputStreamWriter;
|
|
|
import java.io.Writer;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Paths;
|
|
|
import java.util.Iterator;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
-public class GenerateVideoService {
|
|
|
- @Autowired
|
|
|
+@Service
|
|
|
+@NoArgsConstructor
|
|
|
+public class VideoService {
|
|
|
+ @Resource
|
|
|
VehicleMapper vehicleMapper;
|
|
|
- @Autowired
|
|
|
+ @Resource
|
|
|
ConfigMapper configMapper;
|
|
|
@Resource
|
|
|
SimulationAutomaticProjectMapper simulationAutomaticProjectMapper;
|
|
|
- @SneakyThrows
|
|
|
- //map必传 xoscPath 文件夹路径 ;osgbPath,xodrPath 全路径;projectType 项目类型 1.手动;2.自动 ;id项目id;
|
|
|
- public void GenerateVideo( Map<String ,String> map) throws IOException {
|
|
|
- String xoscPath=map.get("xoscPath");
|
|
|
- LinuxUtil.execute("D:\\Users\\anaconda3\\envs\\flaskProject\\python.exe C:/Users/CSS/Desktop/simulation/gqSw/simulation_my.py "+xoscPath);
|
|
|
- //xosc全路径
|
|
|
- xoscPath= xoscPath+"\\xosc\\simulation_my0.xosc";
|
|
|
- map.put("xoscPath",xoscPath);
|
|
|
- //调用修改xosc
|
|
|
- String xoscNewPath= changeXos(map);//返回最新的xosc文件全路径
|
|
|
+ @Resource
|
|
|
+ MinioClient minioClient;
|
|
|
|
|
|
- }
|
|
|
|
|
|
- //获取三维自车对象
|
|
|
- @SneakyThrows
|
|
|
- public VehicleTypePO vehicleById(Map<String,String> map) {
|
|
|
- VehicleTypePO po=new VehicleTypePO();
|
|
|
- String projectType=map.get("projectType");
|
|
|
- String id=map.get("id");
|
|
|
- //项目类型 1.手动;2.自动
|
|
|
- String vehicle="";
|
|
|
- if(projectType.equals("1"))
|
|
|
- {
|
|
|
- vehicle = simulationAutomaticProjectMapper.vehicleBySdId(id);
|
|
|
- }else if (projectType.equals("2")){
|
|
|
- vehicle = simulationAutomaticProjectMapper.vehicleByZdId(id);
|
|
|
- }
|
|
|
- String vehicleId=configMapper.getVehicleId(vehicle);
|
|
|
-
|
|
|
- VehicleVO vehicleInfo = vehicleMapper.getVehicleInfo(vehicleId);
|
|
|
+ //* -------------------------------- Comment --------------------------------
|
|
|
|
|
|
- //自车模型
|
|
|
- String car= vehicleInfo.getVehicleTypeStr().substring(0,vehicleInfo.getVehicleTypeStr().indexOf(","));
|
|
|
- car=car.substring(0,1).toLowerCase()+car.substring(1);
|
|
|
- if(car.indexOf("truck")>-1){
|
|
|
- po.setVehicleCategory("truck");
|
|
|
- }else if(car.indexOf("trailer")>-1){
|
|
|
- po.setVehicleCategory("trailer");
|
|
|
- }else if(car.indexOf("van")>-1){
|
|
|
- po.setVehicleCategory("van");
|
|
|
- }else if(car.indexOf("semitrailer")>-1){
|
|
|
- po.setVehicleCategory("semitrailer");
|
|
|
- }else if(car.indexOf("bus")>-1){
|
|
|
- po.setVehicleCategory("bus");
|
|
|
- }else if(car.indexOf("motorbike")>-1){
|
|
|
- po.setVehicleCategory("motorbike");
|
|
|
- }else if(car.indexOf("bicycle")>-1){
|
|
|
- po.setVehicleCategory("bicycle");
|
|
|
- }else if(car.indexOf("bicycle")>-1){
|
|
|
- po.setVehicleCategory("bicycle");
|
|
|
- }else if(car.indexOf("train")>-1){
|
|
|
- po.setVehicleCategory("train");
|
|
|
- }else if(car.indexOf("tram")>-1){
|
|
|
- po.setVehicleCategory("tram");
|
|
|
- }else{
|
|
|
- po.setVehicleCategory("car");
|
|
|
- }
|
|
|
+ public static final String oldXoscName = "simulation_my0.xosc";
|
|
|
+ public static final String newXoscName = "simulation_my05.xosc";
|
|
|
+ public static final String oldXoscRelativePath = "xosc/simulation_my0.xosc";
|
|
|
+ public static final String[] csvNameArray = {"Ego.csv", "evaluation.csv"};
|
|
|
|
|
|
- //车前距
|
|
|
- BigDecimal frontDistance=vehicleInfo.getFrontDistance();
|
|
|
- //车后距
|
|
|
- BigDecimal mo2=new BigDecimal("2");
|
|
|
- BigDecimal rearDistance=vehicleInfo.getRearDistance();
|
|
|
- if(frontDistance.compareTo(rearDistance)>0){
|
|
|
- BigDecimal CenterX= (frontDistance.add(rearDistance)).divide(mo2).subtract(rearDistance);
|
|
|
- po.setCenterX(CenterX);
|
|
|
- }else{
|
|
|
- BigDecimal CenterX= (frontDistance.add(rearDistance)).divide(mo2).subtract(frontDistance);
|
|
|
- po.setCenterX(CenterX);
|
|
|
- }
|
|
|
- //车高
|
|
|
- BigDecimal mo3=new BigDecimal("3");
|
|
|
- po.setCenterZ(vehicleInfo.getHeightDistance().divide(mo3));
|
|
|
- //车左距 车右距
|
|
|
- BigDecimal LeftDistance=vehicleInfo.getLeftDistance();
|
|
|
- BigDecimal RightDistance= vehicleInfo.getRightDistance();
|
|
|
- po.setDimensionsWidth(LeftDistance.add(RightDistance));
|
|
|
- po.setDimensionsHeight(vehicleInfo.getHeightDistance());
|
|
|
- po.setDimensionsLength(frontDistance.add(rearDistance));
|
|
|
+ //* -------------------------------- Comment --------------------------------
|
|
|
+ @Value("${scheduler.linux-path.temp}")
|
|
|
+ String linuxTempPath;
|
|
|
+ @Value("${scheduler.minio-path.project-result}")
|
|
|
+ String projectResultPathOfMinio;
|
|
|
+ @Value("${minio.bucket-name}")
|
|
|
+ String bucketName;
|
|
|
|
|
|
- po.setPerformanceMaxSpeed(vehicleInfo.getMaxSpeed());
|
|
|
- po.setPerformanceMaxDeceleration(vehicleInfo.getMaxDeceleration());
|
|
|
- BigDecimal moπ=new BigDecimal("3.141516");
|
|
|
- BigDecimal mo180=new BigDecimal("180");
|
|
|
- po.setFrontAxleMaxSteering(vehicleInfo.getMaxSteeringAngle().multiply(moπ).divide(mo180));
|
|
|
- po.setFrontAxleWheelDiameter(vehicleInfo.getWheelDiameter());
|
|
|
- BigDecimal mo4=new BigDecimal("4");
|
|
|
- BigDecimal mo5=new BigDecimal("5");
|
|
|
- po.setFrontAxleTrackWidth(LeftDistance.add(RightDistance).multiply(mo4).divide(mo5));
|
|
|
- po.setFrontAxlePositionX(po.getCenterX());
|
|
|
- po.setFrontAxlePositionZ(po.getCenterZ());
|
|
|
|
|
|
- po.setRearAxleWheelDiameter(vehicleInfo.getWheelDiameter());
|
|
|
- po.setRearAxleTrackWidth(po.getFrontAxleTrackWidth());
|
|
|
- po.setRearAxlePositionX(po.getFrontAxlePositionX());
|
|
|
- po.setRearAxlePositionZ(po.getFrontAxlePositionZ());
|
|
|
- return po;
|
|
|
+ /**
|
|
|
+ * 生成视频
|
|
|
+ */
|
|
|
+ @SneakyThrows
|
|
|
+ public void generateVideo(String projectId, String projectType, String taskId) {
|
|
|
+ //1 获取两个 csv 的目录,和 xodr 和 osgb 三个路径
|
|
|
+ String csvDirectoryPath = linuxTempPath + "video/" + projectId + "/" + taskId + "/";
|
|
|
+ String xodrPath = FileUtil.listAbsolutePathByFiletype(csvDirectoryPath, ".xodr").get(0); // xodr 文件在创建项目时已经下载好了
|
|
|
+ String osgbPath = FileUtil.listAbsolutePathByFiletype(csvDirectoryPath, ".osgb").get(0); // osgb 文件在创建项目时已经下载好了
|
|
|
+ //2 下载 csv 文件
|
|
|
+ for (String csvName : csvNameArray) {
|
|
|
+ MinioUtil.downloadToFile(minioClient, bucketName, projectResultPathOfMinio + projectId + "/" + taskId + "/" + csvName, csvDirectoryPath + "/" + csvName);
|
|
|
+ }
|
|
|
+ //3 生成 xosc 文件
|
|
|
+ String xoscPath = generateXosc(csvDirectoryPath, xodrPath, osgbPath, projectId, projectType);
|
|
|
+ //4 生成图片
|
|
|
+ String pictureDirectoryPath = csvDirectoryPath + "picture";
|
|
|
+ String esminiCommand = "" + pictureDirectoryPath;
|
|
|
+ String esminiResult = LinuxUtil.execute(esminiCommand);
|
|
|
+ //5 生成视频
|
|
|
+ String videoName = "";
|
|
|
+ String videoTargetPathOfLinux = csvDirectoryPath + "video";
|
|
|
+ String videoTargetPathOfMinio = projectResultPathOfMinio + projectId + "/" + taskId + "/" + videoName;
|
|
|
+ String ffmpegCommand = " " + pictureDirectoryPath + " " + videoTargetPathOfLinux;
|
|
|
+ String ffmpegResult = LinuxUtil.execute(ffmpegCommand);
|
|
|
+ //6 将视频上传到 minio
|
|
|
+ MinioUtil.uploadFromFile(minioClient, videoTargetPathOfLinux, bucketName, videoTargetPathOfMinio);
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * Xosc
|
|
|
+ */
|
|
|
+ @SneakyThrows
|
|
|
+ private String generateXosc(String csvDirectoryPath, String xodrPath, String osgbPath, String projectId, String projectType) {
|
|
|
+ //1 生成 xosc 文件
|
|
|
+ String command = "D:\\Users\\anaconda3\\envs\\flaskProject\\python.exe C:/Users/CSS/Desktop/simulation/gqSw/simulation_my.py " + csvDirectoryPath;
|
|
|
+ String execute = LinuxUtil.execute(command); // 执行命令后生成 xosc 到目录下
|
|
|
+ String oldXoscPath = csvDirectoryPath + oldXoscRelativePath; // 生成文件的路径是固定的
|
|
|
+ //2 调用修改xosc
|
|
|
+ return modifyXosc(oldXoscPath, xodrPath, osgbPath, projectId, projectType); // 返回最新的xosc文件全路径
|
|
|
}
|
|
|
|
|
|
|
|
|
- //转换xosc自车形态
|
|
|
+ /**
|
|
|
+ * 修改 xosc 文件
|
|
|
+ */
|
|
|
@SneakyThrows
|
|
|
- public String changeXos(Map<String, String> map){
|
|
|
-
|
|
|
- String xoscPath=map.get("xoscPath");
|
|
|
- String osgbPath=map.get("osgbPath");
|
|
|
- String xodrPath=map.get("xodrPath");
|
|
|
- String xoscNewPath= xoscPath.substring(0,xoscPath.length()-5)+"5.xosc";//
|
|
|
+ private String modifyXosc(String oldXoscPath, String xodrPath, String osgbPath, String projectId, String projectType) {
|
|
|
+ String newXoscPath = oldXoscPath.replace(oldXoscName, newXoscName);
|
|
|
//获取三维自车对象
|
|
|
- VehicleTypePO po =vehicleById(map);
|
|
|
+ VehicleTypePO po = vehicleById(projectId, projectType);
|
|
|
|
|
|
//1 创建Reader对象
|
|
|
SAXReader reader = new SAXReader();
|
|
|
//2 加载xml
|
|
|
// Document document = reader.read(new File("D:\\simulation_my0.xosc"));
|
|
|
- Document document = reader.read(new File(xoscPath));
|
|
|
+ Document document = reader.read(new File(oldXoscPath));
|
|
|
//3 获取根节点
|
|
|
Element root = document.getRootElement();
|
|
|
- Iterator iterator0 = root.elementIterator();
|
|
|
+ Iterator<Element> iterator0 = root.elementIterator();
|
|
|
while (iterator0.hasNext()) { // 第一层遍历
|
|
|
- Element node1 = (Element) iterator0.next();
|
|
|
+ Element node1 = iterator0.next();
|
|
|
if ("Entities".equals(node1.getName())) { // 找到第一层节点后进入第二层遍历
|
|
|
- Iterator iterator1 = node1.elementIterator();
|
|
|
+ Iterator<Element> iterator1 = node1.elementIterator();
|
|
|
while (iterator1.hasNext()) {
|
|
|
- Element node2 = (Element) iterator1.next();
|
|
|
+ Element node2 = iterator1.next();
|
|
|
// 添加节点
|
|
|
if ("ScenarioObject".equals(node2.getName())) {
|
|
|
String scenarioObjectName = node2.attribute("name").getValue();
|
|
@@ -178,12 +141,12 @@ public class GenerateVideoService {
|
|
|
Element node4 = iterator4.next();
|
|
|
if ("Performance".equals(node4.getName())) {
|
|
|
Attribute maxSpeed = node4.attribute("maxSpeed");
|
|
|
- maxSpeed.setText(po.getPerformanceMaxSpeed()+"");
|
|
|
+ maxSpeed.setText(po.getPerformanceMaxSpeed() + "");
|
|
|
Attribute maxDeceleration = node4.attribute("maxDeceleration");
|
|
|
- maxDeceleration.setText(po.getPerformanceMaxDeceleration()+"");
|
|
|
+ maxDeceleration.setText(po.getPerformanceMaxDeceleration() + "");
|
|
|
Attribute maxAcceleration = node4.attribute("maxAcceleration");
|
|
|
maxAcceleration.setText(po.getPerformanceMaxAcceleration());
|
|
|
- }else if("BoundingBox".equals(node4.getName())){
|
|
|
+ } else if ("BoundingBox".equals(node4.getName())) {
|
|
|
Iterator<Element> iterator5 = node4.elementIterator();
|
|
|
while (iterator5.hasNext()) {
|
|
|
Element node5 = iterator5.next();
|
|
@@ -204,7 +167,7 @@ public class GenerateVideoService {
|
|
|
height.setText(po.getDimensionsHeight() + "");
|
|
|
}
|
|
|
}
|
|
|
- }else if("Axles".equals(node4.getName())){
|
|
|
+ } else if ("Axles".equals(node4.getName())) {
|
|
|
Iterator<Element> iterator5 = node4.elementIterator();
|
|
|
while (iterator5.hasNext()) {
|
|
|
Element node5 = iterator5.next();
|
|
@@ -249,9 +212,9 @@ public class GenerateVideoService {
|
|
|
}
|
|
|
|
|
|
if ("RoadNetwork".equals(node1.getName())) { // 找到第一层节点后进入第二层遍历
|
|
|
- Iterator iterator1 = node1.elementIterator();
|
|
|
+ Iterator<Element> iterator1 = node1.elementIterator();
|
|
|
while (iterator1.hasNext()) {
|
|
|
- Element node2 = (Element) iterator1.next();
|
|
|
+ Element node2 = iterator1.next();
|
|
|
// 添加节点
|
|
|
if ("LogicFile".equals(node2.getName())) {
|
|
|
Attribute name = node2.attribute("filepath");
|
|
@@ -264,7 +227,7 @@ public class GenerateVideoService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- Writer osWrite = new OutputStreamWriter(Files.newOutputStream(Paths.get(xoscNewPath)));
|
|
|
+ Writer osWrite = new OutputStreamWriter(Files.newOutputStream(Paths.get(newXoscPath)));
|
|
|
OutputFormat format = OutputFormat.createPrettyPrint(); // 获取输出的指定格式
|
|
|
format.setEncoding("UTF-8");// 设置编码 ,确保解析的xml为UTF-8格式
|
|
|
XMLWriter writer = new XMLWriter(osWrite, format);// XMLWriter
|
|
@@ -272,6 +235,93 @@ public class GenerateVideoService {
|
|
|
writer.flush();
|
|
|
writer.close();
|
|
|
}
|
|
|
- return xoscNewPath;
|
|
|
+ return newXoscPath;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ //获取三维自车对象
|
|
|
+ @SneakyThrows
|
|
|
+ private VehicleTypePO vehicleById(String projectId, String projectType) {
|
|
|
+ VehicleTypePO po = new VehicleTypePO();
|
|
|
+ //项目类型 1.手动;2.自动
|
|
|
+ String vehicle = "";
|
|
|
+ if (projectType.equals("1")) {
|
|
|
+ vehicle = simulationAutomaticProjectMapper.vehicleBySdId(projectId);
|
|
|
+ } else if (projectType.equals("2")) {
|
|
|
+ vehicle = simulationAutomaticProjectMapper.vehicleByZdId(projectId);
|
|
|
+ }
|
|
|
+ String vehicleId = configMapper.getVehicleId(vehicle);
|
|
|
+
|
|
|
+ VehicleVO vehicleInfo = vehicleMapper.getVehicleInfo(vehicleId);
|
|
|
+
|
|
|
+ //自车模型
|
|
|
+ String car = vehicleInfo.getVehicleTypeStr().substring(0, vehicleInfo.getVehicleTypeStr().indexOf(","));
|
|
|
+ car = car.substring(0, 1).toLowerCase() + car.substring(1);
|
|
|
+ if (car.contains("truck")) {
|
|
|
+ po.setVehicleCategory("truck");
|
|
|
+ } else if (car.contains("trailer")) {
|
|
|
+ po.setVehicleCategory("trailer");
|
|
|
+ } else if (car.contains("van")) {
|
|
|
+ po.setVehicleCategory("van");
|
|
|
+ } else if (car.contains("semitrailer")) {
|
|
|
+ po.setVehicleCategory("semitrailer");
|
|
|
+ } else if (car.contains("bus")) {
|
|
|
+ po.setVehicleCategory("bus");
|
|
|
+ } else if (car.contains("motorbike")) {
|
|
|
+ po.setVehicleCategory("motorbike");
|
|
|
+ } else if (car.contains("bicycle")) {
|
|
|
+ po.setVehicleCategory("bicycle");
|
|
|
+ } else if (car.contains("bicycle")) {
|
|
|
+ po.setVehicleCategory("bicycle");
|
|
|
+ } else if (car.contains("train")) {
|
|
|
+ po.setVehicleCategory("train");
|
|
|
+ } else if (car.contains("tram")) {
|
|
|
+ po.setVehicleCategory("tram");
|
|
|
+ } else {
|
|
|
+ po.setVehicleCategory("car");
|
|
|
+ }
|
|
|
+
|
|
|
+ //车前距
|
|
|
+ BigDecimal frontDistance = vehicleInfo.getFrontDistance();
|
|
|
+ //车后距
|
|
|
+ BigDecimal mo2 = new BigDecimal("2");
|
|
|
+ BigDecimal rearDistance = vehicleInfo.getRearDistance();
|
|
|
+ if (frontDistance.compareTo(rearDistance) > 0) {
|
|
|
+ BigDecimal CenterX = (frontDistance.add(rearDistance)).divide(mo2).subtract(rearDistance);
|
|
|
+ po.setCenterX(CenterX);
|
|
|
+ } else {
|
|
|
+ BigDecimal CenterX = (frontDistance.add(rearDistance)).divide(mo2).subtract(frontDistance);
|
|
|
+ po.setCenterX(CenterX);
|
|
|
+ }
|
|
|
+ //车高
|
|
|
+ BigDecimal mo3 = new BigDecimal("3");
|
|
|
+ po.setCenterZ(vehicleInfo.getHeightDistance().divide(mo3));
|
|
|
+ //车左距 车右距
|
|
|
+ BigDecimal LeftDistance = vehicleInfo.getLeftDistance();
|
|
|
+ BigDecimal RightDistance = vehicleInfo.getRightDistance();
|
|
|
+ po.setDimensionsWidth(LeftDistance.add(RightDistance));
|
|
|
+ po.setDimensionsHeight(vehicleInfo.getHeightDistance());
|
|
|
+ po.setDimensionsLength(frontDistance.add(rearDistance));
|
|
|
+
|
|
|
+ po.setPerformanceMaxSpeed(vehicleInfo.getMaxSpeed());
|
|
|
+ po.setPerformanceMaxDeceleration(vehicleInfo.getMaxDeceleration());
|
|
|
+ BigDecimal moπ = new BigDecimal("3.141516");
|
|
|
+ BigDecimal mo180 = new BigDecimal("180");
|
|
|
+ po.setFrontAxleMaxSteering(vehicleInfo.getMaxSteeringAngle().multiply(moπ).divide(mo180));
|
|
|
+ po.setFrontAxleWheelDiameter(vehicleInfo.getWheelDiameter());
|
|
|
+ BigDecimal mo4 = new BigDecimal("4");
|
|
|
+ BigDecimal mo5 = new BigDecimal("5");
|
|
|
+ po.setFrontAxleTrackWidth(LeftDistance.add(RightDistance).multiply(mo4).divide(mo5));
|
|
|
+ po.setFrontAxlePositionX(po.getCenterX());
|
|
|
+ po.setFrontAxlePositionZ(po.getCenterZ());
|
|
|
+
|
|
|
+ po.setRearAxleWheelDiameter(vehicleInfo.getWheelDiameter());
|
|
|
+ po.setRearAxleTrackWidth(po.getFrontAxleTrackWidth());
|
|
|
+ po.setRearAxlePositionX(po.getFrontAxlePositionX());
|
|
|
+ po.setRearAxlePositionZ(po.getFrontAxlePositionZ());
|
|
|
+ return po;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|