IDS peak ICV v1.1.0
Loading...
Searching...
No Matches
polygon.py
Go to the documentation of this file.
1from __future__ import annotations
2
3import ctypes
4from typing import Union, TypeAlias, Any, cast
5
6import numpy as np
7import numpy.typing as npt
8
9from ids_peak_icv import lib_loader
10from ids_peak_icv.exceptions import (NotSupportedException, NotPossibleException, OutOfRangeException,
11 MismatchException)
12from ids_peak_icv.backend.utils import (create_array, execute_and_map_return_codes,
13 check_init_for_classes_with_classmethods_only, map_point_type_to_enum,
14 map_point_type_to_native_type,
15 map_enum_to_point_type)
16from ids_peak_icv.backend.datatypes import peak_icv_polygon_handle, peak_icv_point_type, peak_icv_point_f
18
19# Note: We loose type info here, as it is stored as a pointer to a python object
20PointArray: TypeAlias = npt.NDArray[np.object_]
21
22
23class Polygon:
24 """!
25 @brief
26 A polygon is a two-dimensional geometric shape that is made up of a finite number of straight line
27 segments connected end-to-end to form an open or closed figure.
28 A polygon is represented by the points where the line segments meet.
29
30 @ingroup ids_peak_icv_python_types_geometry
31 @since ids_peak_icv 1.1
32 """
33
34 @classmethod
35 def create_from_points(cls, points: Union[PointArray, np.ndarray]) -> Polygon:
36 """!
37 @brief
38 Creates an instance of Polygon using an array of Points or floats ( [[x,y],...] ).
39
40 @param points: Array of Points or floats
41 @return Returns a polygon instance.
42
43 @exception NotPossibleException: If the given points is not a numpy array of Points or floats.
44 @exception MismatchException: If one of the coordinates in the numpy array is not a float.
45 @exception OutOfRangeException: If one or more points in the numpy array are not two-dimensional.
46
47 @since ids_peak_icv 1.1
48 """
49
50 size = len(points)
51
52 point_type = None
53 if size and ((hasattr(points[0], "x") and isinstance(getattr(points[0], "x"), float))
54 or isinstance(points[0][0], float)):
55 point_type = peak_icv_point_f
56 else:
57 raise NotSupportedException("Only the point type float is supported.")
58
59 native_type = map_point_type_to_native_type(point_type)
60 c_points = create_array(point_type, size)
61 for index, point in enumerate(points):
62 if isinstance(point, Point) and isinstance(points, np.ndarray):
63 if isinstance(point.x, native_type) and isinstance(point.y, native_type):
64 c_points[index] = point_type(point.x, point.y)
65 else:
67 "Both point coordinates have to be of type float.")
68
69 elif isinstance(point, np.ndarray):
70 if len(point) == 2:
71 c_points[index] = point_type(point[0], point[1])
72 else:
74 "Point {index} has invalid length {length}".format(index=index, length=len(point)))
75 else:
77 "Parameter points has to be a numpy array of Point objects or a two-dimensional numpy array.")
78
80 execute_and_map_return_codes(lib_loader.dll().peak_icv_Polygon_CreateFromPoints,
81 ctypes.pointer(handle), c_points, size,
82 map_point_type_to_enum(point_type).value)
83
84 return Polygon(_handle=handle, _point_type=point_type)
85
86 def __init__(self, *args: Any, **kwargs: Any) -> None:
87 """!
88 @brief
89 In order to create an empty Polygon create it with no arguments, in all other cases please refer to the
90 classmethods.
91
92 @since ids_peak_icv 1.1
93 """
94
95 self._points: PointArray | None = None
97 self._point_type = None
98
99 if len(args) or len(kwargs):
100 self._handle = check_init_for_classes_with_classmethods_only(self.__class__.__name__,
101 peak_icv_polygon_handle,
102 *args, **kwargs)
103 self._point_type = kwargs["_point_type"]
104 else:
105 polygon_handle = peak_icv_polygon_handle()
106 execute_and_map_return_codes(lib_loader.dll().peak_icv_Polygon_Create,
107 ctypes.pointer(polygon_handle))
108
109 self._handle = polygon_handle
110
111 c_point_type = ctypes.c_int()
112 execute_and_map_return_codes(lib_loader.dll().peak_icv_Polygon_GetPointType, self._handle,
113 ctypes.pointer(c_point_type))
114 self._point_type = map_enum_to_point_type(peak_icv_point_type(c_point_type.value))
115
116 def __getitem__(self, index: int) -> Point:
117 """!
118 @brief
119 Return a point at index.
120
121 @since ids_peak_icv 1.1
122 """
123 return cast(Point, cast(object, self.points[index]))
124
125 @property
126 def points(self) -> PointArray:
127 """!
128 @brief
129 Get the points of the polygon.
130
131 @return List of Point objects representing the polygon's points.
132
133 @since ids_peak_icv 1.1
134 """
135 if self._points is not None:
136 return self._points
137
138 c_num_points = ctypes.c_size_t()
139 execute_and_map_return_codes(lib_loader.dll().peak_icv_Polygon_GetPoints_GetCount,
140 self._handle, ctypes.pointer(c_num_points))
141
142 if c_num_points.value == 0:
143 self._points = np.ndarray(0)
144 return cast(PointArray, self._points)
145
146 c_points = create_array(cast(Any, self._point_type), c_num_points.value)
147 execute_and_map_return_codes(lib_loader.dll().peak_icv_Polygon_GetPoints,
148 self._handle, c_points, c_num_points.value *
149 ctypes.sizeof(cast(ctypes.Structure, self._point_type)))
150
151 self._points = np.array([Point(p.x, p.y) for p in c_points], dtype=Point)
152
153 return cast(PointArray, self._points)
154
155 @property
156 def is_closed(self) -> bool:
157 """!
158 @brief
159 Checks if a polygon is closed. A closed polygon contains the same point as first and last point.
160
161 @return True if closed, False if open.
162
163 @since ids_peak_icv 1.1
164 """
165 is_closed = ctypes.c_bool()
166 execute_and_map_return_codes(lib_loader.dll().peak_icv_Polygon_IsClosed,
167 self._handle, ctypes.pointer(is_closed))
168
169 return bool(is_closed.value)
170
171 def __del__(self) -> None:
172 if self._handle is not None:
173 lib_loader.dll().peak_icv_Polygon_Destroy(self._handle)
Represents a point in 2D space with x and y coordinates.
Definition point.py:6
A polygon is a two-dimensional geometric shape that is made up of a finite number of straight line se...
Definition polygon.py:23
None __init__(self, *Any args, **Any kwargs)
In order to create an empty Polygon create it with no arguments, in all other cases please refer to t...
Definition polygon.py:86
Point __getitem__(self, int index)
Return a point at index.
Definition polygon.py:116
PointArray points(self)
Get the points of the polygon.
Definition polygon.py:126
Polygon create_from_points(cls, Union[PointArray, np.ndarray] points)
Creates an instance of Polygon using an array of Points or floats ( [[x,y],...] ).
Definition polygon.py:35
bool is_closed(self)
Checks if a polygon is closed.
Definition polygon.py:156
Thrown when input parameters do not match the expected conditions.
Definition exceptions.py:54
Thrown when an operation cannot be performed due to logical constraints.
Definition exceptions.py:85
Thrown when a requested operation is not implemented or supported.
Definition exceptions.py:71
Thrown when a value is outside the allowed range.