home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
vis-ftp.cs.umass.edu
/
vis-ftp.cs.umass.edu.tar
/
vis-ftp.cs.umass.edu
/
pub
/
Software
/
ASCENDER
/
ascendMar8.tar
/
UMass
/
ISR
/
isr2rcde.lisp
< prev
next >
Wrap
Lisp/Scheme
|
1996-02-21
|
15KB
|
374 lines
;;; ISR2RCDE.LISP
;;; Interface between ISR2 and RCDE graphics routines
;;;
;;; Author: Robert T. Collins
;;; Date: April 3, 1995
;;;
;-----------------------------------------------------------------
; (c) Copyright 1995 by The University of Massachusetts
;------------------------------------------------------------------
;8/28/95 - Bob Collins
; updated read-raw-XY-data-lines-into-isr to print out a count of the
; number of lines being updated as they are processed, so that the user
; can see that something is really happening.
;7/4/95 - Bob Collins
; added approx-line-not-visible-p and modified the isr line tokenset object's
; draw-on-view-internal to display faster by ignoring lines outside of the
; view bounding box. To display portions of large linesets any faster
; will probably require the use of ISR grid structures.
(in-package 'cme)
(defclass isr-line-tokenset-object
(2d-object)
((tokenset :initform nil :initarg :tokenset :accessor tokenset)
(tokensetname :initform nil :initarg :tokensetname :accessor tokensetname)
(load-filename :initform nil :initarg :load-filename :accessor load-filename)
(c-filename :initform nil :initarg :c-filename :accessor c-filename)
(color :initform nil :initarg :color :accessor color)
(thickness :initform nil :initarg :thickness :accessor thickness)
)
(:default-initargs :color "GREEN" :thickness 1))
(define-fasd-form-init-plist isr-line-tokenset-object
(with-slot-values (tokenset tokensetname load-filename c-filename
color thickness) self
`(:tokenset (internal-read-tokenset ,tokensetname ,load-filename
,(isr2:token-count tokenset))
:tokensetname ,tokensetname
:load-filename ,load-filename
:c-filename ,c-filename
:color ,color
:thickness ,thickness)))
(defun get-or-make-ISR-tokenset (2dfs)
(let ((isrobject (find 'ISR-LINE-TOKENSET-OBJECT (inferiors 2dfs)
:key #'type-of :test #'equal)))
(unless isrobject
(let* ((2dworld (world 2dfs))
(tksname (concatenate 'string
(replace-spaces-with-hyphens (string (name 2dworld)))
(string (gensym "-lines"))))
(tks (isr2:create tksname
:token-features
'((x1 "" :real)(y1 "" :real)
(x2 "" :real)(y2 "" :real)(contrast "" :real))
:frame-features
'((numrows "" :integer) (numcols "" :integer)
(label "" :string))))
(image (car (base-image-list 2dworld))))
tks
(setf (isr2:value (isr2:handle (list tksname 'numrows)))
(if image (ceiling (image-x-dim image)) 0))
(setf (isr2:value (isr2:handle (list tksname 'numcols)))
(if image (ceiling (image-y-dim image)) 0))
(setf (isr2:value (isr2:handle (list tksname 'label))) "unused")
(setf isrobject
(make-instance 'isr-line-tokenset-object
:tokenset (isr2:handle tksname)
:tokensetname tksname
:load-filename nil
:c-filename nil
:color "GREEN"
:thickness 1))
(add-object isrobject 2dfs)))
(values isrobject (tokensetname isrobject))))
(defun write-isr-line-tokenset (stream isrobject &key (write-load-file t))
(format stream "(make-instance 'isr-line-tokenset-object~%")
(format stream " :tokenset (internal-read-tokenset ~s ~s ~d)~%"
(tokensetname isrobject) (load-filename isrobject)
(isr2::token-count (tokenset isrobject)))
(format stream " :tokensetname ~s~%" (tokensetname isrobject))
(format stream " :load-filename ~s~%" (load-filename isrobject))
(format stream " :c-filename ~s~%" (c-filename isrobject))
(format stream " :color ~s~%" (color isrobject))
(format stream " :thickness ~s~%" (thickness isrobject))
(format stream ")~%")
(when write-load-file
(unless (load-filename isrobject)
(error "ISR tokenset load-filename has not been specified!"))
(format t "Saving fast-load isr2 file ~a~%" (load-filename isrobject))
(isr2:store (tokensetname isrobject) (load-filename isrobject)))
isrobject)
(defun read-isr-line-tokenset (stream)
(eval (read stream)))
(defvar isr2rcde*%%tmpvec* (cme::make-coordinate-vector 3))
(defmacro isr2rcde-inline-transform (transform position &optional to-vector)
`(let ((transform ,transform)
(position ,position)
(to-vector ,to-vector) )
(unless to-vector (setf to-vector isr2rcde*%%tmpvec*))
(unless (vectorp position)
(setq position (cme::position-to-vector position to-vector )))
(if (null transform)
position
(cme::transform-vector transform position to-vector))))
(defun approx-line-not-visible-p (minx miny maxx maxy x1 y1 x2 y2)
" Quick, approximate check to see if a line segment could possibly
pass through the given rectangular region."
(if (< x1 x2)
(or (< x2 minx) (> x1 maxx)
(if (< y1 y2)
(or (< y2 miny) (> y1 maxy))
(or (< y1 miny) (> y2 maxy))))
(or (< x1 minx) (> x2 maxx)
(if (< y1 y2)
(or (< y2 miny) (> y1 maxy))
(or (< y1 miny) (> y2 maxy))))))
(defmethod draw-on-view-internal ((self isr-line-tokenset-object)
view drawing-context)
(with-slot-values (tokenset color thickness) self
(let ((transform (2d-to-window-transform view)))
(ic::with-drawing-context-line-width (drawing-context thickness)
(with-drawing-context-color (drawing-context color)
(bind-vector-elements (minx maxx miny maxy) (window-2d-bbox view)
(isr2::for-every-token (tok tokenset (x1 y1 x2 y2))
(let ((vx1 (isr2::value x1))
(vy1 (isr2::value y1))
(vx2 (isr2::value x2))
(vy2 (isr2::value y2)))
(unless (approx-line-not-visible-p minx miny maxx maxy
vx1 vy1 vx2 vy2)
(bind-vector-elements
(u1 v1)(isr2rcde-inline-transform transform (list vx1 vy1))
(bind-vector-elements
(u2 v2)(isr2rcde-inline-transform transform (list vx2 vy2))
(dc-draw-line drawing-context u1 v1 u2 v2))))))))))))
(defun read-ascii-lines-into-isr (tksname stream numlines &key
(docstring "Read Lines")
(transform nil)(swap-xy nil))
"Read a file of ascii data lines (x1 y1 x2 y2 contrast) into an isr2 tokenset."
(ic::noting-progress (docstring numlines :progress-var readvar)
(dotimes (i numlines tksname)
(let ((u1 (read stream))
(v1 (read stream))
(u2 (read stream))
(v2 (read stream))
(cont (read stream))
x1 y1 x2 y2)
(when swap-xy (rotatef u1 v1) (rotatef u2 v2))
(if transform
(cme::bind-vector-elements
(x y)(cme::isr2rcde-inline-transform transform
(list (coerce u1 'single-float) (coerce v1 'single-float)))
(setf x1 x y1 y)
(cme::bind-vector-elements
(x y)(cme::isr2rcde-inline-transform transform
(list (coerce u2 'single-float) (coerce v2 'single-float)))
(setf x2 x y2 y)))
;;else
(setf x1 u1 x2 u2 y1 v1 y2 v2))
(let ((newtok (isr2:create-new-token tksname)))
(setf (isr2:value (list newtok 'x1)) x1)
(setf (isr2:value (list newtok 'y1)) y1)
(setf (isr2:value (list newtok 'x2)) x2)
(setf (isr2:value (list newtok 'y2)) y2)
(setf (isr2:value (list newtok 'contrast))
(coerce cont 'single-float))))
(ic::note-progress i readvar))))
(defun make-quad-from-bbox (x1 y1 x2 y2)
(list (list x1 y1) (list x1 y2) (list x2 y2) (list x2 y1)))
(defun write-ascii-2dlines-in-pane-bbox (stream pane bbox)
(let* ((topview (cme::top-view pane))
(2dworld (cme::2d-world topview))
(pane-trans (cme::inverse-transform
(cme::2d-to-window-transform topview))))
(multiple-value-bind (minx miny maxx maxy)
(apply #'cme::transform-2d-bounding-box pane-trans bbox)
(multiple-value-bind (ignore tokenset)
(get-or-make-ISR-tokenset (find-fs-named 2dworld *2d-line-fsname*))
ignore
(isr2::for-every-token (tok tokenset (x1 y1 x2 y2 contrast))
(let ((vx1 (isr2::value x1))
(vy1 (isr2::value y1))
(vx2 (isr2::value x2))
(vy2 (isr2::value y2)))
(unless (approx-line-not-visible-p minx miny maxx maxy
vx1 vy1 vx2 vy2)
(format stream "~,2f ~,2f ~,2f ~,2f ~,2f~%"
vx1 vy1 vx2 vy2
(isr2:value contrast)))))))))
(defun create-updated-ascii-line-file (inasciifile isrtokenset outasciifile)
(with-open-file (outfile outasciifile :direction :output)
(with-open-file (file inasciifile :direction :input)
(do ((line (read-line file) (read-line file)))
((string-equal line "DATA:") (format outfile "~a~%" line))
(format outfile "~a~%" line)))
(isr2::for-every-token (tok isrtokenset (x1 y1 x2 y2 contrast))
(format outfile "~,2f ~,2f ~,2f ~,2f ~,2f~%" (isr2:value x1) (isr2:value y1)
(isr2:value x2) (isr2:value y2) (isr2:value contrast)))))
(defun internal-read-tokenset (tokensetname filename numlines)
(format t "Reading tokenset ~a containing ~d lines~%" tokensetname numlines)
(isr2::restore tokensetname filename)
(isr2::handle tokensetname))
(defun replace-spaces-with-hyphens (string)
(substitute #\- #\Space string))
(defun read-image-to-2d-transform (imagefilename)
(let ((plist (ic::get-image-file-property-list imagefilename)))
(let ((pos (position :IMAGE-TO-2D-TRANSFORM plist)))
(when pos (elt plist (+ pos 1))))))
#|==================== OLD CODE, AND CODE FOR TESTING ====================
(defun read-raw-XY-data-lines-into-isr (tksname filename &key
(numrows 0)(numcols 0)(transform nil))
"Read a file of ascii data lines (x1 y1 x2 y2 contrast) into an isr2 tokenset."
(init-number-report
(format nil "~%Loading tokenset ~a into the ISR... " tksname) 0)
(let ((tokcount 0))
(with-open-file (file filename :direction :input)
(do ((line (read-line file) (read-line file)))
((string-equal line "DATA:") nil))
(isr2:create tksname
:token-features
'((x1 "" :real)(y1 "" :real)
(x2 "" :real)(y2 "" :real)(contrast "" :real))
:frame-features
'((numrows "" :integer) (numcols "" :integer) (label "" :string)))
(setf (isr2:value (isr2:handle (list tksname 'numrows))) (ceiling numrows))
(setf (isr2:value (isr2:handle (list tksname 'numcols))) (ceiling numcols))
(setf (isr2:value (isr2:handle (list tksname 'label))) "unused")
(do ((x1 (read file nil :eof) (read file nil :eof)))
((eq x1 :eof) tksname)
(let ((y1 (read file))
(x2 (read file))
(y2 (read file))
(contrast (read file)))
(when (zerop (mod (incf tokcount) 100))
(update-number-report tokcount))
(let ((newtok (isr2:create-new-token tksname)))
(bind-vector-elements
(u1 v1)(isr2rcde-inline-transform transform
(list (coerce x1 'single-float) (coerce y1 'single-float)))
(bind-vector-elements
(u2 v2)(isr2rcde-inline-transform transform
(list (coerce x2 'single-float) (coerce y2 'single-float)))
(setf (isr2:value (list newtok 'x1)) u1)
(setf (isr2:value (list newtok 'y1)) v1)
(setf (isr2:value (list newtok 'x2)) u2)
(setf (isr2:value (list newtok 'y2)) v2)
(setf (isr2:value (list newtok 'contrast))
(coerce contrast 'single-float))
))))))
(update-number-report (isr2::token-count tksname))
(end-number-report " lines")
tksname))
(defun read-and-install-line-featureset (2dworld filename image
newasciifile newisrfile fsname)
(let* ((transform (read-image-to-2d-transform image))
(tksname (read-raw-XY-data-lines-into-isr
(concatenate 'string
(replace-spaces-with-hyphens (string (name 2dworld)))
"-lines")
filename
:transform transform
:numrows (image-x-dim (or (car (base-image-list 2dworld))
image))
:numcols (image-y-dim (or (car (base-image-list 2dworld))
image))))
(isrobject (make-instance 'isr-line-tokenset-object
:tokenset (isr2:handle tksname)
:tokensetname tksname
:load-filename (namestring newisrfile)
:c-filename (namestring newasciifile)
:color "GREEN")))
(format t "Saving updated ascii line file ~a~%" newasciifile)
(create-updated-ascii-line-file filename tksname newasciifile)
(format t "Saving fast-load isr2 file ~a~%" newisrfile)
(isr2:store tksname newisrfile)
(format t "Adding lines to view-list~%")
(let ((fs (make-2d-feature-set :world 2dworld :name fsname
:inferiors (list isrobject))))
(dolist (view (view-list 2dworld))
(add-view fs view :sensitize t)))))
> (time (cme::read-raw-XY-data-lines-into-isr 'foo file))
Loading tokenset FOO into the ISR... 4178 lines
Elapsed Real Time = 63.35 seconds (1 minute, 3.35 seconds)
Total Run Time = 59.60 seconds
User Run Time = 59.54 seconds
System Run Time = 0.06 seconds
Process Page Faults = 406
Dynamic Bytes Consed = 0
Ephemeral Bytes Consed = 5,111,856
There were 9 ephemeral GCs
(defun test-load-isr (tksname filename)
"Read a file of ascii data lines (x1 y1 x2 y2 contrast) into an isr2 tokenset."
(format t "~%Loading tokenset ~a into the ISR... " tksname)
(with-open-file (file filename :direction :input)
(isr2:create tksname
:token-features
'((x1 "" :real)(y1 "" :real)(x2 "" :real)(y2 "" :real)(contrast "" :real))
:frame-features
'((numrows "" :integer) (numcols "" :integer) (label "" :string)))
(setf (isr2:value (isr2:handle (list tksname 'numrows))) 0)
(setf (isr2:value (isr2:handle (list tksname 'numcols))) 0)
(setf (isr2:value (isr2:handle (list tksname 'label))) "unused")
(let ((x1hand (isr2:handle (list tksname "<?>x1")))
(y1hand (isr2:handle (list tksname "<?>y1")))
(x2hand (isr2:handle (list tksname "<?>x2")))
(y2hand (isr2:handle (list tksname "<?>y2")))
(cohand (isr2:handle (list tksname "<?>contrast"))))
(do ((x1 (read file nil :eof) (read file nil :eof))
(index 0 (+ index 1)))
((eq x1 :eof) tksname)
(let ((y1 (read file))
(x2 (read file))
(y2 (read file))
(contrast (read file)))
(let ((newtok (isr2:create-new-token tksname)))
(setf (isr2::handle-token x1hand) index)
(setf (isr2::handle-token x2hand) index)
(setf (isr2::handle-token y1hand) index)
(setf (isr2::handle-token y2hand) index)
(setf (isr2::handle-token cohand) index)
(setf (isr2:value x1hand) (coerce x1 'single-float))
(setf (isr2:value y1hand) (coerce y1 'single-float))
(setf (isr2:value x2hand) (coerce x2 'single-float))
(setf (isr2:value y2hand) (coerce y2 'single-float))
(setf (isr2:value cohand) (coerce contrast 'single-float))
)))))
(format t " ~d lines~%" (isr2::token-count tksname))
tksname)
> (time (cme::test-load-isr 'bar file))
Loading tokenset BAR into the ISR... 4178 lines
Elapsed Real Time = 56.69 seconds
Total Run Time = 55.55 seconds
User Run Time = 55.49 seconds
System Run Time = 0.06 seconds
Process Page Faults = 86
Dynamic Bytes Consed = 0
Ephemeral Bytes Consed = 4,210,136
There were 8 ephemeral GCs
|#