-
Notifications
You must be signed in to change notification settings - Fork 0
/
geom-shape.stanza
105 lines (81 loc) · 4.09 KB
/
geom-shape.stanza
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
defpackage geom/shape :
import core
import math
import geom/vec
import geom/angle
import geom/mat
import geom/box
import geom/pose
import geom/poseable
import geom/bounded
import geom/geom
;;; ANYSHAPE
public defstruct AnyShapeState :
name : False|Symbol
color : Vec4
tags : Tuple<Symbol>
poses : Tuple<KeyValue<Symbol,Mat4>>
public defn default-anyshape-state () : AnyShapeState(false, WHITE, [], [])
val @doc-anyshape = "ANYSHAPE -- leaf poseables allowing more transformations"
public deftype AnyShape <: Poseable
public defmulti state (a:AnyShape) -> AnyShapeState
public defmulti clone<?T> (shape:?T&AnyShape, state:AnyShapeState) -> T
public defmethod name (p:AnyShape) : name(state(p))
public defmethod color (p:AnyShape) : color(state(p))
public defmethod tags (p:AnyShape) : tags(state(p))
public defmethod poses (p:AnyShape) : poses(state(p))
defmethod any-shapes (p:AnyShape) : [p] ;; DEFAULT
public defn bit-or (a:AnyShapeState, b:AnyShapeState) -> AnyShapeState :
AnyShapeState(name(a) when name(a) is Symbol else name(b), color(a),
to-tuple $ cat(tags(a), tags(b)), to-tuple $ cat(poses(a), poses(b)))
defmethod clone (s:AnyShape, name:False|Symbol, color:Vec4, poses:Tuple<KeyValue<Symbol,Mat4>>, tags:Tuple<Symbol>) :
clone(s, AnyShapeState(name, color, tags, poses))
public defn add-poses<?T> (s:?T&AnyShape, poses:Tuple<KeyValue<Symbol,Mat4>>) -> T :
clone(s, AnyShapeState(name(s), color(s), tags(s), poses))
public defn add-tags<?T> (s:?T&AnyShape, tags:Tuple<Symbol>) -> T :
clone(s, AnyShapeState(name(s), color(s), tags, poses(s)))
public defn add-min-x-pose<?T> (s:?T&AnyShape, dir:Double) -> T :
add-poses(s, [`min-x => pose-mat4(x3(x(lo(bounds(s)))), y3(dir * 90.0))])
public defn add-max-x-pose<?T> (s:?T&AnyShape, dir:Double) -> T :
add-poses(s, [`max-x => pose-mat4(x3(x(hi(bounds(s)))), y3(dir * -90.0))])
public defn add-min-y-pose<?T> (s:?T&AnyShape, dir:Double) -> T :
add-poses(s, [`min-y => pose-mat4(y3(y(lo(bounds(s)))), x3(dir * -90.0))])
public defn add-max-y-pose<?T> (s:?T&AnyShape, dir:Double) -> T :
add-poses(s, [`max-y => pose-mat4(y3(y(hi(bounds(s)))), x3(dir * 90.0))])
public defn add-min-z-pose<?T> (s:?T&AnyShape, dir:Double) -> T :
add-poses(s, [`min-z => pose-mat4(z3(z(lo(bounds(s)))), y3(180.0 when dir == 1.0 else 0.0 ))])
public defn add-max-z-pose<?T> (s:?T&AnyShape, dir:Double) -> T :
add-poses(s, [`max-z => pose-mat4(z3(z(hi(bounds(s)))), y3( 0.0 when dir == 1.0 else 180.0))])
public defn name (a:AnyShape, b:AnyShape) :
name(a) when name(a) is Symbol else name(b)
public defn tags (a:AnyShape, b:AnyShape) :
to-tuple $ unique $ cat(tags(a), tags(b))
public defn poses (a:AnyShape, b:AnyShape) :
to-tuple $ unique $ cat(poses(a), poses(b))
public defn color (a:AnyShape, b:AnyShape) :
color(a)
public defn dim-min (d:Double) -> Double : d * -0.5
public defn dim-max (d:Double) -> Double : d * 0.5
public defn dim-min (dim:Vec3) -> Vec3 : Vec3(dim-min(x(dim)), dim-min(y(dim)), dim-min(z(dim)))
public defn dim-max (dim:Vec3) -> Vec3 : Vec3(dim-max(x(dim)), dim-max(y(dim)), dim-max(z(dim)))
val @doc-shape = "SHAPE -- a solid object"
public deftype Shape <: AnyShape
public defn shapes (any-shapes:Seqable<AnyShape>) -> Seqable<Shape> :
generate<Shape> : for e in any-shapes do : yield(e as Shape) when e is Shape and area*(dims(bounds(e))) > 0.0
public defn shapes (p:Poseable) : shapes(any-shapes(p))
public var *resolution*:Double = 8.0
public defn set-resolution (r:Double) : *resolution* = r
;; public defmulti slice (z:Double, s:Shape3) -> Shape2
;; public defmulti pixels (z:Double, s:Shape3) -> Pixels
;; public defmulti box (b:Box3) -> Shape3
;; public defmulti cube (v:Vec3) -> Shape3
;;
;; public defmulti rect (b:Box2f) -> Shape2
;; public defmulti circle (d:Double) -> Shape2
;; public defmulti triangle (a:Vec2, b:Vec2, c:Vec2) -> Shape2
;; public defmulti extrude (h:Double, shape:Shape2) -> Shape3
;;
;; public defmulti bit-and (a:Shape3, b:Shape3) -> Shape3
;; public defmulti rem (a:Shape3, b:Shape3) -> Shape3
;; public defmulti offset (s:Shape3, o:Double) -> Shape3
public defmulti shape-eval (e) -> Geom