My algorithm computes the vertices of a shape in 3-dimensional space by using a 2-dimensional loop iterating over the U and V segments.
for (LONG i=0; i < info.useg + 1; i++) {
// Calculate the u-parameter.
u = info.umin + i * info.udelta;
for (LONG j=0; j < info.vseg + 1; j++) {
// Calculate the v-parameter.
v = info.vmin + j * info.vdelta;
// Compute the point's position.
point = calc_point(op, &info, u, v);
// Set the point to the object and increase the point-index.
points[point_i] = point;
point_i++;
}
}
The array of points is however a one-dimensional array, which is why point_i
is incremented in each loop. I know that I could compute the index via point_i = i * info.vseg + j
.
I want this loop to be multithreaded. My aim was to create a number of threads that all process a specific range of points. In the thread, I'd do it like:
for (LONG x=start; x <= end; x++) {
LONG i = // ...
LONG j = // ...
Real u = info.umin + i * info.udelta;
Real v = info.vmin + j * info.vdelta;
points[i] = calc_point(op, &info, u, v);
}
The problem is to calculate the i
and j
indecies from the linear point-index. How can I compute i
and j
when (well I think):
point_i = i * vsegments + j
I'm unable to solve the math, here..