~seh999/jcog/proto3

« back to all changes in this revision

Viewing changes to spacetime/src.io/opencog/spacetime/reader/ReaderPOLY.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.reader;
42
 
 
43
 
import java.io.IOException;
44
 
import java.io.InputStream;
45
 
import java.io.InputStreamReader;
46
 
import java.io.LineNumberReader;
47
 
import java.util.HashMap;
48
 
import java.util.Vector;
49
 
 
50
 
import opencog.spacetime.geometry.IndexedFaceSetUtility;
51
 
import opencog.spacetime.scene.IndexedFaceSet;
52
 
import opencog.spacetime.scene.SceneGraphComponent;
53
 
import opencog.spacetime.scene.data.Attribute;
54
 
import opencog.spacetime.scene.data.StorageModel;
55
 
import opencog.spacetime.util.Input;
56
 
import opencog.spacetime.util.LoggingSystem;
57
 
 
58
 
 
59
 
 
60
 
/**
61
 
 *
62
 
 * Simple parser for polymake files
63
 
 *
64
 
 * @author weissman
65
 
 *
66
 
 */
67
 
public class ReaderPOLY extends AbstractReader {
68
 
 
69
 
  public void setInput(Input input) throws IOException {
70
 
    super.setInput(input);
71
 
    root = parse(input.getInputStream());
72
 
  }
73
 
  
74
 
  static SceneGraphComponent parse(InputStream is) {
75
 
    InputStreamReader r = new InputStreamReader(is);
76
 
    LineNumberReader lr = new LineNumberReader(r);
77
 
    SceneGraphComponent root = new SceneGraphComponent();
78
 
 
79
 
    Vector v=null;
80
 
    HashMap map = new HashMap();
81
 
    
82
 
    String line; 
83
 
    try {
84
 
        while((line= lr.readLine()) !=null) {
85
 
            line = line.trim();
86
 
            if(line.equals("")) continue;
87
 
            if(Character.isUpperCase(line.charAt(0))) {
88
 
                LoggingSystem.getLogger(ReaderPOLY.class).finer(" make entry "+line);
89
 
                v = new Vector();
90
 
                map.put(line,v);
91
 
            } else if (v != null&& !line.equals("")) {
92
 
                v.add(line);
93
 
            }
94
 
        }
95
 
    } catch (IOException e) {
96
 
        // TODO Auto-generated catch block
97
 
        e.printStackTrace();
98
 
    }
99
 
    // now we have all the data but still mostly unparsed
100
 
    int o = 0;
101
 
    Vector vData = ((Vector)map.get("GEOMETRIC_REALIZATION"));
102
 
    int n = vData.size();
103
 
     double[] vertices = new double[3*n];
104
 
     for(int i = 0; i<n;i++) {
105
 
         String str = (String) vData.get(i);
106
 
         String[] vals = str.split("[\\s\\{\\}/]");
107
 
         LoggingSystem.getLogger(ReaderPOLY.class).finer("vals length "+vals.length);
108
 
         vertices[3*i  ] = Integer.parseInt(vals[0])/(double)Integer.parseInt(vals[1]);
109
 
         vertices[3*i+1] = Integer.parseInt(vals[2])/(double)Integer.parseInt(vals[3]);
110
 
         vertices[3*i+2] = Integer.parseInt(vals[4])/(double)Integer.parseInt(vals[5]);
111
 
     }
112
 
     
113
 
     vData = ((Vector)map.get("FACETS"));
114
 
     n = vData.size();
115
 
     int[][] faces = new int[n][3];
116
 
     for(int i = 0; i<n;i++) {
117
 
         String str = (String) vData.get(i);
118
 
         String[] vals = str.split("[\\s\\{\\}/]");
119
 
         LoggingSystem.getLogger(ReaderPOLY.class).finer("face vals length "+vals.length);
120
 
         if(vals.length>3 ) o = 1;
121
 
         else o = 0;
122
 
         faces[i][0] = Integer.parseInt(vals[o+0]);
123
 
         faces[i][1] = Integer.parseInt(vals[o+1]);
124
 
         faces[i][2] = Integer.parseInt(vals[o+2]);
125
 
     }
126
 
     
127
 
     IndexedFaceSet ifs = new IndexedFaceSet();
128
 
     
129
 
     ifs.setVertexCountAndAttributes(Attribute.COORDINATES,StorageModel.DOUBLE_ARRAY.inlined(3).createReadOnly(vertices));
130
 
     //ifs.setFaceCountAndAttributes(Attribute.INDICES,new IntArrayArray.Array(faces));
131
 
     ifs.setFaceCountAndAttributes(Attribute.INDICES,StorageModel.INT_ARRAY_ARRAY.createReadOnly(faces));
132
 
     IndexedFaceSetUtility.calculateAndSetFaceNormals(ifs);
133
 
     IndexedFaceSetUtility.calculateAndSetVertexNormals(ifs);
134
 
     IndexedFaceSetUtility.calculateAndSetEdgesFromFaces(ifs);
135
 
     root.setGeometry(ifs);
136
 
//     System.out.println("we return "+root+" with geometry "+root.getGeometry());
137
 
    return root;
138
 
}
139
 
}