You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断3D点是否属于splipy生成的样条曲线或计算其距离?

关于Splipy样条曲线的点归属判断与距离计算

1. 判断点是否属于曲线(含近似判断)

由于浮点数计算存在精度误差,严格判断点"完全在曲线上"并不现实,通常通过最小距离小于设定精度阈值来近似判断。Splipy没有直接内置该功能,但可以结合数值优化实现:

import numpy as np
from scipy.optimize import minimize
from splipy import curve_factory

# 初始化样条曲线
pts = [[0,0,0], [1,1,0], [2,0,0], [3,1,0]]
curve = curve_factory.curve(pts)

def distance_to_curve(t, target_point):
    curve_point = curve.evaluate(t)
    return np.linalg.norm(curve_point - target_point)

def is_point_on_curve(target_point, tolerance=1e-6):
    # 在样条默认参数域[0,1]内寻找最小距离
    res = minimize(distance_to_curve, x0=0.5, args=(target_point,), bounds=[(0, 1)])
    return res.fun < tolerance

# 测试示例
test_point_on = curve.evaluate(0.3)
print(is_point_on_curve(test_point_on))  # 输出True

test_point_off = np.array([0.5, 0.5, 1])
print(is_point_on_curve(test_point_off))  # 输出False

2. 计算点到曲线的最短距离

复用上述优化逻辑,直接返回最小距离值即可:

def get_distance_to_curve(target_point):
    res = minimize(distance_to_curve, x0=0.5, args=(target_point,), bounds=[(0, 1)])
    return res.fun

# 示例
test_point = np.array([1.5, 0.5, 0])
print(get_distance_to_curve(test_point))  # 输出该点到曲线的最短距离

注意事项

  • 样条曲线默认参数域为[0,1],若你的曲线参数范围不同,需调整bounds参数
  • 复杂曲线可能存在多个局部最小值,建议多尝试几个初始猜测点(如0、0.5、1)取最小距离
  • 精度阈值tolerance可根据需求调整,工程场景用1e-3即可,高精度场景可设为1e-9

内容的提问来源于stack exchange,提问作者MangoMat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 20:50:32