首页 > 解决方案 > 我如何用 numpy 满足布料模拟的限制

问题描述

我正在尝试使用 python + numpy 编写布料模拟我有一个使用 python 和循环的工作布料模拟。但是 numpy 版本不能按预期工作。tmp 是按边顶点排序的顶点位置数组。| edge1.Point a | edge1.点 b | edge2.Point a | edge2.点b

    pos1 = tmp[::2]
    pos2 = tmp[1::2]
    
    delta = pos2-pos1
    deltaLen = np.linalg.norm(delta, axis=1)
    f = (delta.T * (deltaLen - 0.2)).T * self.k
    tmp[::2] += f
    tmp[1::2] -= f

这是查看链接对之间距离然后设置新距离的片段。

我已经包含了运行模拟的网格的图像,其中目标是使所有边的长度为 0.2,起始长度为 0.25 代码适用于遍历数组,但不适用于 numpy

网格

标签: pythonnumpy

解决方案


Is it because your slices are not the same length?

import numpy as np

tmp = np.random.rand(7)

pos1 = tmp[::2]
pos2 = tmp[1::2]

print(pos1.shape, pos2.shape)
>>> (4,) (3,)

# make array slice same length
pos1 = tmp[:-1:2]
pos2 = tmp[1::2]

print(pos1.shape, pos2.shape)
>>> (3,) (3,)

推荐阅读