2
$\begingroup$

I am trying to calculate the derivatives of a sampled data set by following an example in this book. The data is a set of 2d quads(green area in below) and their vertices(4 each). The data is in the following example is sampled from $z=f(x,y)=e^{-(x^2+y^2)}$ but, in general, the underlying function might not be known.

enter image description here

The book states that the derivatives of any points are calculated by:

enter image description here

enter image description here are the quadrilateral basis functions:

enter image description here

and

enter image description here

I can calculate df/dx and df/dy.

But where I struggle is calculating df/dz. I assume

enter image description here

and therefore df/dz = 0. But this cannot be correct since df/dz should be 1(by making the above analytic function implicit and differentiating it for z).

$\endgroup$
  • $\begingroup$ Can I ask some questions for clarification?So you have a function, f(x,y), that you have sampled on a regular grid of (x,y) locations, to create a set of bilinear patches? Are those corner points, therefore, of the form (xi, yi, f(xi, yi))? That is Z is pointing up the screen? $\endgroup$ – Simon F Oct 28 at 8:47
  • $\begingroup$ Exactly as you said $\endgroup$ – ali Oct 28 at 13:36
  • 1
    $\begingroup$ (Sigh. I wish the comments supported equations.) I don't understand why you are trying to manufacture a "df/dz". You have a surface, which to reduce confusion we'll call G(x,y) = (x, y, f(x,y)), and you need to compute on that surface the two partial derivatives (vectors), dG/dx and dG/dy. These are (1, 0, df/dx) and (0, 1, df/dy) respectively, where, unless I'm mistaken, df/dx is -2*x*f(x,y) etc If you just want your piecewise surface to look smooth say using Gouraud shading, evaluate the tangents at the sample points, create your normals (via cross products) and shade. $\endgroup$ – Simon F Oct 28 at 14:08
  • 1
    $\begingroup$ @SimonF comments do support tex equations. For example: $\frac{\alpha_1}{\gamma}$ $\endgroup$ – joojaa Oct 30 at 15:56
  • 1
    $\begingroup$ I'll try to write something tomorrow. $\endgroup$ – Simon F Nov 5 at 14:15
1
$\begingroup$

Rather than fit bilinear patches to your sample points, which are only C0 continuous$^\spadesuit$ may I instead suggest fitting the surface using Catmull-Rom splines? These will give you C2 continuity. Catmull-Rom is an interpolating spline which means it will pass through your sample points, as did the bilinear surface, but will do so smoothly from patch to patch.

$^\spadesuit$ because the first derivatives won't generally match at the boundaries of your patches

Background/Assumption

You have a function $$F:\Re^2\to\Re^3$$ of the form $$F(s,t)=[s,t,f(s,t)]$$ where $f:\Re^2\to\Re$. The function, $f$, can be considered a 'black box' in the sense that we might not be given it explicitly.

$F$ has been regularly sampled as $$S_{i,j}=F(x_{scale}.i, y_{scale}.j, f(x_{scale}.i,y_{scale}.j))$$ where $i,j\in\mathbb{Z}$. You then want interpolate between these sample points using piecewise bicubic Catmull-Rom patches.

Constructing patches

To form a particular bicubic Catmull-Rom patch, $C_{i,j}$, which has a given 2x2 set of samples $\{S_{i,j}, S_{i+1,j}, S_{i,j+1}, S_{i+1,j+1}\}$ at the corners, you need the set of 4x4 samples formed from the 2x2 at the centre and the surrounding 12 samples, i.e.$\{S_{i-1,j-1}$ through $S_{i+2,j+2}\}$, as shown below. (My apologies that this is 'hand placed' so is far from ideal) enter image description here

If we just consider the behaviour of the surface at sample, $S_{i,j}$, then the partial derivative in the $i$ direction is given by $$\frac{\partial C_{ij}}{\partial i}=\frac{S_{i+1,j}-S_{i-1,j}}{2}$$ and similarly, in the $j$ direction by $$\frac{\partial C_{ij}}{\partial j}=\frac{S_{i,j+1}-S_{i,j-1}}{2}$$

Remembering these are vectors, you can use the cross product to generate a surface normal at any of the $S_{i,j}$ points$^\clubsuit$. (Though you may need an extra set of boundary samples)

Ali: You mentioned that both partial derivatives are vectors(in i,j directions). could you write each vector components plz

Certainly: If we say $S_{i,j}=[x_{i,j},y_{i,j},z_{i,j}]^T$ then $$\frac{\partial C_{ij}}{\partial i}=\frac{1}{2}.[(x_{i+1,j}-x_{i-1,j}),(y_{i+1,j}-y_{i-1,j}),(z_{i+1,j}-z_{i-1,j})]^T$$ $$\frac{\partial C_{ij}}{\partial j}=\frac{1}{2}.[(x_{i,j+1}-x_{i,j-1}),(y_{i,j+1}-y_{i,j-1}),(z_{i,j+1}-z_{i,j-1})]^T$$

If you've sampled the surface 'finely' enough you could probably choose to render the surface by just using 2 or 4 triangles per original patch.

Alternatively, you can recursively subdivide your quad mesh to get a finer set of triangles. To double the number of points in the 'i' direction, simply insert a new point between each $S_{i,j}$ and $S_{i+1,j}$ that equals ... $$\frac{9(S_{i,j}+S_{i+1,j}) - (S_{i-1,j}+S_{i+2,j})}{16}$$ Once you have the finer grid in 'i' simply repeat with 'j' with the appropriate equivalent set of points. You can repeat this ad nauseam to get whatever triangle resolution you desire ;-)

$^\clubsuit$ (Copying some text from the comments below): I've made a couple of valid assumptions here. Noting that if a given first (partial) derivative is non-zero (which is guaranteed in your case because of the way you construct the surface), then it also is locally tangential to that surface. Further, we know your two partial derivates aren't parallel, so the cross product will construct a valid normal. FWIW For some arrangements of control points, i.e. in more general use cases, you can get so-called 'degenerate' surfaces where you can't naively apply the above rules, but you're safe here.

$\endgroup$
  • $\begingroup$ Thanks Simon. You mentioned that both partial derivatives are vectors(in i,j directions). could you write each vector components plz. $\endgroup$ – ali Nov 6 at 23:41
  • 1
    $\begingroup$ Thanks for the update. My math is not very good. I thought derivative is the rate of change of "function value" with respect to one of its arguments(unless it is a vector-valued function and you are assuming that the point S(x,y,z) is the function value); the gradient is a vector but not the partial derivatives, is that correct? $\endgroup$ – ali Nov 7 at 22:43
  • $\begingroup$ "I thought derivative is the rate of change of "function value" with respect to one of its arguments" That is indeed correct. But for a surface, if a given first (partial) derivative is non-zero (which is guaranteed in your case because of the way you construct the surface), then it also is locally tangential to the surface. Further, we know your two partial derivates aren't parallel, so the cross product will construct a valid normal. (FWIW For some arrangements of control points, in more general use cases, you can get so-called 'degenerate' surfaces, but you're safe here) $\endgroup$ – Simon F Nov 8 at 10:56
  • $\begingroup$ "but for a surface, if a given first (partial) derivative is non-zero then it also is locally tangential...". I am not making a fuss :) but your argument already assumes partial derivative to be a vector(otherwise it won't be tangential) whereas I think it is scalar. $\endgroup$ – ali Nov 8 at 14:24
  • 1
    $\begingroup$ Perfect. It's all clear now. I had missed the capital F function. $\endgroup$ – ali Nov 8 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.