///<summary>根据planeviewbox和planecameraheight自动调整相机位置</summary> void planeAdjustCamera() { //计算相机位置 System.Windows.Point pnt = new System.Windows.Point(planeViewBox.Width / 2, planeViewBox.Height / 2); System.Windows.Point geopnt = geohelper.planeToGeo(pnt.ToString()); VECTOR3D vc = MapHelper.JWHToPoint(geopnt.Y, geopnt.X, Para.LineHeight, earthpara); float tmp = 1.0f + planeCameraHeight / Para.Radius; earth.camera = new Camera(new Vector3(vc.x, vc.y, vc.z) * tmp, new Vector3(0, 0, 0), Vector3.Up, earth); }
///<summary>获取相机中心与地面的交点, crosspoint为交点, distance为相机与交点距离</summary> public void getCrossGround(out VECTOR3D?crosspoint, out float?distance) { Vector3?vec3; getCrossGround(out vec3, out distance); if (vec3 == null) { crosspoint = null; } else { crosspoint = new VECTOR3D(((Vector3)vec3).X, ((Vector3)vec3).Y, ((Vector3)vec3).Z); } }
/// <summary> /// 自动调整相机距离以显示指定范围 /// </summary> public void adjustCameraRange(System.Windows.Rect rect) { double minjd, maxjd, minwd, maxwd; minwd = rect.Left; maxwd = rect.Right; minjd = rect.Top; maxjd = rect.Bottom; List <VECTOR3D> corners = new List <VECTOR3D>(); VECTOR3D lefttop, righttop, leftbottom, rightbottom, center; System.Windows.Media.Media3D.Vector3D lt, rt, lb, rb; lefttop = MapHelper.JWHToPoint(minjd, maxwd, 0, earth.earthManager.earthpara); corners.Add(lefttop); lt = Helpler.vecD3DToWpf(lefttop); righttop = MapHelper.JWHToPoint(maxjd, maxwd, 0, earth.earthManager.earthpara); corners.Add(righttop); rt = Helpler.vecD3DToWpf(righttop); leftbottom = MapHelper.JWHToPoint(minjd, minwd, 0, earth.earthManager.earthpara); corners.Add(leftbottom); lb = Helpler.vecD3DToWpf(leftbottom); rightbottom = MapHelper.JWHToPoint(maxjd, minwd, 0, earth.earthManager.earthpara); corners.Add(rightbottom); rb = Helpler.vecD3DToWpf(rightbottom); System.Windows.Media.Media3D.Vector3D cent = new System.Windows.Media.Media3D.Vector3D((corners.Max(p => p.x) + corners.Min(p => p.x)) / 2, (corners.Max(p => p.y) + corners.Min(p => p.y)) / 2, (corners.Max(p => p.z) + corners.Min(p => p.z)) / 2); if (earth.earthManager.earthpara.SceneMode == ESceneMode.地球) { cent = cent * Para.Radius / cent.Length; center = new VECTOR3D(cent.X, cent.Y, cent.Z); } else //zh注:还未验证 { center = new VECTOR3D(cent.X, cent.Y, 0); } double width, height; width = Math.Max((rt - lt).Length, (rb - lb).Length) * 1.5; height = Math.Max((lb - lt).Length, (rb - rt).Length) * 1.5; double distance; if (width / height > earth.global.ScreenWidth / earth.global.ScreenHeight) //以宽度来计算 { distance = width * earth.global.ScreenHeight / earth.global.ScreenWidth / 2 / Math.Atan(FieldOfView / 2); } else //以高度来计算 { distance = height / 2 / Math.Atan(FieldOfView / 2); } aniLook(center); adjustCameraDistance((float)distance); }
internal static extern POINT TransformD3DToScreen(int ekey, VECTOR3D d3dpoint);
public static System.Windows.Media.Media3D.Vector3D vecD3DToWpf(VECTOR3D vec) { return(new System.Windows.Media.Media3D.Vector3D(vec.x, vec.y, vec.z)); }
///<summary>动画移动相机查看指定位置,按内部三维坐标, timerFactor: 速度系数, 为0则无动画直接刷新</summary> public void aniLook(VECTOR3D vecLocation, double speedFactor = 1) { //计算当前与地面夹角 System.Windows.Media.Media3D.Vector3D v1, v2, v3, v4, v5; Vector3 vec = new Vector3(vecLocation.x, vecLocation.y, vecLocation.z); if (earth.earthManager.earthpara.SceneMode == ESceneMode.地球) { vec = vec / vec.Length() * Para.Radius; //换算到地表高度 } //当前观察点 Vector3?crosspnt; float? distance; float height; getCrossGround(out crosspnt, out distance); if (crosspnt != null) { float dis = (float)distance; Vector3 cp = (Vector3)crosspnt; if (earth.earthManager.earthpara.SceneMode == ESceneMode.地球) { v1 = new System.Windows.Media.Media3D.Vector3D(cp.X, cp.Y, cp.Z); v2 = new System.Windows.Media.Media3D.Vector3D(cameraUp.X, cameraUp.Y, cameraUp.Z); v3 = System.Windows.Media.Media3D.Vector3D.CrossProduct(v1, v2); v4 = System.Windows.Media.Media3D.Vector3D.CrossProduct(v3, v1); v5 = new System.Windows.Media.Media3D.Vector3D(cameraDirection.X, cameraDirection.Y, cameraDirection.Z); float angle = (float)System.Windows.Media.Media3D.Vector3D.AngleBetween(v5, v4); height = (new Vector3(cameraPosition.X, cameraPosition.Y, cameraPosition.Z)).Length() - Para.Radius; Vector3 dir = vec; dir.Normalize(); Vector3 axis = Vector3.Cross(dir, cameraUp); axis.Normalize(); Matrix matrix = Matrix.CreateFromAxisAngle(axis, -MathHelper.Pi * (90.0f - angle) / 180.0f); dir = Vector3.Transform(dir, matrix); dir.Normalize(); cameraLookat = vec; cameraPosition = cameraLookat + dir * dis; cameraDirection = cameraLookat - cameraPosition; cameraDirection.Normalize(); } else { Matrix matrix = Matrix.CreateTranslation(vecLocation.x - cp.X, vecLocation.y - cp.Y, 0); cameraPosition = Vector3.Transform(cameraPosition, matrix); height = cameraPosition.Z; } if (speedFactor == 0) { calCameraByDirection(); updateD3DCamera(); earth.global.isUpdate = true; } else { int time = (int)((cp - cameraLookat).Length() / height / speedFactor * 100); //时长,与高度反比,和速度系数反比 calCameraByDirection(); updateD3DCamera(true, time); } } }
///<summary>返回场景中3D点在场景平面的坐标</summary> public System.Windows.Point transformD3DToScreen(VECTOR3D point3d) { POINT p = D3DManager.TransformD3DToScreen(earth.earthkey, point3d); return(new System.Windows.Point(p.x, p.y)); }