Pre-Complied vs. Interactive Shading

Renderman Shading Language has a great potential to create custom image filters. Powerful mathematical functions in conjunction with the ability to access the coordinate information of every point in an image, gives the user a wide freedom to produce and modify different effects. This page demonstrates creating what most digital artist know as "Polar Coordinate" filter which converts the coordinates of an image from Cartesian to Polar or vice-versa.

Definition of Polar Coorditane System:



In mathematics, the polar coordinate system is a two-dimensional coordinate system in which each point on a plane is determined by a distance from a fixed point and an angle from a fixed direction.
The fixed point (analogous to the origin of a Cartesian system) is called the pole, and the ray from the pole with the fixed direction is the polar axis. The distance from the pole is called the radial coordinate or radius, and the angle is the angular coordinate, polar angle , or azimuth.

(wikipedia)

Conversion between Polar and Cartesian Coordinates:



The two polar coordinates r and θ can be converted to the Cartesian coordinates x and y by using the trigonometric functions sine and cosine:

x = r cos θ
y = r sin θ


while the two Cartesian coordinates x and y can be converted to polar coordinate r and θ by

r = √x ² + y ²
θ = arctan (y/x)


(wikipedia)

RSL implementation


 surface

 Polar(float   Kfb = 1,
               RecToPolar = 0; /* [0 or 1] */
       string  mapname = "")
 {
 color    map = 0.0;

  
 float    ss1 = 0.5*(1-t*sin(2*PI*s));
 float    tt1 = 0.5*(1-t*cos(2*PI*s));

  
 float    ss2 = (atan((s-0.5),(t-0.5))+PI)/(2*PI);
 float    tt2 = 2*sqrt(pow(0.5-s,2)+pow(0.5-t,2));

  
 if(mapname != "") {
     if(RecToPolar == 0)
         map = texture(mapname,ss1,tt1);
     else

         map = texture(mapname,ss2,tt2);        
     }
 Oi = Os;
 Ci = Oi * Cs * map * Kfb;
 }

This is the implementation of mathematical formulas in RSL which is arbitrarily called "Polar". There is a points to be considered while calculating the conversions:
The origin of st color space starts from top-left corner. The polar shader needs to be calculated work base on the center of the image. Therefore a 0.5 unit of shift should be imposed to the calculations. Polar shader is devided into two different effects: "Polar to Rectangular" and "Rectangular to Polar".


Polar to Rectangular Conversion

In Polar to Rectangular conversion we have to use the first set of the above formulas.
the center of image expands into a line which will be the new s axis of the distorted image. Therefore it could represent the angle in the polar coordinate: while s is growing from 0 to 1, the angle θ grows from 0 to 2π. Also when t changes from 0 to 1, distance of each point from the center (r) changes from 0 to 0.5 . Therefore we have:

float ss1 = 0.5*(1-t*sin(2*PI*s));
float tt1 = 0.5*(1-t*cos(2*PI*s));

The result of such conversion distorts the image like the animation on the left.

Rectangular to Polar Conversion

In Rectangular to Polar conversion we use the second sets of formulas.
The s axis shrinks down into the center of image. and two left and right sides of image move and unite to form half of t axis. The distance from center is calculated with Pythagorean theorem.

float ss2 = (atan((s-0.5),(t-0.5))+PI)/(2*PI);
float tt2 = 2*sqrt(pow(0.5-s,2)+pow(0.5-t,2));

You can see such conversion on left.

Notice that the atan function (with two arguments)returns an angle between to π. So first we should add a π to the result, and then turn it into degrees.



Here are the final result of the Polar shader:

The original image



R to P Conversion



P to R Conversion