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: Additional curve support #1503

Merged
merged 16 commits into from
Jun 23, 2023
Merged

feat: Additional curve support #1503

merged 16 commits into from
Jun 23, 2023

Conversation

keenancrane
Copy link
Collaborator

@keenancrane keenancrane commented Jun 22, 2023

image

This PR expands on support for curves/paths in Style by adding several functions to Functions.ts:

  • interpolatingSpline() — provides path data for an (open or closed) Catmull-Rom spline interpolating a given set of points, with optional tension value. This method provides an easy way to draw a smooth curve through a list of points without having to explicitly think about tangents.
  • diffusionProcess() — provides a way to generate a common type of "random" curve, namely, those that arise from Brownian motion. This functionality mirrors functionality provided by other packages—for instance, Mathematica provides a similar collection of functions under the heading Stochastic Differential Equation Processes. I decided against simply calling this method randomCurve() which, while a bit more accessible, we might want to reserve for other notions of "random curve."
  • firstPoint()/lastPoint() — readable convenience methods to access the first and last point of a list of points in Style. Much like our other nomenclature choices, these methods are named as a readable alternative to expressions like myList[0] or myList[ length(myList) - 1 ] (which we may or may not even currently support).
  • averagePoint() — returns the average (mean) of all points in a list. This function extends existing list processing functionality like sumVectors(), maxList(), and minList() (which is heavily used in collect statements). It could perhaps be emulated in Style via something like sumVectors(list)/length(list), but again, a convenience function provides more readable code; it's also not clear right now that we provide a mechanism for determining list length outside of collect statements (the plain length() function currently has an extremely special functionality—measuring the length of a Line shape—which should probably be renamed).

In general, more sophisticated list processing in Style, and the ability to interpret lists as various kinds of curves seems pretty essential for real diagramming. The most significant functionality missing from this PR is the ability to concatenate multiple paths to generate another path—e.g., using several curves from different parts of a diagram to make a single region boundary of a shaded shape. This PR can be viewed as sort of "dipping our toes" into the waters of more real-world path manipulation.

Examples

The features above are demonstrated/tested using two new examples (shown above):

  • curve-examples/catmull-rom: demonstrates the interpolatingSpline() function, including several parameter values for tension.
  • stochastic-process: demonstrates the diffusionProcess(), lastPoint(), and averagePoint() functions. This example also shows compositionality of the PR functions—here for instance the point list generated by diffusionProcess() is fed to interpolatingSpline() to get a smoother curve.

Implementation

Some methods were refactors as local computation functions (rather than only being exported to Style), which also helps make them usable through the API. In particular, generation of normally-distributed random numbers, construction of Catmull-Rom splines, and generation of stochastic curves.

@keenancrane keenancrane changed the title Additional curve support feat: Additional curve support Jun 22, 2023
@cloudflare-pages
Copy link

cloudflare-pages bot commented Jun 22, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 184289d
Status: ✅  Deploy successful!
Preview URL: https://41b05148.penrose-72l.pages.dev
Branch Preview URL: https://brownian-motion.penrose-72l.pages.dev

View logs

@github-actions
Copy link

github-actions bot commented Jun 22, 2023

± Registry diff

A	curve-examples/catmull-rom/catmull-rom.svg
A	stochastic-process/stochastic-process.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
     |    |    |    |    |    |    |    |    |
name ▝▀▀▀▀▀▀▀▀▀▚▄▄▄▄▄▄▄▄▞▀▀▀▀▀▀▀▀▀▀▚▄▄▄▄▄▄▄▄▄▖
      compiling labeling optimizing rendering

If a row has only one bar instead of four, that means it's not a trio and the bar just shows the total time spent for that example, again rounded up to the nearest 100ms.

Data

                                                          0s   1s   2s   3s   4s   5s
                                                          |    |    |    |    |    |
set-theory-domain/tree-venn                               ▝▀▞▖
set-theory-domain/tree-tree                               ▝▀▄▞▖
set-theory-domain/tree-venn-3d                            ▝▚▚▖
group-theory/quaternion-multiplication-table              ▝▀▀▄▚▖
group-theory/quaternion-cayley-graph                      ▝▀▞▖
atoms-and-bonds/wet-floor                                 ▝▚▀▀▚
atoms-and-bonds/one-water-molecule                        ▝▞▖
set-theory-domain/continuousmap                           ▝▞▖
linear-algebra-domain/two-vectors-perp                    ▝▚▚
tutorials/tutorial1                                       ▝▞▖
tutorials/tutorial2                                       ▝▞▖
tutorials/tutorial3                                       ▝▞▖
molecules/nitricacid-lewis                                ▝▀▀▄▀▖
exterior-algebra/vector-wedge                             ▝▚▚
shape-spec/all-shapes                                     ▝▞▄
shape-spec/arrowheads                                     ▝▞▖
graph-domain/textbook/sec1/fig1                           ▝▀▀▞▚
graph-domain/textbook/sec1/fig2                           ▝▀▀▚▀▚
graph-domain/textbook/sec1/fig3                           ▝▀▀▀▞▀▚
graph-domain/textbook/sec1/fig4                           ▝▀▀▚▀▀▖
graph-domain/textbook/sec1/fig5                           ▝▀▀▀▀▚▀▀▀▚
graph-domain/textbook/sec1/fig6                           ▝▀▀▀▀▀▀▀▀▚▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▖
graph-domain/textbook/sec1/fig7                           ▝▀▞▖
graph-domain/textbook/sec1/fig8a                          ▝▀▀▀▚▀▀▚
graph-domain/textbook/sec1/fig8b                          ▝▀▀▀▚▀▀▚
graph-domain/textbook/sec1/fig9                           ▝▀▚▀▖
graph-domain/textbook/sec1/fig10                          ▝▀▚▚
graph-domain/textbook/sec1/fig11                          ▝▀▀▀▞▚▖
graph-domain/textbook/sec1/fig12                          ▝▀▀▀▀▞▀▚▖
graph-domain/textbook/sec1/fig13                          ▝▀▀▀▚▀▀▖
graph-domain/textbook/sec2/fig3                           ▝▀▀▀▀▚▀▀▀▀▖
graph-domain/textbook/sec2/fig4                           ▝▀▀▀▚▀▀▚
graph-domain/textbook/sec2/fig5                           ▝▀▀▀▀▀▀▚▀▀▀▀▖
graph-domain/textbook/sec2/fig6                           ▝▀▀▀▀▀▚▀▀▀▚▖
graph-domain/textbook/sec2/fig9                           ▝▀▀▀▀▀▀▀▀▞▀▀▀▀▀▚
graph-domain/textbook/sec2/fig10a                         ▝▀▀▞▀▖
graph-domain/textbook/sec2/fig10b                         ▝▀▀▞▚
graph-domain/textbook/sec2/fig11a                         ▝▀▀▞▖
graph-domain/textbook/sec2/fig11b                         ▝▚▚
graph-domain/textbook/sec2/fig11c                         ▝▀▞▖
graph-domain/textbook/sec2/fig12                          ▝▀▞▖
graph-domain/textbook/sec2/fig13                          ▝▀▀▀▀▀▀▀▀▀▚▀▀▀▀▀▀▀▀▀▀▀▀▖
graph-domain/textbook/sec2/fig14                          ▝▀▀▀▞▄
graph-domain/textbook/sec2/fig16b                         ▝▀▞▄
geometry-domain/textbook_problems/c05p13                  ▝▀▞▖
geometry-domain/textbook_problems/c01p01                  ▝▀▞▖
geometry-domain/textbook_problems/c03p01                  ▝▀▞▖
geometry-domain/textbook_problems/c05p01                  ▝▀▞▖
geometry-domain/textbook_problems/ex                      ▝▀▀▞▚
triangle-mesh-3d/two-triangles                            ▝▚▚
random-sampling/test                                      ▝▀▀▚▚
geometry-domain/textbook_problems/c11p12                  ▝▀▚▚
word-cloud/example                                        ▝▀▞▀▄
geometry-domain/siggraph-teaser                           ▝▀▚▚
minkowski-tests/maze/non-convex                           ▝▚▚
lagrange-bases/lagrange-bases                             ▝▚▚
hypergraph/hypergraph                                     ▝▀▀▚▀▀▀▀▀▀▀▀▀▀▀▀▀▖
persistent-homology/persistent-homology                   ▝▀▚▀▀▀▀▀▄▖
walk-on-spheres/laplace-estimator                         ▝▀▚▚
walk-on-spheres/poisson-estimator                         ▝▀▚▚
walk-on-spheres/nested-estimator                          ▝▀▀▀▞▄
walk-on-spheres/offcenter-estimator                       ▝▀▚▚
walk-on-spheres/walk-on-stars                             ▝▀▀▀▚▚▄▖
shape-distance/points-around-star                         ▝▀▀▞▄
shape-distance/points-around-polyline                     ▝▀▚▚
shape-distance/points-around-line                         ▝▀▞▖
shape-distance/lines-around-rect                          ▝▚▚
fake-3d-linear-algebra/projection                         ▝▚▚
animation/center-shrink-circle                            ▝▞▖
structural-formula/molecules/caffeine                     ▝▀▚▚▖
mobius/mobius                                             ▝▞▖
molecules/glutamine                                       ▝▀▞▖
matrix-ops/tests/matrix-matrix-addition                   ▝▚▚
matrix-ops/tests/matrix-matrix-division-elementwise       ▝▚▚
matrix-ops/tests/matrix-matrix-multiplication-elementwise ▝▚▚
matrix-ops/tests/matrix-matrix-multiplication             ▝▚▚
matrix-ops/tests/matrix-matrix-subtraction                ▝▚▚
matrix-ops/tests/matrix-transpose                         ▝▚▚
matrix-ops/tests/matrix-vector-left-multiplication        ▝▚▚
matrix-ops/tests/matrix-vector-right-multiplication       ▝▚▚
matrix-ops/tests/scalar-vector-division                   ▝▚▚
matrix-ops/tests/scalar-vector-left-multiplication        ▝▚▚
matrix-ops/tests/scalar-vector-right-multiplication       ▝▚▚
matrix-ops/tests/vector-vector-addition                   ▝▚▚
matrix-ops/tests/vector-vector-division-elementwise       ▝▚▚
matrix-ops/tests/vector-vector-multiplication-elementwise ▝▚▚
matrix-ops/tests/vector-vector-outerproduct               ▝▀▞▖
matrix-ops/tests/vector-vector-subtraction                ▝▚▚
logic-circuit-domain/half-adder                           ▝▚▚
curve-examples/cubic-bezier                               ▝▚▀▀▖
triangle-mesh-2d/diagrams/cotan-formula                   ▝▀▞▖
triangle-mesh-2d/diagrams/concyclic-pair                  ▝▀▞▖
triangle-mesh-2d/diagrams/halfedge-mesh                   ▝▀▞▖
triangle-mesh-2d/diagrams/relative-orientation            ▝▚▚
triangle-mesh-2d/diagrams/triangle-centers                ▝▚▚
triangle-mesh-2d/diagrams/angle-equivalence               ▝▀▚▀▀▖
timeline/penrose                                          ▝▀▀▚▚
graph-domain/textbook/sec5/ex32                           ▝▀▀▀▀▀▀▀▚▀▀▀▀▀▀▀▀▀▖
curve-examples/open-elastic-curve                         ▝▀▞▚
curve-examples/closed-elastic-curve                       ▝▀▞▀▖
graph-domain/other-examples/arpanet                       ▝▀▀▀▀▀▀▞▀▀▀▀▀▀▀▀▀▀▚
graph-domain/other-examples/nyc-subway                    ▝▀▀▀▀▀▀▀▀▀▀▞▀▀▀▀▀▀▚
curve-examples/blobs                                      ▝▀▀▀▀▀▞▀▀▀▀▀▀▀▀▀▀▀▀▀▀▖
curve-examples/space-curves                               ▝▀▀▀▀▞▀▀▚
geometric-queries/ray-intersect/test-group                ▝▀▀▀▀▀▀▚▚▖
ray-tracing/path-trace                                    ▝▀▚▚
ray-tracing/bidirectional                                 ▝▀▚▚
ray-tracing/next-event-estimation                         ▝▀▀▞▖
geometric-queries/test                                    ▝▀▞▖
geometric-queries/closest-point/test-group                ▝▚▚
geometric-queries/closest-point/test                      ▝▚▚
geometric-queries/closest-silhouette-point/test           ▝▚▚
geometric-queries/ray-intersect/test                      ▝▀▀▀▀▀▀▀▀▞▄▖
curve-examples/catmull-rom/catmull-rom                    ▝▞▖
stochastic-process/stochastic-process                     ▝▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▞▄▄▄
solid/eigenspace                                          ▐█▌
solid/triangles                                           ▐█
solid/vectors                                             ▐▌

@keenancrane keenancrane marked this pull request as ready for review June 23, 2023 14:07
Copy link
Contributor

@liangyiliang liangyiliang left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks!

packages/core/src/contrib/Functions.ts Show resolved Hide resolved
Copy link
Member

@wodeni wodeni left a comment

Choose a reason for hiding this comment

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

Love it. Thanks, @keenancrane!

@keenancrane keenancrane merged commit daa593d into main Jun 23, 2023
10 checks passed
@keenancrane keenancrane deleted the brownian-motion branch June 23, 2023 16:08
@keenancrane
Copy link
Collaborator Author

Merged—thanks all!

@jiriminarcik jiriminarcik mentioned this pull request Jul 13, 2023
2 tasks
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

4 participants