|
| 1 | +import { CubePrimitive, Vector3, type Color } from "@foxglove/schemas"; |
| 2 | +import { |
| 3 | + MovingObject, |
| 4 | + MovingObject_VehicleClassification_LightState_IndicatorState, |
| 5 | +} from "@lichtblick/asam-osi-types"; |
| 6 | +import { eulerToQuaternion, pointRotationByQuaternion } from "@utils/geometry"; |
| 7 | +import { objectToCubePrimitive } from "@utils/marker"; |
| 8 | +import { DeepRequired } from "ts-essentials"; |
| 9 | + |
| 10 | +import { BRAKE_LIGHT_COLOR } from "../config"; |
| 11 | + |
| 12 | +export enum BrakeLightSide { |
| 13 | + Left, |
| 14 | + Right, |
| 15 | +} |
| 16 | + |
| 17 | +export enum IndicatorLightSide { |
| 18 | + FrontLeft, |
| 19 | + FrontRight, |
| 20 | + RearLeft, |
| 21 | + RearRight, |
| 22 | +} |
| 23 | + |
| 24 | +const BRAKE_LIGHT_DIMENSIONS: Vector3 = { x: 0.5, y: 0.25, z: 0.25 }; |
| 25 | +const BRAKE_LIGHT_POSITION_Y_OFFSET = 0.25; |
| 26 | + |
| 27 | +const INDICATOR_ON_COLOR: Color = { r: 1.0, g: 0.8, b: 0.0, a: 0.7 }; |
| 28 | +const INDICATOR_OFF_COLOR: Color = { r: 0.5, g: 0.5, b: 0.0, a: 0.7 }; |
| 29 | +const INDICATOR_LIGHT_DIMENSIONS: Vector3 = { x: 0.25, y: 0.25, z: 0.25 }; |
| 30 | +const INDICATOR_LIGHT_POSITION_Y_OFFSET = 0.125; |
| 31 | +const INDICATOR_LIGHT_POSITION_Z_OFFSET = 0.25; |
| 32 | + |
| 33 | +export const buildBrakeLight = ( |
| 34 | + moving_obj: DeepRequired<MovingObject>, |
| 35 | + side: BrakeLightSide, |
| 36 | +): CubePrimitive => { |
| 37 | + const brakeLightColor = |
| 38 | + BRAKE_LIGHT_COLOR[moving_obj.vehicle_classification.light_state.brake_light_state]; |
| 39 | + |
| 40 | + const directionMultiplier = side === BrakeLightSide.Left ? 1 : -1; |
| 41 | + const localAxisOffset: Vector3 = { |
| 42 | + x: -(moving_obj.base.dimension.length / 2), |
| 43 | + y: |
| 44 | + directionMultiplier * (moving_obj.base.dimension.width / 2) - |
| 45 | + BRAKE_LIGHT_POSITION_Y_OFFSET * directionMultiplier, |
| 46 | + z: 0.0, |
| 47 | + }; |
| 48 | + const baseOrientation = eulerToQuaternion( |
| 49 | + moving_obj.base.orientation.roll, |
| 50 | + moving_obj.base.orientation.pitch, |
| 51 | + moving_obj.base.orientation.yaw, |
| 52 | + ); |
| 53 | + const globalOffset = pointRotationByQuaternion(localAxisOffset, baseOrientation); |
| 54 | + |
| 55 | + return objectToCubePrimitive( |
| 56 | + moving_obj.base.position.x + globalOffset.x, |
| 57 | + moving_obj.base.position.y + globalOffset.y, |
| 58 | + moving_obj.base.position.z + globalOffset.z, |
| 59 | + moving_obj.base.orientation.roll, |
| 60 | + moving_obj.base.orientation.pitch, |
| 61 | + moving_obj.base.orientation.yaw, |
| 62 | + BRAKE_LIGHT_DIMENSIONS.x, |
| 63 | + BRAKE_LIGHT_DIMENSIONS.y, |
| 64 | + BRAKE_LIGHT_DIMENSIONS.z, |
| 65 | + brakeLightColor, |
| 66 | + ); |
| 67 | +}; |
| 68 | + |
| 69 | +export const buildIndicatorLight = ( |
| 70 | + moving_obj: DeepRequired<MovingObject>, |
| 71 | + side: IndicatorLightSide, |
| 72 | +): CubePrimitive => { |
| 73 | + let lightOn = false; |
| 74 | + switch (moving_obj.vehicle_classification.light_state.indicator_state) { |
| 75 | + case MovingObject_VehicleClassification_LightState_IndicatorState.LEFT: |
| 76 | + if (side === IndicatorLightSide.FrontLeft || side === IndicatorLightSide.RearLeft) { |
| 77 | + lightOn = true; |
| 78 | + } else { |
| 79 | + lightOn = false; |
| 80 | + } |
| 81 | + break; |
| 82 | + case MovingObject_VehicleClassification_LightState_IndicatorState.RIGHT: |
| 83 | + if (side === IndicatorLightSide.FrontRight || side === IndicatorLightSide.RearRight) { |
| 84 | + lightOn = true; |
| 85 | + } else { |
| 86 | + lightOn = false; |
| 87 | + } |
| 88 | + break; |
| 89 | + case MovingObject_VehicleClassification_LightState_IndicatorState.WARNING: |
| 90 | + lightOn = true; |
| 91 | + break; |
| 92 | + default: |
| 93 | + lightOn = false; |
| 94 | + break; |
| 95 | + } |
| 96 | + |
| 97 | + const localAxisOffset: Vector3 = { |
| 98 | + x: |
| 99 | + side === IndicatorLightSide.FrontLeft || side === IndicatorLightSide.FrontRight |
| 100 | + ? moving_obj.base.dimension.length / 2 |
| 101 | + : -(moving_obj.base.dimension.length / 2), |
| 102 | + y: |
| 103 | + side === IndicatorLightSide.FrontLeft || side === IndicatorLightSide.RearLeft |
| 104 | + ? moving_obj.base.dimension.width / 2 - INDICATOR_LIGHT_POSITION_Y_OFFSET |
| 105 | + : -moving_obj.base.dimension.width / 2 + INDICATOR_LIGHT_POSITION_Y_OFFSET, |
| 106 | + z: INDICATOR_LIGHT_POSITION_Z_OFFSET, |
| 107 | + }; |
| 108 | + const baseOrientation = eulerToQuaternion( |
| 109 | + moving_obj.base.orientation.roll, |
| 110 | + moving_obj.base.orientation.pitch, |
| 111 | + moving_obj.base.orientation.yaw, |
| 112 | + ); |
| 113 | + const globalOffset = pointRotationByQuaternion(localAxisOffset, baseOrientation); |
| 114 | + |
| 115 | + return objectToCubePrimitive( |
| 116 | + moving_obj.base.position.x + globalOffset.x, |
| 117 | + moving_obj.base.position.y + globalOffset.y, |
| 118 | + moving_obj.base.position.z + globalOffset.z, |
| 119 | + moving_obj.base.orientation.roll, |
| 120 | + moving_obj.base.orientation.pitch, |
| 121 | + moving_obj.base.orientation.yaw, |
| 122 | + INDICATOR_LIGHT_DIMENSIONS.x, |
| 123 | + INDICATOR_LIGHT_DIMENSIONS.y, |
| 124 | + INDICATOR_LIGHT_DIMENSIONS.z, |
| 125 | + lightOn ? INDICATOR_ON_COLOR : INDICATOR_OFF_COLOR, |
| 126 | + ); |
| 127 | +}; |
0 commit comments