inire/inire/tests/test_variable_grid.py

71 lines
2.1 KiB
Python

import unittest
from inire.geometry.primitives import Port
from inire.model import RoutingOptions, RoutingProblem
from inire.router._astar_types import AStarContext, SearchRunConfig
from inire.router._search import route_astar
from inire.router.cost import CostEvaluator
from inire.geometry.collision import RoutingWorld
class TestIntegerPorts(unittest.TestCase):
def setUp(self):
self.ce = RoutingWorld(clearance=2.0)
self.cost = CostEvaluator(self.ce)
self.bounds = (0, 0, 100, 100)
def _build_context(self) -> AStarContext:
return AStarContext(
self.cost,
RoutingProblem(bounds=self.bounds),
RoutingOptions(),
)
def _route(self, context: AStarContext, start: Port, target: Port):
return route_astar(
start,
target,
net_width=1.0,
context=context,
config=SearchRunConfig.from_options(context.options),
)
def test_route_reaches_integer_target(self):
context = self._build_context()
start = Port(0, 0, 0)
target = Port(12, 0, 0)
path = self._route(context, start, target)
self.assertIsNotNone(path)
last_port = path[-1].end_port
self.assertEqual(last_port.x, 12)
self.assertEqual(last_port.y, 0)
self.assertEqual(last_port.r, 0)
def test_port_constructor_rounds_to_integer_lattice(self):
context = self._build_context()
start = Port(0.0, 0.0, 0.0)
target = Port(12.3, 0.0, 0.0)
path = self._route(context, start, target)
self.assertIsNotNone(path)
self.assertEqual(target.x, 12)
last_port = path[-1].end_port
self.assertEqual(last_port.x, 12)
def test_half_step_inputs_use_integerized_targets(self):
context = self._build_context()
start = Port(0.0, 0.0, 0.0)
target = Port(7.5, 0.0, 0.0)
path = self._route(context, start, target)
self.assertIsNotNone(path)
self.assertEqual(target.x, 8)
last_port = path[-1].end_port
self.assertEqual(last_port.x, 8)
if __name__ == '__main__':
unittest.main()