|
Boost : |
From: Michael Marcin (mike_at_[hidden])
Date: 2007-02-10 16:36:51
Matthias Schabel wrote:
> Hi Michael,
>
>> When working in 3d simulations it is necessary to transform vectors to
>> different spaces. Bugs sometimes crop up when, for instance, adding a
>> object space vector to a world space vector or adding a vector from
>> object a's object space to one from object b's object space. It would
>> be nice to catch such inconsistencies at compile-time. I have a
>> feeling
>> a units-like library would be able to handle this but I have no
>> idea how
>> it would work.
>
> Here are two ways to do it, with the quantity wrapping the vector or the
> vector containing quantities:
>
> <snip>
>
That's pretty cool and would likely catch many of the problems. The
other error cases seem harder to detect at compile time. At least for
my purposes there is only one world space but there can be an arbitrary
number of object spaces. In addition it can be difficult to define
correct conversions between spaces.
This is probably a pretty bad example but let's assume we have a node
that can be attached to a parent node with an offset from the parent
node's origin.
struct node
{
node* parent;
std::vector<vec> offsets; // in *this's object space
vec position; // in *this's object space
std::size_t which_offset; // index into parent's offsets vector
};
vec get_parent_space_position( node* n )
{
return n.position + n.parent.offsets[n.which_offset];
// oops n.position is in n's object space
// but n.parent.offset's position is in n.parent's object space
// should be
return n.position + n.parent.position // transform to parent's space
+ n.parent.offsets[n.which_offset];
}
The same operation is valid is some cases but not in others. I.E.
n.position + n.offsets[0] is valid but n.position + n.parent.offsets[0]
is not valid. Because of the recursive nature they are the same
variable so I don't know how you could embed such validity checks in a type.
Maybe this is asking too much from a library and programming is supposed
to be hard :).
Thanks,
Michael Marcin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk