28 Dec 2011

## Dynamically Dimensioning 3D Models

When interactively manipulating objects in 3D, having clear dimensions that update dynamically while dragging around allows for much greater confidence and accuracy in the process. This is especially true on a tablet where accurate alignment is well near impossible as your finger (and sometimes hand) effectively obscures the drag point. However, what started out as a quick experiment ended up sucking me into a 6 day vortex, swirling around with quaternion maths and 3D text manipulation in an attempt to get radial dimensions visible when viewed from any direction. This tiny applet is the result of those 6 days.

###### Interactive Model Dimensions

The above is an embedded interactive Java applet that demonstrates the automatic generation and update of model dimensions. Click on any object or facet in the scene to see dynamic auto-generated dimensions, Use the middle and right buttons to pan and rotate the view.

### Overview

The applet basically displays the dimensions of each 3D form as you drag it or any of its facets around to change shape. Simply left-click any facet to select it and then click+drag the displayed arrows at its centre point to move it along its surface normal. Double-click to select the whole object, in which case you can drag it around or use the X, Y and Z keys, with the SHIFT key to reverse direction, to move the selection in corresponding axis. Use the Settings panel to play with different dimension parameters.

### Background

A significant part of that 6-day period was spent dealing with radial dimensions that show an arc and an angle between two 3D lines. Whilst it was possible to get most things working with standard 3D matrix rotations, because I wanted just a single method to deal with all possible configurations, I ran into all sorts of trouble when both lines were in arbitrary 3D directions. If either one was axially aligned or ran parallel to one of the Cartesian planes, you could work out the rotations required. However, with both in arbitrary 3D directions, there was no reference point to properly orient the third axis.

Also, at one point I spent many hours debugging one particular angle flip only to find that it may actually have been due to an instance of Gimbal Lock. Who knows if that was really the case but, as I subsequently discovered, I am by no means the first programmer to trip over that particular log and hence began my dalliance with quaternions. I am no maths guru and most sites that describe quaternions assume you are. However, they did seem to make a bit of sense and actually turned out to be the answer to a couple of other transformation niggles I'd been having.

### Dimension Settings

The following is a brief explanation of the various dimension settings available in the Settings panel in the top-left of the applet.

###### Use Spheres Instead of Ticks

Select this option to display each dimension indicator as a small dark sphere instead of an angled, slightly thicker, tick line.

###### Dimension Lines Always Visible

When this option is selected, dimension lines are drawn with no depth buffer test, meaning that they will always be visible and not obstructed by other model geometry. When not selected, they are treated like any other part of the model and depth tested against obstructing surfaces.

###### Adjust Text Orientation with View

When this option is selected, the text on each dimension line will do its best to reorient itself so that it is always facing towards the camera. This was not as straightforward as it sounds.

First, the axis of rotation for each text block is fixed along its dimension line, so there is a limit to how appropriate that rotation can be depending on the angle the dimension line makes with the line of view. Having text that rotates around outside the axis of this line simply does not look right as the orientation of the text is a key indicator of what dimension line it belongs to.

Also, it is often necessary to flip the X and Y axis of text depending on the azimuth angle of the view relative to the dimension line. This is so that text at the same perspective angle always runs in the same relative direction. This avoids situations where the vertical text on one side of a wall runs upwards whilst the text on the other side runs downwards. This way, all parallel text runs in the same direction - upwards if vertical and left-to-right if horizontal.

Second, having text always orient to the camera at higher altitude views also didn't look right, especially for text that ran close to parallel with the line of view. You actually do want text that is on the ground plane to look like it is on the ground plane, not to be oriented slightly off in all but plan view. This is a fine line to walk but, essentially, horizontal text in this applet only reorients when the view altitude is within about 30 degrees to the horizontal, as this is seemed the best compromise.

###### Use Imperial Feet and Inches

Select this option to show each dimension in feet, inches and sixteenths. Obviously the model is originally generated in SI units and the snap settings will be pretty arbitrary, but it at least shows that the units formatting is working reasonably well.

###### Metric Decimals

Use this slider to set the number of decimal points (precision) displayed in the metric dimension text.

###### Dimension Line Darkness

This just shows that it is possible to control the colour of the dimension line, though here it is just black to white.

###### Dimension Text Darkness

This just shows that it is possible to control the colour of the dimension text separate from the dimension line, though here again it is just black to white.

###### Relative Size

Use this to adjust the text size and, as it is related, the dimension line offset distance.

###### Adjust Text with View Distance

If the dimension text was always the same size, then dimensions on objects that are quite far away from the camera become very difficult to read - unless the text size is very large in which case dimensions on objects quite close to the camera look silly. Thus, with this option checked, the text size is adjusted so that distant text is drawn slightly larger and close text slightly smaller. The adjustment isn't large as too much very quickly makes it all look noticeably weird.

###### Adjust Text with View Zoom

When you zoom into a model, text that looks fine from far away quickly starts to look too large. Thus, with this option checked, the text size of all dimensions is adjusted to get smaller relative to the model as you zoom in and slightly larger as you zoom out.