geometry.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package rack
  2. // Footprint es la huella ocupada por un montaje en el grid del rack:
  3. // U 1-based desde el rail inferior, columnas 0-based.
  4. type Footprint struct {
  5. UStart int
  6. UHeight int
  7. ColStart int
  8. ColSpan int
  9. }
  10. // Overlaps informa si dos huellas comparten alguna celda del grid.
  11. func Overlaps(a, b Footprint) bool {
  12. return a.UStart < b.UStart+b.UHeight && b.UStart < a.UStart+a.UHeight &&
  13. a.ColStart < b.ColStart+b.ColSpan && b.ColStart < a.ColStart+a.ColSpan
  14. }
  15. // FitsRack informa si la huella cabe dentro del grid del rack.
  16. func FitsRack(rackUHeight int, f Footprint) bool {
  17. return f.UStart >= 1 && f.UHeight >= 1 && f.UStart+f.UHeight-1 <= rackUHeight &&
  18. f.ColStart >= 0 && f.ColSpan >= 1 && f.ColStart+f.ColSpan <= RackColumns
  19. }
  20. // CanPlace informa si la huella candidata cabe en el rack y no se solapa con
  21. // ninguna huella existente. El servidor lo re-valida al guardar: dos montajes
  22. // solapados en el mismo rack nunca se persisten. existing debe EXCLUIR la
  23. // huella del montaje que se está moviendo (un drag sin cambio de sitio sigue
  24. // siendo válido).
  25. func CanPlace(rackUHeight int, existing []Footprint, cand Footprint) bool {
  26. if !FitsRack(rackUHeight, cand) {
  27. return false
  28. }
  29. for _, e := range existing {
  30. if Overlaps(e, cand) {
  31. return false
  32. }
  33. }
  34. return true
  35. }
  36. // FindSlot aplica el snap del drop: busca primero en la U de destino y luego
  37. // camina hacia fuera (±1, ±2...). En cada U prueba primero la columna deseada
  38. // (clampada al grid) y luego el barrido de columnas de izquierda a derecha.
  39. // Si nada cabe, devuelve false y el caller muestra preview en rojo: un drop
  40. // imposible nunca colisiona en silencio.
  41. func FindSlot(rackUHeight int, existing []Footprint, dropU, dropCol, uHeight, colSpan int) (Footprint, bool) {
  42. if uHeight < 1 || colSpan < 1 {
  43. return Footprint{}, false
  44. }
  45. maxU := rackUHeight - uHeight + 1
  46. maxCol := RackColumns - colSpan
  47. if maxU < 1 || maxCol < 0 {
  48. return Footprint{}, false
  49. }
  50. col := dropCol
  51. if col < 0 {
  52. col = 0
  53. }
  54. if col > maxCol {
  55. col = maxCol
  56. }
  57. targetU := dropU
  58. if targetU < 1 {
  59. targetU = 1
  60. }
  61. if targetU > maxU {
  62. targetU = maxU
  63. }
  64. for offset := 0; offset < rackUHeight; offset++ {
  65. us := []int{targetU}
  66. if offset > 0 {
  67. us = []int{targetU - offset, targetU + offset}
  68. }
  69. for _, u := range us {
  70. if u < 1 || u > maxU {
  71. continue
  72. }
  73. if f, ok := tryAt(rackUHeight, existing, u, col, uHeight, colSpan); ok {
  74. return f, true
  75. }
  76. for c := 0; c <= maxCol; c++ {
  77. if c == col {
  78. continue
  79. }
  80. if f, ok := tryAt(rackUHeight, existing, u, c, uHeight, colSpan); ok {
  81. return f, true
  82. }
  83. }
  84. }
  85. }
  86. return Footprint{}, false
  87. }
  88. func tryAt(rackUHeight int, existing []Footprint, u, c, uHeight, colSpan int) (Footprint, bool) {
  89. f := Footprint{UStart: u, UHeight: uHeight, ColStart: c, ColSpan: colSpan}
  90. if !CanPlace(rackUHeight, existing, f) {
  91. return Footprint{}, false
  92. }
  93. return f, true
  94. }