Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / ext3Dgui / src / org / gvsig / gvsig3dgui / behavior / RectangleBehavior3D.java @ 29938

History | View | Annotate | Download (6.07 KB)

1 21960 jcampos
package org.gvsig.gvsig3dgui.behavior;
2 18331 jcampos
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 20900 jcampos
import org.apache.log4j.Logger;
11 29819 jtorres
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 21943 jcampos
import org.gvsig.gvsig3d.map3d.GraphicLayer3D;
16
import org.gvsig.gvsig3d.map3d.MapContext3D;
17 26998 jtorres
import org.gvsig.osgvp.core.osg.Vec3;
18 20900 jcampos
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 18331 jcampos
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 20900 jcampos
        private static Logger logger = Logger.getLogger(GraphicLayer3D.class
35
                        .getName());
36 18331 jcampos
37
        public RectangleBehavior3D(RectangleListener zili) {
38
                super(zili);
39
                // TODO Auto-generated constructor stub
40
                listener = zili;
41 20900 jcampos
                try {
42
                        rectangle = new SelectionRectangle();
43
                } catch (OSGVPException e) {
44
                        logger.error("Command: "
45
                                        + "Error creating new selection rectangle.", e);
46
                }
47
48 18331 jcampos
        }
49
50
        @Override
51
        public void mouseDragged(MouseEvent e) {
52
                // TODO Auto-generated method stub
53 20900 jcampos
                // super.mouseDragged(e);
54 18331 jcampos
                // Painting
55 20900 jcampos
                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 18331 jcampos
                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 20900 jcampos
                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 18331 jcampos
                height = mcontext.getCanvas3d().getHeight();
81 20900 jcampos
                width = mcontext.getCanvas3d().getWidth();
82
83 18331 jcampos
                if (e.getButton() == MouseEvent.BUTTON1) {
84
                        m_FirstPoint = e.getPoint();
85
                        // Put here the code to star drawing the rectangle
86 20900 jcampos
                        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 18331 jcampos
91 20900 jcampos
                        try {
92
                                rectangle.update();
93
                        } catch (OSGVPException e1) {
94
                                logger.error("Command: "
95
                                                + "Error updating rectangle selection.", e1);
96
                        }
97 18331 jcampos
                        rectangle.setType(PolygonType.EMPTY_POLYGON);
98
                }
99
        }
100
101
        @Override
102
        public void mouseReleased(MouseEvent e) throws BehaviorException {
103 20900 jcampos
104
                if (m_FirstPoint == null)
105
                        return;
106
                Point2D p1 = new Point2D.Double();
107 18331 jcampos
                Point2D p2 = new Point2D.Double();
108
                Point pScreen = e.getPoint();
109 20900 jcampos
110
                // ViewPort vp = getMapControl().getMapContext().getViewPort();
111
112
                // p1 = vp.toMapPoint(m_FirstPoint);
113
                // p2 = vp.toMapPoint(pScreen);
114 18331 jcampos
                Vec3 p1Vec = coordinatesIntersection(m_FirstPoint);
115 20900 jcampos
                p1.setLocation(p1Vec.x(), p1Vec.y());
116 18331 jcampos
                Vec3 p2Vec = coordinatesIntersection(pScreen);
117 20900 jcampos
                p2.setLocation(p2Vec.x(), p2Vec.y());
118
119 18331 jcampos
                if (e.getButton() == MouseEvent.BUTTON1) {
120 20900 jcampos
                        // Fijamos el nuevo extent
121 18331 jcampos
                        Rectangle2D.Double r = new Rectangle2D.Double();
122
                        r.setFrameFromDiagonal(p1, p2);
123 20900 jcampos
124 18331 jcampos
                        Rectangle2D rectPixel = new Rectangle();
125
                        rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen);
126 29938 jtorres
                        // TODO:COMENTADO PARA LA 2.0
127
                        // RectangleEvent event = new RectangleEvent(r, e, rectPixel);
128
                        // listener.rectangle(event);
129 18331 jcampos
                }
130 20900 jcampos
131 18331 jcampos
                // Deleted rectangle
132 20900 jcampos
                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 18331 jcampos
                rectangle.setType(PolygonType.EMPTY_POLYGON);
141 20900 jcampos
142 18331 jcampos
                m_FirstPoint = null;
143
                m_LastPoint = null;
144
        }
145
146
        @Override
147
        public void paintComponent(Graphics g) {
148
                // TODO Auto-generated method stub
149 20900 jcampos
                // super.paintComponent(g);
150 18331 jcampos
                MapContext3D mcontext = (MapContext3D) getMapControl().getMapContext();
151
                mcontext.getCanvas3d().repaint();
152
        }
153
154
        public Vec3 coordinatesIntersection(Point2D pScreen) {
155
                Vec3 intersection;
156 20900 jcampos
157 18331 jcampos
                // 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 20900 jcampos
                        if (mcontext.getProjection().getAbrev().compareToIgnoreCase(
171
                                        "EPSG:4326") == 0) {
172
                                Vec3 geoPt = mcontext.getPlanet()
173
                                                .convertXYZToLatLongHeight(hit);
174 18331 jcampos
                                // Swap the coordinates X and Y, because they are invert.
175 20900 jcampos
                                intersection = new Vec3(geoPt.y(), geoPt.x(), geoPt.z());
176 18331 jcampos
                        } else {
177
                                intersection = hit;
178
                        }
179
                } else {
180 20900 jcampos
                        if (mcontext.getProjection().getAbrev().compareToIgnoreCase(
181
                                        "EPSG:4326") == 0) {
182 18331 jcampos
                                pWorld.setLocation(360, 120);
183 20900 jcampos
                                intersection = new Vec3(360, 120, 0);
184
                        } else {
185
                                intersection = new Vec3(1e100, 1e100, 0);
186
                                // pWorld.setLocation(1e100, 1e100);
187 18331 jcampos
                        }
188
                }
189 20900 jcampos
                NotificationManager.addInfo("Obteniendo punto de informacion "
190
                                + intersection.x() + "    ,   " + intersection.y() + "  ,  "
191
                                + intersection.z());
192 18331 jcampos
                return intersection;
193
        }
194
195
}