File: SVToolObjectImpl.mesa
Copyright © 1984 by Xerox Corporation. All rights reserved.
Contents: Defines the master object classes "tool" and "jack". A tool draws itself as from 1 to 3 orthogonal rectangles (even for ray tracing). Any assembly can be made into a tool temporarily, and can draw itself in wireframe normally but ray trace as a tool to allow easy pointing to important object planes. A jack draws itself as a coordinate frame. Jacks are invisible in ray-traces and shaded polygon drawings.
Created: July 2, 1984 1:03:38 pm PDT
Last edited by: Eric Bier on January 15, 1985 0:12:48 am PST
DIRECTORY
CastRays,
CoordSys,
CSG,
CSGGraphics,
DisplayList3d,
Graphics,
IO,
Rope,
SV2d,
SV3d,
SVBoundBox,
SVModelTypes,
SVPolygon3d,
SVRayTypes,
SVSceneTypes,
SVToolObject,
TFI3d;
SVToolObjectImpl: CEDAR PROGRAM
IMPORTS CastRays, CoordSys, CSG, CSGGraphics, DisplayList3d, IO, SVBoundBox, SVPolygon3d, TFI3d
EXPORTS SVToolObject =
BEGIN
Assembly: TYPE = SVSceneTypes.Assembly;
AssemblyList: TYPE = SVSceneTypes.AssemblyList;
BoundHedron: TYPE = SVModelTypes.BoundHedron;
Camera: TYPE = SVModelTypes.Camera;
Classification: TYPE = SVRayTypes.Classification;
CoordSystem: TYPE = SVModelTypes.CoordSystem;
FrameBlock: TYPE = REF FrameBlockObj;
FrameBlockObj: TYPE = SVSceneTypes.FrameBlockObj;
LightSourceList: TYPE = SVModelTypes.LightSourceList;
MasterObject: TYPE = SVSceneTypes.MasterObject;
MasterObjectClass: TYPE = SVSceneTypes.MasterObjectClass;
PlanarSurface: TYPE = REF PlanarSurfaceObj;
PlanarSurfaceObj: TYPE = SVSceneTypes.PlanarSurfaceObj;
PlanarSurfaceList: TYPE = SVSceneTypes.PlanarSurfaceList;
Point2d: TYPE = SV2d.Point2d;
Point3d: TYPE = SV3d.Point3d;
Poly3d: TYPE = SV3d.Poly3d;
Primitive: TYPE = SVRayTypes.Primitive;
Ray: TYPE = SVRayTypes.Ray;
Shape: TYPE = SVSceneTypes.Shape;
ToolData: TYPE = SVSceneTypes.ToolData;
Vector: TYPE = SV3d.Vector;
toolClass: MasterObjectClass;
jackClass: MasterObjectClass;
ToolMakeMasterObject: PUBLIC PROC [name: Rope.ROPE, toolData: ToolData] RETURNS [mo: MasterObject] = TRUSTED {
mainBody: REF ANY ← toolData;
lineBody: REF ANY ← toolData;
shadeBody: REF ANY ← toolData;
rayCastBody: REF ANY ← toolData;
mo ← DisplayList3d.CreateMasterObject[name, toolClass, mainBody, lineBody, shadeBody, rayCastBody];
};
SizeToFit: PUBLIC PROC [toolData: ToolData, assembly: Assembly] = {
The tool in question will just contain in the [loX, hiX, loY, hiY, loZ, hiZ] box, the assembly given. Compute this tight-fitting box from the union of the boundhedron's of all of assembly's master objects. Express all of the boundHedron points in assembly coordinates. Since the boundhedrons are in master object records, we must walk the tree to find them. Since they are in master object coordinates, we must carefully translate them to the coordinates of assembly.
block: FrameBlock;
loX, hiX, loY, hiY, loZ, hiZ: REAL;
dataFound: BOOL;
[loX, hiX, loY, hiY, loZ, hiZ, dataFound] ← BoundBlockOfAssembly[assembly, assembly];
IF dataFound THEN
block ← NEW[FrameBlockObj ← [loX, hiX, loY, hiY, loZ, hiZ]]
ELSE block ← NEW[FrameBlockObj ← [-400, 400, -400, 400, -400, 400]];
toolData.block ← block;
toolData.infinite ← FALSE;
};
BoundBlockOfAssembly: PROC [assembly: Assembly, wrt: Assembly] RETURNS [loX, hiX, loY, hiY, loZ, hiZ: REAL, dataFound: BOOL] = TRUSTED {
For now, I will assume that any infinite objects are negative objects or are intersected with other objects. In either case, they can be ignored.
localDataFound: BOOL;
lX, hX, lY, hY, lZ, hZ: REAL;
dataFound ← FALSE;
WITH assembly.shape SELECT FROM
assems: AssemblyList => {
FOR subassemblies: LIST OF Assembly ← assems.list, subassemblies.rest
UNTIL subassemblies = NIL DO
[lX, hX, lY, hY, lZ, hZ, localDataFound] ←
BoundBlockOfAssembly[subassemblies.first, wrt];
IF localDataFound THEN {
IF (NOT dataFound) THEN {
loX ← lX; hiX ← hX; loY ← lY; hiY ← hY; loZ ← lZ; hiZ ← hZ;
}
ELSE {
loX ← MIN[loX, lX]; hiX ← MAX[hiX, hX];
loY ← MIN[loY, lY]; hiY ← MAX[hiY, hY];
loZ ← MIN[loZ, lZ]; hiZ ← MAX[hiZ, hZ];
};
};
dataFound ← dataFound OR localDataFound;
ENDLOOP;
};
shape: Shape => {
bh: BoundHedron ← shape.mo.class.getHedron[shape.mo];
dataFound ← TRUE;
IF bh = NIL THEN {dataFound ← FALSE; RETURN};
[loX, hiX, loY, hiY, loZ, hiZ] ← BoundBlockOfBoundHedron[bh, shape.coordSys, wrt.coordSys];
};
ENDCASE => ERROR;
};
BoundBlockOfBoundHedron: PROC [bh: BoundHedron, currentCS: CoordSystem, newCS: CoordSystem] RETURNS [loX, hiX, loY, hiY, loZ, hiZ: REAL] = TRUSTED {
thisPt: Point3d;
thisPt ← CoordSys.FromCSToCS[bh[0], currentCS, newCS];
loX ← hiX ← thisPt[1];
loY ← hiY ← thisPt[2];
loZ ← hiZ ← thisPt[3];
FOR i: NAT IN [1..bh.len) DO
thisPt ← CoordSys.FromCSToCS[bh[i], currentCS, newCS];
loX ← MIN[loX, thisPt[1]]; hiX ← MAX[hiX, thisPt[1]];
loY ← MIN[loY, thisPt[2]]; hiY ← MAX[hiY, thisPt[2]];
loZ ← MIN[loZ, thisPt[3]]; hiZ ← MAX[hiZ, thisPt[3]];
ENDLOOP;
};
NoOpToolFilein: DisplayList3d.FileinProc = {
mo ← NIL;
};
NoOpToolFileout: DisplayList3d.FileoutProc = {};
ToolRayCast: PROC [cameraPoint: Point2d, localRay: Ray, masterObject: REF ANY, prim: Primitive] RETURNS [class: Classification] = {
mo: MasterObject ← NARROW[masterObject];
RETURN[ToolRayCastAux[localRay, NARROW[mo.rayCastBody], prim]];
};
ToolRayCastAux: PROC [localRay: Ray, td: ToolData, prim: Primitive] RETURNS [class: Classification] = TRUSTED {
hitPoint: Point3d;
almostZero: REAL ← 1.0e-12;
p: Point3d;
d: Vector;
t: REAL;
class ← CastRays.GetClassFromPool[];
[p, d] ← CSG.GetLocalRay[localRay];
SELECT td.plane FROM
1 => { -- x = 0 plane only
IF ABS[d[1]] < almostZero THEN CastRays.MakeClassAMiss[class]
ELSE {
t ← (- p[1])/d[1];
hitPoint[1] ← 0.0;
hitPoint[2] ← p[2] + t*d[2];
hitPoint[3] ← p[3] + t*d[3];
IF hitPoint[2] > td.block.hiY OR hitPoint[2] < td.block.loY OR hitPoint[3] > td.block.hiZ OR hitPoint[3] < td.block.loZ THEN
CastRays.MakeClassAMiss[class]
ELSE {
class.count ← 2;
The choice of surface normal depends on the position of the origin of the ray.
IF p[1] < 0.0 THEN {
class.normals[1] ← [-1,0,0];
class.normals[2] ← [1,0,0]}
ELSE {
class.normals[1] ← [1,0,0];
class.normals[2] ← [-1,0,0]};
class.params[1] ← t;
class.params[2] ← t;
class.surfaces[1] ← NIL;
class.surfaces[2] ← NIL;
}
};
};
2 => { -- y = 0 plane only
IF ABS[d[2]] < almostZero THEN CastRays.MakeClassAMiss[class]
ELSE {
t ← (- p[2])/d[2];
hitPoint[1] ← p[1] + t*d[1];
hitPoint[2] ← 0.0;
hitPoint[3] ← p[3] + t*d[3];
IF hitPoint[1] > td.block.hiX OR hitPoint[1] < td.block.loX OR hitPoint[3] > td.block.hiZ OR hitPoint[3] < td.block.loZ THEN
CastRays.MakeClassAMiss[class]
ELSE {
class.count ← 2;
The choice of surface normal depends on the position of the origin of the ray.
IF p[2] < 0.0 THEN {
class.normals[1] ← [0,-1,0];
class.normals[2] ← [0,1,0]}
ELSE {
class.normals[1] ← [0,1,0];
class.normals[2] ← [0,-1,0]};
class.params[1] ← t;
class.params[2] ← t;
class.surfaces[1] ← NIL;
class.surfaces[2] ← NIL;
}
};
};
3 => { -- z = 0 plane only
IF ABS[d[3]] < almostZero THEN CastRays.MakeClassAMiss[class]
ELSE {
t ← (- p[3])/d[3];
hitPoint[1] ← p[1] + t*d[1];
hitPoint[2] ← p[2] + t*d[2];
hitPoint[3] ← 0.0;
IF hitPoint[1] > td.block.hiX OR hitPoint[1] < td.block.loX OR hitPoint[2] > td.block.hiY OR hitPoint[2] < td.block.loY THEN
CastRays.MakeClassAMiss[class]
ELSE {
The choice of surface normal depends on the position of the origin of the ray.
IF p[3] < 0 THEN {
class.normals[1] ← [0,0,-1];
class.normals[2] ← [0,0,1]}
ELSE {
class.normals[1] ← [0,0,1];
class.normals[2] ← [0,0,-1]};
class.count ← 2;
class.params[1] ← t;
class.params[2] ← t;
class.surfaces[1] ← NIL;
class.surfaces[2] ← NIL;
}
};
};
4 => ERROR;
ENDCASE => ERROR;
};
ToolRayCastNoBBoxes: PROC [localRay: Ray, masterObject: REF ANY, prim: Primitive] RETURNS [class: Classification] = {
mo: MasterObject ← NARROW[masterObject];
RETURN[ToolRayCastAux[localRay, NARROW[mo.rayCastBody], prim]];
};
ToolRayCastBoundingSpheres: PROC [localRay: Ray, masterObject: REF ANY, prim: Primitive] RETURNS [class: Classification] = {
mo: MasterObject ← NARROW[masterObject];
RETURN[ToolRayCastAux[localRay, NARROW[mo.rayCastBody], prim]];
};
ToolBoundHedron: PROC [mo: MasterObject] RETURNS [hedron: BoundHedron] = TRUSTED {
toolData: ToolData ← NARROW[mo.mainBody];
block: FrameBlock ← toolData.block;
hedron ← SVBoundBox.RectangularBoundHedron2[block.loX, block.hiX, block.loY, block.hiY, block.loZ, block.hiZ];
};
ToolLineDraw: PROC[dc: Graphics.Context, data: REF ANY, camera: Camera, localCS: CoordSystem] = TRUSTED {
td: ToolData ← NARROW[data];
f: FrameBlock ← td.block;
SELECT td.plane FROM
1 => DrawHitPadX[dc, f.hiZ, f.loZ, f.loY, f.hiY, camera, localCS];
2 => DrawHitPadY[dc, f.loX, f.hiX, f.loZ, f.hiZ, camera, localCS];
3 => DrawHitPadZ[dc, f.loX, f.hiX, f.loY, f.hiY, camera, localCS];
4 => {
DrawHitPadX[dc, f.hiZ, f.loZ, f.loY, f.hiY, camera, localCS];
DrawHitPadY[dc, f.loX, f.hiX, f.loZ, f.hiZ, camera, localCS];
DrawHitPadZ[dc, f.loX, f.hiX, f.loY, f.hiY, camera, localCS];
};
ENDCASE => ERROR;
};
DrawHitPadX: PRIVATE PROC [dc: Graphics.Context, left, right, down, up: REAL, camera: Camera, cs: CoordSystem] = TRUSTED {
CSGGraphics.SetCP[dc, [0, up, left], camera, cs]; -- up left
CSGGraphics.DrawTo[dc, [0, up, right], camera, cs]; --up right
CSGGraphics.DrawTo[dc, [0, down, right], camera, cs]; -- down right
CSGGraphics.DrawTo[dc, [0, down, left], camera, cs]; -- down left
CSGGraphics.DrawTo[dc, [0, up, left], camera, cs]; -- up left
};
DrawHitPadY: PRIVATE PROC [dc: Graphics.Context, left, right, down, up: REAL, camera: Camera, cs: CoordSystem] = TRUSTED {
CSGGraphics.SetCP[dc, [left, 0, up], camera, cs]; -- up left
CSGGraphics.DrawTo[dc, [right, 0, up], camera, cs]; --up right
CSGGraphics.DrawTo[dc, [right, 0, down], camera, cs]; -- down right
CSGGraphics.DrawTo[dc, [left, 0, down], camera, cs]; -- down left
CSGGraphics.DrawTo[dc, [left, 0, up], camera, cs]; -- up left
};
DrawHitPadZ: PRIVATE PROC [dc: Graphics.Context, left, right, down, up: REAL, camera: Camera, cs: CoordSystem] = TRUSTED {
CSGGraphics.SetCP[dc, [left, up, 0], camera, cs]; -- up left
CSGGraphics.DrawTo[dc, [right, up, 0], camera, cs]; --up right
CSGGraphics.DrawTo[dc, [right, down, 0], camera, cs]; -- down right
CSGGraphics.DrawTo[dc, [left, down, 0], camera, cs]; -- down left
CSGGraphics.DrawTo[dc, [left, up, 0], camera, cs]; -- up left
};
ToolCountSurf: PROC [masterObject: MasterObject] RETURNS [len: NAT] = {
toolData: ToolData ← NARROW[masterObject.mainBody];
IF toolData.plane < 4 THEN len ← 1
ELSE len ← 12;
};
SortedToolSurface: TYPE = REF SortedToolSurfaceObj;
SortedToolSurfaceObj: TYPE = RECORD [
plane: NAT,
f: FrameBlock,
left: BOOL,
down: BOOL
];
ToolGetSurf: PROC [assembly: Assembly, camera: CoordSystem] RETURNS [psl: PlanarSurfaceList] = TRUSTED {
The 3 tool rectangles are: (left, right, down, up)
hiZ loZ loY hiY
loX hiX loZ hiZ
loX hiX loY hiY.
mo: MasterObject ← assembly.toolMasterObject;
localCS: CoordSystem ← assembly.coordSys;
thisSortedSurface: PlanarSurface;
poly: Poly3d ← SVPolygon3d.CreatePoly[4];
toolData: ToolData ← NARROW[mo.mainBody];
avgDepth: REAL;
f: FrameBlock ← toolData.block;
psl ← NIL;
SELECT toolData.plane FROM
1,2,3,4 => { -- z = 0 plane
poly ← SVPolygon3d.AddPolyPoint[poly, [f.loX, f.loY, 0]];
poly ← SVPolygon3d.AddPolyPoint[poly, [f.loX, f.hiY, 0]];
poly ← SVPolygon3d.AddPolyPoint[poly, [f.hiX, f.hiY, 0]];
poly ← SVPolygon3d.AddPolyPoint[poly, [f.hiX, f.loY, 0]];
avgDepth ← AverageDepth[poly, localCS, camera];
thisSortedSurface ← NEW[PlanarSurfaceObj ← [
whichSurface: NEW[SortedToolSurfaceObj ← [toolData.plane, f, FALSE, FALSE]],
assembly: assembly,
normal: [0,0,1],
mo: mo,
depth: avgDepth]];
psl ← CONS[thisSortedSurface, psl];
SVPolygon3d.ClearPoly[poly];
};
ENDCASE => ERROR;
};
AverageDepth: PROC [poly: Poly3d, localCS, camera: CoordSystem] RETURNS [avgDepth: REAL] = TRUSTED {
sum: REAL ← 0;
realLen: REAL ← poly.len;
localPoint: Point3d;
FOR i: NAT IN[0..poly.len) DO
localPoint ← poly[i];
localPoint ← CSGGraphics.LocalToCamera[localPoint, localCS];
sum ← sum + localPoint[3];
ENDLOOP;
avgDepth ← sum/realLen;
};
ToolDrawSurf: PROC [dc: Graphics.Context, ps: PlanarSurface, lightSources: LightSourceList, camera: Camera] = TRUSTED {
whichSurface: SortedToolSurface ← NARROW[ps.whichSurface];
poly3d: Poly3d ← SVPolygon3d.CreatePoly[4];
f: FrameBlock ← whichSurface.f;
poly3d ← SVPolygon3d.AddPolyPoint[poly3d, [f.loX, f.loY, 0]];
poly3d ← SVPolygon3d.AddPolyPoint[poly3d, [f.loX, f.hiY, 0]];
poly3d ← SVPolygon3d.AddPolyPoint[poly3d, [f.hiX, f.hiY, 0]];
poly3d ← SVPolygon3d.AddPolyPoint[poly3d, [f.hiX, f.loY, 0]];
CSGGraphics.DrawAreaNormalAbsolute[dc, ps.normal, poly3d, ps.assembly.artwork, lightSources, camera, ps.assembly.coordSys];
};
Jack Objects
JackMakeMasterObject: PUBLIC PROC [name: Rope.ROPE] RETURNS [mo: MasterObject] = TRUSTED {
mainBody: REF ANYNIL;
lineBody: REF ANYNIL;
shadeBody: REF ANYNIL;
rayCastBody: REF ANYNIL;
mo ← DisplayList3d.CreateMasterObject[name, jackClass, mainBody, lineBody, shadeBody, rayCastBody];
};
JackBoundHedron: PUBLIC PROC [mo: MasterObject] RETURNS [hedron: BoundHedron] = TRUSTED {
hedron ← SVBoundBox.RectangularBoundHedron[2.0, 2.0, 2.0];
};
JackLineDraw: PUBLIC PROC[dc: Graphics.Context, data: REF ANY, camera: Camera, localCS: CoordSystem] = TRUSTED {
CSGGraphics.SetCP[dc, [-1.0,0.0,0.0], camera, localCS];
CSGGraphics.DrawTo[dc, [1.0,0.0,0.0], camera, localCS];
CSGGraphics.SetCP[dc, [0.0,-1.0,0.0], camera, localCS];
CSGGraphics.DrawTo[dc, [0.0,1.0,0.0], camera, localCS];
CSGGraphics.SetCP[dc, [0.0,0.0,-1.0], camera, localCS];
CSGGraphics.DrawTo[dc, [0.0,0.0,1.0], camera, localCS];
};
JackCountSurf: PUBLIC PROC [masterObject: MasterObject] RETURNS [len: NAT] = {
len ← 6;
};
JackGetSurf: PUBLIC PROC [assembly: Assembly, camera: CoordSystem] RETURNS [psl: PlanarSurfaceList] = {
shape: Shape ← NARROW[assembly.shape];
masterObject: MasterObject ← shape.mo;
psl ← NIL;
};
JackDrawSurf: PUBLIC PROC [dc: Graphics.Context, ps: PlanarSurface, lightSources: LightSourceList, camera: Camera] = {
};
JackFileout: PUBLIC PROC [f: IO.STREAM, mo: MasterObject] = {
Spheres can be recovered from scratch.
f.PutChar[IO.TAB];
f.PutF["data: procedural\n"];
};
JackFilein: PUBLIC PROC [f: IO.STREAM, name: Rope.ROPE] RETURNS [mo: MasterObject] = TRUSTED {
TFI3d.ReadRope[f, "data: procedural"];
TFI3d.ReadBlank[f];
mo ← JackMakeMasterObject[name];
};
Init: PROC = TRUSTED {
jack: MasterObject;
toolClass ← DisplayList3d.RegisterMasterObjectClass[
"tool",
NoOpToolFilein,
NoOpToolFileout,
DisplayList3d.NoOpFileoutPoly,
ToolRayCast,
ToolRayCastNoBBoxes,
ToolRayCastBoundingSpheres,
ToolBoundHedron,
DisplayList3d.NoOpPreprocess,
ToolLineDraw,
DisplayList3d.NoOpNormalsDraw,
ToolCountSurf,
ToolGetSurf,
ToolDrawSurf,
DisplayList3d.NoOpDrawSubBoxes,
DisplayList3d.NoOpDrawSubSpheres];
jackClass ← DisplayList3d.RegisterMasterObjectClass[
"jack",
JackFilein,
JackFileout,
DisplayList3d.NoOpFileoutPoly,
DisplayList3d.NoOpRayCast,
DisplayList3d.NoOpRayCastNoBBoxes,
DisplayList3d.NoOpRayCastBoundingSpheres,
JackBoundHedron,
DisplayList3d.NoOpPreprocess,
JackLineDraw,
DisplayList3d.NoOpNormalsDraw,
JackCountSurf,
JackGetSurf,
JackDrawSurf,
DisplayList3d.NoOpDrawSubBoxes,
DisplayList3d.NoOpDrawSubSpheres];
jack ← JackMakeMasterObject["jack"];
DisplayList3d.RegisterMasterObject[jack];
};
Init[];
END.