LFUtils

The LFUtils module collects various general-purpose utility functions used throughout LensFactory. It is organized into four submodules: AstrometricOps for astrometric coordinate transformations, ContourFinder for tracing iso-contours of 2D scalar fields, IntersectionFinder for computing intersections of curves, and PolygonOps for polygon and interpolation operations.

AstrometricOps

LensFactory.LFUtils.AstrometricOps.gnomonic_offsets_arcsec — Function
gnomonic_offsets_arcsec(ra_ref::Float64, dec_ref::Float64, ra_cat::Float64, dec_cat::Float64)
source
gnomonic_offsets_arcsec(ra_ref::Float64, dec_ref::Float64, ra_cat::Vector{Float64}, dec_cat::Vector{Float64})

Project catalog positions onto the tangent plane at a reference position using the gnomonic projection and return the angular offsets in arcseconds. The convention is north up, east left (i.e., x increases towards west).

Scalar inputs (ra_cat::Float64, dec_cat::Float64) are also accepted, returning scalar offsets.

Arguments

  • ra_ref::Float64: Reference right ascension (in degrees)
  • dec_ref::Float64: Reference declination (in degrees)
  • ra_cat::Vector{Float64}: Catalog right ascensions (in degrees)
  • dec_cat::Vector{Float64}: Catalog declinations (in degrees)

Returns

  • x::Vector{Float64}: Offsets along the x-axis (in arcseconds)
  • y::Vector{Float64}: Offsets along the y-axis (in arcseconds)
source
LensFactory.LFUtils.AstrometricOps.gnomonic_offsets_radec — Function
gnomonic_offsets_radec(ra_ref::Float64, dec_ref::Float64, x_as::Float64, y_as::Float64)
source
gnomonic_offsets_radec(ra_ref::Float64, dec_ref::Float64, x_as::Vector{Float64}, y_as::Vector{Float64})

Inverse gnomonic projection: convert tangent-plane offsets (in arcseconds) around a reference position back to (RA, Dec) sky coordinates. Follows the same north up, east left convention as gnomonic_offsets_arcsec.

Scalar inputs (x_as::Float64, y_as::Float64) are also accepted, returning scalar coordinates.

Arguments

  • ra_ref::Float64: Reference right ascension (in degrees)
  • dec_ref::Float64: Reference declination (in degrees)
  • x_as::Vector{Float64}: Offsets along the x-axis (in arcseconds)
  • y_as::Vector{Float64}: Offsets along the y-axis (in arcseconds)

Returns

  • ra::Vector{Float64}: Right ascensions (in degrees)
  • dec::Vector{Float64}: Declinations (in degrees)
source

ContourFinder

LensFactory.LFUtils.ContourFinder.get_contour — Function
get_contour(x::AbstractMatrix{<:Real}, y::AbstractMatrix{<:Real}, z::AbstractMatrix{<:Real}, level::Real)

Extract the iso-contours of a 2D scalar field z at the given level using the marching squares algorithm. The grid may be curvilinear: x and y give the coordinates of each grid node and must share the axes of z. Ambiguous saddle cells are resolved with the cell-center average, and contour crossings are located by linear interpolation along cell edges.

Arguments

  • x::AbstractMatrix{<:Real}: x-coordinates of the grid nodes
  • y::AbstractMatrix{<:Real}: y-coordinates of the grid nodes
  • z::AbstractMatrix{<:Real}: Scalar field values at the grid nodes
  • level::Real: Contour level to trace

Returns

  • Vector{Vector{Vector{Float64}}}: A list of contour lines, each a list of [x, y] points. Closed contours end at their starting point; open contours terminate at the grid boundary.
source

IntersectionFinder

LensFactory.LFUtils.IntersectionFinder.get_intersection — Function
get_intersection(x1::AbstractVector{<:Real}, y1::AbstractVector{<:Real},
                 x2::AbstractVector{<:Real}, y2::AbstractVector{<:Real})

Find the intersection points of two piecewise-linear curves. Candidate segment pairs are selected with a bounding-box overlap test and each pair is solved as a 4x4 linear system. Duplicate solutions (within a distance of 1E-12) are removed. Based on the MATLAB routine Fast and Robust Curve Intersections.

Arguments

  • x1::AbstractVector{<:Real}: x-coordinates of the first curve
  • y1::AbstractVector{<:Real}: y-coordinates of the first curve
  • x2::AbstractVector{<:Real}: x-coordinates of the second curve
  • y2::AbstractVector{<:Real}: y-coordinates of the second curve

Returns

  • Vector{NTuple{2,Real}}: Unique intersection points as (x, y) tuples
source

PolygonOps

LensFactory.LFUtils.PolygonOps.bilinear_interpolation — Function
bilinear_interpolation(x::Real, y::Real, df::AbstractMatrix{<:Real})

Bilinear interpolation at (x, y) given in pixel coordinates.

Arguments

  • x::Float64: x pixel coordinate
  • y::Float64: y pixel coordinate
  • df::Matrix{<:Float64}: Data matrix

Returns

  • Float64: Interpolated value at (x, y) position
source
LensFactory.LFUtils.PolygonOps.shoelace — Function
shoelace(polygon::Vector{<:Vector{<:Real}})

Calculate the area of a 2D polygon using the shoelace formula.

Arguments

  • polygon::Vector{Vector{<:Real}}: A vector of [x, y] coordinates representing the polygon vertices

Returns

  • The area of the polygon
source
LensFactory.LFUtils.PolygonOps.hao_sun — Function
hao_sun(point, polygon)

Algorithm to determine if a point is in the given polygon. Taken from Hao et al. (2018)

Arguments

  • point::Vector{Float64}: The (x, y) coordinate of the point to check
  • polygon::Vector{Vector{Float64}}: A vector of [x, y] coordinates representing the polygon vertices

Returns

  • Int:
    • +1: inside
    • 0: outside
    • -1: on the edge
source
LensFactory.LFUtils.PolygonOps.winding_number — Function
winding_number(point::Vector{<:Real}, polygon::Vector{<:Vector{<:Real}})

Calculate the winding number of a closed polygon about the given point, i.e. the (signed) number of times the polygon wraps around the point. A ray is shot from the point towards $+x$ and the crossings of the polygon are counted +1 (polygon crossing the ray upwards) or -1 (crossing it downwards).

The winding number is undefined for a point lying exactly on an edge, in which case the returned value depends on which side of the edge the floating point arithmetic lands.

Arguments

  • point : The (x, y) coordinate of the point to check
  • polygon: A vector of [x, y] coordinates representing the polygon vertices

Returns

  • Int: Winding number about the given point (0 if the point lies outside the polygon)
source
LensFactory.LFUtils.PolygonOps.fit_ellipse — Function
fit_ellipse(x::Vector{Float64}, y::Vector{Float64})

Fits an ellipse to a set of points (x, y) using the least squares method with a quadratic constraint.

Arguments

  • x::Vector{Float64}: The x-coordinates of the points
  • y::Vector{Float64}: The y-coordinates of the points

Returns

  • cx: The x-coordinate of the center of the ellipse
  • cy: The y-coordinate of the center of the ellipse
  • rx: The semi-major axis length of the ellipse
  • ry: The semi-minor axis length of the ellipse
  • θ: The rotation angle of the ellipse (in degrees)
source