TubeIOImpl.mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
Bloomenthal, February 24, 1987 6:14:43 pm PST
DIRECTORY Controls, FS, Imager, ImagerFont, ImagerInterpress, IO, Matrix3d, Rope, ThreeDIO, TubeDefs, TubeDisplay, TubeGeometry, TubeIO, TubeMisc;
TubeIOImpl: CEDAR PROGRAM
IMPORTS Controls, FS, Imager, ImagerFont, ImagerInterpress, IO, Matrix3d, Rope, ThreeDIO, TubeDisplay, TubeGeometry, TubeMisc
EXPORTS TubeIO
~ BEGIN
OPEN TubeDefs;
Line: TYPE ~ ThreeDIO.Line;
Support Procedures
WriteTriple: PROC [stream: STREAM, t: Triple] ~ {
WriteJustified[stream, t.x];
WriteJustified[stream, t.y];
WriteJustified[stream, t.z];
IO.PutF[stream, "\t"];
};
WriteJustified: PROC [stream: STREAM, r: REAL] ~ {
IF r >= 0.0 THEN IO.PutF[stream, " "];
IO.PutF[stream, "%6.5f ", IO.real[r]]
};
Writing Tube in Points-Polygons Format
QueryAndWritePointsPoly: PUBLIC PROC [tube: Tube, o: OuterData, m: Matrix ← NIL] ~ {
nPoints, nPolys: INT;
fileName: ROPE ← Controls.TypeScriptReadFileName[o];
IF fileName = NIL THEN RETURN;
[nPoints, nPolys] ← WritePointsPolys[tube, fileName, m];
Controls.TypeScriptWrite[o, IO.PutFR["%g points and %g polys written\n",
IO.int[nPoints], IO.int[nPolys]]];
};
WritePointsPolys: PUBLIC PROC [tube: Tube, fileName: ROPE, m: Matrix ← NIL]
RETURNS [nPoints, nPolys: INT] ~ {
Point: PointProc ~ {
IO.PutF[out, "%g\t", IO.int[id]];
WriteTriple[out, position];
WriteTriple[out, normal];
IO.PutF[out, "%6.5f %6.5f 0.0\n", IO.real[u], IO.real[v]];
};
Poly: PolyProc ~ {
IO.PutF[out,"%g\t%4g %4g %4g\n", IO.int[id], IO.int[p0], IO.int[p1], IO.int[p2]];
};
out: STREAMFS.StreamOpen[fileName, $create];
IO.PutF[out,
"Vertices~ index: integer xyzCoords: triple normalVec: triple textureCoords: triple\n\n"];
nPoints ← TubeMisc.Points[tube, Point, m];
IO.PutF[out, "\nPolygons~ index: integer vertices: nats\n\n"];
nPolys ← TubeMisc.Polys[tube, Poly];
IO.Close[out];
};
Writing Tube in Nelson Max Format
QueryAndWriteMaxPointsPoly: PUBLIC PROC [tube: Tube, o: OuterData, m: Matrix ← NIL]
~ {
nPoints, nPolys: INT;
fileName: ROPE ← Controls.TypeScriptReadFileName[o];
IF fileName = NIL THEN RETURN;
[nPoints, nPolys] ← WriteMaxPointsPolys[tube, fileName, m];
Controls.TypeScriptWrite[o, IO.PutFR["%g points and %g polys written\n",
IO.int[nPoints], IO.int[nPolys]]];
};
WriteMaxPointsPolys: PUBLIC PROC [tube: Tube, fileName: ROPE, m: Matrix ← NIL]
RETURNS [nPoints, nPolys: INT] ~ {
MaxPoint: PointProc ~ {
IO.PutF[out, "%g,", IO.int[id]];
IO.PutF[out, "%6.5f,%6.5f,%6.5f,", IO.real[position.x], IO.real[position.y], IO.real[position.z]];
IO.PutF[out, "%6.5f,%6.5f,%6.5f,", IO.real[normal.x], IO.real[normal.y], IO.real[normal.z]];
IO.PutF[out, "%6.5f,%6.5f,\n", IO.real[u], IO.real[v]];
};
MaxPoly: PolyProc ~ {
IO.PutF[out, "%g,%g,%g,%g,\n", IO.int[id], IO.int[p0], IO.int[p1], IO.int[p2]];
};
out: STREAMFS.StreamOpen[fileName, $create];
nPoints ← TubeMisc.Points[tube, MaxPoint, m];
IO.PutF[out, "-1\n"];
nPolys ← TubeMisc.Polys[tube, MaxPoly];
IO.PutF[out, "-1\n"];
IO.Close[out];
};
Writing Tube in Interpress Format
QueryAndWriteIP: PUBLIC PROC [tube: Tube, o: OuterData, details: Details, m: Matrix] ~ {
fileName: ROPE ← Controls.TypeScriptReadFileName[o];
IF fileName # NIL THEN {
ref: ImagerInterpress.Ref ← ImagerInterpress.Create[fileName];
WriteIP[ref, tube, details, m];
ImagerInterpress.Close[ref];
};
};
WriteIP: PUBLIC PROC [ref: ImagerInterpress.Ref, tube: Tube, details: Details, m: Matrix] ~ {
font: ImagerFont.Font ← ImagerFont.Scale[ImagerFont.Find["xerox/pressfonts/helvetica-mrr"], 12.0];
ContextProc: PROC [context: Context] ~ {
metersPerPoint: REAL ~ .0254/72.0;
Imager.ScaleT[context, metersPerPoint];
Imager.SetStrokeWidth[context, 1.0];
Imager.SetStrokeEnd[context, round];
Imager.SetFont[context, font];
Imager.TranslateT[context, [0.0, 0.5*11.0*72.0]];
TubeDisplay.DrawTube[tube, context, details, m];
};
ImagerInterpress.DoPage[ref, ContextProc];
};
Writing and Reading Tube in Tube Format
QueryAndWriteTube: PUBLIC PROC [tube: Tube, o: OuterData, m: Matrix ← NIL] ~ {
fileName: ROPE ← Controls.TypeScriptReadFileName[o];
IF fileName # NIL THEN {
stream: STREAMFS.StreamOpen[fileName, $create];
IO.PutF[stream, "\n-- %g, format for each line:\n", IO.rope[fileName]];
IO.PutF[stream, "-- Tube: p0, p1, v0, v1, r0, r1 tw0, tw1 <next|no>, nBranches\n\n"];
WriteTube[stream, tube, m];
IO.Close[stream];
};
};
WriteTube: PUBLIC PROC [stream: STREAM, tube: Tube, m: Matrix ← NIL] ~ {
Write: TubeProc ~ {
IO.PutF[stream, "Tube: "];
WriteTriple[stream, IF m = NIL THEN tube.p0 ELSE Matrix3d.Transform[tube.p0, m]];
WriteTriple[stream, IF m = NIL THEN tube.p1 ELSE Matrix3d.Transform[tube.p1, m]];
WriteTriple[stream, IF m = NIL THEN tube.v0 ELSE Matrix3d.TransformVec[tube.v0, m]];
WriteTriple[stream, IF m = NIL THEN tube.v1 ELSE Matrix3d.TransformVec[tube.v1, m]];
IO.PutF[stream, "%5.4f %5.4f ", IO.real[tube.r0], IO.real[tube.r1]];
IO.PutF[stream, "%5.4f %5.4f ", IO.real[tube.tw0], IO.real[tube.tw1]];
IO.PutF[stream, IF tube.next # NIL THEN "next " ELSE "no "];
IO.PutF[stream, "%g", IO.int[TubeMisc.NBranches[tube]]];
IO.PutF[stream, "\n"];
};
TubeMisc.ApplyToTube[tube, Write];
};
QueryAndReadTube: PUBLIC PROC [o: OuterData] RETURNS [Tube] ~ {
tube: Tube ← NIL;
name: ROPE ← Controls.TypeScriptReadFileName[o];
IF name # NIL THEN {
stream: STREAMFS.StreamOpen[name ! FS.Error => GOTO noOpen];
tube ← ReadTube[stream];
IO.Close[stream];
EXITS
noOpen => Controls.TypeScriptWrite[o, IO.PutFR["Couldn't open %g\n", IO.rope[name]]];
};
RETURN[tube];
};
ReadTube: PUBLIC PROC [stream: STREAM] RETURNS [Tube] ~ {
GetTubeLine: PROC [stream: STREAM] RETURNS [line: Line] ~ {
DO
line ← ThreeDIO.GetLine[stream ! IO.EndOfStream => GOTO eof];
IF Rope.Equal[ThreeDIO.GetWord[line], "tube:", FALSE] THEN RETURN;
REPEAT
eof => RETURN[NIL];
ENDLOOP;
};
InnerReadTube: PROC [prev: Tube] RETURNS [tube: Tube] ~ {
line: Line ← GetTubeLine[stream];
IF line = NIL THEN RETURN[NIL];
tube ← NEW[TubeRep];
tube.prev ← prev;
tube.p0 ← ThreeDIO.GetTriple[line];
tube.p1 ← ThreeDIO.GetTriple[line];
tube.v0 ← ThreeDIO.GetTriple[line];
tube.v1 ← ThreeDIO.GetTriple[line];
tube.r0 ← ThreeDIO.GetReal[line];
tube.r1 ← ThreeDIO.GetReal[line];
tube.tw0 ← ThreeDIO.GetReal[line];
tube.tw1 ← ThreeDIO.GetReal[line];
tube.next ← IF Rope.Equal[ThreeDIO.GetWord[line], "next", FALSE]
THEN InnerReadTube[tube]
ELSE NIL;
tube.branches ← NEW[TubeSequenceRep[ThreeDIO.GetInteger[line]]];
tube.branches.length ← tube.branches.maxLength;
TubeGeometry.SetCoeffs[tube];
IF tube.branches # NIL THEN FOR n: NAT IN [0..tube.branches.length) DO
tube.branches[n] ← InnerReadTube[tube];
ENDLOOP;
};
IF stream = NIL
THEN RETURN[NIL]
ELSE RETURN[InnerReadTube[NIL]];
};
WriteTubeInfo: PUBLIC PROC [tube: Tube, o: OuterData] ~ {
nPoints, nPolys, minCres, maxCres: INTEGER;
[nPoints, nPolys, minCres, maxCres] ← TubeMisc.Info[tube];
Controls.TypeScriptWrite[o, IO.PutFR["%g points, %g polygons, cres: %g-%g\n",
IO.int[nPoints], IO.int[nPolys], IO.int[minCres], IO.int[maxCres]]];
};
END.