<> <> <> <> <> <<>> DIRECTORY Complex USING [SqrAbs, Sub, VEC], Cubic USING [Bezier], FitIO USING [Context, ContextRec, FeedbackRec, MagnifyPoint, UnMagnifyPoint], FitJaM USING [InitProc], FitState USING [Create, Handle], Highlight USING [Context, Data, Object, Procs], Imager USING [black, Context, DoSaveAll, MaskFill, MaskStrokeTrajectory, SetColor, SetStrokeWidth, SetXY, Trans], ImagerBackdoor USING [invert], ImagerPath USING [CurveTo, MoveTo, PathProc, Trajectory], JaM USING [PopReal, PopRope, Register, RegisterInit, RopeToAtom, State], JaMImager USING [Painter], MessageWindow USING [Append, Clear], Rope USING [ROPE]; FitJaMImpl: CEDAR PROGRAM IMPORTS Complex, FitIO, FitState, Imager, ImagerBackdoor, ImagerPath, JaM, JaMImager, MessageWindow EXPORTS FitJaM = { InitProc: TYPE = FitJaM.InitProc; StartRec: TYPE = RECORD[id: ATOM, proc: InitProc]; startList: LIST OF REF StartRec; defaultFitState: PUBLIC FitState.Handle _ FitState.Create[]; defaultFitIO: PUBLIC FitIO.Context _ NEW[FitIO.ContextRec _ [ imager: NIL, --set in paint proc magnify: 1, position: [0,0], feedback: NEW[FitIO.FeedbackRec _ [ color: Imager.black, sampleSize: 1, jointSize: 2, nodeLength: 6, nodeLineWidth: 0, lineWidth: 0 --for DrawSamples and DrawContour ]], logActions: FALSE, log: NIL ]]; <> defaultHighlight: PUBLIC Highlight.Context; RegisterInit: PUBLIC PROC[id: ATOM, proc: InitProc] = { FOR ls: LIST OF REF StartRec _ startList, ls.rest UNTIL ls=NIL DO IF ls.first.id=id THEN ERROR; ENDLOOP; startList _ CONS[NEW[StartRec _ [id: id, proc: proc]],startList]; }; InitAll: PROC [state: JaM.State] = { FOR ls: LIST OF REF StartRec _ startList, ls.rest UNTIL ls=NIL DO ls.first.proc[state]; ENDLOOP; }; InitAtom: PROC [state: JaM.State] = { atom: ATOM _ JaM.RopeToAtom[JaM.PopRope[state]]; FOR ls: LIST OF REF StartRec _ startList, ls.rest UNTIL ls=NIL DO IF atom=ls.first.id THEN {ls.first.proc[state]; EXIT}; ENDLOOP; }; nullPt: Complex.VEC _ [-10000, -10000]; nullBezier: Cubic.Bezier _ [[-1,-1],[-1,-1],[-1,-1],[-1,-1]]; ShowPt: PROC[ctx: Highlight.Context, pt: Complex.VEC] = { Paint: PROC[dc: Imager.Context] = { doIt: PROC = { octagonPath: ImagerPath.PathProc ~ { moveTo[[1, 2]]; lineTo[[2, 1]]; lineTo[[2, -1]]; lineTo[[1, -2]]; lineTo[[-1, -2]]; lineTo[[-2, -1]]; lineTo[[-2, 1]]; lineTo[[-1, 2]]; }; Imager.SetColor[dc, ImagerBackdoor.invert]; Imager.SetXY[dc, [dpt.x, dpt.y]]; Imager.Trans[dc]; Imager.MaskFill[dc, octagonPath]; }; dpt: Complex.VEC; IF ctx.data.lastPt#nullPt THEN { dpt _ FitIO.MagnifyPoint[defaultFitIO, ctx.data.lastPt]; Imager.DoSaveAll[dc,doIt]; }; IF pt#nullPt THEN { dpt _ FitIO.MagnifyPoint[defaultFitIO,pt]; Imager.DoSaveAll[dc,doIt]; }; }; JaMImager.Painter[Paint, NARROW[ctx.data.clientData]]; ctx.data.lastPt _ pt; IF Complex.SqrAbs[Complex.Sub[pt, breakPt]] < breakTol THEN SIGNAL BreakFromHighlight; }; ShowBezier: PROC[ctx: Highlight.Context, bezier: Cubic.Bezier] = { Paint: PROC[dc: Imager.Context] = { doIt: PROC = { Imager.SetColor[dc, ImagerBackdoor.invert]; Imager.SetStrokeWidth[dc, 0]; IF oldPath#NIL THEN Imager.MaskStrokeTrajectory[dc, oldPath]; IF newPath #NIL THEN Imager.MaskStrokeTrajectory[dc, newPath]; }; Imager.DoSaveAll[dc,doIt]; }; makePath: PROC[bz: Cubic.Bezier] RETURNS[path: ImagerPath.Trajectory] = { b0,b1, b2, b3: Complex.VEC; IF bz=nullBezier THEN RETURN[NIL]; b0 _ FitIO.MagnifyPoint[defaultFitIO, [bz.b0.x, bz.b0.y]]; b1 _ FitIO.MagnifyPoint[defaultFitIO, [bz.b1.x, bz.b1.y]]; b2 _ FitIO.MagnifyPoint[defaultFitIO, [bz.b2.x, bz.b2.y]]; b3 _ FitIO.MagnifyPoint[defaultFitIO, [bz.b3.x, bz.b3.y]]; path _ ImagerPath.MoveTo[[b0.x, b0.y]]; path _ ImagerPath.CurveTo[path, [b1.x, b1.y], [b2.x, b2.y], [b3.x, b3.y]]; }; newPath, oldPath: ImagerPath.Trajectory; oldPath _ makePath[ctx.data.lastBezier]; newPath _ makePath[bezier]; JaMImager.Painter[Paint, NARROW[ctx.data.clientData]]; ctx.data.lastBezier _ bezier; }; ShowRope: PROC[ctx: Highlight.Context, rope: Rope.ROPE]= { MessageWindow.Append[rope, TRUE]; }; CleanUp: PROC[ctx: Highlight.Context] = { IF ctx.data.lastPt#nullPt THEN ctx.procs.showPt[ctx, nullPt]; IF ctx.data.lastBezier#nullBezier THEN ctx.procs.showBezier[ctx, nullBezier]; MessageWindow.Clear[]; }; <<>> breakTol: REAL _ -1; breakPt: Complex.VEC _ nullPt; BreakFromHighlight: SIGNAL = CODE; BreakPoint: PROC [state: JaM.State] = { breakTol _ JaM.PopReal[state]; breakTol _ breakTol*breakTol; --keep the square breakPt _ GetPt[state]; breakPt _ FitIO.UnMagnifyPoint[defaultFitIO, breakPt]; }; GetPt: PROC [state: JaM.State] RETURNS [Complex.VEC]= { y: REAL _ JaM.PopReal[state]; x: REAL _ JaM.PopReal[state]; RETURN [[x,y]]; }; <> <> <> <<[name: "Sampled Curve Editor [No File]", file: NIL]]>> <> <<[name: Rope.Concat["Sampled Curve Editor ", name], file: name]];>> <<};>> <<>> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <<};>> <<};>> <<>> JaMInit: PROC [state: JaM.State] = { JaM.Register[state,"FitJaM.InitAll",InitAll]; JaM.Register[state,"FitJaM.InitAtom",InitAtom]; <> <> JaM.Register[state, "FitJaM.BreakPoint", BreakPoint]; --x, y, tol=> jumps to a place to set a break when highlight near this pt defaultHighlight _ NEW[Highlight.Object _ [ data: NEW[Highlight.Data _ [ imager: NIL, lastRope: NIL, lastBezier: nullBezier, lastPt: nullPt, clientData: state]], procs:NEW[Highlight.Procs _ [ showPt: ShowPt, showBezier: ShowBezier, showRope: ShowRope, cleanUp: CleanUp ]] ]]; }; JaM.RegisterInit["FitJaM",JaMInit]; }.