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:Timothy Wall (twal@dev.java.net)
Date:Dec 15, 2007 2:48:56 pm
List:net.java.dev.jna.users

On Dec 15, 2007, at 4:06 PM, Richard Otero wrote:

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, it is.

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

That depends on what the structure of the data looks like. To start with, you can use Pointer, then use the various memory accessors to get the bits that you want.

You can treat the "double**" argument (judging from usage) as an array of double[]. Declare the argument as Pointer to start with. The 2D data looks like this (according to your example):

Array of columns (indexed by "i" in the code below) 0000 <addr0> 0004 <addr1> 0008 <addr2>

<addr0> d0 d1 d2 d3 ... (g.ny elements, indexed by "j" in the code below) <addr1> dm dn do dp ... (g.ny elements) ...

So you can use the Pointer accessors to get to the right element, for example: * read the first column: arg.getDoubleArray(0, g.ny) * write the element j in column i: arg.getPointer (Pointer.SIZE*i).setDouble(j * 8, value)

Doubles are 8 bytes, and pointers are Pointer.SIZE bytes.

thank you! Richard

---------------------------------------------------- The gts_isosurface_cartesian API is: void gts_isosurface_cartesian (GtsSurface *surface, GtsCartesianGrid g, GtsIsoCartesianFunc f, pointer data, double iso);

Where: surface : a GtsSurface. g : a GtsCartesianGrid. f : a GtsIsoCartesianFunc. data : user data to be passed to f. iso : isosurface value.

-----------------------------------------------------

GtsIsoCartesianFunc API is: void (*GtsIsoCartesianFunc) (double **a, GtsCartesianGrid g, int i, pointer data);

Where: a : the 2D array to be filled. g : a GtsCartesianGrid. i : the z direction index of the plane to be filled. data : user data to be passed to this function

-----------------------------------------------------

An example of it being used in C:

static void sphere (gdouble ** f, GtsCartesianGrid g, guint k, gpointer data) { gdouble x, y, z = g.z; guint i, j;

for (i = 0, x = g.x; i < g.nx; i++, x += g.dx) for (j = 0, y = g.y; j < g.ny; j++, y += g.dy) f[i][j] = x*x + y*y + z*z; }

/* Returns isosurface f(x, y, z) = iso with f(x, y, z) = x*x + y*y + z*z. */ int main (int argc, char * argv[]) { GtsCartesianGrid g; // A structure that I have using jna GtsSurface * surface; // Created a pointerType for this double iso; GtsIsoCartesianFunc func = sphere;

g.nx = 20; // number of points used in the x direction g.ny = 20; g.nz = 20; /* create data for the interval used, stored in g */ g.x = -10.0; g.dx = 20./(double) (g.nx - 1); g.y = -10.0; g.dy = 20./(double) (g.ny - 1); g.z = -10.0; g.dz = 20./(double) (g.nz - 1);

iso = 1; // unit sphere

// function to initialize a surface // I have this function working in jna surface = gts_surface_new (gts_surface_class (), gts_face_class (), gts_edge_class (), gts_vertex_class ());

// the function used to create the isosurface // this is the function I would like to get working with jna gts_isosurface_cartesian (surface, g, func, NULL, iso);

return 0; }