0

抱歉,如果这是很多代码,我只是不确定你们到底需要什么来解决我的问题。我知道总线错误或多或少对应于我的主要方法中的堆栈溢出,但我不完全理解为什么会发生这种情况。我有一个填充了数百万个自定义三角形对象的向量,我正在尝试处理这些对象以绘制二维图片。这是我的代码:

vtkImageData *image = NewImage(1786, 1344);
unsigned char *buffer = 
 (unsigned char *) image->GetScalarPointer(0,0,0);
int npixels = 1786*1344;
for (int i = 0 ; i < npixels*3 ; i++)
    buffer[i] = 0;

std::vector<Triangle> triangles = GetTriangles();
Screen *screen = new Screen;
screen->buffer = buffer;
screen->width = 1786;
screen->height = 1344;
double leftEnd, rightEnd;
int highIndex, leftIndex, rightIndex, bottomIndex, midIndex;
for(std::vector<Triangle>::iterator it = triangles.begin(); it != triangles.end(); ++it)
{

    // Get this triangle
    Triangle current = *it;
    Triangle curr2;
    char triangleType = current.whichTriangle();
    // It's a flat-bottom triangle
    if(triangleType == 'b'){    
        highIndex = current.getFlatMidIndex();
        bottomIndex = current.getFlatLeftIndex();
    // It's a flat-top traingle
    }else if(triangleType == 'u'){
        highIndex = current.getFlatLeftIndex();
        bottomIndex = current.getFlatMidIndex();
    //It's an arbitrary triangle
    }else{

        // Make two triangles, with new coordinates

        curr2.X[0] = current.X[current.getArbLowIndex()];
        curr2.Y[0] = current.Y[current.getArbLowIndex()];
        curr2.X[1] = current.X[current.getArbMidIndex()];
        curr2.Y[1] = current.Y[current.getArbMidIndex()];
        curr2.X[2] = splitTriangle(current, curr2.Y[1]);
        curr2.Y[2] = current.Y[current.getArbMidIndex()];
        int currLowIndex = current.getArbLowIndex();
        current.X[currLowIndex] = curr2.X[2];
        current.Y[currLowIndex] = curr2.Y[2];
        highIndex = current.getFlatMidIndex();
        bottomIndex = current.getFlatLeftIndex(); 

    }

        //perform the scan-line alg on this triangle
        for(double i = ceil441(current.Y[bottomIndex]); i <= floor441(current.Y[highIndex]); i++)
        {

            leftEnd = calcLeftEnd(current, i, i);
            rightEnd = calcRightEnd(current, i, i);
            // If this triangle extends past the left border, set it's left edge to 0
            if(leftEnd < 0){
                leftEnd = 0;
            }
            // If this triangle extends past the left border, set it's right edge to 999
            if(rightEnd > (screen->width - 1)){
                rightEnd = screen->width - 1;
            }

            for(double j = ceil441(leftEnd); j <= floor441(rightEnd); j++)
            {   

                // Pixels to color will be ((Y * 50 + X) * 3); this will account
                // for the fact that the buffer is one long array, not a 
                // 2D array like we think of it
                int pixel = ((i * screen->width) + j) * 3;
                // Make sure we don't segfault
                if(pixel < (npixels * 3)){
                    screen->buffer[pixel] = current.color[0];
                    screen->buffer[pixel + 1] = current.color[1];
                    screen->buffer[pixel + 2] = current.color[2];
                }               
            }
        }


    if(triangleType == 'a'){

        highIndex = curr2.getFlatLeftIndex();
        bottomIndex = curr2.getFlatMidIndex();
        for(double i = ceil441(curr2.Y[bottomIndex]); i <= floor441(curr2.Y[highIndex]); i++)
        {

            leftEnd = calcLeftEnd(curr2, i, i);
            rightEnd = calcRightEnd(curr2, i, i);

            // If this triangle extends past the left border, set it's left edge to 0
            if(leftEnd < 0){
                leftEnd = 0;
            }
            // If this triangle extends past the left border, set it's right edge to 999
            if(rightEnd > (screen->width - 1)){
                rightEnd = screen->width - 1;
            }

            for(double j = ceil441(leftEnd); j <= floor441(rightEnd); j++)
            {   

                // Pixels to color will be ((Y * 50 + X) * 3); this will account
                // for the fact that the buffer is one long array, not a 
                // 2D array like we think of it
                int pixel = ((i * screen->width) + j) * 3;
                // Make sure we don't segfault
                if(pixel < (npixels * 3)){
                    screen->buffer[pixel] = current.color[0];
                    screen->buffer[pixel + 1] = current.color[1];
                    screen->buffer[pixel + 2] = current.color[2];
                }               
            }
        }   

    }
}

我已经能够处理大约 200,000 个三角形,但在那之后我得到了一个总线错误。我知道我必须将其中一些代码移出我的主要方法,但我不确定哪些部分以及为什么。感谢帮助。

4

0 回答 0