Houdini 学习文档 -VEX-数组

Arrays

Overview

VEX includes an array datatype. This is useful in several places:

VEX包含一个数组数据类型。用于以下几处:

  • Supporting ramp parameters. 支持斜坡参数

  • Reading capture data from surface nodes using the import() function.使用import()函数从表面节点读取捕获数据。

  • General programming, wherever arrays would be useful.一般用到数组的编程。

Note

Currently VEX does not support multi-dimensional arrays.目前,VEX不支持多维数组。

This example shows off some of the crazy things that you can do with arrays:

这个例子展示了你可以用数组做的一些疯狂的事情:

surface
crazy(
      string maps[] = { "Mandril.rat", "default.pic" };
      export float alength = 0;
      )
{
    vector texclr, av[];

    texclr = texture(maps[s+t > 1], s, t);
    av = array( {1,0,0}, vector(nrandom()), t, texclr, {.5,0,0});

    if (fit(noise(s*8), 0, 1, .3, .7) > t)
        av = array(1, {0,1,0}, 0);

    Cf = spline("linear", s, av);
    alength = len(av);
}

Declaring array types 声明数组类型

To declare an array variable, the general form is  要声明数组变量,一般的形式是 

 member_type var_name[]:

// my_array is an array of floats  ----my_array是一个浮点数数组
float   my_array[];

// v is a single vector, vector_array is an array of vectors ----v是一个向量,vector_array是一个向量数组
vector  v, vector_array[];

// str_array is an array of strings str_array ---是一个字符串数组
string  str_array[];

You can optionally put a size inside the square brackets, but the VEX compiler currently ignores it.

您可以选择将大小放在方括号中,但是VEX编译器目前会忽略它。

To declare a function that returns an array:

声明一个返回数组的函数:

// A function which returns an array of vectors
vector[] rgb_array()
{
...
};    

To declare a nested function that returns an array:

声明返回数组的嵌套函数:

// A function which returns an array of vectors--返回向量数组的函数
cvex
foo()
{
    // Use the optional 'function' keyword to avoid type ambiguity--函数使用可选的“function”关键字来避免类型歧义
    function vector[] rgb_array()
    {
    ...
    };    
}

To specify a literal array, use curly braces, with the array members separated by commas:

若要指定内容数组,请使用大括号,数组成员之间用逗号分隔:

vector an_array[] = { {1, 2, 3}, {2, 3, 4}, {4, 5, 6} };

vector[] rgb_array()
{
    return { {1, 0, 0}, {0, 1, 0}, {0, 0, 1} };
}

If you specify scalars where a vector is expected, the compiler assigns the scalar value to all components of the vector:

如果你指定标量,其中一个向量是预期的,编译器分配标量值给向量的所有组件:

vector an_array[] = { 1, 2, 3};
// an_array[] == { {1, 1, 1}, {2, 2, 2}, {3, 3, 3} }

The array() function creates an array from its arguments.

array() 函数的作用是:从数组的参数中创建一个数组。

int my_array[] = array(1, 2, 3, 4, 5);

You can use array() to generate an array of any type. To force array() to generate vectors (for example):

可以使用array()生成任何类型的数组。强制array()生成向量(例如):

vector (array (value1, value2, ...) );

Accessing and setting array values

访问和设置数组值

Use arrayname[index] to look up a value by its position in the array.

使用arrayname[index]根据值在数组中的位置查找值。

vector bw[] = { 0, 1 };
// bw[] == { {0, 0, 0}, {1, 1, 1} }
Cf = bw[index];

Array bounds are checked at run time. Reading out of bounds will return 0 or "". This may generate a warning or optional run-time error in the future. Writing past the end of an array will resize the array to include the index written to. The new entries will be set to 0 or "".

数组边界在运行时检查。读取超出数组范围时将返回0或“”。这可能在将来生成警告或可选的运行时错误。写入数组末尾后,将调整数组大小以包含写入的索引。新条目将被设置为0或“”。

Python-style indexing is used. This means negative indices refer to positions from the end of the array.

使用python风格的索引。这意味着负索引指的是数组末尾的位置,从1开始倒着数的位置,最后一位是 -1

int nums[] = { 0, 1, 2, 3, 4, 5 };
int n = nums[10];  // Returns 0
int b = nums[-2];  // Returns 4

string strs[] = { };
string s = strs[20];  // Returns ""

You can also assign values using the square brackets notation:

你也可以使用方括号符号赋值:

float nums[] = { };
nums[0] = 3.14;

(The getcomp and setcomp functions are equivalents for using the square brackets notation.)

getcomp和setcomp函数是使用方括号符号的等价函数。比如:getcomp(array[], int index)  取array[]中下标为index的值

Note

The square-brackets operator also works on vectors. You can use it with matrices as well using a pair of brackets: float a = m3[0][1];

方括号运算符也适用于向量。您可以将它与矩阵一起使用,也可以使用一对括号:float a = m3[0][1];

Slicing Arrays 分割数组

The square-brackets can be used to extract sub-arrays using the Python slicing notation.

方括号可用于使用Python切片符号提取子数组。详见:https://blog.csdn.net/qq_41973536/article/details/82690242

int nums[] = { 0, 1, 2, 3, 4, 5 };
int start[] = nums[0:2];  // { 0, 1 }
int end[] = nums[-2:];  // { 4, 5 }
int rev[] = nums[::-1];  // { 5, 4, 3, 2, 1, 0 }
int odd[] = nums[1::2]; // { 1, 3, 5 }

The slice function is the equivalent for using the slice-based square brackets notation.

对于使用基于切片的方括号符号,slice函数是等效的。

Copying between arrays and vectors/matrices

数组和向量/矩阵之间的复制

The assignment operator supports assigning values between vector types and arrays of floats:

赋值运算符支持在向量类型和浮点数组之间赋值:

float x[];
// Cf and P are vectors

x = set(P);   // Assigns the components of P to the corresponding
              // members of the array x

Cf = set(x);  // Assigns the first 3 members of x as the
              // components of the vector Cf

If the array is not long enough to fill the vector/matrix, the last member is repeated as often as necessary.

如果数组不够长,不足以填充向量/矩阵,则根据需要重复最后一个成员

float x[] = {1, 2} // Not long enough to fill a vector
Cf = set(x);  // Cf == {1, 2, 2}

You can also assign between matrix types and arrays of vector2/vector/vector4:

你也可以在矩阵类型和vector2/vector/vector4的数组之间分配:

vector2     v2[];
vector      v[];
vector4     v4[];
matrix2     m2 = 1;
matrix3     m3 = 1;
matrix      m4 = 1;

v = set(m3);   // Each row of the 3x3 matrix is put into a vector
m3 = set(v);   // Copy the vectors into the row vectors of the matrix
v4 = set(m4);  // Extract the rows of the matrix into the vector4 array
m4 = set(v4);  // Create a matrix using the vector4's in the array as row vectors

In summary: 总之

Left side =Right sideNotes
vector2float[]E.g. vector2 v = {1,2}
vectorfloat[]E.g. vector v = {1,2,3}
vector4float[]E.g. vector4 v = {1,2,3,4};
matrix2float[]E.g. matrix2 m = {1,2,3,4};
matrix2vector2[]E.g. matrix2 m = { {1,2}, {4,5} };
matrix3float[]E.g. matrix3 m = {1,2,3,4,5,6,7,8,9};
matrix3vector[]E.g. matrix3 m = { {1,2,3}, {4,5,6}, {7,8,9}};
matrixfloat[]E.g. matrix m = {1,2,3,4,5,6,7,8,9.., 16};
matrixvector4[]E.g. matrix m = { {1,2,3,4}, {5,6,7,8}, ... {13,14,15,16}};
float[]vector2Create an array of 2 floats from the components
float[]vectorCreate an array of 3 floats from the components
float[]vector4Create an array of 4 floats from the components
float[]matrix2Create an array of 4 floats from the matrix2
vector2[]matrix2Create an array of 2 vector2s from the matrix2
float[]matrix3Create an array of 9 floats from the matrix3
vector[]matrix3Create an array of 3 vectors from the matrix3
float[]matrix4Create an array of 16 floats
vector4[]matrix4Create an array of 4 vector4s.

Looping over an array

See foreach.

Working with arrays

The following functions let you query and manipulate arrays.

下面的函数允许查询和操作数组。

resize

Sets the length of the array. If the array is enlarged, intermediate values will be 0 or "".

设置数组的长度。如果数组被放大,中间值将为0或“”。

len

Returns the length of an array. 返回数组的长度

pop

Removes the last item from the array (decreasing the size of the array by 1) and returns it.

从数组中删除最后一项(将数组大小减少1)并返回它。

push

Adds an item to the end of an array (increasing the size of the array by 1).

将项添加到数组末尾(将数组大小增加1)。

getcomp

Gets the value of an array component, the same as array[num].

获取与array[num]相同的数组元素的值。

setcomp

Sets the value of an array component, the same as array[num] = value.

设置数组组件的值,与array[num] = value相同。

array

Efficiently creates an array from its arguments.

有效地根据它的元素创建数组。

serialize

Flattens an array of vectors or matrices into an array of floats.

将一组向量或矩阵展开成一组浮点数。

unserialize

Reverses the effect of serialize: assembles a flat array of floats into an array of vectors or matrices.

反转序列化的效果:将浮点数的平面数组组装成向量或矩阵数组。

neighbours

An array-based replacement for the neighbourcount/neighbour combo. Returns an array of the point numbers of the neighbors of a given point.

一个基于数组的对neighborcount /neighbour组合的替换。返回给定点的相邻点的点编号数组。

In addition, the following functions work with arrays:

此外,以下函数可以处理数组:

VCC pragmas

The ramp pragma lets you specify a ramp user interface for a set of parameters.

ramp pragma允许您为一组参数指定ramp用户界面。

#pragma ramp <ramp_parm> <basis_parm> <keys_parm> <values_parm>

See VCC pragmas for more information. http://www.sidefx.com/docs/houdini/vex/pragmas.html

Limitations 局限

  • Currently VEX does not support multi-dimensional arrays.目前,VEX不支持多维数组。

  • Arrays cannot be passed between shaders (through simport, etc.). 数组不能在着色器之间传递

  • Arrays cannot be written to image planes. 数组不能写入图像平面。

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值