Programming Overview
Programming Overview
Programming Overview
This work by John Galeotti and Damion Shelton, © 2004-2022, was made possible in part by NIH NLM contract#
HHSN276201000580P, and is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this
license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 171 2nd Street, Suite 300, San 1
Francisco, California, 94105, USA. Permissions beyond the scope of this license may be available by emailing [email protected].
The most recent version of these slides may be accessed online via http://itk.galeotti.net/
Goals for this lecture
5
Reference & Review Material
Books
C++ How to Program - Deitel & Deitel
Teach Yourself C++ in 21 Days - Liberty
Using the STL: The C++ Standard Template Library - Robson
Design Patterns; Elements of Reusable Object-Oriented
Software - Gamma et al.
Websites
http://docs.python.org/tutorial/
http://docs.python.org/reference/index.html
http://www.cppreference.com/
I use this one more than the rest.
http://www.cplusplus.com/doc/tutorial/
http://www.sgi.com/tech/stl/table_of_contents.html
6
C++ vs. Python
C++
Compile and Link
Low-level language (but standardized higher-level libraries available)
Writing code takes longer
Code runs very fast
Python
Interpreted
Very high level language
Writing code is quick and easy
Python code runs more slowly, but…
Python can call precompiled C/C++ Libraries
Best of both worlds
So ITK could should execute at full compiled speed, even when called
from Python.
7
Formatting note
8
Python Example Code
(Take notes as needed!)
# Everything on a line after a # is a comment
# Warning: Indentation matters in Python!
import SimpleITK as sitk # use sitk as the module name
9
Python Example Code
(Take notes as needed!)
# Continuing from the previous slide...
sliceNum = 1
while sliceNum < 31: # indention must match!
pixelValue = 16 + 4*sliceNum
imagevolume[96,96,sliceNum] = pixelValue
print(pixelValue)
sliceNum = sliceNum+1
image_viewer = sitk.ImageViewer()
image_viewer.SetTitle(’VolTitle’)
12
Don’t freak out about what’s next
14
Class membership
15
Public membership
16
Private membership
17
Protected membership
18
ITK and membership
19
Why do it this way?
20
Inheritance in a nutshell
21
Overloading
22
An example of inheritance in a
graphical drawing program
Shape
Polygon
Triangle
Quadrilateral
Rectangle
Trapezoid
Rhombus
Pentagon
ConicSection
Ellipse
Circle
Parabola
23
An example of ITK inheritance
itk::DataObject
itk::ImageBase< VImageDimension >
itk::Image< TPixel, VImageDimension>
24
C++ Namespaces
25
C++ Namespaces, cont.
27
C++ Virtual functions
29
C++ Example of polymorphism in a
graphical drawing program
Shape: DrawSelf() = 0;
Polygon: int vertices; DrawSelf() connects vertices with line segments
Triangle: vertices=3
Quadrilateral: vertices=4
Rectangle
Trapezoid
Rhombus
Pentagon: vertices=5
ConicSection
Ellipse: DrawSelf() uses semimajor and semiminor axes
Circle: forces length semiminor axis = length semimajor
Parabola
30
Generic programming
31
Image example
32
Image example, cont.
34
Anatomy of a templated class
35
Anatomy of a templated class
36
Anatomy of a templated class
37
Anatomy of a templated class
38
Anatomy of a templated class
39
Specialization
40
Derivation from templated classes
41
Partial specialization
42
Templated class instances
43
using shorthand type names
44
using shorthand type names
45
Fun with using
46
Naming of templates and using
47
Be careful
48
Typenames
typename exists to “optionally” help the compiler
Different compilers handle it differently
In general, you can take it to mean that you are
promising the compiler that what follows is some sort of
valid type, even if the compiler can’t see that yet
Example of when to use and not use typename:
using PixelType = Tpixel;
// template parameter names don’t need typename
using Superclass = ImageBase<VImageDimension>;
// direct class names don’t need typename either
http://blogs.msdn.com/slippman/archive/2004/08/11/212768.aspx
https://en.cppreference.com/w/cpp/language/dependent_name
https://en.cppreference.com/w/cpp/language/type_alias
50
.hxx, .cxx, .h
51
Did this all make sense?
If not, you probably want to sick to Python or C++ SimpleITK
If you want to use full C++ ITK (not required for this class):
It’s ok if you’re a little rusty on the details, etc.
It’s helpful if you have seen and used some of this stuff before.
If this is mostly new to you:
Understand that neither I nor the TA will teach you how to do basic
programming in Python or C++
You should probably use mostly SimpleITK
Beware that SimpleITK lacks many of ITK’s more advanced features, including several
types of registration and the ability to tweak less frequently used parameters.
If you don’t know how to write and compile C++ programs, then I recommend
using Python!
CMU 15-112: https://www.cs.cmu.edu/~112/
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-
programming-using-python-january-iap-2011/
You could also take a class on C++
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-s096-introduction-to-c-and-c-
january-iap-2013/
52
Final advice
53