gvsig-3d / 2.0 / trunk / org.gvsig.gvsig3d / org.gvsig.gvsig3d.lib / org.gvsig.gvsig3d.lib.impl / src / main / java / org / gvsig / gvsig3d / impl / symbology3d / fill / impl / SimpleFill3DSymbol.java @ 369
History | View | Annotate | Download (9.63 KB)
1 |
package org.gvsig.gvsig3d.impl.symbology3d.fill.impl; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Graphics2D; |
5 |
import java.awt.Rectangle; |
6 |
import java.awt.geom.AffineTransform; |
7 |
import java.awt.geom.PathIterator; |
8 |
|
9 |
import org.gvsig.compat.print.PrintAttributes; |
10 |
import org.gvsig.fmap.dal.feature.Feature; |
11 |
import org.gvsig.fmap.geom.Geometry; |
12 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
13 |
import org.gvsig.fmap.geom.GeometryLocator; |
14 |
import org.gvsig.fmap.geom.GeometryManager; |
15 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
16 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
17 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
18 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
19 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
20 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
21 |
import org.gvsig.gvsig3d.symbology3d.fill.ISimpleFill3DSymbol; |
22 |
import org.gvsig.osgvp.core.osg.Node; |
23 |
import org.gvsig.osgvp.core.osg.Vec3; |
24 |
import org.gvsig.osgvp.exceptions.node.NodeException; |
25 |
import org.gvsig.osgvp.geometry.GeoMultiPolygon3D; |
26 |
import org.gvsig.osgvp.geometry.GeoPolygon3D; |
27 |
import org.gvsig.osgvp.geometry.GeoPolygonList3D; |
28 |
import org.gvsig.osgvp.symbology.ISymbol3D; |
29 |
import org.gvsig.osgvp.symbology.fill.SimpleFill; |
30 |
import org.gvsig.osgvp.terrain.Terrain; |
31 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.AbstractFillSymbol; |
32 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.AbstractSymbol; |
33 |
import org.gvsig.tools.ToolsLocator; |
34 |
import org.gvsig.tools.dynobject.DynStruct; |
35 |
import org.gvsig.tools.persistence.PersistenceManager; |
36 |
import org.gvsig.tools.persistence.PersistentState; |
37 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
38 |
import org.gvsig.tools.task.Cancellable; |
39 |
import org.gvsig.tools.util.Callable; |
40 |
import org.gvsig.tools.util.Caller; |
41 |
import org.gvsig.tools.util.impl.DefaultCaller; |
42 |
import org.slf4j.Logger; |
43 |
import org.slf4j.LoggerFactory; |
44 |
|
45 |
/**
|
46 |
*
|
47 |
* @author jtorres
|
48 |
*
|
49 |
*/
|
50 |
public class SimpleFill3DSymbol extends AbstractFillSymbol implements |
51 |
ISimpleFill3DSymbol { |
52 |
|
53 |
public static final String SIMPLE_FILL3D_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleFill3DSymbol"; |
54 |
private static final GeometryManager geomManager = GeometryLocator |
55 |
.getGeometryManager(); |
56 |
private static final Logger LOG = LoggerFactory |
57 |
.getLogger(SimpleFill3DSymbol.class); |
58 |
public GeoPolygonList3D _geometry;
|
59 |
public GeoMultiPolygon3D _mpolygon;
|
60 |
protected float _height = 10; |
61 |
private Terrain _terrain;
|
62 |
Vec3 _auxVect; |
63 |
protected ISymbol3D sm;
|
64 |
protected int _coordinateSystemType; |
65 |
|
66 |
public SimpleFill3DSymbol() {
|
67 |
super();
|
68 |
try {
|
69 |
sm = new SimpleFill();
|
70 |
_geometry = new GeoPolygonList3D();
|
71 |
sm.setColor(this.getColor());
|
72 |
_terrain = new Terrain();
|
73 |
_auxVect = new Vec3();
|
74 |
} catch (NodeException e) {
|
75 |
// TODO Auto-generated catch block
|
76 |
e.printStackTrace(); |
77 |
} |
78 |
|
79 |
} |
80 |
|
81 |
public ISymbol getSymbolForSelection() {
|
82 |
// TODO Auto-generated method stub
|
83 |
return null; |
84 |
} |
85 |
|
86 |
public void setColor(Color color) { |
87 |
super.setColor(color);
|
88 |
sm.setColor(this.getColor());
|
89 |
} |
90 |
|
91 |
public void draw(Graphics2D g, AffineTransform affineTransform, |
92 |
Geometry geom, Feature f, Cancellable cancel) { |
93 |
|
94 |
// Color c = getFillColor();
|
95 |
// geom.getClass().getName()
|
96 |
|
97 |
sm.setColor(this.getColor());
|
98 |
// WAITING for gvSIG ARCHITECTS to do something with polygons with holes
|
99 |
// in shp files
|
100 |
// in the meanwhile nothing is gonna happen with this kind of polygons
|
101 |
// if (geom.getGeometryType().isTypeOf(Geometry.TYPES.SURFACE))
|
102 |
// {
|
103 |
// Surface s = (Surface)geom.cloneGeometry();
|
104 |
// _geometry.addMultiPolygon(getGeoMultiPolygon(s));
|
105 |
// }
|
106 |
double[] dataLine = new double[6]; |
107 |
GeoMultiPolygon3D mp = null;
|
108 |
try {
|
109 |
mp = new GeoMultiPolygon3D();
|
110 |
} catch (NodeException e1) {
|
111 |
// TODO Auto-generated catch block
|
112 |
e1.printStackTrace(); |
113 |
} |
114 |
GeoPolygon3D p = null;
|
115 |
PathIterator theIteratorL = geom.getGeneralPath().getPathIterator(null); |
116 |
while (!theIteratorL.isDone()) {
|
117 |
|
118 |
int type = theIteratorL.currentSegment(dataLine);
|
119 |
|
120 |
// Adding points
|
121 |
switch (type) {
|
122 |
case PathIterator.SEG_MOVETO: |
123 |
|
124 |
if (p != null) |
125 |
mp.addPolygon(p); |
126 |
try {
|
127 |
p = new GeoPolygon3D();
|
128 |
} catch (NodeException e) {
|
129 |
// TODO Auto-generated catch block
|
130 |
e.printStackTrace(); |
131 |
} |
132 |
|
133 |
if (_coordinateSystemType == Terrain.CoordinateSystemType.PROJECTED) {
|
134 |
p.addPoint(dataLine[0], dataLine[1], _height); |
135 |
} else {
|
136 |
_auxVect.set(dataLine[0], dataLine[1], _height); |
137 |
Vec3 converted = _terrain |
138 |
.convertLatLongHeightToXYZ(_auxVect); |
139 |
p.addPoint(converted.x(), converted.y(), converted.z()); |
140 |
} |
141 |
// p.addPoint(dataLine[0], dataLine[1],10);
|
142 |
|
143 |
break;
|
144 |
|
145 |
case PathIterator.SEG_LINETO: |
146 |
// System.out.println("SEG_LINETO");
|
147 |
|
148 |
if (_coordinateSystemType == Terrain.CoordinateSystemType.PROJECTED) {
|
149 |
p.addPoint(dataLine[0], dataLine[1], _height); |
150 |
} else {
|
151 |
_auxVect.set(dataLine[0], dataLine[1], _height); |
152 |
Vec3 converted = _terrain |
153 |
.convertLatLongHeightToXYZ(_auxVect); |
154 |
p.addPoint(converted.x(), converted.y(), converted.z()); |
155 |
} |
156 |
break;
|
157 |
|
158 |
case PathIterator.SEG_QUADTO: |
159 |
// System.out.println("SEG_QUADTO");
|
160 |
break;
|
161 |
|
162 |
case PathIterator.SEG_CUBICTO: |
163 |
// System.out.println("SEG_CUBICTO");
|
164 |
break;
|
165 |
|
166 |
case PathIterator.SEG_CLOSE: |
167 |
// System.out.println("SEG_CLOSE");
|
168 |
break;
|
169 |
} |
170 |
|
171 |
// contH++;
|
172 |
theIteratorL.next(); |
173 |
} |
174 |
|
175 |
if (p != null) |
176 |
mp.addPolygon(p); |
177 |
// if(p!=null)
|
178 |
// mp.addPolygon(p);
|
179 |
if (mp != null) |
180 |
_geometry.addMultiPolygon(mp); |
181 |
|
182 |
// if (c!=null && hasFill()) {
|
183 |
// g.setColor(c);o
|
184 |
//
|
185 |
//
|
186 |
//
|
187 |
// g.fill(geom.getShape(affineTransform));
|
188 |
// }
|
189 |
// Border color
|
190 |
// if (getOutline() != null && hasOutline()) {
|
191 |
// getOutline().draw(g, affineTransform, geom, f, cancel);
|
192 |
// }
|
193 |
|
194 |
} |
195 |
|
196 |
public GeoPolygonList3D getGeometry() {
|
197 |
return _geometry;
|
198 |
} |
199 |
|
200 |
public int getSymbolType() { |
201 |
// TODO Auto-generated method stub
|
202 |
return 0; |
203 |
} |
204 |
|
205 |
public void drawInsideRectangle(Graphics2D g, |
206 |
AffineTransform scaleInstance, Rectangle r, |
207 |
PrintAttributes properties) throws SymbolDrawingException {
|
208 |
|
209 |
Rectangle rect = new Rectangle(r.x, r.y, r.width, r.height); |
210 |
rect.setFrame(((int) rect.getMinX()) + 1, ((int) rect.getMinY()) + 1, |
211 |
((int) rect.getWidth()) - 2, ((int) rect.getHeight()) - 2); |
212 |
Geometry geom; |
213 |
try {
|
214 |
geom = geomManager.createSurface( |
215 |
new GeneralPathX(rect.getPathIterator(null)), |
216 |
SUBTYPES.GEOM2D); |
217 |
} catch (CreateGeometryException e) {
|
218 |
LOG.error("Creating a surface", e);
|
219 |
throw new SymbolDrawingException(getSymbolType()); |
220 |
} |
221 |
|
222 |
Color c = getFillColor();
|
223 |
if (c != null && hasFill()) { |
224 |
g.setColor(c); |
225 |
g.fillRect(rect.x, rect.y, rect.width, rect.height); |
226 |
} |
227 |
// Border Polygons
|
228 |
//
|
229 |
// if (getOutline() != null && hasOutline()) {
|
230 |
// if (properties==null)
|
231 |
// getOutline().draw(g, scaleInstance, geom, null, null);
|
232 |
// else
|
233 |
// print(g, new AffineTransform(), geom, properties);
|
234 |
// }
|
235 |
|
236 |
} |
237 |
|
238 |
public void loadFromState(PersistentState state) |
239 |
throws PersistenceException {
|
240 |
// Set parent fill symbol properties
|
241 |
super.loadFromState(state);
|
242 |
|
243 |
// // Set own properties
|
244 |
// setStyle(state.getInt(FIELD_MARKERSTYLE));
|
245 |
// setOutlined(state.getBoolean(FIELD_OUTLINED));
|
246 |
// if (isOutlined()) {
|
247 |
// setOutlineColor((Color) state.get(FIELD_OUTLINECOLOR));
|
248 |
// setOutlineSize(state.getDouble(FIELD_OUTLINESIZE));
|
249 |
// }
|
250 |
} |
251 |
|
252 |
public void saveToState(PersistentState state) throws PersistenceException { |
253 |
// Save parent fill symbol properties
|
254 |
super.saveToState(state);
|
255 |
|
256 |
// Save own properties
|
257 |
// state.set(FIELD_MARKERSTYLE, getStyle());
|
258 |
// state.set(FIELD_OUTLINED, isOutlined());
|
259 |
// if (isOutlined()) {
|
260 |
// state.set(FIELD_OUTLINECOLOR, getOutlineColor());
|
261 |
// state.set(FIELD_OUTLINESIZE, getOutlineSize());
|
262 |
// }
|
263 |
} |
264 |
|
265 |
public static class RegisterPersistence implements Callable { |
266 |
|
267 |
public Object call() throws Exception { |
268 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
269 |
if (manager.getDefinition("Symbol") == null) { |
270 |
Caller caller = new DefaultCaller();
|
271 |
caller.add(new AbstractSymbol.RegisterPersistence());
|
272 |
caller.call(); |
273 |
} |
274 |
|
275 |
if (manager.getDefinition("SimpleFillSymbol") == null) { |
276 |
Caller caller = new DefaultCaller();
|
277 |
caller.add(new AbstractFillSymbol.RegisterPersistence());
|
278 |
caller.call(); |
279 |
} |
280 |
|
281 |
DynStruct definition = manager.addDefinition( |
282 |
SimpleFill3DSymbol.class, "SimpleFill3DSymbol",
|
283 |
"SimpleFill3DSymbol Persistence definition", null, null); |
284 |
|
285 |
definition.extend(PersistenceManager.PERSISTENCE_NAMESPACE, |
286 |
"FillSymbol");
|
287 |
|
288 |
return Boolean.TRUE; |
289 |
} |
290 |
} |
291 |
|
292 |
public static class RegisterSymbol implements Callable { |
293 |
|
294 |
public Object call() throws Exception { |
295 |
int[] shapeTypes; |
296 |
SymbolManager manager = MapContextLocator.getSymbolManager(); |
297 |
|
298 |
shapeTypes = new int[] { Geometry.TYPES.SURFACE, |
299 |
Geometry.TYPES.MULTISURFACE }; |
300 |
manager.registerSymbol(ISimpleFill3DSymbol.SYMBOL_NAME, shapeTypes, |
301 |
SimpleFill3DSymbol.class, "project.document.view3d");
|
302 |
|
303 |
return Boolean.TRUE; |
304 |
} |
305 |
} |
306 |
|
307 |
public void print(Graphics2D g, AffineTransform at, Geometry shape, |
308 |
PrintAttributes properties) { |
309 |
// TODO Auto-generated method stub
|
310 |
|
311 |
} |
312 |
|
313 |
public void setFixedHeight(float height) { |
314 |
_height = height; |
315 |
|
316 |
} |
317 |
|
318 |
public void setCoordinateSystemType(int cst) { |
319 |
_coordinateSystemType = cst; |
320 |
|
321 |
} |
322 |
|
323 |
public Node getRenderNode() {
|
324 |
return sm.buildSceneGraph(_geometry);
|
325 |
} |
326 |
|
327 |
public void resetGeometry() { |
328 |
// TODO Implement dispose
|
329 |
// _geometry.dispose();
|
330 |
try {
|
331 |
_geometry = new GeoPolygonList3D();
|
332 |
} catch (NodeException e) {
|
333 |
// TODO Auto-generated catch block
|
334 |
e.printStackTrace(); |
335 |
} |
336 |
} |
337 |
|
338 |
public boolean is3D() { |
339 |
// TODO Auto-generated method stub
|
340 |
return true; |
341 |
} |
342 |
|
343 |
public void setGeometry(GeoPolygonList3D polygons) { |
344 |
|
345 |
this._geometry = polygons;
|
346 |
|
347 |
} |
348 |
|
349 |
} |