Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/pqt/autorib/instr/InstrSixSnaps.java


1   //AutoRIB
2   // Copyright © 1998 - 2002, P W Quint
3   //
4   // Contact: autorib00@aol.com
5   //
6   // This library is free software; you can redistribute it and/or
7   // modify it under the terms of the GNU General Public
8   // License as published by the Free Software Foundation; either
9   // version 2 of the License, or (at your option) any later version.
10  //
11  // This library is distributed in the hope that it will be useful,
12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  // General Public License for more details.
15  //
16  // You should have received a copy of the GNU General Public
17  // License along with this library; if not, write to the Free Software
18  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  
20  package org.pqt.autorib.instr;
21  
22  import java.awt.geom.Rectangle2D;
23  import java.io.*;
24  import java.util.*;
25  
26  import org.pqt.autorib.tokenizer.*;
27  import org.pqt.autorib.globals.*;
28  import org.pqt.autorib.util.*;
29  import org.pqt.autorib.rib.*;
30  
31  /**
32   * Handles the sixsnaps instruction which generates six views of a group of objects
33   * along the cardinal axes
34   */
35  
36  public class InstrSixSnaps extends InstrRequest {
37      
38      /** Lh to Rh transform */
39      protected static Matrix changeHandedness = new Matrix(-1f, -1f, -1f);
40      
41      /** stores the names of objects to omit or include */
42      Vector omitVector = new Vector(5,5);
43      
44      private static final Point offsets[] = { new Point(0f, 1f,  0f), new Point(0f, -1f, 0f),
45      new Point(0f, 0f, 1f), new Point(0f, 0f, -1f), new Point(1f, 0f, 0f),
46      new Point(-1f, 0f, 0f) };
47      
48      private static final Point up[] = { new Point(0f, 0f,  -1f), new Point(0f, 0f, -1f),
49      new Point(0f, 1f, 0f), new Point(0f, 1f, 0f), new Point(0f, 1f, 0f),
50      new Point(0f, 1f, 0f) };
51      
52      private String names[] = { "top", "bottom", "front", "back", "left", "right" };
53      
54      private InstrState state = null;
55      
56      private float scale = 1f;
57      
58      private int mapSize = 0;
59      
60      private String rgba = null;
61      
62      private int priority;
63      
64      public InstrSixSnaps(InstrWReader in) throws IOException, FormatException {
65          read(in);
66          state = (InstrState) in.state.clone();
67          state.omitNames = omitVector;
68      }
69      
70      
71      /** read a call and store it
72       * @param in an InstrWReader object linked to the input file*/
73      public void read(InstrWReader in) throws IOException,
74      FormatException {
75          readRequestName(in);
76          rgba = in.tokenizer.getString();
77          scale = in.tokenizer.getNumber();
78          mapSize = (int) in.tokenizer.getNumber();
79          //we start by omitting everything
80          omitVector.add(new OmitRec("*", true));
81          int type = in.tokenizer.getToken();
82          while ((type == Token.STRING) || (type == Token.ARRAY)) {
83              if (type == Token.STRING)
84                  omitVector.add(new OmitRec(in.tokenizer.token.stringVal, false));
85              else {
86                  OmitRec.addOmitsFromVector(in.tokenizer.token.arrayVal, omitVector,
87                  true);
88              }
89              type = in.tokenizer.getToken();
90          }
91          in.tokenizer.pushBack();
92          priority = in.tokenizer.lineno();
93      }
94      
95      public void write(Writer out) throws IOException {
96          writeRequest(out);
97          Enumeration i = omitVector.elements();
98          while (i.hasMoreElements())
99              out.write(((OmitRec)i.nextElement()).toString());
100     }
101     
102     public void process(RIBWReader rib)
103     throws FormatException, IOException {
104         Bounds3D bounds = rib.calculateWorldBounds(omitVector);
105         float radius = bounds.getRadius() * 1.5f;
106         Point to = bounds.getCentre();
107         Point from;
108         Matrix camera;
109         Bounds3D cameraBounds;
110         String baseName = rib.nameOut == null ? rib.nameIn : rib.nameOut;
111         int t = baseName.lastIndexOf('.');
112         if (t > 0)
113             baseName = baseName.substring(0,t);
114         if (!baseName.endsWith("_"))
115             baseName = baseName + '_';
116         String mapName;
117         float left, right, bottom, top, ratio;
118         int xres, yres;
119         for (int i = 0; i < offsets.length; i++) {
120             from = Point.add(to,Point.mult(offsets[i], radius));
121             camera = MakeMap.placeCamera(from, Point.subtract(to,from));
122             //camera.scale(1f, 1f, -1f);
123             camera.multiply(-1f);
124             cameraBounds = bounds.transform(Matrix.multiply(changeHandedness,camera));
125             //scale the bounds and then get them as a rectangle
126             Rectangle2D.Float rect = cameraBounds.scale(scale).getAsRectangle2D();
127             left = rect.x;
128             right = rect.x + rect.width;
129             bottom = rect.y;
130             top = rect.y + rect.height;
131             if (rect.height != 0)
132                 ratio = rect.width / rect.height;
133             else
134                 ratio = 1f;
135             if (ratio > 1) {
136                 xres = mapSize;
137                 yres = (int) (mapSize / ratio);
138             } else {
139                 yres = mapSize;
140                 xres = (int) (mapSize * ratio);
141             }
142             mapName = baseName + names[i];
143             rib.mapList.add(new InstrMapRec(rib, from, to,
144             up[i], rgba, xres, yres, left, right, bottom, top, state, mapName,
145             priority));
146         }
147     }
148 }
149 
150 
151 
152 
153 
154