sorting floats with index

Started by
20 comments, last by cozzie 11 years, 3 months ago

Hi,

I'm struggling with something which sounds fairly simple... although..

- I have a int array containing incices of meshes, say Meshes[5] = { 3, 1, 4, 9, 2};

- these indices point to objects (meshes) with a member 'distance' (float)

- say the meshes with indices above have: 10, 30, 40, 50, 15 as distance

What I want to achieve is the new index (int array) sorted on the descending order of the member floats..

I got so far to be able to sort the int array without the floats, and I got that far to sort the floats as an array (distances).

Simply don't know how to complete this goal...

Here's the code that partially does this.

Can someone help me out?


	float *dists = new float[mNrD3dMeshesBlended];
	for(oc=0;oc<mNrD3dMeshesBlended;++oc) dists[oc] = mD3dMeshes[mMeshIndexBlended[oc]].mDistToCam;

	float elements = sizeof(dists) / sizeof(dists[0]); 
	std::sort(dists, dists+mNrD3dMeshesBlended, std::greater<float>());

//	int elements = sizeof(mMeshIndexBlended) / sizeof(mMeshIndexBlended[0]); 
//	std::sort(mMeshIndexBlended, mMeshIndexBlended+mNrD3dMeshesBlended, std::greater<int>());

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

I'd create a struct that holds the original index and the distance. Add an operator > which compares the distance members. Populate a std::vector with instances of the struct for each mesh index/distance combination you need. Then use std::sort on the vector, and voila :-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I'd create a struct that holds the original index and the distance. Add an operator > which compares the distance members. Populate a std::vector with instances of the struct for each mesh index/distance combination you need. Then use std::sort on the vector, and voila :-)

As a bonus (slightly briefer) approach, you can use a std::pair<float, int> instead of the custom struct, and the default comparison operator will do the right thing (i.e. compare the float first).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Thanks for the quick reactions, I guess I'll have to dig into pairs/vectors (till now not used that much :))

Keeping in mind that this function will run each frame I render, is this the most efficient way to do this?

(looking at memory allocation for the pairs or structs)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Keeping in mind that this function will run each frame I render, is this the most efficient way to do this?
(looking at memory allocation for the pairs or structs)

As long as you keep the std::vector around between frames, there shouldn't be much allocation going on (only when the vector's length grows significantly). Your struct (or std::pair, for that matter) should be a very thin wrapper class, and created on the stack - no real cost to those allocations, and copying them into the vector should be similarly cheap.

Ideally, the sort itself should dominate the performance cost here. I presume you are not sorting a huge number of items?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

You can also use a custom predicate that sorts the indices by comparing the distance values at the corresponding indices.

The C++11 way:

std::sort(mMeshIndexBlended, mMeshIndexBlended+mNrD3dMeshesBlended, [&](int a, int b) {return dists[a]<dists;});

Or the pre-C++11 way:

struct pred {
pred(float *values) : values(values)
{}

bool operator()(int a, int b)
{ return values[a] < values; }

float *values;
}; ... std::sort(mMeshIndexBlended, mMeshIndexBlended+mNrD3dMeshesBlended, pred(dists));

Wow, that looks easy (although I have do dig in and read/ study it to understand:))
But it compiles directly, thanks :)

The result is not exacly as expected, but nearly there.
It's sorting but not yet as I'd expect.

Array before sorting:

Render: 1 ,dist to cam: 163.554
Render: 3 ,dist to cam: 136.565
Render: 5 ,dist to cam: 111.131
Render: 7 ,dist to cam: 88.6002
Render: 9 ,dist to cam: 71.7635
Render: 11 ,dist to cam: 159.844
Render: 13 ,dist to cam: 132.098
Render: 15 ,dist to cam: 105.594
Render: 17 ,dist to cam: 81.5475
Render: 19 ,dist to cam: 62.849
Render: 21 ,dist to cam: 154.11
Render: 23 ,dist to cam: 150.167
Render: 25 ,dist to cam: 152.151
Render: 27 ,dist to cam: 46.3681
Render: 29 ,dist to cam: 39.37

After sort:

Render: 27 ,dist to cam: 46.3681
Render: 29 ,dist to cam: 39.37
Render: 25 ,dist to cam: 152.151
Render: 15 ,dist to cam: 105.594
Render: 17 ,dist to cam: 81.5475
Render: 19 ,dist to cam: 62.849
Render: 21 ,dist to cam: 154.11
Render: 23 ,dist to cam: 150.167
Render: 1 ,dist to cam: 163.554
Render: 5 ,dist to cam: 111.131
Render: 13 ,dist to cam: 132.098
Render: 3 ,dist to cam: 136.565
Render: 9 ,dist to cam: 71.7635
Render: 11 ,dist to cam: 159.844
Render: 7 ,dist to cam: 88.6002

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

From your initial set of indices, I get the impression now that your indices are not sequential. In that case, my method won't work. It assumes that index n has the distance value dists[n], while it seems like you have a set of index/distance pairs (n, dist) with arbitrary n and dist, and that has to be sorted by dist. If that's the case, then the structure approach suggested earlier will work.

too bad, I'll have to get into that then.

Thanks for the help.

My gut feeling that there must be a easy way was not true then :)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Stick the distances and indices in a std::map<float, int> then, with the float as the key. Then when you iterate through the map you will get the indices back in order of increasing float key.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement