作者:我的惟一fd | 来源:互联网 | 2023-09-24 14:13
我在3D空间中有一个点序列(我有一个表,上面有x,y,z坐标),它们代表3D曲线,我需要一种算法来精确计算a的x,y,z坐标给定新点,从曲线起点到期望点的距离(沿着曲线)。
我到目前为止所做的:
我找到了一个很好而简单的VBA函数,该函数基于一组点(Xi,Yi)和一定的X值,通过三次样条插值来计算相应的Y(X)。但这仅在可以将Y视为X的函数的情况下起作用,因此,如果曲线在不同的Y坐标处重复X坐标,则它不起作用。
然后,我采用了一个通用的解决方案,即使用参数曲线,其中X,Y和Z可以视为参数t的函数。作为参数,我采用了距离的总和。因此,对于每个点,我都像这样计算参数的值:
u(1) = 0
u(i+1) = u(i) + linear distance between point(i+1) and point(i),i = 2,...,N
t(i) = u(i)/u(N),i = 1 ... N
然后,我可以使用相同的VBA函数为t的中间值计算X,Y和Z的值。换句话说,我有想要的样条线。
现在,我希望有一种算法可以计算出新点的x,y,z坐标,并给出从曲线起点到所需点的距离(沿着曲线)。
我找到了一些可以给我指导的文章,例如:
http://www.saccade.com/writing/graphics/RE-PARAM.PDF
我还发现了这两篇文章,它们可能具有即用型算法,但是我无法下载它们:
https://ieeexplore.ieee.org/document/55155
https://ieeexplore.ieee.org/abstract/document/8161111
VBA功能的代码是这样的:
Option Base 1
Function cubic_spline(xi_column As Range,_
yi_column As Range,_
X As Range)
'Purpose: Given a data set consisting of a list of x values
' and y values,this function will smoothly interpolate
' a resulting output (y) value from a given input (x) value
' This counts how points are in the set of data
Dim N As Integer
N = xi_column.Rows.Count
ReDim xin(N) As Single
ReDim yin(N) As Single
Dim i,j,K As Integer
For i = 1 To N
xin(i) = xi_column(i)
yin(i) = yi_column(i)
Next i
'''''''''''''''''''''''''''''''''''''''
' values are populated
'''''''''''''''''''''''''''''''''''''''
Dim p,qn,sig,un As Single
ReDim u(N - 1) As Single
ReDim yt(N) As Single 'these are the 2nd deriv values
yt(1) = 0
u(1) = 0
For i = 2 To N - 1
sig = (xin(i) - xin(i - 1)) / (xin(i + 1) - xin(i - 1))
p = sig * yt(i - 1) + 2
yt(i) = (sig - 1) / p
u(i) = (yin(i + 1) - yin(i)) / (xin(i + 1) - xin(i)) - (yin(i) - yin(i - 1)) / (xin(i) - xin(i - 1))
u(i) = (6 * u(i) / (xin(i + 1) - xin(i - 1)) - sig * u(i - 1)) / p
Next i
qn = 0
un = 0
yt(N) = (un - qn * u(N - 1)) / (qn * yt(N - 1) + 1)
For K = N - 1 To 1 Step -1
yt(K) = yt(K) * yt(K + 1) + u(K)
Next K
''''''''''''''''''''
'now eval spline at one point
'''''''''''''''''''''
Dim klo,khi As Integer
Dim h,B,A As Single
' first find correct interval
klo = 1
khi = N
Do
K = khi - klo
If xin(K) > X Then
khi = K
Else
klo = K
End If
K = khi - klo
Loop While K > 1
h = xin(khi) - xin(klo)
A = (xin(khi) - X) / h
B = (X - xin(klo)) / h
y = A * yin(klo) + B * yin(khi) + ((A ^ 3 - A) * yt(klo) + (B ^ 3 - B) * yt(khi)) * (h ^ 2) / 6
cubic_spline = y
out:
End Function
有人可以帮忙吗?
谢谢!