[vtkusers] c# port of VTK

endre-somogyi at comcast.net endre-somogyi at comcast.net
Mon Aug 2 17:32:09 EDT 2004


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.

So far, most of the files in the 'Common' directory have been ported.

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.

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++.


> 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
> > 
> > 
> > 
> > _______________________________________________
> > This is the private VTK discussion list. Please keep messages on-topic. 
> > Check the FAQ at: <http://public.kitware.com/cgi-bin/vtkfaq>
> > Follow this link to subscribe/unsubscribe:
> > http://www.vtk.org/mailman/listinfo/vtkusers
> > 



More information about the vtkusers mailing list