===== Getting Started =====
==== A Sphere ====
We begin with a simple sphere. The program example1.hf describes a solid sphere: 5^2 - (x^2 + y^2 + z^2) >= 0
-- example1.hf
my_model(x[3], a[1])
{
my_model = 5^2 - (x[1]^2 + x[2]^2 + x[3]^2);
}
Did you save the program //example1.hf//? To see an object, the program has to be interpreted by visualization tools. We use the **HyperFun Polygonizer** that visualizes F-rep objects described in the HyperFun program with polygons. First you must [[tools|download it]] then run the following command from the MS DOS command window:
hfp sphere.hf
The following window will be shown, and you can see the resulting polygonized object. You can rotate an object with the left mouse button and scale with the right mouse button. Select //Quit// in the //File// menu to quit the program.
{{http://hyperfun.org/Tut_HTML_e/images/sphere.jpg?nolink}}
Let's see the program step by step.
line 1: -- sphere.hf
line 2:
line 3: my_model(x[3], a[1])
line 4: {
line 5: my_model = 5^2 - (x[1]^2 + x[2]^2 + x[3]^2);
line 6: }
Line 1: The first line is a comment. The section from -- to the newline is assumed to be a comment. You can insert comments anywhere in a program.
Line 3: The HyperFun program begins with the object name //my_model(x[3], a[1])//, **x** and **a** are arrays. Note that **x** and **a** are reserved key words in HyperFun, so they can be used only in a special way: **x** is a point coordinates array and **a** is used to pass external values. In this example, **a** is not used. In HyperFun, an array index begins with 1. As defaults, x[1], x[2] and x[3] are mapped to point coordinates as:
x[1] -> x
x[2] -> y
x[3] -> z
Line 4, 6:The body of my_model is described in the block enclosed between //{// and //}//. Each statement must be terminated by a semicolon.
Line5: 5^2 - (x[1]^2 + x[2]^2 + x[3]^2) represents 5^2 - (x^2 + y^2 + z^2). The following table shows arithmetic operators available in HyperFun.
^Arithmetic operators in HyperFun^^
|+|addition|
|-|subtraction|
|*|multiplication|
|/|division|
|в_|power|
The object my_model has to return a function value; to do so //my_model =// is used. You can see later that local variables can be used in HyperFun; a variable which has the same name as an object is a special local variable used to return a value. This is like //return// used in C. The Polygonizer uses such values to visualize objects.
==== Use of library functions ====
In the next example, we use a predefined library function available in HyperFun to model a sphere.
-- example2.hf
my_model(x[3], a[1])
{
array center[3];
center = [0.0, 0.0, 0.0];
sphere = hfSphere(x, center, 5.0);
my_model = sphere;
}
Polygonize the above model with the command:
hfp example2.hf
The resulting object is the same as the first example.
Let's examine the program. The structure is almost the same as the first example, so only new things are explained here.
line 1: -- example2.hf
line 2:
line 3: my_model(x[3], a[1])
line 4: {
line 5: array center[3];
line 6:
line 7: center = [0.0, 0.0, 0.0];
line 8: sphere = hfSphere(x, center, 5.0);
line 9: my_model = sphere;
line 10: }
Line 5: //array center[3]// is a declaration of an array. An array must be declared at the beginning of an object's block before being used. The keyword array is used to declare arrays. In this case, //center// is declared as an array with size 3.
Line 7: The array //center// is initialized. There are two ways to initialize arrays: simultaneously and individually.
-- Initialize an array simultaneously
center = [0.0, 0.0, 0.0];
-- Initialize an array individually
center[1] = 0.0;
center[2] = 0.0;
center[3] = 0.0;
Line 8: hfSphere is a [[lib_sphere|library function]] for a solid sphere. You can think of hfSphere as a black box that can represent a solid sphere, so you only specify its position and radius. HyperFun has such library functions and operations, see [[library|F-rep library]]. Here, //sphere// is a local variable to save the value which hfSphere returns. The only type of variables used in HyperFun is real, so you can use variables without declarations.
Line 9: Finally, to return a value, my_model = sphere is used.