[vtkusers] VTK Java/C Wrapping/Coding Problem

Luke Dodd i.like.bread at gmail.com
Thu Aug 19 10:28:36 EDT 2010


I was going for a simple but dirty approach.

Get the vtkObjectBase.vtkId IS a pointer to the vtk object. You could
pass it into C++ via JNI as a long and then simply cast/memcpy it into
a pointer with the correct type. We can avoid thinking about how to
use a java object from C, we can just cut out the java object "middle
man".  If you actually need to call methods on a java object from C
(say a java object you have written yourself) then it is a different
matter entirely.

Sketched out in code:

JAVA:
vtkObjectBlah blah = new vtkObjectBlah(); // java vtk object that we
want to now pass into C land
long id = blah.vtkId // won't work - it's a protected field so have to
do something horrible, or make changes to vtkObjectBase.java to get it
giveToC(id) // giveToC is native method backed by JNI code - it takes a long

C:
void giveToC(long id){
   vtkObjectBlah * blah = (*vtkObjectBlah)id // somehow cast long into
pointer - don't know enough C to know if this is an allowable cast
   blah->Method(); // should work, we now have a normal vtk object pointer
}

.........

I know very little about JNI - that's just a sketch of what I meant.
You don't really need to consider vtkGlobalJava hash, it takes a C
pointer and turns it into a _Java _reference to the object that's
wrapping it if there is already one - this is the other way round from
what you seem to want.

If you do this you'll have to make extra considerations for memory
management and all that stuff.

Hopefully this is enough to get you started. It's tricky for me to go
into more detail because I don't use JNI much myself, so I can't throw
together a quick example without figuring all that stuff out first.

On 19 August 2010 08:13, Ambar C <ambarc at gmail.com> wrote:
> So I'll add some more on this.
>
> What I need to do is be able to somehow get a hold of the
> vtkGlobalJavaHash.PointerToReference (which seems to be a global hash)
> in Java's VTK wrapping system, into my C++ code. I'm not quite sure
> what kind of JNI drudgery I'd need to use - so any input on that or
> whether I'm looking at the wrong part of the C++/Java wrapper would be
> very helpful. My plan after being able to access this would be to feed
> it the vtkID of the object I need to work with (both of which I've
> managed to isolate) and then use the 'get' function from the
> PointerToReference hash to get the actual C++ reference and attempt to
> use some ugly casts on them.
>
> Once again, any recommendation, link, resource, suggestion,
> encouragement/discouragement (:) would be helpful.
>
> Best.
>
> On Wed, Aug 18, 2010 at 3:22 PM, Ambar C <ambarc at gmail.com> wrote:
>> Hi Luke,
>>
>> From what I see - the field vtkID in the vtkObjectBase class is of
>> type 'long' - how would I use this to cast into C++ recognizable
>> objects? Is there some hash that the ID is a key to; or something of
>> the sort?
>>
>> Thanks,
>> Ambar
>>
>> On Thu, Aug 12, 2010 at 1:52 PM, Luke Dodd <i.like.bread at gmail.com> wrote:
>>> Hi Ambar,
>>>
>>> If you look in vtkObjectBase.java (will only exist after comping vtk,
>>> in the build directory - not the source) you will see that it has a
>>> (protected) field called vtkId. This is essentially a C pointer to the
>>> vtk object the java object is wrapping. If you retrieve value and get
>>> it into C++ with a few horrible casts you could have a pointer to the
>>> object. You'll have to work around trying to get the protected field -
>>> this is possible in java (google it) or you could tweak
>>> vtkObjectBase.java (this is annoying because vtk object base is
>>> generated when building).
>>>
>>> Hopefully that should be enough to get you started. If you need more
>>> detailed help just ask.
>>>
>>> Best Wishes,
>>> Luke
>>>
>>> On 10 August 2010 23:12, Ambar C <ambarc at gmail.com> wrote:
>>>> Hello VTK users,
>>>>
>>>> I currently have a project that's in both Java and C++, and part of
>>>> the project involves a wrapper using the Java Native Interface (JNI)
>>>> where I use functionality from my C++ libraries in the Java code.
>>>>
>>>> Now the problem that I face is, both my Java and C++ codes use VTK
>>>> objects - and when I pass down, say a vtkPolyDataAlgorithm object
>>>> created in Java through to the C++ code, I'm not quite sure how to
>>>> cast/use it.
>>>>
>>>> Please feel free to ask any questions that would help you to answer my question.
>>>>
>>>> Best Regards,
>>>> Ambar
>>>> _______________________________________________
>>>> Powered by www.kitware.com
>>>>
>>>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>>>
>>>> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>>>>
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://www.vtk.org/mailman/listinfo/vtkusers
>>>>
>>>
>>
>



More information about the vtkusers mailing list