/packages/webviz-core/src/panels/ThreeDimensionalViz/commands/PoseMarkers.js

https://github.com/cruise-automation/webviz · JavaScript · 139 lines · 117 code · 14 blank · 8 comment · 10 complexity · 4ec7f2137a372c9a0240089a8510c3c5 MD5 · raw file

  1. // @flow
  2. //
  3. // Copyright (c) 2019-present, Cruise LLC
  4. //
  5. // This source code is licensed under the Apache License, Version 2.0,
  6. // found in the LICENSE file in the root directory of this source tree.
  7. // You may not use this file except in compliance with the License.
  8. import { vec3 } from "gl-matrix";
  9. import React, { type Node } from "react";
  10. import {
  11. Arrows,
  12. FilledPolygons,
  13. pointToVec3,
  14. vec3ToPoint,
  15. orientationToVec4,
  16. type Arrow,
  17. type CommonCommandProps,
  18. } from "regl-worldview";
  19. import CarModel from "./CarModel";
  20. import carOutlinePoints from "./CarModel/carOutline.json";
  21. type Props = {
  22. children: Arrow[],
  23. ...CommonCommandProps,
  24. };
  25. const X_SCALING_FACTOR = 1.111;
  26. const Y_SCALING_FACTOR = 1.121;
  27. const scaledCarOutlineBufferPoints = (() => {
  28. const vectorSum = carOutlinePoints.reduce(
  29. (prev, curr) => {
  30. prev.x += curr.x;
  31. prev.y += curr.y;
  32. prev.z += curr.z;
  33. return prev;
  34. },
  35. { x: 0, y: 0, z: 0 }
  36. );
  37. const vectorAverage = { x: vectorSum.x / carOutlinePoints.length, y: vectorSum.y / carOutlinePoints.length, z: 0 };
  38. const scaledVectorAverage = { x: vectorAverage.x * X_SCALING_FACTOR, y: vectorAverage.y * Y_SCALING_FACTOR, z: 0 };
  39. const transform_x = scaledVectorAverage.x - vectorAverage.x;
  40. const transform_y = scaledVectorAverage.y - vectorAverage.y;
  41. const scaledAndTransformedPoints = carOutlinePoints.map(({ x, y, z }) => ({
  42. x: x * X_SCALING_FACTOR - transform_x,
  43. y: y * Y_SCALING_FACTOR - transform_y,
  44. z,
  45. }));
  46. return scaledAndTransformedPoints;
  47. })();
  48. export default React.memo<Props>(function PoseMarkers({ children, layerIndex }: Props): Node[] {
  49. const models = [];
  50. const otherMarkers = [];
  51. const arrowMarkers = [];
  52. children.forEach((marker, i) => {
  53. const { pose, settings, interactionData } = marker;
  54. if (settings?.addCarOutlineBuffer) {
  55. otherMarkers.push(
  56. <FilledPolygons layerIndex={layerIndex} key={`cruise-pose-${i}`}>
  57. {[
  58. {
  59. pose,
  60. interactionData,
  61. points: scaledCarOutlineBufferPoints,
  62. color: { r: 0.6666, g: 0.6666, b: 0.6666, a: 1 },
  63. },
  64. ]}
  65. </FilledPolygons>
  66. );
  67. }
  68. switch (settings?.modelType) {
  69. case "car-outline": {
  70. otherMarkers.push(
  71. <FilledPolygons layerIndex={layerIndex} key={`cruise-pose-${i}`}>
  72. {[
  73. {
  74. pose,
  75. interactionData,
  76. points: carOutlinePoints,
  77. color: { r: 0.3313, g: 0.3313, b: 0.3375, a: 1 },
  78. },
  79. ]}
  80. </FilledPolygons>
  81. );
  82. break;
  83. }
  84. case "car-model": {
  85. models.push(
  86. <CarModel layerIndex={layerIndex} key={i}>
  87. {{ pose, alpha: settings.alpha || 1, interactionData }}
  88. </CarModel>
  89. );
  90. break;
  91. }
  92. case "arrow":
  93. default: {
  94. if (settings && settings.color) {
  95. marker = { ...marker, color: settings.color };
  96. }
  97. if (settings && settings.size) {
  98. marker = {
  99. ...marker,
  100. scale: {
  101. x: settings.size.shaftWidth || marker.scale.x,
  102. y: settings.size.headWidth || marker.scale.y,
  103. z: settings.size.headLength || marker.scale.z,
  104. },
  105. };
  106. }
  107. const pos = pointToVec3(marker.pose.position);
  108. const orientation = orientationToVec4(marker.pose.orientation);
  109. const dir = vec3.transformQuat([0, 0, 0], [1, 0, 0], orientation);
  110. // the total length of the arrow is 4.7, we move the tail backwards by 0.88 (prev implementation)
  111. const tipPoint = vec3.scaleAndAdd([0, 0, 0], pos, dir, 3.82);
  112. const tailPoint = vec3.scaleAndAdd([0, 0, 0], pos, dir, -0.88);
  113. arrowMarkers.push({ ...marker, points: [vec3ToPoint(tailPoint), vec3ToPoint(tipPoint)] });
  114. break;
  115. }
  116. }
  117. });
  118. return [
  119. ...models,
  120. ...otherMarkers,
  121. <Arrows layerIndex={layerIndex} key="arrows">
  122. {arrowMarkers}
  123. </Arrows>,
  124. ];
  125. });