-
Notifications
You must be signed in to change notification settings - Fork 5
How to create your own surface class
This is an example to show how to create a new surface class with the surfaceLib in processing. For this example I take the Handkerchief surface as described here. Paul Bourke's side is an great source for different surface formulas. Also the german side 3d-meier has an enormous range of surface for you.
Let's take a look at the formula:
x = u
y = v
z = u3 / 3 + u v2 + a (u2 - v2)
-1 <= u <= 1, -1 <= v <= 1, a = 1
Ok, this formula is quite simple cause only the z value have some calculations. So what does u and v means. This are degrees between a given range, in this case both between -1 and 1. When you initialize your surface like new someSurface(g, 10,10)
this creates two arrays of 10 values between the given range of the surface formula.
Now lets start. Open a new tab and call it Handkerchief. Then we create a new class, called Handkerchief which extends the Surface class.
class Handkerchief extends Surface{ }
Then we add the constructor, which calls the constructor of the main Surface class. Here we add the range parameters for u and v.
Handkerchief( final PGraphics i_g,
final int i_phiSteps,
final int i_thetaSteps) {
super(i_g, i_phiSteps, i_thetaSteps, -1, 1, -1, 1, null);
}
To increase the speed of the initialization there are some LookupTable? classes. Lookup Table? Look, a surface consist of phiSteps x thetaSteps points. A new someSurface(g, 10,10)
gives us 100 points and all have to be calculated at the above formula. So we calculate 100 times u3 although there are only 10 u values. Therefore we create a lookup table at the beginning, who's pre-compute and save the 10 values for u3. Ok now add the lookupTable. There are a bunch of tables classes for sin, cos, tan and pow and you can create other by your needs. We only need the PowerTable for u2
, u3
and v2
. The parameters phi/thetaSteps
, minPhi/Theta
and maxPhi/Theta
are private variables of the main Surface class. 2 and 3 you guess, are the exponents for the pow()
method.
class Handkerchief extends Surface{
LookUpTable thetaPow2Table;
LookUpTable phiPow3Table;
LookUpTable phiPow2Table;
Handkerchief( final PGraphics i_g,
final int i_phiSteps,
final int i_thetaSteps) {
super(i_g, i_phiSteps, i_thetaSteps, -1, 1, -1, 1, null);
}
void initValues() {
phiPow2Table = new PowerTable(phiSteps, minPhi, maxPhi, 2);
phiPow3Table = new PowerTable(phiSteps, minPhi, maxPhi, 3);
thetaPow2Table = new PowerTable(thetaSteps, minTheta, maxTheta, 2);
}
}
At last we need to calculated the x
, y
and z
values. It's quite easy for x
and y
. At the z
value exchange u2 in the formula above with phiPow2Table and so on. So at the end the class looks like this:
class Handkerchief extends Surface{
LookUpTable thetaPow2Table;
LookUpTable phiPow3Table;
LookUpTable phiPow2Table;
Handkerchief( final PGraphics i_g,
final int i_phiSteps,
final int i_thetaSteps) {
super(i_g, i_phiSteps, i_thetaSteps, -1, 1, -1, 1, null);
}
void initValues() {
phiPow2Table = new PowerTable(phiSteps, minPhi, maxPhi, 2);
phiPow3Table = new PowerTable(phiSteps, minPhi, maxPhi, 3);
thetaPow2Table = new PowerTable(thetaSteps, minTheta, maxTheta, 2);
}
float calculateX(final int i_phiStep, final int i_thetaStep) {
return i_thetaStep;
}
float calculateY(final int i_phiStep, final int i_thetaStep) {
return i_phiStep;
}
float calculateZ(final int i_phiStep, final int i_thetaStep) {
return phiPow3Table.getValue(i_phiStep)/3+i_phiStep*thetaPow2Table.getValue(i_thetaStep)+2*(phiPow2Table.getValue(i_phiStep)-thetaPow2Table.getValue(i_thetaStep));
}
}