Skip to content

add polygon_chord#2969

Draft
burmist-git wants to merge 3 commits into
mainfrom
chord_length
Draft

add polygon_chord#2969
burmist-git wants to merge 3 commits into
mainfrom
chord_length

Conversation

@burmist-git
Copy link
Copy Markdown
Member

@burmist-git burmist-git commented Mar 24, 2026

@burmist-git burmist-git requested a review from maxnoe March 24, 2026 09:56
@kosack
Copy link
Copy Markdown
Member

kosack commented Mar 24, 2026

Maybe we should consider adding shapely as a dependency? It might simplify some of these computations without having to re-implement all of computational geometry.

@maxnoe
Copy link
Copy Markdown
Member

maxnoe commented Mar 26, 2026

Maybe we should consider adding shapely as a dependency?

I already looked into using shapely for a way to support trigger / voltage patches in the past here:
#855 (comment)

@burmist-git
Copy link
Copy Markdown
Member Author

This is possible, but the function itself is very small, so using an external library would be unnecessary. I wouldn’t make it more complex than it is.

X-coordinates of the starting points of the polygon edges.
ri_y : ndarray of shape (N,)
Y-coordinates of the starting points of the polygon edges.
vi_x : ndarray of shape (N,)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should deduce these (if needed) from the vertices. You can write a helper for this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'd also prefer the vertices as input. But maybe the function as it is here is fine and that transformation is made once outside of this function.


Parameters
----------
mu_x : float
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be quantities, as we discussed offline, the best would be to use telescope coordinate system lat/lon.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, perhaps tuples of (x,y) or arrays of them would be more straightforward to define points (arrays of points)


Returns
-------
float
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be also a unit of length (or angular, equivalent in the telescope frame)

- Each polygon edge is defined as:
(x, y) = (ri_x, ri_y) + t * (vi_x, vi_y), with 0 <= t < 1
- The function computes intersections by solving a 2D linear system.
- A small epsilon_d (`1e-20`) is added to the denominator to avoid division by zero.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not using

try:
  ...
except ZeroDivisionError:
  # determinant is zero, the cord goes on the edge, handle this

Copy link
Copy Markdown
Member

@kosack kosack Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remember that catching an exception is quite a bit slower than adding an epsilon, so if you need this to be fast, the epsilon method might be better. Also if you want to use numba to speed things up

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And doesn't vectorize

SQRT2 = np.sqrt(2)


def polygon_chord(mu_x, mu_y, phi, ri_x, ri_y, vi_x, vi_y):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should be njit, as it will be called. If possible, it would also be good if it could be vectorized over multiple tiles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants