Skip to content
Snippets Groups Projects
region_of_intrest.py 2.43 KiB
Newer Older
Simon Wittl's avatar
dev
Simon Wittl committed
import numpy as np

from rq_interfaces.msg import RegionOfIntrest
Simon Wittl's avatar
Simon Wittl committed
from visualization_msgs.msg import Marker
Simon Wittl's avatar
dev
Simon Wittl committed


class PyRegionOfIntrest():
Simon Wittl's avatar
Simon Wittl committed
    def __init__(self, center_points_mm: np.ndarray, dimensions_mm: np.ndarray, frame_id: str = 'object', 
                 resolution_mm: np.ndarray = np.array([0.1, 0.1, 0.1])):
        self.center_points_mm = center_points_mm
        self.dimensions_mm = dimensions_mm
Simon Wittl's avatar
dev
Simon Wittl committed
        self.frame_id = frame_id
Simon Wittl's avatar
Simon Wittl committed
        self.resolution_mm = resolution_mm
Simon Wittl's avatar
dev
Simon Wittl committed

    @classmethod
    def dummy(cls):
Simon Wittl's avatar
Simon Wittl committed
        return cls((np.random.random((3, )) - 0.5) * 20., 
                   (np.random.random((3, )) - 0.5) * 10.)
Simon Wittl's avatar
dev
Simon Wittl committed

    @classmethod
    def from_message(cls, msg: RegionOfIntrest):
Simon Wittl's avatar
Simon Wittl committed
        center_points_mm = list()
        dimensions_mm = list()

        for roi in msg.region_of_intrest_stack.markers:
            roi: Marker
            center_points_mm.append(
                np.array([roi.pose.position.x,
                          roi.pose.position.y,
                          roi.pose.position.z]))
            
            dimensions_mm.append(
                np.array([roi.scale.x,
                          roi.scale.y,
                          roi.scale.z]))
            
            frame = roi.header.frame_id
        resolution_mm = np.array([msg.resolution.x,
                                  msg.resolution.y,
                                  msg.resolution.z])
Simon Wittl's avatar
dev
Simon Wittl committed
        
Simon Wittl's avatar
Simon Wittl committed
        return cls(np.array(center_points_mm), np.array(dimensions_mm), frame, resolution_mm)
    
    @property
    def number_of_rois(self) -> int:
        return self.center_points_mm.shape[0]
Simon Wittl's avatar
dev
Simon Wittl committed
    
    def as_message(self) -> RegionOfIntrest:
        message = RegionOfIntrest()
Simon Wittl's avatar
Simon Wittl committed
        roi_list = list()
Simon Wittl's avatar
dev
Simon Wittl committed

Simon Wittl's avatar
Simon Wittl committed
        for i in range(self.number_of_rois):
            roi = Marker()
Simon Wittl's avatar
dev
Simon Wittl committed

Simon Wittl's avatar
Simon Wittl committed
            roi.pose.position.x = self.center_points_mm[i][0]
            roi.pose.position.y = self.center_points_mm[i][1]
            roi.pose.position.z = self.center_points_mm[i][2]

            roi.scale.x = self.dimensions_mm[i][0]
            roi.scale.y = self.dimensions_mm[i][1]
            roi.scale.z = self.dimensions_mm[i][2]

            roi.header.frame_id = self.frame_id
Simon Wittl's avatar
Simon Wittl committed

            roi_list.append(roi)            
Simon Wittl's avatar
Simon Wittl committed
        
Simon Wittl's avatar
Simon Wittl committed
        message.region_of_intrest_stack.markers = roi_list
Simon Wittl's avatar
dev
Simon Wittl committed

Simon Wittl's avatar
Simon Wittl committed
        message.resolution.x = self.resolution_mm[0]
        message.resolution.x = self.resolution_mm[1]
        message.resolution.x = self.resolution_mm[2]
Simon Wittl's avatar
dev
Simon Wittl committed

        return message