Skip to main content
gamedev.net
🔒 Locked

designing a math vector class

Started by wild_pointer 24 replies 600+ views
Original Post
wild_pointer
wild_pointer
It''s frequently become useful, i''ve noticed, to be able to store a vector in array[3] form, for things such as the OpenGL ''v'' functions. At the same time i''d like to be able to keep the standard x, y, and z members. What would be the best way to do this? I was considering adding an array member to the class - but this means I have to update two sets of data everytime something changes, and I don''t wish to make the data private, so nothing is keeping someone from changing one and not the other. Is there any way I could work out something like my_vector.get_array()?
[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people
[size=2]
CWizard
CWizard
libGDN

a CVS shorcut for you: /libGDN/math/




[edited by - CWizard on December 18, 2002 4:53:53 PM]
Zorak
Zorak

  

class Vector3f
{
union {
struct {
float x, y, z;
};
float v[3];
};

...
};


makes you able to do:

Vector3f frogPos;

frogPos.x = 0.143f;
frogPos.y = 0.113f;
frogPos.z = 0.164f;

or

frogPos.v[0] = 0.f;
frogPos.v[1] = 0.11f;
frogPos.v[3] = 1.33f;






I have spoken.
Domine non secundum peccata nostra facias nobis
Useful
Useful
use a union

like

union
{
float x[3];
struct
{
float x,y,z;
}
}

that or you can use

Class &operator[](int index)

and access the class just like an array

fyi its been a while since ive used C++
CWizard
CWizard
Yes and no. You can do like this:

Vector3 vec;

vec.x() = 12.0;
vec.y() = vec.x();
// etc...



daerid
daerid

  
template<class T>
struct Vector3
{
T x;
T y;
T z;
T& operator[](const int idx)
{
switch(idx)
{
case 0:
return x;
case 1:
return y;
case 2:
return z;
default:
throw SomeErrorHere();
}
}
};
daerid@gmail.com
risingdragon3
risingdragon3
Daerid: an if switch cost per access? expensive.
nameless structs? not standard, doesn''t compile on all compilers.

Trust me, we''ve tried just about every standard-compilant way of doing it. There''s really no way apart from an nameless struct... (which is nonstandard).
Check the libgdn discussion list for the myriad ways, some of which could almost work, but there is almost always a problem (usually #pragma pack)...



-----------------------------
Gamedev for learning.
libGDN for putting it all together.
Nypyren
Nypyren
If you have nameless-struct problems, just pre-define the struct (I usually typedef them so I don't need to say "struct s" (just "s") to declare 'em).

floats shouldn't be suceptible to packing problems, as they are 32 bits in C/C++.

Pragma pack is AFAIK only used if you are using small types like chars that normalls align like ints or shorts (depends on compiler).

[edited by - Nypyren on December 19, 2002 3:34:03 AM]
risingdragon3
risingdragon3
quote:

If you have nameless-struct problems, just pre-define the struct (I usually typedef them so I don''t need to say "struct s" (just "s") to declare ''em).

floats shouldn''t be suceptible to packing problems, as they are 32 bits in C/C++.

Pragma pack is AFAIK only used if you are using small types like chars that normalls align like ints or shorts (depends on compiler).


Vector of complex?
and typedef''s don''t help - that would mean you would have to do this: v.s.x as opposed to v.x.
Floats aren''t necessarily 32 bits

Beer Hunter:

  
vector<float> v;
assert(sizeof(v)==sizeof(float)*3);

HUH!?!
Been there tried that, sir

-----------------------------
Gamedev for learning.
libGDN for putting it all together.
IFooBar
IFooBar
how bout something like this

  
struct vec3
{
vec3()
{
arr[0] = &x
arr[1] = &y
arr[2] = &z
}

float x,y,z;

float& operator[](int index) const
{
return *arr[index];
}

private:
const float *arr[3];
};


Havent tested this yet, but im sure witha little tweaking something could be done...i think


:::Al:::
[Triple Buffer V2.0] - Resource leak imminent. Memory status: Fragile
[size=2]aliak.net
Beer Hunter
Beer Hunter
quote:
Original post by risingdragon3
Beer Hunter:
vector<float> v;
assert(sizeof(v)==sizeof(float)*3);

HUH!?!
Been there tried that, sir
What does that have to do with the code I provided?
Perhaps my code destroyed half your brain.
risingdragon3
risingdragon3
quote:

What does that have to do with the code I provided?
Perhaps my code destroyed half your brain.


No, you said that the OT should use the property class you linkeed
Which will add a pointer to the class ...which is usually fine, except in the case of a vector.
Sizeof(vector) should be 3* sizeof(float).
If you use the property classes you described you will have (sizeof(float) + sizeof(float*)) *3.
No?


alfmga: same thing, the size is incorrect.
It''s easy to provide properties - if you let it take up memory space
and, wouldn''t it be better to hold a reference x = array[0], y = array[1]? No pointers

500 strike 1



-----------------------------
Gamedev for learning.
libGDN for putting it all together.
RamboBones
RamboBones
I don''t mean to be rude but clearly you, wild_pointer, don''t understand how memory in a program works. A structure, or class, has all it''s memory in sequntial order for instance

class CVector3
{
double x,y,z;
};

The memory that you can access by CVector3::x/y/z is in a line in memory. Now let''s go and look at arrays for instance

double vector[3]

Stores it''s memory in sequential order. But that''s exactly the same as the class for instance you can do this

glVertex3dv((double*)&CVector3);

It works because I''ve been doing that for years. I think you need to learn more about how memory is arranged in programs but at least this is one question answered for you

And as for the rest of you what the HELL were some of those classes/structures/templates actually suppose to do. Because whatever they were suppose to do they clearly didn''t answer the question.
risingdragon3
risingdragon3
RamboBones:
And I don''t mean to be rude, but if you''re not sure what we''re posting about, do you have to be so blunt?
quote:

And as for the rest of you what the HELL were some of those classes/structures/templates actually suppose to do. Because whatever they were suppose to do they clearly didn''t answer the question.


He wants an array member (or pseudo-member) and a named member. What he doesn''t want is casting the class to an array.

If you don''t know what the code does, how can you state (rather arrogantly) that the code doesn''t answer his question?

Secondly, I''m not positive, but I think casting a class to an array of T is not guarenteed to work correctly. Padding can change this. I also think that compilers are allowed to pad at the beginning and end of the class [they are allowed to pad in the middle, but only to correctly align the data, which in a class of the same datatypes (such as the vector here) that doesn''t matter].

-----------------------------
Gamedev for learning.
libGDN for putting it all together.
Beer Hunter
Beer Hunter
quote:
Original post by risingdragon3
Sizeof(vector) should be 3* sizeof(float).
...except that vector is a standard c++ template class, and whenever you say "vector", it is assumed that you are referring to that.
risingdragon3
risingdragon3
Oh fine, fine fine =P
sizeof(vec3) == sizeof(float*3); where
struct vec3 {
float values[3];
};

better?

-----------------------------
Gamedev for learning.
libGDN for putting it all together.
Chozo
Chozo
RamboJones - are you sure that method is entirely portable? I don''t mean to question what you know, I''m just curious if that''s a standard feature, or if it''s just how every compiler sets things up.
wild_pointer
wild_pointer
Wow, not only did you fail in your attempts to not be rude, but you were less than helpful. Bravo.

At what point did your limited, peon like brain, decide I don't know how memory works?

I don't mean to be rude but clearly you, RamboBones, don't have the reading comprehension level of a retarded baboon.

And thanks everyone else, some good ideas.



[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on December 20, 2002 5:11:10 AM]
[size=2]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.