Skip to content

Commit

Permalink
Merge pull request #94 from maniek2332/ft/bounding_boxes
Browse files Browse the repository at this point in the history
BoundingBox class, exposed bounding box on Node, Shape and Camera
  • Loading branch information
labuzm committed Sep 12, 2020
2 parents 8ede4a5 + d275aca commit 7fe6223
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/kaa/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(CYTHON_FILES
nodes.pxi
physics.pxi
scenes.pxi
views.pxi
vectors.pxi
colors.pxi
shapes.pxi
Expand Down
100 changes: 99 additions & 1 deletion src/kaa/geometry.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ from .kaacore.math cimport radians, degrees
from .kaacore.vectors cimport CDVec2
from .kaacore.geometry cimport (
CPolygonType, CAlignment, CTransformation, CDecomposedTransformation,
c_classify_polygon
CBoundingBox, c_classify_polygon
)
from .kaacore.hashing cimport c_calculate_hash


DEF TRANSFORMATION_FREELIST_SIZE = 32
DEF DECOMPOSED_TRANSFORMATION_FREELIST_SIZE = 32
DEF BOUNDING_BOX_FREELIST_SIZE = 32


class PolygonType(IntEnum):
Expand Down Expand Up @@ -185,3 +187,99 @@ def classify_polygon(points):
for pt in points:
c_points.push_back((<Vector>pt).c_vector)
return PolygonType(<uint32_t>c_classify_polygon(c_points))


@cython.freelist(BOUNDING_BOX_FREELIST_SIZE)
cdef class BoundingBox:
cdef CBoundingBox c_bounding_box

def __init__(self, double min_x, double min_y, double max_x, double max_y):
self.c_bounding_box = CBoundingBox(min_x, min_y, max_x, max_y)

@staticmethod
cdef BoundingBox create(const CBoundingBox& c_bounding_box):
cdef BoundingBox bounding_box = BoundingBox.__new__(BoundingBox)
bounding_box.c_bounding_box = c_bounding_box
return bounding_box

def __repr__(self):
return (
"BoundingBox(min_x={min_x}, min_y={min_y}, "
"max_x={max_x}, max_y={max_y})"
).format(
min_x=self.min_x, min_y=self.min_y,
max_x=self.max_x, max_y=self.max_y,
)

def __eq__(self, BoundingBox other):
return self.c_bounding_box == other.c_bounding_box

def __hash__(self):
return c_calculate_hash[CBoundingBox](self.c_bounding_box)

@staticmethod
def single_point(Vector point not None):
return BoundingBox.create(
CBoundingBox.single_point(point.c_vector)
)

@staticmethod
def from_points(list points):
cdef vector[CDVec2] c_points
c_points.reserve(len(points))
for v in points:
c_points.push_back((<Vector>v).c_vector)
return BoundingBox.create(
CBoundingBox.from_points(c_points)
)

@property
def min_x(self):
return self.c_bounding_box.min_x

@property
def max_x(self):
return self.c_bounding_box.max_x

@property
def min_y(self):
return self.c_bounding_box.min_y

@property
def max_y(self):
return self.c_bounding_box.max_y

@property
def is_nan(self):
return <bool>(self.c_bounding_box.is_nan())

def merge(self, BoundingBox bounding_box not None):
return BoundingBox.create(
self.c_bounding_box.merge(bounding_box.c_bounding_box)
)

def contains(self, Vector point not None):
return self.c_bounding_box.contains(point.c_vector)

def contains(self, BoundingBox bounding_box not None):
return self.c_bounding_box.contains(bounding_box.c_bounding_box)

def intersects(self, BoundingBox bounding_box not None):
return self.c_bounding_box.intersects(bounding_box.c_bounding_box)

def grow(self, Vector vector not None):
return BoundingBox.create(
self.c_bounding_box.grow(vector.c_vector)
)

@property
def center(self):
return Vector.from_c_vector(
self.c_bounding_box.center()
)

@property
def dimensions(self):
return Vector.from_c_vector(
self.c_bounding_box.dimensions()
)
2 changes: 1 addition & 1 deletion src/kaa/geometry.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ._kaa import (
Vector, Segment, Circle, Polygon, PolygonType, classify_polygon, Alignment,
Transformation,
Transformation, BoundingBox
)
2 changes: 2 additions & 0 deletions src/kaa/kaacore/camera.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .vectors cimport CDVec2
from .geometry cimport CBoundingBox
from .exceptions cimport raise_py_error


Expand All @@ -11,3 +12,4 @@ cdef extern from "kaacore/camera.h" nogil:
CDVec2 scale()
void scale(const CDVec2&)
CDVec2 unproject_position(const CDVec2& pos) except +raise_py_error
CBoundingBox visible_area_bounding_box()
27 changes: 27 additions & 0 deletions src/kaa/kaacore/geometry.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from libcpp.vector cimport vector
from libcpp cimport bool

from .vectors cimport CDVec2
from .exceptions cimport raise_py_error
Expand Down Expand Up @@ -50,3 +51,29 @@ cdef extern from "kaacore/geometry.h" nogil:

CPolygonType c_classify_polygon "kaacore::classify_polygon"(const vector[CDVec2]& points) \
except +raise_py_error

cdef cppclass CBoundingBox "kaacore::BoundingBox<double>":
double min_x
double min_y
double max_x
double max_y

CBoundingBox()
CBoundingBox(double min_x, double min_y, double max_x, double max_y)

bool operator==(const CBoundingBox&)

bool is_nan()
CBoundingBox merge(const CBoundingBox& other)
bool contains(const CDVec2 vector)
bool contains(const CBoundingBox& bbox)
bool intersects(const CBoundingBox& bbox)
CBoundingBox grow(const CDVec2 vector)
CDVec2 center()
CDVec2 dimensions()

@staticmethod
CBoundingBox single_point(const CDVec2 point)

@staticmethod
CBoundingBox from_points(const vector[CDVec2]& points)
4 changes: 3 additions & 1 deletion src/kaa/kaacore/nodes.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from libcpp.memory cimport unique_ptr
from libcpp.unordered_set cimport unordered_set

from .vectors cimport CDVec2, CColor
from .geometry cimport CAlignment, CTransformation
from .geometry cimport CAlignment, CTransformation, CBoundingBox
from .physics cimport CSpaceNode, CBodyNode, CHitboxNode
from .fonts cimport CTextNode
from .shapes cimport CShape
Expand Down Expand Up @@ -113,4 +113,6 @@ cdef extern from "kaacore/nodes.h" nogil:
bool indexable() except +raise_py_error
void indexable(const bool indexable) except +raise_py_error

CBoundingBox bounding_box() except +raise_py_error

CNodeOwnerPtr c_make_node "kaacore::make_node" (CNodeType) except +raise_py_error
4 changes: 3 additions & 1 deletion src/kaa/kaacore/shapes.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ from libcpp.vector cimport vector
from libcpp cimport bool

from .vectors cimport CDVec2
from .geometry cimport CTransformation
from .geometry cimport CTransformation, CBoundingBox
from .exceptions cimport raise_py_error


Expand Down Expand Up @@ -42,3 +42,5 @@ cdef extern from "kaacore/shapes.h" nogil:

CShape transform(const CTransformation& transformation) \
except +raise_py_error

CBoundingBox bounding_box() except +raise_py_error
4 changes: 4 additions & 0 deletions src/kaa/nodes.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ cdef class NodeBase:
def indexable(self, bool value):
self._get_c_node().indexable(value)

@property
def bounding_box(self):
return BoundingBox.create(self._get_c_node().bounding_box())


cdef class Node(NodeBase):
def __init__(self, **options):
Expand Down
4 changes: 4 additions & 0 deletions src/kaa/shapes.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ cdef class ShapeBase:
self.c_shape_ptr[0].transform(transformation.c_transformation)
)

@property
def bounding_box(self):
return BoundingBox.create(self.c_shape_ptr[0].bounding_box())

def __eq__(self, ShapeBase other):
return self.c_shape_ptr[0] == other.c_shape_ptr[0]

Expand Down
4 changes: 4 additions & 0 deletions src/kaa/views.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,7 @@ cdef class _Camera(_SceneResource):
return Vector.from_c_vector(
self._get_c_camera().unproject_position(position.c_vector)
)

@property
def visible_area_bounding_box(self):
return BoundingBox.create(self._get_c_camera().visible_area_bounding_box())

0 comments on commit 7fe6223

Please sign in to comment.