# a question about vtkImplicitFunction::EvaluateGradient

**URL:** https://discourse.vtk.org/t/a-question-about-vtkimplicitfunction-evaluategradient/7999
**Category:** Support
**Tags:** jobs
**Created:** [March 7, 2022, 9:04am UTC](https://discourse.vtk.org/t/a-question-about-vtkimplicitfunction-evaluategradient/7999 "2022-03-07T09:04:00Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![KeyboardMan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/keyboardman/32/968_2.png) [@KeyboardMan](https://discourse.vtk.org/u/KeyboardMan)
#### Post date: [March 7, 2022, 9:04am UTC](https://discourse.vtk.org/t/a-question-about-vtkimplicitfunction-evaluategradient/7999/1 "2022-03-07T09:04:00Z")

</div>

The description on the documentation is "Evaluate function gradient at position x-y-z and pass back vector.But I don’t understand what it does. I created a new class，inherited from vtkImplicitFunction,i overrid EvaluateGradient

```auto
void MyFuntion::EvaluateGradient(double x[3], double g[3])
{

}

```

did nothing，This doesn’t seem to affect my use of it，Can someone tell me why?

---

<div class="post-metadata">

### Author: ![Yohann\_Bearzi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yohann_bearzi/32/671_2.png) [@Yohann\_Bearzi](https://discourse.vtk.org/u/Yohann_Bearzi)
#### Post date: [March 7, 2022, 12:49pm UTC](https://discourse.vtk.org/t/a-question-about-vtkimplicitfunction-evaluategradient/7999/2 "2022-03-07T12:49:37Z")

</div>

Are you creating a custom class inheriting from `vtkImplicitFunction`?

---

<div class="post-metadata">

### Author: ![KeyboardMan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/keyboardman/32/968_2.png) [@KeyboardMan](https://discourse.vtk.org/u/KeyboardMan)
#### Post date: [March 8, 2022, 1:23am UTC](https://discourse.vtk.org/t/a-question-about-vtkimplicitfunction-evaluategradient/7999/3 "2022-03-08T01:23:59Z")

</div>

I want to generate a shape using a custom function，The function seems to be implemented, but I did not implement the “EvaluateGradient” function,So I don’t know how it will affect my future use.

---

<div class="post-metadata">

### Author: ![Jens\_Munk\_Hansen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jens_munk_hansen/32/1215_2.png) [@Jens\_Munk\_Hansen](https://discourse.vtk.org/u/Jens_Munk_Hansen)
#### Post date: [March 8, 2022, 11:42am UTC](https://discourse.vtk.org/t/a-question-about-vtkimplicitfunction-evaluategradient/7999/4 "2022-03-08T11:42:26Z")

</div>

I have implemented numerous implicit functions for use with vtkClipPolyData, where I have simply returned a null vector. Specifically vtkClipPolyData does not make use of the gradient function. Personally, I have only used the EvaluateGradient for testing my implicit functions by visualizing a vector field.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 8, 2022, 2:07pm UTC](https://discourse.vtk.org/t/a-question-about-vtkimplicitfunction-evaluategradient/7999/5 "2022-03-08T14:07:53Z")

</div>

Hello,

My two cents: x[3] is your input coordinates (x,y,z)? and you put the result in g[3]. Though, I think you should declare the `g` output parameter as a pointer.

```cpp
void MyFuntion::EvaluateGradient(double* x, double* g)
{
     /// h is some small separation value (e.g. grid cell size)
     g[0] = my_function(x[0] + h, x[1], x[2]) - my_function(x[0] - h, x[1], x[2])) / (2 * h);
     g[1] = my_function(x[0], x[1] + h, x[2]) - my_function(x[0], x[1] - h, x[2])) / (2 * h);
     g[2] = my_function(x[0], x[1], x[2] + h) - my_function(x[0], x[1], x[2] - h)) / (2 * h);
}

```

Does this make sense?

regards,

Paulo

Edit, making `my_function()` taking x,y,z coordinates.

---

<div class="post-metadata">

### Author: ![KeyboardMan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/keyboardman/32/968_2.png) [@KeyboardMan](https://discourse.vtk.org/u/KeyboardMan)
#### Post date: [March 9, 2022, 1:26am UTC](https://discourse.vtk.org/t/a-question-about-vtkimplicitfunction-evaluategradient/7999/6 "2022-03-09T01:26:04Z")

</div>

I think g is already a pointer,I roughly understand how to return a gradient，But I still don’t understand what the gradient does?

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [March 9, 2022, 11:13am UTC](https://discourse.vtk.org/t/a-question-about-vtkimplicitfunction-evaluategradient/7999/7 "2022-03-09T11:13:03Z")

</div>

I believe the primary purpose is for isocontouring: generating normals on the resulting isosurface. Another use is determining in/out in some situations.

A great way to answer questions like this is to search the VTK source code and/or examples for EvaluateGradient and see how it’s used.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 9, 2022, 5:39pm UTC](https://discourse.vtk.org/t/a-question-about-vtkimplicitfunction-evaluategradient/7999/8 "2022-03-09T17:39:06Z")

</div>

In my example above is how I compute the orthogonal components (x,y,z) of the gradient vector. `h` may even have components itself: hx, hy and hz (e.g. anisotropic grid cells). In this case, it becomes:

```cpp
void MyFuntion::EvaluateGradient(double* x, double* g)
{
     /// hx, hy, hz are small separation values (e.g. grid cell sizes)
     g[0] = my_function(x[0] + hx, x[1], x[2]) - my_function(x[0] - hx, x[1], x[2])) / (2 * hx);
     g[1] = my_function(x[0], x[1] + hy, x[2]) - my_function(x[0], x[1] - hy, x[2])) / (2 * hy);
     g[2] = my_function(x[0], x[1], x[2] + hz) - my_function(x[0], x[1], x[2] - hz)) / (2 * hz);
}

```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 9, 2022, 5:49pm UTC](https://discourse.vtk.org/t/a-question-about-vtkimplicitfunction-evaluategradient/7999/9 "2022-03-09T17:49:42Z")

</div>

Yes, parameters declared like that “decay” to pointers inside the function. But it is a good practice to declare them explicitly as pointers to avoid misleading anyone else reading your code. Declaring them as pointers makes it clear that the array elements will be changed inside the function. IMO, the form `void foo( double x[] )` being equivalent to `void foo( double* x )` is a language design flaw.

cheers,

Paulo

---

<div class="post-metadata">

### Author: ![KeyboardMan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/keyboardman/32/968_2.png) [@KeyboardMan](https://discourse.vtk.org/u/KeyboardMan)
#### Post date: [March 10, 2022, 6:00am UTC](https://discourse.vtk.org/t/a-question-about-vtkimplicitfunction-evaluategradient/7999/10 "2022-03-10T06:00:49Z")

</div>

this is a good suggestion

---

<div class="post-metadata">

### Author: ![KeyboardMan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/keyboardman/32/968_2.png) [@KeyboardMan](https://discourse.vtk.org/u/KeyboardMan)
#### Post date: [March 10, 2022, 6:02am UTC](https://discourse.vtk.org/t/a-question-about-vtkimplicitfunction-evaluategradient/7999/11 "2022-03-10T06:02:15Z")

</div>

That’s a good idea, I really should read the source code
