; ARC-TEC Discussion Paper 90-03                                  9 May 1990

;  A Self-Normalizing Term Representation of Rotation-Symmetric Workpieces

;			     Harold Boley


; A Workpiece is written as a term with the functor wp and a variable number
; of arguments representing the contour line above a symmetry axis called z.
; Each argument may be a curve term of the form
;        (curve-type begin-point end-point . further-properties)
; where curve-type is currently Line [l] or Circle-concAve [ca]. An argument
; may also be a point term (p z-coordinate x-coordinate), e.g. a degenerated
; line term that reduces to its single point.
; The RELFUN definitions first normalize the wp arguments in a call-by-value
; fashion and then check them for `well-formedness': consecutive curve terms
; must share a point, isolated points must coincide with adjacents points or
; curve points, etc.; the reduced wp term is returned. Ill-formed terms lead
; to failures [unknown].
; It is not checked whether the contour line somewhere cuts itself. Thus, wp
; acts as a (mostly) self-normalizing constructor for workpiece terms.
; Over such wp terms operations relevant for their NC-program production are
; defined (e.g. workpiece length and radius).


(setq wp '(; The empty workpiece:
(ft !(wp)
      `(wp))

; Starting from the point (0,0):
(ft !(wp (_f (p 0 0) _end . _t) . _rem)
      (wph `(_f (p 0 0) _end . _t) . _rem))
(ft !(wp (p 0 0) . _rem)
      (wph `(p 0 0) . _rem))

; Ending at some point (z,0):
(ft !(wph (_f _begin (p _z 0) . _t))
      `(wp (_f _begin (p _z 0) . _t)))
(ft !(wph (p _z 0))
      `(wp))

; Checking adjacent curve terms (first's end = second's begin; no z-touch):
(ft !(wph (_f (p _z1 _x1) (p _z2 _x2) . _t)
          (_g (p _z2 _x2) (p _z3 _x3) . _u) . _rem)
      (is t (> _x2 0))
      (is (wp . _s) (wph `(_g (p _z2 _x2) (p _z3 _x3) . _u) . _rem))
      `(wp (_f (p _z1 _x1) (p _z2 _x2) . _t) . _s))

; Joining identical adjacent points:
(ft !(wph (p _z _x) (p _z _x) . _rem)
      (wph `(p _z _x) . _rem))

; Joining a point with an adjacent curve term:
(ft !(wph (p _z _x) (_f (p _z _x) _end . _t) . _rem)
      (wph `(_f (p _z _x) _end . _t) . _rem))

; Joining a curve term with an adjacent point:
(ft !(wph (_f _begin (p _z _x) . _t) (p _z _x) . _rem)
      (wph `(_f _begin (p _z _x) . _t) . _rem))
       

; Constructing lines:
(ft !(l _begin _begin) _begin)
(ft (l _begin _end) `(l _begin _end))

; Constructing circle segments that are concave:
(ft (ca _begin _begin _center) _begin)
(ft (ca _begin _end _center)
     (is t (eql (distance _begin _center) (distance _end _center)))
     `(ca _begin _end _center))

; Constructing points:
(ft (p _z _x) `(p _z _x))

; Selecting point coordinates:
(ft (s-z (p _z _x)) _z)
(ft (s-x (p _z _x)) _x)


; Elementary geometric primitive:
(ft (distance (p _z1 _x1) (p _z2 _x2))
     (sqrt (+ (expt (- _z1 _z2) 2) (expt (- _x1 _x2) 2))))


; Specific workpiece operations:

; Workpiece length:
(ft !(wplength (wp)) 0)
(ft !(wplength (wp (_f _begin (p _z 0) . _t))) _z)
(ft (wplength (wp (_f (p _z1 _x1) (p _z2 _x2) . _t)
                  (_g (p _z2 _x2) (p _z3 _x3) . _u) . _rem))
    (wplength `(wp (_g (p _z2 _x2) (p _z3 _x3) . _u) . _rem)))

; Workpiece curve-term types:
(ft !(wptypes (wp)) (tup))
(ft !(wptypes (wp (_f _begin (p _z 0) . _t))) (tup _f))
(ft (wptypes (wp (_f (p _z1 _x1) (p _z2 _x2) . _t)
                 (_g (p _z2 _x2) (p _z3 _x3) . _u) . _rem))
    (is (tup . _s) (wptypes `(wp (_g (p _z2 _x2) (p _z3 _x3) . _u) . _rem)))
    (tup _f . _s))

; Workpiece edges (contour points):
(ft !(wpedges (wp)) (tup))
(ft !(wpedges (wp (_f _begin (p _z 0) . _t))) (tup `(p _z 0)))
(ft (wpedges (wp (_f (p _z1 _x1) (p _z2 _x2) . _t)
                 (_g (p _z2 _x2) (p _z3 _x3) . _u) . _rem))
    (is (tup . _s) (wpedges `(wp (_g (p _z2 _x2) (p _z3 _x3) . _u) . _rem)))
    (tup `(p _z1 _x1) . _s))

; Workpiece edges' x-coordinates:
(ft (wpxes (wp . _terms)) (maptup s-x (wpedges `(wp . _terms))))

; Workpiece radius (maximum x-coordinate):
(ft (wpradius (wp . _terms))
    (is (tup . _s) (wpxes `(wp . _terms)))
    (max . _s))


; Tuples:
(ft (tup . _elems) `(tup . _elems))
(ft (constup _head (tup . _tail)) `(tup _head . _tail))
(ft (maptup _f (tup)) `(tup))
(ft (maptup _f (tup _head . _tail))
    (constup (_f _head) (maptup _f (tup . _tail))))


; Two sample-workpiece normalization calls in a list:
(ft (test)  (list  (wp (l (p 0 0) (p 0 12.5))     ; this wp is normal except _a
                       (l (p 0 12.5) (p 27 44.5))
                       (l (p 27 44.5) (p 44 44.5))
                       (l (p 44 44.5) (p _a 20))
                       (l (p 44 20) (p 149 20))
                       (l (p 149 20) (p 149 15))
                       (l (p 149 15) (p 183 15))
                       (ca (p 183 15) (p 185 13) (p 185 15))
                       (l (p 185 13) (p 185 0)))
                   (wp (p 0 0)                    ; this wp needs normalization
                       (l (p 0 0) (p 0 12.5))
                       (l (p 0 12.5) (p 0 12.5))
                       (l (p 0 12.5) (p 27 44.5))
                       (p 27 44.5)
                       (l (p 27 44.5) (p 44 44.5))
                       (p 44 44.5)
                       (p 44 44.5)
                       (p 44 44.5)
                       (l (p 44 44.5) (p _a 20))
                       (l (p 44 20) (p 149 20))
                       (l (p 149 20) (p 149 15))
                       (l (p 149 15) (p 183 15))
                       (ca (p 183 15) (p 185 13) (p 185 15))
                       (l (p 185 13) (p 185 0))
                       (p 185 0)
                       (p 185 0))                            ) )
))
; 
; For workpieces that are not well-formed an unknown failure is produced:
; 
; rf> (wp (l (p 0 0) (p 0 12.5)) (l (p 0 13.5) (p 17 0)))   ; lines don't meet
; unknown
; 
; For the well-formed (test) workpieces the normal forms are returned:
; 
; rf> (test)
; ((WP
;   (L (P 0 0) (P 0 12.5))
;   (L (P 0 12.5) (P 27 44.5))
;   (L (P 27 44.5) (P 44 44.5))
;   (L (P 44 44.5) (P 44 20))
;   (L (P 44 20) (P 149 20))
;   (L (P 149 20) (P 149 15))
;   (L (P 149 15) (P 183 15))
;   (CA (P 183 15) (P 185 13) (P 185 15))
;   (L (P 185 13) (P 185 0)) )
;  (WP
;   (L (P 0 0) (P 0 12.5))
;   (L (P 0 12.5) (P 27 44.5))
;   (L (P 27 44.5) (P 44 44.5))
;   (L (P 44 44.5) (P 44 20))
;   (L (P 44 20) (P 149 20))
;   (L (P 149 20) (P 149 15))
;   (L (P 149 15) (P 183 15))
;   (CA (P 183 15) (P 185 13) (P 185 15))
;   (L (P 185 13) (P 185 0)) ) )


; The normalized workpiece abbreviated with (n):
((n)              `(wp (l (p 0 0) (p 0 12.5))
                       (l (p 0 12.5) (p 27 44.5))
                       (l (p 27 44.5) (p 44 44.5))
                       (l (p 44 44.5) (p 44 20))
                       (l (p 44 20) (p 149 20))
                       (l (p 149 20) (p 149 15))
                       (l (p 149 15) (p 183 15))
                       (ca (p 183 15) (p 185 13) (p 185 15))
                       (l (p 185 13) (p 185 0))))

; For the (n) workpiece the specific operations return the following values:
;
; rf> (wplength (n))
; 185
; rf> (wptypes (n))
; (TUP L L L L L L L CA L)
; rf> (wpedges (n))
; (TUP
;  (P 0 0)
;  (P 0 12.5)
;  (P 27 44.5)
;  (P 44 44.5)
;  (P 44 20)
;  (P 149 20)
;  (P 149 15)
;  (P 183 15)
;  (P 185 0) )
; rf> (wpxes (n))
; (TUP 0 12.5 44.5 44.5 20 20 15 15 0)
; rf> (wpradius (n))
; 44.5


; For receiving this file send an email to 
; boley@informatik.uni-kl.de
