ImagerCGStrokeImpl.mesa
Last changed by:
Michael Plass, August 4, 1983 11:28 am
Doug Wyatt, September 18, 1982 6:58 pm
DIRECTORY
ImagerBasic,
Real USING [SqRt],
ScanConverter,
ImagerMasks,
ImagerStroke,
Scaled;
ImagerCGStrokeImpl: CEDAR PROGRAM
IMPORTS Real, ScanConverter, ImagerMasks, Scaled
EXPORTS ImagerStroke = {
Thin stroke implementation
MaskThinSegment: PUBLIC PROC [dest, clipper: ImagerMasks.Mask, sFrom, fFrom, sTo, fTo: Scaled.Value, function: PACKED ARRAY [0..1] OF [0..1]] ~ {
sInt, fInt: INTEGER ← 0;
sDelta, fDelta: INT ← 0;
signDelta: INTEGER ← 0;
e: INT ← 0;
ForwardS: PROC ~ INLINE {
IF sDelta < fDelta THEN ERROR;
sInt ← sInt + 1;
IF (e ← e - fDelta) < 0 THEN {fInt ← fInt + signDelta; e ← e + sDelta};
};
ReverseS: PROC ~ INLINE {
IF sDelta < fDelta THEN ERROR;
sInt ← sInt - 1;
IF (e ← e + fDelta) >= sDelta THEN {fInt ← fInt - signDelta; e ← e - sDelta};
};
ForwardF: PROC ~ INLINE {
IF sDelta > fDelta THEN ERROR;
fInt ← fInt + 1;
IF (e ← e - sDelta) < 0 THEN {sInt ← sInt + signDelta; e ← e + fDelta};
};
ReverseF: PROC ~ INLINE {
IF sDelta > fDelta THEN ERROR;
fInt ← fInt - 1;
IF (e ← e + sDelta) >= fDelta THEN {sInt ← sInt - signDelta; e ← e - fDelta};
};
IF sFrom.GREATER[sTo] THEN {t: Scaled.Value ← sFrom; sFrom ← sTo; sTo ← t; t ← fFrom; fFrom ← fTo; fTo ← t};
};
Wide stroke implementation
Path: TYPE = ImagerBasic.Path;
StrokeEnds: TYPE = ImagerBasic.StrokeEnds;
Vec, Pair: TYPE = ImagerBasic.Pair;
Bezier: TYPE = ImagerBasic.Bezier;
maxdepth: NAT = 10;
Split: PUBLIC PROC[bezier: Bezier] RETURNS[Bezier, Bezier] = {
a,b,c,ab,bc,p: Vec;
Mid: PROC[u,v: Vec] RETURNS[Vec] = INLINE { RETURN[[(u.x+v.x)/2,(u.y+v.y)/2]] };
a ← Mid[bezier.b0,bezier.b1];
b ← Mid[bezier.b1,bezier.b2];
c ← Mid[bezier.b2,bezier.b3];
ab ← Mid[a,b];
bc ← Mid[b,c];
p ← Mid[ab,bc];
RETURN[[bezier.b0, a, ab, p],[p, bc, c, bezier.b3]];
};
Add: PROC[a: Vec, b: Vec] RETURNS[Vec] = INLINE { RETURN[[a.x+b.x,a.y+b.y]] };
Sub: PROC[a: Vec, b: Vec] RETURNS[Vec] = INLINE { RETURN[[a.x-b.x,a.y-b.y]] };
In: PROC[a: Vec, b: Vec, c: Vec] RETURNS[BOOLEAN] = INLINE { RETURN[a.x IN[b.x..c.x] AND a.y IN[b.y..c.y]] };
Min: PROC[a: Vec, b: Vec] RETURNS[Vec] = INLINE { RETURN[[MIN[a.x,b.x],MIN[a.y,b.y]]] };
Max: PROC[a: Vec, b: Vec] RETURNS[Vec] = INLINE { RETURN[[MAX[a.x,b.x],MAX[a.y,b.y]]] };
Dot: PROC[a: Vec, b: Vec] RETURNS[REAL] = INLINE { RETURN[a.x*b.x+a.y*b.y] };
FlatBezier: PUBLIC PROC[bezier: Bezier, epsilon: REAL] RETURNS[BOOLEAN] = {
dx,dy: REAL;
d1,d2,d,bl,bh: Vec;
oh: Vec=[0.5,0.5];
bh ← Add[Max[bezier.b0,bezier.b3],oh];
bl ← Sub[Min[bezier.b0,bezier.b3],oh];
IF ~In[bezier.b1,bl,bh] OR ~In[bezier.b2,bl,bh] THEN RETURN[FALSE];
d1 ← Sub[bezier.b1,bezier.b0];
d2 ← Sub[bezier.b2,bezier.b0];
d ← Sub[bezier.b3,bezier.b0];
dx ← ABS[d.x]; dy ← ABS[d.y];
IF dx+dy < 1 THEN RETURN[TRUE];
IF dy < dx THEN {
dydx: REAL ← d.y/d.x;
RETURN[ABS[d2.y-d2.x*dydx]<epsilon AND ABS[d1.y-d1.x*dydx]<epsilon]
}
ELSE {
dxdy: REAL ← d.x/d.y;
RETURN[ABS[d2.x-d2.y*dxdy]<epsilon AND ABS[d1.x-d1.y*dxdy]<epsilon]
}
};
Subdivide: PUBLIC PROC [b: Bezier, vertex: PROC[Vec], tolerance: REAL ← 1.5, depth: NAT ← 0] = {
IF depth>=maxdepth OR FlatBezier[b, tolerance] THEN vertex[b.b3]
ELSE { b1, b2: Bezier;
[b1, b2] ← Split[b];
Subdivide[b1, vertex, tolerance, depth+1]; -- first half
Subdivide[b2, vertex, tolerance, depth+1]; -- second half
};
};
MaskFromStroke: PUBLIC PROC [path: Path, clientToDevice: ImagerBasic.Transformation, width: REAL, strokeEnds: StrokeEnds, closed: BOOLEAN, sMin, sMax: INTEGER] RETURNS [mask: ImagerMasks.Mask] = {
devicePath: ScanConverter.DevicePath ← ScanConverter.Allocate[];
firstVertex: BOOLEANTRUE;
Xform: PROC [p: Pair] RETURNS [Pair] ~ {RETURN[[
Transforms (x, y) to (f, s) — note f comes first for ScanConvert!
clientToDevice.d * p.x + clientToDevice.e * p.y + clientToDevice.f,
clientToDevice.a * p.x + clientToDevice.b * p.y + clientToDevice.c
]]};
GenPath: PROC [move: PROC[Pair], line: PROC[Pair], curve: PROC[Pair, Pair, Pair]] = {
GenerateStroke[path, width, closed, strokeEnds, Xform, move, line];
};
Runs: PROC[run: PROC[s, fMin: INTEGER, fSize: NAT], repeat: PROC[timesToRepeatScanline: NAT]] ~ {
BoxFromScanConverter: PROC [x, y, w, h: INTEGER] = {
IF h # 1 THEN ERROR;
run[s: y, fMin: x, fSize: w];
};
devicePath.ScanConvert[BoxFromScanConverter, sMin, sMax];
};
devicePath.PushPath[GenPath];
mask ← ImagerMasks.Create[Runs];
devicePath.Release[];
};
Mag: PROC[v: Vec] RETURNS [REAL] = INLINE {
RETURN [Real.SqRt[v.x * v.x + v.y * v.y]]
};
Normalize: PROC[v: Vec] RETURNS [Vec] = INLINE {
m: REAL ← Mag[v];
IF m = 0 THEN RETURN[v] ELSE RETURN[[v.x/m, v.y/m]]
};
Midpoint: PROC[a, b: Vec] RETURNS[Vec] = INLINE {
RETURN[[(a.x+b.x)/2, (a.y+b.y)/2]] };
Miter: PROC[v1, v2: Vec] RETURNS[REAL] = {
s, a: REAL; v3: Vec;
v3 ← Normalize[Add[v1, v2]];
s ← Dot[[v1.y, -v1.x], v3]; -- compute sin of angle
a ← s / Real.SqRt[1 - s*s]; -- compute horz vec for angle between v1 & v3
RETURN[a];
};
Three point flatness test:
Flat: PROC[v1, v2, v3: Vec, epsilon: REAL] RETURNS[b: BOOL] = {
dx, dy: REAL;
d1, d, bl, bh: Vec;
oh: Vec = [0.5, 0.5];
bh ← Add[Max[v1, v3], oh];
bl ← Sub[Min[v1, v3], oh];
IF NOT In[v2, bl, bh] THEN RETURN[FALSE];
d1 ← Sub[v2, v1];
d ← Sub[v3, v1];
dx ← ABS[d.x]; dy ← ABS[d.y];
IF dx + dy < 1 THEN RETURN[TRUE];
IF dy < dx THEN { dydx: REAL ← d.y / d.x; RETURN[ABS[d1.y - d1.x * dydx] < epsilon] }
ELSE { dxdy: REAL ← d.x / d.y; RETURN[ABS[d1.x - d1.y * dxdy] < epsilon] };
};
GenerateStroke: PUBLIC PROC[path: Path, width: REAL, closed: BOOLEAN, ends: StrokeEnds,
map: PROC[Vec] RETURNS[Vec], moveTo, lineTo: PROC[Vec]] = {
h: REALABS[width]/2; -- half width
hsq: REAL ← h*h;
smallSin: REAL ← 0.001*hsq;
line: BOOLEAN; -- is current segment a line?
b0, b1, b2, b3: Vec; -- control points for current segment
u0, u3: Vec; -- half width normal vectors at b0, b3
p0, q0, p3, q3: Vec; -- corners of stroke boundary for current segment
MiterFlags: TYPE = RECORD[p, q: BOOLEAN];
m0, m3: MiterFlags;
fb0, fb1, fb2, fb3, fu0, fu3, fp3, fq3: Vec; -- saved values for deferred segment
fline: BOOLEAN; fm3: MiterFlags; -- ditto
begin, defer: BOOLEAN;
firstVertex: BOOLEANTRUE;
vertex: PROC[p: Vec] ~ {IF firstVertex THEN {firstVertex ← FALSE; moveTo[p]} ELSE lineTo[p]};
close: PROC ~ INLINE {firstVertex ← TRUE};
Cap: PROC[b, d, p, q: Vec] = {
SELECT ends FROM
butt => { };
square => {
v0: Vec ← [b.x-d.y, b.y+d.x];
v3: Vec ← [b.x+d.y, b.y-d.x];
v1: Vec ← [v0.x+d.x, v0.y+d.y];
v2: Vec ← [v3.x+d.x, v3.y+d.y];
vertex[p]; vertex[map[v1]]; vertex[map[v2]]; vertex[q]; close[];
};
round => {
tolerance: REAL = 1.0;
bz: Bezier;
v0, v1, v2, v3: Vec;
f: REAL = 0.5522848; -- 4*(sqrt2-1)/3
a: Vec ← [f*d.x, f*d.y];
v0 ← [b.x-d.y, b.y+d.x];
v3 ← [b.x+d.x, b.y+d.y];
v1 ← [v0.x+a.x, v0.y+a.y];
v2 ← [v3.x-a.y, v3.y+a.x];
bz.b0 ← p; bz.b1 ← map[v1]; bz.b2 ← map[v2]; bz.b3 ← map[v3];
vertex[p]; Subdivide[bz, vertex, tolerance];
v0 ← v3;
v3 ← [b.x+d.y, b.y-d.x];
v1 ← [v0.x+a.y, v0.y-a.x];
v2 ← [v3.x+a.x, v3.y+a.y];
bz.b0 ← bz.b3; bz.b1 ← map[v1]; bz.b2 ← map[v2]; bz.b3 ← q;
Subdivide[bz, vertex, tolerance]; close[];
};
ENDCASE => ERROR;
};
HNormal: PROC[a, b: Vec] RETURNS[Vec] = {
returns a vector of length halfWidth normal to the direction from a to b
d: Vec ← Sub[b, a];
u: Vec ← Normalize[d];
RETURN[[-u.y*h, u.x*h]];
};
ComputeP: PROC[b, u: Vec] RETURNS[Vec] = INLINE { RETURN[map[[b.x+u.x, b.y+u.y]]] };
ComputeQ: PROC[b, u: Vec] RETURNS[Vec] = INLINE { RETURN[map[[b.x-u.x, b.y-u.y]]] };
tolerance: REAL = 1.0;
DoCurve: PROC[b0, b1, b2, b3: Vec, p0, p3: Vec, depth: NAT ← 0] = {
b01: Vec ← Midpoint[b0, b1];
b12: Vec ← Midpoint[b1, b2];
b23: Vec ← Midpoint[b2, b3];
b11: Vec ← Midpoint[b01, b12];
b22: Vec ← Midpoint[b12, b23];
bm: Vec ← Midpoint[b11, b22]; -- middle point on curve (at t=1/2)
um: Vec ← HNormal[b11, b22]; -- half width normal at bm
pm: Vec ← map[Add[bm, um]]; -- point on stroke edge
IF depth>0 -- always divide at least once --
AND Flat[p0, pm, p3, tolerance] THEN vertex[p3]
ELSE {
DoCurve[b0, b01, b11, bm, p0, pm, depth+1]; -- first half
DoCurve[bm, b22, b23, b3, pm, p3, depth+1]; -- second half
};
};
EmitSegment: PROC = {
IF NOT m0.p THEN p0 ← ComputeP[b0, u0];
IF NOT m0.q THEN q0 ← ComputeQ[b0, u0];
IF NOT m3.p THEN p3 ← ComputeP[b3, u3];
IF NOT m3.q THEN q3 ← ComputeQ[b3, u3];
vertex[p0];
IF line THEN vertex[p3] ELSE DoCurve[b0, b1, b2, b3, p0, p3];
vertex[q3];
IF line THEN vertex[q0] ELSE DoCurve[b3, b2, b1, b0, q3, q0];
close[];
};
EmitPreviousSegment: PROC[u4: Vec, miter: BOOLEANTRUE] = {
IF begin THEN {
IF closed THEN {
fb0 ← b3; fu0 ← u4;
defer ← TRUE }
ELSE {
u3 ← u4;
p3 ← ComputeP[b3, u3]; q3 ← ComputeQ[b3, u3];
m3 ← [TRUE,TRUE];
Cap[b3, [-u3.y, u3.x], q3, p3];
defer ← FALSE };
begin ← FALSE }
ELSE {
IF miter THEN {
sin: REAL ← u3.x*u4.y-u3.y*u4.x;
IF ABS[sin]<smallSin THEN {
cos: REAL ← u3.x*u4.x+u3.y*u4.y;
IF cos>0 THEN {
p3 ← ComputeP[b3, u3]; q3 ← ComputeQ[b3, u3];
m3 ← [TRUE,TRUE] }
ELSE m3 ← [FALSE,FALSE];
}
ELSE {
f: REAL ← hsq/sin;
r: Vec ← [f*(u4.y-u3.y), f*(u3.x-u4.x)];
IF sin<0 THEN { p3 ← map[[b3.x+r.x, b3.y+r.y]]; m3 ← [p: TRUE, q: FALSE] }
ELSE { q3 ← map[[b3.x-r.x, b3.y-r.y]]; m3 ← [p: FALSE, q: TRUE] };
};
}
ELSE m3 ← [FALSE,FALSE];
IF defer THEN {
fb1 ← b1; fb2 ← b2; fb3 ← b3; fline ← line;
fu3 ← u3; fp3 ← p3; fq3 ← q3; fm3 ← m3;
defer ← FALSE }
ELSE EmitSegment[];
};
b0 ← b3; u0 ← u4; p0 ← p3; q0 ← q3; m0 ← m3;
};
first: BOOLEANTRUE;
Move: PROC[v: Vec] = {IF first THEN first ← FALSE ELSE Close[]; b3 ← v; begin ← TRUE };
Line: PROC[v: Vec] = {
u0: Vec ← HNormal[b3, v];
EmitPreviousSegment[u0];
b3 ← v; u3 ← u0; line ← TRUE;
};
Curve: PROC[v1, v2, v3: Vec] = {
u0: Vec ← (IF v1#b3 THEN HNormal[b3, v1] ELSE HNormal[b3, v2]);
EmitPreviousSegment[u0];
b1 ← v1; b2 ← v2; b3 ← v3; line ← FALSE;
u3 ← (IF b2#b3 THEN HNormal[b2, b3] ELSE HNormal[b1, b2]);
};
Close: PROC = {
IF begin THEN {
trajectory is a single point
IF closed THEN { --ignore-- }
ELSE {
just pick an arbitrary direction for end caps
u3 ← [0, h];
p3 ← ComputeP[b3, u3];
q3 ← ComputeQ[b3, u3];
Cap[b3, [-h, 0], q3, p3];
Cap[b3, [h, 0], p3, q3];
};
}
ELSE IF closed THEN {
IF b3#fb0 THEN Line[fb0]; -- if necessary, extend a line to the first point
EmitPreviousSegment[fu0]; -- emit the segment ending at the first point
b1 ← fb1; b2 ← fb2; b3 ← fb3; line ← fline;
u3 ← fu3; p3 ← fp3; q3 ← fq3; m3 ← fm3;
EmitSegment[]; -- emit the deferred first segment
}
ELSE { EmitPreviousSegment[u3, FALSE]; Cap[b3, [u3.y, -u3.x], p3, q3] };
};
path.generateProc[path, Move, Line, Curve];
IF NOT first THEN Close[];
};
}.