1 ! Copyright (C) 2008 Jeff Bigot
\r
2 ! See http://factorcode.org/license.txt for BSD license.
\r
44 ! -------------------------------------------------------------
\r
46 VALUE: remove-hidden-solids?
\r
47 VALUE: VERY-SMALL-NUM
\r
49 VALUE: MAX-FACE-PER-CORNER
\r
51 t \ remove-hidden-solids? set-value
\r
52 0.0000001 \ VERY-SMALL-NUM set-value
\r
53 0.0000001 \ ZERO-VALUE set-value
\r
54 4 \ MAX-FACE-PER-CORNER set-value
\r
55 ! -------------------------------------------------------------
\r
56 ! sequence complement
\r
58 : with-pv ( i quot -- ) [ swap >pv call ] with-scope ; inline
\r
60 : dimension ( array -- x ) length 1 - ; inline
\r
61 : change-last ( seq quot -- )
\r
62 [ [ dimension ] keep ] dip change-nth ; inline
\r
64 ! -------------------------------------------------------------
\r
66 ! -------------------------------------------------------------
\r
68 TUPLE: light name { direction array } color ;
\r
69 : <light> ( -- tuple ) light new ;
\r
71 ! -------------------------------------------------------------
\r
72 ! halfspace manipulation
\r
73 ! -------------------------------------------------------------
\r
75 : constant+ ( v x -- w ) '[ [ _ + ] change-last ] keep ;
\r
76 : translate ( u v -- w ) dupd v* sum constant+ ;
\r
78 : transform ( u matrix -- w )
\r
79 [ swap m.v ] 2keep ! compute new normal vector
\r
81 [ [ abs ZERO-VALUE > ] find ] keep
\r
82 ! find a point on the frontier
\r
83 ! be sure it's not null vector
\r
85 swap /f neg swap ! intercept value
\r
89 [ * ] with map ! apply intercep value
\r
92 suffix ! add value as constant at the end of equation
\r
95 : position-point ( halfspace v -- x )
\r
96 -1 suffix v* sum ; inline
\r
97 : point-inside-halfspace? ( halfspace v -- ? )
\r
98 position-point VERY-SMALL-NUM > ;
\r
99 : point-inside-or-on-halfspace? ( halfspace v -- ? )
\r
100 position-point VERY-SMALL-NUM neg > ;
\r
101 : project-vector ( seq -- seq )
\r
102 pv> [ head ] [ 1 + tail ] 2bi append ;
\r
103 : get-intersection ( matrice -- seq )
\r
104 [ 1 tail* ] map flip first ;
\r
106 : islenght=? ( seq n -- seq n ? ) 2dup [ length ] [ = ] bi* ;
\r
108 : compare-nleft-to-identity-matrix ( seq n -- ? )
\r
109 [ [ head ] curry map ] keep identity-matrix m-
\r
111 [ abs ZERO-VALUE < ] all?
\r
114 : valid-solution? ( matrice n -- ? )
\r
116 [ compare-nleft-to-identity-matrix ]
\r
117 [ 2drop f ] if ; inline
\r
119 : intersect-hyperplanes ( matrice -- seq )
\r
120 [ solution dup ] [ first dimension ] bi
\r
121 valid-solution? [ get-intersection ] [ drop f ] if ;
\r
123 ! -------------------------------------------------------------
\r
125 ! -------------------------------------------------------------
\r
127 TUPLE: face { halfspace array }
\r
128 touching-corners adjacent-faces ;
\r
129 : <face> ( v -- tuple ) face new swap >>halfspace ;
\r
130 : flip-face ( face -- face ) [ vneg ] change-halfspace ;
\r
131 : erase-face-touching-corners ( face -- face )
\r
132 f >>touching-corners ;
\r
133 : erase-face-adjacent-faces ( face -- face )
\r
134 f >>adjacent-faces ;
\r
135 : faces-intersection ( faces -- v )
\r
136 [ halfspace>> ] map intersect-hyperplanes ;
\r
137 : face-translate ( face v -- face )
\r
138 [ translate ] curry change-halfspace ; inline
\r
139 : face-transform ( face m -- face )
\r
140 [ transform ] curry change-halfspace ; inline
\r
141 : face-orientation ( face -- x ) pv> swap halfspace>> nth sgn ;
\r
142 : backface? ( face -- face ? ) dup face-orientation 0 <= ;
\r
143 : pv-factor ( face -- f face )
\r
144 halfspace>> [ pv> swap nth [ * ] curry ] keep ; inline
\r
145 : suffix-touching-corner ( face corner -- face )
\r
146 [ suffix ] curry change-touching-corners ; inline
\r
147 : real-face? ( face -- ? )
\r
148 [ touching-corners>> length ]
\r
149 [ halfspace>> dimension ] bi >= ;
\r
151 : (add-to-adjacent-faces) ( face face -- face )
\r
152 over adjacent-faces>> 2dup member?
\r
153 [ 2drop ] [ swap suffix >>adjacent-faces ] if ;
\r
155 : add-to-adjacent-faces ( face face -- face )
\r
156 2dup = [ drop ] [ (add-to-adjacent-faces) ] if ;
\r
158 : update-adjacent-faces ( faces corner -- )
\r
159 '[ [ _ suffix-touching-corner drop ] each ] keep
\r
161 [ first ] keep second
\r
162 [ add-to-adjacent-faces drop ] 2keep
\r
163 swap add-to-adjacent-faces drop
\r
166 : face-project-dim ( face -- x ) halfspace>> length 2 - ;
\r
168 : apply-light ( color light normal -- u )
\r
169 over direction>> v.
\r
172 [ color>> swap ] dip
\r
173 [ * ] curry map v+
\r
180 : enlight-projection ( array face -- color )
\r
181 ! array = lights + ambient color
\r
182 [ [ third ] [ second ] [ first ] tri ]
\r
183 [ halfspace>> project-vector normalize ] bi*
\r
184 [ apply-light ] curry each
\r
188 : (intersection-into-face) ( face-init face-adja quot -- face )
\r
190 [ [ pv-factor ] bi@
\r
195 [ touching-corners>> ] bi@
\r
196 [ swap [ = ] curry find nip f = ] curry find nip
\r
201 point-inside-halfspace? [ vneg ] unless
\r
206 : intersection-into-face ( face-init face-adja -- face )
\r
207 [ [ project-vector ] bi@ ] (intersection-into-face) ;
\r
209 : intersection-into-silhouette-face ( face-init face-adja -- face )
\r
210 [ ] (intersection-into-face) ;
\r
212 : intersections-into-faces ( face -- faces )
\r
214 adjacent-faces>> [ intersection-into-face ] with map
\r
217 : (face-silhouette) ( face -- faces )
\r
218 clone dup adjacent-faces>>
\r
220 [ intersection-into-silhouette-face ] [ 2drop f ] if
\r
225 : face-silhouette ( face -- faces )
\r
226 backface? [ drop f ] [ (face-silhouette) ] if ;
\r
228 ! --------------------------------
\r
230 ! -------------------------------------------------------------
\r
231 TUPLE: solid dimension silhouettes
\r
232 faces corners adjacencies-valid color name ;
\r
234 : <solid> ( -- tuple ) solid new ;
\r
236 : suffix-silhouettes ( solid silhouette -- solid )
\r
237 [ suffix ] curry change-silhouettes ;
\r
239 : suffix-face ( solid face -- solid )
\r
240 [ suffix ] curry change-faces ;
\r
241 : suffix-corner ( solid corner -- solid )
\r
242 [ suffix ] curry change-corners ;
\r
243 : erase-solid-corners ( solid -- solid ) f >>corners ;
\r
245 : erase-silhouettes ( solid -- solid )
\r
246 dup dimension>> f <array> >>silhouettes ;
\r
247 : filter-real-faces ( solid -- solid )
\r
248 [ [ real-face? ] filter ] change-faces ;
\r
249 : initiate-solid-from-face ( face -- solid )
\r
250 face-project-dim <solid> swap >>dimension ;
\r
252 : erase-old-adjacencies ( solid -- solid )
\r
253 erase-solid-corners
\r
254 [ dup [ erase-face-touching-corners
\r
255 erase-face-adjacent-faces drop ] each ]
\r
258 : point-inside-or-on-face? ( face v -- ? )
\r
259 [ halfspace>> ] dip point-inside-or-on-halfspace? ;
\r
261 : point-inside-face? ( face v -- ? )
\r
262 [ halfspace>> ] dip point-inside-halfspace? ;
\r
264 : point-inside-solid? ( solid point -- ? )
\r
265 [ faces>> ] dip [ point-inside-face? ] curry all? ; inline
\r
267 : point-inside-or-on-solid? ( solid point -- ? )
\r
269 [ point-inside-or-on-face? ] curry all? ; inline
\r
271 : unvalid-adjacencies ( solid -- solid )
\r
272 erase-old-adjacencies f >>adjacencies-valid
\r
273 erase-silhouettes ;
\r
275 : add-face ( solid face -- solid )
\r
276 suffix-face unvalid-adjacencies ;
\r
278 : cut-solid ( solid halfspace -- solid ) <face> add-face ;
\r
280 : slice-solid ( solid face -- solid1 solid2 )
\r
281 [ [ clone ] bi@ flip-face add-face
\r
282 [ "/outer/" append ] change-name ] 2keep
\r
283 add-face [ "/inner/" append ] change-name ;
\r
288 : add-silhouette ( solid -- solid )
\r
290 ! find-adjacencies
\r
292 [ face-silhouette append ] reduce
\r
296 over dimension>> >>dimension
\r
297 over name>> " silhouette " append
\r
298 pv> number>string append
\r
300 ! ensure-adjacencies
\r
301 suffix-silhouettes ; inline
\r
303 : find-silhouettes ( solid -- solid )
\r
305 dup dimension>> [ [ add-silhouette ] with-pv ] each ;
\r
307 : ensure-silhouettes ( solid -- solid )
\r
308 dup silhouettes>> [ f = ] all?
\r
309 [ find-silhouettes ] when ;
\r
313 : corner-added? ( solid corner -- ? )
\r
314 ! add corner to solid if it is inside solid
\r
316 [ point-inside-or-on-solid? ]
\r
317 [ swap corners>> member? not ]
\r
319 [ suffix-corner drop t ] [ 2drop f ] if ;
\r
321 : process-corner ( solid faces corner -- )
\r
323 [ corner-added? ] keep swap ! test if corner is inside solid
\r
324 [ update-adjacent-faces ]
\r
328 : compute-intersection ( solid faces -- )
\r
329 dup faces-intersection
\r
330 dup f = [ 3drop ] [ process-corner ] if ;
\r
332 : test-faces-combinaisons ( solid n -- )
\r
333 [ dup faces>> ] dip among
\r
334 [ compute-intersection ] with each ;
\r
336 : compute-adjacencies ( solid -- solid )
\r
337 dup dimension>> [ >= ] curry
\r
338 [ keep swap ] curry MAX-FACE-PER-CORNER swap
\r
339 [ [ test-faces-combinaisons ] 2keep 1 - ] while drop ;
\r
341 : find-adjacencies ( solid -- solid )
\r
342 erase-old-adjacencies
\r
343 compute-adjacencies
\r
345 t >>adjacencies-valid ;
\r
347 : ensure-adjacencies ( solid -- solid )
\r
348 dup adjacencies-valid>>
\r
349 [ find-adjacencies ] unless
\r
353 : (non-empty-solid?) ( solid -- ? )
\r
354 [ dimension>> ] [ corners>> length ] bi < ;
\r
355 : non-empty-solid? ( solid -- ? )
\r
356 ensure-adjacencies (non-empty-solid?) ;
\r
358 : compare-corners-roughly ( corner corner -- ? )
\r
360 ! : remove-inner-faces ( -- ) ;
\r
361 : face-project ( array face -- seq )
\r
364 [ [ enlight-projection ]
\r
365 [ initiate-solid-from-face ]
\r
366 [ intersections-into-faces ] tri
\r
371 : solid-project ( lights ambient solid -- solids )
\r
373 [ color>> ] [ faces>> ] bi [ 3array ] dip
\r
374 [ face-project ] with map
\r
376 [ ensure-adjacencies ] map
\r
379 : (solid-move) ( solid v move -- solid )
\r
380 curry [ map ] curry
\r
381 [ dup faces>> ] dip call drop
\r
382 unvalid-adjacencies ; inline
\r
384 : solid-translate ( solid v -- solid )
\r
385 [ face-translate ] (solid-move) ;
\r
386 : solid-transform ( solid m -- solid )
\r
387 [ face-transform ] (solid-move) ;
\r
389 : find-corner-in-silhouette ( s1 s2 -- elt bool )
\r
390 pv> swap silhouettes>> nth
\r
392 [ point-inside-solid? ] with find swap ;
\r
394 : valid-face-for-order ( solid point -- face )
\r
395 [ point-inside-face? not ]
\r
396 [ drop face-orientation 0 = not ] 2bi and ;
\r
398 : check-orientation ( s1 s2 pt -- int )
\r
399 [ nip faces>> ] dip
\r
400 [ valid-face-for-order ] curry find swap
\r
401 [ face-orientation ] [ drop f ] if ;
\r
403 : (order-solid) ( s1 s2 -- int )
\r
404 2dup find-corner-in-silhouette
\r
405 [ check-orientation ] [ 3drop f ] if ;
\r
407 : order-solid ( solid solid -- i )
\r
410 [ swap (order-solid)
\r
414 : subtract ( solid1 solid2 -- solids )
\r
415 faces>> swap clone ensure-adjacencies ensure-silhouettes
\r
416 [ swap slice-solid drop ] curry map
\r
417 [ non-empty-solid? ] filter
\r
418 [ ensure-adjacencies ] map
\r
421 ! -------------------------------------------------------------
\r
423 ! -------------------------------------------------------------
\r
424 TUPLE: space name dimension solids ambient-color lights ;
\r
425 : <space> ( -- space ) space new ;
\r
426 : suffix-solids ( space solid -- space )
\r
427 [ suffix ] curry change-solids ; inline
\r
428 : suffix-lights ( space light -- space )
\r
429 [ suffix ] curry change-lights ; inline
\r
430 : clear-space-solids ( space -- space ) f >>solids ;
\r
432 : space-ensure-solids ( space -- space )
\r
433 [ [ ensure-adjacencies ] map ] change-solids ;
\r
434 : eliminate-empty-solids ( space -- space )
\r
435 [ [ non-empty-solid? ] filter ] change-solids ;
\r
437 : projected-space ( space solids -- space )
\r
438 swap dimension>> 1 - <space>
\r
439 swap >>dimension swap >>solids ;
\r
441 : get-silhouette ( solid -- silhouette )
\r
442 silhouettes>> pv> swap nth ;
\r
443 : solid= ( solid solid -- ? ) [ corners>> ] same? ;
\r
445 : space-apply ( space m quot -- space )
\r
446 curry [ map ] curry [ dup solids>> ] dip
\r
447 [ call ] [ 2drop ] recover drop ; inline
\r
448 : space-transform ( space m -- space )
\r
449 [ solid-transform ] space-apply ;
\r
450 : space-translate ( space v -- space )
\r
451 [ solid-translate ] space-apply ;
\r
453 : describe-space ( space -- )
\r
455 [ [ corners>> [ pprint ] each ] [ name>> . ] bi ] each ;
\r
457 : clip-solid ( solid solid -- solids )
\r
460 [ order-solid -1 = ] 2tri
\r
462 [ get-silhouette subtract ]
\r
468 : (solids-silhouette-subtract) ( solids solid -- solids )
\r
469 [ clip-solid append ] curry { } -rot each ; inline
\r
471 : solids-silhouette-subtract ( solids i solid -- solids )
\r
472 ! solids is an array of 1 solid arrays
\r
473 [ (solids-silhouette-subtract) ] curry map-but
\r
476 : remove-hidden-solids ( space -- space )
\r
477 ! We must include each solid in a sequence because
\r
478 ! during substration
\r
479 ! a solid can be divided in more than on solid
\r
481 [ [ 1array ] map ]
\r
485 [ solids-silhouette-subtract ] 2each
\r
486 { } [ append ] reduce
\r
488 eliminate-empty-solids ! TODO include into change-solids
\r
491 : space-project ( space i -- space )
\r
494 remove-hidden-solids? [ remove-hidden-solids ] when
\r
498 [ ambient-color>> ] tri
\r
499 [ rot solid-project ] 2curry
\r
501 [ append ] { } -rot each
\r
502 ! TODO project lights
\r
504 ! remove-inner-faces
\r
506 eliminate-empty-solids
\r
508 ] [ 3drop <space> ] recover
\r
511 : middle-of-space ( space -- point )
\r
512 solids>> [ corners>> ] map concat
\r
513 [ [ ] [ v+ ] map-reduce ] [ length ] bi v/n
\r
516 ! -------------------------------------------------------------
\r
518 ! -------------------------------------------------------------
\r
520 : face-reference ( face -- halfspace point vect )
\r
522 [ touching-corners>> first ]
\r
523 [ touching-corners>> second ] tri
\r
527 : theta ( v halfspace point vect -- v x )
\r
528 [ [ over ] dip v- ] dip
\r
529 [ cross dup norm >float ]
\r
537 : ordered-face-points ( face -- corners )
\r
538 [ touching-corners>> 1 head ]
\r
539 [ touching-corners>> 1 tail ]
\r
540 [ face-reference [ theta ] 3curry ] tri
\r
541 { } map>assoc sort-values keys
\r
545 : point->GL ( point -- ) gl-vertex ;
\r
546 : points->GL ( array -- ) do-cycle [ point->GL ] each ;
\r
548 : face->GL ( face color -- )
\r
549 [ ordered-face-points ] dip
\r
550 [ first3 1.0 glColor4d GL_POLYGON
\r
551 [ [ point->GL ] each ] do-state ] curry
\r
552 [ 0 0 0 1 glColor4d GL_LINE_LOOP
\r
553 [ [ point->GL ] each ] do-state ]
\r
557 : solid->GL ( solid -- )
\r
560 [ face->GL ] curry each ; inline
\r
562 : space->GL ( space -- )
\r
564 [ solid->GL ] each ;
\r