-- this function generates a mesh bounded by the 1st curves in the
-- two given splines and that has the given number of segments along
-- curves
--						John Wainwright.
--
fn ruled_mesh s1 s2 segs = 
(
	-- make the initial mesh
	local u_step = 1.0 / segs,
	      n_steps = (segs + 1) as integer,
	      n_verts = 2 * n_steps,
	      n_faces = n_verts - 2,
	      m = mesh numVerts:n_verts numFaces:n_faces
/*		  m.material=standard()
		  m.material.opacity=100
		  m.wirecolor=green*/
		  
	-- set the vertex positions by stepping parametrically along each spline
	-- vertex numbers alternate from spline-to-spline starting at 
	-- beginning of s1
    i=numKnots s1 1
	n1=getKnotPoint s1 1 1
    n2=getKnotPoint s1 1 i
    i=numKnots s2 1
	n3=getKnotPoint s2 1 1
    n4=getKnotPoint s2 1 i
	
	if distance n1 n3 + distance n2 n4 > distance n1 n4 + distance n2 n3 do reverse s1 1
    m.wirecolor=s1.wirecolor
	m.parent=s1
	
	local u = 0
	for i = 1 to n_verts by 2 do
	(
		setVert m i (lengthinterp s1 u)
		setVert m (i+1) (lengthinterp s2 u)
		u += u_step
	)
	
	-- now set up the faces: (3,2,1)(4,2,3)(5,4,3)(6,4,5)(7,6,5)(8,6,7)...
	-- and edge vis (2,3)(1,3)...
	for i = 1 to n_faces by 2 do
	(
		setFace m i [i+2, i+1, i]
		setEdgeVis m i 2 on; setEdgeVis m i 3 on
		setFace m (i+1) [i+3, i+1, i+2]
		setEdgeVis m (i+1) 1 on; setEdgeVis m (i+1) 3 on
	)
	
	-- update mesh caches & display, return it
	update m
	m
)
global RuledMeshRollout
rollout RuledMeshRollout "Ruled Mesh" width:150 height:130
(
local sp1, sp2
	spinner spn1 "Num Segments:" pos:[10,70] width:130 height:16 type:#integer range:[2,700,32]
	pickbutton line1 "Spline 1" pos:[10,10] width:130 height:20
	pickbutton line2 "Spline 2" pos:[10,40] width:130 height:20
	button btn9 "OK" pos:[100,100] width:40 height:20
	
	on line1 picked obj do 
	(
	sp1 = obj
	line1.text = obj.name
	)
	on line2 picked obj do 
	(
	sp2 = obj
	line2.text = obj.name
	)
	on btn9 pressed do(
      ruled_mesh sp1 sp2 spn1.value
      ruled_mesh sp2 sp1 spn1.value
	)  
	
)
CreateDialog RuledMeshRollout width:150 height:130
		  
	