Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / lib3DMap / src / com / iver / ai2 / gvsig3d / map3d / ViewPort3D.java @ 18268

History | View | Annotate | Download (7.63 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

    
11
import com.iver.ai2.gvsig3d.map3d.layers.Layer3DProps;
12
import com.iver.andami.PluginServices;
13
import com.iver.andami.messages.NotificationManager;
14
import com.iver.andami.ui.mdiManager.IWindow;
15
import com.iver.cit.gvsig.fmap.ExtentHistory;
16
import com.iver.cit.gvsig.fmap.ViewPort;
17
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
18
import com.iver.cit.gvsig.fmap.layers.FLayer;
19
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
20
import com.iver.utiles.StringUtilities;
21
import com.iver.utiles.XMLEntity;
22

    
23
import es.upv.ai2.osgvp.Vec3;
24
import es.upv.ai2.osgvp.planets.Planet;
25
import es.upv.ai2.osgvp.viewer.IViewerContainer;
26
import es.upv.ai2.osgvp.viewer.Intersections;
27

    
28
public class ViewPort3D extends ViewPort implements MouseListener {
29

    
30
        private Planet planet;
31

    
32
        private IViewerContainer canvas3d;
33

    
34
        // private Rectangle2D _extent;
35

    
36
        private boolean dirty = false;
37

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

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

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

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

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

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

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

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

    
70
                dirty = true;
71
        }
72

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

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

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

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

    
136
                return pWorld;
137
        }
138

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

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

    
152
                return 0.1; // TEST
153
        }
154

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

    
158
        }
159

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

    
163
        }
164

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

    
168
        }
169

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

    
173
        }
174

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

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

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

    
210
        public void refreshExtent() {
211

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

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

    
229
                ViewPort3D vp = new ViewPort3D(null);
230

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

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

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

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

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

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

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

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

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

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