473,385 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

R3 vector

What is the best way to implement a vector in space R3, i.e a vector
holding three floats, supporting arithmetic operations, dot and cross
product etc in c++? is there a standard library class for this?
Apr 27 '07 #1
4 3018
helge wrote:
What is the best way to implement a vector in space R3, i.e a vector
holding three floats, supporting arithmetic operations, dot and cross
product etc in c++? is there a standard library class for this?
No, there is no standard class, but there is plenty of commercial and
free implementations out there. Just google for 3d geometry operations
or some such.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 27 '07 #2
helge wrote:
What is the best way to implement a vector in space R3, i.e a vector
holding three floats, supporting arithmetic operations, dot and cross
product etc in c++? is there a standard library class for this?
Not a standard class, but if your requirements are not too high, you can
use the std::valarray<double>. It supports all the aritmetic operation,
it supports the sum of the elements so that the norm and the dot product
it's straightforward, and you'll have to define just the cross product.

you can also hide a little bit of information encapsulating the valarray
into a class:

class R3Vector
: public std::valarray<double>
{

R3Vector()
: std::valarray<double>(0.0, 3)
{
}

R3Vector(double a, double b, double c)
: std::valarray<double>(0.0, 3)
{
(*this)[0] = a;
(*this)[1] = b;
(*this)[2] = c;
}

R3Vector(const valarray<double>& v)
: std::valarray<double>(0.0, 3)
{
if(v.size() != 3)
error
else
copy
}

R3Vector crossProd(const R3Vector& v) const;

};

etc.
Regards,

Zeppe
Apr 27 '07 #3
This seemed like a good solution. However, I'm having som type related
problems when using this approach. Are STL classes ment to be inherited
from like this? e.g when trying to use dynamic_cast I get the error that
source does not support polymorphism. Wouldn't it be better to keep a
valarray<doubleas a member and use it's functionality to simplify
implementations of the norm, dot product and cross product etc?

Zeppe wrote:
helge wrote:
>What is the best way to implement a vector in space R3, i.e a vector
holding three floats, supporting arithmetic operations, dot and cross
product etc in c++? is there a standard library class for this?

Not a standard class, but if your requirements are not too high, you can
use the std::valarray<double>. It supports all the aritmetic operation,
it supports the sum of the elements so that the norm and the dot product
it's straightforward, and you'll have to define just the cross product.

you can also hide a little bit of information encapsulating the valarray
into a class:

class R3Vector
: public std::valarray<double>
{

R3Vector()
: std::valarray<double>(0.0, 3)
{
}

R3Vector(double a, double b, double c)
: std::valarray<double>(0.0, 3)
{
(*this)[0] = a;
(*this)[1] = b;
(*this)[2] = c;
}

R3Vector(const valarray<double>& v)
: std::valarray<double>(0.0, 3)
{
if(v.size() != 3)
error
else
copy
}

R3Vector crossProd(const R3Vector& v) const;

};

etc.
Regards,

Zeppe
Apr 30 '07 #4
On 2007-04-30 21:58, helge wrote:
Zeppe wrote:
>helge wrote:
>>What is the best way to implement a vector in space R3, i.e a vector
holding three floats, supporting arithmetic operations, dot and cross
product etc in c++? is there a standard library class for this?

Not a standard class, but if your requirements are not too high, you can
use the std::valarray<double>. It supports all the aritmetic operation,
it supports the sum of the elements so that the norm and the dot product
it's straightforward, and you'll have to define just the cross product.

you can also hide a little bit of information encapsulating the valarray
into a class:

class R3Vector
: public std::valarray<double>
{

R3Vector()
: std::valarray<double>(0.0, 3)
{
}

R3Vector(double a, double b, double c)
: std::valarray<double>(0.0, 3)
{
(*this)[0] = a;
(*this)[1] = b;
(*this)[2] = c;
}

R3Vector(const valarray<double>& v)
: std::valarray<double>(0.0, 3)
{
if(v.size() != 3)
error
else
copy
}

R3Vector crossProd(const R3Vector& v) const;

};

This seemed like a good solution. However, I'm having som type related
problems when using this approach. Are STL classes ment to be inherited
from like this? e.g when trying to use dynamic_cast I get the error that
source does not support polymorphism. Wouldn't it be better to keep a
valarray<doubleas a member and use it's functionality to simplify
implementations of the norm, dot product and cross product etc?
Please put your reply below the text you are replying to in the future.

No, the STL classes are not meant to be inherited from, and while I have
no experience of it myself I seem to recall that some people have had
some unexpected consequences and private inheritance is usually recommended.

Having worked on a vector and matrix library recently I would advice you
to first take a look at any existing implementations before rolling your
own, especially if your applications is performance sensitive. There are
two scenarios where I think it might be a good idea to make your own,
and that is if you only need a small and simple implementation or if you
have very special needs.

If you decide to make your own you should probably think twice before
using the valarray unless you need its slice features. The reason is
simple, while the valarray was a good idea (a special class to represent
mathematical vectors and arrays that the library/compiler vendors could
heavily optimize) the reality is that, for a number of reasons, it is
just as unoptimized as could be. When I wrote my code I took a look at
the valarray implementation in VS2005 and it was just the same as what I
would have done, loops over elements.

Here's (a modified) part of my vector, I've chosen to parameterize the
type but you could of course hardcode it to float.

template<typename T>
class Vector
{
T data_[3];

public:
// Constructor
Vector(const Vector<T>& v);

// Assignment-operator
Vector<T>& operator=(const Vector<T>& v);

// Accessing
const T& operator()(size_t n) const;
T& operator()(size_t n);

// Operations
Vector<Toperator+(const Vector<T>& v) const;
Vector<Toperator-(const Vector<T>& v) const;
// ...
Vector<T>& operator+=(const Vector<T>& v);
Vector<T>& operator-=(const Vector<T>& v);
// ...
Vector<Toperator-() const;
bool operator==(const Vector<T>& v) const;
bool operator!=(const Vector<T>& v) const;

// Other
T length() const;
Vector<T>& normalize();
// ...
};

--
Erik Wikström
Apr 30 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
16
by: Martin Jørgensen | last post by:
Hi, I get this using g++: main.cpp:9: error: new types may not be defined in a return type main.cpp:9: note: (perhaps a semicolon is missing after the definition of 'vector') main.cpp:9:...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.