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