The above math appears to work. Huh. Go figure.

Code:

// ==================================================================== Coordinate Transforms
// Coordinate Transforms
// --------------------------------------------------------------------
inline LWTypes::LWDVector Transform (const LWTypes::LWDVector spot_position,
const LWTypes::LWDVector translation,
const LWTypes::LWDVector rotation,
const LWTypes::LWDVector scale)
{
// Note: negative/inverse transform amounts are used -- otherwise the
// texture transforms will be opposite of the intended
// direction.
// Scale
const double scaled_x = spot_position.X() / scale.X();
const double scaled_y = spot_position.Y() / scale.Y();
const double scaled_z = spot_position.Z() / scale.Z();
// Rotate
const double rx = -rotation.P();
const double ry = -rotation.H();
const double rz = -rotation.B();
const double cos_rx = cos(rx);
const double cos_ry = cos(ry);
const double cos_rz = cos(rz);
const double sin_rx = sin(rx);
const double sin_ry = sin(ry);
const double sin_rz = sin(rz);
const double neg_sin_rx = -sin_rx;
const double neg_sin_ry = -sin_ry;
const double neg_sin_rz = -sin_rz;
const double rotated_x = scaled_x * (cos_ry * cos_rz + neg_sin_ry * neg_sin_rx * neg_sin_rz) +
scaled_y * (cos_ry * sin_rz + neg_sin_ry * neg_sin_rx * cos_rz ) +
scaled_z * (neg_sin_ry * cos_rx);
const double rotated_y = scaled_x * (cos_rx * neg_sin_rz) +
scaled_y * (cos_rx * cos_rz ) +
scaled_z * (sin_rx);
const double rotated_z = scaled_x * (sin_ry * cos_rz + cos_ry * neg_sin_rx * neg_sin_rz) +
scaled_y * (sin_ry * sin_rz + cos_ry * neg_sin_rx * cos_rz ) +
scaled_z * (cos_ry * cos_rx);
// Translate
return LWTypes::LWDVector( rotated_x - translation.X(),
rotated_y - translation.Y(),
rotated_z - translation.Z() );
}

I hope this can save other coders some of their valuable time.

I decided not to simplify anything in the rotated_* calculations as I've found it's generally not wise to try and outsmart the compiler. Since everything in there is const, the compiler's optimizer should be able to make the best decisions as to how to structure the whole thing.

## Bookmarks