]> gitweb.factorcode.org Git - factor.git/blob - extra/ui/gadgets/charts/lines/lines.factor
sequences: move last2 to the sequences vocab
[factor.git] / extra / ui / gadgets / charts / lines / lines.factor
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
10
11 SPECIALIZED-ARRAY: c:float
12
13 ! Data must be a sequence of { x y } coordinates sorted by
14 ! non-descending x vaues.
15 TUPLE: line < gadget color data ;
16
17 <PRIVATE
18
19 : (line-vertices) ( seq -- vertices )
20     concat [ 0.3 + ] float-array{ } map-as ;
21
22 ALIAS: x first
23 ALIAS: y second
24
25 : search-first ( elt seq -- index elt )
26     [ first <=> ] with search ;
27
28 : search-first? ( elt seq -- index elt exact-match? )
29     dupd search-first rot [ dup first ] dip = ;
30
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* ;
36
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* ;
42
43 ! : data-rect ( data -- rect )
44 !    [ [ first x ] [ last x ] bi ] keep
45 !    [ y ] map minmax swapd
46 !    [ 2array ] bi@ <extent-rect> ;
47
48 : x-in-bounds? ( min,max pairs -- ? )
49     {
50         [ [ first ] dip last x > not ]
51         [ [ second ] dip first x < not ]
52     } 2&& ;
53
54 : y-in-bounds? ( min,max pairs -- ? )
55     [ y ] map minmax 2array
56     {
57         [ [ first ] dip second > not ]
58         [ [ second ] dip first < not ]
59     } 2&& ;
60
61 ! : xy-in-bounds? ( bounds pairs -- ? )
62 !    {
63 !        [ [ first ] dip x-in-bounds? ]
64 !        [ [ second ] dip y-in-bounds? ]
65 !    } 2&& ;
66
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 ;
71
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 )
76     2dup first x > [
77         [ dupd first2 y-at 2array ] keep rest-slice swap prefix
78     ] [
79         nip
80     ] if ;
81
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 )
86     2dup last x < [
87         [ dupd last2 y-at 2array ] keep but-last-slice swap suffix
88     ] [
89         nip
90     ] if ;
91
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 ;
96
97 : clip-by-x ( min,max pairs -- pairs' )
98     2dup x-in-bounds? [
99         [ dup first ] dip [ search-first? not ] keep
100         adjusted-tail-slice
101         [ dup second ] dip [ search-first? not ] keep
102         adjusted-head-slice
103         dup length 1 > [ min-max-cut ] [ nip ] if
104         dup slice? [ dup like ] when
105     ] [
106         2drop { }
107     ] if ;
108
109 : between<=> ( value min max -- <=> )
110     3dup between? [ 3drop +eq+ ] [ nip > +gt+ +lt+ ? ] if ;
111
112 : calc-point-y ( slope y point -- xy ) over [ calc-x ] dip 2array ;
113
114 : xyy>chunk ( x y1 y2 -- chunk )
115     overd 2array [ 2array ] dip 2array ;
116
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
122     ] [
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
127     ] if ;
128
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&& ]
135     } 2|| [
136         left
137     ] [
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
141         ] [
142             left-point right-point calc-line-slope
143             y-coord left-point calc-point-y
144         ] if
145         left swap suffix
146     ] if ;
147
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&& ]
154     } 2|| [
155         right
156     ] [
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
160         ] [
161             left-point right-point calc-line-slope
162             y-coord left-point calc-point-y
163         ] if
164         right swap prefix
165     ] if ;
166
167 : first-point ( chunks -- first-point ) first first ;
168
169 : last-point ( chunks -- last-point ) last last ;
170
171 :: (make-pair) ( prev next min max -- next' )
172     prev next min max
173     prev next [ first y min max between<=> ] bi@ 2array
174     {
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 ]
182     } case ;
183
184 ! Drop chunks that are out of bounds, add extra points where needed.
185 :: (drawable-chunks) ( chunks min max -- chunks' )
186     chunks length {
187         { 0 [ chunks ] }
188         { 1 [
189                 chunks first-point y min max between?
190                 chunks { } ?
191             ]
192         }
193         [
194             drop [
195                 chunks [ ] [ min max (make-pair) ] map-reduce
196                 dup first y min max between? [ , ] [ drop ] if
197             ] { } make
198         ]
199     } case ;
200
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 )
205     first2 [
206         '[ [ y _ _ between<=> ] bi@ = ]
207         monotonic-split-slice
208     ] 2keep (drawable-chunks) ;
209
210 : flip-y-axis ( chunks ymin,ymax -- chunks )
211     first2 + '[ [ _ swap - ] assoc-map ] map ;
212
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
216
217 ! Sometimes no scaling is needed.
218 ! : scale-mapper ( width min,max -- quot: ( value -- value' ) )
219 !    first2 swap 3dup - = [
220 !        3drop [ ]
221 !    ] [
222 !        '[ _ swap _ _ scale ]
223 !    ] if ; inline
224
225 : scale-chunks ( chunks xwidth xmin,xmax yheight ymin,ymax -- chunks' )
226     [ scale-mapper ] 2bi@ '[ [ _ _ bi* ] assoc-map ] map ;
227
228 PRIVATE>
229
230 : draw-line ( seq -- )
231     dup [ but-last-slice ] over length odd? [ dip ] [ call ] if
232     rest-slice append
233     [ (line-vertices) gl-vertex-pointer GL_LINES 0 ] keep
234     length glDrawArrays ;
235
236 ! bounds: { { xmin xmax } { ymin ymax } }
237 : clip-data ( bounds data -- data' )
238     dup empty? [ nip ] [
239         dupd [ first ] dip clip-by-x
240         dup empty? [ nip ] [
241             [ second ] dip [ y-in-bounds? ] keep swap
242             [ drop { } ] unless
243         ] if
244     ] if ;
245
246 M: line draw-gadget*
247     dup parent>> dup chart? [| line chart |
248         chart chart-axes
249         COLOR: black line [ default-color ] [ data>> ] bi
250         dupd clip-data swap second [ drawable-chunks ] keep
251         flip-y-axis
252         chart chart-dim first2 [ chart chart-axes first2 ] dip swap
253         scale-chunks
254         [ draw-line ] each
255     ] [ 2drop ] if ;