-- CornerStitching.mesa
-- Written by Mark Shand, September 12, 1983 11:40 pm
-- Last Edited by: Mccreight, November 28, 1983 5:53 pm
-- Last Edited by: Jacobi, February 24, 1984 12:14 pm
-- Last Edited by: Shand, June 1, 1984 2:20:32 pm PDT
DIRECTORY
CSBaseType USING [Number, Coord, Rect]
;
CornerStitching: CEDAR DEFINITIONS ~ BEGIN
-- Clean up.
DumpCache: PROCEDURE;
-- Creating Tile Worlds.
NewTesselation: PROCEDURE RETURNS [REF Tesselation];
FreeTesselation: PROCEDURE [plane: REF Tesselation, freeCache: BOOLEANTRUE];
-- Modifying the Tile World.
ChangeRect: PROCEDURE [plane: REF Tesselation, rect: Rect, newValue: REF ANYNIL];
FuncChangeRect: PROCEDURE [plane: REF Tesselation, rect: Rect, perTile: PerTileChangeProc, data: REF ANYNIL];
PerTileChangeProc: TYPE ~ PROCEDURE [plane: REF Tesselation, rect: Rect, oldValue: REF ANY, data: REF ANY];
ChangeTile: PROCEDURE [plane: REF Tesselation, tile: TilePtr, newValue: REF ANY] ~ INLINE {
-- Let a client change a tile provided it doesn't violate invariants. May raise ERROR ChangeUnsafe
TRUSTED {
ChangeUnsafe: ERROR ~ CODE;
IF tile.ne.value = newValue
OR tile.sw.value = newValue
OR (tile.en.value = newValue AND tile.en.pos.x = tile.pos.x AND tile.en.ne.pos.x = tile.ne.pos.x)
OR (tile.ws.value = newValue AND tile.ws.pos.x = tile.pos.x AND tile.ws.ne.pos.x = tile.ne.pos.x) THEN
ERROR ChangeUnsafe
};
tile.value ← newValue
};
-- Tile Enumeration routines.
-- Enumeration routines take a backgroundValue parameter, tiles with this value are ignored. If you want every tile, set backgroundValue to some value you know no tile has (e.g. NEW[INT]). perTile is called for each Tile found. If perTile = NIL then returns LIST OF REF Region, the list contains each tile not of backgroundValue clipped to rect. If ChangeRect is called from perTile during an Enumeration results are unpredictable!
PerTileProc: TYPE ~ PROCEDURE [tile: TilePtr, data: REF ANY];
-- In EnumerateArea the order of enumeration is well defined. A tile appears in the enumeration immediately after the last of its children. Where tileA is defined to be a child of tileB iff its sw stitch points to tileB. Children are ordered such that the southernmost is the last. For tiles touching the western and southern borders these rules do not apply, these tiles are parent-less. Such tiles appear at some point after all the parent-less tiles touching touching a given parent-less tile's northern and western borders have been enumerated.
EnumerateArea: PROCEDURE [plane: REF Tesselation, rect: Rect, perTile: PerTileProc ← NIL, data: REF ANYNIL, backgroundValue: REF ANYNIL] RETURNS [REF ANY];
-- If perTile = NIL then returns LIST OF REF Region;
TileAt: PROCEDURE [plane: REF Tesselation, pos: Coord] RETURNS [TilePtr];
-- Stepping from Tile to Tile.
-- Co-ordinates increase from south to north, and west to east. All tile worlds are bordered by tile at 'infinity' (i.e. INT.FIRST or INT.LAST), so bounded searches never need worry about tile = NIL. The tiles at infinity are not stitched to tiles in the Tesselation, rather they are stitched to each other in the manner of the bevelled edges of a picture frame. The meanings of the Stepping routines are as follows ...
-- ENorthNeighbour — eastern most tile to the north
-- NEastNeighbour — northern most tile to the east
-- WSouthNeighbour — western most tile to the south
-- SWestNeighbour — southern most tile to the west
ENorthNeighbour: PROCEDURE [tile: TilePtr] RETURNS [TilePtr] ~ INLINE {
RETURN [tile.en]
};
NEastNeighbour: PROCEDURE [tile: TilePtr] RETURNS [TilePtr] ~ INLINE {
TRUSTED { RETURN [LOOPHOLE[tile.ne]] }
};
WSouthNeighbour: PROCEDURE [tile: TilePtr] RETURNS [TilePtr] ~ INLINE {
TRUSTED { RETURN [LOOPHOLE[tile.ws]] }
};
SWestNeighbour: PROCEDURE [tile: TilePtr] RETURNS [TilePtr] ~ INLINE {
RETURN [tile.sw]
};
-- Tile Enquiries
Value: PROCEDURE [tile: TilePtr] RETURNS [REF ANY] ~ INLINE {
RETURN [tile.value]
};
Area: PROCEDURE [tile: TilePtr] RETURNS [Rect] ~ INLINE {
RETURN [[WestEdge[tile], SouthEdge[tile], EastEdge[tile], NorthEdge[tile]]]
};
NorthEdge: PROCEDURE [t: TilePtr] RETURNS [Number] ~ INLINE {RETURN [t.en.pos.y]};
EastEdge: PROCEDURE [t: TilePtr] RETURNS [Number] ~ INLINE { TRUSTED { RETURN [t.ne.pos.x] } };
SouthEdge: PROCEDURE [t: TilePtr] RETURNS [Number] ~ INLINE {RETURN [t.pos.y]};
WestEdge: PROCEDURE [t: TilePtr] RETURNS [Number] ~ INLINE {RETURN [t.pos.x]};
-- Area Enquiries.
AreaEmpty: PROCEDURE [plane: REF Tesselation, rect: Rect, backgroundValue: REF ANYNIL] RETURNS [BOOLEAN];
ContentsBound: PROCEDURE [plane: REF Tesselation, rect: Rect, backgroundValue: REF ANYNIL] RETURNS [bBox: Rect];
-- ContentsBound returns a minimal bounding box for non-backgroundValue tiles in rect
-- ERROR Codes.
TileValue: ERROR;
TileDeleted: ERROR;
-- Types.
-- Corner stitching is too circular for the current implementation of Cedar safestorage (too many tiles are pinned) so some of the stitches are pointers (idea courtesy of Mark Brown). We are careful to stitch in a way that connects every tile through a chain of REFs to the southeast tile, which we hang on to in the Tesselation record. Actually there is one benefit, now to free a Tesselation you only need drop this REF.
TilePtr: TYPE ~ REF Tile;
Tile: PRIVATE TYPE ~ RECORD [
-- Always leave tile in second direction, i.e en = NorthEast corner, facing North; ne faces East.
en, sw: REF Tile ← NIL,
ne, ws: LONG POINTER TO Tile ← NIL,
pos: Coord,
value: REF ANYNIL -- NIL for space tiles
];
Number: TYPE ~ CSBaseType.Number;
Coord: TYPE ~ CSBaseType.Coord;
Rect: TYPE ~ CSBaseType.Rect;
Region: TYPE ~ RECORD [
rect: Rect,
value: REF ANYNIL -- NIL for space tiles
];
Tesselation: TYPE ~ PRIVATE RECORD [
southEast, current: REF Tile ←,
tilesInTesselationCount: INT ← 1
];
END.