ImagerStrokeImpl.mesa
Copyright © 1984 Xerox Corporation. All rights reserved.
Michael Plass, February 16, 1984 10:34:32 am PST
Doug Wyatt, October 9, 1984 8:54:09 am PDT
DIRECTORY
ImagerPath USING [Filter, PathProc, Transform],
ImagerScanConverter USING [CreatePath, DevicePath, DeviceRectangle],
ImagerStroke USING [Bezier, StrokeStyle],
ImagerTransformation USING [SingularValues, Transform, Transformation],
Real USING [SqRt],
Vector2 USING [VEC];
ImagerStrokeImpl: CEDAR PROGRAM
IMPORTS ImagerPath, ImagerScanConverter, ImagerTransformation, Real
EXPORTS ImagerStroke
= BEGIN
VEC: TYPE = Vector2.VEC;
Bezier: TYPE = ImagerStroke.Bezier;
StrokeStyle: TYPE = ImagerStroke.StrokeStyle;
DevicePath: TYPE ~ ImagerScanConverter.DevicePath;
DeviceRectangle: TYPE ~ ImagerScanConverter.DeviceRectangle;
Transformation: TYPE ~ ImagerTransformation.Transformation;
PathProc: TYPE ~ ImagerPath.PathProc;
maxdepth: NAT ← 10; -- Used in Subdivide
tolerance: REAL ← 0.5; -- Used in GenerateStroke, GenerateThinStroke
Split: PUBLIC PROC [bezier: Bezier] RETURNS [firstHalf, secondHalf: 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 NOT In[bezier.b1,bl,bh] OR NOT 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 [bezier: Bezier, vertex: PROC[VEC], tolerance: REAL] ~ {
depth: CARDINAL ← 0;
Subdiv: PROC [b: Bezier] ~ {
IF depth>=maxdepth OR FlatBezier[b, tolerance] THEN vertex[b.b3]
ELSE { b1, b2: Bezier;
[b1, b2] ← Split[b];
depth ← depth + 1;
Subdiv[b1]; -- first half
Subdiv[b2]; -- second half
depth ← depth - 1;
};
};
Subdiv[bezier];
};
DevicePathFromStroke: PUBLIC PROC[
pathProc: ImagerPath.PathProc, pathData: REF,
width: REAL, style: StrokeStyle, closed: BOOLEAN,
clientToDevice: Transformation,
clipBox: DeviceRectangle,
scratch: DevicePath
] RETURNS [devicePath: DevicePath] = {
outerPathData: REF ~ pathData;
transformedStroke: ImagerPath.PathProc ~ {
sv: VEC ~ ImagerTransformation.SingularValues[clientToDevice];
AlmostCircular: PROC[p: VEC] RETURNS[BOOL] ~ {
min: REAL ~ MIN[p.x, p.y]; max: REAL ~ MAX[p.x, p.y];
epsilon: REAL ~ 0.1;
RETURN[ABS[min/max-1]<epsilon];
};
IF AlmostCircular[sv] AND sv.y * width < 1.0 THEN GenerateThinStroke[
pathProc: pathProc, pathData: outerPathData,
closed: closed, transformation: clientToDevice, clipBox: clipBox,
moveTo: moveTo, lineTo: lineTo]
ELSE GenerateStroke[
pathProc: pathProc, pathData: outerPathData,
width: width, style: style, closed: closed, transformation: clientToDevice,
moveTo: moveTo, lineTo: lineTo];
};
devicePath ← ImagerScanConverter.CreatePath[
pathProc: transformedStroke, pathData: NIL,
transformation: NIL, clipBox: clipBox, scratch: scratch];
};
GenerateThinStroke: PROC[pathProc: PathProc, pathData: REF,
closed: BOOL, transformation: Transformation, clipBox: DeviceRectangle,
moveTo: PROC[VEC], lineTo: PROC[VEC]] = {
sp: VEC; -- first point of (transformed) trajectory
mp: VEC; -- last point
xMin: REAL ← clipBox.sMin;
xMax: REAL ← clipBox.sMin+clipBox.sSize;
yMin: REAL ← clipBox.fMin;
yMax: REAL ← clipBox.fMin+clipBox.fSize;
Move: PROC[p: VEC] = { sp ← mp ← p };
Line: PROC[p: VEC] = {
p0: VEC ← mp;
p1: VEC ← p;
mp ← p1;
IF MAX[p0.x, p1.x] < xMin THEN RETURN;
IF MAX[p0.y, p1.y] < yMin THEN RETURN;
IF MIN[p0.y, p1.y] > yMax THEN RETURN;
IF MIN[p0.x, p1.x] > xMax THEN RETURN;
IF p1.x < p0.x THEN {t: VEC ← p0; p0 ← p1; p1 ← t};
IF p1.x-p0.x >= ABS[p1.y-p0.y] THEN {
moveTo[[p0.x-0.5, p0.y]];
lineTo[[p0.x, p0.y+0.5]];
lineTo[[p1.x, p1.y+0.5]];
lineTo[[p1.x+0.5, p1.y]];
lineTo[[p1.x, p1.y-0.5]];
lineTo[[p0.x, p0.y-0.5]];
}
ELSE {
IF p1.y < p0.y THEN {t: VEC ← p0; p0 ← p1; p1 ← t};
moveTo[[p0.x, p0.y-0.5]];
lineTo[[p0.x-0.5, p0.y]];
lineTo[[p1.x-0.5, p1.y]];
lineTo[[p1.x, p1.y+0.5]];
lineTo[[p1.x+0.5, p1.y]];
lineTo[[p0.x+0.5, p0.y]];
};
};
Curve: PROC[p1, p2, p3: VEC] = {
IF MAX[mp.x, p1.x, p2.x, p3.x] < xMin THEN {mp ← p3; RETURN};
IF MAX[mp.y, p1.y, p2.y, p3.y] < yMin THEN {mp ← p3; RETURN};
IF MIN[mp.y, p1.y, p2.y, p3.y] > yMax THEN {mp ← p3; RETURN};
IF MIN[mp.x, p1.x, p2.x, p3.x] > xMax THEN {mp ← p3; RETURN};
Subdivide[bezier: [mp, p1, p2, p3], vertex: Line, tolerance: tolerance];
};
Close: PROC = { IF closed THEN Line[sp] };
ImagerPath.Transform[pathProc: pathProc, pathData: pathData, m: transformation,
moveTo: Move, lineTo: Line, curveTo: Curve, close: Close];
};
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: PROC[pathProc: PathProc, pathData: REF,
width: REAL, style: StrokeStyle, closed: BOOL, transformation: Transformation,
moveTo: PROC[VEC], lineTo: PROC[VEC]] = {
map: PROC[p: VEC] RETURNS[VEC] ~ INLINE { RETURN[transformation.Transform[p]] };
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: BOOLEAN;
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 style FROM
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[];
};
butt => NULL;
ENDCASE => {
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[];
};
};
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]]] };
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;
};
Move: PROC[p: VEC] = { b3 ← p; begin ← TRUE };
Line: PROC[p: VEC] = {
u0: VEC ← HNormal[b3, p];
EmitPreviousSegment[u0];
b3 ← p; u3 ← u0; line ← TRUE;
};
Curve: PROC[p1, p2, p3: VEC] = {
u0: VEC ← (IF p1#b3 THEN HNormal[b3, p1] ELSE HNormal[b3, p2]);
EmitPreviousSegment[u0];
b1 ← p1; b2 ← p2; b3 ← p3; 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] };
};
ImagerPath.Filter[pathProc: pathProc, pathData: pathData,
moveTo: Move, lineTo: Line, curveTo: Curve, close: Close];
};
END.