Die-hard vertex-sorting function not accepting input values!

Member
Posts: 281
Joined: 2009.04
Post: #1
OK, so, here I am again.I have been trying and trying to get my vertex sorting physics function to work. The idea is:

• Pick a grid square (400*400)
• Go through all the vertexes of mesh->verts and if they are in the grid square, copy them to the temporary array.
• Pick a new grid square, and copy the temporary array to the old grid square:

Code:
grid_list[grid_square].arr[] //is the grid squares vertex list
grid_list[grid_square].size i//s the size of the list.

• When all the grid squares are done, exit the function.

Here is the code:
(There are a few unused variables)

Code:
void setupLvl(Mesh * mesh)
{
    float x1;
    float y1;
    float z1;
    
    int cellx;
    int celly;
    
    int csizetemp=0;
    
    int temp_array[640000];
    
    int x;
    
    int cellhash;
    
    int counterx = 0;
    int countery = 0;
    
    int vhash;
    
    int index;
    
    cellhash = hash_func(counterx,countery,400);
    // formula is (int)(x/cellsize) *cellsize 10*
    
    while (counterx <= 400 && countery <= 400)
    {
        if (  (index/3) <  ((sizeof(mesh->verts)/sizeof(int))/3)    ) // if this is the last vertex....
        {
            if (counterx > 400) // move the box
            {
                printf("moving box...");
                counterx = 0;
                countery++;
            }
            else
                counterx++;
            
            grid_list[cellhash].arr = malloc(sizeof(int)*csizetemp); // set the size of the proper array...
            grid_list[cellhash].size = csizetemp; // ^^
            
            x = 0;
            
            while (x < csizetemp) // copy the elements of the temporary array to the proper array...
            {
                printf("grid_list has changed...");
                grid_list[cellhash].arr[x] = temp_array[x];
                x++;
            }
    
            csizetemp = 0;
            memset(temp_array,0,640000); // reset temp_array
            
            cellhash = hash_func(counterx,countery,400);
        }
            
            
        printf("picking vertex number %i out of %i\n", index/3,(int) ((sizeof(mesh->verts)/sizeof(int))/3) );
        
        x1 = mesh->verts[index];
        y1 = mesh->verts[index+1];
        z1 =  mesh->verts[index+2];
    
    
        printf("x1 = %f",x1);
        cellx = (int)(x1/10);
        celly = (int)(z1/10);
        
        vhash = hash_func(cellx,celly,400);
        
        if (vhash == cellhash)
            
        {
            temp_array[csizetemp] = index;
            csizetemp++;
            index += 3;
        }

    }

}
And the Hash function:
Code:
int hash_func( x,  y,  n)
{
    return (x*2185031351ul ^ y*4232417593ul) % n;
}

So, the problem is, I get this when it runs:
Code:
Preparing to sort map verts...picking vertex number -357914788 out of 0
x1 = -0.000000picking vertex number -357914787 out of 0
x1 = 0.000000picking vertex number -357914786 out of 0
x1 = 0.000000picking vertex number -357914785 out of 0
x1 = 0.000000picking vertex number -357914784 out of 0
// etc, etc


This shows that the mesh->verts is NULL!
But, if I do this in the function call:

Code:
printf("Preparing to sort map verts...");
        if (Lvl1 == NULL)
        {
            printf("Lvl1 is null");
        }
        setupLvl(Lvl1);
        printf("Finished sorting map verts...");

Nothing is printed! Lvl1 is not NULL!

Please help,

Thankyou

~ Bring a Pen ~
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #2
Two things - are you sure mesh is being allocated correctly? It is possible to call malloc(0), it the spec doesn't say what should happen if you do, so you might get a weird pointer back.
And secondly, it might help if you initialised index.
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #3
Hmm. So, I enter
Code:
int index = 0;
.
But now it says
Code:
[b]Preparing to sort map verts...
picking vertex number 0 out of 0
x1 = -90.794769
picking vertex number 0 out of 0
x1 = -90.794769
[/b]

The x coordinate is a vertex of the OBJ (now mesh->verts). This proves it mesh->verts is actually something! But the line:

Code:
    printf("picking vertex number %i out of %i\n", index/3,(int) ((sizeof(mesh->verts)/sizeof(int))/3) );

Should return something.

~ Bring a Pen ~
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #4
OK - got it. You're sizeofing the wrong thing. (I suspect "sizeofing" probably isn't a real word Sneaky ).
Code:
sizeof(mesh->verts);            //this returns the size a pointer, which is 8 bytes
((sizeof(mesh->verts)/sizeof(int))/3);    //that means this returns (8/4)/3 which is less than 1, so it rounds down to 0

//You want to do this
sizeof( *(mesh->verts) );        //this should return the size of the array in bytes

That loop looks a little odd though. It looks like it can get to the end without incrementing index, which would cause it to investigate the same vertex repeatedly.
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #5
Well, thanks for the help so far, but it still isn't working.

Code:
void setupLvl(Mesh * mesh)
{
    float x1;
    float y1;
    float z1;
    
    int cellx;
    int celly;
    
    int csizetemp=0;
    
    int temp_array[640000];
    
    int x;
    
    int cellhash;
    
    int counterx = 0;
    int countery = 0;
    
    int vhash;
    
    int indexref = 0;
    
    cellhash = hash_func(counterx,countery,400);
    // formula is (int)(x/cellsize) *cellsize 10*
    
    while (counterx <= 400 && countery <= 400)
    {

    //(index/3) <  ((sizeof(*(mesh->verts))/sizeof(int))/3)
    
        if ((indexref/3) <  ((sizeof(*(mesh->verts))/sizeof(int))/3)) // if this is the last vertex....
        {
            if (counterx > 400) // move the box
            {
                printf("moving box...");
                counterx = 0;
                countery++;
            }
            else
                counterx++;
            
            grid_list[cellhash].arr = malloc(sizeof(int)*csizetemp); // set the size of the proper array...
            grid_list[cellhash].size = csizetemp; // ^^
            
            x = 0;
            
            while (x < csizetemp) // copy the elements of the temporary array to the proper array...
            {
                printf("grid_list has changed...");
                grid_list[cellhash].arr[x] = temp_array[x];
                x++;
            }
            
            
                
            csizetemp = 0;
            memset(temp_array,0,640000); // reset temp_array
            
            cellhash = hash_func(counterx,countery,400);
        }
        else
        {
            
            
    

        printf("picking vertex number %i out of %i\n", indexref/3,(int) (sizeof(*(mesh->verts))) );
        
        x1 = mesh->verts[indexref];
        y1 = mesh->verts[indexref+1];
        z1 =  mesh->verts[indexref+2];
            printf("x1 = %f\n",x1);
        printf("y1 = %f\n",y1);
        printf("z1 = %f\n",z1);

        cellx = (int)(x1/10);
        celly = (int)(z1/10);

        vhash = hash_func(cellx,celly,400);
        
        if (vhash == cellhash)
            
        {
            printf("vhash is equal to cellhash...\n");
            temp_array[csizetemp] = indexref;
            printf("temp_array[%i] is set to %i\n",csizetemp,indexref);
            csizetemp++;
                    
        }
        
        indexref += 3;
        }
        
    }
    
}

I think I need a proper way to determine if index has reached the last vertex.
Also, just out of interest, I changed the sizeof line, and it tells me mesh->verts is 4 bytes in size! Is this right?

~ Bring a Pen ~
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #6
Quote:Also, just out of interest, I changed the sizeof line, and it tells me mesh->verts is 4 bytes in size! Is this right?
Yeah - I assume you compiled the code 32 bit (4 bytes for an address) and my test was 64 bit (8 bytes for an address). The effect of dividing it by 6 is much the same.

I think you should ramp up the logging here. Start printing out the results of all your calculations and comparisons so you can properly analyse what your code is doing. It is best if you also operate on a simple data set so you can easily compare what it does with what you want it to do. Where the logs don't match the expected results you have one or two lines of code to examine for the problem.
If you haven't already, try mapping out the flow of your algorithm on paper as well and going through it manually to see if you can spot any flaws in your current approach.
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #7
Hmm... Here's a flowchart:

[Image: v8pgs0.png]

~ Bring a Pen ~
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  squares[] and sorting vertices into arrays... mikey 3 3,075 Sep 11, 2009 11:21 AM
Last Post: mikey
  Depth Sorting algorithm Leroy 1 4,640 Jul 2, 2007 01:47 AM
Last Post: aegidian
  NSWindow frame message not returning correct values after moving the window Svarog 6 4,969 Jan 7, 2007 06:41 PM
Last Post: OneSadCookie
  Converting integer/numeric values to Strings vnvrymdreglage 5 3,759 Oct 23, 2006 07:18 PM
Last Post: vnvrymdreglage
  Mock Hard Drive Architecture Nick 6 5,096 Jun 16, 2005 09:49 PM
Last Post: Nick