【问题标题】:How to get the x,y coordinates of a offset spline from a x,y list of points and offset distance如何从点和偏移距离的 x,y 列表中获取偏移样条的 x,y 坐标
【发布时间】:2015-09-24 23:41:35
【问题描述】:

我需要制作一个翼型轮廓曲线的偏移平行封闭,但我不知道如何使所有点在所需距离处与主轮廓曲线上的点等距。

这是我的翼型剖面示例

这是我最好但不好的方法

编辑 @Patrick 距离 0.2 的解决方案

【问题讨论】:

  • 如何存储当前曲线?它是 int (x, y) 的列表吗?您需要什么精度的结果?
  • @IlyaPeterov 曲线以 x,y 坐标存储在 np.array 中,我不需要太多准确的结果,只需要近似曲线就可以了,因为我需要这个来制作有限元网格这两个配置文件之间
  • 增量应该从每个段的中点开始。让我添加一个例子。
  • @PatrickMaupin 非常感谢这个例子
  • @efirvida 是的,但不如帕特里克的。它没有半圆,一般来说有点不准确。虽然这对我来说是一个很好的练习,但谢谢。

标签: python graphics offset curve spline


【解决方案1】:

您必须使用无穷大/零的特殊斜率,但基本方法是使用插值计算某个点的斜率,然后找到垂直斜率,然后计算该距离处的点。

我修改了here 中的示例以添加第二个图表。它适用于data file you provided,但您可能需要更改不同信封的符号计算。

编辑根据您的 cmets 关于希望信封是连续的,我在末尾添加了一个俗气的半圆,非常接近为您执行此操作。从本质上讲,在创建信封时,您可以将其做得越圆越凸,效果就越好。另外,开头和结尾需要重叠,否则会有空隙。

此外,几乎可以肯定它可以变得更高效——无论如何我都不是 numpy 专家,所以这只是纯粹的 Python。

def offset(coordinates, distance):
    coordinates = iter(coordinates)
    x1, y1 = coordinates.next()
    z = distance
    points = []
    for x2, y2 in coordinates:
        # tangential slope approximation
        try:
            slope = (y2 - y1) / (x2 - x1)
            # perpendicular slope
            pslope = -1/slope  # (might be 1/slope depending on direction of travel)
        except ZeroDivisionError:
            continue
        mid_x = (x1 + x2) / 2
        mid_y = (y1 + y2) / 2

        sign = ((pslope > 0) == (x1 > x2)) * 2 - 1

        # if z is the distance to your parallel curve,
        # then your delta-x and delta-y calculations are:
        #   z**2 = x**2 + y**2
        #   y = pslope * x
        #   z**2 = x**2 + (pslope * x)**2
        #   z**2 = x**2 + pslope**2 * x**2
        #   z**2 = (1 + pslope**2) * x**2
        #   z**2 / (1 + pslope**2) = x**2
        #   z / (1 + pslope**2)**0.5 = x

        delta_x = sign * z / ((1 + pslope**2)**0.5)
        delta_y = pslope * delta_x

        points.append((mid_x + delta_x, mid_y + delta_y))
        x1, y1 = x2, y2
    return points

def add_semicircle(x_origin, y_origin, radius, num_x = 50):
    points = []
    for index in range(num_x):
        x = radius * index / num_x
        y = (radius ** 2 - x ** 2) ** 0.5
        points.append((x, -y))
    points += [(x, -y) for x, y in reversed(points)]
    return [(x + x_origin, y + y_origin) for x, y in points]

def round_data(data):
    # Add infinitesimal rounding of the envelope
    assert data[-1] == data[0]
    x0, y0 = data[0]
    x1, y1 = data[1]
    xe, ye = data[-2]

    x = x0 - (x0 - x1) * .01
    y = y0 - (y0 - y1) * .01
    yn = (x - xe) / (x0 - xe) * (y0 - ye) + ye
    data[0] = x, y
    data[-1] = x, yn
    data.extend(add_semicircle(x, (y + yn) / 2, abs((y - yn) / 2)))
    del data[-18:]

from pylab import *

with open('ah79100c.dat', 'rb') as f:
    f.next()
    data = [[float(x) for x in line.split()] for line in f if line.strip()]

t = [x[0] for x in data]
s = [x[1] for x in data]


round_data(data)

parallel = offset(data, 0.1)
t2 = [x[0] for x in parallel]
s2 = [x[1] for x in parallel]

plot(t, s, 'g', t2, s2, 'b', lw=1)

title('Wing with envelope')
grid(True)

axes().set_aspect('equal', 'datalim')

savefig("test.png")
show()

【讨论】:

  • 在您的代码中x2, x1y2, y1 是翼型轮廓中的两个连续点??所以我必须从x[0] 迭代到x[len(x)-1] ??
  • 是的,但这只会为您提供与原始曲线相同数量的点。您甚至可能想要插入更多点以使您的另一条曲线更平滑,因为它比您的第一条曲线更长。
  • @efirvida 另外,您可能希望从线段的中点开始增量,因为那里的切线更接近,例如(x1 + x2) / 2, (y1 + y2) / 2
  • 我用你的解决方案和我的点集更新了我的帖子,效果很好!!,现在需要关闭打开的偏移配置文件,我可以做这个机智曲线拟合解决方案或类似的东西吗?跨度>
  • @efirvida 你试过我最新的改变了吗?它似乎与您提供的数据一起使用 matplotlib。
【解决方案2】:

如果您愿意(并且能够)安装第三方工具,我强烈推荐Shapely 模块。这是一个向内和向外偏移的小样本:

from StringIO import StringIO
import matplotlib.pyplot as plt
import numpy as np
import requests
import shapely.geometry as shp

# Read the points    
AFURL = 'http://m-selig.ae.illinois.edu/ads/coord_seligFmt/ah79100c.dat'
afpts = np.loadtxt(StringIO(requests.get(AFURL).content), skiprows=1)

# Create a Polygon from the nx2 array in `afpts`
afpoly = shp.Polygon(afpts)

# Create offset airfoils, both inward and outward
poffafpoly = afpoly.buffer(0.03)  # Outward offset
noffafpoly = afpoly.buffer(-0.03)  # Inward offset

# Turn polygon points into numpy arrays for plotting
afpolypts = np.array(afpoly.exterior)
poffafpolypts = np.array(poffafpoly.exterior)
noffafpolypts = np.array(noffafpoly.exterior)

# Plot points
plt.plot(*afpolypts.T, color='black')
plt.plot(*poffafpolypts.T, color='red')
plt.plot(*noffafpolypts.T, color='green')
plt.axis('equal')
plt.show()

这是输出;注意向内偏移上的“bowties”(自相交)是如何被自动删除的:

【讨论】:

    猜你喜欢
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多