internal Rectangle(int x, int y, int w, int h) { X = x; Y = y; W = w; H = h; }
internal Rectangle(double x, double y, double w, double h) { X = (FixedPoint)x; Y = (FixedPoint)y; W = (FixedPoint)w; H = (FixedPoint)h; }
private static int GetIndex(FixedPoint center, FixedPoint winSz, FixedPoint offset) { //Find ideal coordinate var coord = center - winSz / 2; //Snap to grid return((coord / offset).Round()); }
private FixedPoint GetPixelValue(int x, int y, FixedPoint zoom, FixedPoint xBase, FixedPoint yBase) { var xloc = xBase + x * zoom - 1; var yloc = yBase + y * zoom - 1; var topLeft = Interpolate(xloc, yloc); var lowLeft = Interpolate(xloc, yloc + zoom); var topRight = Interpolate(xloc + zoom, yloc); var lowRight = Interpolate(xloc + zoom, yloc + zoom); return lowRight - topRight - lowLeft + topLeft; }
//Note that this is the opposite of normal bounds checking: the region between lowBound and highBound //must fit entirely within the region from location to location+width. private static bool CheckContains(FixedPoint location, FixedPoint winSz, FixedPoint lowBound, FixedPoint highBound) { if (location > lowBound) { return(false); } if (location + winSz < highBound) { return(false); } return(true); }
//private FixedPoint WinSize(int size) //{ // var sz = WinSizes().GetEnumerator(); // for (var i = 0; i < size; i++) // { // sz.MoveNext(); // } // return sz.Current; //} //C# doesn't allow to set floating point modes, but we require absolute repeatability... private IEnumerable <FixedPoint> WinSizes() { FixedPoint sz = _szMin; while (sz < Math.Min(_w, _h)) { yield return(sz); var multiplier = (1 << _szExp) + 1; //int divisor = 1 << stepExp; sz = (sz * multiplier) >> _szExp; } }
private int GetNumWindows(FixedPoint winSz, int length) { //double availableWidth = (length - winSz); //double offset = (winSz / Math.Pow(2, offsetExp)); //return checked((int)Math.Floor(availableWidth / offset) + 1); if (winSz < 0) { throw new ArgumentOutOfRangeException("winSz"); } if (length < winSz) { throw new ArgumentOutOfRangeException("length"); } return(((length / winSz - 1) << _offsetExp).Floor() + 1); }
internal static Rectangle Rectangle(this Canvas canvas, Rectangle rect) { if (canvas.Background is ImageBrush && ((ImageBrush)canvas.Background).ImageSource is BitmapImage) { var image = (BitmapImage)((ImageBrush)canvas.Background).ImageSource; rect.X = FixedPoint.Min(FixedPoint.Max(rect.X, 0), image.PixelWidth - 1); rect.W = FixedPoint.Min(rect.W, image.PixelWidth - rect.X - 1); rect.Y = FixedPoint.Min(FixedPoint.Max(rect.Y, 0), image.PixelHeight - 1); rect.H = FixedPoint.Min(rect.H, image.PixelHeight - rect.Y - 1); rect.H = rect.W = FixedPoint.Min(rect.W, rect.H); } else { rect.X = FixedPoint.Max(rect.X, 0); rect.Y = FixedPoint.Max(rect.Y, 0); rect.H = rect.W = FixedPoint.Min(rect.H, rect.W); } return(rect); }
private FixedPoint Interpolate(FixedPoint x, FixedPoint y) { var wholeX = x.Floor(); var wholeY = y.Floor(); var fracX = x - wholeX; var fracY = y - wholeY; /* Key: * +++++^ +: sum * +++++^ ^: rt * +++++^ ~: lo * ~~~~~& &: lr * */ var sum = InImage(wholeX, wholeY) ? _integralImage[wholeX, wholeY] : 0; var rt = InImage(wholeX, wholeY + 1) ? _integralImage[wholeX, wholeY + 1] - sum : 0; var lo = InImage(wholeX + 1, wholeY) ? _integralImage[wholeX + 1, wholeY] - sum : 0; var lr = InImage(wholeX + 1, wholeY + 1) ? _integralImage[wholeX + 1, wholeY + 1] - (sum + rt + lo) : 0; return sum + rt * fracX + lo * fracY + lr * fracX * fracY; }
private int GetNumWindows(FixedPoint winSz) { return GetNumWindows(winSz, _w) * GetNumWindows(winSz, _h); }
private FixedPoint GetSurroundingPixVal(int n, int x, int y, FixedPoint zoom, FixedPoint xBase, FixedPoint yBase) { return GetPixelValue(x + Configuration.XOffsets[n], y + Configuration.YOffsets[n], zoom, xBase, yBase); }
public static FixedPoint Max(FixedPoint left, FixedPoint right) { return(left > right ? left : right); }
public static FixedPoint Max(FixedPoint left, FixedPoint right) { return left > right ? left : right; }
public static FixedPoint Min(FixedPoint left, FixedPoint right) { return left > right ? right : left; }
private int GetNumWindows(FixedPoint winSz) { return(GetNumWindows(winSz, _w) * GetNumWindows(winSz, _h)); }
public static FixedPoint Min(FixedPoint left, FixedPoint right) { return(left > right ? right : left); }
public void OperatorMinusTest(FixedPoint number, FixedPoint subtrahend) { Assert.AreEqual(4 - (FixedPoint)3, 1); }
public void OperatorLShiftTest(FixedPoint number, int shift) { Assert.AreEqual((FixedPoint)4 << 3, 4*8); }
private int GetNumWindows(FixedPoint winSz, int length) { //double availableWidth = (length - winSz); //double offset = (winSz / Math.Pow(2, offsetExp)); //return checked((int)Math.Floor(availableWidth / offset) + 1); if (winSz < 0) { throw new ArgumentOutOfRangeException("winSz"); } if (length < winSz) { throw new ArgumentOutOfRangeException("length"); } return ((length / winSz - 1) << _offsetExp).Floor() + 1; }
//Note that this is the opposite of normal bounds checking: the region between lowBound and highBound //must fit entirely within the region from location to location+width. private static bool CheckContains(FixedPoint location, FixedPoint winSz, FixedPoint lowBound, FixedPoint highBound) { if (location > lowBound) return false; if (location + winSz < highBound) return false; return true; }
private static int GetIndex(FixedPoint center, FixedPoint winSz, FixedPoint offset) { //Find ideal coordinate var coord = center - winSz / 2; //Snap to grid return (coord / offset).Round(); }