程序在注释中显示不同的输出

时间:2019-07-13 08:27:17

标签: c++ output cout variations

我的程序正在triangle上执行缩放,平移和旋转操作。 algorithm在相应的代码块中被注释掉,我检查了一千遍,算法或逻辑实现没有问题。(我知道我的编码结构很笨拙,我将发表有关如何设计的文章稍后在stack review中编写更好的代码。

程序从scene.txt获取输入并输出到stage1.txt。在单独的计算机上运行时,输出是不同的。

  

scene.txt

0.0 0.0 50.0
0.0 0.0 0.0 
0.0 1.0 0.0
80.0 1.0 1.0 100.0
push
scale
2.0 2.0 2.0
translate
10.0 0.0 0.0
rotate
90.0 0.0 0.0 1.0
triangle
0.0 0.0 0.0
5.0 0.0 0.0
0.0 5.0 0.0
pop
end

stage1.txt中,预期输出为:

20.0000000 0.0000000 0.0000000 
20.0000000 10.0000000 0.0000000 
10.0000000 0.0000000 0.0000000 

但是它在我的机器上显示:

20.0000000 0.0000000 0.0000000 
20.0000000 10.0000000 10.0000000 
10.0000000 0.0000000 0.0000000 

当我从此行中删除//时,我的机器将显示正确的输出。

  

//cout << "\ntemp4 " << temp4.x << " " << temp4.y << " " << temp4.z << endl;

我无法理解这是什么法术。代码如下:

//
// Created by afsara on 7/11/19.
//

#include <iostream>
#include <stack>
#include <vector>
#include <cstdio>
#include <fstream>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;
const double PI = acos(-1.0);
const double EPS = 1e-4;

ifstream infile;
ofstream outfile;
int sz = 4;

stack<float (*)[10]> s;
stack<int> Size;


struct Point {
    float x, y, z, w = 1;

    Point() {}
};

struct Vector {
    float x, y, z, w = 0;

    Vector() {
        x = x;
        y = y;
        z = z;
    };

    Vector(float vx, float vy, float vz) {

        x = vx;
        y = vy;
        z = vz;
        w = 0;
    }

};

Vector eye(0, 0, 0), look(0, 0, 0), up(0, 0, 0);
float fovY, aspectRatio, near, far;

/*  functions   */

float (*matrixMultiplication(float firstMatrix[][10], float secondMatrix[][10],
                             int rowFirst,
                             int columnFirst, int rowSecond, int columnSecond))[10];


float (*makeIdentityMatrix(int identity_sz))[10];

void insertToStack(float arr[][10]);

float (*makeTranslationMatrix(float transX, float transY, float transZ))[10];

float (*makeScalingMatrix(float scaleX, float scaleY, float scaleZ))[10];

inline double degToRad(double ang);

static inline bool isNearlyEqual(const double &a, const double &b);

float Cos(float angle);

float Sin(float angle);

Vector crossProduct(const Vector &vec1, const Vector &vec2);

float dotProduct(const Vector &vec1, const Vector &vec2);

Vector normalize(Vector a);

Vector multiply(Vector v, float scalar);

Vector add(Vector v1, Vector v2);

Vector rotateRod(Vector x, Vector rotateAxis, float rotateAngle);

void printMatrix(float (*matrix)[10]);

void print(float (*matrix)[10]);

void readFromFile();

int main() {


    float (*identityMatrix)[10] = makeIdentityMatrix(sz);
    insertToStack(identityMatrix);

    outfile.open("stage1.txt");
    readFromFile();
    outfile.close();

    return 0;
}


inline double degToRad(double ang) {
    return ang * PI / 180.0;
}

static inline bool isNearlyEqual(const double &a, const double &b) {
    return abs(a - b) < EPS;
}

float Cos(float angle) {
    float var = cos(degToRad(angle));
    if (isNearlyEqual(var, 0)) var = 0;
    return var;
}

float Sin(float angle) {
    float var = sin(degToRad(angle));
    if (isNearlyEqual(var, 0)) var = 0;
    return var;
}

Vector crossProduct(const Vector &vec1, const Vector &vec2) {

    Vector res;
    res.x = vec1.y * vec2.z - vec2.y * vec1.z;
    res.y = vec1.z * vec2.x - vec2.z * vec1.x;
    res.z = vec1.x * vec2.y - vec2.x * vec1.y;

    return res;
}


float dotProduct(const Vector &vec1, const Vector &vec2) {

    float res;

    res += vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z;
    if (isNearlyEqual(res, 0)) res = 0;

    return res;
}

Vector normalize(Vector a) {

    float val = sqrt(a.x * a.x + a.y * a.y + a.z * a.z);

    Vector p;
    p.x = a.x / val;
    p.y = a.y / val;
    p.z = a.z / val;

    //cout << "\nnormalizing\n[ " << p.x << " " << p.y << " " << p.z << " " << p.w << " ] \n";
    return p;
}

Vector multiply(Vector v, float scalar) {
    // cout << "scalar is " << scalar << endl;
    v.x = v.x * scalar;
    v.y = v.y * scalar;
    v.z = v.z * scalar;
    return v;
}

Vector add(Vector v1, Vector v2) {
    Vector ret(0, 0, 0);
    ret.x = v1.x + v2.x;
    ret.y = v1.y + v2.y;
    ret.z = v1.z + v2.z;
    return ret;
}

Vector rotateRod(Vector x, Vector rotateAxis, float rotateAngle) {

    Vector temp1 = multiply(x, Cos(rotateAngle)); //cos(theta)*x ; x is a vector
    //cout << "\ntemp1 " << temp1.x << " " << temp1.y << " " << temp1.z << endl;

    Vector temp2 = crossProduct(rotateAxis, x); // a cross x
    // cout << "a cross x : " << rotateAxis.x << " " << rotateAxis.y << " " << rotateAxis.z << " cross " << x.x << " "
    // << x.y << " " << x.z << endl;
    //cout << "\ntemp2 " << temp2.x << " " << temp2.y << " " << temp2.z << endl;

    Vector temp3 = multiply(temp2, Sin(rotateAngle)); //sin(theta) * (a cross x)
    //cout << "\ntemp3 " << temp3.x << " " << temp3.y << " " << temp3.z << endl;

    Vector temp4 = add(temp1, temp3); // cos(theta)*x + sin(theta) * (a cross x)
    //cout << "\ntemp4 " << temp4.x << " " << temp4.y << " " << temp4.z << endl;


    float temp5 = dotProduct(rotateAxis, x); // a dot x
    //cout << "\ntemp5 " << temp5 << endl;

    Vector temp6 = multiply(rotateAxis, temp5); // (a dot x)*a
    //cout << "\ntemp6 " << temp6.x << " " << temp6.y << " " << temp6.z << endl;

    Vector temp7 = multiply(temp6, (1 - Cos(rotateAngle))); // (1-cos(theta)) * (a dot x)*a
    //cout << "\ntemp7 " << temp7.x << " " << temp7.y << " " << temp7.z << endl;

    Vector finalR = add(temp4,
                        temp7); // cos(theta)*x + sin(theta) * (a cross x) + (1-cos(theta)) * (a dot x)*a
    //cout << "\nfinal  " << finalR.x << " " << finalR.y << " " << finalR.z << endl;

    return finalR;
}


void printMatrix(float (*matrix)[10]) {
    cout << "\n in print matrix \n";

    for (int i = 0; i < sz - 1; ++i) {
        for (int j = 0; j < sz - 1; ++j) {
            outfile << setprecision(7) << fixed << matrix[j][i] << " ";
        }
        outfile << endl;
    }
    outfile << endl;
}

void print(float (*matrix)[10]) {

    //cout << "\nprint in console\n";
    for (int i = 0; i < sz; ++i) {
        for (int j = 0; j < sz; ++j) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}


float (*balance_W(float (*m)[10]))[10] {

    float w1 = m[sz - 1][0];
    float w2 = m[sz - 1][1];
    float w3 = m[sz - 1][2];

    for (int i = 0; i < sz; ++i) {
        for (int j = 0; j < sz; ++j) {
            if (j == 0) m[i][j] = m[i][j] / w1;
            if (j == 1) m[i][j] = m[i][j] / w2;
            if (j == 2) m[i][j] = m[i][j] / w3;
        }
    }

    return m;

}


void readFromFile() {
    // open a file in read mode.
    cout << "\n\n~~~~~~~~~~~~~~~~~~~~~~~~ Reading from the scene.txt file  ~~~~~~~~~~~~~~~~~~~~~~~\n\n";

    string line;

    infile.open("scene.txt");

    if (!infile.is_open()) {
        perror("Error open");
        exit(EXIT_FAILURE);
    }

    int line_num = 0;
    vector<string> tokens[100];     // Create vector to hold the words
    while (getline(infile, line)) {

        string buf;                     // Have a buffer string
        stringstream ss(line);       // Insert the string into a stream

        while (ss >> buf)
            tokens[line_num].push_back(buf);

        line_num++;
    }

    //printTokens(tokens, line_num);

    string command;
    for (int i = 0; i < line_num; ++i) {

        if (i == 0) {
            eye.x = stof(tokens[i][0].c_str());
            eye.y = stof(tokens[i][1].c_str());
            eye.z = stof(tokens[i][2].c_str());
        } else if (i == 1) {
            look.x = stof(tokens[i][0].c_str());
            look.y = stof(tokens[i][1].c_str());
            look.z = stof(tokens[i][2].c_str());
        } else if (i == 2) {
            up.x = stof(tokens[i][0].c_str());
            up.y = stof(tokens[i][1].c_str());
            up.z = stof(tokens[i][2].c_str());
        } else if (i == 3) {
            fovY = stof(tokens[i][0].c_str());
            aspectRatio = stof(tokens[i][1].c_str());
            near = stof(tokens[i][2].c_str());
            far = stof(tokens[i][3].c_str());
        } else {

            for (int itr = 0; itr < tokens[i].size(); ++itr) {

                command = tokens[i][itr];

                //start parsing commands
                if (command == "triangle") {

                    //go to next line
                    i++;

                    cout << "\t\tfound a triangle " << endl;

                    //input three points
                    struct Point firstPoint, secondPoint, thirdPoint;

                    firstPoint.x = stof(tokens[i][itr].c_str());
                    firstPoint.y = stof(tokens[i][itr + 1].c_str());
                    firstPoint.z = stof(tokens[i][itr + 2].c_str());

                    i++;
                    secondPoint.x = stof(tokens[i][itr].c_str());
                    secondPoint.y = stof(tokens[i][itr + 1].c_str());
                    secondPoint.z = stof(tokens[i][itr + 2].c_str());

                    i++;
                    thirdPoint.x = stof(tokens[i][itr].c_str());
                    thirdPoint.y = stof(tokens[i][itr + 1].c_str());
                    thirdPoint.z = stof(tokens[i][itr + 2].c_str());

                    float myMatrix[10][10];

                    vector<float> temp;
                    temp.push_back(firstPoint.x);
                    temp.push_back(secondPoint.x);
                    temp.push_back(thirdPoint.x);
                    temp.push_back(1);

                    temp.push_back(firstPoint.y);
                    temp.push_back(secondPoint.y);
                    temp.push_back(thirdPoint.y);
                    temp.push_back(1);

                    temp.push_back(firstPoint.z);
                    temp.push_back(secondPoint.z);
                    temp.push_back(thirdPoint.z);
                    temp.push_back(1);

                    temp.push_back(1);
                    temp.push_back(1);
                    temp.push_back(1);
                    temp.push_back(1);


                    for (int j = 0; j < sz; ++j) {

                        for (int k = 0; k < sz; ++k) {
                            myMatrix[j][k] = temp.at(j * 4 + k);
                        }
                    }


                    /*cout << "\n\t\tABOUT TO MULTIPLY THE FOLLOWING\n\n";

                    print(s.top());
                    cout << endl;
                    print(myMatrix);
                    cout << "\n\n";*/


                    float (*resultant)[10];

                    resultant = matrixMultiplication(s.top(), myMatrix, sz, sz, sz, sz);
                    resultant = balance_W(resultant);

                    printMatrix(resultant); //T*I

                    print(resultant);

                    //showstack(s);

                } else if (command == "scale") {
                    // input scaling factors
                    // generate the corresponding scaling matrix T
                    // S.push(product(S.top,T))

                    //go to next line
                    i++;

                    cout << "\t\tdo scaling " << endl;
                    struct Point scaleFactor;

                    //parsing values
                    scaleFactor.x = stof(tokens[i][itr].c_str());
                    scaleFactor.y = stof(tokens[i][itr + 1].c_str());
                    scaleFactor.z = stof(tokens[i][itr + 2].c_str());


                    float (*scaleMatrix)[10] = new float[10][10];
                    scaleMatrix = makeScalingMatrix(scaleFactor.x, scaleFactor.y, scaleFactor.z);
                    //print(scaleMatrix);


                    float (*prev)[10];
                    float (*New)[10];
                    prev = s.top();
                    New = matrixMultiplication(prev, scaleMatrix, sz, sz, sz, sz);
                    s.push(New);

                    //showstack(s);

                } else if (command == "translate") {

                    // input translation amounts
                    // generate the corresponding translation matrix T
                    // S.push(product(S.top,T))

                    //go to next line
                    i++;

                    cout << "\t\tdo translate " << endl;
                    struct Point t;

                    //parsing values
                    t.x = stof(tokens[i][itr].c_str());
                    t.y = stof(tokens[i][itr + 1].c_str());
                    t.z = stof(tokens[i][itr + 2].c_str());


                    float (*T)[10] = new float[10][10];
                    T = makeTranslationMatrix(t.x, t.y, t.z);
                    //printMatrix(T);

                    float (*prev)[10];
                    float (*New)[10];
                    prev = s.top();
                    New = matrixMultiplication(prev, T, sz, sz, sz, sz);
                    s.push(New);

                } else if (command == "rotate") {
                    // input rotation angle and axis
                    // generate the corresponding rotation matrix T
                    // S.push(product(S.top,T))

                    //go to next line
                    i++;

                    cout << "\t\tdo rotate " << endl;
                    struct Vector rotateAxis;
                    float rotateAngle;

                    //parsing values
                    rotateAngle = stof(tokens[i][itr].c_str());
                    //rotateAngle = degToRad(rotateAngle);

                    rotateAxis.x = stof(tokens[i][itr + 1].c_str());
                    rotateAxis.y = stof(tokens[i][itr + 2].c_str());
                    rotateAxis.z = stof(tokens[i][itr + 3].c_str());

                    rotateAxis = normalize(rotateAxis);

                    Vector c1, c2, c3;

                    Vector iHat(1, 0, 0), jHat(0, 1, 0), kHat(0, 0, 1);

                    c1 = rotateRod(iHat, rotateAxis, rotateAngle);
                    c2 = rotateRod(jHat, rotateAxis, rotateAngle);
                    c3 = rotateRod(kHat, rotateAxis, rotateAngle);

                    /*cout << "c1 : " << c1.x << " " << c1.y << " " << c1.z << " " << endl;
                    cout << "c2 : " << c2.x << " " << c2.y << " " << c2.z << " " << endl;
                    cout << "c3 : " << c3.x << " " << c3.y << " " << c3.z << " " << endl;*/

                    float R[10][10];

                    vector<float> temp;
                    temp.push_back(c1.x);
                    temp.push_back(c2.x);
                    temp.push_back(c3.x);
                    temp.push_back(0);

                    temp.push_back(c1.y);
                    temp.push_back(c2.y);
                    temp.push_back(c3.y);
                    temp.push_back(0);

                    temp.push_back(c1.z);
                    temp.push_back(c2.z);
                    temp.push_back(c3.z);
                    temp.push_back(0);

                    temp.push_back(0);
                    temp.push_back(0);
                    temp.push_back(0);
                    temp.push_back(1);

                    for (int j = 0; j < sz; j++) {

                        for (int k = 0; k < sz; k++) {
                            R[j][k] = temp.at(j * 4 + k);
                        }
                    }

                    cout << "printing rotation matrix" << endl;
                    print(R);

                    float (*prev)[10];
                    float (*New)[10];
                    prev = s.top();
                    New = matrixMultiplication(prev, R, sz, sz, sz, sz);
                    s.push(New);

                } else if (command == "push") {

                    cout << "PUSH" << endl;
                    Size.push(s.size());

                } else if (command == "pop") {


                    cout << "POP" << endl;
                    if (s.size() == 1) continue;
                    int l = Size.top();
                    Size.pop();
                    while (s.size() > l) {
                        s.pop();
                    }
                } else if (command == "end") {
                    break;
                }
            }
        }
    }
    infile.close();
}


float
(*matrixMultiplication(float firstMatrix[][10], float secondMatrix[][10], int rowFirst,
                       int columnFirst,
                       int rowSecond, int columnSecond))[10] {

    //cout << "first matrix is :\n";
    // print(firstMatrix);

    //cout << "second matrix is :\n";
    // print(secondMatrix);
    float (*resultantMatrix)[10] = new float[10][10]();
    int i, j, k;

    // multiplying firstMatrix and secondMatrix and storing in array resultantMatrix.
    for (i = 0; i < rowFirst; ++i) {
        for (j = 0; j < columnSecond; ++j) {
            for (k = 0; k < columnFirst; ++k) {
                resultantMatrix[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
            }
        }
    }

    //printing
    for (int l = 0; l < sz; ++l) {
        for (int m = 0; m < sz; ++m) {
            // cout << " [ " << l << "] [" << m << " ] " << resultantMatrix[l][m] << " ";
        }
        //cout << endl;
    }
    return resultantMatrix;
}


float (*makeIdentityMatrix(int identity_sz))[10] {

    float (*identity)[10] = new float[10][10];
    int row, col;

    for (row = 0; row < identity_sz; row++) {
        for (col = 0; col < identity_sz; col++) {

            // Checking if row is equal to column
            if (row == col) {
                identity[row][col] = 1;
            } else {
                identity[row][col] = 0;
            }
        }
    }
    return identity;
}


void insertToStack(float (*arr)[10]) {

    cout << "\n\ninserting to stack\n";

    s.push(arr);

    cout << "s.size() : " << s.size() << "\n\n";

}

float (*makeScalingMatrix(float scaleX, float scaleY, float scaleZ))[10] {

    float (*myMatrix)[10] = new float[10][10];

    for (int i = 0; i < sz; ++i) {
        for (int j = 0; j < sz; ++j) {

            if (i == j && i == 0) myMatrix[i][j] = scaleX;
            else if (i == j && i == 1) myMatrix[i][j] = scaleY;
            else if (i == j && i == 2) myMatrix[i][j] = scaleZ;
            else if (i == j && i == 3) myMatrix[i][j] = 1;
            else
                myMatrix[i][j] = 0;

        }
    }
    return myMatrix;
}


float (*makeTranslationMatrix(float transX, float transY, float transZ))[10] {

    float (*myMatrix)[10] = new float[10][10];

    for (int i = 0; i < sz; ++i) {
        for (int j = 0; j < sz; ++j) {

            if (i == j) {
                myMatrix[i][j] = 1;

            } else {
                if (i == 0 && j == 3) myMatrix[i][j] = transX;
                else if (i == 1 && j == 3) myMatrix[i][j] = transY;
                else if (i == 2 && j == 3) myMatrix[i][j] = transZ;
                else myMatrix[i][j] = 0;
            }

        }
    }
    return myMatrix;
}

1 个答案:

答案 0 :(得分:1)

真的很抱歉,但是代码太可怕了。您应该重构它。

问题之一是您没有初始化变量。所以在

float dotProduct(const Vector &vec1, const Vector &vec2) {

    float res;

    res += vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z;
    if (isNearlyEqual(res, 0)) res = 0;

    return res;
}

存在未初始化的float res。然后,将+ =赋值给一个未初始化的值。

因此,请删除真正的bug并将+ =替换为=

然后开始使用C ++而不是C语言进行编程。重构代码。使用诱人的变量名。永远不要,实际上永远不要使用using namespace std;。写评论。

最重要的是:编译带有所有警告的代码。编译器将为您检测到此错误。始终是最高警告级别。然后删除所有警告!