Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard 3d vector class in C++

In an existing project, I see a class Vector_3d, templated, with the usual operations for vectors (in the sense of algebra). After profiling, I noticed that a large amount of the time is spent in this class.

I was wondering if there was a well-known implementation of such a basic concept as a 3d vector in C++. Indeed, it might be easier to use a good implementation of the vector instead of trying to optimize this one.

Edit: This is in a context of a geometrical representation of some objects. But it is independent from any visualization. I will see if there is a way of avoiding to call the various methods too often. And I will have a look at the proposed packages.

like image 281
Barth Avatar asked Apr 20 '11 12:04

Barth


People also ask

Are there 3D vectors?

A 3D vector is a line segment in three-dimensional space running from point A (tail) to point B (head). Each vector has a magnitude (or length) and direction.

Do vectors work in C?

Vectors are a modern programming concept, which, unfortunately, aren't built into the standard C library. Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.

What is vector type in C?

A vector type represents a vector of as many of the specified C data type as will fit in a 128-bit register. Hence, the vector signed int is a 128-bit operand containing four 32-bit signed ints . The vector unsigned short is a 128-bit operand containing eight unsigned values.


1 Answers

There is no much room for improvement in a 3d vector class (basically, dot / cross products are fairly easy, matrix multiplication as well). If so much time is spent in that class, maybe your code using it is flawed. Have you checked against

  • copy vs references
  • wrong association (like multiply matrix then all vectors by the resulting matrix, rather than all vectors by the chain of matrices)

I know that there is QVector3D in Qt, that might help you (by the way, they got Vector 2D and 4D as well for common 3D geometry operations)

like image 83
Bruce Avatar answered Nov 03 '22 01:11

Bruce