[vtkusers] c# port of VTK

Sean McInerney seanm at nmr.mgh.harvard.edu
Mon Aug 2 18:31:54 EDT 2004


endre-somogyi at comcast.net wrote:
> No wrapping at all. This is a pure 'port' of the original c++ library.
 > Basically, I copy the contents of each original file (e.g.
 > vtkPoints.cxx) to a new file, Points.cs, make a new class, and fix the
 > errors.
> 
IMHO Debugging code into existence is usually problematic. In this case 
it will also be *extremely* laborious to maintain as the underlying VTK 
library changes. You may want to have a look at either VTK's own 
wrapping mechanism and create a C# version -OR- have a look at Swig 
<http://www.swig.org/> since it already has a means of compiling C/C++ 
interfaces into C#.

> So far, most of the files in the 'Common' directory have been ported.
> 
You go girl! ... uhhh ... Andy! But there are some caveats.

> An example of my question would be the 'vtkPoints' class. It currently
 > has method signatures of :
> void SetPoint(vtkIdType id, const double x[3]) ;
> void SetPoint(vtkIdType id, double x, double y, double z);
> 
> The first one, the one that take a const double x[3] is problimatic
 > for c#.  If I were to use a 'managed array', the c# signarure of the
 > method would be:
> public void SetPoint(IdType id, double[] x) ;
> This looks nice syntacially, however allocating thousands of length 3
 > arrays just to store vectors is VERY EXPENSIVE because they are all
 > garbage collected, not to mention that each managed array is a
 > reference type, so that tacks on an extra 12 bytes of size for each
 > instance.
> 
I am not C# savvy, but are you saying that you cannot just pass a good 
old array of 3 doubles? Whatever you pass in (array[3] or 3 doubles), 
the point coordinates are *copied* into the internal instance of 
vtkDataArray. There is no need to be allocating a new array[3] each time 
you call SetPoint. For example, you could implement the first version of 
SetPoint using the second:

void SetPoint(vtkIdType id, const double a[3])
   { this->SetPoint(id, a[0], a[1], a[2]); }

> That is why I am considering changing the interface to use a Vector
 > struct instead of a managed array. A struct in c# is a value type, it
 > is allways allocated on the stack, and has virtually no overhead. It
 > behaves just like a good old C style struct.
> 
> So, using a Vector struct, the new method signatures would look like:
> public void SetPoint(IdType id, Vector3 x) ;
> public void SetPoint(IdType id, double x, double y, double z);
> 
> The other advantage of useing a struct is that in time critical
 > sections, it can be cast as a double* and accessed just like a raw
 > memory block.
> 
> The biggest disadvantage of using a Vector struct is that in
 > introduces an unfarmiliar syntax to current VTK users who are used to
 > using a double x[3] array in c++.
> 
Well ... in the case that my assessment above is wrong *and* since you 
are porting rather than wrapping VTK ... I suppose making API changes is 
not out of the question. Just consider that the road that you are going 
down is similar to that of the Mesa port of OpenGL. You may find that 
this is more than a one man show. The point at which you port from C++ 
will be a separate branch of development. This is what I mean by 
difficult to maintain.

I think that you really should consider an automated wrapping approach.

-Sean

>>Andy,
>>
>>   I am sure that a C# port could be very popular. Are you using a 
>>wrapping approach or are you parsing C++ code to generate C#?
>>
>>   Also, I am unsure of where you are encountering the problem that you 
>>describe in your message. Could you provide a few specific instances?
>>
>>-Sean
>>
>>Andy Somogyi wrote:
>>
>>>Hello VTK users
>>>
>>>I am in the process of completly porting VTK (4.2) to c#.
>>>As this is c#, it can be used nativly from any .net language.
>>>
>>>When finished, it will be freely availble under the existing VTL lisence.
>>>
>>>My question is this:
>>>
>>>The c++ version of VTK uses length 3 arrays to represent a vector in 3 
>>>space.
>>>This is fine in c++ because you can create arrays on the stack.
>>>
>>>It is not possible to create a managed array in c# on the stack, they 
>>>are allways
>>>heap allocated and garbage collected. So using a large number of managed 
>>>arrays, or
>>>creating large numbers of transitory arrays is very inefficient.
>>>
>>>The use of a 'value type' or a 'struct' in other words is far more 
>>>effcient when
>>>large numbers of objects are involved as they are created on the stack 
>>>and are not
>>>garbage collected.
>>>
>>>I have thought about using a raw double pointer (double*) instead of an 
>>>managed array
>>>for all functions that use a vector (double[3]). This has the advantage 
>>>of being very
>>>fast, but on the other hand, it is highly error prone, and a user of one 
>>>of these functions
>>>must declare thier method 'unsafe'. I would suspsct that this would 
>>>discourage many users.
>>>
>>>There are many good vector and matrix classes avaible for .net, such as 
>>>the standard vector and
>>>matrix types that ship with directx 9.
>>>
>>>So my question to any perspective vtk.net users is would you prefer all 
>>>existing methods
>>>that currently accept a c++ style array (double[3]) accept the following 
>>>in the new
>>>c# port:
>>>
>>>1: managed array (new double[3])
>>>    advantages:
>>>    1.1 farmiliar syntax
>>>    1.2 do not have to declare 'unsafe' code
>>>    disadvanteges:
>>>    1.2 very ineffcient both in terms of size and performance
>>>
>>>2: unmanaged pointer (double*)
>>>    advanteges:
>>>    2.1 very effecient
>>>    disadvantes:
>>>    2.2 error prone
>>>    2.3 need to pin a managed array or use 'stackalloc'
>>>
>>>3: use a 'Vector3' value type
>>>    advanteges
>>>    3.1 very effcient
>>>    3.2 very safe
>>>    3.3 easy to convert to and from a 'double*'
>>>    disadvantes:
>>>    3.3 introduces unfarmiliair syntax
>>>
>>>Any ides or oppinions would be greatly appcieated
>>>
>>>thanks
>>>




More information about the vtkusers mailing list