Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provide a shapeDistance function #1328

Merged
merged 12 commits into from Mar 2, 2023
Merged

feat: provide a shapeDistance function #1328

merged 12 commits into from Mar 2, 2023

Conversation

samestep
Copy link
Collaborator

@samestep samestep commented Feb 28, 2023

Description

This PR adds a new function called shapeDistance which computes the signed distance from the origin on the Minkowski sum of the first shape and the negative of the second shape.

This works on all shapes. It uses the following approximations:

  • when both shapes are Circle, we are exact
  • when both shapes are Rectlike, we are exact
  • when one shape is Text or Equation and the other is Line, we use the bounding circle approximation added by @wodeni in feat: improve euclidean.sty #1117
  • when both shapes are Polygonlike, we use the intersection-of-half-planes approximation added by @jiriminarcik in feat: Line polygon constraints #810
  • when one shape is Rectlike and the other is Circle, we are exact
  • when one shape is Polygonlike and the other is Ellipse, I'm not exactly sure what we do; @jiriminarcik do you happen to know if this is an approximation?
  • when one shape is Circle and the other is Line, I believe we are exact
  • in all other cases, we approximate both shapes by their respective axis-aligned bounding boxes

Implementation strategy and design decisions

I factored out most of the existing overlapping constraint. Also, for better typechecking, I added a ShapeTuple type representing the pair format expected by objectives and constraints, and a shapeTupleToShape function that converts this to just a Shape. In the same file, I also added Rectlike and Polygonlike types along with isRectlike and isPolygonlike type predicates to go along with those.

This patch made the overlapping constraint more correct for the case where one shape is a circle or an ellipse and the other is a rectangle, so I corrected the tests for that case.

Checklist

  • I have commented my code, particularly in hard-to-understand areas
  • I have reviewed any generated registry diagram changes

Open questions

I didn't factor out the cases where one shape is an ellipse and the other is either a circle or an ellipse, because I don't know how to use our existing code to actually calculate the distance there.

@cloudflare-pages
Copy link

cloudflare-pages bot commented Feb 28, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 68d086a
Status: ✅  Deploy successful!
Preview URL: https://0670bc37.penrose-72l.pages.dev
Branch Preview URL: https://shape-distance.penrose-72l.pages.dev

View logs

@github-actions
Copy link

github-actions bot commented Mar 2, 2023

± Registry diff

M	circle-example-euclidean.svg
M	continuousmap-continuousmap.svg
M	cubic-bezier-cubic-bezier.svg
M	hypergraph-hypergraph.svg
M	mobius-mobius.svg
M	persistent-homology-persistent-homology.svg
M	small-graph-disjoint-rect-line-horiz.svg
M	tree-venn-3d.svg
M	tree-venn.svg
M	two-triangles-triangle-mesh-3d.svg
M	word-cloud-example-word-cloud.svg
M	wos-laplace-estimator-walk-on-spheres.svg
M	wos-nested-estimator-walk-on-spheres.svg
M	wos-offcenter-estimator-walk-on-spheres.svg
M	wos-poisson-estimator-walk-on-spheres.svg

📊 Performance

Key

Note that each bar component rounds up to the nearest 100ms, so each full bar is an overestimate by up to 400ms.

     0s   1s   2s   3s   4s   5s   6s   7s   8s   9s
     |    |    |    |    |    |    |    |    |    |
name ▝▀▀▀▀▀▀▀▀▀▀▀▚▄▄▄▄▄▄▄▄▄▞▀▀▀▀▀▀▀▀▀▀▀▀▚▄▄▄▄▄▄▄▄▄▖
      compilation labelling optimization rendering

Data

                                                    0s   1s   2s   3s   4s   5s   6s   7s
                                                    |    |    |    |    |    |    |    |
3d-projection-fake-3d-linear-algebra                ▝▞▖
allShapes-allShapes                                 ▝▞▄▄
arrowheads-arrowheads                               ▝▞▖
caffeine-structural-formula                         ▝▀▚▀▖
center-shrink-circle-animation                      ▝▞▖
circle-example-euclidean                            ▝▀▞▀▀▚
closest-point-test-closest-point                    ▝▀▀▀▀▀▞▖
collinear-euclidean                                 ▝▚▚
congruent-triangles-euclidean                       ▝▀▚▚
continuousmap-continuousmap                         ▝▞▖
cubic-bezier-cubic-bezier                           ▝▚▀▀▖
glutamine-molecules-basic                           ▝▀▞▖
half-adder-distinctive-shape                        ▝▚▚
hypergraph-hypergraph                               ▝▀▀▀▞▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▖
incenter-triangle-euclidean                         ▝▀▞▖
lagrange-bases-lagrange-bases                       ▝▞▖
matrix-matrix-addition-matrix-ops                   ▝▚▚
matrix-matrix-division-elementwise-matrix-ops       ▝▚▚
matrix-matrix-multiplication-elementwise-matrix-ops ▝▚▚
matrix-matrix-multiplication-matrix-ops             ▝▚▚
matrix-matrix-subtraction-matrix-ops                ▝▚▚
matrix-transpose-matrix-ops                         ▝▞▖
matrix-vector-left-multiplication-matrix-ops        ▝▚▚
matrix-vector-right-multiplication-matrix-ops       ▝▚▚
midsegment-triangles-euclidean                      ▝▀▞▖
mobius-mobius                                       ▝▞▖
non-convex-non-convex                               ▝▚▚
one-water-molecule-atoms-and-bonds                  ▝▞▖
parallel-lines-euclidean                            ▝▀▞▚
persistent-homology-persistent-homology             ▝▀▀▞▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄
points-around-line-shape-distance                   ▝▀▚▚
points-around-polyline-shape-distance               ▝▀▚▚
points-around-star-shape-distance                   ▝▀▀▚▚
quaternion-group-group-theory-cayley-graph          ▝▀▞▖
quaternion-group-group-theory-multiplication-table  ▝▀▀▚▞▖
scalar-vector-division-matrix-ops                   ▝▚▚
scalar-vector-left-multiplication-matrix-ops        ▝▞▖
scalar-vector-right-multiplication-matrix-ops       ▝▞▖
siggraph-teaser-euclidean-teaser                    ▝▀▞▖
small-graph-disjoint-rect-line-horiz                ▝▚▚
small-graph-disjoint-rects                          ▝▞▖
small-graph-disjoint-rects-large-canvas             ▝▞▖
small-graph-disjoint-rects-small-canvas             ▝▞▖
tree-tree                                           ▝▀▞▖
tree-venn                                           ▝▀▞▖
tree-venn-3d                                        ▝▚▚▄▖
two-triangles-triangle-mesh-3d                      ▝▚▚
two-vectors-perp-vectors-dashed                     ▝▞▖
vector-vector-addition-matrix-ops                   ▝▞▖
vector-vector-division-elementwise-matrix-ops       ▝▞▖
vector-vector-multiplication-elementwise-matrix-ops ▝▞▖
vector-vector-outerproduct-matrix-ops               ▝▞▖
vector-vector-subtraction-matrix-ops                ▝▞▖
vector-wedge-exterior-algebra                       ▝▞▖
wet-floor-atoms-and-bonds                           ▝▚▀▚
word-cloud-example-word-cloud                       ▝▀▞▖
wos-laplace-estimator-walk-on-spheres               ▝▀▞▖
wos-nested-estimator-walk-on-spheres                ▝▀▀▞▀▚
wos-offcenter-estimator-walk-on-spheres             ▝▀▞▀▖
wos-poisson-estimator-walk-on-spheres               ▝▀▞▀▖

@codecov
Copy link

codecov bot commented Mar 2, 2023

Codecov Report

Merging #1328 (68d086a) into main (4aa7443) will increase coverage by 0.14%.
The diff coverage is 70.40%.

@@            Coverage Diff             @@
##             main    #1328      +/-   ##
==========================================
+ Coverage   61.40%   61.55%   +0.14%     
==========================================
  Files          65       65              
  Lines        8056     8055       -1     
  Branches     1888     1884       -4     
==========================================
+ Hits         4947     4958      +11     
+ Misses       3001     2989      -12     
  Partials      108      108              
Impacted Files Coverage Δ
packages/core/src/contrib/ConstraintsUtils.ts 78.57% <ø> (+13.18%) ⬆️
packages/core/src/contrib/Functions.ts 39.52% <0.00%> (-0.07%) ⬇️
packages/core/src/contrib/Queries.ts 77.00% <68.05%> (-23.00%) ⬇️
packages/core/src/contrib/Constraints.ts 85.39% <70.58%> (+2.51%) ⬆️
packages/core/src/contrib/Utils.ts 56.09% <100.00%> (+4.74%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@samestep samestep marked this pull request as ready for review March 2, 2023 19:20
@joshsunshine
Copy link
Member

@samestep, please describe what shapes this works on, what approximations (if any) are used, etc. in the PR description.

@samestep
Copy link
Collaborator Author

samestep commented Mar 2, 2023

@joshsunshine done!

@samestep samestep merged commit 76a91e5 into main Mar 2, 2023
@samestep samestep deleted the shape-distance branch March 2, 2023 22:51
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.

None yet

3 participants