outofroad.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package main
  2. import (
  3. "cicv-data-closedloop/common/entity"
  4. "cicv-data-closedloop/pjisuv_msgs"
  5. "fmt"
  6. "math"
  7. )
  8. func Topic() string {
  9. return "/cicv_location"
  10. }
  11. func Label() string {
  12. return "outofroad"
  13. }
  14. type Point struct {
  15. X, Y float64
  16. }
  17. func getVehicleCorners(x0, y0, D1, D2, D3, h0 float64) []pjisuv_msgs.Point64 {
  18. h0Rad := h0 * math.Pi / 180 // 转换角度到弧度
  19. cosH0, sinH0 := math.Cos(h0Rad), math.Sin(h0Rad)
  20. // 车辆四个角点相对于后轴中心点的坐标(局部坐标系)
  21. cornersLocal := []Point{
  22. {D1, D3}, // 前左角
  23. {D1, -D3}, // 前右角
  24. {-D2, D3}, // 后左角
  25. {-D2, -D3}, // 后右角
  26. }
  27. // 旋转矩阵并计算全局坐标
  28. rotationMatrix := [2][2]float64{{cosH0, -sinH0}, {sinH0, cosH0}}
  29. cornersGlobal := make([]pjisuv_msgs.Point64, len(cornersLocal))
  30. for i, corner := range cornersLocal {
  31. cornersGlobal[i].X = corner.X*rotationMatrix[0][0] + corner.Y*rotationMatrix[1][0] + x0
  32. cornersGlobal[i].Y = corner.X*rotationMatrix[0][1] + corner.Y*rotationMatrix[1][1] + y0
  33. }
  34. return cornersGlobal
  35. }
  36. func ccw(A, B, C pjisuv_msgs.Point64) bool {
  37. return (C.Y-A.Y)*(B.X-A.X) > (C.X-A.X)*(B.Y-A.Y)
  38. }
  39. func lineIntersect(A, B, C, D pjisuv_msgs.Point64) bool {
  40. return ccw(A, C, D) != ccw(B, C, D) && ccw(A, B, C) != ccw(A, B, D)
  41. }
  42. func polygonLineIntersect(polygon []pjisuv_msgs.Point64, A, B pjisuv_msgs.Point64) bool {
  43. for i := 0; i < len(polygon)-1; i++ {
  44. C := polygon[i]
  45. D := polygon[(i+1)%len(polygon)]
  46. if lineIntersect(A, B, C, D) {
  47. return true
  48. }
  49. }
  50. return false
  51. }
  52. func Rule(data *pjisuv_msgs.PolygonStamped, param entity.PjisuvParam) string {
  53. Points := data.Polygon.Points
  54. D1, D2, D3 := 3.813, 0.952, 1.086
  55. corners := getVehicleCorners(param.PositionXOfCicvLocation, param.PositionYOfCicvLocation, D1, D2, D3, param.YawOfCicvLocation)
  56. for i := 0; i < len(Points)-1; i++ {
  57. A := Points[i]
  58. B := Points[i+1]
  59. if polygonLineIntersect(corners, A, B) {
  60. fmt.Println("outofroad")
  61. return
  62. }
  63. }
  64. }