1 ! (c)2009 Joe Groff bsd license
2 USING: accessors arrays grouping kernel locals math math.order
3 math.ranges math.vectors math.vectors.homogeneous sequences
5 SPECIALIZED-ARRAY: float
15 dup zero? [ recip ] unless ;
17 :: order-index-knot-constants ( curve order index -- knot-constants )
18 curve knots>> :> knots
19 index order 1 - + knots nth :> knot_i+k-1
20 index knots nth :> knot_i
21 index order + knots nth :> knot_i+k
22 index 1 + knots nth :> knot_i+1
24 knot_i+k-1 knot_i - ?recip :> c1
25 knot_i+1 knot_i+k - ?recip :> c2
28 knot_i+k c2 * neg :> c4
30 c1 c2 c3 c4 float-array{ } 4sequence ;
32 : order-knot-constants ( curve order -- knot-constants )
33 2dup [ knots>> length ] dip - iota
34 [ order-index-knot-constants ] with with map ;
36 : knot-constants ( curve -- knot-constants )
38 [ order-knot-constants ] with map ;
40 : update-knots ( curve -- curve )
41 dup knot-constants >>(knot-constants) ;
43 : <nurbs-curve> ( order control-points knots -- nurbs-curve )
44 f nurbs-curve boa update-knots ;
46 : knot-interval ( nurbs-curve t -- index )
47 [ knots>> ] dip [ > ] curry find drop 1 - ;
49 : clip-range ( from to sequence -- from' to' )
50 length min [ 0 max ] dip ;
52 :: eval-base ( knot-constants bases t -- base )
53 knot-constants first t * knot-constants third + bases first *
54 knot-constants second t * knot-constants fourth + bases second *
57 : (eval-curve) ( base-values control-points -- value )
58 [ n*v ] 2map { 0.0 0.0 0.0 } [ v+ ] binary-reduce h>v ;
60 :: (eval-bases) ( curve t interval values order -- values' )
61 order 2 - curve (knot-constants)>> nth :> all-knot-constants
62 interval order interval + all-knot-constants clip-range :> to :> from
63 from to all-knot-constants subseq :> knot-constants
64 values { 0.0 } { 0.0 } surround 2 <clumps> :> bases
66 knot-constants bases [ t eval-base ] 2map :> values'
68 [ values' from to curve control-points>> subseq (eval-curve) ]
69 [ curve t interval 1 - values' order 1 + (eval-bases) ] if ;
71 : eval-nurbs ( nurbs-curve t -- value )
72 2dup knot-interval 1 - { 1.0 } 2 (eval-bases) ;