Statistics
| Revision:

gvsig-3d / 1.10 / trunk / libraries / lib3DMap / src / org / gvsig / gvsig3d / map3d / ViewPort3D.java @ 76

History | View | Annotate | Download (8.44 KB)

1 5 jzarzoso
package org.gvsig.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 9 jzarzoso
import org.gvsig.osgvp.core.osg.Matrix;
11 5 jzarzoso
import org.gvsig.osgvp.core.osg.Vec3;
12
import org.gvsig.osgvp.terrain.Terrain;
13
import org.gvsig.osgvp.viewer.IViewerContainer;
14
import org.gvsig.osgvp.viewer.Intersections;
15
16
import com.iver.ai2.gvsig3d.map3d.layers.Layer3DProps;
17
import com.iver.andami.PluginServices;
18
import com.iver.andami.messages.NotificationManager;
19
import com.iver.andami.ui.mdiManager.IWindow;
20
import com.iver.cit.gvsig.fmap.ExtentHistory;
21
import com.iver.cit.gvsig.fmap.ViewPort;
22
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
23
import com.iver.cit.gvsig.fmap.layers.FLayer;
24
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
25
import com.iver.utiles.StringUtilities;
26
import com.iver.utiles.XMLEntity;
27
28
public class ViewPort3D extends ViewPort implements MouseListener {
29
30
        private Terrain                         _terrain;
31
        private IViewerContainer         _canvas3d;
32
        private boolean                         _dirty = false;
33
34
        public ViewPort3D(IProjection proj) {
35
                super(proj);
36 9 jzarzoso
                setBackColor(Color.WHITE);
37 5 jzarzoso
                // TODO Auto-generated constructor stub
38
        }
39
40
        public Terrain getTerrain() {
41
                return _terrain;
42
        }
43
44
        public void setTerrain(Terrain terrain) {
45
                _terrain = terrain;
46
        }
47
48
        public void setViewer(IViewerContainer canvas) {
49
                _canvas3d = canvas;
50
        }
51
52
        public void setDirty(boolean isDirty) {
53
                _dirty = isDirty;
54
        }
55
56
        public boolean getDirty() {
57
                return _dirty;
58
        }
59
60
        public Rectangle2D getAdjustedExtent() {
61
                return extent;
62
        }
63
64
        public void setExtent(Rectangle2D r) {
65
                extent = r;
66
67
                _dirty = true;
68
        }
69
70 9 jzarzoso
        @Override
71
        public Point2D fromMapPoint(double x, double y) {
72
73
74
                Matrix viewM = _canvas3d.getOSGViewer().getCamera().getViewMatrix();
75
                Matrix projM = _canvas3d.getOSGViewer().getCamera().getProjectionMatrix();
76
77
                Vec3 geoPt;
78
79
                if (getProjection().getAbrev().compareToIgnoreCase("EPSG:4326") == 0) {
80
                        geoPt = _terrain.convertLatLongHeightToXYZ(new Vec3(x,y,0.0));
81
82
83
                } else {
84
                        geoPt = new Vec3(x,y,0);
85
                }
86
87
                Matrix finalM = viewM.prod(projM);
88
89
                Vec3 projP = finalM.prod(geoPt, finalM);
90
91
                Vec3 windowP = new Vec3();
92
                windowP.setX((projP.x() + 1) * (_canvas3d.getWidth()* 0.5));
93
                windowP.setY((projP.y() + 1) * (_canvas3d.getHeight()*0.5));
94
95
                System.out.println(windowP.x() + " " + windowP.y());
96
97
98
                return new Point2D.Double(windowP.x(),windowP.y());
99
        }
100
101 5 jzarzoso
        /**
102
         * Returns a point in geocoordinates from window coordinates
103
         *
104
         * @param pScreen
105
         *            Screen coordinates
106
         *
107
         * @return point in geocoordinates
108
         *
109
         * @throws RuntimeException
110
         */
111
        @Override
112
        public Point2D toMapPoint(Point2D pScreen) {
113 9 jzarzoso
114 5 jzarzoso
                // getting layer information
115
                float heigth= 0;
116
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
117
                if (f instanceof BaseView) {
118
                        BaseView baseView = (BaseView) f;
119
                        FLayer[] lyrs = baseView.getMapControl().getMapContext().getLayers().getActives();
120
                        if(lyrs != null && lyrs.length > 0) {
121
                                FLayer layer = lyrs[lyrs.length-1];
122
                                Layer3DProps props = Layer3DProps.getLayer3DProps(layer);
123
                                if(props != null)
124
                                        heigth = props.getHeigth();
125
                        }
126 9 jzarzoso
127 5 jzarzoso
                }
128 9 jzarzoso
                //                System.err.println("Coordenadas de pantalla " + pScreen.getX() + ","+ pScreen.getY());
129 5 jzarzoso
                Intersections hits = _canvas3d.getOSGViewer().rayPick(
130
                                (int) pScreen.getX(), (int) pScreen.getY());
131
                Point2D pWorld = new Point2D.Double();
132
                if (hits.containsIntersections()) {
133
                        // get XYZ coordinates on terrain
134
                        Vec3 hit = hits.getFirstIntersection().getIntersectionPoint();
135
                        // convert to geo coordinates
136
                        if (getProjection().getAbrev().compareToIgnoreCase("EPSG:4326") == 0) {
137
                                Vec3 geoPt = _terrain.convertXYZToLatLongHeight(hit);
138
139 40 jzarzoso
                                //if (Math.abs(heigth - geoPt.z()) <= 1000) {
140 5 jzarzoso
                                        pWorld.setLocation(geoPt.y(), geoPt.x());
141 40 jzarzoso
//                                } else {
142
//                                        pWorld.setLocation(360, 120);
143
//                                }
144
                                NotificationManager.addInfo(PluginServices.getText(this, "ObtainPoint")
145
                                                + " " + pWorld.getX() + " , " + pWorld.getY());
146 5 jzarzoso
                        } else {
147 40 jzarzoso
                                //if (Math.abs(heigth - hit.z()) <= 100) {
148 5 jzarzoso
                                        pWorld.setLocation(hit.x(), hit.y());
149 40 jzarzoso
//                                } else {
150
//                                        pWorld.setLocation(360, 120);
151
//                                }
152
                                NotificationManager.addInfo(PluginServices.getText(this, "ObtainPoint")
153
                                                + " " + pWorld.getX() + " , " + pWorld.getY());
154 5 jzarzoso
                        }
155
                } else {
156
                        if (getProjection().getAbrev().compareToIgnoreCase("EPSG:4326") == 0) {
157
                                pWorld.setLocation(360, 120);
158
                        } else
159
                                pWorld.setLocation(1e100, 1e100);
160
                }
161
162
                return pWorld;
163
        }
164 76 mten
165
        public Point2D toMapPoint(int x, int y)
166
        {
167
                return toMapPoint(new Point2D.Double(x,y));
168
        }
169
170 5 jzarzoso
171
        /**
172
         * Returns geographic distance from pixel distance
173 76 mten
          * @param d distance in pixels
174
         *
175
         * @return distance equivalent in <i>map coordinates</i>
176 5 jzarzoso
         */
177 76 mten
        public double toMapDistance(int d) {
178
                Point2D p1 = toMapPoint(0, 0);
179
                Point2D p2 = toMapPoint(1, 0);
180
181
                double scaleX = p2.distance(p1) ;
182
183
                double dist = d * scaleX;
184
185
                if(dist <= 0) dist = 0.1;
186
187
                return  dist;
188 5 jzarzoso
        }
189
190
        public void mouseClicked(MouseEvent e) {
191
                // TODO Auto-generated method stub
192
193
        }
194
195
        public void mouseEntered(MouseEvent e) {
196
                // TODO Auto-generated method stub
197
198
        }
199
200
        public void mouseExited(MouseEvent e) {
201
                // TODO Auto-generated method stub
202
203
        }
204
205
        public void mousePressed(MouseEvent e) {
206
                // TODO Auto-generated method stub
207
208
        }
209
210
        /*
211
         * (non-Javadoc)
212
         *
213
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
214
         */
215
        public void mouseReleased(MouseEvent e) {
216
                // Center point of locator
217 36 jzarzoso
218 5 jzarzoso
                Point2D center = null;
219
                // Scale factor
220
                double factor = 0;
221
                if (_terrain != null) {
222
                        // Getting center point longitude and latitude
223
                        center = new Point2D.Double((_terrain.getLongitude()), (_terrain
224
                                        .getLatitude()));
225
                        // Getting distance
226
                        double elevation = _terrain.getAltitude();
227
                        factor = (elevation * 0.000005);
228
229
                }
230
                // View3D aspect radio
231
                double aspect = (double) _canvas3d.getWidth() / _canvas3d.getHeight();
232
                // Calculate new width & height
233
                double width = factor * aspect * 2;
234
                double height = (factor / aspect) * 2;
235
236
                height = height > 180 ? 180 : height;
237
                width = width > 360 ? 360 : height;
238
                // New extent
239
                Rectangle2D extend = new Rectangle2D.Double(center.getX() - width / 2,
240
                                center.getY() - height / 2, width, height);
241
                extent = extend;
242
                // Locator Refresh
243 36 jzarzoso
                //refreshExtent();
244
245 5 jzarzoso
        }
246
247
        public void refreshExtent() {
248
249
                // Calling extent changed to locator
250
                super.callExtentChanged(extent);
251
        }
252
253
        /**
254
         * Crea un nuevo ViewPort a partir del XMLEntity.
255
         *
256
         * @param xml
257
         *            XMLEntity.
258
         *
259
         * @return Nuevo ViewPort.
260
         */
261
        public static ViewPort createFromXML(XMLEntity xml) {
262
                // A BETTER WAY TO DO THIS SHOULD BE TO SEPARATE CONSTRUCTOR CALL FROM
263
                // XML READING
264
                // SO SUPER'S XML READING COULD BE USED HERE
265
266
                ViewPort3D vp = new ViewPort3D(null);
267
268
                if (xml.contains("adjustedExtentX")) {
269
                        vp.adjustedExtent = new Rectangle2D.Double(xml
270
                                        .getDoubleProperty("adjustedExtentX"), xml
271
                                        .getDoubleProperty("adjustedExtentY"), xml
272
                                        .getDoubleProperty("adjustedExtentW"), xml
273
                                        .getDoubleProperty("adjustedExtentH"));
274
                }
275
276
                if (xml.contains("backColor")) {
277
                        vp.setBackColor(StringUtilities.string2Color(xml
278
                                        .getStringProperty("backColor")));
279
                } else {
280
                        vp.setBackColor(Color.white);
281
                }
282
283
                if (xml.contains("clipX")) {
284
                        vp.setClipRect( new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
285
                                        xml.getDoubleProperty("clipY"), xml
286 9 jzarzoso
                                        .getDoubleProperty("clipW"), xml
287
                                        .getDoubleProperty("clipH")));
288 5 jzarzoso
                }
289
290
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
291
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
292
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
293
                vp.extents = ExtentHistory.createFromXML(xml.getChild(0));
294
295
                if (xml.contains("extentX")) {
296
                        vp.setExtent(new Rectangle2D.Double(xml
297
                                        .getDoubleProperty("extentX"), xml
298
                                        .getDoubleProperty("extentY"), xml
299
                                        .getDoubleProperty("extentW"), xml
300
                                        .getDoubleProperty("extentH")));
301
                }
302
303
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
304
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"), xml
305
                                .getDoubleProperty("offsetY")));
306
307
                if (xml.contains("proj")) {
308
                        vp.setProjection(CRSFactory.getCRS(xml.getStringProperty("proj")));
309
                }
310
                vp.refreshExtent();
311
                return vp;
312
        }
313
}