开发者

C++ Filling an 1D array to represent a n-dimensional object based on a straight line segment

READ FIRST: I have rewritten this question with the help of a friend to be hopefully more specific in what is required. It can be found here

I'm not very clear on n-cubes, but I believe they are what I am referring to as the square family.

New Question Wording:

Perhaps I wasn't clear enough. What I'm asking, is how to set a 1D array to hold data for a cloud of a number of evenly-spaced points that form the most complete representation of the space occupied by an n-cube of n dimensions.

In 1D this would simply fill the array with a series of 1D co-ordinates creating a line segment. A 1-cube.

In 2D however this would fill every first co-ordinate to the x value and the every second to the y, generating the most complete square possible for that spacing and number of particles. The most complete possible 2-cube.

In 3D, this would fill ever first with x, every second with y and every third with z, generating the most complete possible cube for that spacing and number of particles. The most complete possible 3-cube.

I wish to be able to do this for any reasonable combination of number of particles, spacing and dimensions. Ideally I could do at least up to a 4-cube using a generic fill algorithm for all n-cubes initialised to double * parts_

Yet another definition of what kind of object I'm trying to represent:

In 1D its a line. Sweep it through the second dimension it becomes a square. Sweep that square through the third, it becomes a cube. I presume this behaviour extends past three dimensions and wish to store a cloud of points representing the space taken up by one of these objects of any reasonable dimension, spacing and number of points in a 1D array.

The original wording of the question:

I'm struggling to find a good way to put this question but here goes. I'm making a system that uses a 1D array implemented as double * parts_ = new double[some_variable];. I want to use this to hold co-ordinates for a particle system that can run in various dimensions.

What I want to be able to do is write a generic fill algorithm for filling this in n-dimensions with a common increment in all direction to a variable size. Examples will serve best I think.

Consider the case where the number of particles stored by the array is 4

In 1D this produces 4 elements in the array because each particle only has one co-ordinate.

1D:

{0, 25, 50, 75};

In 2D this produces 8 elements in the array because each particle has two co-ordinates..

2D:

{0, 0, 0, 25, 25, 0, 25, 25}

In 3D this produces 12 elements in the array because each particle now has three co-ordinates

{0, 0, 0, 0, 0, 25, 0, 0, 50, ... }

These examples are still not quite accurate, but they hopefully will suffice.

The way I would do this normally for two dimensions:

int i = 0; 
for(int x = 0; x < parts_size_ / dims_ / dims_ * 25; x += 25) {  
    for(int y = 0; y < parts_size_ / dims_ / dims_ * 25; y += 25) { 
        parts_[i] = x;
        parts_[i+1] = y;
        i+=2;
    }
}

How can I implement this for n-dimensions where 25 can be any number?

The straight line part is because it seems to me logical that a line is a somewhat regular shape in 1D, as is a square in 2D, and a cube in 3D. It seems to me that it would follow that there would be similar shapes in this family that could be implemented for 4D and higher dimensions via a similar fill pattern. This is the shape I wish to set my array to represent.

EDIT: Apparently I'm trying to fill this开发者_如何学C array to represent the n-cube with the fewest missing elements for the given n, spacing and number of elements. If that makes my goal any clearer.


As I understand it, you aren't sure how to process every element in multi-dimensional array (stored as 1D array), where N is arbitrary number of dimensions.

Processing of multidimensional array with arbitrary number of dimensions goes like this:

#include <stdio.h>
#include <vector>
using std::vector;

int main(int argc, char** argv){
    int index = 0;
    const int numDimensions = 10;
    vector<int> counters;
    vector<int> dimensionSizes;
    counters.resize(numDimensions);
    dimensionSizes.resize(numDimensions);

    for (int i = 0; i < numDimensions; i++){
        counters[i] = 0;
        dimensionSizes[i] = 13;
    }

    long long arraySize = 1;
    for (int i = 0; i < numDimensions; i++)
        arraySize *= dimensionSizes[i];


    printf("%d\n", arraySize);
    for (int elementIndex = 0; elementIndex < arraySize; elementIndex++){
        fprintf(stderr, "element %08d: ", elementIndex);
        for (int i = 0; i < numDimensions; i++)    
            fprintf(stderr, "%04d ", counters[i]);
        fprintf(stderr, "\n");
    //at this point you have 1D element index 
    //AND all n-dimensional coordinates stored in counters array. 
    //Just use them to for your data
    //"counters" is N-dimensional coord. XYZW etc.

        for (int i = 0; i < numDimensions; i++){
            counters[i] = counters[i] + 1;                
            if (counters[i] < dimensionSizes[i])
                break;
            else
                counters[i] = 0;
        }
    }


    return 0;
}

Just make an array of structs you need to access in N dimensions, and access them using calculated index somewhere after comment. It is better to use array of structs representing the data you want to be stored in N dimensionals. If you don't want to do that, you'll have to multiply elementIndex by number of doubles per element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜