001/* 002 003*/ 004 005package renderer.models; 006import renderer.scene.*; 007 008import java.util.Scanner; 009import java.io.File; 010import java.io.FileInputStream; 011import java.io.IOException; 012import java.io.FileNotFoundException; 013 014/** 015 Create a wirefram model from a GRS file. 016<p> 017 GRS files are a simple file format for describing two-dimensional 018 drawings made up of "polylines". The format was created for the textbook 019 "Computer Graphics Using OpenGL", 3rd Ed, by Francis S Hill 020 and Stephen M Kelley (see pages 61-63). 021<p> 022 See <a href="https://en.wikipedia.org/wiki/Polyline" target="_top"> 023 https://en.wikipedia.org/wiki/Polyline</a> 024<p> 025 The structure of a GRS file is: 026 <ol> 027 <li>A number of comment lines followed by a line 028 starting with an asterisk, {@code '*'}. 029 <li>A line containing the "extent" (bounding box) 030 of the drawing given as four doubles in model 031 coordinates (left, top, right, bottom). 032 <li>The number of line-strips (i.e., polylines) 033 in the drawing. 034 <li>The list of line-strips. Each line-strip starts 035 with the number of vertices in the line-strip, 036 followed by the (x, y) model coordinates for 037 each vertex. 038 </ol> 039*/ 040public class GRSModel extends Model 041{ 042 // the figure's extents (bounding box) 043 public double left = 0.0; 044 public double top = 0.0; 045 public double right = 0.0; 046 public double bottom = 0.0; 047 public int numLineStrips = 0; 048 049 /** 050 Create a wireframe model from the contents of an GRS file. 051 052 @param grsFile {@link File} object for the GRS data file 053 */ 054 public GRSModel(File grsFile) 055 { 056 super(); 057 058 // Open the GRS file. 059 String grsName = null; 060 FileInputStream fis = null; 061 try 062 { 063 grsName = grsFile.getCanonicalPath(); 064 fis = new FileInputStream( grsFile ); 065 } 066 catch (FileNotFoundException e) 067 { 068 e.printStackTrace(System.err); 069 System.err.printf("ERROR! Could not find GRS file: %s\n", grsName); 070 System.exit(-1); 071 } 072 catch (IOException e) 073 { 074 e.printStackTrace(System.err); 075 System.err.printf("ERROR! Could not open GRS file: %s\n", grsName); 076 System.exit(-1); 077 } 078 079 Scanner scanner = new Scanner(fis); 080 081 // Get the geometry from the GRS file. 082 try 083 { 084 // skip over the comment lines 085 String line = scanner.nextLine(); 086 while ( ! line.startsWith("*") ) 087 { 088 //System.err.println(line); 089 line = scanner.nextLine(); 090 } 091 092 // read the figure extents 093 this.left = scanner.nextDouble(); 094 this.top = scanner.nextDouble(); 095 this.right = scanner.nextDouble(); 096 this.bottom = scanner.nextDouble(); 097 098 // read the number of line-strips 099 this.numLineStrips = scanner.nextInt(); 100 101 // read each line-strip 102 for(int j = 0; j < this.numLineStrips; j++) 103 { 104 // read the number of vertices in this line-strip 105 int numVertices = scanner.nextInt(); 106 107 // put this line-strip in the Model object 108 double x = scanner.nextDouble(); // read the first vertex in the line-strip 109 double y = scanner.nextDouble(); 110 Vertex v0 = new Vertex(x, y, 0); 111 for (int i = 1; i < numVertices; i++) 112 { 113 // read the next model coordinate pair 114 x = scanner.nextDouble(); 115 y = scanner.nextDouble(); 116 Vertex v1 = new Vertex(x, y, 0); 117 // create a new LineSegment in the Model 118 this.addLineSegment(v0, v1); 119 v0 = v1; 120 } 121 } 122 fis.close(); 123 } 124 catch (IOException e) 125 { 126 e.printStackTrace(System.err); 127 System.err.printf("ERROR! Could not read GRS file: %s\n", grsName); 128 System.exit(-1); 129 } 130 } 131}//GRSModel