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: compute rect-line distance exactly #1332

Merged
merged 11 commits into from Mar 7, 2023
Merged

feat: compute rect-line distance exactly #1332

merged 11 commits into from Mar 7, 2023

Conversation

samestep
Copy link
Collaborator

@samestep samestep commented Mar 4, 2023

Description

This PR provides a rectLineDist function which differentiably computes the exact signed distance from the origin to the Minkowski difference of a rectangle and a line, and modifies the implementation of the shapeDistance function added by #1328 to use this when one shape is a Line and the other is an Equation or Text (as opposed to the previous implementation, which used a bounding circle as an approximation instead of a bounding box). I also added a new example to the registry to demonstrate visually that this works.

Implementation strategy and design decisions

The reason to export a new rectLineDist function, instead of just using shapeDistance, is that sometimes we want to phrase an objective or constraint as if something is a straight line segment, even if the actual shape being drawn is something else, like a quadratic Bézier curve in the case of #1317. Using shapeDistance requires you to actually have a genuine Line shape in the diagram.

Also, we didn't have an xor autodiff function before, so I added one here. While I was doing that, I noticed that some comments in the autodiff types file were out of date, so I updated them.

I changed the variation for parallel-lines-euclidean because for some reason this patch was causing it to take a long time to optimize (almost ten seconds) with the old variation.

Checklist

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

@codecov
Copy link

codecov bot commented Mar 4, 2023

Codecov Report

Merging #1332 (227465f) into main (76a91e5) will increase coverage by 0.33%.
The diff coverage is 95.08%.

@@            Coverage Diff             @@
##             main    #1332      +/-   ##
==========================================
+ Coverage   61.55%   61.89%   +0.33%     
==========================================
  Files          65       65              
  Lines        8055     8103      +48     
  Branches     1884     1887       +3     
==========================================
+ Hits         4958     5015      +57     
+ Misses       2989     2980       -9     
  Partials      108      108              
Impacted Files Coverage Δ
packages/core/src/contrib/Functions.ts 39.46% <0.00%> (-0.07%) ⬇️
packages/core/src/types/ad.ts 100.00% <ø> (ø)
packages/core/src/utils/Wasm.ts 98.33% <ø> (ø)
packages/core/src/contrib/Queries.ts 91.03% <96.49%> (+14.03%) ⬆️
packages/core/src/engine/Autodiff.ts 79.41% <100.00%> (+0.02%) ⬆️
packages/core/src/engine/AutodiffFunctions.ts 100.00% <100.00%> (ø)

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

@cloudflare-pages
Copy link

cloudflare-pages bot commented Mar 4, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 227465f
Status: ✅  Deploy successful!
Preview URL: https://2dff5e68.penrose-72l.pages.dev
Branch Preview URL: https://rect-line-dist.penrose-72l.pages.dev

View logs

@github-actions
Copy link

github-actions bot commented Mar 4, 2023

± Registry diff

M	circle-example-euclidean.svg
M	collinear-euclidean.svg
M	congruent-triangles-euclidean.svg
M	cubic-bezier-cubic-bezier.svg
M	incenter-triangle-euclidean.svg
A	lines-around-rect-rect-line-dist.svg
M	parallel-lines-euclidean.svg
M	siggraph-teaser-euclidean-teaser.svg
M	small-graph-disjoint-rect-line-horiz.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   8s
                                                    |    |    |    |    |    |    |    |    |
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                       ▝▚▚
lines-around-rect-rect-line-dist                    ▝▚▚
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               ▝▀▚▀▚

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

joshsunshine commented Mar 6, 2023

modifies the implementation of the shapeDistance function added by #1328 to use this when one shape is a Line and the other is an Equation or Text

After reading the code, it looks like the shapeDistance function calls rectLineDist for all RectLike shapes which also includes Rectangle and Image. @samestep, is that correct?

@samestep
Copy link
Collaborator Author

samestep commented Mar 6, 2023

@joshsunshine No, that is not correct:

else if ((t1 === "Text" || t1 === "Equation") && t2 === "Line")

@joshsunshine
Copy link
Member

I am glad I asked. Why didn't you add Rectangle and Image to that else if? Is there a reason this doesn't work for those shapes?

@samestep
Copy link
Collaborator Author

samestep commented Mar 6, 2023

Nope, no reason not to do that! Do you want me to do it in this PR, or in a followup?

@joshsunshine
Copy link
Member

Nope, no reason not to do that! Do you want me to do it in this PR, or in a followup?

This PR.

@samestep
Copy link
Collaborator Author

samestep commented Mar 6, 2023

@joshsunshine Done.

@samestep samestep merged commit b86be31 into main Mar 7, 2023
@samestep samestep deleted the rect-line-dist branch March 7, 2023 02:13
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

2 participants