Skip to content

Commit

Permalink
Merge pull request #30 from ch-sa/ch/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
ch-sa authored Oct 24, 2021
2 parents 36ca070 + f014f1b commit 6c5b9b9
Show file tree
Hide file tree
Showing 17 changed files with 493 additions and 587 deletions.
3 changes: 1 addition & 2 deletions config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,4 @@ viewing_precision = 2
near_plane = 0.1
far_plane = 300
; keep last perspective between point clouds
keep_perspective = False

keep_perspective = False
44 changes: 20 additions & 24 deletions labelCloud/control/alignmode.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
three points on the plane that serves as the ground. Then the old point cloud will be
saved up and the aligned current will overwrite the old.
"""
from typing import Union, TYPE_CHECKING
from typing import TYPE_CHECKING, Union

import numpy as np

import utils.oglhelper as ogl

from .pcd_manager import PointCloudManger

if TYPE_CHECKING:
from view.gui import GUI


class AlignMode:
def __init__(self, pcd_manager: PointCloudManger):
class AlignMode(object):
def __init__(self, pcd_manager: PointCloudManger) -> None:
self.pcd_manager = pcd_manager
self.view: Union[GUI, None] = None
self.activated = False
self.is_active = False
self.point_color = (1, 1, 0, 1)
self.area_color = (1, 1, 0, 0.6)
self.plane1 = None
Expand All @@ -28,40 +28,37 @@ def __init__(self, pcd_manager: PointCloudManger):
self.tmp_p2 = None
self.tmp_p3 = None

def set_view(self, view: "GUI"):
def set_view(self, view: "GUI") -> None:
self.view = view
self.view.glWidget.align_mode = self

def is_active(self):
return self.activated

def change_activation(self, force=None):
def change_activation(self, force=None) -> None:
if force is not None:
self.activated = force
elif self.activated:
self.activated = False
self.is_active = force
elif self.is_active:
self.is_active = False
self.reset()
else:
self.activated = True
self.is_active = True

if self.activated:
if self.is_active:
self.view.update_status(
"Select three points on the plane that should be the floor.",
"alignment",
)
self.view.action_alignpcd.setChecked(self.activated)
self.view.action_alignpcd.setChecked(self.is_active)
self.view.activate_draw_modes(
not self.activated
not self.is_active
) # Prevent bbox drawing while aligning
print("Alignmode was changed to %s!" % self.activated)
print(f"Alignmode was changed to {self.is_active}!")

def reset(self, points_only: bool = False):
def reset(self, points_only: bool = False) -> None:
self.plane1, self.plane2, self.plane3 = (None, None, None)
self.tmp_p2, self.tmp_p3 = (None, None)
if not points_only:
self.change_activation(force=False)

def register_point(self, new_point):
def register_point(self, new_point) -> None:
if self.plane1 is None:
self.plane1 = new_point
elif not self.plane2:
Expand All @@ -75,13 +72,13 @@ def register_point(self, new_point):
else:
print("Cannot register point.")

def register_tmp_point(self, new_tmp_point):
def register_tmp_point(self, new_tmp_point) -> None:
if self.plane1 and (not self.plane2):
self.tmp_p2 = new_tmp_point
elif self.plane2 and (not self.plane3):
self.tmp_p3 = new_tmp_point

def draw_preview(self):
def draw_preview(self) -> None:
if not self.plane3:
if self.plane1:
ogl.draw_points([self.plane1], color=self.point_color)
Expand All @@ -104,15 +101,14 @@ def draw_preview(self):
)

elif self.plane1 and self.plane2 and self.plane3:

ogl.draw_points(
[self.plane1, self.plane2, self.plane3], color=self.point_color
)
ogl.draw_triangles(
[self.plane1, self.plane2, self.plane3], color=self.area_color
)

def calculate_angles(self):
def calculate_angles(self) -> None:
# Calculate plane normal with self.plane1 as origin
plane_normal = np.cross(
np.subtract(self.plane2, self.plane1), np.subtract(self.plane3, self.plane1)
Expand Down
63 changes: 31 additions & 32 deletions labelCloud/control/bbox_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
Bounding Box Management: adding, selecting updating, deleting bboxes;
Possible Active Bounding Box Manipulations: rotation, translation, scaling
"""
from typing import TYPE_CHECKING, Union, List
from typing import TYPE_CHECKING, List, Optional, Union

import numpy as np


from utils import oglhelper
from model.bbox import BBox
from utils import oglhelper

from .config_manager import config

if TYPE_CHECKING:
Expand Down Expand Up @@ -46,66 +45,66 @@ def wrapper(*args, **kwargs):
return wrapper


class BoundingBoxController:
class BoundingBoxController(object):
STD_SCALING = config.getfloat("LABEL", "std_scaling")

def __init__(self):
def __init__(self) -> None:
self.view = None
self.bboxes = []
self.active_bbox_id = -1 # -1 means zero bboxes
self.pcdc = None

# GETTERS
def has_active_bbox(self):
def has_active_bbox(self) -> bool:
return 0 <= self.active_bbox_id < self.get_no_of_bboxes()

def get_active_bbox(self) -> Union[BBox, None]:
def get_active_bbox(self) -> Optional[BBox]:
if self.has_active_bbox():
return self.bboxes[self.active_bbox_id]
else:
return None

def get_bboxes(self):
def get_bboxes(self) -> List[BBox]:
return self.bboxes

def get_no_of_bboxes(self):
def get_no_of_bboxes(self) -> int:
return len(self.bboxes)

@has_active_bbox_decorator
def get_classname(self):
def get_classname(self) -> str:
return self.get_active_bbox().get_classname()

# SETTERS

def set_view(self, view: "GUI"):
def set_view(self, view: "GUI") -> None:
self.view = view

def add_bbox(self, bbox: BBox):
def add_bbox(self, bbox: BBox) -> None:
if isinstance(bbox, BBox):
self.bboxes.append(bbox)
self.set_active_bbox(self.bboxes.index(bbox))
self.view.update_status(
"Bounding Box added, it can now be corrected.", mode="correction"
)

def update_bbox(self, bbox_id: int, bbox: BBox):
def update_bbox(self, bbox_id: int, bbox: BBox) -> None:
if isinstance(bbox, BBox) and (0 <= bbox_id < len(self.bboxes)):
self.bboxes[bbox_id] = bbox
self.update_label_list()

def delete_bbox(self, bbox_id: int):
def delete_bbox(self, bbox_id: int) -> None:
if 0 <= bbox_id < len(self.bboxes):
del self.bboxes[bbox_id]
if bbox_id == self.active_bbox_id:
self.set_active_bbox(self.get_no_of_bboxes() - 1)
else:
self.update_label_list()

def delete_current_bbox(self):
def delete_current_bbox(self) -> None:
selected_item_id = self.view.label_list.currentRow()
self.delete_bbox(selected_item_id)

def set_active_bbox(self, bbox_id: int):
def set_active_bbox(self, bbox_id: int) -> None:
if 0 <= bbox_id < len(self.bboxes):
self.active_bbox_id = bbox_id
self.update_all()
Expand All @@ -116,31 +115,31 @@ def set_active_bbox(self, bbox_id: int):
self.deselect_bbox()

@has_active_bbox_decorator
def set_classname(self, new_class: str):
def set_classname(self, new_class: str) -> None:
self.get_active_bbox().set_classname(new_class)
self.update_label_list()

@has_active_bbox_decorator
def set_center(self, cx: float, cy: float, cz: float):
def set_center(self, cx: float, cy: float, cz: float) -> None:
self.get_active_bbox().center = (cx, cy, cz)

def set_bboxes(self, bboxes: List[BBox]):
def set_bboxes(self, bboxes: List[BBox]) -> None:
self.bboxes = bboxes
self.deselect_bbox()
self.update_label_list()

def reset(self):
def reset(self) -> None:
self.deselect_bbox()
self.set_bboxes([])

def deselect_bbox(self):
def deselect_bbox(self) -> None:
self.active_bbox_id = -1
self.update_all()
self.view.update_status("", mode="navigation")

# MANIPULATORS
@has_active_bbox_decorator
def update_position(self, axis: str, value: float):
def update_position(self, axis: str, value: float) -> None:
if axis == "pos_x":
self.get_active_bbox().set_x_translation(value)
elif axis == "pos_y":
Expand All @@ -151,7 +150,7 @@ def update_position(self, axis: str, value: float):
raise Exception("Wrong axis describtion.")

@has_active_bbox_decorator
def update_dimension(self, dimension: str, value: float):
def update_dimension(self, dimension: str, value: float) -> None:
if dimension == "length":
self.get_active_bbox().set_length(value)
elif dimension == "width":
Expand All @@ -162,7 +161,7 @@ def update_dimension(self, dimension: str, value: float):
raise Exception("Wrong dimension describtion.")

@has_active_bbox_decorator
def update_rotation(self, axis: str, value: float):
def update_rotation(self, axis: str, value: float) -> None:
if axis == "rot_x":
self.get_active_bbox().set_x_rotation(value)
elif axis == "rot_y":
Expand All @@ -174,7 +173,7 @@ def update_rotation(self, axis: str, value: float):

@only_zrotation_decorator
@has_active_bbox_decorator
def rotate_around_x(self, dangle: float = None, clockwise: bool = False):
def rotate_around_x(self, dangle: float = None, clockwise: bool = False) -> None:
dangle = dangle or config.getfloat("LABEL", "std_rotation")
if clockwise:
dangle *= -1
Expand All @@ -184,7 +183,7 @@ def rotate_around_x(self, dangle: float = None, clockwise: bool = False):

@only_zrotation_decorator
@has_active_bbox_decorator
def rotate_around_y(self, dangle: float = None, clockwise: bool = False):
def rotate_around_y(self, dangle: float = None, clockwise: bool = False) -> None:
dangle = dangle or config.getfloat("LABEL", "std_rotation")
if clockwise:
dangle *= -1
Expand All @@ -195,7 +194,7 @@ def rotate_around_y(self, dangle: float = None, clockwise: bool = False):
@has_active_bbox_decorator
def rotate_around_z(
self, dangle: float = None, clockwise: bool = False, absolute: bool = False
):
) -> None:
dangle = dangle or config.getfloat("LABEL", "std_rotation")
if clockwise:
dangle *= -1
Expand All @@ -210,7 +209,7 @@ def rotate_around_z(
@has_active_bbox_decorator
def rotate_with_mouse(
self, x_angle: float, y_angle: float
): # TODO: Make more intuitive
) -> None: # TODO: Make more intuitive
# Get bbox perspective
pcd_z_rotation = self.pcdc.get_pointcloud().rot_z
bbox_z_rotation = self.get_active_bbox().get_z_rotation()
Expand All @@ -224,7 +223,7 @@ def rotate_with_mouse(
self.rotate_around_z(x_angle)

@has_active_bbox_decorator
def translate_along_x(self, distance: float = None, left: bool = False):
def translate_along_x(self, distance: float = None, left: bool = False) -> None:
distance = distance or config.getfloat("LABEL", "std_translation")
if left:
distance *= -1
Expand All @@ -237,7 +236,7 @@ def translate_along_x(self, distance: float = None, left: bool = False):
)

@has_active_bbox_decorator
def translate_along_y(self, distance: float = None, forward: bool = False):
def translate_along_y(self, distance: float = None, forward: bool = False) -> None:
distance = distance or config.getfloat("LABEL", "std_translation")
if forward:
distance *= -1
Expand All @@ -250,7 +249,7 @@ def translate_along_y(self, distance: float = None, forward: bool = False):
)

@has_active_bbox_decorator
def translate_along_z(self, distance: float = None, down: bool = False):
def translate_along_z(self, distance: float = None, down: bool = False) -> None:
distance = distance or config.getfloat("LABEL", "std_translation")
if down:
distance *= -1
Expand Down
10 changes: 4 additions & 6 deletions labelCloud/control/config_manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Load configuration from .ini file."""
import configparser


import os
from typing import List

Expand All @@ -27,21 +25,21 @@ class ConfigManager(object):
PATH_TO_CONFIG = "config.ini"
PATH_TO_DEFAULT_CONFIG = "ressources/default_config.ini"

def __init__(self):
def __init__(self) -> None:
self.config = ExtendedConfigParser(comment_prefixes="/", allow_no_value=True)
self.read_from_file()

def read_from_file(self):
def read_from_file(self) -> None:
if os.path.isfile(ConfigManager.PATH_TO_CONFIG):
self.config.read(ConfigManager.PATH_TO_CONFIG)
else:
self.config.read(ConfigManager.PATH_TO_DEFAULT_CONFIG)

def write_into_file(self):
def write_into_file(self) -> None:
with open(ConfigManager.PATH_TO_CONFIG, "w") as configfile:
self.config.write(configfile, space_around_delimiters=True)

def reset_to_default(self):
def reset_to_default(self) -> None:
self.config.read(ConfigManager.PATH_TO_DEFAULT_CONFIG)

def get_file_settings(self, key: str) -> str:
Expand Down
Loading

0 comments on commit 6c5b9b9

Please sign in to comment.