Statistics
| Revision:

root / trunk / libraries / lib3DMap / src / com / iver / ai2 / gvsig3d / map3d / ViewPort3D.java @ 21937

History | View | Annotate | Download (7.62 KB)

1
package com.iver.ai2.gvsig3d.map3d;
2

    
3
import java.awt.Color;
4
import java.awt.event.MouseEvent;
5
import java.awt.event.MouseListener;
6
import java.awt.geom.Point2D;
7
import java.awt.geom.Rectangle2D;
8

    
9
import org.cresques.cts.IProjection;
10
import org.gvsig.gvsig3d.map3d.layers.Layer3DProps;
11
import org.gvsig.osgvp.Vec3;
12
import org.gvsig.osgvp.planets.Planet;
13
import org.gvsig.osgvp.viewer.IViewerContainer;
14
import org.gvsig.osgvp.viewer.Intersections;
15

    
16
import com.iver.andami.PluginServices;
17
import com.iver.andami.messages.NotificationManager;
18
import com.iver.andami.ui.mdiManager.IWindow;
19
import com.iver.cit.gvsig.fmap.ExtentHistory;
20
import com.iver.cit.gvsig.fmap.ViewPort;
21
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
22
import com.iver.cit.gvsig.fmap.layers.FLayer;
23
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
24
import com.iver.utiles.StringUtilities;
25
import com.iver.utiles.XMLEntity;
26

    
27
public class ViewPort3D extends ViewPort implements MouseListener {
28

    
29
        private Planet planet;
30

    
31
        private IViewerContainer canvas3d;
32

    
33
        // private Rectangle2D _extent;
34

    
35
        private boolean dirty = false;
36

    
37
        public ViewPort3D(IProjection proj) {
38
                super(proj);
39
                // TODO Auto-generated constructor stub
40
        }
41

    
42
        public Planet getPlanet() {
43
                return planet;
44
        }
45

    
46
        public void setPlanet(Planet planet) {
47
                this.planet = planet;
48
        }
49

    
50
        public void setViewer(IViewerContainer canvas) {
51
                canvas3d = canvas;
52
        }
53

    
54
        public void setDirty(boolean isDirty) {
55
                dirty = isDirty;
56
        }
57

    
58
        public boolean getDirty() {
59
                return dirty;
60
        }
61

    
62
        public Rectangle2D getAdjustedExtent() {
63
                return extent;
64
        }
65

    
66
        public void setExtent(Rectangle2D r) {
67
                extent = r;
68

    
69
                dirty = true;
70
        }
71

    
72
        /**
73
         * Returns a point in geocoordinates from window coordinates
74
         * 
75
         * @param pScreen
76
         *            Screen coordinates
77
         * 
78
         * @return point in geocoordinates
79
         * 
80
         * @throws RuntimeException
81
         */
82
        @Override
83
        public Point2D toMapPoint(Point2D pScreen) {
84
                
85
                // getting layer information
86
                float heigth= 0;
87
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
88
                if (f instanceof BaseView) {
89
                        BaseView baseView = (BaseView) f;
90
                        FLayer[] lyrs = baseView.getMapControl().getMapContext().getLayers().getActives();
91
                        FLayer layer = lyrs[lyrs.length-1];
92
                        Layer3DProps props = Layer3DProps.getLayer3DProps(layer);
93
                        heigth = props.getHeigth();
94
                        
95
                }
96
                
97

    
98
//                System.err.println("Coordenadas de pantalla " + pScreen.getX() + ","+ pScreen.getY());
99
                Intersections hits = canvas3d.getOSGViewer().rayPick(
100
                                (int) pScreen.getX(), (int) pScreen.getY());
101
                Point2D pWorld = new Point2D.Double();
102
                if (hits.containsIntersections()) {
103
                        // get XYZ coordinates on planet
104
                        Vec3 hit = hits.getFirstIntersection().getIntersectionPoint();
105
                        // convert to geo coordinates
106

    
107
                        // System.err.println("Interseccion de osg " + hit.x() + ","+
108
                        // hit.y());
109
                        if (getProjection().getAbrev().compareToIgnoreCase("EPSG:4326") == 0) {
110
                                Vec3 geoPt = planet.convertXYZToLatLongHeight(hit);
111

    
112
                                if (Math.abs(heigth - geoPt.z()) <= 1000) {
113
                                        pWorld.setLocation(geoPt.y(), geoPt.x());
114
                                } else {
115
                                        pWorld.setLocation(360, 120);
116
                                }
117
                                NotificationManager.addInfo("Obteniendo punto de informacion "
118
                                                + pWorld.getX() + "    ,   " + pWorld.getY());
119
                        } else {
120
                                if (Math.abs(heigth - hit.z()) <= 100) {
121
                                        pWorld.setLocation(hit.x(), hit.y());
122
                                } else {
123
                                        pWorld.setLocation(360, 120);
124
                                }
125
                                NotificationManager.addInfo("Obteniendo punto de informacion "
126
                                                + pWorld.getX() + "    ,   " + pWorld.getY());
127
                        }
128
                } else {
129
                        if (getProjection().getAbrev().compareToIgnoreCase("EPSG:4326") == 0) {
130
                                pWorld.setLocation(360, 120);
131
                        } else
132
                                pWorld.setLocation(1e100, 1e100);
133
                }
134

    
135
                return pWorld;
136
        }
137

    
138
        /**
139
         * Returns geographic distance from pixel distance
140
         * 
141
         * @param d
142
         *            Pixel distance
143
         * 
144
         * @return geographic distance
145
         */
146
        public double toMapDistance(int d) {
147
                // double dist = d / trans.getScaleX();
148

    
149
                double zoom = planet.getZoom(); // distance to center in meters
150

    
151
                return 0.1; // TEST
152
        }
153

    
154
        public void mouseClicked(MouseEvent e) {
155
                // TODO Auto-generated method stub
156

    
157
        }
158

    
159
        public void mouseEntered(MouseEvent e) {
160
                // TODO Auto-generated method stub
161

    
162
        }
163

    
164
        public void mouseExited(MouseEvent e) {
165
                // TODO Auto-generated method stub
166

    
167
        }
168

    
169
        public void mousePressed(MouseEvent e) {
170
                // TODO Auto-generated method stub
171

    
172
        }
173

    
174
        /*
175
         * (non-Javadoc)
176
         * 
177
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
178
         */
179
        public void mouseReleased(MouseEvent e) {
180
                // Center point of locator
181
                Point2D center = null;
182
                // Scale factor
183
                double factor = 0;
184
                if (planet != null) {
185
                        // Getting center point longitude and latitude
186
                        center = new Point2D.Double((planet.getLongitude()), (planet
187
                                        .getLatitude()));
188
                        // Getting distance
189
                        double elevation = planet.getAltitude();
190
                        factor = (elevation * 0.000005);
191

    
192
                }
193
                // View3D aspect radio
194
                double aspect = (double) canvas3d.getWidth() / canvas3d.getHeight();
195
                // Calculate new width & height
196
                double width = factor * aspect * 2;
197
                double height = (factor / aspect) * 2;
198

    
199
                height = height > 180 ? 180 : height;
200
                width = width > 360 ? 360 : height;
201
                // New extent
202
                Rectangle2D extend = new Rectangle2D.Double(center.getX() - width / 2,
203
                                center.getY() - height / 2, width, height);
204
                extent = extend;
205
                // Locator Refresh
206
                refreshExtent();
207
        }
208

    
209
        public void refreshExtent() {
210

    
211
                // Calling extent changed to locator
212
                super.callExtentChanged(extent);
213
        }
214

    
215
        /**
216
         * Crea un nuevo ViewPort a partir del XMLEntity.
217
         * 
218
         * @param xml
219
         *            XMLEntity.
220
         * 
221
         * @return Nuevo ViewPort.
222
         */
223
        public static ViewPort createFromXML(XMLEntity xml) {
224
                // A BETTER WAY TO DO THIS SHOULD BE TO SEPARATE CONSTRUCTOR CALL FROM
225
                // XML READING
226
                // SO SUPER'S XML READING COULD BE USED HERE
227

    
228
                ViewPort3D vp = new ViewPort3D(null);
229

    
230
                if (xml.contains("adjustedExtentX")) {
231
                        vp.adjustedExtent = new Rectangle2D.Double(xml
232
                                        .getDoubleProperty("adjustedExtentX"), xml
233
                                        .getDoubleProperty("adjustedExtentY"), xml
234
                                        .getDoubleProperty("adjustedExtentW"), xml
235
                                        .getDoubleProperty("adjustedExtentH"));
236
                }
237

    
238
                if (xml.contains("backColor")) {
239
                        vp.setBackColor(StringUtilities.string2Color(xml
240
                                        .getStringProperty("backColor")));
241
                } else {
242
                        vp.setBackColor(Color.white);
243
                }
244

    
245
                if (xml.contains("clipX")) {
246
                        vp.setClipRect( new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
247
                                        xml.getDoubleProperty("clipY"), xml
248
                                                        .getDoubleProperty("clipW"), xml
249
                                                        .getDoubleProperty("clipH")));
250
                }
251

    
252
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
253
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
254
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
255
                vp.extents = ExtentHistory.createFromXML(xml.getChild(0));
256

    
257
                if (xml.contains("extentX")) {
258
                        vp.setExtent(new Rectangle2D.Double(xml
259
                                        .getDoubleProperty("extentX"), xml
260
                                        .getDoubleProperty("extentY"), xml
261
                                        .getDoubleProperty("extentW"), xml
262
                                        .getDoubleProperty("extentH")));
263

    
264
                        // Calcula la transformaci?n af?n
265
                        // vp.calculateAffineTransform();
266

    
267
                        // Lanzamos los eventos de extent cambiado
268
                        // vp.callExtentListeners(vp.adjustedExtent);
269
                }
270

    
271
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
272
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"), xml
273
                                .getDoubleProperty("offsetY")));
274

    
275
                if (xml.contains("proj")) {
276
                        vp.setProjection(CRSFactory.getCRS(xml.getStringProperty("proj")));
277
                }
278

    
279
                // vp.setScale(xml.getDoubleProperty("scale"));
280
                vp.refreshExtent();
281
                return vp;
282
        }
283
}