We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › 3d Vector class by David Huebner
Page Index Toggle Pages: 1
3d Vector class by David Huebner (Read 2712 times)
3d Vector class by David Huebner
Jun 5th, 2005, 7:22am
 
In reference to David Heubners vector class:
http://www.millbridge.de/processing/Vector.pde

Found through an earlier post of mine:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Contribution_3DOpenGL;action=display;num=1116270837

I hope the author doesn't mind me publically discussing this, but I had a few open questions/comments about the class.

For a start, I added my own method to the end:
Code:
  public Vector clone() {
   return new Vector(x[0], x[1], x[2]);
 }

This creates a new copy of the clone in memory, which I've found useful. And then I noticed, the class has highly similar methods e.g. rotateZ() and rotateMeZ(). I presume the intention was that you could produce a new vector by rotating the existing vector (rotateZ()) or simply rotate the vector changing its space in memory (rotateMeZ()).

The rotateZ method looks wrong to me because you're not creating a copy of the vector on the line Vector result = this;, you're just assigning an alias so when the rotation is performed you are actually damaging the vector and returning another alias?

And so, I think the code for rotateZ() (and similar functions) should read:
Quote:
 public Vector rotateZ(float val) {
   Vector result = this.clone();
   double cosval = cos(val);
   double sinval = sin(val);
   double tmp1   = x[0]*cosval - x[1]*sinval;
   double tmp2   = x[0]*sinval + x[1]*cosval;

   result.x[0] = (float)tmp1;
   result.x[1] = (float)tmp2;

   return result;
 }

The other thing I wanted to add about this class is, would it be possible to have some documentation on this? Lastly, to TomC's comment (in the other post) about renaming it - I don't mind it being called Vector if it has the same functionality as the original plus the extra features. I'm including as needed for the moment.
Re: 3d Vector class by David Huebner
Reply #1 - Jun 5th, 2005, 9:14pm
 
Thanks Markavian for finding that stuff in the rotate methods. And I realy don't mind posting it. The method clone() does the same as the constructor FVector(FVector v2) which returns a clone of this vector. But maybe it's easier to read so I also integrated clone().
I went trough the whole vector class and changed a couple of things. You can find the new version here:

http://www.millbridge.de/processing/FVector.pde.

Note that I changed the name to FVector according to its elements being all float values. I also changed the (public) array x[] to e[] so that there is no confusion with x,y,z and all the array containing all vector elements. The methods of my previous post might need to be changed using FVector and the array e[].

This class should now work fine in Processing and standard Java. Documentation got slightly advanced.

This vector class ist not limited to a 3D vector, although some of the methods only work with 3 dimensions.

Most methods have a second variant. The normal one like multiply() returns a new vector with the calculations applied to. The one with "Me" in its name like multiplyMe() applies the calculations to this vector.

Please report any bugs and additions.
Re: 3d Vector class by David Huebner
Reply #2 - Jun 9th, 2005, 9:28am
 
Ok, well the new FVector class seems to work fine, it fulfills all the features of the previous one any how. I replaced the other Vector class in one of my programs. It took me a while to find/replace all the Vector variables and replace them with FVector, and also change the vect.x[0] access with vect.e[0] access (used in a few of your methods).

Heres a bit of documentation I found on how the clone method is supposed to be implemented. http://java.sun.com/docs/books/tutorial/java/javaOO/objectclass.html
I disagree that you chose cloneMe() instead of clone() since what else are you going to clone?
Re: 3d Vector class by David Huebner
Reply #3 - Jun 9th, 2005, 4:42pm
 
Oh hehe. The problem was, that I used Eclipse and it told me, that I can't use clone() as it is already a Java method. So I changed it to cloneMe, although I agree with you that this name does not make more sense than clone().
Re: 3d Vector class by David Huebner
Reply #4 - Jun 20th, 2005, 9:15pm
 
I have strange things when I use rotateMeX. x[i] is changed and then called on the second line.

Code:
public void rotateMeX(float val) {
double cosval = cos(val);
double sinval = sin(val);
x[1] = (float)(x[1]*cosval - x[2]*sinval); <----(1)
x[2] = (float)([b]x[1][/b]*sinval + x[2]*cosval); <-----(2)
}


Should it be:
Code:
public void rotateMeX(float val) {
double cosval = cos(val);
double sinval = sin(val);
double tmp1 = (x[1]*cosval - x[2]*sinval);
double tmp2 = (x[1]*sinval + x[2]*cosval);
x[1] = (float) tmp1 ;
x[2] = (float) tmp2;
}
?
Re: 3d Vector class by David Huebner
Reply #5 - Jun 20th, 2005, 9:42pm
 
Yes, that the problem. I looked at the code and realised it was doing something wrong so switched to:

cVector = cVector.rotateX(degrees(30));

instead of cVector.rotateMeX(degrees(30));

Well done for spotting that bug anyhow.
Re: 3d Vector class by David Huebner
Reply #6 - Jun 21st, 2005, 3:58am
 
there might be another problem (maybe not) but I think you mean:

Code:
cVector = cVector.rotateX(radians(30)); 



instead of:

Code:
cVector = cVector.rotateX(degrees(30)); 



since degrees() converts from radians to degrees and 30 seems a value used in degrees.
Re: 3d Vector class by David Huebner
Reply #7 - Jun 21st, 2005, 6:24am
 
That was just a typo, I meant radians(30).
Re: 3d Vector class by David Huebner
Reply #8 - Jun 21st, 2005, 7:01am
 
Wich one is more rapid?   Code:
float x[0], x[1], x[2], x[3]; 

or   Code:
float x, y, z, w; 

With a lot of calls it should be slower with an array? In other way, It's nice to keep them homogenous.
Re: 3d Vector class by David Huebner
Reply #9 - Jun 21st, 2005, 11:12am
 
Hey thanks for spotting the bugs. Gonna change that this evening.

Using an array instead of x,y,z,w makes it a bit slower (processing was never made to be fast). But it should need less memory and the most important for me: It's not restricted to any number of elements.
Re: 3d Vector class by David Huebner
Reply #10 - Jun 21st, 2005, 12:58pm
 
Arguably, whos going to need a 4, or 5 point vector? Perhaps it would be a better idea to write an optimized class with fix memory variables.

I would assume it only gets heavy if you have many vectors as apposed to processing the same vector over and over again.
Re: 3d Vector class by David Huebner
Reply #11 - Jun 21st, 2005, 4:56pm
 
You might need 4 elements everytime you want to use matrix calculations. Of course, you can work around the 4. element, but I wouldn't like that.

More then 4 values is quite unlikely for most of the users, as they might only want to use vectors for 3D calculations i.e. 3D drawing. But that's not the only use of vectors. Although I can't give you an example right now Wink ...
Re: 3d Vector class by David Huebner
Reply #12 - Jun 21st, 2005, 9:21pm
 
Ok, bugs have been sorted.
I also included a rotation around a given axis and some minor changes.
Page Index Toggle Pages: 1