root / branches / v2_0_0_prep / extensions / ext3Dgui / src / org / gvsig / gvsig3dgui / behavior / RectangleBehavior3D.java @ 29938
History | View | Annotate | Download (6.07 KB)
1 |
package org.gvsig.gvsig3dgui.behavior; |
---|---|
2 |
|
3 |
import java.awt.Graphics; |
4 |
import java.awt.Point; |
5 |
import java.awt.Rectangle; |
6 |
import java.awt.event.MouseEvent; |
7 |
import java.awt.geom.Point2D; |
8 |
import java.awt.geom.Rectangle2D; |
9 |
|
10 |
import org.apache.log4j.Logger; |
11 |
import org.gvsig.andami.messages.NotificationManager; |
12 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
13 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.RectangleBehavior; |
14 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.RectangleListener; |
15 |
import org.gvsig.gvsig3d.map3d.GraphicLayer3D; |
16 |
import org.gvsig.gvsig3d.map3d.MapContext3D; |
17 |
import org.gvsig.osgvp.core.osg.Vec3; |
18 |
import org.gvsig.osgvp.exceptions.OSGVPException; |
19 |
import org.gvsig.osgvp.exceptions.node.NodeException; |
20 |
import org.gvsig.osgvp.features.SelectionRectangle; |
21 |
import org.gvsig.osgvp.features.Polygon.PolygonType; |
22 |
import org.gvsig.osgvp.planets.PlanetViewer; |
23 |
import org.gvsig.osgvp.viewer.Intersections; |
24 |
|
25 |
public class RectangleBehavior3D extends RectangleBehavior { |
26 |
|
27 |
private Point2D m_FirstPoint; |
28 |
private Point2D m_LastPoint; |
29 |
private RectangleListener listener;
|
30 |
private SelectionRectangle rectangle;
|
31 |
private int height; |
32 |
private int width; |
33 |
|
34 |
private static Logger logger = Logger.getLogger(GraphicLayer3D.class |
35 |
.getName()); |
36 |
|
37 |
public RectangleBehavior3D(RectangleListener zili) {
|
38 |
super(zili);
|
39 |
// TODO Auto-generated constructor stub
|
40 |
listener = zili; |
41 |
try {
|
42 |
rectangle = new SelectionRectangle();
|
43 |
} catch (OSGVPException e) {
|
44 |
logger.error("Command: "
|
45 |
+ "Error creating new selection rectangle.", e);
|
46 |
} |
47 |
|
48 |
} |
49 |
|
50 |
@Override
|
51 |
public void mouseDragged(MouseEvent e) { |
52 |
// TODO Auto-generated method stub
|
53 |
// super.mouseDragged(e);
|
54 |
// Painting
|
55 |
rectangle.setMouseCoords((int) e.getPoint().getX(), height
|
56 |
- (int) e.getPoint().getY());
|
57 |
try {
|
58 |
rectangle.update(); |
59 |
} catch (OSGVPException e1) {
|
60 |
logger.error("Command: " + "Error updating rectangle selection.", |
61 |
e1); |
62 |
} |
63 |
|
64 |
rectangle.setType(PolygonType.FILLED_POLYGON); |
65 |
|
66 |
} |
67 |
|
68 |
@Override
|
69 |
public void mousePressed(MouseEvent e) { |
70 |
// TODO Auto-generated method stub
|
71 |
MapContext3D mcontext = (MapContext3D) getMapControl().getMapContext(); |
72 |
try {
|
73 |
((PlanetViewer) mcontext.getCanvas3d().getOSGViewer()) |
74 |
.addNodeToHUD(rectangle); |
75 |
} catch (NodeException e1) {
|
76 |
logger.error("Command: "
|
77 |
+ "Error adding the rectangle selecton to the hud node.",
|
78 |
e1); |
79 |
} |
80 |
height = mcontext.getCanvas3d().getHeight(); |
81 |
width = mcontext.getCanvas3d().getWidth(); |
82 |
|
83 |
if (e.getButton() == MouseEvent.BUTTON1) { |
84 |
m_FirstPoint = e.getPoint(); |
85 |
// Put here the code to star drawing the rectangle
|
86 |
rectangle.setFirstPoint((int) m_FirstPoint.getX(), height
|
87 |
- (int) m_FirstPoint.getY());
|
88 |
rectangle.setMouseCoords((int) m_FirstPoint.getX(), height
|
89 |
- (int) m_FirstPoint.getY());
|
90 |
|
91 |
try {
|
92 |
rectangle.update(); |
93 |
} catch (OSGVPException e1) {
|
94 |
logger.error("Command: "
|
95 |
+ "Error updating rectangle selection.", e1);
|
96 |
} |
97 |
rectangle.setType(PolygonType.EMPTY_POLYGON); |
98 |
} |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
103 |
|
104 |
if (m_FirstPoint == null) |
105 |
return;
|
106 |
Point2D p1 = new Point2D.Double(); |
107 |
Point2D p2 = new Point2D.Double(); |
108 |
Point pScreen = e.getPoint();
|
109 |
|
110 |
// ViewPort vp = getMapControl().getMapContext().getViewPort();
|
111 |
|
112 |
// p1 = vp.toMapPoint(m_FirstPoint);
|
113 |
// p2 = vp.toMapPoint(pScreen);
|
114 |
Vec3 p1Vec = coordinatesIntersection(m_FirstPoint); |
115 |
p1.setLocation(p1Vec.x(), p1Vec.y()); |
116 |
Vec3 p2Vec = coordinatesIntersection(pScreen); |
117 |
p2.setLocation(p2Vec.x(), p2Vec.y()); |
118 |
|
119 |
if (e.getButton() == MouseEvent.BUTTON1) { |
120 |
// Fijamos el nuevo extent
|
121 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
122 |
r.setFrameFromDiagonal(p1, p2); |
123 |
|
124 |
Rectangle2D rectPixel = new Rectangle(); |
125 |
rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen); |
126 |
// TODO:COMENTADO PARA LA 2.0
|
127 |
// RectangleEvent event = new RectangleEvent(r, e, rectPixel);
|
128 |
// listener.rectangle(event);
|
129 |
} |
130 |
|
131 |
// Deleted rectangle
|
132 |
rectangle.setMouseCoords((int) m_FirstPoint.getX(), height
|
133 |
- (int) m_FirstPoint.getY());
|
134 |
try {
|
135 |
rectangle.update(); |
136 |
} catch (OSGVPException e1) {
|
137 |
logger.error("Command: " + "Error updating rectangle selection.", |
138 |
e1); |
139 |
} |
140 |
rectangle.setType(PolygonType.EMPTY_POLYGON); |
141 |
|
142 |
m_FirstPoint = null;
|
143 |
m_LastPoint = null;
|
144 |
} |
145 |
|
146 |
@Override
|
147 |
public void paintComponent(Graphics g) { |
148 |
// TODO Auto-generated method stub
|
149 |
// super.paintComponent(g);
|
150 |
MapContext3D mcontext = (MapContext3D) getMapControl().getMapContext(); |
151 |
mcontext.getCanvas3d().repaint(); |
152 |
} |
153 |
|
154 |
public Vec3 coordinatesIntersection(Point2D pScreen) { |
155 |
Vec3 intersection; |
156 |
|
157 |
// System.err.println("Coordenadas de pantalla " + pScreen.getX() + ","+
|
158 |
// pScreen.getY());
|
159 |
MapContext3D mcontext = (MapContext3D) getMapControl().getMapContext(); |
160 |
Intersections hits = mcontext.getCanvas3d().getOSGViewer().rayPick( |
161 |
(int) pScreen.getX(), (int) pScreen.getY()); |
162 |
Point2D pWorld = new Point2D.Double(); |
163 |
if (hits.containsIntersections()) {
|
164 |
// get XYZ coordinates on planet
|
165 |
Vec3 hit = hits.getFirstIntersection().getIntersectionPoint(); |
166 |
// convert to geo coordinates
|
167 |
|
168 |
// System.err.println("Interseccion de osg " + hit.x() + ","+
|
169 |
// hit.y());
|
170 |
if (mcontext.getProjection().getAbrev().compareToIgnoreCase(
|
171 |
"EPSG:4326") == 0) { |
172 |
Vec3 geoPt = mcontext.getPlanet() |
173 |
.convertXYZToLatLongHeight(hit); |
174 |
// Swap the coordinates X and Y, because they are invert.
|
175 |
intersection = new Vec3(geoPt.y(), geoPt.x(), geoPt.z());
|
176 |
} else {
|
177 |
intersection = hit; |
178 |
} |
179 |
} else {
|
180 |
if (mcontext.getProjection().getAbrev().compareToIgnoreCase(
|
181 |
"EPSG:4326") == 0) { |
182 |
pWorld.setLocation(360, 120); |
183 |
intersection = new Vec3(360, 120, 0); |
184 |
} else {
|
185 |
intersection = new Vec3(1e100, 1e100, 0); |
186 |
// pWorld.setLocation(1e100, 1e100);
|
187 |
} |
188 |
} |
189 |
NotificationManager.addInfo("Obteniendo punto de informacion "
|
190 |
+ intersection.x() + " , " + intersection.y() + " , " |
191 |
+ intersection.z()); |
192 |
return intersection;
|
193 |
} |
194 |
|
195 |
} |