-
Notifications
You must be signed in to change notification settings - Fork 79
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
Implement uniform random point in polygon, triangle and circle #83
Open
dgtized
wants to merge
7
commits into
thi-ng:feature/no-org
Choose a base branch
from
dgtized:random-point-in-polygon
base: feature/no-org
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d464925
Introduce random-point-in-triangle2 for uniform distribution
dgtized 636e81f
Use m/random and optimize out use of sort
dgtized a1aae21
Add uniform random point inside of circle
dgtized 2cf90e9
Introduce geom.utils.probability with rand-weighted
dgtized 763801b
Implement random-point-inside for polygons
dgtized 75d2ed5
Just use gu/from-barycentric with correct weights for random in triangle
dgtized ed92ef5
Add svg uniform distribution example to visually demo change
dgtized File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
(ns thi.ng.geom.examples.svg.uniform-distribution | ||
(:require [thi.ng.geom.circle :as c] | ||
[thi.ng.geom.core :as g] | ||
[thi.ng.geom.polygon :as p] | ||
[thi.ng.geom.rect :as r] | ||
[thi.ng.geom.svg.adapter :as adapt] | ||
[thi.ng.geom.svg.core :as svg] | ||
[thi.ng.geom.triangle :as t] | ||
[thi.ng.geom.vector :as v])) | ||
|
||
(defn sample-points | ||
([shape sample-method] | ||
(sample-points shape sample-method 400)) | ||
([shape sample-method n] | ||
(let [centered (g/center shape)] | ||
(repeatedly n #(sample-method centered))))) | ||
|
||
(defn example [pos shape points description] | ||
(let [shape-centered (g/center shape)] | ||
(svg/group {} | ||
(svg/text (g/translate pos (v/vec2 0 -70)) | ||
description | ||
{:text-anchor "middle"}) | ||
(svg/group {:fill "none" :stroke "red"} (g/translate shape-centered pos)) | ||
(svg/group {:opacity 0.8} | ||
(for [[x y] points] | ||
(g/translate (c/circle x y 0.5) pos)))))) | ||
|
||
(defn scene [] | ||
(let [circle (c/circle 0 0 50) | ||
;; ellipse is not implemented? | ||
rectangle (r/rect 0 0 100 100) | ||
rotated-rectangle (g/rotate rectangle 0.25) | ||
triangle (t/triangle2 (v/vec2 0 0) (v/vec2 100 50) (v/vec2 25 100)) | ||
polygon (p/polygon2 [0 0] [50 75] [100 100] [100 50] [75 25])] | ||
(svg/svg {:width 800 :height 600 :stroke "black"} | ||
(example (v/vec2 100 100) circle | ||
(sample-points circle g/random-point-inside) | ||
"g/random-point-inside") | ||
(example (v/vec2 300 100) circle | ||
(sample-points circle c/random-point-in-circle) | ||
"c/random-point-in-circle") | ||
(example (v/vec2 500 100) circle | ||
(sample-points circle g/random-point 200) | ||
"g/random-point") | ||
(example (v/vec2 700 100) circle | ||
(g/sample-uniform (g/center circle) 10 true) | ||
"g/sample-uniform") | ||
|
||
(example (v/vec2 100 250) rectangle | ||
(sample-points rectangle g/random-point-inside) | ||
"g/random-point-inside") | ||
(example (v/vec2 300 250) rotated-rectangle | ||
(sample-points rotated-rectangle g/random-point-inside) | ||
"g/random-point-inside (polygon)") | ||
(example (v/vec2 500 250) rectangle | ||
(sample-points rectangle g/random-point 200) | ||
"g/random-point") | ||
(example (v/vec2 700 250) rectangle | ||
(g/sample-uniform (g/center rectangle) 10 true) | ||
"g/sample-uniform") | ||
|
||
(example (v/vec2 100 400) triangle | ||
(sample-points triangle g/random-point-inside) | ||
"g/random-point-inside") | ||
(example (v/vec2 300 400) triangle | ||
(sample-points triangle t/random-point-in-triangle2) | ||
"t/random-point-in-triangle2") | ||
(example (v/vec2 500 400) triangle | ||
(sample-points triangle g/random-point 200) | ||
"g/random-point") | ||
(example (v/vec2 700 400) triangle | ||
(g/sample-uniform (g/center triangle) 10 true) | ||
"g/sample-uniform") | ||
|
||
(example (v/vec2 300 550) polygon | ||
(sample-points polygon g/random-point-inside) | ||
"g/random-point-inside") | ||
(example (v/vec2 500 550) polygon | ||
(sample-points polygon g/random-point 200) | ||
"g/random-point") | ||
(example (v/vec2 700 550) polygon | ||
(g/sample-uniform (g/center polygon) 10 true) | ||
"g/sample-uniform") | ||
))) | ||
|
||
(->> (scene) | ||
adapt/all-as-svg | ||
svg/serialize | ||
(spit "out/uniform-distribution.svg")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
(ns thi.ng.geom.utils.probability | ||
(:require [thi.ng.math.core :as m])) | ||
|
||
;; TODO: move namespace to thi.ng.math.core? | ||
|
||
(defn- mapping | ||
"Create a mapping of every value in collection to f(value)." | ||
[f coll] | ||
(reduce (fn [m x] (assoc m x (f x))) {} coll)) | ||
|
||
(defn rand-weighted | ||
"Given a mapping of values to weights, randomly choose a value biased by weight" | ||
[weights] | ||
(let [sample (m/random (apply + (vals weights)))] | ||
(loop [cumulative 0.0 | ||
[[choice weight] & remaining] weights] | ||
(when weight | ||
(let [sum (+ cumulative weight)] | ||
(if (< sample sum) | ||
choice | ||
(recur sum remaining))))))) | ||
|
||
(defn rand-weighted-by | ||
"Given a sequence of values `xs`, weight each value by a function `f` and return | ||
a weighted random selection." | ||
[f xs] | ||
(rand-weighted (mapping f xs))) | ||
|
||
(comment | ||
(rand-weighted {}) | ||
(frequencies (repeatedly 1000 #(rand-weighted {:a 0.2 :b 0.8}))) | ||
(frequencies (repeatedly 1000 #(rand-weighted {:a 0.1 :b 0.7 :c 0.2}))) | ||
(frequencies (repeatedly 1000 #(rand-weighted {:a 2 :b 8 :c 32}))) | ||
(rand-weighted-by inc []) | ||
(frequencies (repeatedly 1000 #(rand-weighted-by inc [0 2 5])))) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If #89 is merged, this conversion to triangles will no longer be necessary.