PiecewiseFitJaM.mesa
JaM interfaces for PiecewiseFit
Maureen Stone May 1, 1985 6:10:40 pm PDT
DIRECTORY
Seq,
LSPiece USING [MetricsRec, Metrics],
Cubic USING [Bezier],
PiecewiseFit,
FitState USING [CurrentSamples, GetClosed, CurrentJoints, ResetData, Handle, AddLink],
FitJaM USING [RegisterInit, InitProc, defaultFitState, defaultHighlight, defaultFitIO],
FitIO USING [DrawCubic],
Highlight USING [ShowBezier, CleanUp],
IO USING[PutFR, int, real],
JaM;
PiecewiseFitJaM: CEDAR PROGRAM
IMPORTS JaM, FitState, FitJaM, PiecewiseFit, IO, Highlight, FitIO = {
data: PiecewiseFit.Data ← NEW[PiecewiseFit.DataRec];
printMetrics: BOOLEANFALSE;
PrintMetrics: PROC [state: JaM.State] = {printMetrics ← JaM.PopBool[state]};
GetData: PROC = {
fitstate: FitState.Handle ← FitJaM.defaultFitState;
data.samples ← FitState.CurrentSamples[fitstate];
data.closedFitState.GetClosed[fitstate];
[joints: data.joints, tangents: data.tangents] ← FitState.CurrentJoints[fitstate];
PiecewiseFit.Default[data];
};
OutputCubic: PROC [state: JaM.State, bezier: Cubic.Bezier, sumErr, maxDev: REAL, iterations: INT] RETURNS[BOOLEAN] = {
FitState.AddLink[FitJaM.defaultFitState, bezier];
FitIO.DrawCubic[FitJaM.defaultFitIO, bezier];
IF printMetrics THEN
JaM.ExecuteRope[state, IO.PutFR["(Final: sumErr: %g, maxDev: %g, iterations: %g \n) .print",
IO.real[sumErr], IO.real[maxDev], IO.int[iterations]] ! JaM.Stop => CONTINUE];
RETURN[state.aborted];
};
HighlightProc: PROC [state: JaM.State, bezier: Cubic.Bezier, sumErr, maxDev: REAL, iterations: INT] RETURNS[BOOLEAN] = {
Highlight.ShowBezier[FitJaM.defaultHighlight, bezier];
IF printMetrics THEN
JaM.ExecuteRope[state, IO.PutFR["(sumErr: %g, maxDev: %g, iterations: %g \n) .print",
IO.real[sumErr], IO.real[maxDev], IO.int[iterations]]];
RETURN[state.aborted];
};
FitSpline: PROC [state: JaM.State] = { -- Makes links between every pair of nodes
outputCubic: PiecewiseFit.CubicProc = {RETURN[OutputCubic[state, bezier, sumErr, maxDev,iterations]]};
FitState.ResetData[FitJaM.defaultFitState, links];
GetData[];
PiecewiseFit.FitSpline[data,metrics, outputCubic];
};
GrowSpline: PROC [state: JaM.State] = { -- Makes links using GrowSpline
outputCubic: PiecewiseFit.CubicProc = {RETURN[OutputCubic[state, bezier, sumErr, maxDev,iterations]]};
hilight: PiecewiseFit.CubicProc = {
quit ← HighlightProc[state, bezier, sumErr, maxDev,iterations];
RETURN[quit]
};
FitState.ResetData[FitJaM.defaultFitState, links];
GetData[];
PiecewiseFit.GrowSpline[data,metrics, outputCubic, hilight ! PiecewiseFit.Error => {
JaM.ExecuteRope[state, why]; CONTINUE}];
Highlight.CleanUp[FitJaM.defaultHighlight];
};
DynSpline: PROC [state: JaM.State] = { -- Makes links using GrowSpline
outputCubic: PiecewiseFit.CubicProc = {RETURN[OutputCubic[state, bezier, sumErr, maxDev,iterations]]};
trim: BOOLEAN ← JaM.PopBool[state];
penalty: REAL ← JaM.PopReal[state];
hilight: PiecewiseFit.CubicProc = {
quit ← HighlightProc[state, bezier, sumErr, maxDev,iterations];
RETURN[quit]
};
FitState.ResetData[FitJaM.defaultFitState, links];
GetData[];
PiecewiseFit.DynSpline[data,metrics, penalty, trim, outputCubic, hilight];
Highlight.CleanUp[FitJaM.defaultHighlight];
};
metrics: LSPiece.Metrics ← NEW[LSPiece.MetricsRec ← [
maxItr: 10, -- limit on number of iterations
maxDev: 1, --stop iterating if the maximum deviation gets below this amount
sumErr: 1, -- stop iterating if the sum of squares gets below this amount
deltaT: 0 -- stop when the t values each change by less than this
]];
SetMetrics: PROC [state: JaM.State] = {--maxItr maxDev sumErr deltaT
metrics.deltaT ← JaM.PopReal[state];
metrics.sumErr ← JaM.PopReal[state];
metrics.maxDev ← JaM.PopReal[state];
metrics.maxItr ← JaM.PopInt[state];
};
Init: FitJaM.InitProc = {
JaM.Register[state, ".fit",FitSpline]; -- Makes links between every pair of nodes
JaM.Register[state, ".grow",GrowSpline]; -- Makes links using GrowSpline
JaM.Register[state, ".dynspline",DynSpline]; -- penalty trim => Makes links using DynSpline
JaM.Register[state, ".metrics",SetMetrics]; -- maxItr maxDev sumErr deltaT=> set metrics
JaM.Register[state, ".printmetrics",PrintMetrics]; -- boolean prints feedback in jam window
};
FitJaM.RegisterInit[id: $PiecewiseFit, proc: Init];
}.