~seh999/jcog/proto3

« back to all changes in this revision

Viewing changes to spacetime/src.io/opencog/spacetime/reader/ReaderPTS.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.awt.Color;
44
 
import java.io.IOException;
45
 
import java.io.LineNumberReader;
46
 
 
47
 
import opencog.spacetime.geometry.BoundingBoxUtility;
48
 
import opencog.spacetime.geometry.GeometryUtility;
49
 
import opencog.spacetime.geometry.PointSetFactory;
50
 
import opencog.spacetime.scene.Appearance;
51
 
import opencog.spacetime.scene.SceneGraphComponent;
52
 
import opencog.spacetime.shader.CommonAttributes;
53
 
import opencog.spacetime.util.Input;
54
 
import opencog.spacetime.util.Rectangle3D;
55
 
 
56
 
 
57
 
/**
58
 
 *
59
 
 * simple reader for the PTS file format. 
60
 
 * 
61
 
 * @author Steffen Weissmann
62
 
 *
63
 
 */
64
 
public class ReaderPTS extends AbstractReader {
65
 
 
66
 
  public ReaderPTS() {
67
 
    root = new SceneGraphComponent();
68
 
    Appearance app = new Appearance();
69
 
    app.setAttribute(CommonAttributes.SPHERES_DRAW, false);
70
 
    app.setAttribute(CommonAttributes.PICKABLE, false);
71
 
    app.setAttribute(CommonAttributes.POINT_SHADER+"."+CommonAttributes.PICKABLE, false);
72
 
    app.setAttribute(CommonAttributes.POINT_SHADER+"."+CommonAttributes.POINT_SIZE, 30.);
73
 
    app.setAttribute(CommonAttributes.VERTEX_DRAW, true);
74
 
    app.setAttribute(CommonAttributes.POINT_SHADER+"."+CommonAttributes.DIFFUSE_COLOR, Color.white);
75
 
    root.setAppearance(app);
76
 
  }
77
 
 
78
 
  public void setInput(Input input) throws IOException {
79
 
    super.setInput(input);
80
 
    load();
81
 
  }
82
 
 
83
 
  private void load() throws IOException {
84
 
        int skip=2;
85
 
    LineNumberReader r = new LineNumberReader(input.getReader());
86
 
    
87
 
    String l = null;
88
 
    while ((l=r.readLine().trim()).startsWith("#"));
89
 
    
90
 
    int pointCount = Integer.parseInt(l)/(skip+1);
91
 
    double[] points = new double[pointCount*3];
92
 
    double[] colors = new double[pointCount*3];
93
 
    
94
 
    int index=0;
95
 
    while ((l=r.readLine())!=null) {
96
 
        if (index==pointCount) break;
97
 
        for (int i = 0; i < skip; i++) r.readLine(); 
98
 
        String[] split = l.split(" ");
99
 
        if (split.length!=7) continue;
100
 
        points[3*index]=Double.parseDouble(split[0]);
101
 
        points[3*index+1]=Double.parseDouble(split[1]);
102
 
        points[3*index+2]=Double.parseDouble(split[2]);
103
 
        
104
 
        colors[3*index]=Double.parseDouble(split[4])/255;
105
 
        colors[3*index+1]=Double.parseDouble(split[5])/255;
106
 
        colors[3*index+2]=Double.parseDouble(split[6])/255;
107
 
        index++;
108
 
    }
109
 
    
110
 
    PointSetFactory psf = new PointSetFactory();
111
 
    psf.setVertexCount(pointCount);
112
 
    psf.setVertexCoordinates(points);
113
 
    psf.setVertexColors(colors);
114
 
    psf.update();
115
 
    Rectangle3D bb = BoundingBoxUtility.calculateBoundingBox(psf.getPointSet());
116
 
    psf.getPointSet().setGeometryAttributes(GeometryUtility.BOUNDING_BOX, bb);
117
 
    root.setGeometry(psf.getPointSet());
118
 
  }
119
 
 
120
 
}