// April 14, 1978  4:06 PM				*** overlay C ***


get "zpDefs.bcpl"

get "zpPressDf.bcpl"


// outgoing procedures:

external [
	WritePieceObject
	]

// incoming procedures:

external [
	FLD; FLDI; FST		// FLOAT
	FTR; FML; FDV
	FAD; FNEG; FSB; FCM; FSN
	FSTDP; FLDDP; DPAD

	ComputeNormal		// ZPPIECE
	MakeCubic

	PrintFpValues	// ZPPRESS
	PutObjectInELtable

	PutsLine		// ZPPUTS
	PutsMove
	PutsCurve
	PutsRoundTip
	]

// incoming statics:

external [
	@showValues		// ZPPRESS
	]


// local floating point registers

manifest [
	t0=0; t1=1; t2=2; t3=3
	]



let WritePieceObject(pressFile, thickness, X0, Y0, dX0, dY0, X1, Y1, dX1, dY1, pec0, pec1) =
valof [WritePieceObject
	// PIECE OBJECT (pec0 & pec1 are end conditions at points 0 & 1)
	if showValues then PrintFpValues("WPO: ", X0, Y0, dX0, dY0, X1, Y1, dX1, dY1)
	let objectWordCount=0
	// scaled normals
	let nX0=vec 2; let nY0=vec 2
	let nX1=vec 2; let nY1=vec 2
	// scaled tangents
	let tX0=vec 2; let tY0=vec 2
	let tX1=vec 2; let tY1=vec 2
	unless ComputeNormal(thickness, dX0, dY0, nX0, nY0, tX0, tY0) then
		resultis PutObjectInELtable(0)
	unless ComputeNormal(thickness, dX1, dY1, nX1, nY1, tX1, tY1) then
		resultis PutObjectInELtable(0)
	// side points & derivatives
	let sX0=vec 2; let sY0=vec 2
	let cX0=vec 2; let cY0=vec 2
	let sX1=vec 2; let sY1=vec 2
	let cX1=vec 2; let cY1=vec 2
	let sX00tr,sY00tr=nil,nil
	let sX0tr,sY0tr=nil,nil
	let sX1tr,sY1tr=nil,nil
	// cubic coefs
	let aX0=vec 2; let aY0=vec 2
	let bX0=vec 2; let bY0=vec 2

	// SIDE 1
	// move to beginning of piece (remember this point: sX00tr, sY00tr)
	sX00tr=FTR(FST(FAD(FLD(t0,X0),nX0),sX0))
	sY00tr=FTR(FST(FAD(FLD(t1,Y0),nY0),sY0))
	sX1tr=FTR(FST(FAD(FLD(t2,X1),nX1),sX1))
	sY1tr=FTR(FST(FAD(FLD(t3,Y1),nY1),sY1))
	FST(FLD(t0,dX0),cX0); FST(FLD(t1,dY0),cY0)
	FST(FLD(t2,dX1),cX1); FST(FLD(t3,dY1),cY1)
	PutsMove(pressFile, lv objectWordCount, sX00tr, sY00tr)
	// curve for side 1
	MakeCubic(sX0, sX1, cX0, cX1, aX0, bX0)
	MakeCubic(sY0, sY1, cY0, cY1, aY0, bY0)
	PutsCurve(pressFile,lv objectWordCount, cX0, cY0, bX0, bY0, aX0, aY0)
	// connect to true end point, in order to avoid round off errors
	PutsLine(pressFile, lv objectWordCount, sX1tr, sY1tr)

	// SIDE 2
	sX0tr=FTR(FST(FSB(FLD(t2,X1),nX1),sX0))
	sY0tr=FTR(FST(FSB(FLD(t3,Y1),nY1),sY0))
	// after side 1, end condition 1 (flat/square/round):
	if pec1 eq fPEC then PutsLine(pressFile, lv objectWordCount, sX0tr, sY0tr)
	if pec1 eq sPEC then [
		FAD(FAD(FLD(t0, X1), nX1), tX1)
		FAD(FAD(FLD(t1, Y1), nY1), tY1)
		PutsLine(pressFile, lv objectWordCount, FTR(t0), FTR(t1))
		PutsLine(pressFile, lv objectWordCount, FTR(FAD(t2, tX1)), FTR(FAD(t3, tY1)))
		PutsLine(pressFile, lv objectWordCount, sX0tr, sY0tr)
		]
	if pec1 eq rPEC then
		PutsRoundTip(pressFile, lv objectWordCount, sX1tr, sY1tr, sX0tr, sY0tr)
	sX1tr=FTR(FST(FSB(FLD(t0,X0),nX0),sX1))
	sY1tr=FTR(FST(FSB(FLD(t1,Y0),nY0),sY1))
	FST(FNEG(FLD(t0,dX0)),cX1)
	FST(FNEG(FLD(t1,dY0)),cY1)
	FST(FNEG(FLD(t2,dX1)),cX0)
	FST(FNEG(FLD(t3,dY1)),cY0)
	// curve for side 2
	MakeCubic(sX0, sX1, cX0, cX1, aX0, bX0)
	MakeCubic(sY0, sY1, cY0, cY1, aY0, bY0)
	PutsCurve(pressFile,lv objectWordCount, cX0, cY0, bX0, bY0, aX0, aY0)
	// connect to true end point, in order to avoid round off errors
	PutsLine(pressFile, lv objectWordCount, sX1tr, sY1tr)

	// after side 2, end condition 0 (flat/square/round):
	if pec0 eq fPEC then PutsLine(pressFile, lv objectWordCount, sX00tr, sY00tr)
	if pec0 eq sPEC then [
		FSB(FSB(FLD(t0, X0), nX0), tX0)
		FSB(FSB(FLD(t1, Y0), nY0), tY0)
		PutsLine(pressFile, lv objectWordCount, FTR(t0), FTR(t1))
		FSB(FAD(FLD(t2, X0), nX0), tX0)
		FSB(FAD(FLD(t3, Y0), nY0), tY0)
		PutsLine(pressFile, lv objectWordCount, FTR(t2), FTR(t3))
		PutsLine(pressFile, lv objectWordCount, sX00tr, sY00tr)
		]
	if pec0 eq rPEC then
		PutsRoundTip(pressFile, lv objectWordCount, sX1tr, sY1tr, sX00tr, sY00tr)

	resultis PutObjectInELtable(objectWordCount)
	]WritePieceObject