~seh999/jcog/proto3

« back to all changes in this revision

Viewing changes to spacetime/src/opencog/spacetime/io/in/ReaderMSMS.java

  • Committer: SeH
  • Date: 2009-09-19 22:59:48 UTC
  • Revision ID: seh999@gmail.com-20090919225948-q3ab80xa57i74mm6
start of major jReality refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *
 
3
 * This file is part of jReality. jReality is open source software, made
 
4
 * available under a BSD license:
 
5
 *
 
6
 * Copyright (c) 2003-2006, jReality Group: Charles Gunn, Tim Hoffmann, Markus
 
7
 * Schmies, Steffen Weissmann.
 
8
 *
 
9
 * All rights reserved.
 
10
 *
 
11
 * Redistribution and use in source and binary forms, with or without
 
12
 * modification, are permitted provided that the following conditions are met:
 
13
 *
 
14
 * - Redistributions of source code must retain the above copyright notice, this
 
15
 *   list of conditions and the following disclaimer.
 
16
 *
 
17
 * - Redistributions in binary form must reproduce the above copyright notice,
 
18
 *   this list of conditions and the following disclaimer in the documentation
 
19
 *   and/or other materials provided with the distribution.
 
20
 *
 
21
 * - Neither the name of jReality nor the names of its contributors nor the
 
22
 *   names of their associated organizations may be used to endorse or promote
 
23
 *   products derived from this software without specific prior written
 
24
 *   permission.
 
25
 *
 
26
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
27
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
28
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
29
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
30
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
31
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
32
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
33
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
34
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
35
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
36
 * POSSIBILITY OF SUCH DAMAGE.
 
37
 *
 
38
 */
 
39
 
 
40
 
 
41
package opencog.spacetime.io.in;
 
42
 
 
43
import java.awt.Color;
 
44
import java.io.BufferedReader;
 
45
import java.io.IOException;
 
46
import java.io.InputStream;
 
47
import java.io.InputStreamReader;
 
48
import java.io.Reader;
 
49
import java.io.StreamTokenizer;
 
50
import java.util.logging.Level;
 
51
 
 
52
import opencog.math.Rn;
 
53
import opencog.spacetime.geometry.IndexedFaceSetFactory;
 
54
import opencog.spacetime.geometry.Primitives;
 
55
import opencog.spacetime.geometry.QuadMeshFactory;
 
56
import opencog.spacetime.space.IndexedFaceSet;
 
57
import opencog.spacetime.space.IndexedLineSet;
 
58
import opencog.spacetime.space.SpaceComponent;
 
59
import opencog.spacetime.space.Transformation;
 
60
import opencog.spacetime.space.data.Attribute;
 
61
import opencog.spacetime.space.data.StorageModel;
 
62
import opencog.spacetime.util.Input;
 
63
import opencog.spacetime.util.LoggingSystem;
 
64
import opencog.spacetime.util.SceneGraphUtility;
 
65
 
 
66
 
 
67
 
 
68
/**
 
69
 *
 
70
 * A rudimentary reader for OOGL files (Geomview format). 
 
71
 * <p>
 
72
 * Needs to be converted to a real parser, e.g., by
 
73
 * using the antlr parsing package.
 
74
 * <p>
 
75
 * Current limitations: handles only following types: "OFF", "MESH", "VECT",  "LIST", "inst", "tlist".
 
76
 * @author Charles Gunn
 
77
 *
 
78
 */
 
79
public class ReaderMSMS extends AbstractReader {
 
80
 
 
81
  public void setInput(Input input) throws IOException {
 
82
    super.setInput(input);
 
83
    root = load(input.getInputStream());
 
84
  }
 
85
  
 
86
  /**
 
87
   * @param inputStream
 
88
   * @return
 
89
   */
 
90
  SpaceComponent load(InputStream inputStream) {
 
91
      Reader r = new BufferedReader(new InputStreamReader(inputStream));
 
92
       
 
93
      StreamTokenizer st = new StreamTokenizer(r);
 
94
      
 
95
      st.resetSyntax();
 
96
      st.eolIsSignificant(false);
 
97
      st.wordChars('0', '9');
 
98
      st.wordChars('A', 'Z');
 
99
      st.wordChars('a' , 'z');
 
100
      st.wordChars('.','.');
 
101
      st.wordChars('-','-');
 
102
      st.wordChars('+','+');
 
103
      st.wordChars('\u00A0', '\u00FF' );
 
104
      st.ordinaryChar('=');
 
105
      st.ordinaryChar('{');
 
106
      st.ordinaryChar('}');
 
107
      st.whitespaceChars('\u0000',  '\u0020');
 
108
      st.commentChar('#');
 
109
      //st.parseNumbers();
 
110
       
 
111
       SpaceComponent current= null;
 
112
      LoggingSystem.getLogger(ReaderMSMS.class).log(Level.FINER,"start.");
 
113
      try {
 
114
          current =SceneGraphUtility.createFullSceneGraphComponent("MSMS-node");
 
115
            //LoggingSystem.getLogger().log(Level.FINER,"found object!");
 
116
          int numV, numS;
 
117
          double density, probeRadius;
 
118
          st.nextToken();
 
119
          numV = Integer.parseInt(st.sval);
 
120
          st.nextToken();
 
121
          numS = Integer.parseInt(st.sval);
 
122
          st.nextToken();
 
123
          density = Double.parseDouble(st.sval);
 
124
          st.nextToken();
 
125
          probeRadius = Double.parseDouble(st.sval);
 
126
          double[][] verts = new double[numV][3],
 
127
                        normals = new double[numV][3];
 
128
          int[] nearestSphere = new int[numV], vertexType = new int[numV];
 
129
          for (int i=0; i<numV; ++i)  {
 
130
              for (int j = 0; j<3; ++j)  {
 
131
                  st.nextToken();
 
132
                  //LoggingSystem.getLogger().log(Level.FINER,"Token is "+st.sval);
 
133
                  verts[i][j] = Double.parseDouble(st.sval);                        
 
134
              }
 
135
              for (int j = 0; j<3; ++j)  {
 
136
                  st.nextToken();
 
137
                  //LoggingSystem.getLogger().log(Level.FINER,"Token is "+st.sval);
 
138
                  normals[i][j] = Double.parseDouble(st.sval);                        
 
139
              }
 
140
              st.nextToken();
 
141
              st.nextToken();
 
142
              nearestSphere[i] = Integer.parseInt(st.sval);
 
143
              st.nextToken();
 
144
              vertexType[i] = Integer.parseInt(st.sval);
 
145
          }
 
146
          // get the faces now
 
147
          int numF;
 
148
          st.nextToken();
 
149
          numF = Integer.parseInt(st.sval);
 
150
          // skip over redundant fields
 
151
          st.nextToken();
 
152
          st.nextToken();
 
153
          st.nextToken();
 
154
          int[][] indices = new int[numF][3];
 
155
          int[] faceType = new int[numF];
 
156
          Color[] facec = new Color[numF];
 
157
                   Color[] colors = {
 
158
                                new Color(255,55,0), new Color(100,255,0), new Color(50,50,255),
 
159
                                new Color(255,200,0), new Color(255,0,255), new Color(0,255,255)};
 
160
          for (int i = 0; i<numF; ++i)  {
 
161
                  for (int j = 0; j<3; ++j)     {
 
162
                          st.nextToken();
 
163
                          indices[i][j] = Integer.parseInt(st.sval) - 1;
 
164
                  }  
 
165
                  st.nextToken();
 
166
                  faceType[i] = Integer.parseInt(st.sval);
 
167
                  facec[i] = colors[faceType[i] % colors.length];
 
168
                  st.nextToken();
 
169
         }
 
170
          
 
171
          LoggingSystem.getLogger(ReaderMSMS.class).log(Level.INFO,"Read "+numV+" vertices and "+numF+" faces");
 
172
                IndexedFaceSetFactory ifsf = new IndexedFaceSetFactory();
 
173
                ifsf.setVertexCount(verts.length);
 
174
                ifsf.setVertexCoordinates(verts);
 
175
                ifsf.setVertexNormals(normals);
 
176
                ifsf.setFaceCount(indices.length);
 
177
                ifsf.setFaceIndices(indices);
 
178
//              ifsf.setFaceColors(facec);
 
179
                ifsf.setGenerateEdgesFromFaces(true);
 
180
                ifsf.setGenerateFaceNormals(true);
 
181
                ifsf.update();
 
182
          IndexedFaceSet ifs = ifsf.getIndexedFaceSet();
 
183
          ifs.setName("OFF Geometry");
 
184
          current.setGeometry(ifs);
 
185
        } catch (IOException e) {
 
186
          e.printStackTrace();
 
187
      }
 
188
      return current;
 
189
  }
 
190
 
 
191
 
 
192
}