Skip to content

Commit ea01b5f

Browse files
committed
addedf a method to generate parameters for ri.Polygon from PrimData useful for renderman demos
1 parent 646abab commit ea01b5f

5 files changed

Lines changed: 105 additions & 7 deletions

File tree

‎src/ncca/ngl/__init__.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,18 @@
4040
from .text import Text
4141
from .texture import Texture
4242
from .transform import Transform, TransformRotationOrder
43-
from .util import PerspMode, calc_normal, clamp, frustum, lerp, look_at, ortho, perspective, renderman_look_at
43+
from .util import (
44+
PerspMode,
45+
calc_normal,
46+
clamp,
47+
frustum,
48+
lerp,
49+
look_at,
50+
ortho,
51+
perspective,
52+
prim_data_to_ri_points_polygons,
53+
renderman_look_at,
54+
)
4455
from .vao_factory import VAOFactory, VAOType
4556
from .vec2 import Vec2
4657
from .vec2_array import Vec2Array
@@ -111,4 +122,5 @@
111122
PySideEventHandlingMixin,
112123
PerspMode,
113124
renderman_look_at,
125+
prim_data_to_ri_points_polygons,
114126
]

‎src/ncca/ngl/util.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import enum
77
import math
88

9+
import numpy as np
10+
911
from .mat4 import Mat4
1012

1113

@@ -214,3 +216,53 @@ def renderman_look_at(eye, look, up):
214216
result.m[3][2] = -eye.dot(n)
215217

216218
return result
219+
220+
221+
def prim_data_to_ri_points_polygons(triangles: np.ndarray):
222+
"""
223+
Convert a packed numpy array of triangles to RenderMan PointsPolygons format.
224+
This is designed to work with the PrimData class outputs.
225+
Parameters
226+
----------
227+
triangles : np.ndarray
228+
Array of shape (n_vertices, 8) where each row is: x, y, z, nx, ny, nz, u, v
229+
n_vertices must be divisible by 3 (since we have triangles)
230+
231+
Returns
232+
-------
233+
tuple
234+
(nvertices, vertices, parameterlist)
235+
- nvertices: list of vertex counts per polygon (all 3 for triangles)
236+
- vertices: flat list of vertex indices
237+
- parameterlist: dict with 'P', 'N', 'st' arrays for RenderMan
238+
"""
239+
# Ensure it's a 2D array
240+
if triangles.ndim == 1:
241+
# If completely flat, reshape to (n_verts, 8)
242+
if len(triangles) % 8 != 0:
243+
raise ValueError("1D array length must be divisible by 8")
244+
triangles = triangles.reshape(-1, 8)
245+
246+
n_verts = triangles.shape[0]
247+
if n_verts % 3 != 0:
248+
raise ValueError("Number of vertices must be divisible by 3")
249+
250+
n_triangles = n_verts // 3
251+
252+
# Extract components from each row
253+
positions = triangles[:, 0:3] # (n_verts, 3) - x, y, z
254+
normals = triangles[:, 3:6] # (n_verts, 3) - nx, ny, nz
255+
uvs = triangles[:, 6:8] # (n_verts, 2) - u, v
256+
257+
# RenderMan PointsPolygons format
258+
nvertices = [3] * n_triangles # Each polygon has 3 vertices
259+
vertices = list(range(n_verts)) # Sequential vertex indices
260+
261+
# Parameter list - flatten to 1D arrays as RenderMan expects
262+
parameterlist = {
263+
"P": positions.flatten().tolist(), # Position
264+
"N": normals.flatten().tolist(), # Normals
265+
"st": uvs.flatten().tolist(), # Texture coordinates
266+
}
267+
268+
return nvertices, vertices, parameterlist

‎test_all.sh‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env zsh
22

3-
uv run pytest
4-
uv run pytest -m opengl
5-
uv run pytest -m webgpu
6-
uv run pytest -m qt
3+
set -euo pipefail
4+
uv run pytest || exit 1
5+
uv run pytest -m opengl || exit 1
6+
uv run pytest -m webgpu || exit 1
7+
uv run pytest -m qt || exit 1

‎tests/test_util.py‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1+
import numpy as np
12
import pytest
23

3-
from ncca.ngl import Mat4, PerspMode, Vec3, calc_normal, clamp, frustum, lerp, look_at, ortho, perspective
4+
from ncca.ngl import (
5+
Mat4,
6+
PerspMode,
7+
Vec3,
8+
calc_normal,
9+
clamp,
10+
frustum,
11+
lerp,
12+
look_at,
13+
ortho,
14+
perspective,
15+
prim_data_to_ri_points_polygons,
16+
)
417

518

619
def test_clamp():
@@ -101,3 +114,23 @@ def test_frustum():
101114
],
102115
abs=1e-3,
103116
)
117+
118+
119+
def test_prim_data_to_ri_points_polygons():
120+
# Create example triangle data
121+
triangles = np.array(
122+
[
123+
# Triangle 1: 3 vertices, each with x,y,z,nx,ny,nz,u,v
124+
[[0, 0, 0, 0, 0, 1, 0, 0], [1, 0, 0, 0, 0, 1, 1, 0], [0, 1, 0, 0, 0, 1, 0, 1]],
125+
# Triangle 2
126+
[[1, 0, 0, 0, 0, 1, 1, 0], [1, 1, 0, 0, 0, 1, 1, 1], [0, 1, 0, 0, 0, 1, 0, 1]],
127+
],
128+
dtype=np.float32,
129+
).flatten()
130+
131+
nvertices, vertices, parameterlist = prim_data_to_ri_points_polygons(triangles)
132+
assert nvertices == [3, 3]
133+
assert vertices == [0, 1, 2, 3, 4, 5]
134+
assert parameterlist.get("P")
135+
assert parameterlist.get("N")
136+
assert parameterlist.get("st")

‎tests/test_vec3.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66
import pytest
77

8-
from ncca.ngl import Mat3, Vec3, Vec2, Vec4
8+
from ncca.ngl import Mat3, Vec2, Vec3, Vec4
99

1010

1111
def test_properties():

0 commit comments

Comments
 (0)