ITK
ITK Development
[edit]What are .txx files?
[edit].txx files are where ITK puts the implementation parts of templated classes (actually class templates). These can't go in a standard .cxx (or .cpp) file, because the compiler needs to have the complete implementation of a template in any compilation unit (normally a cxx file) that uses a template. This is because a template is just a pattern for making a class, and can't actually get built by the compiler until it has a specific type to make an actual version of a template (an instantiation). For most developers, this means that template implementations just get put into header files (.h or .hpp). In fact, this is effectively what happens in ITK - the .txx file gets included by the .h file. This is what you should do in any case if you want to write a new templated class: just include the .txx file at the bottom of your .h file.
Of course the actual story is more complicated. For example, it is possible for the developer to predict in advance what types will probably be used with the template, and to make explicit instantiations of these in a .cxx file that gets compiled into an object. This can save a lot of compilation time. However, if you try to use a different type, and you don't have the complete implementation available to your compilation unit, you will get linker errors.
If you want to know more about templates in general, this is a good source: http://www.parashift.com/c++-faq-lite/templates.html
ITK has in fact a much more sophisticated mechanism that provides explicit instantiations where possible and includes the template implementation only where an explicit instantiation is unavailable. If you want, you can read about it here: http://cmake.org/Wiki/Proposals:Explicit_Instantiation . Fortunately, if you are just using ITK classes in your code, it works transparently. And if you want to create a new class that inherits from an ITK class, you don't have to understand or use this mechanism: as mentioned just include your .txx at the bottom of your .h file and you're good to go.
Actually there is one more complication that you may need to know about. The VXL component (math stuff mostly) of ITK uses an entirely different mechanism for template implementations. For details see http://public.kitware.com/vxl/doc/release/books/core/book_17.html (scroll down to "C.5 On the Use of Templates") . Basically, VXL provides only explicit instantiations, so if you want to use some odd-ball type as a template parameter, you'll need to make your own explicit instantiation. VXL (and hence ITK) provide a macro for doing this in a single line. Refer to the linked page for details.
What is the difference between Array and Vector, and Between Array2D and Matrix?
[edit]Basically, Array and Array2D do memory allocation on the heap to store their data. Their sizes are fixed when they are constructed and cannot be changed thereafter. Vector and Matrix in contrast contain their data, so if they are constructed on the stack, their data is also on the stack and no malloc or equivalent is called, which is much faster. Their sizes are set at compilation time.
You can see this difference in the way they are declared:
<source lang=c>
// X is a Vector of dimension 3. The dimension is a template parameter that must be set at compile time. itk::Vector<double,3> X; // Y is an array. You can declare it without a size and set the size at run time. itk::Array<double> Y; // Now set its size. Once set, it cannot be changed. Y.SetSize(3)
// This is another way to create the object Y. It has the same result. // Unlike a Vector however, the dimension can be set with a variable. itk::Array<double> Y(3); </source>
What this means basically is that Vector and Matrix are very fast and cheap to make copies of, and use a little less memory. These advantages are only significant when Vector and Matrix are smallish. Feel free to pass around Vector and Matrix objects by value, but you should probably not use Vector or Matrix for very large arrays. Array and Array2D objects should be passed around by reference.
Array derives from vnl_vector. Array2D derives from vnl_matrix. Matrix has a vnl_matrix_fixed as a member. Vector on the other hand does not derive from, nor contain, any vnl object (although a vnl_vector_fixed object does exist). However, it has methods to provide copies of itself as a vnl_vector_fixed; copies of itself and vnl_vector_fixed are cheap.