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