1from __future__
import annotations
4from typing
import Union, TypeAlias, Any, cast
7import numpy.typing
as npt
9from ids_peak_icv
import lib_loader
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)
20PointArray: TypeAlias = npt.NDArray[np.object_]
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.
30 @ingroup ids_peak_icv_python_types_geometry
31 @since ids_peak_icv 1.1
38 Creates an instance of Polygon using an array of Points or floats ( [[x,y],...] ).
40 @param points: Array of Points or floats
41 @return Returns a polygon instance.
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.
47 @since ids_peak_icv 1.1
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
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)
67 "Both point coordinates have to be of type float.")
69 elif isinstance(point, np.ndarray):
71 c_points[index] = point_type(point[0], point[1])
74 "Point {index} has invalid length {length}".format(index=index, length=len(point)))
77 "Parameter points has to be a numpy array of Point objects or a two-dimensional numpy array.")
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)
84 return Polygon(_handle=handle, _point_type=point_type)
86 def __init__(self, *args: Any, **kwargs: Any) ->
None:
89 In order to create an empty Polygon create it with no arguments, in all other cases please refer to the
92 @since ids_peak_icv 1.1
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,
106 execute_and_map_return_codes(lib_loader.dll().peak_icv_Polygon_Create,
107 ctypes.pointer(polygon_handle))
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))
119 Return a point at index.
121 @since ids_peak_icv 1.1
123 return cast(Point, cast(object, self.
points[index]))
129 Get the points of the polygon.
131 @return List of Point objects representing the polygon's points.
133 @since ids_peak_icv 1.1
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))
142 if c_num_points.value == 0:
144 return cast(PointArray, self.
_points)
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)))
151 self.
_points = np.array([
Point(p.x, p.y)
for p
in c_points], dtype=Point)
153 return cast(PointArray, self.
_points)
159 Checks if a polygon is closed. A closed polygon contains the same point as first and last point.
161 @return True if closed, False if open.
163 @since ids_peak_icv 1.1
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))
169 return bool(is_closed.value)
173 lib_loader.dll().peak_icv_Polygon_Destroy(self.
_handle)
Represents a point in 2D space with x and y coordinates.
A polygon is a two-dimensional geometric shape that is made up of a finite number of straight line se...
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...
Point __getitem__(self, int index)
Return a point at index.
PointArray points(self)
Get the points of the polygon.
Polygon create_from_points(cls, Union[PointArray, np.ndarray] points)
Creates an instance of Polygon using an array of Points or floats ( [[x,y],...] ).
bool is_closed(self)
Checks if a polygon is closed.
Thrown when input parameters do not match the expected conditions.
Thrown when an operation cannot be performed due to logical constraints.
Thrown when a requested operation is not implemented or supported.
Thrown when a value is outside the allowed range.