<> <> <> <> <> DIRECTORY CastRays, CSG, CSGGraphics, DisplayList3d, Imager, IO, Rope, SV2d, SV3d, SVDraw3d, SVHalfSpaces, SVPolygon3d, SVModelTypes, SVRayTypes, SVSceneTypes, TFI3d; SVHalfSpacesImpl: PROGRAM IMPORTS CastRays, CSG, CSGGraphics, DisplayList3d, IO, SVDraw3d, SVPolygon3d, TFI3d EXPORTS SVHalfSpaces = BEGIN Assembly: TYPE = SVSceneTypes.Assembly; BoundHedron: TYPE = SVModelTypes.BoundHedron; CoordSystem: TYPE = SVModelTypes.CoordSystem; Classification: TYPE = SVRayTypes.Classification; LightSourceList: TYPE = SVModelTypes.LightSourceList; Point2d: TYPE = SV2d.Point2d; Ray: TYPE = SVRayTypes.Ray; PlanarSurface: TYPE = REF PlanarSurfaceObj; PlanarSurfaceObj: TYPE = SVSceneTypes.PlanarSurfaceObj; PlanarSurfaceList: TYPE = SVSceneTypes.PlanarSurfaceList; Plane: TYPE = SV3d.Plane; Point3d: TYPE = SV3d.Point3d; Poly3d: TYPE = SV3d.Poly3d; Camera: TYPE = SVModelTypes.Camera; MasterObjectClass: TYPE = SVSceneTypes.MasterObjectClass; MasterObject: TYPE = SVSceneTypes.MasterObject; Primitive: TYPE = SVRayTypes.Primitive; Shape: TYPE = SVSceneTypes.Shape; Surface: TYPE = REF ANY; Vector: TYPE = SV3d.Vector; <<>> <> HalfSpaceRec: TYPE = REF HalfSpaceRecObj; HalfSpaceRecObj: TYPE = SVHalfSpaces.HalfSpaceRecObj; PlaneSurface: TYPE = REF PlaneSurfaceObj; PlaneSurfaceObj: TYPE = SVHalfSpaces.PlaneSurfaceObj; SortedHalfSpaceSurface: TYPE = REF SortedHalfSpaceSurfaceObj; SortedHalfSpaceSurfaceObj: TYPE = RECORD [i, j: INT]; halfSpaceClass: MasterObjectClass; HalfSpaceBody: PRIVATE PROC RETURNS [halfSpaceRec: HalfSpaceRec] = { halfSpaceRec _ NEW[HalfSpaceRecObj]; halfSpaceRec.gridSide _ 72.0; }; HalfSpaceMakeMasterObject: PUBLIC PROC [name: Rope.ROPE] RETURNS [mo: MasterObject] = { mainBody: HalfSpaceRec _ HalfSpaceBody[]; lineBody: REF ANY _ mainBody; shadeBody: REF ANY _ mainBody; rayCastBody: REF ANY _ HalfSpaceGetRayCastBody[]; mo _ DisplayList3d.CreateMasterObject[name, halfSpaceClass, mainBody, lineBody, shadeBody, rayCastBody]; }; HalfSpaceBoundHedron: PUBLIC PROC [mo: MasterObject] RETURNS [hedron: BoundHedron] = { hedron _ NIL; }; HalfSpaceGetRayCastBody: PRIVATE PROC [] RETURNS [planeS: PlaneSurface] = { planeS _ NEW[PlaneSurfaceObj]; }; HalfSpaceRayCast: PUBLIC PROC [cameraPoint: Point2d, localRay: Ray, masterObject: REF ANY, prim: Primitive] RETURNS [class: Classification] = { mo: MasterObject _ NARROW[masterObject]; RETURN[HalfSpaceCastAux[localRay, NARROW[mo.rayCastBody], prim]]; }; HalfSpaceCastAux: PRIVATE PROC [localRay: Ray, plane: Surface, prim: Primitive] RETURNS [class: Classification] = { <> <> <> <> <> almostZero: REAL _ 1.0e-12; t: REAL; rayStartsOut: BOOL; p: Point3d; d: Vector; class _ CastRays.GetClassFromPool[]; [p, d] _ CSG.GetLocalRay[localRay]; rayStartsOut _ p[2] >0; IF Abs[d[2]] > almostZero THEN { t _ -p[2]/d[2]; IF t > 0 THEN { <> class.count _ 1; class.params[1] _ t; class.surfaces[1] _ plane; class.normals[1] _ [0,1,0]; class.primitives[1] _ prim; IF rayStartsOut THEN { class.classifs[1] _ FALSE; class.classifs[2] _ TRUE; } ELSE { class.classifs[1] _ TRUE; class.classifs[2] _ FALSE; }; } ELSE { <> class.count _ 0; class.classifs[1] _ NOT rayStartsOut; }; } ELSE { <> class.count _ 0; IF rayStartsOut THEN class.classifs[1] _ FALSE ELSE class.classifs[1] _ TRUE; }; }; -- end of HalfSpaceCastAux Abs: PROC [r: REAL] RETURNS [REAL] = { RETURN[IF r >= 0 THEN r ELSE -r]; }; HalfSpaceRayCastNoBBoxes: PUBLIC PROC [localRay: Ray, masterObject: REF ANY, prim: Primitive] RETURNS [class: Classification] = { mo: MasterObject _ NARROW[masterObject]; RETURN[HalfSpaceCastAux[localRay, NARROW[mo.rayCastBody], prim]]; }; HalfSpaceRayCastBoundingSpheres: PUBLIC PROC [localRay: Ray, masterObject: REF ANY, prim: Primitive] RETURNS [class: Classification] = { mo: MasterObject _ NARROW[masterObject]; RETURN[HalfSpaceCastAux[localRay, NARROW[mo.rayCastBody], prim]]; }; HalfSpaceFancyLineDraw: PRIVATE PROC[dc: Imager.Context, data: REF ANY, camera: Camera, localCS: CoordSystem] = { halfSpaceRec: HalfSpaceRec _ NARROW[data]; gridSide: REAL _ halfSpaceRec.gridSide; plane: Plane _ SVPolygon3d.PlaneFromCoefficients[0,1,0,0]; CSGGraphics.DrawInfiniteLine[dc, [0,0,0], [1,0,0], camera, localCS]; CSGGraphics.DrawInfiniteLine[dc, [0,0,gridSide], [1,0,gridSide], camera, localCS]; CSGGraphics.DrawInfiniteLine[dc, [0,0,-gridSide], [1,0,-gridSide], camera, localCS]; CSGGraphics.DrawInfiniteLine[dc, [0,0,0], [0,0,1], camera, localCS]; CSGGraphics.DrawInfiniteLine[dc, [gridSide,0,0], [gridSide,0,1], camera, localCS]; CSGGraphics.DrawInfiniteLine[dc, [-gridSide,0,0], [-gridSide,0,1], camera, localCS]; CSGGraphics.DrawHorizonOfPlane[dc, plane, camera, localCS]; }; HalfSpaceLineDraw: PUBLIC PROC[dc: Imager.Context, data: REF ANY, camera: Camera, localCS: CoordSystem] = { halfSpaceRec: HalfSpaceRec _ NARROW[data]; gridSide: REAL _ halfSpaceRec.gridSide; lineLen: REAL _ 100.0; CSGGraphics.DrawLine[dc, [-lineLen,0,0], [lineLen,0,0], camera, localCS]; CSGGraphics.DrawLine[dc, [-lineLen,0,gridSide], [lineLen,0,gridSide], camera, localCS]; CSGGraphics.DrawLine[dc, [-lineLen,0,-gridSide], [lineLen,0,-gridSide], camera, localCS]; CSGGraphics.DrawLine[dc, [0,0,-lineLen], [0,0,lineLen], camera, localCS]; CSGGraphics.DrawLine[dc, [gridSide,0,-lineLen], [gridSide,0,lineLen], camera, localCS]; CSGGraphics.DrawLine[dc, [-gridSide,0,-lineLen], [-gridSide,0,lineLen], camera, localCS]; }; HalfSpaceDrawNormals: PUBLIC PROC[dc: Imager.Context, data: REF ANY, camera: Camera, localCS: CoordSystem] = { SVDraw3d.DrawLocalVector[dc, [0,1,0], [0,0,0], camera, localCS]; }; HalfSpaceCountSurf: PUBLIC PROC [masterObject: MasterObject] RETURNS [len: NAT] = { len _ 4; }; HalfSpaceCountVert: PUBLIC PROC [masterObject: MasterObject] RETURNS [len: NAT] = { len _ 9; }; <> <> <> <> <> <> <> <> <<}; >> AverageDepthInCamera: PROC [poly: Poly3d, localCS, camera: CoordSystem] RETURNS [avgDepth: REAL] = { 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; }; HalfSpaceGetSurf: PUBLIC PROC [assembly: Assembly, camera: CoordSystem] RETURNS [psl: PlanarSurfaceList] = { poly: Poly3d _ SVPolygon3d.CreatePoly[4]; avgDepth: REAL; shape: Shape _ NARROW[assembly.shape, Shape]; mo: MasterObject _ shape.mo; localCS: CoordSystem _ shape.coordSys; thisSortedSurface: PlanarSurface; halfSpaceRec: HalfSpaceRec _ NARROW[mo.shadeBody]; gridSide: REAL _ halfSpaceRec.gridSide; FOR i: INT IN [-1..0] DO FOR j: INT IN [-1..0] DO poly _ SVPolygon3d.AddPolyPoint[poly, [i*gridSide, 0, j*gridSide]]; poly _ SVPolygon3d.AddPolyPoint[poly, [i*gridSide, 0, (j+1)*gridSide]]; poly _ SVPolygon3d.AddPolyPoint[poly, [(i+1)*gridSide, 0, (j+1)*gridSide]]; poly _ SVPolygon3d.AddPolyPoint[poly, [(i+1)*gridSide, 0, j*gridSide]]; avgDepth _ AverageDepthInCamera[poly, localCS, camera]; thisSortedSurface _ NEW[PlanarSurfaceObj _ [ whichSurface: NEW[SortedHalfSpaceSurfaceObj _ [i,j]], assembly: assembly, normal: [0,1,0], mo: mo, depth: avgDepth]]; psl _ CONS[thisSortedSurface, psl]; SVPolygon3d.ClearPoly[poly]; ENDLOOP; ENDLOOP; }; <> <> <> <> <> <<};>> <<>> HalfSpaceDrawSurf: PUBLIC PROC [dc: Imager.Context, ps: PlanarSurface, lightSources: LightSourceList, camera: Camera] = { i, j: INT; poly: Poly3d _ SVPolygon3d.CreatePoly[4]; avgDepth: REAL; shape: Shape; localCS: CoordSystem; thisHalfSpaceSurface: SortedHalfSpaceSurface; mo: MasterObject; halfSpaceRec: HalfSpaceRec; gridSide: REAL; thisHalfSpaceSurface _ NARROW[ps.whichSurface]; shape _ NARROW[ps.assembly.shape]; mo _ shape.mo; halfSpaceRec _ NARROW[mo.shadeBody]; gridSide _ halfSpaceRec.gridSide; localCS _ shape.coordSys; i _ thisHalfSpaceSurface.i; j _ thisHalfSpaceSurface.j; avgDepth _ ps.depth; poly _ SVPolygon3d.AddPolyPoint[poly, [i*gridSide, 0, j*gridSide]]; poly _ SVPolygon3d.AddPolyPoint[poly, [i*gridSide, 0, (j+1)*gridSide]]; poly _ SVPolygon3d.AddPolyPoint[poly, [(i+1)*gridSide, 0, (j+1)*gridSide]]; poly _ SVPolygon3d.AddPolyPoint[poly, [(i+1)*gridSide, 0, j*gridSide]]; CSGGraphics.DrawAreaNormalAbsolute[dc, ps.normal, poly, ps.assembly.artwork, lightSources, camera, localCS]; }; HalfSpaceFileout: PUBLIC PROC [f: IO.STREAM, mo: MasterObject] = { <> f.PutChar[IO.TAB]; f.PutF["data: procedural\n"]; }; HalfSpaceFilein: PUBLIC PROC [f: IO.STREAM, name: Rope.ROPE] RETURNS [mo: MasterObject] = { TFI3d.ReadRope[f, "data: procedural"]; TFI3d.ReadBlank[f]; mo _ HalfSpaceMakeMasterObject[name]; }; Init: PROC = { halfSpace: MasterObject; halfSpaceClass _ DisplayList3d.RegisterMasterObjectClass[ "halfSpace", HalfSpaceFilein, HalfSpaceFileout, DisplayList3d.NoOpFileoutPoly, HalfSpaceRayCast, HalfSpaceRayCastNoBBoxes, HalfSpaceRayCastBoundingSpheres, HalfSpaceBoundHedron, DisplayList3d.NoOpPreprocess, HalfSpaceLineDraw, HalfSpaceDrawNormals, HalfSpaceCountSurf, HalfSpaceGetSurf, HalfSpaceDrawSurf, DisplayList3d.NoOpDrawSubBoxes, DisplayList3d.NoOpDrawSubSpheres]; halfSpace _ HalfSpaceMakeMasterObject["halfSpace"]; DisplayList3d.RegisterMasterObject[halfSpace]; }; Init[]; END.