public static BRDF parseBRDF(InputStream inputFile, Scene scene) { BRDF result; KeywordEnum key = inputFile.expectKeywords(new List <KeywordEnum>() { KeywordEnum.Diffuse, KeywordEnum.Specular }); inputFile.expectSymbol("("); IPigment pigment = parsePigment(inputFile, scene); inputFile.expectSymbol(")"); if (key == KeywordEnum.Diffuse) { result = new DiffuseBRDF(pigment); } else if (key == KeywordEnum.Specular) { result = new SpecularBRDF(pigment); } else { throw new GrammarError(inputFile.location, "This line should be unreachable"); } return(result); }
public void ParseKeyword(KeywordEnum keyword) { if (((KeywordToken)_tokens.Current).keyword == keyword) { return; } throw new Exception("Syntax error with unexpect keyword :" + _tokens.Current.str); }
public static IPigment parsePigment(InputStream inputFile, Scene scene) { KeywordEnum key = inputFile.expectKeywords(new List <KeywordEnum>() { KeywordEnum.Uniform, KeywordEnum.Checkered, KeywordEnum.Image }); inputFile.expectSymbol("("); IPigment result; if (key == KeywordEnum.Uniform) { Color color = parseColor(inputFile, scene); result = new UniformPigment(color: color); } else if (key == KeywordEnum.Checkered) { Color col1 = parseColor(inputFile, scene); inputFile.expectSymbol(","); Color col2 = parseColor(inputFile, scene); inputFile.expectSymbol(","); // optional parameter? int steps = (int)inputFile.expectNumber(scene); result = new CheckeredPigment(col1, col2, steps); } else if (key == KeywordEnum.Image) { string fileName = inputFile.expectString(); using (Stream imageStream = File.OpenRead(fileName)) { HdrImage img = new HdrImage(imageStream); result = new ImagePigment(img); } } else { throw new GrammarError(inputFile.location, "This line should be unreachable"); } inputFile.expectSymbol(")"); return(result); }
private void appendExitOrNextStatement(SequentialStatement statement, KeywordEnum keyword, LoopStatement loop, Expression condition) { appendLabel(statement); writer.Append(keyword.ToString()); if (loop != null) { string label = loop.Label; if (label == null) { //FIXME: unify handling of null values throw new ArgumentNullException("Loop label is null"); } writer.Append(' ').Append(label); } if (condition != null) { writer.Append(' ').Append(KeywordEnum.WHEN.ToString()).Append(' '); output.writeExpression(condition); } writer.Append(';').NewLine(); }
public static Camera parseCamera(InputStream inputFile, Scene scene) { inputFile.expectSymbol("("); KeywordEnum key = inputFile.expectKeywords(new List <KeywordEnum>() { KeywordEnum.Perspective, KeywordEnum.Orthogonal }); inputFile.expectSymbol(","); Transformation tr = parseTransformation(inputFile, scene); inputFile.expectSymbol(","); float aspectRatio = inputFile.expectNumber(scene); inputFile.expectSymbol(","); float distance = inputFile.expectNumber(scene); inputFile.expectSymbol(")"); Camera result; if (key == KeywordEnum.Perspective) { result = new PerspectiveCamera(distance, aspectRatio, tr); } else if (key == KeywordEnum.Orthogonal) { result = new OrthogonalCamera(aspectRatio, tr); } else { throw new GrammarError(inputFile.location, "Unknown Camera type!"); } return(result); }
private void AssertIsKeyword(Token token, KeywordEnum keyword) { Assert.True(token is KeywordToken, "The token is not a key word"); Assert.True(((KeywordToken)token).keyword == keyword, $"Token {token} is not equal to keyword {keyword}"); }
private void OnKeyDown(Keys key, bool shift, bool ctrl, bool alt) { //Console.WriteLine("The Key: " + key); Debug.WriteLine($"Key: {key.ToString()} Shift: { shift.ToString()} \n"); string keyStr = GetKeyStr(key, shift); if (string.IsNullOrEmpty(keyStr)) { return; } if (key != Keys.Back) { _sentence += keyStr.ToString(); if (_sentence.Length > 256) { _sentence = _sentence.Substring(1); } } else { if (_sentence.Length > 0) { _sentence = _sentence.Substring(0, _sentence.Length - 1); } } Debug.WriteLine(_sentence); if (_keywordState == KeywordEnum.None) { if (_sentence.Contains(_keyword)) { _keywordState = KeywordEnum.Started; } } else if (_keywordState == KeywordEnum.Started) { if (!string.IsNullOrEmpty(_sentence)) { //Ended state can be reached in 2 seconds if new events wouldn't be raised ThreadPool.QueueUserWorkItem( x => { Thread.Sleep(TimeSpan.FromSeconds(_delayInSeconds)); lock (_locker) { if (DateTime.Now.Subtract(_lastPressedTime).TotalSeconds >= _delayInSeconds) { _keywordState = KeywordEnum.Ended; var word = _sentence.Substring(_sentence.LastIndexOf(_keyword) + _keyword.Length).Trim(); if (!string.IsNullOrEmpty(word)) { ActivatedKeyword(_keyword, word); _lastPressedTime = DateTime.Now; } } } }); } } else if (_keywordState == KeywordEnum.Ended) { _keywordState = KeywordEnum.None; if (DeactivatedKeyword != null) { DeactivatedKeyword(_keyword, null); } _sentence = string.Empty; } _lastPressedTime = DateTime.Now; }
public static CSGIntersection parseCSGIntersection(InputStream inputFile, Scene scene) { List <KeywordEnum> allowedKeys = new List <KeywordEnum>() { KeywordEnum.Plane, KeywordEnum.Sphere, KeywordEnum.Box, KeywordEnum.Cylinder, KeywordEnum.Cone, KeywordEnum.CSGUnion, KeywordEnum.CSGIntersection, KeywordEnum.CSGDifference }; Shape lShape, rShape; inputFile.expectSymbol("("); KeywordEnum lShapeKey = inputFile.expectKeywords(allowedKeys); switch (lShapeKey) { case KeywordEnum.Plane: lShape = parsePlane(inputFile, scene); break; case KeywordEnum.Sphere: lShape = parseSphere(inputFile, scene); break; case KeywordEnum.Box: lShape = parseBox(inputFile, scene); break; case KeywordEnum.Cylinder: lShape = parseCylinder(inputFile, scene); break; case KeywordEnum.Cone: lShape = parseCone(inputFile, scene); break; case KeywordEnum.CSGUnion: lShape = parseCSGUnion(inputFile, scene); break; case KeywordEnum.CSGDifference: lShape = parseCSGDifference(inputFile, scene); break; case KeywordEnum.CSGIntersection: lShape = parseCSGIntersection(inputFile, scene); break; default: throw new GrammarError(inputFile.location, $"Shape not found at {inputFile.location.ToString()}"); } inputFile.expectSymbol(","); KeywordEnum rShapeKey = inputFile.expectKeywords(allowedKeys); switch (rShapeKey) { case KeywordEnum.Plane: rShape = parsePlane(inputFile, scene); break; case KeywordEnum.Sphere: rShape = parseSphere(inputFile, scene); break; case KeywordEnum.Box: rShape = parseBox(inputFile, scene); break; case KeywordEnum.Cylinder: rShape = parseCylinder(inputFile, scene); break; case KeywordEnum.Cone: rShape = parseCone(inputFile, scene); break; case KeywordEnum.CSGUnion: rShape = parseCSGUnion(inputFile, scene); break; case KeywordEnum.CSGDifference: rShape = parseCSGDifference(inputFile, scene); break; case KeywordEnum.CSGIntersection: rShape = parseCSGIntersection(inputFile, scene); break; default: throw new GrammarError(inputFile.location, $"Shape not found at {inputFile.location.ToString()}"); } inputFile.expectSymbol(","); Transformation tr = parseTransformation(inputFile, scene); inputFile.expectSymbol(")"); return(new CSGIntersection(lShape, rShape, tr)); }
public static Transformation parseTransformation(InputStream inputFile, Scene scene) { Transformation result = new Transformation(1); List <KeywordEnum> keyList = new List <KeywordEnum>() { KeywordEnum.Identity, KeywordEnum.Translation, KeywordEnum.Scaling, KeywordEnum.RotationX, KeywordEnum.RotationY, KeywordEnum.RotationZ }; // now we look for transformations until there is no more * while (true) { KeywordEnum key = inputFile.expectKeywords(keyList); if (key == KeywordEnum.Identity) { break; } else if (key == KeywordEnum.Translation) { inputFile.expectSymbol("("); result = result * Transformation.Translation(parseVector(inputFile, scene)); inputFile.expectSymbol(")"); } else if (key == KeywordEnum.RotationX) { inputFile.expectSymbol("("); result = result * Transformation.RotationX(Utility.DegToRad((int)inputFile.expectNumber(scene))); inputFile.expectSymbol(")"); } else if (key == KeywordEnum.RotationY) { inputFile.expectSymbol("("); result = result * Transformation.RotationY(Utility.DegToRad((int)inputFile.expectNumber(scene))); inputFile.expectSymbol(")"); } else if (key == KeywordEnum.RotationZ) { inputFile.expectSymbol("("); result = result * Transformation.RotationZ(Utility.DegToRad((int)inputFile.expectNumber(scene))); inputFile.expectSymbol(")"); } else if (key == KeywordEnum.Scaling) { inputFile.expectSymbol("("); result = result * Transformation.Scaling(parseVector(inputFile, scene)); inputFile.expectSymbol(")"); } Token nextKey = inputFile.readToken(); if (!(nextKey is SymbolToken) || ((SymbolToken)nextKey).symbol != "*") { inputFile.unreadToken(nextKey); break; } } //while return(result); } //parseTranformation
/// <summary> /// Basic contructor for KeywordToken. /// </summary> /// <param name="sourceLoc"></param> /// <param name="key"></param> public KeywordToken(SourceLocation sourceLoc, KeywordEnum key) : base(sourceLoc) { this.keyword = key; }