--m.stone September 26, 1980 5:12 PM
--PointDefs
-- Last Edited by: Stone, January 26, 1983 4:22 pm
DIRECTORY
 ControllerDefs: FROM "ControllerDefs",
 PointDefs: FROM "PointDefs",
 XFormDefs: FROM "XFormDefs",
 Real: FROM "Real",
 GriffinMemoryDefs USING [CZone];

PointFns: PROGRAM
IMPORTS Real,XFormDefs,ControllerDefs, GriffinMemoryDefs
EXPORTS PointDefs=
BEGIN OPEN PointDefs, GriffinMemoryDefs;
xyzw: INTEGER;
ScrToObjMatrix: XFormDefs.XFMDescriptor ← CZone.NEW[XFormDefs.XFormMatrix];
ObjToScrMatrix: XFormDefs.XFMDescriptor ← CZone.NEW[XFormDefs.XFormMatrix];
ValScale: REAL;

OutOfRange: PUBLIC SIGNAL[value: REAL] = CODE;

--real space to screen space. Float will return an error if pt is out of range of 32 bits
--This will signal if pt is not in [32767..-32768]. ie, inside 16 bits, 2's compliment
ObjValToScrVal: PUBLIC PROCEDURE [val: REAL] RETURNS [INTEGER] =
BEGIN
int: INTEGER;
val ← val*ValScale; 
int ← Real.RoundI[val];
RETURN[int];
END;

ObjToScr: PUBLIC PROCEDURE [pt: ObjPt] RETURNS [ScrPt] =
BEGIN
scr: ScrPt;
pt ← XFormDefs.XFormPt[pt,ObjToScrMatrix];
scr[X] ← Real.RoundI[pt[X]];
scr[Y] ← Real.RoundI[pt[Y]];
RETURN[scr];
END;

ScrValToObjVal: PUBLIC PROCEDURE [val: INTEGER] RETURNS [REAL] =
BEGIN
real: REAL ← val/ValScale;
RETURN[real];
END;

ScrToObj: PUBLIC PROCEDURE [pt: ScrPt] RETURNS [ObjPt] =
BEGIN
obj: ObjPt;
FOR xyzw IN [X..NDIM] DO
 obj[xyzw] ← pt[xyzw]; --convert to REAL
ENDLOOP;
obj ← XFormDefs.XFormPt[obj,ScrToObjMatrix];
RETURN[obj];
END;

InitPointFns: PUBLIC PROCEDURE =
BEGIN OPEN XFormDefs;
DController: ControllerDefs.DisplayController ← ControllerDefs.ReadDisplayController[];
HController: ControllerDefs.HardcopyController ← ControllerDefs.ReadHardcopyController[];
realpt,dcenterobj: ObjPt;
hcenterscr: ScrPt;

BEGIN OPEN DController,HController; --only for open
 InitXForms[ObjToScrMatrix];
 Translate[[0,dheight],ObjToScrMatrix];
 Scale[[1,-1],ObjToScrMatrix]; --flip it
 Scale[[dxscale,dyscale],ObjToScrMatrix];

 ValScale ← dxscale; --arbitrary choice

 InitXForms[ScrToObjMatrix];
 Scale[[1/dxscale,1/dyscale],ScrToObjMatrix];
 Translate[[0,dheight],ScrToObjMatrix];
 Scale[[1,-1],ScrToObjMatrix]; --flip it

 dcenterobj ← XFormPt[[dxcenter,dycenter],ScrToObjMatrix];
 realpt ← [hxcenter,hycenter];
 hcenterscr ← ObjToScr[realpt];
 Translate[[dcenterobj[X]-hxcenter,dcenterobj[Y]-hycenter],ObjToScrMatrix];
 Translate
  [[hcenterscr[X]-INTEGER[dxcenter],hcenterscr[Y]-INTEGER[dycenter]],ScrToObjMatrix];
END;
END;

ObjToScrReal: PUBLIC PROCEDURE [pt: ObjPt] RETURNS [ObjPt] =
BEGIN
obj: ObjPt ← XFormDefs.XFormPt[pt,ObjToScrMatrix];
RETURN[obj];
END;
END.