[vtkusers] Using a class derived from vtkImplicitFunction in C#

Ricardo Villegas ricvilleg at gmail.com
Fri Nov 9 20:39:29 EST 2007


Hello,
I'm trying to render some implicit functions using the vtkDotNet
wrapper for C#. I've already defined my own classes derived from
vtkImplicitFunction and implemented the EvaluateFunction and
EvaluateGradient methods. Then I pass an object from this class to the
SetImplicitFunction of the vtkSampleFunction class. However, at
runtime a crash occurs and produces this message:

"Could not downcast pointer to native class"

It seems the CLR can't convert the reference object of my class to the
one expected in the SetImplicitFunction method.
What's the problem here? What am I missing? Do I need reimplement
other methods besides the two aforementioned?
Here's the code I'm using:

// Defining my class

public class Funcion1 : vtkImplicitFunction class Funcion1 : vtkImplicitFunction
{
     public Funcion1()
    { }

    public double EvaluateFunction(double[] xyz)
   {
       return (xyz[0] * xyz[0]);
   }

   public void EvaluateGradient(double[] xyz, double[] g)
  {
       g[0] = 2.0 * xyz[0];
       g[1] = 0.0;
       g[2] = 0.0;
   }
}

------
   Later, I try to use it:

           Funcion1 func = new Funcion1();

           vtkSampleFunction functionSample = new vtkSampleFunction();


            // Crash occurs here!!!!
            functionSample.SetImplicitFunction(func);

            functionSample.SetSampleDimensions(100, 100, 100);
            functionSample.CappingOn();

            vtkContourFilter functionSurface = new vtkContourFilter();
            functionSurface.SetInputConnection(functionSample.GetOutputPort());
            functionSurface.SetValue(0, 0.0);

            vtkPolyDataMapper functionMapper = new vtkPolyDataMapper();
            functionMapper.SetInputConnection(functionSurface.GetOutputPort());

            vtkActor function = new vtkActor();
            function.SetMapper(functionMapper);
            function.GetProperty().SetColor(0.6902, 0.7686, 0.8706);


Could somebody please help me?

Thanks in advance

Ricardo Villegas



More information about the vtkusers mailing list