numpy中射线三角交叉算法的优化

时间:2017-01-26 13:59:17

标签: python numpy

我正在编写通过numpy-stl库加载STL文件并呈现它的图像的应用程序。

为了渲染最终图像,我需要在网格中找到光线和三角形的交点

我已经在numpy中实现了Möller–Trumbore intersection algorithm但是对于更大的模型,性能非常差。

import numpy as np

#Loading the model via file
self.temp_mesh = Mesh.from_file("test.stl")

def intersectionRayModel(self, rayStart, rayEnd):
    w = rayEnd - rayStart
    w /= np.linalg.norm(w)

    data = self.temp_mesh

    counter = 0
    for tri in data.vectors+self.pos:
        b = [.0, .0, .0]

        e1 = tri[1]
        e1 -= tri[0]
        e2 = tri[2]
        e2 -= tri[0]

        n = self.temp_mesh.normals[counter]

        q = np.cross(w, e2)
        a = np.dot(e1, q)

        counter += 1
        if (np.dot(n, w) >= .0) or (abs(a) <= .0001):
            continue

        s = np.array(rayStart)
        s -= tri[0]
        s /= a

        r = np.cross(s, e1)
        b[0] = np.dot(s, q)
        b[1] = np.dot(r, w)
        b[2] = 1.0 - b[0] - b[1]

        if (b[0] < .0) or (b[1] < .0) or (b[2] < .0):
            continue

        t = np.dot(e2, r)
        if t >= .0:
            point = rayStart + t*w
            return True, point
        else:
            continue

    return False, None

我认为for循环在这里非常糟糕,但我无法找到摆脱它的方法。

0 个答案:

没有答案