problems with resizing 2D array

Started by
3 comments, last by Dragon_Strike 17 years, 7 months ago
im having problems with resizing a 2d array... the result just isnt right... this is what ive got... and yea i got it form somewhere on the forum after several failed attempts.. but this still gave the same results...

#ifndef _Array_H_
#define _Array_H_

#include "math.h"

class CARRAY
{
	

	private:
		

	public:
		float m_values;
		int m_size;

		float Interpolate(float x, float z)
		{
			// linear interpolation
			float col;
			int base_x, base_z;
			float dx, dz, value;

			base_x = (int)floor(x);
			base_z = (int)floor(z);

			if(base_x < 0 || base_z < 0 || base_x+1 >= m_size || base_z+1 >= m_size)
				return(0.0f);

			dx = x - base_x;
			dz = z - base_z;

			col = ((1-dx) * (1-dz) * m_values[base_x+(m_size*(base_z))] +
				(1-dx) * (dz) * m_values[base_x+1+(m_size*(base_z))] +
				(dx) * (1-dz) * m_values[base_x+(m_size*(base_z+1))] +
				(dx) * (dz) * m_values[base_x+1+(m_size*(base_z+1))]);

			return col;
		}

		void Resize(int newsize)
		{
			float* temp = new float [newsize*newsize];

			int x, z;
			float x2, z2, dx, dz;
		    
			dx = m_size / newsize;
			dz = m_size / newsize;

			for(z=0, z2=0; z<newsize; z++, z2+=dz)
			{
				for(x=0, x2=0; x<newsize; x++, x2+=dx)
				{
					temp[x+(newsize*(z))] = Interpolate(x2, z2);
				}
			}

			delete m_values;
			m_size = newsize;
			m_values = new float [newsize*newsize];

			for (int z = 0; z < newsize; z++)
			{
				for (int x = 0; x < newsize; x++)
				{			
					m_values[x+(newsize*z)] = temp[x+(newsize*z)];
					
				}
			}
		}
};


Advertisement
Well I didn't go into the details. You mean like that your interpolation isn't working well ?

Well, there is another deadly serious bug in your code which is mixing of "new[]" with "delete". That will kill your program eventually.

Another thing, when you allocate the temp array, you don't need to create another array "m_values = new float [newsize*newsize];" and then copy the temp array. Why not just assign "m_values = temp", now m_values points to the right memory area.


In the case where the array gets resized smaller, the interpolation code won't work well. Perhaps you could show what kind of problems you are facing with it?
You interpolation code looks more like bilinear.

//The interpolation could be something like this too.temp1 = array[x,y] + (array[x+1,y] - array[x,y]) * dx;temp2 = array[x,y+1] + (array[x+1,y+1] - array[x,y+1]) * dx;col = temp1 + (temp2 - temp1) * dz;
i will look into it more tomorow...

u mean i should use delete[] instead of just delete?

the problem is that the result becmoes a single (same) value at all positions...? this happens when i try to resize to a larger size..

when i use this function with the same size i get some wierd diagonal artifact and stretching... ill post an image if u want..

[boost]::multi_array is all you need for a multi-dimensional, resizable yet rectangular array.

That said, I'm not even sure what your functions are trying to do. :/ If I put in the time to refactor stuff (BTW, you made a mistake already on the #ifndef line; names starting with an underscore followed by a capital letter are reserved for the implementation), I might be able to work it out, but seriously, it's not worth the effort. Don't reinvent the wheel. Implement your "interpolation" on top of a boost::multi_array, and use its provided resizing functionality.
welll i would rather not use library since i want to learn this stuff first... "first learn the basics and then u can skip them"

this is what ive got now... it seems to work when down sampling but when i upsample all i get is a single value...
		float Interpolate(float a, float b, float x)		{			float f  = ( 1.0f - (float)cos ( x * PI ) ) * 0.5f;			return ( a * ( 1.0f - f ) + b * f );		}		void Resize(int newsize)		{			float* temp = new float [newsize*newsize];			float r = m_size / newsize;			for(int z=0; z<newsize; z++)			{				for(int x=0; x<newsize; x++)				{					int bx = int(x*r);					int bz = int(z*r);					float fx = x*r - bx; 					float fz = z*r - bz; 					float v1 = GetValue(bx, bz);					float v2 = GetValue(bx + 1, bz);					float v3 = GetValue(bx, bz + 1);					float v4 = GetValue(bx + 1, bz + 1);					float i1 = Interpolate(v1, v2, fx);					float i2 = Interpolate(v3, v4, fx);					float i3 = Interpolate(i1, i2, fz);					temp[x+(newsize*(z))] =  i3;				}			}			delete[] m_values;			m_values = temp;			m_size = newsize;		}


[Edited by - Dragon_Strike on September 9, 2006 3:16:09 PM]

This topic is closed to new replies.

Advertisement