python - 我如何用 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
解决方案
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,)
推荐阅读
- node.js - 我的 ejs 页面没有像其他 ejs 页面那样加载
- angular - 浏览器操作在无头 chrome 中不起作用
- python - Python truncate 函数未按预期工作
- php - 如何在不卸载 PHP 7.4 的情况下降级或使用 PHP 7.2?是否可以默认使用 PHP 7.2 而不是最新版本?
- android - 第一次在 xamarin 表单中使用 webivew 保存空白页
- python - 向对象添加属性
- javascript - 如何将json键与json中的额外字符串连接起来?
- javascript - 我无法在 Hook 中保存数据
- angular - NGXS 中状态的基于类的对象实例
- java - Java Mailx - openshift中的中文字符问题