[ODE] Why is dVector3 defined as dReal[4] ???

Royce3 royce3 at ev1.net
Thu Feb 13 07:04:02 2003


I got a chance to sit down at the computer, and here's a little
ditty I wrote:

#include <stdio.h>

typedef float dReal;

typedef struct _dVector3
{
	union
	{
		struct
		{
			dReal x, y, z, w;
		};
		struct
		{
			dReal a[4];
		};
	};
} dVector3;

int main()
{
	printf ( "sizeof(dReal) = %lu\n", sizeof(dReal) );
	printf ( "sizeof(dVector3) = %lu\n", sizeof(dVector3) );
	printf ( "sizeof(dVector3)/sizeof(dReal) = %lu\n", sizeof(dVector3)/sizeof(dReal) );
	dVector3 v;
	v.x = 1; printf ( "v.x = %lu\n", (long)v.x );
	v.y = 2; printf ( "v.y = %lu\n", (long)v.y );
	v.z = 3; printf ( "v.z = %lu\n", (long)v.z );
	v.w = 4; printf ( "v.w = %lu\n", (long)v.w );
	for ( int i = 0; i < 4; i++ )
		printf ( "v.a[%i] = %lu\n", i, (long)v.a[i] );
	return 0;
}

Obviously, the only problem with using this approach is that the .a operator
would have to be used when accessing it as an array, which would break existing
code. Obviously you could fix that with C++, but if the library is in C...

>
>Subject: Re: [ODE] Why is dVector3 defined as dReal[4] ???
>   From: Royce Mitchell III <royce3@ev1.net>
>   Date: Thu, 13 Feb 2003 06:56:04 -0600
>     To: ode@q12.org
>
>> struct dVector3{
>>    dReal x, y, z;
>>    dReal _pad;
>> #ifdef __cplusplus
>>    dReal &operator[](int);
>> #endif
>> };
>
>> (Here, the clear advantage is using C++, because C users won't have
>> array-access operator[], which works now with the array concept.)
>
>Personally, I prefer C++, but lets not needlessly outcast C users. You
>can always use a union to give C users the array operator. In fact,
>just doing it that way will give uniformity to both C & C++.
>
>--
>Peace favor your sword,
>  Royce3
>
>_______________________________________________
>ODE mailing list
>ODE@q12.org
>http://q12.org/mailman/listinfo/ode