Here is something I originally posted in general programming on the subject of contouring.
With some corrections...!
If you look at the following link
http://paulbourke.net/papers/conrec/ there are some explanation
on what is involved for determining a contour through a single triangle.
Here I have crudely translated the C routine proposed in that page to Autolisp (Tested and No attempt at Optimization)
;Create a contour slice through a 3 vertex facet pa, pb, pc
;The contour "level" is a horizontal plane perpendicular to the z axis,
;ie: The equation of the contour plane Ax + By + Cz + D = 0
;has A = 0, B = 0, C = 1, D = -level
; Return:
; ((nil nil nil) (nil nil nil) 0) if the contour plane doesn't cut the facet
; ( (x y z) (x y z) 2) if it does cut the facet
; ((nil nil nil) (nil nil nil) -1) for an unexpected occurrence
;If a vertex touches the contour plane nothing need to be drawn
(defun contourfacet (triangle level / pa pb pc sidea sideb sidec p1x p1y p1z p2x p2y p2z)
(setq pa (car triangle)
pb (cadr triangle)
pc (caddr triangle)
)
(setq sidea (-(caddr pa)level)
sideb (-(caddr pb)level)
sidec (-(caddr pc)level)
)
(cond
;---Are all the vertices on one side----------
((and (>= sidea 0) (>= sideb 0) (>= sidec 0))
(setq ret 0)
)
((and (<= sidea 0) (<= sideb 0) (<= sidec 0))
(setq ret 0)
)
;---Is pa the only point on a side by itself---
((and (/=(sign sidea) (sign sideb)) (/=(sign sidea) (sign sidec)))
(setq p1x (- (car pa) (* sidea (/ (- (car pc) (car pa)) (- sidec sidea)))))
(setq p1y (- (cadr pa) (* sidea (/ (- (cadr pc) (cadr pa)) (- sidec sidea)))))
(setq p1z (- (caddr pa) (* sidea (/ (- (caddr pc) (caddr pa)) (- sidec sidea)))))
(setq p2x (- (car pa) (* sidea (/ (- (car pb) (car pa)) (- sideb sidea)))))
(setq p2y (- (cadr pa) (* sidea (/ (- (cadr pb) (cadr pa)) (- sideb sidea)))))
(setq p2z (- (caddr pa) (* sidea (/ (- (caddr pb) (caddr pa)) (- sideb sidea)))))
(setq ret 2)
)
;---Is pb the only point on a side by itself---
((and (/=(sign sideb) (sign sidea)) (/=(sign sideb) (sign sidec)))
(setq p1x (- (car pb) (* sideb (/ (- (car pc) (car pb)) (- sidec sideb)))))
(setq p1y (- (cadr pb) (* sideb (/ (- (cadr pc) (cadr pb)) (- sidec sideb)))))
(setq p1z (- (caddr pb) (* sideb (/ (- (caddr pc) (caddr pb)) (- sidec sideb)))))
(setq p2x (- (car pb) (* sideb (/ (- (car pa) (car pb)) (- sidea sideb)))))
(setq p2y (- (cadr pb) (* sideb (/ (- (cadr pa) (cadr pb)) (- sidea sideb)))))
(setq p2z (- (caddr pb) (* sideb (/ (- (caddr pa) (caddr pb)) (- sidea sideb)))))
(setq ret 2)
)
;---Is pc the only point on a side by itself---
((and (/=(sign sidec) (sign sidea)) (/=(sign sidec) (sign sideb)))
(setq p1x (- (car pc) (* sidec (/ (- (car pa) (car pc)) (- sidea sidec)))))
(setq p1y (- (cadr pc) (* sidec (/ (- (cadr pa) (cadr pc)) (- sidea sidec)))))
(setq p1z (- (caddr pc) (* sidec (/ (- (caddr pa) (caddr pc)) (- sidea sidec)))))
(setq p2x (- (car pc) (* sidec (/ (- (car pb) (car pc)) (- sideb sidec)))))
(setq p2y (- (cadr pc) (* sidec (/ (- (cadr pb) (cadr pc)) (- sideb sidec)))))
(setq p2z (- (caddr pc) (* sidec (/ (- (caddr pb) (caddr pc)) (- sideb sidec)))))
(setq ret 2)
)
(t (setq ret -1))
)
(list (list p1x p1y p1z) (list p2x p2y p2z) ret)
)
;--Define the Signum function-----
(defun sign (x)
( cond
(( minusp x ) -1 )
(( zerop x) 0 )
( t 1 )
)
)
Now in this next link
http://www.originlab.com/www/helponline/Origin/en/UserGuide/Creating_Contour_Graphs.html#Drawing_of_contour_linesthere is a nice explanation on how you should follow a contour on the TIN.
Basically you find a triangle which has a crossing for the level under consideration, calculate the two points p1 and p2 that intersect the triangle
with above routine. The first point is saved as the starting point of a polyline. This triangle is marked as traversed.
You then move to the neighboring triangle (The one which has a reversed edge called a twin), calculate the two points again, mark as traversed until you reach the outside of the TIN (You are sitting on an edge that has no twin) or you reach your starting point.
We now check if any other triangle still has a crossing for that level, If it is the case we start a new polyline on the same level.
We then repeat the previous steps for the next level.
Now who is up to come up with an efficient way to do this ?
I am quite rusty with my Autolisp and I am just starting with the visual stuff.
Now you know why I am after a DCEL connected structure from the triangulation.
ymg