Merge branch 'master' into dev_MogoAP_eagle-220_211207_8.0.17

# Conflicts:
#	gradle.properties
#	libraries/map-custom/src/main/java/com/mogo/map/impl/custom/AMapWrapper.java
#	libraries/map-custom/src/main/java/com/mogo/map/impl/custom/CustomMapApiBuilder.java
#	modules/mogo-module-common/src/main/java/com/mogo/module/common/drawer/IdentifyDataDrawer.java
This commit is contained in:
donghongyu
2022-01-14 14:49:29 +08:00
19 changed files with 359 additions and 554 deletions

View File

@@ -1,35 +1,27 @@
package com.mogo.module.common.drawer;
import static com.mogo.cloud.socket.entity.SocketDownDataHelper.FROM_ADAS;
import static java.lang.Math.PI;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mogo.commons.AbsMogoApplication;
import com.mogo.eagle.core.utilcode.mogo.thread.WorkThreadHandler;
import com.mogo.map.marker.IMogoMarker;
import com.mogo.module.common.MogoApisHandler;
import com.mogo.module.common.R;
import com.mogo.module.common.constants.AdasRecognizedType;
import com.mogo.module.common.datacenter.SnapshotLocationDataCenter;
import com.mogo.module.common.drawer.bean.SpeedData;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/*
/**
* @author congtaowang
* @since 2020/10/30
* <p>
@@ -42,21 +34,6 @@ public class BaseDrawer {
*/
public static final int MSG_REMOVE_DIRTY_MARKERS = 9990;
/**
* 地图刷新频率
*/
public static final int MAP_RENDER_FRAME_FREQUENCY = 30;
/**
* 显示速度
*/
public static final int MSG_DISPLAY_SPEED = 11;
/**
* 移动点的时间间隔
*/
public static final int MAP_MARKER_MOVE_INTERVAL = MAP_RENDER_FRAME_FREQUENCY;
/**
* 地图内部资源md5缓存便于资源复用
*/
@@ -64,77 +41,8 @@ public class BaseDrawer {
protected final Context mContext;
private static TextView mSpeedView;
public BaseDrawer() {
mContext = AbsMogoApplication.getApp();
initWorkThreadHandler();
initView();
}
private void initView() {
if (mSpeedView == null) {
mSpeedView = new TextView(mContext);
mSpeedView.setSingleLine(false);
mSpeedView.setTextColor(Color.WHITE);
mSpeedView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
mSpeedView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
private static Handler mWorkThreadHandler;
protected static Handler mRenderThreadHandler = null;
/**
* 处理 marker 移除的线程
*/
private static void initWorkThreadHandler() {
if (mWorkThreadHandler == null) {
mWorkThreadHandler = new Handler(WorkThreadHandler.newInstance("3d-marker-work-thread").getLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == MSG_REMOVE_DIRTY_MARKERS) {
if (msg.obj instanceof Map) {
removeDirtyMarkers(((Map) msg.obj));
Set<String> key = ((Map) msg.obj).keySet();
for (String id : key) {
// 清除道路缓存
clearRoadCacheById(id);
}
} else if (msg.obj instanceof String) {
MogoApisHandler.getInstance().getApis()
.getMapServiceApi()
.getMarkerManager(AbsMogoApplication.getApp())
.removeMarkers(((String) msg.obj));
}
}
}
};
}
if (mRenderThreadHandler == null) {
mRenderThreadHandler = new Handler(WorkThreadHandler.newInstance("render-thread-" + new Random().nextLong()).getLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == MSG_DISPLAY_SPEED) {
if (msg.obj instanceof SpeedData) {
showSpeed((SpeedData) msg.obj);
}
}
}
};
}
}
public static void showSpeed(SpeedData speedData) {
showIdentitySpeed(speedData.getMarker()
, speedData.getSpeed()
, speedData.getUuid()
, speedData.getType()
, speedData.getHeading()
, speedData.getIsVrMode());
}
/**
@@ -162,10 +70,7 @@ public class BaseDrawer {
* @param data 数据体
*/
public void sendMessage(int msg, Object data) {
Message message = Message.obtain();
message.what = msg;
message.obj = data;
mWorkThreadHandler.sendMessage(message);
}
/**
@@ -185,20 +90,6 @@ public class BaseDrawer {
&& recognizedType != AdasRecognizedType.classIdUnKnow;
}
/**
* 是否展示车速UUID以及类型
*
* @param type {@link AdasRecognizedType}
* @return showSpeed
*/
public boolean shouldShowSpeed(int type) {
return true; //todo 验证行人预警,对行人和自行车不做infoWindow过滤
// AdasRecognizedType recognizedType = AdasRecognizedType.valueFrom(type);
// return recognizedType != AdasRecognizedType.classIdPerson;
// &&recognizedType != AdasRecognizedType.classIdBicycle //todo Bicycle显示
// && recognizedType != AdasRecognizedType.classIdMoto //todo moto显示
}
/**
* 获取3D锚点模型资源
*
@@ -258,19 +149,6 @@ public class BaseDrawer {
return Car3DModelColor.Normal.color;
}
/**
* 返回当前自车速度
*
* @return isCurSpeed
*/
protected double getCurSpeed() {
double speed = MogoApisHandler.getInstance().getApis().getAdasControllerApi().getLastSpeed();
if (speed <= 0) {
speed = SnapshotLocationDataCenter.getInstance().getCurSpeed();
}
return speed;
}
/**
* 返回当前自车经纬度
*
@@ -305,45 +183,15 @@ public class BaseDrawer {
* 模型颜色
*/
public enum Car3DModelColor {
Normal("#D8D8D8FF"),
Warming("#FFD53EFF"),
Dangerous("#FF3C45FF"),
Error("#7fb80e");
private final String color;
Car3DModelColor(String color) {
this.color = color;
}
}
/**
* 展示车辆速度
*
* @param mogoMarker {@link IMogoMarker}
* @param speed 是否显示速度
* @param isVrMode 是否是vrMode
*/
public static void showIdentitySpeed(IMogoMarker mogoMarker, double speed, String uuid,
int type, double heading, boolean isVrMode) {
Log.d("EmArrow", "showSelf uuid : " + uuid + " speed : " + speed);
if (mogoMarker == null || mogoMarker.isDestroyed()) {
return;
}
if (!isVrMode) {
mogoMarker.hideInfoWindow();
return;
}
int speedIntVal = (int) (speed * 3.6);
mogoMarker.setInfoWindowOffset(0, 20);
String text = "uuid : " + uuid + "\n"
+ "speed : " + speedIntVal + "\n"
+ "type : " + type + "\n"
+ "heading : " + heading;
mSpeedView.setText(text);
mogoMarker.updateInfoWindowView(mSpeedView);
}
/**
@@ -386,35 +234,6 @@ public class BaseDrawer {
mMarkerCachesResMd5Values.put(id, md5);
}
/**
* 道路匹配到车道中心点
*
* @param lon 经度
* @param lat 纬度
* @param angle 方向角
* @param isRtk 是否是RTK
* @return 匹配经纬度值
*/
public double[] matchRoad(String id, double lon, double lat, double angle, boolean isRtk) {
final long start = System.currentTimeMillis();
double[] matchRoad = MogoApisHandler.getInstance()
.getApis()
.getMapServiceApi()
.getMapUIController()
.matchRoad(id, lon, lat, angle, true, isRtk);
Log.i("timer-matchRoad", "cost " + (System.currentTimeMillis() - start) + "ms");
return matchRoad;
}
/**
* 清理缓存路段数据
*
* @param id 缓存数据Key
*/
public static void clearRoadCacheById(String id) {
MogoApisHandler.getInstance().getApis().getMapServiceApi().getMapUIController().clearRoadCacheById(id);
}
/**
* 使用系统时间或卫星时间计算出动画的运动时间最小值45防止两个点距离过近设置的最小动画执行时间
*
@@ -435,53 +254,4 @@ public class BaseDrawer {
return interval;
}
/**
* 根据位置信息、车头朝向计算道路吸附
*
* @param id UUID
* @param lon 经度
* @param lat 纬度
* @param heading 方向角
* @param lastLon 上一个点经度
* @param lastLat 上一个点纬度
* @return 匹配经纬度
*/
protected double[] getMatchLonLat(String id, double lon, double lat, double heading, double lastLon, double lastLat) {
double[] matchedPoint = matchRoad(id, lon,
lat,
heading,
true
);
boolean match;
if (matchedPoint != null) {
// Logger.d( TAG, "matchPoint %s distance = %s",lineCounter, matchedPoint[2] );
match = matchedPoint[2] < 1 && matchedPoint[2] > 0;
if (lastLon != -1) {
double clearAngle;
if (heading > 0 && heading <= 90) {
clearAngle = heading;
} else if (heading > 90 && heading <= 180) {
clearAngle = 180 - heading;
} else if (heading > 180 && heading <= 270) {
clearAngle = heading - 180;
} else {
clearAngle = 360 - heading;
}
double _angle = Math.atan2(Math.abs(matchedPoint[0] - lastLon), Math.abs(matchedPoint[1] - lastLat)) * (180 / PI);
_angle = Math.abs(clearAngle - _angle);
// Logger.d(TAG, "matchPoint %s angel = %s", lineCounter, _angle);
if (_angle > 22.5) {
match = false;
}
}
if (match) {
lon = matchedPoint[0];
lat = matchedPoint[1];
}
}
return new double[]{lon, lat};
}
}

View File

@@ -1,46 +1,42 @@
package com.mogo.module.common.drawer;
import static com.mogo.cloud.socket.entity.SocketDownDataHelper.FROM_ADAS;
import android.text.TextUtils;
import android.content.Context;
import com.mogo.commons.AbsMogoApplication;
import com.mogo.commons.debug.DebugConfig;
import com.mogo.eagle.core.data.map.MogoLatLng;
import com.mogo.eagle.core.data.enums.TrafficTypeEnum;
import com.mogo.eagle.core.data.traffic.TrafficData;
import com.mogo.eagle.core.utilcode.mogo.logger.Logger;
import com.mogo.map.marker.IMogoMarker;
import com.mogo.map.marker.MogoMarkerOptions;
import com.mogo.module.common.MogoApisHandler;
import com.mogo.module.common.constants.AdasRecognizedType;
import com.mogo.module.common.constants.DataTypes;
import com.mogo.utils.UiThreadHandler;
import com.mogo.utils.logger.Logger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
/**
* @author xiaoyuzhou
* @date 2021/10/19 10:45 上午
* 域控制器识别信息绘制
*/
public class IdentifyDataDrawer extends BaseDrawer {
public class IdentifyDataDrawer {
private static final String TAG = "IdentifyDataDrawer";
protected final Context mContext;
private static volatile IdentifyDataDrawer sInstance;
/**
* 上一帧数据的缓存
*/
private static Map<String, IMogoMarker> mMarkersCaches = new ConcurrentHashMap<>();
private static ConcurrentHashMap<String, TrafficData> mMarkersCaches = new ConcurrentHashMap<>();
/**
* 最后一帧数据的缓存
* 已经感知不到的脏数据
*/
private final Map<String, TrafficData> mLastPositions = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, TrafficData> mDirtyPositions = new ConcurrentHashMap<>();
private IdentifyDataDrawer() {
super();
mContext = AbsMogoApplication.getApp();
addPreVehicleModel();
}
public static IdentifyDataDrawer getInstance() {
@@ -63,10 +59,10 @@ public class IdentifyDataDrawer extends BaseDrawer {
*
* @param resultList adas感知融合数据
*/
public void renderAdasRecognizedResult(List<TrafficData> resultList) {
final long start = System.nanoTime();
public void renderAdasRecognizedResult(ArrayList<TrafficData> resultList) {
if (resultList == null || resultList.isEmpty() || !DebugConfig.isUseAdasRecognize()) {
clearOldMarker();
Logger.w(TAG, "感知数据为空无需渲染……");
return;
}
@@ -76,186 +72,35 @@ public class IdentifyDataDrawer extends BaseDrawer {
return;
}
Map<String, IMogoMarker> newAdasRecognizedMarkersCaches = new ConcurrentHashMap<>();
List<TrafficData> newDiffSet = new ArrayList<>();
for (TrafficData recognizedListResult : resultList) {
// 循环将集合中的数据提取记录
ArrayList<String> trafficDataUuidList = new ArrayList<>();
for (TrafficData trafficData : resultList) {
trafficDataUuidList.add(trafficData.getUuid());
}
if (isUselessValue(recognizedListResult)) {
continue;
}
if (AdasRecognizedType.valueFrom(recognizedListResult.getType().getType()) == AdasRecognizedType.classIdUnKnow) {
continue;
}
// 复用之前存在的 marker
String uniqueKey = recognizedListResult.getUuid();
IMogoMarker marker = mMarkersCaches.remove(uniqueKey);
if (marker != null && !marker.isDestroyed()) {
//Log.d(TAG, "发现缓存marker id : " + uniqueKey);
updateCacheMarkerRes(marker, recognizedListResult);
renderAdasOneFrame(marker, uniqueKey, recognizedListResult, newAdasRecognizedMarkersCaches);
} else {
// 新增添加进差集
newDiffSet.add(recognizedListResult);
// 找出上一针数据中已经不在本次数据中存在的数据
for (String uuid : mMarkersCaches.keySet()) {
if (!trafficDataUuidList.contains(uuid)) {
mDirtyPositions.put(uuid, mMarkersCaches.get(uuid));
}
}
removeUselessMarker(mMarkersCaches);
removeUselessLastRecord();
int newDiffSetSize = newDiffSet.size();
// Log.d(TAG, "原数据量 " + resultList.size() + " 新增marker数量 " + newDiffSetSize);
// 复用过期 marker
if (newDiffSetSize > 0) {
for (int i = 0; i < newDiffSetSize; i++) {
TrafficData recognizedListResult = newDiffSet.get(i);
if (AdasRecognizedType.valueFrom(recognizedListResult.getType().getType()) == AdasRecognizedType.classIdUnKnow) {
continue;
}
String uniqueKey = recognizedListResult.getUuid();
IMogoMarker marker = drawAdasRecognizedDataMarker(recognizedListResult);
if (marker == null) {
continue;
}
//Log.d(TAG, "新增marker id : " + uniqueKey);
renderAdasOneFrame(marker, uniqueKey, recognizedListResult, newAdasRecognizedMarkersCaches);
}
}
sendMessage(MSG_REMOVE_DIRTY_MARKERS, mMarkersCaches);
mMarkersCaches.clear();
mMarkersCaches = newAdasRecognizedMarkersCaches;
// Log.d(TAG, "ADAS数据延时绘制 render 接收数据 -> 处理结束 " + TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - start)) + "ms");
}
/**
* todo 后面涉及到此类变化的数据均改动
* 更新缓存Marker的资源
*
* @param marker
* @param recognizedListResult
*/
private void updateCacheMarkerRes(IMogoMarker marker, TrafficData recognizedListResult) {
// 获取3D资源文件
int resId = getModelRes(recognizedListResult.getType().getType());
String resIdVal = resId + "";
// 查询缓存中是否存在
String resName = mMarkerCachesResMd5Values.get(resIdVal);
// 缓存存在直接试用,节省内存
if (!TextUtils.isEmpty(resName)) {
if (!TextUtils.equals(resName, marker.getMarkerResName())) {
marker.use3DResource(resName);
}
} else {
// 缓存不存在,取出名称进行缓存
resName = marker.use3DResource(resId);
mMarkerCachesResMd5Values.put(resIdVal, resName);
}
}
/**
* // todo 最好重新设计一个数据结构用于多线程数据过期失效的场景参见redis数据过期
* 移除工控机长时间没有下发的数据
*/
private void removeUselessLastRecord() {
if (mLastPositions.isEmpty()) {
return;
}
Iterator<TrafficData> iterator = mLastPositions.values().iterator();
// Log.d(TAG, "EmArrow removeUselessLastRecord size : " + mLastPositions.size());
while (iterator.hasNext()) {
TrafficData result = iterator.next();
long internal = result.getSatelliteTime() - getCurSatelliteTime();
if (internal > 3000) { //防止帧率过低导致误删除上一个节点对象,从而出现跳跃现象
iterator.remove();
}
}
}
/**
* 过滤无用数据
*
* @param recognizedListResult {@link TrafficData}
* @return useless
*/
private boolean isUselessValue(TrafficData recognizedListResult) {
if (recognizedListResult == null) {
return true;
}
if (nonRenderType(recognizedListResult.getType().getType())) {
return true;
}
String uniqueKey = recognizedListResult.getUuid();
return TextUtils.isEmpty(uniqueKey);
}
/**
* 绘制某个物体的一个数据
*
* @param recognizedListResult {@link TrafficData}
* @param newAdasRecognizedMarkersCaches 缓存集合
*/
private void renderAdasOneFrame(IMogoMarker marker,
String uniqueKey,
TrafficData recognizedListResult,
Map<String, IMogoMarker> newAdasRecognizedMarkersCaches) {
final long start = System.nanoTime();
TrafficData lastPosition = mLastPositions.remove(uniqueKey);
mLastPositions.put(uniqueKey, recognizedListResult);
long interval = 45;
if (lastPosition != null) {
interval = computeAnimDuration(lastPosition.getSatelliteTime(), recognizedListResult.getSatelliteTime());
}
final MogoLatLng renderLoc = new MogoLatLng(recognizedListResult.getLat(), recognizedListResult.getLon());
long cost = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
//Log.d(TAG, "ADAS动画数据 cost : " + cost);
final long intervalRef = interval - cost;
//Log.d(TAG, "ADAS动画数据 最终赋值 : " + intervalRef + " 两帧间隔 : " + interval + " uuid : " + recognizedListResult.getUuid());
marker.addDynamicAnchorPosition(renderLoc, (float) recognizedListResult.getHeading(), intervalRef);
String carColor = getModelRenderColor(recognizedListResult.getType().getType(), FROM_ADAS, recognizedListResult.getThreatLevel());
marker.setAnchorColor(carColor);
newAdasRecognizedMarkersCaches.put(uniqueKey, marker);
// 显示目标车的信息,但是数据多的时候会导致崩溃,不建议打开
// if (shouldShowSpeed(recognizedListResult.getType().getType())) {
// SpeedData speedData = new SpeedData(marker
// , recognizedListResult.getSpeed()
// , recognizedListResult.getUuid()
// , recognizedListResult.getType().getType()
// , recognizedListResult.getHeading()
// , MogoApisHandler.getInstance().getApis().getStatusManagerApi().isVrMode());
// showSpeed(speedData);
// // 移除脏数据
// for (String uuid : mDirtyPositions.keySet()) {
// MogoApisHandler.getInstance().getApis()
// .getMapServiceApi()
// .getMarkerManager(mContext)
// .removeMarker(uuid);
// }
// 绘制新数据
MogoApisHandler.getInstance().getApis()
.getMapServiceApi()
.getMarkerManager(mContext)
.updateBatchMarkerPosition(resultList);
}
/**
* 创建一个新 marker
*
* @param recognizedListResult {@link TrafficData}
* @return {@link IMogoMarker}
*/
private IMogoMarker drawAdasRecognizedDataMarker(TrafficData recognizedListResult) {
if (recognizedListResult == null) {
return null;
}
int resId = getModelRes(recognizedListResult.getType().getType());
String resIdVal = resId + "";
String carColor = getModelRenderColor(recognizedListResult.getType().getType(), FROM_ADAS, recognizedListResult.getThreatLevel());
MogoMarkerOptions options = new MogoMarkerOptions()
.owner(DataTypes.TYPE_MARKER_ADAS)
.anchor(0.5f, 0.5f)
.set3DMode(true)
.gps(true)
.anchorColor(carColor)
.controlAngle(true)
.resName(mMarkerCachesResMd5Values.get(resIdVal))
.icon3DRes(resId)
.rotate((float) recognizedListResult.getHeading())
.position(new MogoLatLng(recognizedListResult.getLat(), recognizedListResult.getLon()));
IMogoMarker marker = MogoApisHandler.getInstance().getApis().getMapServiceApi().getMarkerManager(mContext).addMarker(DataTypes.TYPE_MARKER_ADAS, options);
cacheMarkerIconResMd5Val(resIdVal, marker);
return marker;
}
/**
* 清除旧的 marker 数据
@@ -264,7 +109,48 @@ public class IdentifyDataDrawer extends BaseDrawer {
if (mMarkersCaches != null) {
mMarkersCaches.clear();
}
mLastPositions.clear();
sendMessage(MSG_REMOVE_DIRTY_MARKERS, DataTypes.TYPE_MARKER_ADAS);
}
private void addPreVehicleModel() {
Logger.d(TAG, "添加感知模型到地图中……");
addPreVehicleModelWeiZhi(TrafficTypeEnum.TYPE_TRAFFIC_ID_WEI_ZHI, "添加感知模型到地图中……preVehicleStrWeiZhi=");
addPreVehicleModelWeiZhi(TrafficTypeEnum.TYPE_TRAFFIC_ID_PEOPLE, "添加感知模型到地图中……preVehicleStrPeople=");
addPreVehicleModelWeiZhi(TrafficTypeEnum.TYPE_TRAFFIC_ID_BICYCLE, "添加感知模型到地图中……preVehicleStrBicycle=");
addPreVehicleModelWeiZhi(TrafficTypeEnum.TYPE_TRAFFIC_ID_TA_CHE, "添加感知模型到地图中……preVehicleStrTaChe=");
addPreVehicleModelWeiZhi(TrafficTypeEnum.TYPE_TRAFFIC_ID_MOTO, "添加感知模型到地图中……preVehicleStrMoto=");
addPreVehicleModelWeiZhi(TrafficTypeEnum.TYPE_TRAFFIC_ID_BUS, "添加感知模型到地图中……preVehicleStrBus=");
addPreVehicleModelWeiZhi(TrafficTypeEnum.TYPE_TRAFFIC_ID_TRUCK, "添加感知模型到地图中……preVehicleStrTruck=");
}
/**
* 添加模型到地图中
*
* @param typeTrafficIdWeiZhi
* @param s
*/
private void addPreVehicleModelWeiZhi(TrafficTypeEnum typeTrafficIdWeiZhi, String s) {
String preVehicleStrWeiZhi = MogoApisHandler.getInstance().getApis()
.getMapServiceApi()
.getMarkerManager(mContext)
.addPreVehicleModel(typeTrafficIdWeiZhi.getType(),
typeTrafficIdWeiZhi.getTraffic3DIconId());
Logger.d(TAG, s + preVehicleStrWeiZhi);
if (preVehicleStrWeiZhi == null) {
UiThreadHandler.postDelayed(new Runnable() {
@Override
public void run() {
Logger.w(TAG, "添加感知模型到地图中失败,尝试重复添加……");
addPreVehicleModelWeiZhi(typeTrafficIdWeiZhi, s);
}
}, 1000L);
}
}
}