代码优化

This commit is contained in:
wangcongtao
2021-03-02 15:30:19 +08:00
parent 9b31b66746
commit f6cf5ba544
12 changed files with 441 additions and 364 deletions

View File

@@ -69,12 +69,8 @@ class AdasRecognizedResultDrawer extends BaseDrawer {
* 渲染 adas 识别的数据
*
* @param resultList
* @param machineVision
* @param curSpeed
*/
public void renderAdasRecognizedResult( List< ADASRecognizedResult > resultList,
boolean machineVision,
double curSpeed ) {
public void renderAdasRecognizedResult( List< ADASRecognizedResult > resultList ) {
if ( resultList == null || resultList.isEmpty() ) {
clearOldMarker();
return;
@@ -99,7 +95,7 @@ class AdasRecognizedResultDrawer extends BaseDrawer {
IMogoMarker marker = mAdasRecognizedMarkersCaches.remove( uniqueKey );
ADASRecognizedResult lastPosition = mLastPositions.put( uniqueKey, recognizedListResult );
if ( marker == null || marker.isDestroyed() ) {
marker = drawAdasRecognizedDataMarker( recognizedListResult, machineVision, curSpeed );
marker = drawAdasRecognizedDataMarker( recognizedListResult );
if ( marker == null ) {
continue;
}
@@ -134,36 +130,49 @@ class AdasRecognizedResultDrawer extends BaseDrawer {
* 绘制 marker
*
* @param recognizedListResult
* @param machineVision
* @param curSpeed
* @return
*/
private IMogoMarker drawAdasRecognizedDataMarker( ADASRecognizedResult recognizedListResult,
boolean machineVision,
double curSpeed ) {
private IMogoMarker drawAdasRecognizedDataMarker( ADASRecognizedResult recognizedListResult ) {
if ( recognizedListResult == null ) {
return null;
}
String resIdVal = null;
int resId = getVrModelResId();
resIdVal = resId + "";
MogoMarkerOptions options = new MogoMarkerOptions()
.owner( DataTypes.TYPE_MARKER_ADAS )
.anchor( 0.5f, 0.5f )
.set3DMode( true )
.controlAngle( true )
.icon3DRes( getVrModel() )
.resName( mMarkerCachesResMd5Values.get( resIdVal ) )
.icon3DRes( resId )
.rotate( ( float ) recognizedListResult.heading )
.position( new MogoLatLng( recognizedListResult.lat, recognizedListResult.lon ) );
return MogoApisHandler.getInstance().getApis().getMapServiceApi().getMarkerManager( mContext ).addMarker( DataTypes.TYPE_MARKER_ADAS, options );
IMogoMarker marker = MogoApisHandler.getInstance().getApis().getMapServiceApi().getMarkerManager( mContext ).addMarker( DataTypes.TYPE_MARKER_ADAS, options );
cacheMarkerIconResMd5Val( resIdVal, marker );
return marker;
}
private int getVrModel() {
/**
* 获取 vr 模型资源 id
*
* @return
*/
private int getVrModelResId() {
return R.raw.cargrey;
}
/**
* vr 模式切换
*/
public void notifyVrModeChanged() {
clearOldMarker();
}
/**
* 清除旧的 marker 数据
*/
private void clearOldMarker() {
if ( mAdasRecognizedMarkersCaches != null ) {
mAdasRecognizedMarkersCaches.clear();

View File

@@ -5,20 +5,18 @@ import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mogo.commons.AbsMogoApplication;
import com.mogo.map.CoordinatesTransformer;
import com.mogo.map.marker.IMogoMarker;
import com.mogo.module.common.MogoApisHandler;
import com.mogo.module.common.constants.AdasRecognizedType;
import com.mogo.module.common.constants.CarModelType;
import com.mogo.module.common.constants.DataTypes;
import com.mogo.module.common.constants.SafeType;
import com.mogo.utils.WorkThreadHandler;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public
@@ -30,19 +28,15 @@ public
*/
class BaseDrawer {
public static final int POS_LON = 1;
public static final int POS_LAT = 0;
public static final double BOUND_DISTANCE_DANGEROUS = 0.5;
public static final double BOUND_SPEED_DANGEROUS = 1.5;
public static final double BOUND_DISTANCE_WARM = 1;
public static final double BOUND_SPEED_WARM = 1.1;
// 移除过期的 marker
public static final int MSG_REMOVE_DIRTY_MARKERS = 9990;
public static final int MSG_REMOVE_ADAS_MARKERS = 9992;
/**
* 地图内部资源md5缓存便于资源复用
*/
protected static final Map< String, String > mMarkerCachesResMd5Values = new HashMap<>();
public BaseDrawer() {
if ( mWorkThreadHandler == null ) {
initWorkThreadHandler();
@@ -93,66 +87,11 @@ class BaseDrawer {
}
/**
* 安全类型
* 判断是否是车辆
*
* @param distance
* @param speed
* @param curSpeed
* @param type
* @return
*/
protected SafeType getSafeType( double distance, double speed, double curSpeed ) {
if ( distance < BOUND_DISTANCE_DANGEROUS ) {
return SafeType.DistanceDangerous;
}
if ( speed > curSpeed * BOUND_SPEED_DANGEROUS ) {
return SafeType.SpeedDangerous;
}
if ( distance < BOUND_DISTANCE_WARM ) {
return SafeType.DistanceDangerous;
}
if ( speed > curSpeed * BOUND_SPEED_WARM ) {
return SafeType.DistanceWarm;
}
return SafeType.Normal;
}
protected SafeType getSafeType( double distanceX, double distanceY, double speed, double curSpeed ) {
if ( distanceX < BOUND_DISTANCE_DANGEROUS || distanceY < BOUND_DISTANCE_DANGEROUS ) {
return SafeType.DistanceDangerous;
}
if ( speed > curSpeed * BOUND_SPEED_DANGEROUS ) {
return SafeType.SpeedDangerous;
}
if ( distanceX < BOUND_DISTANCE_WARM || distanceY < BOUND_DISTANCE_WARM ) {
return SafeType.DistanceWarm;
}
if ( speed > curSpeed * BOUND_SPEED_WARM ) {
return SafeType.SpeedWarm;
}
return SafeType.Normal;
}
/**
* 车模
*
* @return
*/
protected CarModelType getCarModelType() {
// 根据车道、行驶方向等计算出选用哪个车模图片
return CarModelType.Other;
}
private CoordinatesTransformer mTransformer;
protected double[] transformGcj02ToFitMap( double lat, double lon ) {
if ( mTransformer == null ) {
mTransformer = MogoApisHandler.getInstance().getApis().getMapServiceApi().getCoordinatesTransformer();
}
return mTransformer.transform( lat, lon );
}
protected boolean isCarType( int type ) {
AdasRecognizedType recognizedType = AdasRecognizedType.valueFrom( type );
if ( recognizedType == AdasRecognizedType.classIdCar
@@ -166,6 +105,14 @@ class BaseDrawer {
private TextView mSpeedView = null;
/**
* 展示车辆速度
*
* @param context
* @param mogoMarker
* @param speed
* @param isVrMode
*/
protected void showSelfSpeed( Context context, IMogoMarker mogoMarker, double speed, boolean isVrMode ) {
if ( mogoMarker == null || mogoMarker.isDestroyed() ) {
return;
@@ -213,4 +160,21 @@ class BaseDrawer {
}
dirtyMarkers.clear();
}
/**
* 缓存绘制 marker 的 id 和 marker 资源缓存的 md5 的关系
*
* @param id
* @param marker
*/
protected static void cacheMarkerIconResMd5Val( String id, IMogoMarker marker ) {
if ( marker == null || marker.isDestroyed() ) {
return;
}
String md5 = marker.getMarkerResName();
if ( TextUtils.isEmpty( md5 ) || TextUtils.isEmpty( id ) ) {
return;
}
mMarkerCachesResMd5Values.put( id, md5 );
}
}

View File

@@ -2,7 +2,6 @@ package com.mogo.module.common.drawer;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
@@ -135,8 +134,7 @@ class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClickListen
*
* @param data 自车周边数据
*/
public void renderSnapshotData( MogoSnapshotSetData data,
boolean machineVision ) {
public void renderSnapshotData( MogoSnapshotSetData data ) {
if ( clear( data ) ) {
return;
@@ -164,11 +162,11 @@ class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClickListen
continue;
}
long start = System.currentTimeMillis();
IMogoMarker marker = mCloudSnapshotMarkersCaches.remove( uniqueKey );
CloudRoadData lastPosition = mLastPositions.put( uniqueKey, cloudRoadData );
if ( marker == null || marker.isDestroyed() ) {
marker = drawSnapshotDataMarker( cloudRoadData, machineVision, data.curSpeed );
marker = drawSnapshotDataMarker( cloudRoadData );
if ( marker == null ) {
continue;
}
@@ -176,22 +174,18 @@ class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClickListen
bindClickListener( marker );
}
}
Log.i( "mock-snapshot-timer2", "cost " + ( System.currentTimeMillis() - start ) + "ms" );
start = System.currentTimeMillis();
newAdasRecognizedMarkersCaches.put( uniqueKey, marker );
if ( mChangeCarModeStatus ) {
mIsVrMode = MogoApisHandler.getInstance().getApis().getStatusManagerApi().isVrMode();
mChangeCarModeStatus = false;
if ( mIsVrMode ) {
Logger.d( TAG, "3D模型-%s", uniqueKey );
marker.getMogoMarkerOptions().set3DMode( true );
marker.use3DResource( getVrModel( cloudRoadData ) );
marker.use3DResource( getVrModelResId( cloudRoadData ) );
} else {
Logger.d( TAG, "2D贴图-%s", uniqueKey );
marker.getMogoMarkerOptions().set3DMode( false );
marker.setIcon( ViewUtils.fromView( inflateView( cloudRoadData, machineVision, 0 ) ) );
marker.setIcon( ViewUtils.fromView( inflateView( cloudRoadData ) ) );
}
}
@@ -217,14 +211,12 @@ class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClickListen
}
interval2 -= 25;
marker.startSmoothInMs( points, interval2 );
Logger.d( TAG, "平滑移动 - %s duration = %s", uniqueKey, interval2 );
}
} else {
marker.setRotateAngle( ( float ) cloudRoadData.getHeading() );
marker.setPosition( cloudRoadData.getLat(), cloudRoadData.getLon() );
}
showSelfSpeed( mContext, marker, cloudRoadData.getSpeed(), MogoApisHandler.getInstance().getApis().getStatusManagerApi().isVrMode() );
Log.i( "mock-snapshot-timer3", "cost " + ( System.currentTimeMillis() - start ) + "ms" );
}
sendMessage( MSG_REMOVE_DIRTY_MARKERS, mCloudSnapshotMarkersCaches );
mCloudSnapshotMarkersCaches = newAdasRecognizedMarkersCaches;
@@ -279,16 +271,13 @@ class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClickListen
* 绘制 marker
*
* @param data
* @param machineVision
* @param curSpeed
* @return
*/
private IMogoMarker drawSnapshotDataMarker( CloudRoadData data, boolean machineVision, double curSpeed ) {
private IMogoMarker drawSnapshotDataMarker( CloudRoadData data ) {
if ( data == null ) {
return null;
}
Logger.d( TAG, "draw marker uniqueKey = %s", data.getUniqueKey() );
MogoMarkerOptions options = new MogoMarkerOptions()
.owner( DataTypes.TYPE_MARKER_CLOUD_DATA )
.anchor( 0.5f, 0.5f )
@@ -296,17 +285,23 @@ class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClickListen
.object( data )
.controlAngle( true )
.position( new MogoLatLng( data.getLat(), data.getLon() ) );
String resIdVal = null;
if ( MogoApisHandler.getInstance().getApis().getStatusManagerApi().isVrMode() ) {
options.set3DMode( true );
options.anchorColor( "#00FF00" );
Logger.d( TAG, "3D模型 - %s", data.getUniqueKey() );
options.icon3DRes( getVrModel( data ) );
int resId = getVrModelResId( data );
resIdVal = resId + "";
options.resName( mMarkerCachesResMd5Values.get( resIdVal ) );
options.icon3DRes( resId );
} else {
options.set3DMode( false );
Logger.d( TAG, "2D贴图 - %s", data.getUniqueKey() );
options.icon( inflateView( data, machineVision, curSpeed ) );
View view = inflateView( data );
options.icon( view );
resIdVal = view.getId() + "";
}
return MogoApisHandler.getInstance().getApis().getMapServiceApi().getMarkerManager( mContext ).addMarker( DataTypes.TYPE_MARKER_CLOUD_DATA, options );
IMogoMarker marker = MogoApisHandler.getInstance().getApis().getMapServiceApi().getMarkerManager( mContext ).addMarker( DataTypes.TYPE_MARKER_CLOUD_DATA, options );
cacheMarkerIconResMd5Val( resIdVal, marker );
return marker;
}
/**
@@ -315,7 +310,7 @@ class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClickListen
* @param data
* @return
*/
private int getVrModel( CloudRoadData data ) {
private int getVrModelResId( CloudRoadData data ) {
switch ( data.getFromType() ) {
case CloudRoadData.FROM_ADAS:
return R.raw.cargrey;
@@ -327,20 +322,18 @@ class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClickListen
}
}
private View inflateView( CloudRoadData data, boolean machineVision, double curSpeed ) {
/**
* 生成 2d marker 资源
*
* @param data
* @return
*/
private View inflateView( CloudRoadData data ) {
View rootView = LayoutInflater.from( AbsMogoApplication.getApp() ).inflate( R.layout.module_commons_layout_car, null );
// SafeType safeType = getSafeType( data.getDistance(), data.getSpeed(), curSpeed );
// TextView tv = rootView.findViewById( R.id.module_commons_marker_car_speed );
// // 机器视觉展示速度,用户视觉展示安全类型
// tv.setText( machineVision ? String.valueOf( data.getSpeed() ) : safeType.getMsg() );
ImageView iv = rootView.findViewById( R.id.module_commons_marker_car_model );
// iv.setImageResource( MarkerResourceManager.getMarkerDrawableResId(
// machineVision ? VisionMode.Machine : VisionMode.User,
// AdasRecognizedType.valueFrom( data.getType() ),
// getCarModelType(),
// safeType
// ) );
iv.setImageResource( get2DModel( data ) );
int viewIdLike = get2DModel( data );
iv.setImageResource( viewIdLike );
rootView.setId( viewIdLike );
return rootView;
}
@@ -353,9 +346,7 @@ class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClickListen
private int get2DModel( CloudRoadData data ) {
switch ( data.getFromType() ) {
case CloudRoadData.FROM_ADAS:
// return R.drawable.icon_map_marker_car_type_taxi;
case CloudRoadData.FROM_ROAD_UNIT:
// return R.drawable.icon_map_marker_car_type_bus;
case CloudRoadData.FROM_MY_LOCATION:
default:
return R.drawable.icon_map_marker_car_gray;