Example #1
0
void MaskedImage::maskedInterpolate(const UArray<double> & xs, const UArray<double> & ys, UArray<double> & outValues, UArray<bool> & outMask) const
{
	U_ASSERT((xs.getSize() == ys.getSize()) && (xs.getSize() == outValues.getSize()) && (xs.getSize() == outMask.getSize()));

	static double cubicSplineMatrix[4*4] = 
		{0, 1, 0, 0,
		-0.5, 0, 0.5, 0,
		1, -2.5, 2, -0.5,
		-0.5, 1.5, -1.5, 0.5};

	double xPowers[4];
	double yPowers[4];
	xPowers[0] = 1.0;
	yPowers[0] = 1.0;

	for(SInt32 dataIndex = 0; dataIndex < xs.getSize(); dataIndex++)
	{
		double x = (xs[dataIndex]- getXLower())/getDeltaX();
		double y = (ys[dataIndex]- getYLower())/getDeltaY();
		SInt32 xIndex = (SInt32)x;
		SInt32 yIndex = (SInt32)y;

		// if the index is too close to the edge, extrapolate from the nearest location inbounds
		if((xIndex < 1) || (xIndex > getXSize()-3)
			|| (yIndex < 1) || (yIndex > getYSize()-3))
		{
			outMask[dataIndex] = false;
			continue;
		}

		outMask[dataIndex] = true;
		for(SInt32 j = 0; j < 4; j++)
		{
			for(SInt32 i = 0; i < 4; i++)
			{
				outMask[dataIndex] = outMask[dataIndex] && getMask(xIndex + i - 1,yIndex + j - 1);
			}
		}

		if(!outMask[dataIndex])
			continue;

		x = x - (double)xIndex;
		y = y - (double)yIndex;

		xPowers[1] = x;
		xPowers[2] = x*x;
		xPowers[3] = x*x*x;
		yPowers[1] = y;
		yPowers[2] = y*y;
		yPowers[3] = y*y*y;

		double result = 0;
		for(SInt32 k = 0; k < 4; k++)
		{
			double localResult = 0;
			for(SInt32 j = 0; j < 4; j++)
				for(SInt32 i = 0; i < 4; i++)
					localResult = localResult + xPowers[i]*cubicSplineMatrix[j+4*i]*(*this)(xIndex + j - 1, yIndex + k - 1);
			for(SInt32 i = 0; i < 4; i++)
				result = result + yPowers[i]*cubicSplineMatrix[k+4*i]*localResult;
		}

		outValues[dataIndex] = result;
	}	
}
Example #2
0
void MaskedImage::maskedInterpolateAtOffsetPixels(double xLower, double yLower, SInt32 xCount, SInt32 yCount,
												  UArray<double> & outValues, UArray<bool> & outMask) const
{
	U_ASSERT((xCount*yCount == outValues.getSize()) && (xCount*yCount == outMask.getSize()));

	static double cubicSplineMatrix[4*4] = 
		{0, 1, 0, 0,
		-0.5, 0, 0.5, 0,
		1, -2.5, 2, -0.5,
		-0.5, 1.5, -1.5, 0.5};

	double xPowers[4];
	double yPowers[4];
	xPowers[0] = 1.0;
	yPowers[0] = 1.0;

	double x = (xLower - getXLower())/getDeltaX();
	double y = (yLower - getYLower())/getDeltaY();

	SInt32 xLowerIndex = (SInt32)x;
	SInt32 yLowerIndex = (SInt32)y;
	x = x - (double)xLowerIndex;
	y = y - (double)yLowerIndex;

	xPowers[1] = x;
	xPowers[2] = x*x;
	xPowers[3] = x*x*x;
	yPowers[1] = y;
	yPowers[2] = y*y;
	yPowers[3] = y*y*y;

	double coefficients[4*4];
	for(SInt32 i = 0; i < 16; i++)
		coefficients[i] = 0;
	for(SInt32 k = 0; k < 4; k++)
	{
		for(SInt32 j = 0; j < 4; j++)
		{
			double result = 0;
			for(SInt32 l = 0; l < 4; l++)
				for(SInt32 i = 0; i < 4; i++)
					result = result + xPowers[i]*cubicSplineMatrix[j+4*i]*yPowers[l]*cubicSplineMatrix[k+4*l];
			coefficients[j+4*k] = result;
		}
	}

	
	SInt32 dataIndex = 0;
	for(SInt32 yIndex = yLowerIndex; yIndex < yLowerIndex+yCount; yIndex++)
	{
		for(SInt32 xIndex = xLowerIndex; xIndex < xLowerIndex+xCount; xIndex++)
		{
			// if the index is too close to the edge
			if((xIndex < 1) || (xIndex > getXSize()-3)
				|| (yIndex < 1) || (yIndex > getYSize()-3))
			{
				outMask[dataIndex] = false;
				dataIndex++;
				continue;
			}

			outMask[dataIndex] = true;
			for(SInt32 j = 0; j < 4; j++)
			{
				for(SInt32 i = 0; i < 4; i++)
				{
					outMask[dataIndex] = outMask[dataIndex] && getMask(xIndex + i - 1,yIndex + j - 1);
				}
			}

			if(!outMask[dataIndex])
			{
				dataIndex++;
				continue;
			}

			double result = 0;
			for(SInt32 k = 0; k < 4; k++)
				for(SInt32 j = 0; j < 4; j++)
					result = result + coefficients[j+4*k]*(*this)(xIndex + j - 1, yIndex + k - 1);

			outValues[dataIndex] = result;
			dataIndex++;
		}
	}
}
int Accelerometer::getAbsDeltaY() {
	return abs(getDeltaY());
}