Geometry
3D geometry calculations and coordinate utilities.
Geometry utilities for 3D calculations in eye tracking.
This module provides geometric functions for: - 3D coordinate conversions and transformations - Ray-surface intersection calculations - Vector and matrix utilities - Eye rotation calculations (Listing’s law) - Plane detection for calibration
Conversions
Coordinate and gaze conversion utilities for eye tracking simulation.
Implements conversions between gaze direction, rotation angles, and observer/screen coordinates.
- pyetsimul.geometry.conversions.gaze2angle(gaze, rest_pos=None)[source]
Convert gaze direction to eye rotation angles.
Calculates horizontal and vertical rotation angles from a 3D gaze direction relative to the eye’s rest position. Uses Listing’s law coordinate system.
- Parameters:
gaze (
Direction3D) – 3D gaze direction vectorrest_pos (
RotationMatrix|None) – Optional 3x3 rotation matrix for eye rest position. Defaults to [[-1,0,0], [0,0,1], [0,1,0]]
- Return type:
- Returns:
Point2D containing [horizontal_angle, vertical_angle] in radians
- pyetsimul.geometry.conversions.angle2gaze(angles, rest_pos=None)[source]
Convert eye rotation angles to gaze direction.
Calculates gaze direction from horizontal and vertical rotation angles using Euler rotation matrices. Applies rotations in Listing’s law order.
- Parameters:
angles (
Point2D) – 2D point containing rotation angles [horizontal, vertical] in radiansrest_pos (
RotationMatrix|None) – Optional 3x3 rotation matrix for eye rest position. Defaults to [[-1,0,0], [0,0,1], [0,1,0]]
- Return type:
- Returns:
Direction3D representing the gaze direction vector
- pyetsimul.geometry.conversions.calculate_angular_error_degrees(actual_point, predicted_point, observer_pos)[source]
Calculate angular error between actual and predicted gaze points.
Creates 3D gaze vectors from observer to each point and computes the angle between them using the dot product formula. Handles numerical precision issues.
- Parameters:
actual_point (
Point3D) – Actual target point [x, y, z] in mmpredicted_point (
Point3D) – Predicted gaze point [x, y, z] in mmobserver_pos (
Position3D) – Observer position [x, y, z] in mm
- Return type:
- Returns:
Angular error in degrees
Plane Detection
Automatic 2D plane detection for calibration targets.
This module provides utilities to automatically detect which 2D plane calibration points lie in and extract appropriate coordinate mappings for polynomial fitting.
- class pyetsimul.geometry.plane_detection.PlaneInfo(plane_type, primary_axis, secondary_axis, constant_axis, constant_value)[source]
Bases:
objectInformation about detected calibration plane.
Represents a 2D plane in 3D space for coordinate mapping and polynomial fitting. Automatically determines which axis is constant and which two axes vary.
- Parameters:
- extract_2d_coords(position)[source]
Extract 2D coordinates for polynomial fitting.
Maps 3D position to 2D coordinates based on plane orientation.
- Return type:
- Parameters:
position (Position3D)
- pyetsimul.geometry.plane_detection.detect_calibration_plane(calib_points, tolerance=1e-06)[source]
Automatically detect which 2D plane the calibration points lie in.
Uses variance analysis to determine which axis is constant and which two axes vary. Supports standard orthogonal planes (xy, xz, yz) for polynomial gaze models.
- Parameters:
calib_points (
list[Position3D]) – List of calibration target positionstolerance (
float) – Variance threshold for considering an axis constant
- Returns:
Information about the detected plane and coordinate mappings
- Return type:
- Raises:
ValueError – If points don’t lie in exactly one of the standard 2D planes
- pyetsimul.geometry.plane_detection.summarize_plane_detection(calib_points, plane_info)[source]
Create a human-readable summary of the plane detection results.
Generates formatted output showing plane type, coordinate mapping, and coverage area for logging and display purposes.
- Parameters:
calib_points (
list[Position3D]) – List of calibration target positionsplane_info (
PlaneInfo) – Information about the detected plane
- Returns:
Formatted summary for logging/display
- Return type:
Intersections
Ray-surface intersection and geometric calculation utilities for eye tracking simulation.
Provides functions for intersecting rays with spheres, circles, planes, and conic surfaces, as well as related geometric operations.
- pyetsimul.geometry.intersections.intersect_ray_sphere(ray, sphere_center, sphere_radius)[source]
Find intersection points between ray and sphere.
Uses quadratic equation to find intersection points. Returns both intersection points ordered by distance from ray origin (closer first).
- Parameters:
ray (
Ray) – Ray with origin and directionsphere_center (
Position3D) – Sphere center positionsphere_radius (
float) – Sphere radius
- Return type:
- Returns:
Tuple of (closer_result, farther_result) where closer_result is closer intersection, farther_result is farther. Returns (None, None) if no intersection.
- pyetsimul.geometry.intersections.intersect_ray_circle(ray, circle_center, circle_radius)[source]
Find intersection between ray and circle in 2D plane.
Uses quadratic equation to find intersection points in x-y plane. Returns the closest intersection point to ray origin.
- Parameters:
- Return type:
- Returns:
Intersection result with closest point to ray origin, or None if no intersection
- pyetsimul.geometry.intersections.intersect_ray_plane(ray, plane_point, plane_normal)[source]
Find intersection between ray and plane.
Solves the parametric ray equation with the plane equation using dot product. Returns intersection point with surface normal.
- Parameters:
ray (
Ray) – Ray with origin and directionplane_point (
Position3D) – Point on planeplane_normal (
Direction3D) – Plane normal vector
- Return type:
- Returns:
Intersection result, or None if ray is parallel to plane
- pyetsimul.geometry.intersections.intersect_ray_conic(ray, conic_center, radius, conic_constant)[source]
Find intersection between ray and conic section.
Uses quadratic equation derived from conic surface equation. Returns both intersection points ordered by distance from ray origin.
- Parameters:
ray (
Ray) – Ray with origin and directionconic_center (
Position3D) – Conic center positionradius (
float) – Radius parameter (R in the formula, mm)conic_constant (
float) – Conic constant (k < 0 for prolate, k = 0 for sphere, k > 0 for oblate)
- Returns:
Intersection results closer and farther from ray origin. Returns (None, None) if no intersection.
- Return type:
Tuple (closer_result, farther_result)
- pyetsimul.geometry.intersections.conic_surface_normal(point, conic_center, radius, conic_constant)[source]
Calculate surface normal at a point on conic section surface.
Uses gradient of conic equation to compute normal vector. Handles degenerate case at conic apex.
- Parameters:
point (
Point3D) – Point on conic surfaceconic_center (
Position3D) – Conic center positionradius (
float) – Radius parameter (R in the formula, mm)conic_constant (
float) – Conic constant (k < 0 for prolate, k = 0 for sphere, k > 0 for oblate)
- Return type:
- Returns:
Unit normal vector pointing outward from conic surface
- pyetsimul.geometry.intersections.point_on_conic_surface(conic_center, direction, radius, conic_constant)[source]
Calculate point on conic surface given direction from start point.
Uses quadratic equation to find intersection of ray with conic surface. Chooses best intersection point based on direction alignment.
- Parameters:
conic_center (
Position3D) – Starting point of the raydirection (
Vector3D) – Direction vector (will be normalized)radius (
float) – Radius parameter of the conic (R)conic_constant (
float) – Shape parameter of the conic (k)
- Return type:
- Returns:
Point on conic surface, or None if no intersection
Listings Law
Listing’s law implementation for eye rotation calculations.
This module implements Listing’s law, which describes how the eye rotates when changing fixation direction. Extracted from the Eye class for better modularity and testability.
- pyetsimul.geometry.listings_law.calculate_eye_rotation(out_rest, out_new)[source]
Calculate eye rotation matrix using Listing’s law.
Computes rotation matrix for eye movement from rest position to new position using Listing’s law coordinate system approach.
- Parameters:
- Return type:
- Returns:
3x3 rotation matrix A representing the eye rotation
Utilities
Geometric utility functions for eye tracking simulation.
Provides line-line closest point, 2D line intersection, and other basic geometric operations.
- pyetsimul.geometry.utils.lines_closest_point(p1, d1, p2, d2)[source]
Find the closest points between two lines in 3D space.
Uses the minimum distance approach to find points on each line that are closest to each other. For parallel lines, returns NaN points.
- Parameters:
- Return type:
- Returns:
Tuple of (x1, x2) where x1 is closest point on first line, x2 is closest point on second line. Returns (NaN point, NaN point) if lines are parallel.