Ошибка шины при сканировании алгоритма обработки графики

Извините, если кода много, я просто не уверен, что именно вам понадобится, чтобы выяснить мою проблему. Я понимаю, что ошибка шины более или менее соответствует переполнению стека в моем основном методе, но я не совсем понимаю, почему это происходит. У меня есть вектор, заполненный миллионами пользовательских объектов треугольника, которые я пытаюсь обработать, чтобы нарисовать двумерную картинку. Вот мой код:

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 треугольников, но после этого я получаю ошибку шины. Я понимаю, что должен удалить часть этого кода из основного метода, но я не уверен, какие части и почему. Спасибо за помощь.

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …