Skip to content

Commit 4fd38ad

Browse files
author
Yiannis Gatsoulis
committed
regional algebra 'ra' new QSR
1 parent fa31f1c commit 4fd38ad

File tree

5 files changed

+86
-4
lines changed

5 files changed

+86
-4
lines changed

qsr_lib/scripts/example_extended.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def pretty_print_world_qsr_trace(which_qsr, qsrlib_response_message):
2727

2828

2929
if __name__ == "__main__":
30-
options = ["rcc2", "rcc3", "rcc8", "cardir", "qtcbs", "qtccs", "qtcbcs", "argd", "argprobd", "mos", "multiple"]
30+
options = sorted(QSRlib().qsrs_registry.keys()) + ["multiple"]
3131
multiple = options[:]; multiple.remove("multiple"); multiple.remove("argd"); multiple.remove("argprobd")
3232

3333
parser = argparse.ArgumentParser()
@@ -50,7 +50,7 @@ def pretty_print_world_qsr_trace(which_qsr, qsrlib_response_message):
5050

5151
dynamic_args = {}
5252

53-
if which_qsr == "rcc3" or which_qsr == "rcc2":
53+
if which_qsr in ["rcc2", "rcc3", "ra"]:
5454
dynamic_args = {which_qsr: {"quantisation_factor": args.quantisation_factor}}
5555
o1 = [Object_State(name="o1", timestamp=0, x=1., y=1., xsize=5., ysize=8.),
5656
Object_State(name="o1", timestamp=1, x=1., y=2., xsize=5., ysize=8.),

qsr_lib/src/qsrlib_io/world_trace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def return_bounding_box_2d(self, xsize_minimal=0, ysize_minimal=0):
128128
:param xsize_minimal: If object has no x-size (i.e. simply a point) then compute bounding box based on this minimal x-size.
129129
:type xsize_minimal: int or float
130130
:param ysize_minimal: If object has no y-size (i.e. simply a point) then compute bounding box based on this minimal y-size.
131-
:return: The coordinates of the upper-left and bottom-right corners of the bounding box.
131+
:return: The coordinates of the first and third corner of the bounding box.
132132
:rtype: list
133133
"""
134134
xsize = xsize_minimal if isnan(self.xsize) else self.xsize

qsr_lib/src/qsrlib_qsrs/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from qsr_arg_prob_relations_distance import QSR_Arg_Prob_Relations_Distance
1010
from qsr_moving_or_stationary import QSR_Moving_or_Stationary
1111
from qsr_new_mwe import QSR_MWE
12+
from qsr_ra import QSR_RA
1213

1314
# register new qsrs by class name below
1415
qsrs_registry = (QSR_RCC2_Rectangle_Bounding_Boxes_2D,
@@ -21,4 +22,5 @@
2122
QSR_Arg_Relations_Distance,
2223
QSR_Arg_Prob_Relations_Distance,
2324
QSR_Moving_or_Stationary,
24-
QSR_MWE)
25+
QSR_MWE,
26+
QSR_RA)

qsr_lib/src/qsrlib_qsrs/qsr_abstractclass.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class QSR_Abstractclass(object):
1515
def __init__(self):
1616
"""Constructor."""
1717
self._dtype_map = {"points": self._return_points,
18+
"bounding_boxes": self._return_bounding_boxes_2d, # todo this is to handle 2D/3D and needs its own function
1819
"bounding_boxes_2d": self._return_bounding_boxes_2d}
1920
"""dict: Mapping of _dtype to methods."""
2021

qsr_lib/src/qsrlib_qsrs/qsr_ra.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import print_function, division
3+
from numpy import isnan
4+
from qsrlib_qsrs.qsr_dyadic_abstractclass import QSR_Dyadic_1t_Abstractclass
5+
6+
7+
class QSR_RA(QSR_Dyadic_1t_Abstractclass):
8+
"""Regional Algebra.
9+
10+
Members:
11+
* _unique_id: "ra"
12+
* _all_possible_relations: ("<", ">", "m", "mi", "o", "oi", "s", "si", "d", "di", "f", "fi", "=")
13+
* _dtype: "bounding_boxes"
14+
15+
Some explanation about the QSR. Maybe a reference if it exists.
16+
"""
17+
18+
_unique_id = "ra"
19+
"""str: Unique identifier name of the QSR."""
20+
21+
_all_possible_relations = ("<", ">", "m", "mi", "o", "oi", "s", "si", "d", "di", "f", "fi", "=")
22+
"""tuple: All possible relations of the QSR."""
23+
24+
_dtype = "bounding_boxes"
25+
"""str: On what kind of data the QSR works with."""
26+
27+
_inverse_map = {"<": ">", "m": "mi", "o": "oi", "s": "si", "d": "di", "f": "fi",
28+
">": "<", "mi": "m", "o1": "o", "si": "s", "di": "d", "fi": "f"}
29+
"""dict: Inverse relations"""
30+
31+
def __init__(self):
32+
"""Constructor.
33+
34+
:return:
35+
"""
36+
super(QSR_RA, self).__init__()
37+
38+
def _compute_qsr(self, bb1, bb2, qsr_params, **kwargs):
39+
"""Compute QSR value.
40+
41+
:param bb1: First object's bounding box.
42+
:type bb2: tuple or list
43+
:param bb2: Second object's bounding box.
44+
:type bb2: tuple or list
45+
:param qsr_params: QSR specific parameters passed in `dynamic_args`.
46+
:type qsr_params: dict
47+
:param kwargs: Optional further arguments.
48+
:return: The computed QSR value: two/three comma separated Allen relations for 2D/3D.
49+
:rtype: str
50+
"""
51+
if len(bb1) == 4 and len(bb2) == 4: # 2D version
52+
return ",".join([self.__allen((bb1[0], bb1[2]), (bb2[0], bb2[2])),
53+
self.__allen((bb1[1], bb1[3]), (bb2[1], bb2[3]))])
54+
elif len(bb1) == 6 and len(bb2) == 6: # 3D version
55+
return ",".join([self.__allen((bb1[0], bb1[3]), (bb2[0], bb2[3])),
56+
self.__allen((bb1[1], bb1[4]), (bb2[1], bb2[4])),
57+
self.__allen((bb1[2], bb1[5]), (bb2[2], bb2[5]))])
58+
else:
59+
raise ValueError("bb1 and bb2 must have length of 4 (2D) or 6 (3D)")
60+
61+
def __allen(self, i1, i2):
62+
if isnan(i1).any() or isnan(i2).any(): # nan values cause dragons
63+
raise ValueError("illegal 'nan' values found")
64+
65+
if i1[1] < i2[0]:
66+
return "<"
67+
if i1[1] == i2[0]:
68+
return "m"
69+
if i1[0] < i2[0] < i1[1] and i2[0] < i1[1] < i2[1]:
70+
return "o"
71+
if i1[0] == i2[0] and i1[1] < i2[1]:
72+
return "s"
73+
if i2[0] < i1[0] < i2[1] and i2[0] < i1[1] < i2[1]:
74+
return "d"
75+
if i2[0] < i1[0] < i2[1] and i1[1] == i2[1]:
76+
return "f"
77+
if i1[0] == i2[0] and i1[1] == i2[1]:
78+
return "="
79+
return self._inverse_map[self.__allen(i2, i1)]

0 commit comments

Comments
 (0)