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

使用NVIDIA Warp Python库光线追踪四边形时出现意外退化三角形

问题:NVIDIA Warp库渲染四边形时误检测到退化三角形

我使用NVIDIA Warp库编写了一个渲染四边形的简单程序,在内核中查询三角形顶点并检查退化三角形,但意外发现存在退化三角形——尽管输入的几何数据是正常的。

该网格是由两个三角形组成的简单四边形,原始数据中没有任何退化三角形。我怀疑是API使用方式有误,但查阅官方文档后仍未定位到问题。

import matplotlib.pyplot as plt
import numpy as np
import warp as wp


@wp.func
def get_triangle_points(
    mesh_id: wp.uint64, face_idx: int
) -> tuple[wp.vec3, wp.vec3, wp.vec3]:
    i1 = wp.mesh_get_index(mesh_id, 3 * face_idx)
    i2 = wp.mesh_get_index(mesh_id, 3 * face_idx + 1)
    i3 = wp.mesh_get_index(mesh_id, 3 * face_idx + 2)
    v1 = wp.mesh_get_point(mesh_id, i1)
    v2 = wp.mesh_get_point(mesh_id, i2)
    v3 = wp.mesh_get_point(mesh_id, i3)
    return v1, v2, v3


@wp.kernel
def render_quad(
    mesh_id: wp.uint64,
    width: int,
    height: int,
    pixels: wp.array(dtype=wp.vec3),
):
    tid = wp.tid()
    x = tid % width
    y = tid // width

    sx = 2.0 * (float(x) + 0.5) / float(width) - 1.0
    sy = 2.0 * (float(y) + 0.5) / float(height) - 1.0

    ro = wp.vec3(0.0, 5.0, 0.0)
    rd = wp.normalize(wp.vec3(-1.0, sy, sx))

    query = wp.mesh_query_ray(mesh_id, ro, rd, 1.0e6)

    color = wp.vec3(0.0, 0.0, 0.0)

    if query.result:
        face_idx = query.face
        v0, v1, v2 = get_triangle_points(mesh_id, face_idx)
        area = 0.5 * wp.length(wp.cross(v1 - v0, v2 - v0))
        if area < 1e-5:
            color = wp.vec3(0.0, 1.0, 0.0)
        else:
            color = wp.vec3(1.0, 0.0, 0.0)

    pixels[tid] = color


def main():
    quad_points = np.array(
        [
            [-5.0, 0.0, 5.0],
            [-5.0, 0.0, -5.0],
            [-5.0, 10.0, -5.0],
            [-5.0, 10.0, 5.0],
        ],
        dtype=np.float32,
    )

    quad_indices = np.array([0, 1, 2, 0, 2, 3], dtype=np.int32)
    wp_points = wp.array(np.ascontiguousarray(quad_points), dtype=wp.vec3)
    wp_indices = wp.array(np.ascontiguousarray(quad_indices), dtype=wp.int32)
    wp_mesh = wp.Mesh(points=wp_points, indices=wp_indices, velocities=None)

    width = 256
    height = 256

    pixels = wp.zeros(width * height, dtype=wp.vec3)

    wp.launch(
        kernel=render_quad,
        dim=width * height,
        inputs=[wp_mesh.id, width, height, pixels],
    )

    image = pixels.numpy().reshape((height, width, 3))

    plt.figure(figsize=(6, 6))
    plt.imshow(image, origin="lower", interpolation="nearest")
    plt.axis("off")
    plt.show()


if __name__ == "__main__":
    main()

示例输出

示例输出(绿色区域本不应出现):
Image Output

运行说明

运行最小复现示例前,请先在Python环境中执行以下命令安装依赖:

pip install warp-lang matplotlib numpy

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 01:20:06