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.
Page Index Toggle Pages: 1
Vector3D as a core type (Read 4900 times)
Vector3D as a core type
Oct 30th, 2006, 8:03pm
 
Do you expect you might eventually include a Vector3D class in the core Processing code at some point, or do you consider that outside the scope of what you are trying to provide?

I've found myself working with traer.physics a lot lately and at a certain point I decided to use Shiffman's Vector3D class that he's offered as a library. Well, it turns out that traer.physics also has a Vector3D class in it but it is undocumented. However, the name conflict means that I can't use both in the same project.

Wouldn't it be helpful to have a built in Vector3D class so that people creating libraries that need Vectors can use the same Vector class everyone else is? This could facilitate communication between objects from two different libraries, as well.
Re: Vector3D as a core type
Reply #1 - Jan 5th, 2007, 7:28am
 
This is indeed a problem I've encountered.

So far every library I've seen has used its own form of Vector. For example, in my own personal "library" I use Vec2 for 2D, although I know that similar ones exist in other libraries.

The problem with making a "generic" vector library is that everyone has their own style of doing things. If Traer used my Vec3 objects I think it would drive him nuts. I've added a ton of functionality to it, including VecX support for all shapes vertex(Vec3 v) for example. Also, for Vec2 I've included polar coordinates support so you can easily get x,y from an angle and distance.

Personally I feel this is better left untouched, however I wouldn't cross my fingers on that. Rumor has it there's a PShape class coming in the future...
Re: Vector3D as a core type
Reply #2 - Jan 5th, 2007, 2:19pm
 
casey and i have discussed this a good bit. the main reasons that we've not included a vector class are:

1) it's slower to use a class to store points. it's actually faster to have local variables x,y,z or even an array than doing someObject.x etc.

2) it's slower to create/delete a lot of objects. this is one reason java2d is so slow--it works the garbage collector incessantly. new Point2D.Float(blah).. new AffineTransform() and so on..

3) for reasons #1 and #2, we don't use vectors internally to processing.core. with the advent of simon's work on P3D and lighting, he introduced a matrix class, though even that is slower than just having local variables m00, m01, etc. we wound up using it, though, because the overhead in maintaining lots of tiny variables was too much.

4) we're always trying to keep the class count down.

5) the api issue that you mention--what things do we include?

6) sun has a nice 3d vector class available that can be dropped into any p5 code, and shiffman also has his library.

7) how big a vector is it? Vec3? Vec2? Vec4? seems like if you do one, people are gonna want the others. Vec3 is the most useful, but..

all that said, the reasons we perhaps /should/ include a class include:

a) the reasons you cite re: avoiding a situation where everyone makes their own vector class. though i'm not really convinced having a 'standard' class would really curb this, though it might encourage subclassing instead of creating things from scratch.

b) when a prof like shiffman creates their own, to be used in a course which is our target audience, then it seems more like something that's missing from the core libraries. we try to listen a lot to people who are teaching with p5, because they have a good overview of what students/beginners have trouble with.

c) making lists of x,y,z points is a mess if you don't have a class. sure you can re-allocate lists of floats, but..

so that's where we are. i think the balance still leans toward not including a separate class for it, and we've used #6 as our means of ducking it, but perhaps we might be convinced otherwise.
Re: Vector3D as a core type
Reply #3 - Apr 8th, 2007, 10:18am
 
I would tend to agree with all of those pros and cons; however, I've got to say that I am pretty strongly in the pro-Vector3d camp, with some caveats.  The fact is that just about everybody uses a version of this class, and this is going to become quite a problem when the number of available libraries grows further; there are only so many names you can come up with for an N-dimensional vector, after all, so when Processing leaves beta and the libraries start really pouring in, we're going to be in for a lot of nasty namespace conflicts, not to mention the need to build vector-vector adapters for each pair of libraries that don't happen to conflict with each other if you ever want to let them interconnect.

I understand your speed concerns completely.  When you have vectors as a class you tend to throw them around just about everywhere, creating and abandoning them recklessly just because the math makes better sense that way.  So they certainly should not be used in the core internals, especially the crucial areas.  However, in most sketches that I've seen and written, the bottleneck is almost never caused by the difference between a few floats and a Vector3d object unless you're doing something fairly naughty anyways.  Generally speaking, I find that the increase in productivity from using vector objects heavily outweighs the minor decrease in speed, though that's not to say that careful inlining of frequently used expressions that would otherwise be fully vector-based doesn't speed things up immensely in a pinch!

Given my druthers, I would probably prefer to see a well chosen vector library included in the default list of libraries rather than as a change to the core, mainly because you're likely to break a lot of code by adding a new internal class (though I suppose few people are likely using PVector3d, so I guess that trouble could be minimized).  This would have the effect of establishing an effective standard without forcing it on anybody, and I think it would be a fair and useful compromise; while I am perfectly aware that standard vector libraries exist, the fact is, I have never used them because they are a few clicks further away than the "Sketch" menu.  If you did decide to integrate it into the core, though, one option to consider on the speed front would be adding some automatic unrolling of simple vector operations as a preprocessing step - I remember from programming in Flash (where the object penalty is significantly higher than in Java) that some vector operations could be fairly easily recognized and inlined into equivalent float manipulations, especially when local vectors were involved.  This might get a bit messy, and I suspect adding another layer of preprocessing is not something you would probably like to do, but in a lot of cases it can really speed things up, especially if you are aware of the types of things that can and cannot be automatically optimized in this way.

Dimensionality: Clearly 3, maybe 2, though I don't know whether it's even worth it; I usually use 3 anyways so I can do stuff with cross products.  4 can be occasionally useful but it seems like overkill since we already have the graphics renderer underneath - are any people using 'em in Processing?

API: IMO, if the operation has a symbol that an undergrad math major would recognize, it should probably be in there - arithmetic, dot, cross, normalize, norm, normSquared (save a square root if you can) and projection are the big ones for me.  Apart from that, maybe some simple rotation functions and a way to be acted on by a matrix or quaternion.  On the other hand, if this stuff was added merely as a standard library, it might make sense to include a whole crapload of utility functions, I'm not sure.  The only other thing I would suggest is to have both mutator and vector methods - i.e. vecA.plus(vecB) returns a new vector, whereas vecA.add(vecB) merely alters vecA.
Re: Vector3D as a core type
Reply #4 - Dec 3rd, 2007, 1:01pm
 
My 5 cents:

It's true that using classes is slower than local variables, but still you can "cache" class values into locals for performance critical parts, or avoid using them at all. So having a vector class per se is not slow, using it in the "wrong" way is. And by the way, if the class is "final" as it should be (mainly for performance reasons in java), then the penality is not too big.

It's true that allocating a lot of small objects is a BIG performance hog. But for that you can/should use object pools. And it could be nice to have vectorarrays as well, so you can do vectorarray.transform(matrix) and perform the operation faster than a loop that calls transform for each element (even if hotspot should inline that call).

As for the Vector2/Vector3/Vector4 problem, usually for 3d graphics a vector4 is used but this is mainly for alignment reasons (simd), so in java I would recomend a vector3.

Still I'm not sure if with all this stuff added, the code would be easier or not. It probably will for parts of the code that are not performance-critical and for most processing projects. But it could make some parts of the core more bloated.

But you could simply add vectors outside the core, just as a standard (built-in) library function for processing projects...
Page Index Toggle Pages: 1