The differences between vectors and vertices... in code!
For my project, I have a vertex class. I know that some people have vertex classes and some have vector classes, and some have both. And I also know that vectors and vertices are very different in math. But the thing is, they don''t look to different in code! Probably the biggest thing is that the x, y, and z specify a direction for vectors, not a position.
Should I use the vertex class for vectors too? Or should I make a vector class? Thanks for your patience.
well, the main thing isn''t definition programatically, but mathmatically. a vertex is a coordinate defined by x,y,z components. a vector is a direction. in 3d space it is defined again by x,y,z components. you can define them as the same thing, but they mean two different things.
My Homepage
My Homepage
You shouldn''t ever need more than a single class for both concepts.
I use "Vector#" (replace # by how many dimensions it has (2 for me, 3 for you))
500
I use "Vector#" (replace # by how many dimensions it has (2 for me, 3 for you))
500
i use Vector for the x, y, z thing, and only the x, y, z. then i use Vertex for things that go to the card, like a mesh vertex might look like:
struct CVertex
{
CVector Pos;
CVector Norm;
CColor Color;
CCoord uv1, uv2;
};
but a vector always is just x, y or x, y, z.
struct CVertex
{
CVector Pos;
CVector Norm;
CColor Color;
CCoord uv1, uv2;
};
but a vector always is just x, y or x, y, z.
One benefit of seperate classes is simply error checking. You can add two vectors together, but not two points. You can take the dot product and cross product of two vectors, but not two points. In many ways it is no differant than indentation. It isn''t strictly necessary, but decreases the likelyhood of an error.
quote:
You can take the dot product and cross product of two vectors, but not two points
Yes, you can. A point is nothing but a vector from the origin. I use the same class for both vectors and points. I use the name ''vertex'' for a point containing additional information, such as normal and texture coords.
Mathmatically, a vector is different than a vertex. You cannot take the dot product or cross product of a vertex, in the strictest sense. You can subtract vertices, the result of which would be a vector. You can use the same class for both, but it can be tough to keep track of which is which. I personally like to keep the two seperate. Vector would have the functions magnitude and normalize, as well as the +, -, dot, and cross operators (though the last two aren't overloaded operators in the code). Vertex would have the function homogenize and the - operator, and can be multiplied by matrices as well. I'd also suggest you use homogenous coordinate systems, i.e., x, y, z, and w. Perhaps the biggest advantage is that, if using homogenous coordinates, all affine transformations can be represented as matrix multiplications. This will save you work down the line, even though you're working in an extra dimension. The difference between a vector and a vertex in homogenous systems is that the w component of a vector is 0, and of a vertex is 1. This will be important as you perform transformations on both, as a vertex does not transform the same as a vector. For example, if you translate a vector, it should remain the same, whereas a vertex, obviously, should move. I'll leave the math up to you. If you just need a vertex and vector, and they aren't related to graphics at all, use 3 dimensions and combine the classes. If this is for a graphic course, though, i guarantee you'll find it easier to use 4 dimensions and seperate the two classes. anyhow, that's how i do it. Whatever floats your boat. Just please don't quote my entire post and give a line by line account of why i'm an idiot. I'm only trying to help.
[edited by - kdogg on March 7, 2003 1:24:47 AM]
[edited by - kdogg on March 7, 2003 1:24:47 AM]
quote:Original post by kdogg
Just please don''t quote my entire post and give a line by line account of why i''m an idiot. I''m only trying to help.
You didn''t capitalize your ''I''! BWahaahaha!! You are so foolish!!
just kidding
quote:Original post by Yann L
You can take the dot product and cross product of two vectors, but not two points
Yes, you can. A point is nothing but a vector from the origin. I use the same class for both vectors and points. I use the name ''vertex'' for a point containing additional information, such as normal and texture coords.
No, you can''t. Mathematically, a point is a combination of a vector and an origin. Dot- and cross-products are defined on vectors, but not on origins; hence, there''s no way to do it.
Homogeneous coordinates (the w being 0 for vectors and 1 for points) is a somewhat kludgey way of doing things, IMHO, but it serves as a good illustration of what you can and can''t do with points and vectors.
But... but that''s what HITLER would say!!
quote:Original post by Sneftel
No, you can't. Mathematically, a point is a combination of a vector and an origin. Dot- and cross-products are defined on vectors, but not on origins; hence, there's no way to do it.
Yes, of course there is, assuming that all your 3D points have a homogeneous coordinate of 1. This is always the case with standard geometry. Mathematically, a point is a combination of the origin and a vector. In practice however, the origin being (0,0,0,1), the point has exactly the same value as the vector defining it from the origin (if we ignore w, and we can do this in the case of world space geometry).
If you have two points, then their coordinates define a plane, in combination with the origin. Hence, P1 x P2 (crossproduct between point 1 and 2) will yield the normal of that plane. This is a speed optimization often used in computer graphics.
If you have two separate definitions for points and vectors, then conversions are needed, and that's a bad thing, if you're interested in performance. Mathematically, they might not be exactly the same, but in practice they are. Sacrifying performance for a purely theoretical concern (with absolutely no prectical relevance in this case) is definitely not a good idea.
Oh, and I know that some people don't like the idea of 'betraying' mathematical concepts. But that's the way it works. Have a look at vertex-shader shadow volume extrusion, for example: it achieves quick and accurate extrusion by defining a point with a w=0. That is mathematically impossible, but it works very well in practice.
PS: if you *really* need the distinction in the source, then use something like typedef vector3 point3. At least you won't lose performance that way. But keep in mind, that lots of advanced graphics effects will treat points as vectors and vice-versa. So your exact distinction will quickly become very fuzzy.
[edited by - Yann L on March 7, 2003 4:21:30 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement