gvsig-3d / 1.10 / trunk / libraries / lib3DMap / src / org / gvsig / gvsig3d / map3d / ViewPort3D.java @ 36
History | View | Annotate | Download (8.21 KB)
1 |
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 |
import org.gvsig.osgvp.core.osg.Matrix; |
11 |
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 |
setBackColor(Color.WHITE);
|
37 |
// 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 |
@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 |
/**
|
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 |
|
114 |
// 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 |
|
127 |
} |
128 |
|
129 |
|
130 |
// System.err.println("Coordenadas de pantalla " + pScreen.getX() + ","+ pScreen.getY());
|
131 |
Intersections hits = _canvas3d.getOSGViewer().rayPick( |
132 |
(int) pScreen.getX(), (int) pScreen.getY()); |
133 |
Point2D pWorld = new Point2D.Double(); |
134 |
if (hits.containsIntersections()) {
|
135 |
// get XYZ coordinates on terrain
|
136 |
Vec3 hit = hits.getFirstIntersection().getIntersectionPoint(); |
137 |
// convert to geo coordinates
|
138 |
if (getProjection().getAbrev().compareToIgnoreCase("EPSG:4326") == 0) { |
139 |
Vec3 geoPt = _terrain.convertXYZToLatLongHeight(hit); |
140 |
|
141 |
if (Math.abs(heigth - geoPt.z()) <= 1000) { |
142 |
pWorld.setLocation(geoPt.y(), geoPt.x()); |
143 |
} else {
|
144 |
pWorld.setLocation(360, 120); |
145 |
} |
146 |
NotificationManager.addInfo("Obteniendo punto de informacion "
|
147 |
+ pWorld.getX() + " , " + pWorld.getY());
|
148 |
} else {
|
149 |
if (Math.abs(heigth - hit.z()) <= 100) { |
150 |
pWorld.setLocation(hit.x(), hit.y()); |
151 |
} else {
|
152 |
pWorld.setLocation(360, 120); |
153 |
} |
154 |
NotificationManager.addInfo("Obteniendo punto de informacion "
|
155 |
+ pWorld.getX() + " , " + pWorld.getY());
|
156 |
} |
157 |
} else {
|
158 |
if (getProjection().getAbrev().compareToIgnoreCase("EPSG:4326") == 0) { |
159 |
pWorld.setLocation(360, 120); |
160 |
} else
|
161 |
pWorld.setLocation(1e100, 1e100); |
162 |
} |
163 |
|
164 |
return pWorld;
|
165 |
} |
166 |
|
167 |
/**
|
168 |
* Returns geographic distance from pixel distance
|
169 |
*
|
170 |
* @param d
|
171 |
* Pixel distance
|
172 |
*
|
173 |
* @return geographic distance
|
174 |
*/
|
175 |
public double toMapDistance(int d) { |
176 |
double zoom = _terrain.getZoom(); // distance to center in meters |
177 |
|
178 |
return zoom; // TEST |
179 |
} |
180 |
|
181 |
public void mouseClicked(MouseEvent e) { |
182 |
// TODO Auto-generated method stub
|
183 |
|
184 |
} |
185 |
|
186 |
public void mouseEntered(MouseEvent e) { |
187 |
// TODO Auto-generated method stub
|
188 |
|
189 |
} |
190 |
|
191 |
public void mouseExited(MouseEvent e) { |
192 |
// TODO Auto-generated method stub
|
193 |
|
194 |
} |
195 |
|
196 |
public void mousePressed(MouseEvent e) { |
197 |
// TODO Auto-generated method stub
|
198 |
|
199 |
} |
200 |
|
201 |
/*
|
202 |
* (non-Javadoc)
|
203 |
*
|
204 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
205 |
*/
|
206 |
public void mouseReleased(MouseEvent e) { |
207 |
// Center point of locator
|
208 |
|
209 |
Point2D center = null; |
210 |
// Scale factor
|
211 |
double factor = 0; |
212 |
if (_terrain != null) { |
213 |
// Getting center point longitude and latitude
|
214 |
center = new Point2D.Double((_terrain.getLongitude()), (_terrain |
215 |
.getLatitude())); |
216 |
// Getting distance
|
217 |
double elevation = _terrain.getAltitude();
|
218 |
factor = (elevation * 0.000005);
|
219 |
|
220 |
} |
221 |
// View3D aspect radio
|
222 |
double aspect = (double) _canvas3d.getWidth() / _canvas3d.getHeight(); |
223 |
// Calculate new width & height
|
224 |
double width = factor * aspect * 2; |
225 |
double height = (factor / aspect) * 2; |
226 |
|
227 |
height = height > 180 ? 180 : height; |
228 |
width = width > 360 ? 360 : height; |
229 |
// New extent
|
230 |
Rectangle2D extend = new Rectangle2D.Double(center.getX() - width / 2, |
231 |
center.getY() - height / 2, width, height);
|
232 |
extent = extend; |
233 |
// Locator Refresh
|
234 |
//refreshExtent();
|
235 |
|
236 |
} |
237 |
|
238 |
public void refreshExtent() { |
239 |
|
240 |
// Calling extent changed to locator
|
241 |
super.callExtentChanged(extent);
|
242 |
} |
243 |
|
244 |
/**
|
245 |
* Crea un nuevo ViewPort a partir del XMLEntity.
|
246 |
*
|
247 |
* @param xml
|
248 |
* XMLEntity.
|
249 |
*
|
250 |
* @return Nuevo ViewPort.
|
251 |
*/
|
252 |
public static ViewPort createFromXML(XMLEntity xml) { |
253 |
// A BETTER WAY TO DO THIS SHOULD BE TO SEPARATE CONSTRUCTOR CALL FROM
|
254 |
// XML READING
|
255 |
// SO SUPER'S XML READING COULD BE USED HERE
|
256 |
|
257 |
ViewPort3D vp = new ViewPort3D(null); |
258 |
|
259 |
if (xml.contains("adjustedExtentX")) { |
260 |
vp.adjustedExtent = new Rectangle2D.Double(xml |
261 |
.getDoubleProperty("adjustedExtentX"), xml
|
262 |
.getDoubleProperty("adjustedExtentY"), xml
|
263 |
.getDoubleProperty("adjustedExtentW"), xml
|
264 |
.getDoubleProperty("adjustedExtentH"));
|
265 |
} |
266 |
|
267 |
if (xml.contains("backColor")) { |
268 |
vp.setBackColor(StringUtilities.string2Color(xml |
269 |
.getStringProperty("backColor")));
|
270 |
} else {
|
271 |
vp.setBackColor(Color.white);
|
272 |
} |
273 |
|
274 |
if (xml.contains("clipX")) { |
275 |
vp.setClipRect( new Rectangle2D.Double(xml.getDoubleProperty("clipX"), |
276 |
xml.getDoubleProperty("clipY"), xml
|
277 |
.getDoubleProperty("clipW"), xml
|
278 |
.getDoubleProperty("clipH")));
|
279 |
} |
280 |
|
281 |
vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
|
282 |
vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
|
283 |
vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
|
284 |
vp.extents = ExtentHistory.createFromXML(xml.getChild(0));
|
285 |
|
286 |
if (xml.contains("extentX")) { |
287 |
vp.setExtent(new Rectangle2D.Double(xml |
288 |
.getDoubleProperty("extentX"), xml
|
289 |
.getDoubleProperty("extentY"), xml
|
290 |
.getDoubleProperty("extentW"), xml
|
291 |
.getDoubleProperty("extentH")));
|
292 |
} |
293 |
|
294 |
vp.setMapUnits(xml.getIntProperty("mapUnits"));
|
295 |
vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"), xml |
296 |
.getDoubleProperty("offsetY")));
|
297 |
|
298 |
if (xml.contains("proj")) { |
299 |
vp.setProjection(CRSFactory.getCRS(xml.getStringProperty("proj")));
|
300 |
} |
301 |
vp.refreshExtent(); |
302 |
return vp;
|
303 |
} |
304 |
} |