三维是ArcGIS Runtime Quartz版本的新功能,截至2017年7月,三维功能正式覆盖Android、IOS、Windows三大平台。三维场景的构建、三维内容的加载,在之前的博客里面也有提到,像第二篇博客中提到的构建第一个ArcGIS Runtime WPF应用程序即搭建的三维应用程序。
我们来根据Runtime SDK中提供的两个重要类以及若干重要属性来认识Runtime的三维。
相机类-Camera
在SceneView中,有个Camera类型的属性,这个Camera对象控制着程序展现给用户的内容。暂且可以称它为主相机,主相机观察到的内容,就是SceneView呈现的内容。
Camera类有三个构造函数,最重要的两个构造函数是这样的。public Camera(MapPoint locationPoint, double heading, double pitch, double roll);其中,locationPoint是相机在三维空间中的位置,由x,y,z确定。朝向(Heading)是相机朝向与正北方向的夹角,相机镜头对向北极时,Heading值为0,顺时针转动角度值增大。倾角(Pitch)是相机视线到相机重锤线的夹角。相机视角线与重锤线重合时,倾角为90°。旋转角(Roll)是指相机自身绕视线旋转的角度。
public Camera(MapPoint lookAtPoint, double distance, double heading, double pitch, double roll);
其中,lookAtPoint为相机观察点,distance是相机与目标的距离。
再来看,Camera类提供的几个重要方法。
public Camera MoveForward(double distance);
MoveForward()方法允许相机沿视线前进若干距离。
public Camera MoveTo(MapPoint location);
MoveTo()方法允许相机移动到某一空间点。
public Camera RotateAround(MapPoint targetPoint, double deltaHeading, double deltaPitch, double deltaRoll);
RotateAround()方法允许相机绕某一固定点旋转,这个方法在展示地形、模型时作用较大。
public Camera RotateTo(double heading, double pitch, double roll);
RotateTo()方法允许相机原地改变姿态。
观察视角类-ViewPoint
观察视角类的构造函数有10个。ViewPoint是相机的观察点。ViewPoint的构造函数很多。
public Viewpoint(Geometry.Geometry targetGeometry); public Viewpoint(Geometry.Geometry targetGeometry, Camera camera); public Viewpoint(Geometry.Geometry targetGeometry, double rotation); public Viewpoint(MapPoint center, double scale); public Viewpoint(Geometry.Geometry targetGeometry, double rotation, Camera camera); public Viewpoint(double latitude, double longitude, double scale); public Viewpoint(MapPoint center, double scale, Camera camera); public Viewpoint(MapPoint center, double scale, double rotation); public Viewpoint(double latitude, double longitude, double scale, Camera camera); public Viewpoint(MapPoint center, double scale, double rotation, Camera camera);ViewPoint的实例可以配合SceneView的SetViewpoint()和SetViewpointAsync()方法来使用。使用方法见下。
SceneView的几个重要方法
带Async的方法,从字面上就能看出来是异步方法,SetViewpointCamera()和SetViewpointCameraAsync()的签名如下:
public void SetViewpointCamera(Camera camera); public Task<bool> SetViewpointCameraAsync(Camera camera); public Task<bool> SetViewpointCameraAsync(Camera camera, TimeSpan duration);
SetViewpointCamera()能将主相机瞬间移动至camera参数所在的位置。而SetViewpointCameraAsync()允许将主相机通过动画移动至目标相机位置,并且这个异步方法有两个重载,在第二个重载中,还能去设置这个动画的时间。
动目标展示
Runtime提供接口,允许动态改变要素的位置。这样一来,Runtime也可用作三维情态势展示、沙盘演练等。假设有一个客户端发报机,或者中转机,在使用UDP协议不断往服务器发报,承载报文的结构体如下:
struct ObjectPosition { //经度 public double longitude; //纬度 public double latitude; //高度 public double altitude; //朝向 public double heading; //倾角 public double pitch; //翻滚 public double roll; } struct LiObject { public string batch; public int bathno; public List<ObjectPosition> objecttrail; }
报文示例如下:N000001;000001;103.32;43.78;50000;30.19;20.69;2.52;
接收到报文后,处理报文的代码如下,由于要素的位置改变牵扯到前端UI线程,故这里用Dispatcher.Invoke()。
public void ReciveMsg() { while (true) { EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号 byte[] buffer = new byte[1024]; int length = client.ReceiveFrom(buffer, ref point);//接收数据报 string message = Encoding.UTF8.GetString(buffer, 0, length); //报文分组 string [] ms=message.Split(';'); ObjectPosition op = new ObjectPosition(); //先解析批号 String batchname = ms[0]; //看批号是否存在,如果已存在,更新 if (findLiObject.Keys.Contains(batchname)) { Graphic curGraphic = findGraphic[batchname]; op.longitude = double.Parse(ms[2]); op.latitude = double.Parse(ms[3]); op.altitude = double.Parse(ms[4]); op.heading = double.Parse(ms[5]); op.pitch = double.Parse(ms[6]); op.roll = double.Parse(ms[7]); Dispatcher.Invoke(DispatcherPriority.Normal, new AddGraphicDelegate(UpdateGraphicPosition), op, curGraphic); } else { Dispatcher.Invoke(DispatcherPriority.Normal, new createModelGraphicDelegate(createModelGraphic),batchname); } } }
private void UpdateGraphicPosition(ObjectPosition op,Graphic g) { MapPoint location = new MapPoint(op.longitude, op.latitude, op.altitude, SpatialReferences.Wgs84); g.Geometry = location; g.Attributes["Heading"] = op.heading; g.Attributes["Pitch"] = op.pitch; g.Attributes["Roll"] = op.roll; }
这样一来,就能根据发报机发送的报文,时时更新要素空间姿态。
相机控制器
var orbitGraphicController = new OrbitGeoElementCameraController(graphics, 50000); mysceneview.CameraController = new GlobeCameraController();
mysceneview.CameraController = new GlobeCameraController();