File: SVViewerToolsImpl.mesa
Created December 18, 1982 1:40 am
Copyright © 1984 by Xerox Corporation. All rights reserved.
Last edited by Eric Bier on July 8, 1987 1:59:41 pm PDT
Contents: Basic functions for retrieving values from and writing values to text viewers.
DIRECTORY
Feedback, Imager, ImagerColor, IO, Rope, Shading, SV3d, SVViewerTools, TFI3d, ViewerClasses, ViewerTools;
SVViewerToolsImpl:
CEDAR PROGRAM
IMPORTS Feedback, ImagerColor, IO, Rope, Shading, TFI3d, ViewerTools
EXPORTS SVViewerTools =
BEGIN
Color: TYPE = Imager.Color;
Point3d: TYPE = SV3d.Point3d;
Viewer: TYPE = ViewerClasses.Viewer;
GetReal:
PUBLIC
PROC [textViewer: Viewer, default:
REAL]
RETURNS [r:
REAL] = {
rRope: Rope.ROPE ← ViewerTools.GetContents[textViewer];
IF Rope.Size[rRope] = 0
THEN r ← default
ELSE {
IF Rope.Find[rRope, ".", 0, FALSE] = -1 THEN rRope ← Rope.Concat[rRope, ".0"];
r ← IO.GetReal[IO.RIS[rRope]];
};
};
GetBool:
PUBLIC
PROC [textViewer: Viewer]
RETURNS [b:
BOOL] = {
bRope: Rope.ROPE ← ViewerTools.GetContents[textViewer];
b ← IF Rope.Equal[bRope, "TRUE"] THEN TRUE ELSE FALSE;
};
GetColor:
PUBLIC
PROC [textViewer: Viewer]
RETURNS [color: Color, success:
BOOL] = {
r, g, b: REAL;
success ← TRUE;
[r, g, b] ← GetThreeReals[textViewer];
IF r > 1 OR r < 0 THEN {Feedback.AppendRaw[$Solidviews, "r value must be between 0 and 1", oneLiner]; Feedback.BlinkRaw[$Solidviews]; success ← FALSE};
IF g > 1 OR g < 0 THEN {Feedback.AppendRaw[$Solidviews, "g value must be between 0 and 1", oneLiner]; Feedback.BlinkRaw[$Solidviews]; success ← FALSE};
IF b > 1 OR b < 0 THEN {Feedback.AppendRaw[$Solidviews, "b value must be between 0 and 1", oneLiner]; Feedback.BlinkRaw[$Solidviews]; success ← FALSE};
IF success THEN color ← ImagerColor.ColorFromRGB [[r, g, b]];
};
GetThreeReals:
PUBLIC
PROC [textViewer: Viewer]
RETURNS [x, y, z:
REAL] = {
wholeRope: Rope.ROPE ← ViewerTools.GetContents[textViewer];
wholeStream: IO.STREAM ← IO.RIS[wholeRope];
threeNumsThere: BOOL ← TRUE;
TFI3d.ReadBlank[wholeStream];
x ← TFI3d.ReadReal[wholeStream];
TFI3d.ReadBlankAndRope[wholeStream, ","
!TFI3d.RopeNotOnTop => {threeNumsThere ← FALSE; CONTINUE}];
IF NOT threeNumsThere THEN y ← x
ELSE {
TFI3d.ReadBlank[wholeStream]; y ← TFI3d.ReadReal[wholeStream]};
TFI3d.ReadBlankAndRope[wholeStream, ","
!TFI3d.RopeNotOnTop => {threeNumsThere ← FALSE; CONTINUE}];
IF NOT threeNumsThere THEN z ← y
ELSE {
TFI3d.ReadBlank[wholeStream]; z ← TFI3d.ReadReal[wholeStream]};
};
GetNat:
PUBLIC
PROC [textViewer: Viewer]
RETURNS [n:
NAT] = {
nRope: Rope.ROPE ← ViewerTools.GetContents[textViewer];
IF Rope.Size[nRope] = 0
THEN n ← 0
ELSE {
n ← IO.GetInt[IO.RIS[nRope]];
};
};
GetPoint:
PUBLIC
PROC [textViewer: Viewer]
RETURNS [point: Point3d, success:
BOOL] = {
Assumes a rope of the form "[<real1>,<real2>]".
pointRope: Rope.ROPE ← ViewerTools.GetContents[textViewer];
f: IO.STREAM ← IO.RIS[pointRope];
ReadRope[f, "["];
point[1] ← ReadBlankAndReal[f];
ReadRope[f, ","];
point[2] ← ReadBlankAndReal[f];
ReadRope[f, ","];
point[3] ← ReadBlankAndReal[f];
ReadRope[f, "]"];
success ← TRUE;
};
SetReal:
PUBLIC
PROC [viewer: Viewer, real:
REAL] = {
s: IO.STREAM;
realRope: Rope.ROPE;
s ← IO.ROS[];
s.PutF["%g", [real[real]]];
realRope ← IO.RopeFromROS[s];
ViewerTools.SetContents[viewer, realRope];
};
SetBool:
PUBLIC
PROC [viewer: Viewer, bool:
BOOL] = {
s: IO.STREAM;
boolRope: Rope.ROPE;
s ← IO.ROS[];
s.PutF["%g", [boolean[bool]]];
boolRope ← IO.RopeFromROS[s];
ViewerTools.SetContents[viewer, boolRope];
};
SetColor:
PUBLIC
PROC [viewer: Viewer, color: Color] = {
r,g,b: REAL;
[r,g,b] ← Shading.ExtractRGB[color];
SetThreeReals[viewer, r, g, b];
};
SetThreeReals:
PUBLIC
PROC [viewer: Viewer, a, b, c:
REAL] = {
s: IO.STREAM;
realsRope: Rope.ROPE;
s ← IO.ROS[];
s.PutF["%g, %g, %g", [real[a]], [real[b]], [real[c]]];
realsRope ← IO.RopeFromROS[s];
ViewerTools.SetContents[viewer, realsRope];
};
SetPoint:
PUBLIC
PROC [viewer: Viewer, point: Point3d] = {
pointRope: Rope.ROPE;
pointRope ← IO.PutFR["[%g,%g,%g]", [real[point[1]]], [real[point[2]]], [real[point[3]]]];
ViewerTools.SetContents[viewer, pointRope];
};
ReadRope:
PUBLIC
PROC [f:
IO.
STREAM, rope: Rope.
ROPE] = {
Removes the given rope from the top of the stream. Used to remove formatting words and phrases from 3d files. We are not interested in these strings but only in the data in between them.
Signals RopeNotOnTop if some other rope is on top.
c: CHAR;
endofstream: BOOL ← FALSE;
FOR i:
INT
IN[1..Rope.Length[rope]]
DO
c ←
IO.GetChar[f
! IO.EndOfStream => {endofstream ← TRUE; CONTINUE}];
IF endofstream
THEN
SIGNAL RopeNotOnTop [IO.GetIndex[f], NIL, rope];
IF
NOT c = Rope.Fetch[rope,i-1]
THEN
SIGNAL RopeNotOnTop [IO.GetIndex[f], Rope.FromChar[c], rope];
ENDLOOP;
};
RopeNotOnTop: PUBLIC SIGNAL [position: NAT, wasThere: Rope.ROPE, notThere: Rope.ROPE] = CODE;
ReadBlankAndReal:
PROC [f:
IO.
STREAM]
RETURNS [r:
REAL] = {
A convenience function. Equivalent to ReadBlank[f]; r ← ReadReal[f];
ReadBlank[f];
r ← ReadReal[f];
};
ReadBlank:
PROC [f:
IO.
STREAM] = {
Reads, <SPACE>'s, <CR>'s, and <TAB>'s until something else is encountered. Doesn't mind if no white space characters are found. Treats comments as white space.
[] ← IO.SkipWhitespace[f, TRUE];
};
ReadReal:
PROC [f:
IO.
STREAM]
RETURNS [r:
REAL] = {
Reads digits up to the next ], <CR>, <SPACE> or <COMMA>. Leaves these terminators on the stream.
realRope: Rope.ROPE;
end: BOOL ← FALSE;
[realRope,
----] ←
IO.GetTokenRope[f, RealBreakProc
!IO.EndOfStream => {end ← TRUE; CONTINUE}];
IF end THEN {r ← 0.0; RETURN};
IF Rope.Find[realRope, ".", 0, FALSE] = -1 THEN realRope ← Rope.Concat[realRope, ".0"];
r ← IO.GetReal[IO.RIS[realRope]];
};
RealBreakProc:
PROC [char:
CHAR]
RETURNS [
IO.CharClass] = {
SELECT char
FROM
'], ', => RETURN [break];
IO.CR =>RETURN [break];
IO.SP => RETURN [break];
ENDCASE => RETURN [other];
};
END.