Statistics
| Revision:

root / trunk / libraries / lib3DMap / src / org / gvsig / gvsig3d / cacheservices / OSGCacheService.java @ 23017

History | View | Annotate | Download (6.03 KB)

1
package org.gvsig.gvsig3d.cacheservices;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.File;
5
import java.io.FileNotFoundException;
6

    
7
import org.apache.log4j.Logger;
8
import org.cresques.cts.IProjection;
9
import org.gvsig.cacheservice.CacheService;
10
import org.gvsig.fmap.geom.operation.GeometryOperationException;
11
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
12
import org.gvsig.fmap.geom.primitive.AbstractPrimitive;
13
import org.gvsig.geometries3D.MultiGeometry;
14
import org.gvsig.geometries3D.MultiSolid;
15
import org.gvsig.gvsig3d.drivers.GvsigDriverOSG;
16
import org.gvsig.operations3D.Draw3DMultiSolid;
17
import org.gvsig.operations3D.context.Draw3DContext;
18
import org.gvsig.osgvp.Group;
19
import org.gvsig.osgvp.Node;
20
import org.gvsig.osgvp.PositionAttitudeTransform;
21
import org.gvsig.osgvp.Vec3;
22
import org.gvsig.osgvp.osgDB;
23
import org.gvsig.osgvp.exceptions.node.NodeException;
24
import org.gvsig.osgvp.planets.Planet;
25
import org.gvsig.osgvp.planets.PlanetViewer;
26
import org.gvsig.osgvp.viewer.IViewerContainer;
27

    
28
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
29
import com.iver.ai2.gvsig3d.map3d.layers.Layer3DProps;
30
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
31
import com.iver.cit.gvsig.fmap.layers.FLayer;
32
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
33

    
34
public class OSGCacheService extends CacheService {
35

    
36
        private IViewerContainer _canvas3D;
37

    
38
        private PlanetViewer _planetViewer;
39

    
40
        private Rectangle2D _lyrExtentRect;
41

    
42
        private FLayer _layer;
43

    
44
        // vector caching for libJOSG
45
        private Group _layerNode;
46
        
47
        private boolean newLayerOSG = false;
48

    
49
        private static Logger logger = Logger.getLogger(OSGCacheService.class
50
                        .getName());
51

    
52
        // private ISymbol _currentSymbol;
53

    
54
        public OSGCacheService(IViewerContainer canvas3D, Planet planet,
55
                        String name, FLayer layer, IProjection viewProj) {
56
                super(planet.getPlanetName(), name);
57

    
58
                // getNames();
59

    
60
                _canvas3D = canvas3D;
61
                _planetViewer = (PlanetViewer) canvas3D.getOSGViewer();
62
                _layer = layer;
63
                _layerNode = new Group();
64
                _layerNode.setNodeName("layer3DOSG");
65
                
66
                try {
67
                        _planetViewer.addSpecialNode(_layerNode);
68
                } catch (NodeException e1) {
69
                        logger.error("Command: "
70
                                        + "Error adding new child node to the special node.", e1);
71
                }
72

    
73
                int cacheType = CacheService.GLOBAL;
74
                if (planet.getCoordinateSystemType() == Planet.CoordinateSystemType.GEOCENTRIC)
75
                        cacheType += SPHERIC;
76
                else
77
                        cacheType += PLANE;
78
                setCacheType(cacheType);
79

    
80
                try {
81
                        _lyrExtentRect = _layer.getFullExtent();
82
                } catch (ExpansionFileReadException e) {
83
                        // TODO Auto-generated catch block
84
                        e.printStackTrace();
85
                } catch (ReadDriverException e) {
86
                        // TODO Auto-generated catch block
87
                        e.printStackTrace();
88
                }
89

    
90
        }
91
        
92
        public Group getLayerNode() {
93
                return _layerNode;
94
        }
95
        
96

    
97
        // uses a strategy to get all features from layer and their symbols
98
        // and add them to the planet
99

    
100
        public void AddFeaturesToPlanet() {
101

    
102
                boolean newLayer = Layer3DProps.getLayer3DProps(_layer).isNewLayerOSG();
103
                
104
                if(!newLayer)
105
                {
106
                        GvsigDriverOSG osgD = (GvsigDriverOSG) ((FLyrVect) _layer).getSource()
107
                        .getDriver();
108
                        try {
109
                                File file = osgD.getFile();
110
                                Node n = null; 
111
                                
112
                                
113
                                try {
114
                                        n = osgDB.readNodeFile(file.getAbsolutePath());
115
                                } catch (FileNotFoundException e) {
116
                                        // TODO Auto-generated catch block
117
                                        e.printStackTrace();
118
                                }
119
                                MultiGeometry root = osgD.getOSGDriver().getRootFeature();
120
                                // Insert the geometry in the root node of the graph.
121
                                Group g = new Group();
122
//                                buildGeometry(root, g);
123
                                g.addChild(n);
124
                                _layerNode.addChild(g);
125
                        } catch (NodeException e) {
126
                                // TODO Auto-generated catch block
127
                                e.printStackTrace();
128
                        }
129
                }
130
                this._canvas3D.repaint();
131
        }
132

    
133
        public void RefreshFeaturesToPlanet() {
134
                this.DeleteFeaturesToPlanet();
135
                this.AddFeaturesToPlanet();
136
        }
137

    
138
        public void DeleteFeaturesToPlanet() {
139
                try {
140
                        _planetViewer.removeSpecialNode(_layerNode);
141
                } catch (NodeException e) {
142
                        logger.error("Command: "
143
                                        + "Error removing new child node to the special node.", e);
144
                }
145
        }
146

    
147
        public void refreshFeaturesToPlanet(boolean visible) {
148
        }
149

    
150
        public void AddGeometryToLayer(MultiGeometry multiGeometry, Vec3 position,
151
                        Vec3 rotation, Vec3 scale) {
152
                Group group = new Group();
153
                PositionAttitudeTransform posAttTrasn = null;
154
                try {
155
                        posAttTrasn = new PositionAttitudeTransform();
156
                        posAttTrasn.setPosition(position);
157
                        posAttTrasn.setScale(scale);
158
                        posAttTrasn.setAttitude((float) rotation.x(), new Vec3(1,0,0));
159
                        posAttTrasn.setAttitude((float) rotation.y(), new Vec3(0,1,0));
160
                        posAttTrasn.setAttitude((float) rotation.z(), new Vec3(0,0,1));
161
                        buildGeometry(multiGeometry, group);
162
                        posAttTrasn.addChild(group);
163
                        _layerNode.addChild(posAttTrasn);
164
                } catch (NodeException e1) {
165
                        // TODO Auto-generated catch block
166
                        e1.printStackTrace();
167
                }
168

    
169

    
170
        }
171

    
172
        private void buildGeometry(AbstractPrimitive geom, Group group)
173
                        throws NodeException {
174

    
175
                int i;
176

    
177
                if (geom instanceof MultiGeometry) {
178
                        MultiGeometry multiGeometry = (MultiGeometry) geom;
179
                        for (i = 0; i < multiGeometry.getGeometries().size(); i++) {
180

    
181
                                Group child = new Group();
182
                                group.addChild(child);
183
                                buildGeometry(multiGeometry.getGeometries().get(i), child);
184
                        }
185
                } else if (geom instanceof MultiSolid) {
186
                        // Getting the geometry
187
                        MultiSolid multiSolid = (MultiSolid) geom;
188
                        // Creating the context and adding parameters
189
                        Draw3DContext ctx3D = new Draw3DContext();
190
                        ctx3D.setGroup(group);
191
                        // Creating the drawing operation
192
                        Draw3DMultiSolid d3DMultiSolid = new Draw3DMultiSolid();
193

    
194
                        try {
195
                                // Invoking the operation for the multisolid
196
                                multiSolid.invokeOperation(d3DMultiSolid.getOperationIndex(),
197
                                                ctx3D);
198
                        } catch (GeometryOperationNotSupportedException e) {
199
                                // TODO Auto-generated catch block
200
                                e.printStackTrace();
201
                        } catch (GeometryOperationException e) {
202
                                // TODO Auto-generated catch block
203
                                e.printStackTrace();
204
                        }
205
                }
206

    
207
        }
208

    
209

    
210
}