# python 根据TIN查询点云坐标

PYthon TIN生成

## 2.2 判断特征点所属的三角形

### 2.3 通过插值生成特征点坐标

TIN为线性插值，因此只需判断三角形三个顶点，即可求得三角形面内任意一点的坐标。（根据三点成面原理）

## 代码实现

import os
import math
from program import CreateTIN as ct
import datetime
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

# 判断检查点是否与原始点存在同坐标情况
def checkdata(checkpoint_list, initialdata_list):
checkName_list = []  # 保存检查点的点名标签
initialName_list = []  # 保存原始点的点名标签
for checkpoint in checkpoint_list:
for initialdata in initialdata_list:
if checkpoint.X == initialdata.X and checkpoint.Y == initialdata.Y:
checkName_list.append(checkpoint.PointName)
initialName_list.append(initialdata.PointName)
if checkName_list != [] and initialName_list != []:
return checkName_list, initialName_list
else:
print("没找到同坐标点")

def getNearistPoint(Point,initialdata_list,Net):
# 判断点处于哪个三角形中，并返回三个角点
line_list = Net[0]  # 边列表
Triangle_list = Net[1]  # 三角形列表
nearpoint = initialdata_list[ct.NearistPoint(Point, initialdata_list)]  ## 在原始数据中返回距离最近的点
nearpoint_list = []  # 用以存储与最近点有关系的点
nearline_list = []  # 用以储存最近点相关边
slope_list = []  # 储存斜率
for line in line_list:
if line.BeginPoint == nearpoint:
slope = slopee(line.BeginPoint.X, line.BeginPoint.Y, line.EndPoint.X, line.EndPoint.Y)
slope_list.append(slope)   # 添加斜率
nearline_list.append(line)  # 添加最近点相关边
Point_aothr = line.EndPoint
nearpoint_list.append(Point_aothr)   # 添加与最近点有关系的点
elif line.EndPoint == nearpoint:
slope = slopee(line.BeginPoint.X, line.BeginPoint.Y, line.EndPoint.X, line.EndPoint.Y)
slope_list.append(slope)  # 添加斜率
nearline_list.append(line)  # 添加最近点相关边
Point_aothr = line.BeginPoint
nearpoint_list.append(Point_aothr)  # 添加与最近点有关系的点

slope_label = list(np.argsort(slope_list))  # 返回斜率递减下标
slope_label.append(slope_label[0])  # 把开头扩充到结尾
nearline_list.append(nearline_list[0])  # 把列表扩充一个开头
slope = slopee(nearpoint.X, nearpoint.Y, Point.X, Point.Y)  # 目标斜率
# 判断目标斜率在哪两个斜率之间
index = 0
for i in range(len(slope_label) - 1):
if slope_list[slope_label[i]] <= slope <= slope_list[slope_label[i + 1]]:
index = i
# nearline_list[slope_label[index]]  # 一条边
# nearline_list[slope_label[index+1]]  # 另一条边

if (nearline_list[slope_label[index]].BeginPoint ==nearline_list[slope_label[index+1]].BeginPoint or nearline_list[slope_label[index]].BeginPoint ==nearline_list[slope_label[index+1]].EndPoint):
Point_1 = nearline_list[slope_label[index]].EndPoint
Point_2 = nearline_list[slope_label[index+1]].BeginPoint
Point_3 = nearline_list[slope_label[index+1]].EndPoint
elif (nearline_list[slope_label[index]].EndPoint == nearline_list[slope_label[index+1]].BeginPoint or nearline_list[slope_label[index]].EndPoint ==nearline_list[slope_label[index+1]].EndPoint):
Point_1 = nearline_list[slope_label[index]].BeginPoint
Point_2 = nearline_list[slope_label[index+1]].BeginPoint
Point_3 = nearline_list[slope_label[index+1]].EndPoint
return Point_1,Point_2,Point_3

def getHigh(point1,point2,point3,point4):
# 依据三点成面，判断一个已知X，Y坐标的点的H坐标,此公式没错
#  A*x+B*y+C*z+D=0 ,求解A，B，C
M = ((point3.H-point1.H)*(point2.X-point1.X)-(point2.H-point1.H)*(point3.X-point1.X))
N = ((point3.Y-point1.Y)*(point2.X-point1.X)-(point2.Y-point1.Y)*(point3.X-point1.X))
D = ((-point1.H*(point2.X-point1.X)+(point2.H-point1.H)*point1.X)*N+(point1.Y*(point2.X-point1.X)-(point2.Y-point1.Y)*point1.X)*M)
A = -(point2.H-point1.H)*N+M*(point2.Y-point1.Y)
B = -(point2.X-point1.X)*M
C = (point2.X-point1.X)*N
if C!=0:
hight = (-D -A*point4.X-B*point4.Y)/C
else:
hight = 0
return hight

def draw(point_list,line_list):
#ax.scatter(xs, ys, zs, s=20, c=None, depthshade=True, *args, *kwargs)
# np.random.rand(n)产生1*n数组，元素大小0-1
fig = plt.figure()  # 打开画图窗口，在三维空间中绘图
for point in point_list:
ax.scatter(point.X, point.Y, point.H, c='c', marker= 'o')
for line in line_list:
x = [line.BeginPoint.X,line.EndPoint.X]
y = [line.BeginPoint.Y,line.EndPoint.Y]
z = [line.BeginPoint.H,line.EndPoint.H]
ax.plot(x,y,z,c='r')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()

def slopee(x1,y1,x2,y2): # 求斜率
x = (y2 - y1) / (x2 - x1)
return x

if __name__ == '__main__':
start = datetime.datetime.now()  # 起始时间
print('起始时间：', start)
# '''*******************调试********************'''

# 读取数据
initialdata_path = r'\\'
checkpoint_path = r"\\"
checkdata(checkpoint_list, initialdata_list)
Net = ct.CreatTIN(initialdata_list)  # 生成三角网

# 求RMSE
MSE = 0
for checkpoint in checkpoint_list:
point_list = getNearistPoint(checkpoint, initialdata_list, Net)  # 返回三个点坐标
hight = getHigh(point_list[0],point_list[1],point_list[2],checkpoint)# 通过三个坐标返回高程值
MSE = MSE + (hight - checkpoint.H)**2
RMSE = math.sqrt(MSE / len(checkpoint_list))
print(RMSE)# 求RMSE

end = datetime.datetime.now()  # 结束时间
print('总用时：', end - start)


## 示例

• 1
点赞
• 8
收藏
觉得还不错? 一键收藏
• 0
评论
11-28 1507
03-27 6811
12-25
04-23
11-20
07-26
08-13
08-16 2415
06-22 1万+
09-11 595
12-05 1883
10-15 1097

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。