Draw2dTestsImpl.mesa
Copyright Ó 1985, 1990, 1992 by Xerox Corporation. All rights reserved.
Bloomenthal, July 17, 1992 2:15 pm PDT
Doug Wyatt, April 9, 1992 2:29 pm PDT
DIRECTORY BasicTime, Buttons, ChoiceButtons, Commander, Convert, Draw2d, Imager, ImagerCG6Context, ImagerColor, ImagerDither, IO, MessageWindow, RawViewers, Real, RealFns, Rope, ViewerClasses, ViewerOps, ViewerTools, ViewersWorldInstance;
Draw2dTestsImpl: CEDAR PROGRAM
IMPORTS BasicTime, Buttons, ChoiceButtons, Commander, Convert, Draw2d, Imager, ImagerCG6Context, ImagerColor, IO, MessageWindow, RawViewers, Real, RealFns, Rope, ViewerOps, ViewerTools, ViewersWorldInstance
~ BEGIN
Types
VEC:   TYPE ~ Imager.VEC;
ROPE:   TYPE ~ Rope.ROPE;
ClickProc: TYPE ~ ViewerClasses.ClickProc;
Viewer:  TYPE ~ ViewerClasses.Viewer;
Data:   TYPE ~ RECORD [viewer, colorBut, modeBut: Viewer, fixed: BOOL ¬ TRUE];
IntegerPair: TYPE ~ RECORD [x, y: INTEGER];
Timing Tests for Line Drawing
Draw2dLines: Commander.CommandProc ~ {
MakeButton: PROC [name: ROPE, x, y: NAT, proc: ClickProc] RETURNS [r: Viewer] ~ {
r ¬ Buttons.Create[info: [parent: v, name: name, wx: x, wy: y], clientData: d, proc: proc];
};
d: REF Data ¬ NEW[Data];
v: Viewer ¬ d.viewer ¬ ViewerOps.CreateViewer[
flavor: $Lines,
paint: FALSE,
info: [name: "Draw2d Lines", openHeight: 420, scrollable: FALSE, data: d, iconic: TRUE]];
[] ¬ MakeButton["Repaint", 20, 380, Repaint];
d.modeBut ¬ MakeButton["Fixed ", 77, 380, ToggleFixed];
d.colorBut ¬
ChoiceButtons.BuildTextPrompt[v, 173, 380, "Color:", "0.0 0.0 0.0 ",, 100, d].textViewer;
RawViewers.SetColorWorld[ViewersWorldInstance.GetWorld[], $dense];
ViewerOps.OpenIcon[d.viewer];
};
ToggleFixed: ClickProc ~ {
d: REF Data ¬ NARROW[clientData];
Buttons.ReLabel[d.modeBut, IF (d.fixed ¬ NOT d.fixed) THEN "Fixed" ELSE "Fade to white"];
};
Repaint: ClickProc ~ {ViewerOps.PaintViewer[NARROW[clientData, REF Data].viewer, client]};
Lines: ViewerClasses.PaintProc ~ {
Time: PROC [proc: PROC, nTimes: NAT ¬ 1] RETURNS [time: REAL] ~ {
ENABLE Convert.Error => GOTO Bad;
s: IO.STREAM ¬ IO.RIS[ViewerTools.GetContents[d.colorBut]];
r ¬ IO.GetReal[s ! IO.EndOfStream => CONTINUE];
g ¬ IO.GetReal[s ! IO.EndOfStream => CONTINUE];
b ¬ IO.GetReal[s ! IO.EndOfStream => CONTINUE];
Imager.SetColor[context, ImagerColor.ColorFromRGB[[r, g, b]]];
time ¬ BasicTime.PulsesToSeconds[BasicTime.GetClockPulses[]];
THROUGH [0..nTimes) DO proc[]; ENDLOOP;
time ¬ (BasicTime.PulsesToSeconds[BasicTime.GetClockPulses[]]-time)/REAL[nTimes];
EXITS Bad => MessageWindow.Append["Bad color", TRUE];
};
Label: PROC [v: VEC, time, ref: REAL, name: ROPE] RETURNS [arg: REAL] ~ {
Imager.SetGray[context, 1.0];
Draw2d.Label[context, v, IO.PutFR1[Rope.Concat[name, ": %6.4f"], IO.real[arg ¬ time]]];
IF ref # 0.0 THEN
Draw2d.Label[context, [v.x, v.y-12], IO.PutFR1["(%7.6f times faster)", IO.real[ref/time]]];
};
SetImagerColor: PROC [f: REAL] ~ {
Imager.SetColor[context, ImagerColor.ColorFromRGB[[f+r*(1-f), f+g*(1-f), f+b*(1-f)]]]
};
ImagerLine: PROC ~ {
Imager.SetStrokeEnd[context, round];
FOR i: NAT IN [0..nVecs) DO
IF NOT d.fixed THEN SetImagerColor[i/REAL[nVecs]];
Imager.MaskVector[context, imager[i], [100, 160]];
ENDLOOP;
};
Draw2dLine: PROC ~ {
zip: Draw2d.Zip ¬ Draw2d.GetZip[context];
FOR i: NAT IN [0..nVecs) DO
IF NOT d.fixed THEN SetImagerColor[i/REAL[nVecs]];
Draw2d.Line[context, draw2d[i], [300, 160], solid, zip];
ENDLOOP;
Draw2d.ReleaseZip[zip];
};
CG6Line: PROC ~ {
Inner: PROC ~ {
ImagerCG6Context.SetCG6SubScreen[context,
self.wx+self.cx, self.wy+self.cy, self.cw, self.ch];
FOR i: NAT IN [0..nVecs) DO
vec: IntegerPair ¬ cg6[i];
IF NOT d.fixed THEN {
f: REAL ¬ REAL[i]/REAL[nVecs];
ImagerCG6Context.SetCG6RGBColor[context, f+r*(1-f), f+g*(1-f), f+b*(1-f)];
};
ImagerCG6Context.LineCG6[context, vec.x, vec.y, 500, 160];
ENDLOOP;
};
ImagerCG6Context.DoSaveCG6[context, Inner];
};
refTime: REAL;
nVecs: NAT ~ 200;
r, g, b: REAL ¬ 0.0;
d: REF Data ¬ NARROW[self.data];
cg6: ARRAY [0..200) OF IntegerPair;
imager, draw2d: ARRAY [0..200) OF VEC;
FOR i: NAT IN [0..nVecs) DO
t: REAL ¬ REAL[i]*2.0*3.1415926535/REAL[nVecs];
vec: VEC ¬ [90.0*RealFns.Cos[t], 150.0*RealFns.Sin[t]];
imager[i] ¬ [vec.x+100.0, vec.y+160.0];
draw2d[i] ¬ [vec.x+300.0, vec.y+160.0];
cg6[i] ¬ [Real.Round[vec.x]+500, Real.Round[vec.y]+160];
ENDLOOP;
refTime ¬ Label[[20, 350], Time[ImagerLine, 3], 0.0, "Imager.MaskVector"];
[] ¬ Label[[220, 350], Time[Draw2dLine, 3], refTime, "Draw2d.Solid"];
[] ¬ Label[[420, 350], Time[CG6Line, 3], refTime, "CG6Context.Line"];
};
Print the Color Map
Print: ViewerClasses.PaintProc ~ {
x: INTEGER ¬ 10;
y: INTEGER ¬ self.wh-30;
entries: ImagerDither.MapEntries ¬ ImagerCG6Context.GetDitherMap[context];
IF entries = NIL THEN Draw2d.Label[context, [x, y], "NIL MAP"];
FOR l: ImagerDither.MapEntries ← entries, l.rest WHILE l # NIL DO
e: ImagerDither.MapEntry ¬ l.first;
Draw2d.Label[context, [x, y], IO.PutFLR["%g: (%g, %g, %g)",
LIST[IO.int[e.mapIndex], IO.int[e.red], IO.int[e.green], IO.int[e.blue]]]];
IF (x ¬ x+150) > 500 THEN {x ¬ 10; y ¬ y-12};
ENDLOOP;
};
Draw2dPrintCG6Map: Commander.CommandProc ~ {
RawViewers.SetColorWorld[ViewersWorldInstance.GetWorld[], $dense];
ViewerOps.OpenIcon[ViewerOps.CreateViewer[
flavor: $Print, info: [name: "ImagerCG6 Map Entries", openHeight: 850]]];
};
Start Code
ViewerOps.RegisterViewerClass[$Lines, NEW[ViewerClasses.ViewerClassRec ¬ [paint: Lines]]];
ViewerOps.RegisterViewerClass[$Print, NEW[ViewerClasses.ViewerClassRec ¬ [paint: Print]]];
Commander.Register["Draw2dLines", Draw2dLines, "\nTest line drawing methods."];
Commander.Register["Draw2dPrintCG6Map", Draw2dPrintCG6Map];
END.