Statistics
| Revision:

svn-gvsig-desktop / import / ext3D / branches / ext3D_v1.1 / ext3D / src / com / iver / ai2 / gvsig3d / map3d / ViewPort3D.java @ 15441

History | View | Annotate | Download (6.42 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.cit.gvsig.fmap.ExtentHistory;
12
import com.iver.cit.gvsig.fmap.ViewPort;
13
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
14
import com.iver.utiles.StringUtilities;
15
import com.iver.utiles.XMLEntity;
16

    
17
import es.upv.ai2.libjosg.Node;
18
import es.upv.ai2.libjosg.Vec3;
19
import es.upv.ai2.libjosg.planets.Planet;
20
import es.upv.ai2.libjosg.viewer.IViewerContainer;
21
import es.upv.ai2.libjosg.viewer.Intersections;
22

    
23
public class ViewPort3D extends ViewPort implements MouseListener {
24

    
25
        private Planet planet;
26

    
27
        private IViewerContainer canvas3d;
28

    
29
        // private Rectangle2D _extent;
30

    
31
        private boolean dirty = false;
32

    
33
        public ViewPort3D(IProjection proj) {
34
                super(proj);
35
                // TODO Auto-generated constructor stub
36
        }
37

    
38
        public Planet getPlanet() {
39
                return planet;
40
        }
41

    
42
        public void setPlanet(Planet planet) {
43
                this.planet = planet;
44
        }
45

    
46
        public void setViewer(IViewerContainer canvas) {
47
                canvas3d = canvas;
48
        }
49

    
50
        public void setDirty(boolean isDirty) {
51
                dirty = isDirty;
52
        }
53

    
54
        public boolean getDirty() {
55
                return dirty;
56
        }
57

    
58
        public Rectangle2D getAdjustedExtent() {
59
                return extent;
60
        }
61

    
62
        public void setExtent(Rectangle2D r) {
63
                extent = r;
64

    
65
                dirty = true;
66
        }
67

    
68
        /**
69
         * Returns a point in geocoordinates from window coordinates
70
         * 
71
         * @param pScreen
72
         *            Screen coordinates
73
         * 
74
         * @return point in geocoordinates
75
         * 
76
         * @throws RuntimeException
77
         */
78
        public Point2D toMapPoint(Point2D pScreen) {
79
                int x = (int) pScreen.getX();
80
                int y = this.getImageHeight() - (int) pScreen.getY() - 1;
81

    
82
                Intersections hits = canvas3d.getOSGViewer().rayPick(x, y);
83
                Point2D pWorld = new Point2D.Double();
84
                if (hits.containsIntersections()) {
85
                        // get XYZ coordinates on planet
86
                        Vec3 hit = hits.getFirstIntersection().getIntersectionPoint();
87
                        // convert to geo coordinates
88
                        
89
                        if (proj.getAbrev().compareToIgnoreCase("EPSG:4326") == 0) {
90
                                Vec3 geoPt = planet.convertXYZToLatLongHeight(new Vec3(
91
                                                (double) hit.x(), (double) hit.y(), (double) hit.x()));
92
                                pWorld.setLocation(geoPt.y(), geoPt.x());
93
                        } else
94
                                pWorld.setLocation((double) hit.x(), (double) hit.y());
95
                } else {
96
                        if (proj.getAbrev().compareToIgnoreCase("EPSG:4326") == 0) {
97
                                pWorld.setLocation(360, 120);
98
                        } else
99
                                pWorld.setLocation(1e100, 1e100);
100
                }
101

    
102
                return pWorld;
103
        }
104

    
105
        /**
106
         * Returns geographic distance from pixel distance
107
         * 
108
         * @param d
109
         *            Pixel distance
110
         * 
111
         * @return geographic distance
112
         */
113
        public double toMapDistance(int d) {
114
                // double dist = d / trans.getScaleX();
115

    
116
                double zoom = planet.getZoom(); // distance to center in meters
117

    
118
                return 0.1; // TEST
119
        }
120

    
121
        public void mouseClicked(MouseEvent e) {
122
                // TODO Auto-generated method stub
123

    
124
        }
125

    
126
        public void mouseEntered(MouseEvent e) {
127
                // TODO Auto-generated method stub
128

    
129
        }
130

    
131
        public void mouseExited(MouseEvent e) {
132
                // TODO Auto-generated method stub
133

    
134
        }
135

    
136
        public void mousePressed(MouseEvent e) {
137
                // TODO Auto-generated method stub
138

    
139
        }
140

    
141
        /*
142
         * (non-Javadoc)
143
         * 
144
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
145
         */
146
        public void mouseReleased(MouseEvent e) {
147
                // Center point of locator
148
                Point2D center = null;
149
                // Scale factor
150
                double factor = 0;
151
                if (planet != null) {
152
                        // Getting center point longitude and latitude
153
                        center = new Point2D.Double((planet.getLongitude()), (planet
154
                                        .getLatitude()));
155
                        // Getting distance
156
                        double elevation = planet.getAltitude();
157
                        factor = (elevation * 0.000005);
158

    
159
                }
160
                // View3D aspect radio
161
                double aspect = (double) canvas3d.getWidth() / canvas3d.getHeight();
162
                // Calculate new width & height
163
                double width = factor * aspect * 2;
164
                double height = (factor / aspect) * 2;
165

    
166
                height = height > 180 ? 180 : height;
167
                width = width > 360 ? 360 : height;
168
                // New extent
169
                Rectangle2D extend = new Rectangle2D.Double(center.getX() - width / 2,
170
                                center.getY() - height / 2, width, height);
171
                extent = extend;
172
                // Locator Refresh
173
                refreshExtent();
174
        }
175

    
176
        public void refreshExtent() {
177

    
178
                // Calling extent changed to locator
179
                super.callExtentChanged(extent);
180
        }
181

    
182
        /**
183
         * Crea un nuevo ViewPort a partir del XMLEntity.
184
         * 
185
         * @param xml
186
         *            XMLEntity.
187
         * 
188
         * @return Nuevo ViewPort.
189
         */
190
        public static ViewPort createFromXML(XMLEntity xml) {
191
                // A BETTER WAY TO DO THIS SHOULD BE TO SEPARATE CONSTRUCTOR CALL FROM
192
                // XML READING
193
                // SO SUPER'S XML READING COULD BE USED HERE
194

    
195
                ViewPort3D vp = new ViewPort3D(null);
196

    
197
                if (xml.contains("adjustedExtentX")) {
198
                        vp.adjustedExtent = new Rectangle2D.Double(xml
199
                                        .getDoubleProperty("adjustedExtentX"), xml
200
                                        .getDoubleProperty("adjustedExtentY"), xml
201
                                        .getDoubleProperty("adjustedExtentW"), xml
202
                                        .getDoubleProperty("adjustedExtentH"));
203
                }
204

    
205
                if (xml.contains("backColor")) {
206
                        vp.setBackColor(StringUtilities.string2Color(xml
207
                                        .getStringProperty("backColor")));
208
                } else {
209
                        vp.setBackColor(Color.white);
210
                }
211

    
212
                if (xml.contains("clipX")) {
213
                        vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
214
                                        xml.getDoubleProperty("clipY"), xml
215
                                                        .getDoubleProperty("clipW"), xml
216
                                                        .getDoubleProperty("clipH"));
217
                }
218

    
219
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
220
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
221
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
222
                vp.extents = ExtentHistory.createFromXML(xml.getChild(0));
223

    
224
                if (xml.contains("extentX")) {
225
                        vp.setExtent(new Rectangle2D.Double(xml
226
                                        .getDoubleProperty("extentX"), xml
227
                                        .getDoubleProperty("extentY"), xml
228
                                        .getDoubleProperty("extentW"), xml
229
                                        .getDoubleProperty("extentH")));
230

    
231
                        // Calcula la transformaci?n af?n
232
                        // vp.calculateAffineTransform();
233

    
234
                        // Lanzamos los eventos de extent cambiado
235
                        // vp.callExtentListeners(vp.adjustedExtent);
236
                }
237

    
238
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
239
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"), xml
240
                                .getDoubleProperty("offsetY")));
241

    
242
                if (xml.contains("proj")) {
243
                        vp.proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
244
                }
245

    
246
                // vp.setScale(xml.getDoubleProperty("scale"));
247
                vp.refreshExtent();
248
                return vp;
249
        }
250
}