G3dShape.mesa
Copyright Ó 1985, 1992 by Xerox Corporation. All rights reserved.
Bloomenthal, October 21, 1992 5:00 pm PDT
Crow, May 5, 1989 4:55:50 pm PDT
DIRECTORY Atom, G2dBasic, G3dBasic, G3dMatrix, G3dQuaternion, IO, Rope;
G3dShape: CEDAR DEFINITIONS
~ BEGIN
Errors
Error:     SIGNAL [code: ATOM, reason: ROPE];
Imported Types and Constants
PropList:    TYPE ~ Atom.PropList;
Box2d:     TYPE ~ G2dBasic.Box;
IntegerPair:   TYPE ~ G2dBasic.IntegerPair;
Box3d:     TYPE ~ G3dBasic.Box;
NatSequence:   TYPE ~ G3dBasic.NatSequence;
Pair:     TYPE ~ G3dBasic.Pair;
PairSequence:  TYPE ~ G3dBasic.PairSequence;
Quad:     TYPE ~ G3dBasic.Quad;
Ray:     TYPE ~ G3dBasic.Ray;
RealSequence:  TYPE ~ G3dBasic.RealSequence;
Screen:    TYPE ~ G3dBasic.Screen;
Sphere:    TYPE ~ G3dBasic.Sphere;
SurfaceSequence: TYPE ~ G3dBasic.SurfaceSequence;
Triple:    TYPE ~ G3dBasic.Triple;
TripleSequence:  TYPE ~ G3dBasic.TripleSequence;
Matrix:    TYPE ~ G3dMatrix.Matrix;
Viewport:    TYPE ~ G3dMatrix.Viewport;
Quaternion:   TYPE ~ G3dQuaternion.Quaternion;
STREAM:    TYPE ~ IO.STREAM;
ROPE:     TYPE ~ Rope.ROPE;
zAxis:     Ray ~ [[0.0, 0.0, 0.0], [0.0, 0.0, 1.0]];
Shape Types
Shape:     TYPE ~ REF ShapeRep;
ShapeRep:   TYPE ~ RECORD [
Data for rendering, modeling, organizing, and the client
renderData:   REF ¬ NIL,    -- rendering params/procs, (see G3dRender)
modelData:   REF ¬ NIL,    -- modeling parameters (see G3dModel)
hierarchyData:  REF ¬ NIL,    -- node within hierarchy (see G3dTimeTrees)
clientData:   REF ¬ NIL,    -- data for a client
Bookkeeping
name:     ROPE ¬ NIL,    -- local shape name (allows instancing)
fileName:    ROPE ¬ NIL,    -- file name
props:     PropList ¬ NIL,   -- for extensibility
Transformation
centroid:    Triple ¬ [],    -- vertex average
scale:     REAL ¬ 1.0,    -- scale of the object
translation:   Triple ¬ [],    -- within a scene
orientation:   Quaternion ¬ [],  -- rotation (degrees) about an axis
rotationBase:   Triple ¬ [],    -- origin point of axis of rotation
matrix:    Matrix ¬ NIL,   -- within world space; presumed always valid
State
visible:    BOOL ¬ TRUE,   -- display shape or not?
centroidValid:  BOOL ¬ FALSE,   -- centroid computed?
renderValid:   BOOL ¬ FALSE,   -- are vertex normals, etc. valid?
showBackfaces:  BOOL ¬ FALSE,   -- if back facing polygons to be displayed
triangulated:   BOOL ¬ FALSE,   -- all surfaces are three-sided
normalsNegated:  BOOL ¬ FALSE,   -- if file vertex normals negated
surfacesReversed: BOOL ¬ FALSE,   -- if file surfaces order reversed
verticesIsolated:  BOOL ¬ FALSE,   -- if vertices have been isolated
Interaction
selected:    BOOL ¬ FALSE,   -- true if chosen
Bounds
sphereExtent:  Sphere ¬ [],    -- center and radius of bounding sphere
objectExtent:   Box3d ¬ [],    -- 3d bounds
Vertices
vertices:    VertexSequence ¬ NIL, -- vertices used by the renderer
Surfaces
type:     ATOM ¬ $ConvexPolygon, -- patches, polygons, or other
faces:     FaceSequence ¬ NIL, -- polygon normals, colors, and transmittances
surfaces:    SurfaceSequence ¬ NIL, -- vertex connectivity (polygons or patches)
Edges
edges:     EdgeSequence ¬ NIL-- for fast line drawing and other operations
];
Validity:    TYPE ~ {normal, color, texture, transmit, center};
ShapeSequence:  TYPE ~ REF ShapeSequenceRep;
ShapeSequenceRep: TYPE ~ RECORD [
length:      CARDINAL ¬ 0,
element:      SEQUENCE maxLength: CARDINAL OF Shape
];
ShapeProc:   TYPE ~ PROC [shape: Shape] RETURNS [continue: BOOL ¬ TRUE];
SurfaceProc:   TYPE ~ PROC [surface: NatSequence, index: INTEGER]
        RETURNS [continue: BOOL ¬ TRUE];
Vertex Types
Vertex:    TYPE ~ REF VertexRep;
VertexRep:   TYPE ~ RECORD [    -- 31 words
point:       Triple ¬ [],   -- 6 words: location of vertex
normal:      Triple ¬ [],   -- 6 words: surface normal
color:       Triple ¬ [1, 1, 1], -- 6 words: color of surface
texture:      Pair ¬ [0.0, 0.0],  -- 4 words: texture coordinates
transmittance:    REAL ¬ 1.0,   -- 2 words: transmittance of surface
ref:       REF ¬ NIL   -- 2 words: client data
];
The default vertex is transparent, and is scaled by a global transmission factor defaulted to 0.
This allows affect by global transmission whereas a default transmission of 0 would not.
VertexSequence:  TYPE ~ REF VertexSequenceRep;
VertexSequenceRep: TYPE ~ RECORD [
valid:       ARRAY Validity OF BOOL ¬ ALL[FALSE],
length:      CARDINAL ¬ 0,
element:      SEQUENCE maxLength: CARDINAL OF Vertex
];
VertexProc:   TYPE ~ PROC [vertex: Vertex, index: INTEGER]
        RETURNS [continue: BOOL ¬ TRUE];
Screen Types
ScreenSequence:  TYPE ~ REF ScreenSequenceRep;
ScreenSequenceRep: TYPE ~ RECORD [
screensValid:     BOOL ¬ FALSE,
extentValid:     BOOL ¬ FALSE,
extent:      Box2d ¬ [[0, 0], [0, 0]], -- 2d bounds (depends on view, device)
length:      CARDINAL ¬ 0,
element:      SEQUENCE maxLength: CARDINAL OF Screen
];
Polygon Types
Face:     TYPE ~ REF FaceRep;
FaceRep:    TYPE ~ RECORD [    -- 22 words
normal:      Triple ¬ [],   -- 6 words: surface normal
center:      Triple ¬ [],   -- 6 words: center of surface
color:       Triple ¬ [1, 1, 1], -- 6 words: color of surface
transmittance:    REAL ¬ 1.0,   -- 2 words: transmittance of surface
fwdFacing:     BOOL ¬ FALSE,  -- 1 bit: is this face forward facing?
ref:       REF ¬ NIL   -- 2 words: client data
];
FaceSequence:  TYPE ~ REF FaceSequenceRep;
FaceSequenceRep: TYPE ~ RECORD [
valid:       ARRAY Validity OF BOOL ¬ ALL[FALSE],
length:      CARDINAL ¬ 0,
element:      SEQUENCE maxLength: CARDINAL OF Face
];
Edge Types
Edge:     TYPE ~ REF EdgeRep;
EdgeRep:    TYPE ~ RECORD [
v0, v1:      INTEGER ¬ -1, -- vertex indices, v0 < v1
p0, p1:      INTEGER ¬ -1 -- polygon indices
];
EdgeSequence:  TYPE ~ REF EdgeSequenceRep;
EdgeSequenceRep: TYPE ~ RECORD [
length:      CARDINAL ¬ 0,
element:      SEQUENCE maxLength: CARDINAL OF Edge
];
An edge sequence is presumed ordered: edges[i].v0 < edges[i+1].v0.
File IO
ShapeFromFile: PROC [fileName: ROPE] RETURNS [Shape];
Return the shape given in the file.
ShapeFromStream: PUBLIC PROC [stream: STREAM] RETURNS [Shape];
Return the shape given the input file stream.
ShapeToFile: PROC [
fileName: ROPE,
shape: Shape,
index: BOOL ¬ FALSE,
tallyOnPolys: BOOL ¬ FALSE,
comment: ROPE ¬ NIL];
Write the shape to the given file. If index, add indexing for vertices and polygons.
If tallyOnPolys, write the polygon length for each polygon before the vertex ids.
If comment not NIL, write comment after the keyword "Comment ."
ShapeToStream: PROC [
shape: Shape,
out: STREAM,
index: BOOL ¬ FALSE,
tallyOnPolys: BOOL ¬ FALSE];
Write the shape to the given stream. If index, add indexing for vertices and polygons.
If tallyOnPolys, write the polygon length for each polygon before the vertex ids.
ShapeToStreamPerFormat: PROC [shape: Shape, out: STREAM, format: ATOM]
RETURNS [success: BOOL];
Write the shape to the stream per the specified format.
See G3dShapeImpl.mesa for supported formats.
Creation
ShapeFromData: PROC [
name: ROPE ¬ NIL,
surfaces: SurfaceSequence,
vertices: TripleSequence,
normals: TripleSequence ¬ NIL,
colors: TripleSequence ¬ NIL,
transmittances: RealSequence ¬ NIL,
textures: PairSequence ¬ NIL,
showBackfaces: BOOL ¬ TRUE,
faceted: BOOL ¬ FALSE,
type: ATOM ¬ $ConvexPolygon]
RETURNS [Shape];
Create a shape from the supplied data.
If faceted, then colors are applied to surfaces (facets) rather than vertices.
Copying/Combining
CopyShape: PROC [shape: Shape] RETURNS [Shape];
Return a copy of the input shape. Everything defined in this definitions file is physically duplicated, so changes to a copy won't affect the original. RenderData, modelData and clientData are copied as pointers. Code specific to those fields should treat them as it sees fit.
CombineShapes: PROC [shape1, shape2: Shape] RETURNS [Shape];
Return a shape that is a combination of the two input shapes.
Coincident vertices and redundant polygons are not culled.
Transformations
ComputeMatrix: PROC [shape: Shape];
Set shape.matrix based on shape.position, orientation, and scale.
SetMatrix: PROC [shape: Shape, matrix: Matrix];
Set shape.matrix to be matrix.
TransformShape: PROC [
shape: Shape,
translate: Triple ¬ [],
axis: Ray ¬ zAxis,
rotation: REAL ¬ 0,
scale: REAL ¬ 1.0,
concat: BOOL ¬ FALSE];
Places the local origin of shape at absolute position in world space.
Apply a rotation (in degrees) to the shape around the given axis.
Note the axis need not pass through [0, 0, 0].
This does not modify vertex fields.
If concat, concatenate the transform to the current matrix; otherwise, begin with identity.
TransformVertices: PROC [shape: Shape, matrix: Matrix];
Modify the vertex locations and normals by the given matrix.
ApplyTransformsToShape: PUBLIC PROC [shape: Shape];
Call TransformVertices, set the shape's matrix to identity, and recompute bounding sphere
and box. Set validities to force recomputation of face information if needed later.
Vertex Procedures
DoWithVertices: PROC [shape: Shape, vertexProc: VertexProc];
Apply vertexProc to each of the shape's vertices.
VertexValid: PROC [shape: Shape, test: Validity] RETURNS [BOOL];
Test the given vertex attribute for validity.
SetVertexNormals: PROC [shape: Shape];
Compute the vertex normal as an average of the surrounding face normals.
Computes face normals if not previously computed.
SetFwdFacingVertices: PROC [shape: Shape, view: Matrix, screens: ScreenSequence];
Set the fwdFacing boolean for each vertex,
depending on whether the vertex belongs to a forward facing polygon or not.
UnitizeVertexNormals: PROC [shape: Shape];
Set the shape vertex normals to unit length.
NegateVertexNormals: PROC [shape: Shape];
Invert the direction of the vertex normals.
NumberOfVertices: PROC [shapes: ShapeSequence] RETURNS [INTEGER];
Return sum of all vertices in all shapes;
SetExtent: PROC [screens: ScreenSequence];
Recompute screens.extent.
InterpolateVertex: PROC [t: REAL, v0, v1: Vertex] RETURNS [v: Vertex];
Return a vertex linearly interpolated such that v = v0+t*(v1-v0).
Interpolates normal, color, and texture if non-zero.
Polygon Procedures
DoWithAllPolygons: PROC [shape: Shape, action: SurfaceProc];
Apply action to all polygons.
DoWithFacingPolygons: PROC [
shape: Shape,
view: Matrix,
frontFacingAction, backFacingAction: SurfaceProc ¬ NIL];
Apply frontFacingAction (if non-NIL) to front facing polygons.
Apply backFacingAction (if non-NIL) to back facing polygons.
FaceValid: PROC [shape: Shape, test: Validity] RETURNS [BOOL];
Test the given face attribute for validity.
SetFwdFacingFaces: PROC [shape: Shape, view: Matrix];
Set the fwdFacing boolean for each of shape.faces.
GetPolygonPoints: PROC [shape: Shape, nPoly: INTEGER, points: TripleSequence ¬ NIL]
RETURNS [TripleSequence];
Return the vertices of the nPolyth polygon.
SetFaceNormals: PROC [shape: Shape, complyWithVertices: BOOL ¬ TRUE];
Set the face normals for the polygons, assuming shape.pts or shape.vertices is defined.
If complyWithVertices and shape.vertices # NIL, then the face normals are inverted,
if necessary, to face in the same half plane as their first vertex.
SetFaceCenters: PROC [shape: Shape];
Set the face centers for the polygons.
UnitizeFaceNormals: PROC [shape: Shape];
Set the shape face normals to unit length.
ReversePolygons: PROC [shape: Shape];
Reverse the ordering of the polygons and negate the face normals.
NegateFaceNormals: PROC [shape: Shape];
Invert the direction of the face normals.
Triangulate: PROC [shape: Shape];
Convert the shape's polygons into triangles.
NumberOfPolygons: PROC [shapes: ShapeSequence] RETURNS [INTEGER];
Return sum of all polygons in all shapes.
Edge Procedures
MakeEdges: PROC [shape: Shape] RETURNS [EdgeSequence];
Return the non-duplicating edges of a points-polygons shape.
FindEdge: PROC [edges: EdgeSequence, v0, v1: INTEGER] RETURNS [index: INT];
Return index so that edges[index].v0 = MIN[v0,v1] and edges[index].v1 = MAX[v0,v1];
return -1 if no such index exists.
Miscellany
ObjectScale: PROC [shape: Shape] RETURNS [REAL];
Return the scale necessary to fit the shape into a +1/-1 range.
PointsFromShape: PROC [shape: Shape, points: TripleSequence ¬ NIL]
RETURNS [TripleSequence];
Return the locations of shape.vertices, using points if non-NIL.
FindShape: PROC [shapes: ShapeSequence, shapeName: ROPE] RETURNS [Shape];
Finds the shape with the given name; return NIL if no such shape.
SetTextureCoords: PROC [shape: Shape, textures: PairSequence];
Assign the texture coordinates to the shape's vertices.
Bounds
BoundingBox: PROC [shape: Shape] RETURNS [Box3d];
Return the bounding box for the given shape.
BoundingSphere: PROC [shape: Shape] RETURNS [Sphere];
Return the bounding sphere for the given shape.
Sequences
CopyShapeSequence: PROC [shapes: ShapeSequence] RETURNS [ShapeSequence];
Return a copy of the input sequence of shapes. This does not allocate new shapes.
AddToShapeSequence: PROC [shapes: ShapeSequence, shape: Shape]
RETURNS [ShapeSequence];
Add shape to the sequence.
LengthenShapeSequence: PROC [shapes: ShapeSequence, amount: REAL ¬ 1.3]
RETURNS [ShapeSequence];
Return a copy of the input sequence whose maxLength is amount*input.maxLength.
CopyVertexSequence: PROC [vertices: VertexSequence] RETURNS [VertexSequence];
Return a copy of the input sequence of vertices. This does not allocate new vertices.
AddToVertexSequence: PROC [vertices: VertexSequence, vertex: Vertex]
RETURNS [VertexSequence];
Add vertex to the sequence.
LengthenVertexSequence: PROC [vertices: VertexSequence, amount: REAL ¬ 1.3]
RETURNS [VertexSequence];
Return a copy of the input sequence whose maxLength is amount*input.maxLength.
CopyFaceSequence: PROC [faces: FaceSequence] RETURNS [FaceSequence];
Return a copy of the input sequence of faces. This does not allocate new faces.
AddToFaceSequence: PROC [faces: FaceSequence, face: Face] RETURNS [FaceSequence];
Add face to the sequence.
LengthenFaceSequence: PROC [faces: FaceSequence, amount: REAL ¬ 1.3]
RETURNS [FaceSequence];
Return a copy of the input sequence whose maxLength is amount*input.maxLength.
END.