====== RGL library ====== R has a nice library for plotting in 3d called **''rgl''** (because it wraps the OpenGL framework). The functions are pretty self explanatory. There are 2 interfaces with separate sets of functions that are named either rgl.* or *3d. The *3d are slightly higher level and may be preferable. However they were added later and my code therefore uses the rgl.* functions (for example in the plotneuron3d routine) Quoting rgl's help: ''At present, there are two main practical differences between the r3d functions and the rgl.* functions is that the r3d functions call open3d if there is no device open, and the rgl.* functions call rgl.open. By default open3d sets the initial orientation of the coordinate system in 'world coordinates', i.e. a right-handed coordinate system in which the x-axis increasingfrom left to right, the y-axis increases with depth into the scene, and the z-axis increases from bottom to top of the screen. rgl.* functions, on the other hand, use a right-handed coordinate system similar to that used in OpenGL. The x-axis matches that of r3d, but the y-axis increases from bottom to top, and the z-axis decreases with depth into the scene. Since the user can manipulate the scene, either system can be rotated into the other one. '' {{warping_manual:3dcoords.png|}}{{warping_manual:rglcoords.png|}} [do ''?r3d'' in R to see the code for those plots] ====== Coordinates ====== One vexing problem is the choice of coordinate system to display objects. As mentioned in the supplemental information for Jefferis, Potter et al 2007 we conventionally use a right handed system: . +z / / O---------# +x | | | | # +y (z into page) We expect that +ve movement along these axes normally corresponds to the movement in the fly's brain in the left, ventral and posterior directions respectively. As you can see from the text above ''rgl'' has 2 possible default coordinate systems neither of which are what we want. Our preferred system is at least right-handed like the rgl systems and is in principle obtainable by rotating an rgl view. However when you do rgl.open() or this is called implicitly by drawing an rgl object then you end up with a coord system which is actually clamped so that it cannot even be rotated 180 degrees around the x axis to reach our preferred orientation. This is clearly a bit naff. So in the past what I did was manually multiply all y and z coordinates by -1 to end up with a new object that looked ok in the default coordinate system. However it looks like there is now a better way. Use the *3d functions and set rgl's userMatrix to default to: 1 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 1 You can either do this for the current rgl view by setting par3d as follows: par3d(userMatrix=matrix(c(1,rep(0,4), -1, rep(0,4), -1, rep(0,4), 1),ncol=4)) or you can set ''r3dDefaults'' (an object in [[http://cran.r-project.org/doc/manuals/R-lang.html#Global-environment|.GlobalEnvir]]) r3dDefaults$userMatrix=matrix(c(1,rep(0,4), -1, rep(0,4), -1, rep(0,4), 1),ncol=4) r3dDefaults$bg=NULL # optional, but I prefer the standard grey background setting