Archive for the ‘Teaching’ Category

Annual C++ textbook update

Monday, October 8th, 2012

A very nice person, by the name of Allen B. Downey, has written several textbooks. All of which are available under free licences. So you can have the source and tinker with them.

A few years ago, I adopted his free-content C++ book as my standard course text. Since then, I’ve made a few updates every year. The latest version is downloadable from Google Docs.

I have an update to “my” OpenGL book in the pipeline too.

Extending Clutter-Box2d

Monday, May 7th, 2012

The following is a block diagram of ClutterBox2dmm. It’s at a high level of abstraction, so most of the detail is lost. What’s of interest is that Cluttermm is stacked on top of Clutter and that there exists a Clutter-Box2d.

Clutter is the graphics library I use for first year C++. I’ll explain why some day. The Clutter library is written in C. Cluttermm contains the C++ bindings for Clutter. Therefore we can use the awesome accelerated graphics of Clutter in C++. Clutter also has a related library called Clutter-Box2d. The Clutter-Box2d library contains C bindings for the upstream (and awesome) Box2D C++ library. Box2D is used in many places including, I believe, in Angry Birds. Box2D has also been cloned in many languages such as JavaScript. On top of Clutter-Box2d, I mantain Clutter-Box2dmm which are C++ bindings to Clutter-Box2d (yes, C++ bindings to C bindings for a C++ library….but it does make sense).

I’ve just extended both Clutter-box2d and Clutter-box2dmm to support the IsFixedRotation() functionality in Box2d. This hack isn’t hard. It does require modifying the C library Clutter-Box2d and then modifginy the C++ library Clutter-Box2dmm on top of this. The relevent commits are here and here. If you need this feature in Clutter-box2dmm you currently have to compile from source. Get it whilst it’s hot.

Recording presentations

Monday, September 26th, 2011

I have had a need to record lecture presentations. To this end I’ve hacked up some software which (a) takes a feed from a webcam, and (b) takes a PDF/ODF presentation and combines them into a WebM file.

For the code:

git clone git://gitorious.org/lecturec/lecturec.git

A simple overview:

Creating Arbitrary Foo for QuickCheck

Tuesday, May 17th, 2011

I always write semi-automated tests, whatever language I’m using. In Haskell my preferred framework is QuickCheck, which goes beyond what an XUnit framework is capable of. I come to QuickCheck as a convert from the old religion of Object Oriented programming. However much I’ve adopted functional practices I find myself, in the darkest of times, falling back on idioms from a past life. Recently I’ve been testing code that looks like the following:

data Squad = Squad { players :: Set Char,
team :: Set Char }

So what I’ve got is some type which is composed of a set and some other set which is a subset of the original. I hope the Squad metaphor is reasonable. In many sports you’ll have a panel of players, called the squad, and then for any given game you’ll have a team which is a subset of the players in your squad. To simplify this problem, each player is identified by a single character rather than a name. Now, we don’t have a dependent type system here. Just plain old Haskell. So the constraint that “team is a subset of players” is not enforced by the type system.

Now I want to tell Haskell to create Arbitrary instances of Squad.

randSubset :: Ord a => Set a -> Gen (Set a)
randSubset xs = do {
mask <- vectorOf (Set.size xs) arbitrary;
return (Set.fromList [ e | (e,True) <- zip (Set.toList xs) mask ]) }

instance Arbitrary Squad where
arbitrary = do {
players <- listOf1 (arbitrary :: Gen Char);
team <- randSubset (Set.fromList players);
return (Squad (Set.fromList players) team))}

The smarts here is in randSubset, but listOf1 ensures that there’s at least one player in our squad. I was struggling when playing around with a random number generator, to try and pull out random elements from a Set. Such thinking is a false idol. With some guidance from the only person on the planet to be 90% Real Jedi I realised you can just use the common generator combinators as provided by QuickCheck.

So the fragment

do {
mask <- vectorOf (Set.size xs) arbitrary;
return (Set.fromList [ e | (e,True) <- zip (Set.toList xs) mask ])}

asks for an arbitrary list of boolean values where the list is the same length as the size of the input Set. Then where the ith element of the list is true, the ith element of the set is considered to be in the subset, or omitted otherwise. It’s a nice idiom, useful anywhere you’re playing with sets that aren’t quite arbitrary themselves, but are constrained to be a subset of another.

A C++ Cheat Sheet

Sunday, October 4th, 2009

Many of my students have small, niggling issues with C++ syntax -v- Java syntax. I’ve put together a syntax cheat sheet.

c++-crib-sheet, now updated with all the feedback from comments. Thanks a lot.

I’d appreciate it if people could point out errors and omissions.

Final year projects in Launchpad

Saturday, January 10th, 2009

Two of my students so far have released their (in development) final year project code as Free Software using Canonical’s Launchpad system. I hope a few more of my students will follow suit but, of course, I’m not insisting that they do so. The students are

The project have not been submitted yet so I won’t make any value judgement on them. But, as the code exists, it is obvious that some progress has been made.

Computer Graphics Algorithms (first assignment)

Wednesday, October 22nd, 2008

As we discussed last week. I’m not in for lectures on Thursday 23rd of October. That’s why we had two lectures last week and it’s why we’ll have two lectures next week. In the mean time I want you to get started on the assignment.

For the first assignment I want you to load up Suzanne (the blender monkey) from a raw data file into an OpenGL context. You’re to take the raw data and create an onscreen rendering of the model by drawing quads and triangles on an OpenGL canvas. My version of the raw data is here. But you can create your own in Blender if you want.

Quick tip: Have a look at the data in the file using a text editor. Notice the structure of the data on each line? Why is it so regular?

I expect everyone to load the model up and display it onscreen. Of course that means that everyone will get a “B” (under the CMIS marking scheme) in this part of the assignment. Those of you looking for “A”‘s will have to do something I don’t expect – add some value. The second part of the assignment will extend your skills further. However, with the first part, I want to ensure that everyone has basic OpenGL skills.

Listy: A simply useless TurboGears application

Tuesday, July 22nd, 2008

I finally sat down for a day to write an application I’ve been wanting to write for a while. I’m on holidays, so I can do fun things like this now. I choose TurboGears as the framework to achieve this. I could have chosen other frameworks such as Django, Ruby on Rails, PHP Cake, etc…. But I didn’t. I choose TurboGears because it’s a Python framework and I feel that I need to develop my Python skills. I choose TurboGears over Django because the Fedora guys have written some very neat applications koji and bodhi. I didn’t choose it because it’s necessarily better, but because I’ve seen some good results achieved using TG and because I have a penchant for build systems which’ll make me want to dig into koji at some later date.

TG is based on the model-view-controller design pattern. If you’re a student of mine you certainly know this pattern :) For the model it uses SQLObject as an object relational mapper. The controller is CherryPy and to construct an AJAXified view one uses the Kid template engine and Mochikit. TG essentially integrates these four independent projects to make them easier to develop for and deploy.

I now like developing in TG as I’ve figured out how to do it. I did find it extremely frustrating to figure out how to develop in TG. This is down, I think, to two issues;

  1. me expecting the SQLObject ORM to do more than it does, and
  2. the documentation in each of the five projects being written in a different manner and to differing standards.

I found the TG screencasts great for giving an overview of the project and explaining that the separate components work together. I found the screencasts gave a poor explanation of how to make the components work together. Most of the tutorials/screencasts I found concentrated on “Using SQLObject with TG” or “CherryPy controllers in TG” but I’ve not yet found an example of a very simple application taken all the way from start to finish using TG.

Enter Listy. Listy is the most simple application I could think of. It allows you to Create, Read, Update and Destroy list enteries. That’s it. It’s really dumb but exercises all aspects of TG in a very simple manner. I’m going to assume that you know how to use tg-admin to set up a default project and that you’ve read the TG overview documentation.

The model is simple. A list contains items which are Unicode strings:

class ListItems(SQLObject):
  item_text = UnicodeCol()

There are no one-to-one relations or many-to-one etc… If you want to see examples of more complex models have a look at the TG book. I don’t really like SQLObject. It’s too SQL’y for me particularly when it comes to creating relationships between data. One of the gotcha’s for me is that if I create a new ListItems object and modify an item the results are not persisted to the DB eg:

l = ListItems(item_text="foo")
l.item_text = "bar"

I found that, unless you change some of your project configuration, you have to use the set call.

l = ListItems(item_text="foo")
l.set(item_text="bar")

This was very counter intuitive for me. As ListItems derives from SQLObject there are some nice select functions defined and ListItems will also have an “id” field as it’s primary key (you can change this).

In the contoller I expose four simple methods. The returns the entire list as a dictionary via the “welcome.kid” template….we’ll look at that later. The latter three methods expose themselves as JSON calls. Take the modify(self, item_id, item_text) call. TG will expose this as either “/modity?item_id=1&item_text=foo” or the more RESTful “/modify/1/foo”. I like the RESTful way of doing things.


class Root(controllers.RootController):
  @expose(template="listy.templates.welcome")
  def index(self):
    return dict(list=ListItems.select())

  @expose(format="json")
  def add(self):
    l = ListItems(item_text = "Change this text")
    return dict(list=l)

  @expose(format="json")
  @validate(validators={'item_id': validators.Int(),
    'item_text': validators.PlainText()})
  def modify(self, item_id, item_text):
    l = ListItems.get(item_id)
    l.set(item_text=item_text)
    return dict(list=l)

  @expose(format="json")
  @validate(validators={'item_id': validators.Int()})
  def delete(self, item_id):
    ListItems.get(item_id).destroySelf()
    return dict(result=True)

The validators in the above code are nice. They ensure that the passed in values are validated that, say item_text is simply plain text and not some SQL injection string like ';drop all; nuke world;.

The view was where I got a little confused. Mainly because if you want to use Mochikit with TG you have to add tg.include_widgets = ['turbogears.mochikit'] to your app.conf. I don’t understand this. I know there are other AJAX technologies (Eg: jQuery) that may be used with TG but it would be nice to have the default one turned on by default. Anyway…. I have a very simple page defined in welcome.kid. It simply takes the list returned in the dictionary from index in the controller and prints it to screen.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#"
    py:extends="'master.kid'">
<head>

<script type="text/javascript">
// intentionally blank for the moment.
<script>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" py:replace="''"/>
<title>Listy
</head>
<body>
  <h1>A List

  <ul id="list">
    <li py:for="l in list" id="${l.id}"><input type="text" id="input${l.id}" value="${l.item_text}"
    onChange="modify_item(${l.id})" /> <a href="#" onclick="delete_item(${l.id})">(delete)</a></li>
  </ul>
<a href="#" onclick="add_item()">Add list item
</body>
</html>

This produces the list as returned by index() and displays each entry in a textbox. Each textbox has a unique id of the form input10. The onChange event for each textbox is an ECMAScript function called modify_item(id). There’s a delete link on each line whose ECMAScript onClick event is hooked up to delete_item(id). Finally there’s a link to add an item to a list whose onClick event fires the add_item() method.

Let’s have a look at the modify_item method first. It’s the most straightforward.

function modify_item(id) {
r = doSimpleXMLHttpRequest("/modify/" + id + "/" + $('input'+id).value);
}

The call doSimpleXMLHttpRequest() is provided by Mochikit and will work on all major browsers (at least). It simply calls the URL /modify/id/value which was exposed by the controller earlier. The $('input'+id) is Mochikit shorthand for “get me the DOM element with the following id: input10″ (assuming the value of id=10).

The code for add_item() is a little more interesting.

function add_item() {
r = doSimpleXMLHttpRequest("/add");
r.addCallback(add_item_handler);
}

It calls the previously exposed “/add” URL. And then it registers a callback. It is at this stage that we realise the Asynchronous aspect of AJAX. Our simple HTTP request may take a while. Maybe a second or maybe a lot longer if the server is under heavy load. The callback is executed when the results are returned. Here’s the code for the callback

function add_item_handler(result) {
item = evalJSONRequest(result);
id = item.list.id;
node = LI({'id':id});
input = INPUT({'type':'text', 'onchange': 'modify_item('+ id +')','id':'input' + id, 'value':item.list.item_text})
node.appendChild(input)
node.appendChild(SPAN(" "));
node.appendChild(A({'href':'#', 'onclick':'delete_item('+id+')'}, "(delete)"));
(document.getElementById("list")).appendChild(node);
}

The callback takes the result and turns it into an ECMAScript object. Then it creates a new XML node which contains a list item (<li>) which has three children; an input field, a span field and a hypertext link. Given this list item we find the list in the document and append it as a child of the list. This is all pretty standard DOM manipulation. I like the ease with which Mochikit provides the asynchronous callback.

Just to complete the picture, the code for delete_item() is

function delete_item(id) {
elem = document.getElementById(id);
hideElement(elem);
doSimpleXMLHttpRequest("/delete/" + id);
}

where the function hideElement is a Mochikit special. Essentially I tell the controller to delete the specified object and then hide it in the view. I should only really hide the element in the callback for successful completion….but I’m lazy. You can also register callbacks to handle the condition where the doSimpleXMLHttpRequest fails. I don’t. I’m lazy.

Anyway. There’s a very simple TG application from back to front. It looks like the following picture

A simple TurboGears application

Cell BE development and Fedora 9

Monday, July 14th, 2008

IBM have Cell development tools…ummm…..obviously. Their tools are GCC :) And Jochen at IBM has been quite keen to push packaged versions of these tools into Fedora. I have a need for them on Fedora 9 (on PS3). Interested parties can follow the development here. I’ve got every confidence that their GCC package will follow soon.

This is great stuff for a few reasons. One; being totally self-centred, I need a spu-gcc package. I have my own internal one but it’d be nicer to have stuff upstream. Two; IBM previously offered their Cell SDK on RedHat Enterprise Linux. This shows that they are quite happy to engage with the wider community. I know they’ve engaged in lots of different areas but there is a qualitative difference between supporting Linux by providing a download and actively working with the community.

It’s quite novel to be talking to IBM (actually Jochen specifically) and for them to take on board my requirements and use them to inform internal development. It would be hard to buy that level of support from most companies and, at the moment, I’m getting it for free from Big Blue.

Maybe they didn’t ;) Maybe they’re just doing what they were going to do anyway. But at least it’s “The Right Thing ™” and I’m astroturfing them.

The spu-binutils package is available now.

Fedora 9 Cell processor packages

Monday, June 2nd, 2008

I’m building a toolchain for the Cell processor. This is the processor found in the PS3 and IBM blade servers. Others have built the toolchain before notably IBM, the Barcelona Supercomputing group and YellowDog Linux. However, these sets of packages do not move at the speed that Fedora does. They are currently GCC 4.1 based whereas Fedora 9 includes GCC 4.3. Furthermore, the other implementations of a Cell toolchain do not include the Fedora patches to GCC and binutils. Thus my packages are closer to the Fedora packages than the IBM ones. This makes maintenance and debugging easier from the system integrators point of view (i.e. my point of view).

This toolchain is also part of my evil plan to replace Mesa on the Cell architecture with Gallium3d. Gallium3d is an implementation of OpenGL (amongst other things) using the Cell SPUs. This will allow developers to develop OpenGL applications on PS3 Linux.

In order to do this I need to build the following packages

  • binutils for the SPU,
  • gcc for the SPU,
  • libspe2 in order to control the SPUs,
  • newlib for the SPU, and
  • gallium3d using the toolchain developed in the previous packages.

My libspe2 package is based on the IBM package but recompiled for Fedora 9, the binutils package is based on the binutils in Fedora 9. Both of these packages are complete. I’m currently arguing with GCC. Then I’ll tackle newlib (a much easier issue) and then Gallium3d.

Test’em and beat me if they don’t work.