1 ! Copyright (C) 2016-2017 Alexander Ilin.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs binary-search colors
4 combinators combinators.short-circuit kernel make
5 math math.order math.statistics math.vectors opengl opengl.gl
6 sequences specialized-arrays splitting.monotonic ui.gadgets
7 ui.gadgets.charts ui.gadgets.charts.utils ui.render ;
8 QUALIFIED-WITH: alien.c-types c
9 IN: ui.gadgets.charts.lines
11 SPECIALIZED-ARRAY: c:float
13 ! Data must be a sequence of { x y } coordinates sorted by
14 ! non-descending x vaues.
15 TUPLE: line < gadget color data ;
19 : (line-vertices) ( seq -- vertices )
20 concat [ 0.3 + ] float-array{ } map-as ;
25 : search-first ( elt seq -- index elt )
26 [ first <=> ] with search ;
28 : search-first? ( elt seq -- index elt exact-match? )
29 dupd search-first rot [ dup first ] dip = ;
31 ! Return a slice of the seq with all elements equal to elt to the
32 ! left of the index, plus one that's not equal, if requested.
33 :: adjusted-tail-slice ( n elt plus-one? seq -- slice )
34 n seq elt x '[ x _ = not ] find-last-from drop seq swap
35 [ plus-one? [ 1 + ] unless tail-slice ] when* ;
37 ! Return a slice of the seq with all elements equal to elt to the
38 ! right of the index, plus one that's not equal, if requested.
39 :: adjusted-head-slice ( n elt plus-one? seq -- slice )
40 n seq elt x '[ x _ = not ] find-from drop seq swap
41 [ plus-one? [ 1 + ] when index-or-length head-slice ] when* ;
43 ! : data-rect ( data -- rect )
44 ! [ [ first x ] [ last x ] bi ] keep
45 ! [ y ] map minmax swapd
46 ! [ 2array ] bi@ <extent-rect> ;
48 : x-in-bounds? ( min,max pairs -- ? )
50 [ [ first ] dip last x > not ]
51 [ [ second ] dip first x < not ]
54 : y-in-bounds? ( min,max pairs -- ? )
55 [ y ] map minmax 2array
57 [ [ first ] dip second > not ]
58 [ [ second ] dip first < not ]
61 ! : xy-in-bounds? ( bounds pairs -- ? )
63 ! [ [ first ] dip x-in-bounds? ]
64 ! [ [ second ] dip y-in-bounds? ]
67 : calc-line-slope ( point1 point2 -- slope ) v- first2 swap / ;
68 : calc-y ( slope x point -- y ) first2 [ - * ] dip + ;
69 : calc-x ( slope y point -- x ) first2 swap [ - swap / ] dip + ;
70 : y-at ( x point1 point2 -- y ) dupd calc-line-slope -rot calc-y ;
72 ! Due to the way adjusted-tail-slice works, the first element of
73 ! pairs is <= xmin, and if the first is < xmin, then the second is
74 ! > xmin. Otherwise the first one would be = xmin.
75 : left-cut-x ( xmin pairs -- seq )
77 [ dupd first2 y-at 2array ] keep rest-slice swap prefix
82 ! Due to the way adjusted-head-slice works, the last element of
83 ! pairs is >= xmax, and if the last is > xmax, then the second to
84 ! last is < xmax. Otherwise the last one would be = xmax.
85 : right-cut-x ( xmax pairs -- seq )
87 [ dupd last2 y-at 2array ] keep but-last-slice swap suffix
92 ! If the line spans beyond min or max, make sure there are points
93 ! with x = min and x = max in seq.
94 : min-max-cut ( min,max pairs -- seq )
95 [ first2 ] dip right-cut-x left-cut-x ;
97 : clip-by-x ( min,max pairs -- pairs' )
99 [ dup first ] dip [ search-first? not ] keep
101 [ dup second ] dip [ search-first? not ] keep
103 dup length 1 > [ min-max-cut ] [ nip ] if
104 dup slice? [ dup like ] when
109 : between<=> ( value min max -- <=> )
110 3dup between? [ 3drop +eq+ ] [ nip > +gt+ +lt+ ? ] if ;
112 : calc-point-y ( slope y point -- xy ) over [ calc-x ] dip 2array ;
114 : xyy>chunk ( x y1 y2 -- chunk )
115 overd 2array [ 2array ] dip 2array ;
117 :: 2-point-chunk ( left right ymin ymax -- chunk )
118 left last :> left-point
119 right first :> right-point
120 left-point x right-point x = [
121 left-point x ymin ymax xyy>chunk
123 left-point right-point calc-line-slope :> slope
124 slope ymin left-point calc-point-y
125 slope ymax left-point calc-point-y
126 left-point y right-point y > [ swap ] when 2array
129 :: fix-left-chunk ( left right ymin ymax -- left' )
130 left last :> left-point
131 right first :> right-point
132 left-point y right-point y {
133 [ { [ drop ymin = ] [ > ] } 2&& ]
134 [ { [ drop ymax = ] [ < ] } 2&& ]
138 left-point y right-point y > ymin ymax ? :> y-coord
139 left-point x right-point x = [
140 left-point x y-coord 2array
142 left-point right-point calc-line-slope
143 y-coord left-point calc-point-y
148 :: fix-right-chunk ( left right ymin ymax -- right' )
149 left last :> left-point
150 right first :> right-point
151 left-point y right-point y {
152 [ { [ ymin = nip ] [ < ] } 2&& ]
153 [ { [ ymax = nip ] [ > ] } 2&& ]
157 left-point y right-point y < ymin ymax ? :> y-coord
158 left-point x right-point x = [
159 right-point x y-coord 2array
161 left-point right-point calc-line-slope
162 y-coord left-point calc-point-y
167 : first-point ( chunks -- first-point ) first first ;
169 : last-point ( chunks -- last-point ) last last ;
171 :: (make-pair) ( prev next min max -- next' )
173 prev next [ first y min max between<=> ] bi@ 2array
175 { { +gt+ +eq+ } [ fix-right-chunk ] }
176 { { +lt+ +eq+ } [ fix-right-chunk ] }
177 { { +eq+ +gt+ } [ fix-left-chunk , next ] }
178 { { +eq+ +lt+ } [ fix-left-chunk , next ] }
179 { { +gt+ +lt+ } [ 2-point-chunk , next ] }
180 { { +lt+ +gt+ } [ 2-point-chunk , next ] }
181 [ drop "same values - can't happen" throw ]
184 ! Drop chunks that are out of bounds, add extra points where needed.
185 :: (drawable-chunks) ( chunks min max -- chunks' )
189 chunks first-point y min max between?
195 chunks [ ] [ min max (make-pair) ] map-reduce
196 dup first y min max between? [ , ] [ drop ] if
201 ! Split data into chunks to be drawn within the [ymin,ymax] limits.
202 ! Return the (empty?) sequence of chunks, possibly with some new
203 ! points at ymin and ymax at the gap bounds.
204 : drawable-chunks ( data ymin,ymax -- chunks )
206 '[ [ y _ _ between<=> ] bi@ = ]
207 monotonic-split-slice
208 ] 2keep (drawable-chunks) ;
210 : flip-y-axis ( chunks ymin,ymax -- chunks )
211 first2 + '[ [ _ swap - ] assoc-map ] map ;
213 ! Return quotation that can be used in map operation.
214 : scale-mapper ( width min,max -- quot: ( value -- value' ) )
215 first2 swap '[ _ swap _ _ scale ] ; inline
217 ! Sometimes no scaling is needed.
218 ! : scale-mapper ( width min,max -- quot: ( value -- value' ) )
219 ! first2 swap 3dup - = [
222 ! '[ _ swap _ _ scale ]
225 : scale-chunks ( chunks xwidth xmin,xmax yheight ymin,ymax -- chunks' )
226 [ scale-mapper ] 2bi@ '[ [ _ _ bi* ] assoc-map ] map ;
230 : draw-line ( seq -- )
231 dup [ but-last-slice ] over length odd? [ dip ] [ call ] if
233 [ (line-vertices) gl-vertex-pointer GL_LINES 0 ] keep
234 length glDrawArrays ;
236 ! bounds: { { xmin xmax } { ymin ymax } }
237 : clip-data ( bounds data -- data' )
239 dupd [ first ] dip clip-by-x
241 [ second ] dip [ y-in-bounds? ] keep swap
247 dup parent>> dup chart? [| line chart |
249 COLOR: black line [ default-color ] [ data>> ] bi
250 dupd clip-data swap second [ drawable-chunks ] keep
252 chart chart-dim first2 [ chart chart-axes first2 ] dip swap