Overloading operator * - c++

I have a vector3 class which i need to implement different multiplication options ( so i overloaded the operator *) depending of the types that im multiplying.
The problem is that in the last one i get the error:
Description Resource Path Location Type
ambiguating new declaration of 'Pang::vector3 Pang::operator*(const Pang::vector3&, const Pang::vector3&)' vector3.h /PangGame/src line 130
C/C++ Problem
But i have only one operator overloaded that returns vector and muyltiplies two vectors.
Hope you can help ( just to clarify the class vector 3 has threee double numbers ) ex: vector3(double x, double y, double z); )
friend vector3 operator* (const double& number, const vector3& vector)
{
vector3 result;
result.x = number*vector.x;
result.y = number*vector.y;
result.z = number*vector.z;
return result;
}
friend vector3 operator* (const vector3& vector, const double& number)
{
vector3 result;
result.x = number*vector.x;
result.y = number*vector.y;
result.z = number*vector.z;
return result;
}
//Scalar product: If a = a1i + a2j + a3k and b = b1i + b2j + b3k then
// a · b = a1*b1 + a2*b2 + a3*b3
friend double operator* (const vector3& vector1, const vector3& vector2)
{
double result;
result= (vector1.x)*(vector2.x)+(vector1.y)*(vector2.y) + (vector1.z)*(vector2.z);
return result;
}
/* Product: Vector x Vector
* Example: The cross product of a = (2,3,4) and b = (5,6,7)
cx = aybz - azby = 3×7 - 4×6 = -3
cy = azbx - axbz = 4×5 - 2×7 = 6
cz = axby - aybx = 2×6 - 3×5 = -3
Answer: a × b = (-3,6,-3)*/
friend vector3 operator* (const vector3& vector,const vector3& vector2)
{
vector3 result;
result.x = (vector.y)*(vector2.z) - (vector.z)*(vector2.y);
result.y = (vector.z)*(vector2.x) - (vector.x)*(vector2.z);
result.z = (vector.x)*(vector2.y) - (vector.y)*(vector2.x);
return result;
}

The problem is that you are trying to overload operator* based on the return type:
double operator* (const vector3& vector1, const vector3& vector2)
vector3 operator* (const vector3& vector1, const vector3& vector2)
This is not allowed because overload resolution takes into account the function signature, which does not include the return type:
3.19 signature [defns.signature]
⟨function⟩ name, parameter-type-list, and enclosing namespace (if any)
One possible solution, if you do want your operator* to possibly yield either a double or another vector3, you can return a proxy type that is convertible to these types:
struct vector3_multiplication_proxy {
vector3 lhs, rhs;
operator double() { return 0; /* Your inner product calculation here */ }
operator vector3() { return {}; /* Your cross product calculation here */ }
};
vector3_multiplication_proxy operator* (const vector3& lhs, const vector3& rhs) {
return {lhs, rhs};
}
This does have lifetime pitfalls and may delay calculation depending on how you use it, so it may or may not be a good idea. In your particular case, it's probably a bad idea, because the inner and cross products are different things and should probably be denoted by different syntax.

Related

Overloading issue

I'm trying to create a set of classes in order to handle complex numbers. I've seen that there is already a set of classes for the complex numbers, but because I'm learning C++ I thought it was a good idea to create a basic implementation. The problem comes up when I tried to overload the operator "/". I got a segfault and I can't understand if the problem is my implementation of the division:
complex.hpp :
#include <iostream>
#include <cstdlib>
class Complex {
float real;
float imm;
public:
Complex(float new_real = 0,float new_imm = 0) {this->real = new_real;this->imm = new_imm;}
void set(float new_real,float new_imm) {this->real = new_real; this->imm = new_imm;}
float get_real(void) const { return this->real;}
float get_imm(void) const { return this->imm;}
Complex conj(void) const {Complex tmp; tmp.set(this->real,-1.0 * this->imm); return tmp;}
friend std::ostream& operator<<(std::ostream& os, const Complex& cpx) {os << "Real: " << cpx.real << " Imm: " << cpx.imm << std::endl; return os; }
friend Complex operator*(const Complex& lhs,const Complex& rhs);
friend Complex operator+(const Complex& lhs,const Complex& rhs);
friend Complex operator+(const Complex& lhs,const float& rhs);
};
complex.cpp:
#include "complex.hpp"
Complex operator*(const Complex& lhs,const Complex& rhs)
{
float real_part = (lhs.real * rhs.real) - ( lhs.imm * rhs.imm);
float imm_part = (lhs.real * rhs.imm) + ( lhs.imm * rhs.real);
Complex result;
result.set(real_part,imm_part);
return result;
}
Complex operator+(const Complex& lhs,const Complex& rhs)
{
float real_part = lhs.real + rhs.real;
float imm_part = lhs.imm + rhs.imm;
Complex result;
result.set(real_part,imm_part);
return result;
}
Complex operator+(const Complex& lhs,const float& rhs)
{
float real_part = lhs.real + rhs;
float imm_part = lhs.imm;
Complex result;
result.set(real_part,imm_part);
return result;
}
Complex operator/(const Complex& lhs,const Complex& rhs)
{
Complex numerator(0,0);
numerator = rhs * rhs.conj();
Complex denominator(0,0);
denominator = lhs * rhs.conj();
Complex result;
float real_numerator = numerator.get_real();
result = denominator / real_numerator;
return result;
}
Complex operator/(const Complex& lhs,const float& rhs)
{
float real_part = lhs.get_real() / rhs;
float imm_part = lhs.get_imm() / rhs;
Complex result;
result.set(real_part,imm_part);
return result;
}
The whole idea of the division between 2 complex is to multiply the numerator and denominator for the conjugate of the numerator in order to have only a real number on the numerator. Just to make it clear :
(a + ib) / (c + id) = ((a + ib) / (c + id)) * ((c - id) / (c - id)) = ((a + ib) * (c - id)) / (c^2 + d^2)
Now when I try to do this:
main.cpp :
int main(int argc, char *argv[])
{
Complex x(4,8);
Complex y(3,7);
Complex result = x / y;
result = x / 6;
return 0;
}
I got this segfault, which I don't understand:
(gdb) break main
Breakpoint 2 at 0x401c56: file equalization_main.cpp, line 49.
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
`/home/campiets/workspace/frontend/dfe_equalizer_fe/dev/view/src_c/test' has changed; re-reading symbols.
Starting program: /home/campiets/workspace/frontend/dfe_equalizer_fe/dev/view/src_c/test
Breakpoint 2, main (argc=1, argv=0x7fffffffbf08) at equalization_main.cpp:49
49 Complex x(4,8);
(gdb) n
50 Complex y(3,7);
(gdb) n
51 Complex result = x / y;
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401e64 in Complex::Complex (this=<error reading variable: Cannot access memory at address 0x7fffff3feff8>, new_real=<error reading variable: Cannot access memory at address 0x7fffff3feff4>,
new_imm=<error reading variable: Cannot access memory at address 0x7fffff3feff0>) at complex.hpp:38
38 Complex(float new_real = 0,float new_imm = 0) {this->real = new_real; this->imm = new_imm;}
Any ideas ?
Complex operator/(const Complex& lhs,const Complex& rhs)
{
...
Complex denominator...;
...
float real_numerator = ...;
result = denominator / real_numerator;
...
}
That's infinite recursion.
Since the compiler hasn't seen operator/(const Complex &lhs, const float &rhs), it converts the float argument to Complex and hence you get recursion.
The simplest solution is to declare or define operator/(const Complex &lhs, const float &rhs) before operator/(const Complex &lhs, const Complex &rhs).
My preference would be to implement the operators as class members, though. That yields simpler source code and solves the problem too.
The function
Complex operator/(const Complex& lhs,const Complex& rhs) { ... }
causes stack overflow since the line
result = denominator / real_numerator;
ends up being interpreted as:
result = denominator / Complex(real_numerator);
You can resolve that problem by defining or declaring
Complex operator/(const Complex& lhs, const float& rhs)
before it.
If you change your code to use:
Complex operator/(const Complex& lhs,const float& rhs)
{
return Complex(lhs.get_real()/rhs, lhs.get_imm()/rhs);
}
Complex operator/(const Complex& lhs,const Complex& rhs)
{
...
}
your program will work ok.
A suggestion for simplifying the above operator/ function.
If you add the following member function
float magnitude_square() const { return (real*real + imm*imm); }
then you can use
Complex operator/(const Complex& lhs,const Complex& rhs)
{
return (lhs * rhs.conj())/rhs.magnitude_square());
}

C++ Member Function Cannot Change Private Field [duplicate]

This question already has an answer here:
About c++11 range for loops and iterators
(1 answer)
Closed 7 years ago.
OH! I found the problem. As I said at end, it was a simple problem.
In for loop, i iterated wiht copy of "Particle"s. I updated them but the operation does not affect the original values that stored in vector.
For requests about solution one can access the particle objects using 3 major ways.
lvalue-ref object way:
for (auto& p: particles)
...
Or by iterators:
for (auto it=particles.begin(); i<particles.end();++it)
...
Or by plain indexes:
for (size_t i=0; i<particles.size(); ++i)
...
For ones that curious about code, you may look below:
If I am not missing something very obvious, I encountered with a really strange problem.
To simplify the problem, I have a class named "Particle", which has a function called "update". Particle class has some private variables like position, velocity etc. And update function is supposed to make some calculations and add proper values to that private variables. But, that simply does not work.
Actually, I am sceptical about the type of that variables which is a class written by me, "Vector2".
To add values to variables, I've used the "+=" operator, and I think I implemented them correctly(?) in my Vector2 class.
To clarify situation, IMHO, it is better to give some code:
This is my Vector2.h :
namespace sim {
#include <ostream>
#include <cmath>
using namespace std;
struct Vector2 {
double x, y;
Vector2& operator+=(const Vector2& rhs) {
this->x += rhs.x;
this->y += rhs.y;
cout << "op+=" << endl;
return *this;
}
Vector2& operator*=(double k) {
x *= k;
y *= k;
cout << "op*=" << endl;
return *this;
}
friend ostream& operator<<(ostream& os, const Vector2& v2);
friend Vector2 operator+(Vector2 lhs, const Vector2& rhs);
friend Vector2 operator-(Vector2 lhs, const Vector2& rhs);
friend Vector2 operator*(Vector2 lhs, double k);
friend Vector2 operator*(double k, Vector2 rhs);
};
ostream& operator<<(ostream& os, const Vector2& v2) {
os << "[ " << v2.x << "; " << v2.y << " ]";
return os;
}
Vector2 operator+(Vector2 lhs, const Vector2& rhs) {
lhs += rhs;
cout << "op+" << endl;
return lhs;
}
Vector2 operator*(Vector2 lhs, double k) {
lhs *= k;
cout << "op*" << endl;
return lhs;
}
(removed some unrelated parts like constructor)
And this is Particle class:
class Particle {
private:
Vector2 pos;
Vector2 vel;
Vector2 acc;
double rad;
SDL_Color clr;
public:
Particle(
Vector2 _p,
Vector2 _v,
Vector2 _a,
double _r = 10.0,
SDL_Color _c = { 255, 255, 255, 255 }) {
pos = _p;
vel = _v;
acc = _a;
rad = _r;
clr = _c;
}
Vector2& move_particle(Vector2 move_by) {
return (pos += move_by);
}
void update(double dt) {
this->vel += acc * dt;
this->pos += vel * dt;
cout << "update " << pos << vel << acc << endl;
}
void render(SDL_Renderer* renderer) {
// TODO: this was supposed to be a circle, but, who cares? ;)
SDL_Rect r = { int(pos.x), int(pos.y), int(rad), int(rad) };
SDL_RenderFillRect(renderer, &r);
cout << "render" << endl;
}
};
And the result: update does not update.
How do I know (guess) my overloads DOES work?
For 3 Vector2 objects, I can do any kind of operation I defined correctly.
And the results are just what they supposed to be. (Or, maybe not?)
But debugging update function roughly brings me to the point that actually += operator works for one time, and not again BUT this behaviour is showing itself only in update function.
And I know, this will be such a simple mistake that I possibly feel ashamed (just kidding).
Oh, and the main code of course:
int main() {
SDL_Init(SDL_INIT_EVERYTHING);
auto window = SDL_CreateWindow("My Very First Particle Simulation", -1, -1,
1024, 768, SDL_WINDOW_SHOWN);
auto renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
const Vector2 g(0.0, 9.8);
double dt = 0.1;
std::vector<Particle> particles;
bool quit = false;
while (!quit) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
quit = true;
break;
case SDL_MOUSEBUTTONUP:
particles.emplace_back(Vector2(event.button.x, event.button.y), Vector2(1.0, 0.0), g);
break;
default:
break;
}
SDL_RenderClear(renderer);
for (auto p : particles) {
p.update(dt);
p.render(renderer);
}
SDL_RenderPresent(renderer);
}
}
}
Some comments on your code,
you can just change these to be member functions
friend Vector2 operator+(Vector2 lhs, const Vector2& rhs);
friend Vector2 operator-(Vector2 lhs, const Vector2& rhs);
friend Vector2 operator*(Vector2 lhs, double k);
friend Vector2 operator*(double k, Vector2 lhs);
as in
Vector2 operator+(const Vector2& rhs);
Vector2 operator-(const Vector2& rhs);
Vector2 operator*(const double k);
friend Vector2 operator*(const double k, Vector2 lhs);
In them copy this to a new Vector2 and then update the value and return. The fourth function is kept as friend so that you can have something like double*Vector2 while the third one takes care of Vector2*double
Any operation that has Vector2 as the left operand can be done as a member function.
I can see that these two operators are taking their first args by value. Change them so the take their args by reference.
Vector2 operator+(Vector2 lhs, const Vector2& rhs) { // lhs should be a const reference
lhs += rhs;
cout << "op+" << endl;
return lhs;
}
Vector2 operator*(Vector2 lhs, double k) { // lhs should be a const reference
lhs *= k;
cout << "op*" << endl;
return lhs;
}
EDIT:
Also these functions should take their class args as const referenecs:
friend Vector2 operator+(Vector2 lhs, const Vector2& rhs);
friend Vector2 operator-(Vector2 lhs, const Vector2& rhs);
friend Vector2 operator*(Vector2 lhs, double k);
friend Vector2 operator*(double k, Vector2 rhs);

Overloading Multiplication Operator

I am working on an assignment for my c++ class. We are having to overload several operators such as +, -, !=, =, etc. Well, I have all of them figured out except the multiplication. Everything I have tried gives an overflow or just doesn't compile. Not sure what I need for it.
Here is the header file that holds my overloads.
#ifndef COMPLEXNUMBER_H
#define COMPLEXNUMBER_H
#include<iostream>
using namespace std;
class ComplexNumber{
public:
double real, imaginary;
ComplexNumber(){
real = 0;
imaginary = 0;
}
ComplexNumber(double a, double b){
real = a;
imaginary = b;
}
ComplexNumber(double a){
real = a;
imaginary = 0;
}
ComplexNumber & operator= (const ComplexNumber & rhs){
if(this == &rhs){
return *this;
}
else{
real = rhs.imaginary;
imaginary = rhs.imaginary;
}
return *this;
}
ComplexNumber & operator+= (const ComplexNumber &rhs){
real += rhs.real;
imaginary += rhs.imaginary;
return *this;
}
ComplexNumber & operator-= (const ComplexNumber &rhs){
real -= rhs.real;
imaginary -= rhs.imaginary;
return *this;
}
const ComplexNumber operator+ (const ComplexNumber &rhs){
ComplexNumber result = *this;
result += rhs;
return result;
}
const ComplexNumber operator- (const ComplexNumber &rhs){
ComplexNumber result = *this;
result -= rhs;
return result;
}
const ComplexNumber operator* (const ComplexNumber &rhs){
return *this * rhs;
}
friend ostream & operator<< (ostream &out, const ComplexNumber &c){
out << "(" << c.real << (c.imaginary<0?" - ":" + ") << abs(c.imaginary) << " i)";
return out;
}
friend istream & operator>> (istream & in, ComplexNumber &c){
in >> c.real >> c.imaginary;
return in;
}
operator double(){
return real;
}
bool operator== (const ComplexNumber & rhs) const {
bool result = (this->real == rhs.real) && (this->imaginary == rhs.imaginary);
return result;
}
bool operator!= (const ComplexNumber &rhs) const{
return !(*this == rhs);
}
};
#endif
I know that multiplication operator is way off, but its just what I have at the moment. Here it is on its own. Any ideas would be greatly appreciated!!
const ComplexNumber operator* (const ComplexNumber &rhs){
return *this * rhs;
}
It gives you an overflow because of the way you call it. With your call, you are multiplying a complex number with a complex number, and it just keeps calling the same operator without doing anything. You could try to use some basic math and derive the formula for complex number multiplication. Specifically, let's say we have two complex numbers Z1 and Z2. Let Z1 = a + bi, where a is the real part, and b is the imaginary part, and Z2 = c + di, where c is the real part, and d is the imaginary part. We have Z1 * Z2 = (a + bi)(c + di) = ( ac + adi + cbi - bd ). Now, we separate the real and the imaginary part, the real part here is everything without i, so ac - bd, and the imaginary part would be ad + cb. Now, use that in the terms of your class members, and you would get something like this:
const ComplexNumber operator* (const ComplexNumber &rhs)
{
ComplexNumber result;
result.real = real * rhs.real - imaginary * rhs.imaginary;
result.imaginary = real * rhs.imaginary + imaginary * rhs.real;
return result;
}

Overloading * operator gives no match error

Hi I am trying to create a ray tracer that renders a polygonized, triangle-based model.
I have a point 3D struct in point3d.h that holds x,y, and z coordinates.
#ifndef __POINT3D_H__
#define __POINT3D_H__
#include <iostream>
using namespace std;
struct Point3D
{
double x;
double y;
double z;
Point3D() : x(0.0), y(0.0), z(0.0) {}
Point3D(const double & nx, const double & ny, const double & nz) : x(nx), y(ny), z(nz) {}
Point3D operator+(const Point3D & rhs) const {
return Point3D(x + rhs.x, y + rhs.y, z + rhs.z); }
Point3D operator-(const Point3D & rhs) const {
return Point3D(x - rhs.x, y - rhs.y, z - rhs.z); }
Point3D operator*(double val) const {
return Point3D(x * val, y * val, z * val); }
Point3D operator/(double val) const {
return Point3D(x / val, y / val, z / val); }
Point3D operator+=(const Point3D & rhs) {
x += rhs.x; y += rhs.y; z += rhs.z; return *this; }
Point3D operator-=(const Point3D & rhs) {
x -= rhs.x; y -= rhs.y; z -= rhs.z; return *this; }
Point3D operator*=(double val) {
x *= val; y *= val; z *= val; return *this; }
Point3D operator/=(double val) {
x /= val; y /= val; z /= val; return *this; }
void print() {
cout << '(' << x << ',' << y << ',' << z << ')';
}
};
#endif
Here is where I try to use the * operator to multiple two Point3Ds together
Point3D phong(Point3D mColor, Point3D lColor, Point3D L, Point3D N, Point3D R, Point3D V)
{
Point3D k(1.0, 1.0, 1.0);
Point3D ambient = mColor * k.x;
Point3D diffuse_angle = ((N * L) / (length(N) * length(L)));
Point3D diffuse = lColor * k.y * diffuse_angle;
Point3D specular_angle = ((R * V) / (length(R) * length(V)));
double specular_x = pow(specular_angle.x, 100.0);
double specular_y = pow(specular_angle.y, 100.0);
double specular_z = pow(specular_angle.z, 100.0);
Point3D specular_power(specular_x, specular_y, specular_z);
Point3D specular = lColor * k.z * specular_power;
return ambient + (lColor * (diffuse + specular));
}
When I try to multiple two Point3D's together, I am getting a no match error.
Here is where the code fails. I feel like it is a simple mistake but I cannot figure it out. I am including the Point3d header file as follows: #include "point3d.h".
Point3D operator*(double val) const
You have just this version, Point3D * double and nothing else, but you are trying to use this operator for Point3D * Point3D. Point3D is not implicitly constructible from double, so this is why you have compilation error.
Point3D operator*(double val) const {
This is for multiplication Point3D * double. And by
N * L
you are trying to do Point3D * Point3D.
You can rectify this either by providing proper operator* for your class OR provide a conversion from double to your class through single argument constructor. Although I prefer former.
You should need a function like this
Point3D operator *(Point3D &temp) const {
}
Since you don't have function to multiply two 3d points you are getting errors.Try adding this function.
You need a function for the operation Point3D * Point3D, which can't be adapted for the call of Point3D::operator*(double val). Such as:
Point3D operator*(const Point3D & rhs) const {
return Point3D(x * rhs.x, y * rhs.y, z * rhs.z); }

Generating Catmull Rom spline and returning garbage

After creating my Catmull Rom Spline such as:
vector3 SplineVector = newSpline.createCatmulRomSpline(vectorOne, vectorTwo, vectorThree, vectorFour, i);
However, when I read out the result from
vector3 SplineVector
I get garbage values.
Here is a listing of my spline class, a segment of my vector3 class and in the implementation within the initialization.
initialization:
for(float i = 0.0f; i <= 1.0f; i += 0.1f)
{
vector3 SplineVector = newSpline.createCatmulRomSpline(vectorOne, vectorTwo, vectorThree, vectorFour, i);
cout << "\n\ncurve pos X: " << SplineVector.getx();
cout << "\ncurve pos Y: " << SplineVector.gety();
cout << "\ncurve pos Z: " << SplineVector.getz();
}
Vector3:
class vector3
{
public:
vector3::vector3():x(0),y(0),z(0)
{
}
vector3::~vector3()
{
}
vector3(float);
vector3(float inx,float iny,float inz): x(inx), y(iny), z(inz)
{
}
//Vector operators
_inline vector3 operator=(const vector3& invec){ // Assignment
return vector3(this->x=invec.x,this->y=invec.y,this->z=invec.z);
}
_inline vector3 operator+(const vector3& invec){//Addition
return vector3(this->x+invec.x,this->y+invec.y,this->z+invec.z);
}
_inline vector3 operator-(const vector3& invec){//Subtraction
return vector3(this->x-invec.x,this->y-invec.y,this->z-invec.z);
}
_inline vector3 operator*(const vector3& invec){//Multiplication
return vector3(this->x*invec.x,this->y*invec.y,this->z*invec.z);
}
_inline vector3 operator/(const vector3& invec){//Division
return vector3(this->x/invec.x,this->y/invec.y,this->z/invec.z);
}
//Scaler operators
_inline vector3& operator+=(const float& scaler){//Addition self-assignment
return vector3(this->x+=scaler,this->y+=scaler,this->z+=scaler);
}
_inline vector3& operator-=(const float& scaler){//Subtraction self-assignment
return vector3(this->x-=scaler,this->y-=scaler,this->z-=scaler);
}
_inline vector3& operator*=(const float& scaler){//Multiplication self-assignment
return vector3(this->x*=scaler,this->y*=scaler,this->z*=scaler);
}
_inline vector3& operator*(const float& scalar){
return vector3(this->x*scalar, this->y*scalar, this->z*scalar);
}
//Math methods
_inline vector3 operator^(vector3& invec){//Cross product
return vector3( (this->y*invec.z-this->z*invec.y),
(this->z*invec.x-this->x*invec.z),
(this->x*invec.y-this->y*invec.x));
}
_inline vector3 operator&(vector3& invec){//Dot product
return (this->x*invec.x)+(this->y*invec.y)+(this->z*invec.z);
}
_inline vector3 distance(vector3&);//Distance
_inline void normalize(){
float mag = sqrtf(this->x*this->x+this->y*this->y+this->z*this->z);
this->x/=mag;
this->y/=mag;
this->z/=mag;
}
float x;
float y;
float z;
float getx();
float gety();
float getz();
float getMagnitude();
private:
float mag;
};
Catmull Rom Spline Generation
_inline vector3 createCatmulRomSpline(vector3 P0, vector3 P1, vector3 P2, vector3 P3, float t)
{
float t2 = t*t;
float t3 = t2*t;
vector3 result = ((P1*2) + (P2 - P0) * t + (P0*2 - P1 * 5 + P2*4 - P3)*t2 + (P1*3 - P0- P2*3 + P3) * t3)*0.5f;
return result;
}
I have tried other peoples code and when it comes to outputting the data into the final vector, it outputs bogus values.
You had a number of things wrong with your operators, although most of them weren't used. I modified your class. I notice you have not shown your code for the getx() (etc) members. I implemented those inline, but perhaps they were responsible for the garbage? Here is the test program with my changes. It seems to work fine:
class vector3
{
public:
vector3::vector3()
:x(0), y(0), z(0)
{}
vector3::~vector3()
{}
vector3(float inx,float iny,float inz)
: x(inx), y(iny), z(inz)
{}
//Vector operators
_inline vector3& operator=(const vector3& invec) { // Assignment
x = invec.x;
y = invec.y;
z = invec.z;
return *this;
}
_inline vector3 operator+(const vector3& invec) const {//Addition
return vector3(x+invec.x,y+invec.y,z+invec.z);
}
_inline vector3 operator-(const vector3& invec) const {//Subtraction
return vector3(x-invec.x,y-invec.y,z-invec.z);
}
_inline vector3 operator*(const vector3& invec) const {//Multiplication
return vector3(x*invec.x,y*invec.y,z*invec.z);
}
_inline vector3 operator/(const vector3& invec) const {//Division
return vector3(x/invec.x,y/invec.y,z/invec.z);
}
//scalar operators
_inline vector3& operator+=(const float& scalar){//Addition self-assignment
x+=scalar,y+=scalar,z+=scalar;
return *this;
}
_inline vector3& operator-=(const float& scalar){//Subtraction self-assignment
x-=scalar,y-=scalar,z-=scalar;
return *this;
}
_inline vector3& operator*=(const float& scalar){//Multiplication self-assignment
x*=scalar,y*=scalar,z*=scalar;
return *this;
}
_inline vector3 operator*(const float& scalar) const {
return vector3(x*scalar, y*scalar, z*scalar);
}
//Math methods
_inline vector3 operator^(const vector3& invec) const {//Cross product
return vector3( (y*invec.z-z*invec.y),
(z*invec.x-x*invec.z),
(x*invec.y-y*invec.x));
}
_inline float operator&(const vector3& invec) const {//Dot product
return (x*invec.x)+(y*invec.y)+(z*invec.z);
}
_inline float distance(vector3&) const;//Distance
_inline void normalize(){
float mag = sqrtf(x*x+y*y+z*z);
x/=mag;
y/=mag;
z/=mag;
}
float x;
float y;
float z;
_inline float getx() const { return x; }
_inline float gety() const { return y; }
_inline float getz() const { return z; }
float getMagnitude() const;
};
_inline vector3 createCatmulRomSpline(vector3 P0, vector3 P1, vector3 P2, vector3 P3, float t)
{
float t2 = t*t;
float t3 = t2*t;
vector3 result = ((P1*2) + (P2 - P0) * t + (P0*2 - P1 * 5 + P2*4 - P3)*t2 + (P1*3 - P0- P2*3 + P3) * t3)*0.5f;
return result;
}
int main() {
vector3 vectorOne(0,0,0);
vector3 vectorTwo(5,10,1);
vector3 vectorThree(10,10,2);
vector3 vectorFour(15,0,3);
const int ndiv = 10;
for( int i = 0; i <= ndiv; i++ )
{
float t = (float)i / ndiv;
vector3 SplineVector = createCatmulRomSpline(vectorOne, vectorTwo, vectorThree, vectorFour, t);
cout << "curve pos X: " << SplineVector.getx() << "\n";
cout << "curve pos Y: " << SplineVector.gety() << "\n";
cout << "curve pos Z: " << SplineVector.getz() << "\n\n";
}
return 0;
}