如何创建一个numpy数组来描述三角形的顶点? [英] How to create a numpy array to describe the vertices of a triangle?

查看:117
本文介绍了如何创建一个numpy数组来描述三角形的顶点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢使用Numpy创建要传递给 glsl 的顶点数组.

I like to use Numpy to create an array of vertices that is to be passed into glsl.

顶点将是一个包含3个顶点信息的numpy数组.

Vertices will be a numpy array that comprises the info of 3 vertex.

每个 vertex 包括:

  1. pos =(x,y)一种具有32位的64位带符号浮点格式以字节0..3为单位的R分量,以字节4..7为单位的32位G分量,和
  2. color =(r,g,b)一种96位带符号浮点格式,其格式为以字节0..3为单位的32位R分量,以字节为单位的32位G分量4..7,以及字节8..11中的32位B分量
  1. pos = (x, y) a 64-bit signed floating-point format that has a 32-bit R component in bytes 0..3, and a 32-bit G component in bytes 4..7, and
  2. color = (r, g, b) a 96-bit signed floating-point format that has a 32-bit R component in bytes 0..3, a 32-bit G component in bytes 4..7, and a 32-bit B component in bytes 8..11

即每个 vertex =(pos,color)=(((x,y),(r,g,b))

一个三角形有3个顶点,所以最后我需要一个1D numpy数组来描述

A triangle has 3 vertices, so finally I need a 1D numpy array to describe

Vertices = [vertex1, vertex2, vertex3]
         = [ ( (x, y), (r, g, b) ), 
             ( (x, y), (r, g, b) ), 
             ( (x, y), (r, g, b) ) ] 

如何在numpy中创建 Vertexes ?以下语法看起来错误.

How can I create Vertices in numpy? The below syntax looks wrong.

Vertices = np.array([( (x1, y1), (r1, g1, b1) ), 
                     ( (x2, y2), (r2, g2, b2) ), 
                     ( (x3, y3), (r3, g3, b3) )], dtype=np.float32)

每个 vertex 的字节大小应为64/8 + 96/8 = 8 + 12 = 20个字节. Vertices 的字节大小应为20字节x 3 = 60字节.

The bytes size of each vertex should be 64/8 + 96/8 = 8 + 12 = 20 bytes. The bytes size of Vertices should be 20 bytes x 3 = 60 bytes.

推荐答案

这很简单,实际上是 numpy .使用结构化数组:

This is quite simple, in numpy actually. Use structured arrays:

In [21]: PosType = np.dtype([('x','f4'), ('y','f4')])

In [22]: ColorType = np.dtype([('r','f4'), ('g', 'f4'), ('b', 'f4')])

In [23]: VertexType = np.dtype([('pos', PosType),('color', ColorType)])

In [24]: VertexType
Out[24]: dtype([('pos', [('x', '<f4'), ('y', '<f4')]), ('color', [('r', '<f4'), ('g', '<f4'), ('b', '<f4')])])

In [25]: VertexType.itemsize
Out[25]: 20

然后简单地:

In [26]: vertices = np.array([( (1, 2), (3, 4, 5) ),
    ...:                      ( (6, 7), (8, 9, 10) ),
    ...:                      ( (11, 12), (13, 14, 15) )], dtype=VertexType)

In [27]: vertices.shape
Out[27]: (3,)

基本索引编制:

In [28]: vertices[0]
Out[28]: (( 1.,  2.), ( 3.,  4.,  5.))

In [29]: vertices[0]['pos']
Out[29]: ( 1.,  2.)

In [30]: vertices[0]['pos']['y']
Out[30]: 2.0

In [31]: VertexType.itemsize
Out[31]: 20

numpy 曾经提供记录数组,因此您可以使用属性访问代替索引:

numpy ever provides record-arrays, so you can use attribute-access instead of indexing:

In [32]: vertices = np.rec.array([( (1, 2), (3, 4, 5) ),
    ...:                          ( (6, 7), (8, 9, 10) ),
    ...:                          ( (11, 12), (13, 14, 15) )], dtype=VertexType)

In [33]: vertices[0].pos
Out[33]: (1.0, 2.0)

In [34]: vertices[0].pos.x
Out[34]: 1.0

In [35]: vertices[2].color.g
Out[35]: 14.0

这篇关于如何创建一个numpy数组来描述三角形的顶点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆