Update sources and objects to new position system

This commit is contained in:
Pim Nelissen
2026-02-10 11:24:40 +01:00
parent a1acf95004
commit 9c1b97d912
2 changed files with 24 additions and 23 deletions

View File

@ -1,36 +1,42 @@
import math
from typing import Self from typing import Self
import numpy as np
class BaseObject: class BaseObject:
def __init__( def __init__(
self, self,
x: float, pos: tuple[float, float, float],
y: float,
z: float,
name: str = "Unnamed object", name: str = "Unnamed object",
color: str = 'grey'): color: str = 'grey'):
""" """
A generic object. A generic object.
Args: Args:
x (float): X coordinate. pos (tuple[float, float, float]): Position vector (x,y,z).
y (float): Y coordinate.
z (float): Z coordinate.
name (str, optional): Name for the object. name (str, optional): Name for the object.
Defaults to "Unnamed object". Defaults to "Unnamed object".
color (str, optional): Matplotlib compatible color string. color (str, optional): Matplotlib compatible color string.
Defaults to "red". Defaults to "red".
""" """
self.x = x if len(pos) != 3:
self.y = y raise ValueError("Position must be tuple of length 3 (x,y,z).")
self.z = z self.pos = pos
self.name = name self.name = name
self.color = color self.color = color
def distance_to(self, other: Self) -> float: def distance_to(self, other: Self | tuple) -> float:
return math.dist( if isinstance(other, tuple) and len(other) == 3:
(self.x, self.y, self.z), r = np.linalg.norm(
(other.x, other.y, other.z), np.subtract(self.pos, other)
) )
else:
try:
r = np.linalg.norm(
np.subtract(self.pos, other.pos)
)
except AttributeError as e:
raise e("other must be an object in the world \
or a position tuple (x,y,z).")
return r

View File

@ -11,9 +11,7 @@ class PointSource(BaseObject):
def __init__( def __init__(
self, self,
x: float, pos: tuple,
y: float,
z: float,
activity: int, activity: int,
isotope: Isotope, isotope: Isotope,
name: str | None = None, name: str | None = None,
@ -21,9 +19,7 @@ class PointSource(BaseObject):
"""A point source. """A point source.
Args: Args:
x (float): X coordinate. pos (tuple): a position vector of length 3 (x,y,z).
y (float): Y coordinate.
z (float): Z coordinate.
activity (int): Activity A in MBq. activity (int): Activity A in MBq.
isotope (Isotope): The isotope. isotope (Isotope): The isotope.
name (str | None, optional): Can give the source a unique name. name (str | None, optional): Can give the source a unique name.
@ -40,11 +36,10 @@ class PointSource(BaseObject):
if name is None: if name is None:
name = f"Source {self.id}" name = f"Source {self.id}"
super().__init__(x, y, z, name, color) super().__init__(pos, name, color)
self.activity = activity self.activity = activity
self.isotope = isotope self.isotope = isotope
self.color = color
logger.debug(f"Source created: {self.name}") logger.debug(f"Source created: {self.name}")