gvsig-3d / 1.10 / trunk / libraries / lib3DMap / src / org / gvsig / gvsig3d / cacheservices / OSGCacheService.java @ 39
History | View | Annotate | Download (10.7 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 |
import java.util.Vector; |
7 |
|
8 |
import javax.swing.JOptionPane; |
9 |
|
10 |
import org.apache.log4j.Logger; |
11 |
import org.cresques.cts.IProjection; |
12 |
import org.gvsig.cacheservice.CacheService; |
13 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
14 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
15 |
import org.gvsig.fmap.geom.primitive.AbstractPrimitive; |
16 |
import org.gvsig.geometries3D.MultiGeometry; |
17 |
import org.gvsig.geometries3D.MultiSolid; |
18 |
import org.gvsig.gvsig3d.drivers.GvsigDriverOSG; |
19 |
import org.gvsig.gvsig3d.listener.EditorListener; |
20 |
import org.gvsig.gvsig3d.navigation.NavigationMode; |
21 |
import org.gvsig.operations3D.Draw3DMultiSolid; |
22 |
import org.gvsig.operations3D.context.Draw3DContext; |
23 |
import org.gvsig.osgvp.core.osg.Group; |
24 |
import org.gvsig.osgvp.core.osg.Node; |
25 |
import org.gvsig.osgvp.core.osg.PositionAttitudeTransform; |
26 |
import org.gvsig.osgvp.core.osg.Vec3; |
27 |
import org.gvsig.osgvp.core.osg.Vec4; |
28 |
import org.gvsig.osgvp.core.osgdb.osgDB; |
29 |
import org.gvsig.osgvp.exceptions.node.NodeException; |
30 |
import org.gvsig.osgvp.features.Text; |
31 |
import org.gvsig.osgvp.manipulator.EditionManager; |
32 |
import org.gvsig.osgvp.manipulator.ManipulatorHandler; |
33 |
import org.gvsig.osgvp.manipulator.RemoveAllSelectionCommand; |
34 |
import org.gvsig.osgvp.terrain.Terrain; |
35 |
import org.gvsig.osgvp.terrain.TerrainViewer; |
36 |
import org.gvsig.osgvp.util.ActionCommand; |
37 |
import org.gvsig.osgvp.viewer.IViewerContainer; |
38 |
|
39 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
40 |
import com.iver.ai2.gvsig3d.map3d.layers.Layer3DProps; |
41 |
|
42 |
import com.iver.ai2.gvsig3d.resources.ResourcesFactory; |
43 |
import com.iver.andami.PluginServices; |
44 |
|
45 |
import com.iver.andami.PluginServices; |
46 |
|
47 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
48 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
49 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
50 |
|
51 |
public class OSGCacheService extends CacheService { |
52 |
|
53 |
private IViewerContainer _canvas3d;
|
54 |
private TerrainViewer _terrainViewer;
|
55 |
private FLayer _layer;
|
56 |
// vector caching for libJOSG
|
57 |
private Group _layerNode; |
58 |
private EditionManager _editionManager;
|
59 |
private EditorListener _editionListener;
|
60 |
private ManipulatorHandler _manipulatorHandler;
|
61 |
private RemoveAllSelectionCommand _command;
|
62 |
private String _filePath; |
63 |
private boolean _manipulatorsActive; |
64 |
private static Logger _logger = Logger.getLogger(OSGCacheService.class |
65 |
.getName()); |
66 |
|
67 |
public OSGCacheService(IViewerContainer canvas3D, Terrain terrain,
|
68 |
String name, FLayer layer, IProjection viewProj) {
|
69 |
super(terrain.getTerrainName(), name);
|
70 |
|
71 |
_canvas3d = canvas3D; |
72 |
_terrainViewer = (TerrainViewer) canvas3D.getOSGViewer(); |
73 |
_layer = layer; |
74 |
_layerNode = new Group(); |
75 |
_layerNode.setNodeName("layer3DOSG");
|
76 |
_manipulatorsActive = false;
|
77 |
try {
|
78 |
_editionManager = new EditionManager();
|
79 |
_layerNode.addChild(_editionManager); |
80 |
_manipulatorHandler = new ManipulatorHandler();
|
81 |
_manipulatorHandler.setActive(false);
|
82 |
|
83 |
_editionListener = new EditorListener(_editionManager,
|
84 |
_manipulatorHandler, _layer, _canvas3d, _terrainViewer); |
85 |
|
86 |
_canvas3d.getOSGViewer().addEventHandler(_manipulatorHandler); |
87 |
|
88 |
} catch (NodeException e2) {
|
89 |
// TODO Auto-generated catch block
|
90 |
e2.printStackTrace(); |
91 |
} |
92 |
|
93 |
try {
|
94 |
_terrainViewer.addFeature(_layerNode); |
95 |
} catch (NodeException e1) {
|
96 |
_logger.error("Command: "
|
97 |
+ "Error adding new child node to the special node.", e1);
|
98 |
} |
99 |
|
100 |
int cacheType = CacheService.GLOBAL;
|
101 |
if (terrain.getCoordinateSystemType() == Terrain.CoordinateSystemType.GEOCENTRIC)
|
102 |
cacheType += SPHERIC; |
103 |
else
|
104 |
cacheType += PLANE; |
105 |
setCacheType(cacheType); |
106 |
|
107 |
} |
108 |
|
109 |
public Group getLayerNode() { |
110 |
|
111 |
if (!Layer3DProps.getLayer3DProps(this._layer).isEditing()) { |
112 |
try {
|
113 |
return (Group) _editionManager.getScene(); |
114 |
} catch (NodeException e) {
|
115 |
// TODO Auto-generated catch block
|
116 |
e.printStackTrace(); |
117 |
} |
118 |
} |
119 |
|
120 |
else {
|
121 |
|
122 |
try {
|
123 |
return _editionManager.getTransformedScene();
|
124 |
} catch (NodeException e) {
|
125 |
// TODO Auto-generated catch block
|
126 |
e.printStackTrace(); |
127 |
} |
128 |
|
129 |
} |
130 |
return _layerNode;
|
131 |
} |
132 |
|
133 |
// uses a strategy to get all features from layer and their symbols
|
134 |
// and add them to the terrain
|
135 |
|
136 |
public void AddFeaturesToTerrain() { |
137 |
|
138 |
boolean newLayer = Layer3DProps.getLayer3DProps(_layer).isNewLayerOSG();
|
139 |
|
140 |
if (!newLayer) {
|
141 |
GvsigDriverOSG osgD = (GvsigDriverOSG) ((FLyrVect) _layer) |
142 |
.getSource().getDriver(); |
143 |
try {
|
144 |
File file = osgD.getFile();
|
145 |
_filePath = file.getAbsolutePath(); |
146 |
Node n = null;
|
147 |
|
148 |
try {
|
149 |
n = osgDB.readNodeFile(file.getAbsolutePath()); |
150 |
} catch (FileNotFoundException e) { |
151 |
// TODO Auto-generated catch block
|
152 |
e.printStackTrace(); |
153 |
} |
154 |
|
155 |
// Insert the geometry in the root node of the graph.
|
156 |
Group g = new Group(n.getCPtr()); |
157 |
// buildGeometry(root, g);
|
158 |
// g.addChild(n);
|
159 |
for (int i = 0; i < g.getNumChildren(); i++) { |
160 |
|
161 |
_editionManager.setScene(g.getChild(i)); |
162 |
|
163 |
} |
164 |
|
165 |
// _layerNode.addChild(n);
|
166 |
// startEditing();
|
167 |
// _layerNode.addChild(g);
|
168 |
} catch (NodeException e) {
|
169 |
_logger |
170 |
.error( |
171 |
"Command: "
|
172 |
+ "Error loading OSG file. File must be generated with gvSIG",
|
173 |
e); |
174 |
|
175 |
} |
176 |
} |
177 |
|
178 |
} |
179 |
|
180 |
public void RefreshFeaturesToTerrain() { |
181 |
this.DeleteFeaturesToTerrain();
|
182 |
this.AddFeaturesToTerrain();
|
183 |
} |
184 |
|
185 |
public void DeleteFeaturesToTerrain() { |
186 |
try {
|
187 |
endEditing(); |
188 |
_layerNode.removeChildren(); |
189 |
_terrainViewer.removeFeature(_layerNode); |
190 |
_layerNode = null;
|
191 |
} catch (NodeException e) {
|
192 |
_logger.error("Command: "
|
193 |
+ "Error removing new child node to the special node.", e);
|
194 |
} |
195 |
} |
196 |
|
197 |
public void refreshFeaturesToTerrain(boolean visible) { |
198 |
_layerNode.setNodeMask(visible ? 0xffffffff : 0x00000000); |
199 |
} |
200 |
|
201 |
public void AddGeometryToLayer(MultiGeometry multiGeometry, Vec3 position, |
202 |
Vec3 rotation, Vec3 scale) { |
203 |
Group group = new Group(); |
204 |
group.setNodeName("GROUP-PAT");
|
205 |
|
206 |
PositionAttitudeTransform posAttTrasn = null;
|
207 |
try {
|
208 |
posAttTrasn = new PositionAttitudeTransform();
|
209 |
posAttTrasn.setPosition(position); |
210 |
posAttTrasn.setScale(scale); |
211 |
posAttTrasn.setNodeName("PAT");
|
212 |
buildGeometry(multiGeometry, group); |
213 |
posAttTrasn.addChild(group); |
214 |
|
215 |
// if (props3D.isEditing()){
|
216 |
_editionManager.setScene(posAttTrasn); |
217 |
// }else{
|
218 |
// _layerNode.addChild(posAttTrasn);
|
219 |
// }
|
220 |
// _layerNode.addChild(group);
|
221 |
} catch (NodeException e1) {
|
222 |
// TODO Auto-generated catch block
|
223 |
e1.printStackTrace(); |
224 |
} |
225 |
|
226 |
} |
227 |
|
228 |
public void AddNodeToLayer(Node node, Vec3 position, Vec3 rotation, |
229 |
Vec3 scale) { |
230 |
|
231 |
PositionAttitudeTransform posAttTrasn = null;
|
232 |
try {
|
233 |
posAttTrasn = new PositionAttitudeTransform();
|
234 |
posAttTrasn.setPosition(position); |
235 |
posAttTrasn.setScale(scale); |
236 |
posAttTrasn.setNodeName("PAT");
|
237 |
// posAttTrasn.setAttitude((float) rotation.x(), new Vec3(1,0,0));
|
238 |
// posAttTrasn.setAttitude((float) rotation.y(), new Vec3(0,1,0));
|
239 |
// posAttTrasn.setAttitude((float) rotation.z(), new Vec3(0,0,1));
|
240 |
// buildGeometry(multiGeometry, group);
|
241 |
|
242 |
posAttTrasn.addChild(node); |
243 |
|
244 |
// if (props3D.isEditing()){
|
245 |
_editionManager.setScene(posAttTrasn); |
246 |
// }else{
|
247 |
// _layerNode.addChild(posAttTrasn);
|
248 |
// }
|
249 |
// _layerNode.addChild(group);
|
250 |
} catch (NodeException e1) {
|
251 |
// TODO Auto-generated catch block
|
252 |
e1.printStackTrace(); |
253 |
} |
254 |
|
255 |
} |
256 |
|
257 |
private void buildGeometry(AbstractPrimitive geom, Group group) |
258 |
throws NodeException {
|
259 |
|
260 |
int i;
|
261 |
|
262 |
if (geom instanceof MultiGeometry) { |
263 |
MultiGeometry multiGeometry = (MultiGeometry) geom; |
264 |
for (i = 0; i < multiGeometry.getGeometries().size(); i++) { |
265 |
|
266 |
Group child = new Group(); |
267 |
group.addChild(child); |
268 |
buildGeometry(multiGeometry.getGeometries().get(i), child); |
269 |
} |
270 |
} else if (geom instanceof MultiSolid) { |
271 |
// Getting the geometry
|
272 |
MultiSolid multiSolid = (MultiSolid) geom; |
273 |
// Creating the context and adding parameters
|
274 |
Draw3DContext ctx3D = new Draw3DContext();
|
275 |
ctx3D.setGroup(group); |
276 |
// Creating the drawing operation
|
277 |
Draw3DMultiSolid d3DMultiSolid = new Draw3DMultiSolid();
|
278 |
|
279 |
try {
|
280 |
// Invoking the operation for the multisolid
|
281 |
multiSolid.invokeOperation(d3DMultiSolid.getOperationIndex(), |
282 |
ctx3D); |
283 |
} catch (GeometryOperationNotSupportedException e) {
|
284 |
// TODO Auto-generated catch block
|
285 |
e.printStackTrace(); |
286 |
} catch (GeometryOperationException e) {
|
287 |
// TODO Auto-generated catch block
|
288 |
e.printStackTrace(); |
289 |
} |
290 |
} |
291 |
|
292 |
} |
293 |
|
294 |
public void startEditing() { |
295 |
|
296 |
Layer3DProps props3D = Layer3DProps.getLayer3DProps(this._layer);
|
297 |
if ((props3D.getType() == Layer3DProps.layer3DOSG)
|
298 |
&& (!props3D.isEditing())) { |
299 |
props3D.setEditing(true);
|
300 |
props3D.setNewLayerOSG(true);
|
301 |
JOptionPane.showMessageDialog(null, PluginServices.getText(this, |
302 |
"edited_layer"));
|
303 |
enableEditionListener(); |
304 |
|
305 |
} |
306 |
} |
307 |
|
308 |
public void endEditing() { |
309 |
|
310 |
Layer3DProps props3D = Layer3DProps.getLayer3DProps(this._layer);
|
311 |
if ((props3D.getType() == Layer3DProps.layer3DOSG)
|
312 |
&& (props3D.isEditing())) { |
313 |
props3D.setEditing(false);
|
314 |
|
315 |
if (props3D.isNewLayerOSG())
|
316 |
JOptionPane.showMessageDialog(null, PluginServices.getText( |
317 |
this, "modified_layer")); |
318 |
|
319 |
_command = new RemoveAllSelectionCommand(_editionManager);
|
320 |
_command.execute(); |
321 |
|
322 |
disableEditionListener(); |
323 |
} |
324 |
|
325 |
} |
326 |
|
327 |
public EditionManager getEditionManager() {
|
328 |
return _editionManager;
|
329 |
} |
330 |
|
331 |
public ManipulatorHandler getManipulatorHandler() {
|
332 |
|
333 |
return _manipulatorHandler;
|
334 |
|
335 |
} |
336 |
|
337 |
public void setEditionManager(EditionManager editionManager) { |
338 |
this._editionManager = editionManager;
|
339 |
} |
340 |
|
341 |
public String getLayerPath() { |
342 |
return _filePath;
|
343 |
} |
344 |
|
345 |
public void enableEditionListener() { |
346 |
|
347 |
_canvas3d.addKeyListener(_editionListener); |
348 |
_canvas3d.addMouseListener(_editionListener); |
349 |
_manipulatorHandler.setActive(true);
|
350 |
NavigationMode.removeAllNavigationModes(_terrainViewer |
351 |
.getTerrainCameraManipulator()); |
352 |
_manipulatorsActive = true;
|
353 |
|
354 |
} |
355 |
|
356 |
public void disableEditionListener() { |
357 |
|
358 |
_canvas3d.removeKeyListener(_editionListener); |
359 |
_canvas3d.removeMouseListener(_editionListener); |
360 |
_manipulatorHandler.setActive(false);
|
361 |
NavigationMode.restoreAllNavigationModes(_terrainViewer |
362 |
.getTerrainCameraManipulator()); |
363 |
ActionCommand command; |
364 |
command = new RemoveAllSelectionCommand(_editionManager);
|
365 |
command.execute(); |
366 |
_manipulatorsActive = false;
|
367 |
|
368 |
} |
369 |
|
370 |
public boolean getManipulatorsActive() { |
371 |
|
372 |
return _manipulatorsActive;
|
373 |
} |
374 |
|
375 |
public void setManipulatorsActive(boolean active) { |
376 |
|
377 |
_manipulatorsActive = active; |
378 |
|
379 |
} |
380 |
|
381 |
} |