From bb82c22b73e6028d1b97ceb3283a998d219e6c83 Mon Sep 17 00:00:00 2001 From: Rick Wierenga Date: Tue, 12 Nov 2024 18:08:14 -0800 Subject: [PATCH] fix: get_absolute_location includes anchor even if parent is None --- CHANGELOG.md | 1 + pylabrobot/resources/resource.py | 18 ++++++++++-------- pylabrobot/resources/resource_tests.py | 9 +++++++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd109b243c..4b094ddd32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -129,6 +129,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). - fix get_child_location for resources rotated by 180 degrees (https://github.com/PyLabRobot/pylabrobot/pull/269) - volume tracking on channel 1-n (https://github.com/PyLabRobot/pylabrobot/pull/273) - correct trash location on Vantage (https://github.com/PyLabRobot/pylabrobot/pull/285) +- `Resource.get_absolute_location` includes anchor even if parent is None ### Removed diff --git a/pylabrobot/resources/resource.py b/pylabrobot/resources/resource.py index 4cc7e4f734..31cce7b27e 100644 --- a/pylabrobot/resources/resource.py +++ b/pylabrobot/resources/resource.py @@ -212,22 +212,24 @@ def get_absolute_location(self, x: str = "l", y: str = "f", z: str = "b") -> Coo """ assert self.location is not None, f"Resource {self.name} has no location." + + rotated_anchor = Coordinate( + *matrix_vector_multiply_3x3( + self.get_absolute_rotation().get_rotation_matrix(), + self.get_anchor(x=x, y=y, z=z).vector(), + ) + ) + if self.parent is None: - return self.location - parent_pos = self.parent.get_absolute_location() + return self.location + rotated_anchor + parent_pos = self.parent.get_absolute_location() rotated_location = Coordinate( *matrix_vector_multiply_3x3( self.parent.get_absolute_rotation().get_rotation_matrix(), self.location.vector(), ) ) - rotated_anchor = Coordinate( - *matrix_vector_multiply_3x3( - self.get_absolute_rotation().get_rotation_matrix(), - self.get_anchor(x=x, y=y, z=z).vector(), - ) - ) return parent_pos + rotated_location + rotated_anchor def _get_rotated_corners(self) -> List[Coordinate]: diff --git a/pylabrobot/resources/resource_tests.py b/pylabrobot/resources/resource_tests.py index d7a5f5ea9c..f5bf83a991 100644 --- a/pylabrobot/resources/resource_tests.py +++ b/pylabrobot/resources/resource_tests.py @@ -1,5 +1,3 @@ -"""Tests for Resource""" - import math import unittest import unittest.mock @@ -138,6 +136,13 @@ def test_get_absolute_location_with_anchor(self): Coordinate(20, 15, 20), ) + single = Resource("single", size_x=5, size_y=5, size_z=5) + single.location = Coordinate.zero() + self.assertEqual( + single.get_absolute_location(x="right", y="front", z="top"), + Coordinate(5, 0, 5), + ) + def test_unassign_child(self): deck = Deck() parent = Resource("parent", size_x=10, size_y=10, size_z=10)