-- File: IPBasicOps.mesa
-- Last Edited by: CSChow, February 1, 1985 8:20:23 am PST
--IPBasicOps = BasicOperations --
--Intro:This interface contains a lot of the very basic operations on those very basic types
-- Used throughout the rest of the code and helps achieve greater code density
DIRECTORY
IP;
IPBasicOps: CEDAR DEFINITIONS = BEGIN
CornerTypes: TYPE = IP.CornerTypes;
EdgeTypes: TYPE = IP.EdgeTypes;
PolarityTypes: TYPE = IP.PolarityTypes;
OrientationTypes: TYPE = IP.OrientationTypes;
ETCombineFailed: ERROR;
CTOrient: PROC[ct: CornerTypes, operation: IP.Orientation] RETURNS [CornerTypes];
CTMirror: PROC[ct: CornerTypes, mirrorOrient: OrientationTypes] RETURNS [CornerTypes];
CTRotate: PROC[ct: CornerTypes, cnt: INT ← 1] RETURNS[CornerTypes];
CTFindRotDirectn: PROC[ct: CornerTypes, ot: OrientationTypes] RETURNS [PolarityTypes];
CTDiag: PROC[ct: CornerTypes] RETURNS[CornerTypes] = INLINE {RETURN [CTRotate[ct, 2]]}; --CTDiag--
CTResolve: PROC[ct: CornerTypes] RETURNS [hor, ver: EdgeTypes];
CTResolveFor: PROC[ct: CornerTypes, ot: OrientationTypes] RETURNS [EdgeTypes];
CTDirectn: PROC[ct: CornerTypes] RETURNS [hor, ver: PolarityTypes];
CTDirectnFor: PROC[ct: CornerTypes, ot: OrientationTypes] RETURNS [PolarityTypes];
CTInvDirectn: PROC[ct: CornerTypes] RETURNS [hor, ver: PolarityTypes] = INLINE {[hor, ver] ← CTDirectn[ct]; hor ← PTNot[hor]; ver ← PTNot[ver]}; --CTInvDirectn--
CTInvDirectnFor: PROC[ct: CornerTypes, ot: OrientationTypes] RETURNS [PolarityTypes] = INLINE {RETURN [PTNot[CTDirectnFor[ct, ot]]]}; --CTInvDirectn--
CTFromPTs: PROC[hor, ver: PolarityTypes] RETURNS [CornerTypes];
ETRotate: PROC[et: EdgeTypes, cnt: INT ← 1] RETURNS[EdgeTypes];
ETOrient: PROC[et: EdgeTypes] RETURNS [OrientationTypes];
ETCombine: PROC[et1, et2: EdgeTypes] RETURNS [CornerTypes]; --! Raise ETCombineError--
ETDirectn: PROC[et: EdgeTypes] RETURNS [PolarityTypes];
ETInvDirectn: PROC[et: EdgeTypes] RETURNS [PolarityTypes] = INLINE {
RETURN [PTNot[ETDirectn[et]]]}; --ETInvDirectn--
PTNot: PROC [pt: PolarityTypes] RETURNS [PolarityTypes] = INLINE {RETURN [(SELECT pt FROM neg => pos, pos => neg, ENDCASE => ERROR)] }; --PTNot--
PTToInt: PROC[pt: PolarityTypes] RETURNS [INT] = INLINE {RETURN [(SELECT pt FROM neg => -1, pos => 1, ENDCASE => ERROR)] }; --PTToInt--
PTFromInt: PROC[i: INT] RETURNS [PolarityTypes] = INLINE {RETURN [(SELECT i FROM < 0 => neg, > 0 => pos, ENDCASE => ERROR)] }; --PTFromInt--
OTFlip: PROC [ot: OrientationTypes] RETURNS [OrientationTypes] = INLINE {RETURN [(SELECT ot FROM hor => ver, ver => hor, ENDCASE => ERROR)] }; --PTNot--
--Basic Creation Procs
NuIntVector: PROC[x, y: INT] RETURNS [REF IP.IntVector] = INLINE {
RETURN [NEW[IP.IntVector ← [x, y]]]
}; --NuIntVector
NuNatVector: PROC[x, y: NAT] RETURNS [REF IP.NatVector] = INLINE {
RETURN [NEW[IP.NatVector ← [x, y]]]
}; --NuNatVector
--Basic Copy Procs
Copy1Shape: PROC[oShape: REF IP.ShapeRep] RETURNS [REF IP.ShapeRep];
Copy2Shape: PROC[oShape: REF IP.ShapeRep] RETURNS [REF IP.ShapeRep];
--Primitive IntVector Operations
IntVectorNegate: PROC[v: IP.IntVector] RETURNS [res: IP.IntVector] = INLINE {
res.x ← - v.x;
res.y ← - v.y;
};--IntVectorNegate
IntVectorScale: PROC[v: IP.IntVector, factor: INT] RETURNS [res: IP.IntVector] = INLINE {
res.x ← v.x * factor;
res.y ← v.y * factor;
}; --IntVectorScale
IntVectorDivide: PROC[v: IP.IntVector, quotient: INT] RETURNS [res: IP.IntVector] = INLINE {
res.x ← v.x / quotient;
res.y ← v.y / quotient;
}; --IntVectorDivide
IntVectorMDistance: PROC[v1, v2: IP.IntVector] RETURNS [INT] = INLINE{
--Returns the Manhattan Distance between v1 and v2
RETURN [ABS[v1.x - v2.x] + ABS[v1.y - v2.y]]
};--IntVectorMLength
RectMDistanceToPt: PUBLIC PROC[rect: IP.Rect, pt: IP.IntVector] RETURNS [INT] = INLINE {
--MDistance = Manhattan Distance
xDist: INTIF rect.x1 <= pt.x AND pt.x <= rect.x2 THEN 0 ELSE MIN[ABS[pt.x - rect.x1], ABS[pt.x - rect.x2]];
yDist: INTIF rect.y1 <= pt.y AND pt.y <= rect.y2 THEN 0 ELSE MIN[ABS[pt.y - rect.y1], ABS[pt.y - rect.y2]];
RETURN [xDist + yDist]
}; --RectMDistanceToPt
RectExtendToPt: PROC[rect: IP.Rect, pt: IP.IntVector] RETURNS [IP.Rect] = INLINE {
-- Extend rect minimallly
RETURN [IP.Rect[x1: MIN[rect.x1, pt.x], y1: MIN[rect.y1, pt.y], x2: MAX[rect.x2, pt.x], y2: MAX[rect.y2, pt.y]]]}; --RectExtendToPt
END.