生成Catmull-Rom样条曲线并返回垃圾



创建Catmull Rom样条线后,例如:

vector3 SplineVector = newSpline.createCatmulRomSpline(vectorOne, vectorTwo, vectorThree, vectorFour, i);

然而,当我从中读出结果时

vector3 SplineVector

我得到垃圾值。

这里列出了我的样条曲线类、vector3类的一个片段以及在初始化中的实现。

初始化:

for(float i = 0.0f; i <= 1.0f; i += 0.1f)
    {
        vector3 SplineVector = newSpline.createCatmulRomSpline(vectorOne, vectorTwo, vectorThree, vectorFour, i);
        cout << "nncurve pos X: " << SplineVector.getx();
        cout << "ncurve pos Y: " << SplineVector.gety();
        cout << "ncurve pos Z: " << SplineVector.getz();
    }

矢量3:

   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样条线生成

_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;
}

我尝试过其他人的代码,当涉及到将数据输出到最终向量时,它会输出伪造的值。

您的运算符有很多问题,尽管大多数都没有使用。我修改了你的课。我注意到您尚未显示getx()(etc)成员的代码。我实现了这些内联,但也许它们对垃圾负有责任?这是我修改的测试程序。它似乎运行良好:

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() << "nn";
    }
    return 0;
}

相关内容

  • 没有找到相关文章

最新更新