[vtkusers] How to use vtkMath functions in ActiViz.NET for data of type double?

Mark DeArman mark at cascadeacoustic.com
Thu Nov 9 17:02:51 EST 2017

Yes, I saw you closed your issue, but I was going to suggest we move the 
discussion over hear beforehand.  It's probably not not something I will 
get around to fixing, but I was interested in tracking down the cause of 
the behavior you were seeing.  

Moving your project into CLR C++ might simplify the work syntactically, but 
your still going to require the same careful attention.  I find it is quite 
easy to write mixed C#/C++ as long as you follow a rigid pattern for how 
you operate on your data, where you do your allocations, and how you write 
your C++ signatures.   
     Your Code

  // Create two points.
float[] pt0 = new float[3] { 0.0F, 0.0F, 0.0F };
float[] pt1 = new float[3] { 1.0F, 1.0F, 1.0F };
 fixed(float* pt0_p = pt0, pt1_p = pt1)
    // Find the squared distance between the points.
    squaredDistance = vtkMath.Distance2BetweenPoints(new IntPtr(pt0_p), new 
 double distance = Math.Sqrt(squaredDistance);
     The ActiViz generated pinvoke signature 

 [DllImport(vtkCommonCoreEL_dll, CallingConvention = 
CallingConvention.Cdecl, EntryPoint = 
internal static extern float vtkMath_Distance2BetweenPoints_21(IntPtr p1, 
IntPtr p2);

     The ActiViz generated wrapper function

public static float Distance2BetweenPoints(IntPtr p1, IntPtr p2)
    float rv = vtkMath_Distance2BetweenPoints_21(p1, p2);
    return rv;
 I don't know if it is an option for your project, but as I said 
previously, I normally use the unsafe flag, so that I can do allocation in 
C#, pin those arrays, then pass pointers into C functions to be operated 
on.  Unlike the Activiz autogenerated signature above, by not using IntPtr, 
and using strongly type pointers, its is much easier to keep track of what 
is going on. 
     Sample signature 

 [DllImport(myDSP_dll, CallingConvention = CallingConvention.Cdecl, 
EntryPoint = "myDSP_doSomething")]
internal static extern int myDSP_doSomething(double* input, double* 
     Sample C# Code

double[] x = new double[3] { 0.0, 0.0, 0.0 };
double[] y = new double[3];
 fixed(double* px = x, py = y)
         throw new MyException()

 With attention to house keeping inside of your C++ DllMain,  it is rather 
easy to keep allocations split up between managed and unmanaged, and stay 
out of trouble generating memory leaks.  So I will end up with only code 
having to do with the GUI, user interaction, in the C# side, and all of the 
data processing happening in C++.

   Mark DeArman
 Cascade Acoustic Research
 (253) 200-5353


 From: "csumushu" <csumushu at 126.com>
Sent: Thursday, November 9, 2017 5:45 AM
To: vtkusers at vtk.org
Subject: Re: [vtkusers] SPAM-LOW: How to use vtkMath functions in 
ActiViz.NET for data of type double?   
Oh wow, nice to see you again, a few days ago I had questions to ask your
advice on Github.
How about using C++ CLR instead of PInvoke? For some classes, such as
vtkTransform, I want to call the entire class, not some member functions.

Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
Powered by www.kitware.com

Visit other Kitware open-source projects at 

Please keep messages on-topic and check the VTK FAQ at: 

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20171109/573ed494/attachment.html>

More information about the vtkusers mailing list