Houdini 学习文档 -VEX-数组

Arrays

Overview

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

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

  • Supporting ramp parameters. 支持斜坡参数Houdini 学习文档 -VEX-数组_第1张图片

  • 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的值

Houdini 学习文档 -VEX-数组_第2张图片

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函数是等效的。

Houdini 学习文档 -VEX-数组_第3张图片

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 side Notes
vector2 float[] E.g. vector2 v = {1,2}
vector float[] E.g. vector v = {1,2,3}
vector4 float[] E.g. vector4 v = {1,2,3,4};
matrix2 float[] E.g. matrix2 m = {1,2,3,4};
matrix2 vector2[] E.g. matrix2 m = { {1,2}, {4,5} };
matrix3 float[] E.g. matrix3 m = {1,2,3,4,5,6,7,8,9};
matrix3 vector[] E.g. matrix3 m = { {1,2,3}, {4,5,6}, {7,8,9}};
matrix float[] E.g. matrix m = {1,2,3,4,5,6,7,8,9.., 16};
matrix vector4[] E.g. matrix m = { {1,2,3,4}, {5,6,7,8}, ... {13,14,15,16}};
float[] vector2 Create an array of 2 floats from the components
float[] vector Create an array of 3 floats from the components
float[] vector4 Create an array of 4 floats from the components
float[] matrix2 Create an array of 4 floats from the matrix2
vector2[] matrix2 Create an array of 2 vector2s from the matrix2
float[] matrix3 Create an array of 9 floats from the matrix3
vector[] matrix3 Create an array of 3 vectors from the matrix3
float[] matrix4 Create an array of 16 floats
vector4[] matrix4 Create 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:

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

  • min 返回数组中的最小值。

  • avg 返回输入值的平均值 ---返回数组中值的平均值。

  • spline 沿着折线或样条曲线采样一个值。http://www.sidefx.com/docs/houdini/vex/functions/spline.html

  • import()

  • addattribute()  http://www.sidefx.com/docs/houdini/vex/functions/addattribute.html

  • metaimport  http://www.sidefx.com/docs/houdini/vex/functions/metaimport.html

VCC pragmas

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

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

#pragma ramp    

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. 数组不能写入图像平面。

你可能感兴趣的:(Houdini)