Skip to content
Snippets Groups Projects
projection_geometry.py 3.28 KiB
Newer Older
Simon Wittl's avatar
Simon Wittl committed
from __future__ import annotations

Simon Wittl's avatar
dev
Simon Wittl committed
import numpy as np

Simon Wittl's avatar
Simon Wittl committed
from rq_interfaces.msg import ProjectionGeometry, Projection
Simon Wittl's avatar
dev
Simon Wittl committed

class PyProjectionGeometry():
Simon Wittl's avatar
Simon Wittl committed
    def __init__(self, focal_spot_mm: np.ndarray, detector_postion_mm: np.ndarray, 
                 detector_orientation_quad: np.ndarray,
                 frame_id: str = 'object', focal_spot_orientation_quad: np.ndarray = np.array([0., 0., 0, 1.])
                 ) -> None:
Simon Wittl's avatar
dev
Simon Wittl committed
        self.focal_spot_mm = focal_spot_mm
        self.detector_postion_mm = detector_postion_mm
Simon Wittl's avatar
Simon Wittl committed
        self.focal_spot_orientation_quad = focal_spot_orientation_quad
Simon Wittl's avatar
dev
Simon Wittl committed
        self.detector_orientation_quad = detector_orientation_quad
        self.frame_id = frame_id

    @classmethod
    def dummy(cls):
        return cls(np.array([1., 0., 0]), 
                   np.array([-1., 0., 0]),
Simon Wittl's avatar
Simon Wittl committed
                   np.array([0., 0., 0, 1.]))
Simon Wittl's avatar
dev
Simon Wittl committed

    @classmethod
    def from_message(cls, msg: ProjectionGeometry):
        focal_spot_mm = np.array([msg.focal_spot_postion_mm.x,
                                  msg.focal_spot_postion_mm.y,
                                  msg.focal_spot_postion_mm.z,])

        detector_center_mm = np.array([msg.detector_postion_mm.x,
                                       msg.detector_postion_mm.y,
                                       msg.detector_postion_mm.z,])
        
        detector_orientation_quad = np.array([msg.detector_orientation_quad.x,
                                              msg.detector_orientation_quad.y,
                                              msg.detector_orientation_quad.z,
                                              msg.detector_orientation_quad.w])
Simon Wittl's avatar
Simon Wittl committed
        
        focal_spot_orientation = np.array([msg.focal_spot_orientation_quad.x,
                                           msg.focal_spot_orientation_quad.y,
                                           msg.focal_spot_orientation_quad.z,
                                           msg.focal_spot_orientation_quad.w])
        
Simon Wittl's avatar
dev
Simon Wittl committed
        frame_id = msg.header.frame_id

Simon Wittl's avatar
Simon Wittl committed
        return cls(focal_spot_mm, detector_center_mm, detector_orientation_quad, frame_id, focal_spot_orientation)
Simon Wittl's avatar
dev
Simon Wittl committed
    
    def as_message(self) -> ProjectionGeometry:
        message = ProjectionGeometry()

        message.focal_spot_postion_mm.x = self.focal_spot_mm[0]
        message.focal_spot_postion_mm.y = self.focal_spot_mm[1]
        message.focal_spot_postion_mm.z = self.focal_spot_mm[2]

        message.detector_postion_mm.x = self.detector_postion_mm[0]
        message.detector_postion_mm.y = self.detector_postion_mm[1]
        message.detector_postion_mm.z = self.detector_postion_mm[2]

Simon Wittl's avatar
Simon Wittl committed
        message.focal_spot_orientation_quad.x = self.focal_spot_orientation_quad[0]
        message.focal_spot_orientation_quad.y = self.focal_spot_orientation_quad[1]
        message.focal_spot_orientation_quad.z = self.focal_spot_orientation_quad[2]
        message.focal_spot_orientation_quad.w = self.focal_spot_orientation_quad[3]

Simon Wittl's avatar
dev
Simon Wittl committed
        message.detector_orientation_quad.x = self.detector_orientation_quad[0]
        message.detector_orientation_quad.y = self.detector_orientation_quad[1]
        message.detector_orientation_quad.z = self.detector_orientation_quad[2]
        message.detector_orientation_quad.w = self.detector_orientation_quad[3]

        message.header.frame_id = self.frame_id

        return message