3 messages in net.java.dev.jna.usersRe: [jna-users] Calling a Java method...
FromSent OnAttachments
Richard OteroDec 15, 2007 1:06 pm 
Albert StrasheimDec 15, 2007 2:18 pm 
Timothy WallDec 15, 2007 2:48 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Calling a Java method from the C code + pointer handling for 2D matrixActions...
From:Albert Strasheim (full@gmail.com)
Date:Dec 15, 2007 2:18:25 pm
List:net.java.dev.jna.users

Hello

----- Original Message ----- From: "Richard Otero" <rich@gmail.com> To: <use@jna.dev.java.net> Sent: Saturday, December 15, 2007 11:06 PM Subject: [jna-users] Calling a Java method from the C code + pointer handling for 2D matrix

Hello all!

I've gotten a good section of the GTS library accessible to Java. It

Nice one.

I need some help though on: gts_isosurface_cartesian. I've include the API and usage below.

It would take as an argument a pointer to a function that graphically describes a shape. For instance a sphere; f(x, y, z) = iso with f(x, y, z) = x*x + y*y + z*z.

Ideally, I would like to be able to create the function, that describes the shape, in Java and then pass the function to gts_isosurface_cartesian to have it generate the mesh for the described surface. That way later edits to the shapes or future shapes would be able to stay within the Java portion of the code.

Is this an instance where I should be using callbacks?

Yes, that appears to be the case.

If I write this function in Java; how would one handle a double **a type, as appears in the GtsIsoCartesianFunc API?

This is a bit tricky.

Since the callback function needs to make changes to this argument, you'll need to wrap it in such a way that changes are made to the memory directly or that changes are copied back from Java to the original memory.

The first thing that comes to mind is to wrap the argument as a Pointer. You can then provide an abstract base class for callback implementations (calling it something like AbstractGtsIsoCartesianFunc) that has a callback function as required by the library which then calls an abstract method with a more user-friendly signature (so that you only have to write the code to convert from Pointer to something more sensible once).

This callback method could do a few things.

It can look at the GtsCartesianGrid parameter and figure out the dimensions of the matrix you're dealing with. It could then construct a double[][], copy the contents of the native array using Pointer#getPointerArray and Pointer#getDoubleArray, delegate to the callback with the user-friendly signature, and finally write back the values from the double[][] using Pointer#write or something similar.

Alternatively, you could pass a DoubleBuffer[] to the user-friendly callback (although this is already less user-friendly to my mind). To construct this array, you could try using Pointer#getByteBuffer on each of the Pointers returned by Pointer#getPointerArray, followed by a ByteBuffer#asDoubleBuffer. This way, there is no copying, which might be preferable if you're dealing with large matrices.

JNA might be smart enough to know what to when you wrap the argument as a Pointer[], which would allow you to skip the getPointerArray step. I don't think JNA has a mapping for something like DoubleBuffer[].

You might also want to look at JNA's TypeMapper functionality to handle some of these conversion details. I think a type mapper only has access to a single argument at a time, so you won't be able to use info from the GtsCartesianGrid argument to figure out the dimensions of the matrix argument.

Hope you're able to follow the breadcrumbs and that they don't lead you too far astray. ;-)

Cheers,

Albert