irsim.util.util#
Functions#
|
Check whether a file exists and return its absolute path. |
|
|
|
The function WrapToPi transforms an angle in radians to the range [-pi, pi]. |
|
The function WrapTo2Pi transforms an angle in radians to the range [0, 2pi]. |
|
Transform an angle to a defined range, with length of 2*pi. |
|
Convert input to a list with a specific length. |
|
Convert input to a list with a specific length for dictionaries. |
|
Check if a list contains only dictionaries. |
|
Check if a list contains only numbers. |
|
Check if a list contains lists. |
|
Check if a list does not contain lists. |
|
Calculate the relative position and angle between two points. |
|
Get rotation and translation matrices from state. |
|
Transform a point using a state. |
|
Get affine transform parameters from state. |
|
Transform geometry using a state. |
|
Transform vertices using a state. |
|
Convert omnidirectional velocity to differential velocity. |
|
Convert differential velocity to omnidirectional velocity. |
|
Decorator to measure function execution time. |
|
Decorator to measure function execution time with instance attribute check. |
|
Compute the cross product of vectors OA and OB. |
|
Determine if the polygon is convex and return the order (CW or CCW). |
|
Generate inequality constraints for a convex polygon. |
|
Compute the distance between two points. |
|
|
|
Generate a random point within a range. |
|
Returns True if 'data' is a non-empty list of lists (or tuples), indicating a 2D structure. |
Module Contents#
- irsim.util.util.file_check(file_name: str | None, root_path: str | None = None) str | None[source]#
Check whether a file exists and return its absolute path.
- Parameters:
file_name (str) – Name of the file to check.
root_path (str, optional) – Root path to use if the file is not found.
- Returns:
Absolute path of the file if found.
- Return type:
str
- Raises:
FileNotFoundError – If the file is not found.
- irsim.util.util.WrapToPi(rad: float, positive: bool = False) float[source]#
The function WrapToPi transforms an angle in radians to the range [-pi, pi].
- Parameters:
rad (float) – Angle in radians. The rad parameter in the WrapToPi function represents an angle in radians that you want to transform to the range [-π, π]. The function ensures that the angle is within this range by wrapping it around if it exceeds the bounds.
positive (bool) – Whether to return the positive value of the angle. Useful for angles difference.
- Returns:
The function WrapToPi(rad) returns the angle rad wrapped to the range [-pi, pi].
- irsim.util.util.WrapTo2Pi(rad: float) float[source]#
The function WrapTo2Pi transforms an angle in radians to the range [0, 2pi].
- Parameters:
rad (float) – Angle in radians. The rad parameter in the WrapTo2Pi function represents an angle in radians that you want to transform to the range [0, 2pi]. The function ensures that the angle is within this range by wrapping it around if it exceeds the bounds.
- Returns:
The function WrapTo2Pi(rad) returns the angle rad wrapped to the range [0, 2pi].
- irsim.util.util.WrapToRegion(rad: float, range: list[float]) float[source]#
Transform an angle to a defined range, with length of 2*pi.
- Parameters:
rad (float) – Angle in radians.
range (list) – List defining the range [min, max].
- Returns:
Wrapped angle.
- Return type:
float
- irsim.util.util.convert_list_length(input_data: list[Any], number: int = 0) list[Any][source]#
Convert input to a list with a specific length.
- Parameters:
input_data – Data to convert.
number (int) – Desired length.
- Returns:
Converted list.
- Return type:
list
- irsim.util.util.convert_list_length_dict(input_data: list[Any], number: int = 0) list[Any][source]#
Convert input to a list with a specific length for dictionaries.
- Parameters:
input_data – Data to convert.
number (int) – Desired length.
- Returns:
Converted list.
- Return type:
list
- irsim.util.util.is_list_of_dicts(lst: Any) bool[source]#
Check if a list contains only dictionaries.
- Parameters:
lst (list) – List to check.
- Returns:
True if all elements are dictionaries, False otherwise.
- Return type:
bool
- irsim.util.util.is_list_of_numbers(lst: Any) bool[source]#
Check if a list contains only numbers.
- Parameters:
lst (list) – List to check.
- Returns:
True if all elements are numbers, False otherwise.
- Return type:
bool
- irsim.util.util.is_list_of_lists(lst: Any) bool[source]#
Check if a list contains lists.
- Parameters:
lst (list) – List to check.
- Returns:
True if any element is a list, False otherwise.
- Return type:
bool
- irsim.util.util.is_list_not_list_of_lists(lst: Any) bool[source]#
Check if a list does not contain lists.
- Parameters:
lst (list) – List to check.
- Returns:
True if no elements are lists, False otherwise.
- Return type:
bool
- irsim.util.util.relative_position(position1: numpy.ndarray, position2: numpy.ndarray, topi: bool = True) tuple[float, float][source]#
Calculate the relative position and angle between two points.
- Parameters:
position1 (np.array) – First position [x, y] (2x1).
position2 (np.array) – Second position [x, y] (2x1).
topi (bool) – Whether to wrap angle to [-pi, pi] (default True).
- Returns:
Distance and angle (radians).
- Return type:
tuple
- irsim.util.util.get_transform(state: numpy.ndarray) tuple[numpy.ndarray, numpy.ndarray][source]#
Get rotation and translation matrices from state.
- Parameters:
state (np.array) – State [x, y, theta] (3x1) or [x, y] (2x1).
- Returns:
Translation vector and rotation matrix.
- Return type:
tuple
- irsim.util.util.transform_point_with_state(point: numpy.ndarray, state: numpy.ndarray) numpy.ndarray[source]#
Transform a point using a state.
- Parameters:
point (np.array) – Point [x, y, theta] (3x1).
state (np.array) – State [x, y, theta] (3x1).
- Returns:
Transformed point (2x1).
- Return type:
np.array
- irsim.util.util.get_affine_transform(state: numpy.ndarray) list[float][source]#
Get affine transform parameters from state.
- Parameters:
state (np.array) – State [x, y, theta] (3x1).
- Returns:
Affine transform parameters.
- Return type:
list
- irsim.util.util.geometry_transform(geometry: Any, state: numpy.ndarray) Any[source]#
Transform geometry using a state.
- Parameters:
geometry – Shapely geometry to transform.
state (np.array or sequence of 3 floats) – [xoff, yoff, theta]
- Returns:
Transformed geometry.
shapely expects [a, b, d, e, xoff, yoff] for: x’ = a*x + b*y + xoff y’ = d*x + e*y + yoff
- irsim.util.util.vertices_transform(vertices: numpy.ndarray, state: numpy.ndarray) numpy.ndarray | None[source]#
Transform vertices using a state.
- Parameters:
vertices (np.array) – Vertices of the object. (2xN)
state (np.array) – State [x, y, theta] (3x1).
- Returns:
Transformed vertices.
- Return type:
np.array
- irsim.util.util.omni_to_diff(state_ori: float, vel_omni: numpy.ndarray, w_max: float = 1.5, guarantee_time: float = 0.2, tolerance: float = 0.1, mini_speed: float = 0.02) numpy.ndarray[source]#
Convert omnidirectional velocity to differential velocity.
- Parameters:
state_ori (float) – Orientation angle.
vel_omni (np.array) – Omnidirectional velocity [vx, vy] (2x1).
w_max (float) – Maximum angular velocity.
guarantee_time (float) – Time to guarantee velocity.
tolerance (float) – Angular tolerance.
mini_speed (float) – Minimum speed threshold.
- Returns:
Differential velocity [linear, angular] (2x1).
- Return type:
np.array
- irsim.util.util.diff_to_omni(state_ori: float, vel_diff: numpy.ndarray) numpy.ndarray[source]#
Convert differential velocity to omnidirectional velocity.
- Parameters:
state_ori (float) – Orientation angle.
vel_diff (np.array) – Differential velocity [linear, angular] (2x1).
- Returns:
Omnidirectional velocity [vx, vy] (2x1).
- Return type:
np.array
- irsim.util.util.time_it(name: str = 'Function') Any[source]#
Decorator to measure function execution time.
- Parameters:
name (str) – Function name for logging (default “Function”).
print (bool) – Whether to print execution time (default True).
- Returns:
Wrapped function with timing.
- Return type:
function
- irsim.util.util.time_it2(name: str = 'Function') Any[source]#
Decorator to measure function execution time with instance attribute check.
- Parameters:
name (str) – Function name for logging (default “Function”).
- Returns:
Wrapped function with timing.
- Return type:
function
- irsim.util.util.cross_product(o: list[float], a: list[float], b: list[float]) float[source]#
Compute the cross product of vectors OA and OB.
- Parameters:
o (array-like) – Points representing vectors.
a (array-like) – Points representing vectors.
b (array-like) – Points representing vectors.
- Returns:
Cross product value.
- Return type:
float
- irsim.util.util.is_convex_and_ordered(points: numpy.ndarray) tuple[bool, str | None][source]#
Determine if the polygon is convex and return the order (CW or CCW).
- Parameters:
points (np.ndarray) – A 2xN NumPy array representing the vertices of the polygon.
- Returns:
- A tuple where the first element is True if the polygon is convex,
and the second element is ‘CW’ or ‘CCW’ based on the order. If not convex, returns (False, None).
- Return type:
(bool, str)
- irsim.util.util.gen_inequal_from_vertex(vertex: numpy.ndarray)[source]#
Generate inequality constraints for a convex polygon.
- Parameters:
vertex (np.array) – Vertices of the polygon (2xN).
- Returns:
G matrix and h vector for the inequality Gx <= h.
- Return type:
tuple
- irsim.util.util.distance(point1: list[float] | numpy.ndarray, point2: list[float] | numpy.ndarray) float[source]#
Compute the distance between two points.
- Parameters:
point1 (np.array) – First point [x, y] (2x1).
point2 (np.array) – Second point [x, y] (2x1).
- Returns:
Distance between points.
- Return type:
float
- irsim.util.util.random_point_range(range_low: list[float] | None = None, range_high: list[float] | None = None) list[float][source]#
Generate a random point within a range.
- Parameters:
range_low (list) – Lower bound of the range.
range_high (list) – Upper bound of the range.
- Returns:
Random point within the range.
- Return type:
np.array