Skip to content

Commit

Permalink
[core][types][physics] update PClear protocol & impls (add support for
Browse files Browse the repository at this point in the history
mutable types), add vec benchmarks
  • Loading branch information
postspectacular committed Mar 31, 2015
1 parent 5385b4f commit d8b61f9
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 43 deletions.
16 changes: 16 additions & 0 deletions geom-core/bench/vector.org
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@
[] #(g/rotate A2 HALF_PI))
#+END_SRC

#+BEGIN_SRC clojure :noweb-ref bench
(defgoal :vec2-ops-mut "vec2-ops-mutable")

(defcase :vec2-ops-mut :add-v-ctor
[] #(g/+ (vec2 1 2) B2))

(defcase :vec2-ops-mut :add-v!
[] #(g/+! (vec2 1 2) B2))

(defcase :vec2-ops-mut :madd-v-ctor
[] #(g/madd (vec2 1 2) N B2))

(defcase :vec2-ops-mut :madd-v!
[] #(g/madd! (vec2 1 2) N B2))
#+END_SRC

** Vec3

#+BEGIN_SRC clojure :noweb-ref bench
Expand Down
3 changes: 2 additions & 1 deletion geom-core/src/core.org
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ access to entities in this context.

#+BEGIN_SRC clojure :noweb-ref protos-shape
(defprotocol PClear
(clear* [_]))
(clear* [_])
(clear! [_]))
#+END_SRC

**** TODO PClip
Expand Down
6 changes: 4 additions & 2 deletions geom-core/src/vector.org
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@
*** PClear
#+BEGIN_SRC clojure :noweb-ref vec2
g/PClear
(clear* [_] V2)
(clear* [_] (Vec2. #+clj (double-array 2) #+cljs (js/Float32Array. 2) nil nil))
(clear! [_] (aset buf 0 0.0) (aset buf 1 0.0) (set! _hash nil) _)
#+END_SRC
*** PCrossProduct

Expand Down Expand Up @@ -1149,7 +1150,8 @@

#+BEGIN_SRC clojure :noweb-ref vec3
g/PClear
(clear* [_] V3)
(clear* [_] (Vec3. #+clj (double-array 3) #+cljs (js/Float32Array. 3) nil nil))
(clear! [_] (aset buf 0 0.0) (aset buf 1 0.0) (aset buf 2 0.0) (set! _hash nil) _)
#+END_SRC

*** PCrossProduct
Expand Down
4 changes: 2 additions & 2 deletions geom-core/test/core.org
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
(is (delta= [(* ax) (* ay)] (g/* a2)) "* a2")
(is (delta= [(/ ax) (/ ay)] (g/div a2)) "/ a2")
(is (delta= [(m/abs ax) (m/abs ay)] (g/abs (g/- a2))) "abs a2")
(is (identical? V2 (g/clear* a2)) "clear a2")
(is (= V2 (g/clear* a2)) "clear a2")
(context-vec2-op "+" g/+ + 0.0)
(context-vec2-op "-" g/- - 0.0)
(context-vec2-op "*" g/* * 0.0)
Expand All @@ -286,7 +286,7 @@
(is (delta= [(* ax) (* ay) (* az)] (g/* a3))) "* a3"
(is (delta= [(/ ax) (/ ay) (/ az)] (g/div a3))) "/ a3"
(is (delta= [(m/abs ax) (m/abs ay) (m/abs az)] (g/abs (g/- a3))) "abs a3")
(is (identical? V3 (g/clear* a3)) "clear a3")
(is (= V3 (g/clear* a3)) "clear a3")
(context-vec3-op "+" g/+ + 0.0)
(context-vec3-op "-" g/- - 0.0)
(context-vec3-op "*" g/* * 0.0)
Expand Down
10 changes: 5 additions & 5 deletions geom-physics/src/core.org
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@
(velocity
[_] (g/- pos prev))
(add-force
[_ f] (set! force (g/+ force f)) _)
[_ f] (g/+! force f) _)
(clear-force
[_] (set! force (g/clear* force)))
[_] (g/clear! force) _)
(apply-force
[_ delta]
(let [pos' (g/madd force (* inv-weight (* delta delta)) (g/msub pos 2.0 prev))]
(let [pos' (g/madd! force (* inv-weight (* delta delta)) (g/msub pos 2.0 prev))]
(set! prev pos)
(set! pos pos')
(set! force (g/clear* force)))
Expand Down Expand Up @@ -170,7 +170,7 @@
(if-not (locked? a)
(set-position a (g/madd diff (* nd aw) pa)))
(if-not (locked? b)
(set-position b (g/madd diff (* (- nd) bw) pb))))))
(set-position b (g/madd! diff (* (- nd) bw) pb))))))

(defrecord PullbackSpring
[^VerletParticle a
Expand All @@ -192,7 +192,7 @@
(if-not (locked? a)
(set-position a (g/madd diff (* nd aw) pa)))
(if-not (locked? b)
(set-position b (g/madd diff (* (- nd) bw) pb))))))))
(set-position b (g/madd! diff (* (- nd) bw) pb))))))))
#+END_SRC

*** VerletPhysics
Expand Down
82 changes: 49 additions & 33 deletions geom-types/src/spatialtree.org
Original file line number Diff line number Diff line change
Expand Up @@ -178,30 +178,35 @@
(get-point-data [_] data)

g/PClear
(clear* [_]
(clear*
[_] (MutableQuadtreeNode. x y w h nil nil nil bounds))
(clear!
[_]
(set! children nil)
(set! children point)
(set! children data)
_)

PTreeOps
(child-index-for-point [_ [px py]]
(child-index-for-point
[_ [px py]]
(if (< px (+ x w))
(if (< py (+ y h)) 0 2)
(if (< py (+ y h)) 1 3)))
(child-for-point
[_ p] (if children (children (child-index-for-point _ p))))

(make-child-for-point [_ p d add?]
(make-child-for-point
[_ p d add?]
(let [idx (child-index-for-point _ p)]
(or (children idx)
(let [cx (if (pos? (bit-and idx 1)) (+ x w) x)
cy (if (pos? (bit-and idx 2)) (+ y h) y)
c (MutableQuadtreeNode.
cx cy (* 0.5 w) (* 0.5 h) nil (if add? p) (if add? d) nil)]
(let [cx (if (> (bit-and idx 1) 0) (+ x w) x)
cy (if (> (bit-and idx 2) 0) (+ y h) y)
c (MutableQuadtreeNode.
cx cy (* 0.5 w) (* 0.5 h) nil (if add? p) (if add? d) nil)]
(set-child _ idx c)
c))))
(split-node [_]
(split-node
[_]
(set! children [nil nil nil nil])
(set! point nil)
(set! data nil)
Expand All @@ -212,20 +217,23 @@
(set-point [_ p d] (set! point p) (set! data d) _)

g/PBounds
(bounds [_]
(bounds
[_]
(if bounds
bounds
(set! bounds
(thi.ng.geom.types.Rect2.
(vec2 x y) (vec2 (* w 2.0) (* h 2.0))))))

Object
(toString [_] (str "#thi.ng.geom.spatialtree.MutableQuadtreeNode"
"{:bounds " (pr-str (g/bounds _))
" :children " (pr-str children)
" :p " (pr-str point)
" :d " (pr-str data)
"}")))
(toString
[_]
(str "#thi.ng.geom.spatialtree.MutableQuadtreeNode"
"{:bounds " (pr-str (g/bounds _))
" :children " (pr-str children)
" :p " (pr-str point)
" :d " (pr-str data)
"}")))
#+END_SRC

*** Octree implementation
Expand All @@ -250,33 +258,38 @@
(get-point-data [_] data)

g/PClear
(clear* [_]
(clear*
[_] (MutableOctreeNode. x y z w h d nil nil nil bounds))
(clear!
[_]
(set! children nil)
(set! children point)
(set! children data)
_)

PTreeOps
(child-index-for-point [_ [px py pz]]
(child-index-for-point
[_ [px py pz]]
(+ (if (< pz (+ z d)) 0 4)
(if (< px (+ x w))
(if (< py (+ y h)) 0 2)
(if (< py (+ y h)) 1 3))))
(child-for-point
[_ p] (if children (children (child-index-for-point _ p))))

(make-child-for-point [_ p data add?]
(make-child-for-point
[_ p data add?]
(let [idx (child-index-for-point _ p)]
(if (children idx)
(children idx)
(let [cx (if (pos? (bit-and idx 1)) (+ x w) x)
cy (if (pos? (bit-and idx 2)) (+ y h) y)
cz (if (pos? (bit-and idx 4)) (+ z d) z)
c (MutableOctreeNode.
cx cy cz (* 0.5 w) (* 0.5 h) (* 0.5 d) nil (if add? p) (if add? data) nil)]
(let [cx (if (> (bit-and idx 1) 0) (+ x w) x)
cy (if (> (bit-and idx 2) 0) (+ y h) y)
cz (if (> (bit-and idx 4) 0) (+ z d) z)
c (MutableOctreeNode.
cx cy cz (* 0.5 w) (* 0.5 h) (* 0.5 d) nil (if add? p) (if add? data) nil)]
(set-child _ idx c)
c))))
(split-node [_]
(split-node
[_]
(set! children [nil nil nil nil nil nil nil nil])
(set! point nil)
_)
Expand All @@ -286,20 +299,23 @@
(set-point [_ p d] (set! point p) (set! data d) _)

g/PBounds
(bounds [_]
(bounds
[_]
(if bounds
bounds
(set! bounds
(thi.ng.geom.types.AABB.
(vec3 x y z) (vec3 (* w 2.0) (* h 2.0) (* d 2.0))))))

Object
(toString [_] (str "#thi.ng.geom.spatialtree.MutableOctreeNode"
"{:bounds " (pr-str (g/bounds _))
" :children " (pr-str children)
" :p " (pr-str point)
" :d " (pr-str data)
"}")))
(toString
[_]
(str "#thi.ng.geom.spatialtree.MutableOctreeNode"
"{:bounds " (pr-str (g/bounds _))
" :children " (pr-str children)
" :p " (pr-str point)
" :d " (pr-str data)
"}")))
#+END_SRC

*** Override print methods
Expand Down

0 comments on commit d8b61f9

Please sign in to comment.