/// <summary> /// Create a ForcedRandomSeries with the given sample sequence and the seed as offset. /// The sequence is supposed to contain NxN points in random order. /// </summary> public ForcedRandomSeries(V2i[] series, int matrixSize, IRandomUniform rnd, bool jitter = true) { m_series = series; m_matrixSize = matrixSize; m_norm = 1.0 / m_matrixSize; // [0, 1) m_rnd = rnd; m_jitter = jitter; // random offset of sample pattern m_seed = new V2i(rnd.UniformInt(m_matrixSize), rnd.UniformInt(m_matrixSize)); }
/// <summary> /// Calculates the sum for a given set of V2is. /// </summary> public static V2i Sum(this V2i[] vectors) { V2i sum = V2i.Zero; for (var i = 0; i < vectors.Length; i++) { sum += vectors[i]; } return(sum); }
/// <summary> /// Calculates the sum for a given set of V2is. /// </summary> public static V2i Sum(this IEnumerable <V2i> vectors) { V2i sum = V2i.Zero; foreach (var e in vectors) { sum += e; } return(sum); }
private static PixImage LoadImage(int img) { var size = new V2i(IL.GetInteger(IntName.ImageWidth), IL.GetInteger(IntName.ImageHeight)); var dataType = IL.GetDataType(); var format = IL.GetFormat(); var channels = IL.GetInteger(IntName.ImageChannels); Type type; Col.Format fmt; // try to get format information if (!s_pixDataTypes.TryGetValue(dataType, out type)) { return(null); } if (!s_pixColorFormats.TryGetValue(format, out fmt)) { return(null); } // if the image has a lower-left origin flip it var mode = (OriginMode)IL.GetInteger((IntName)0x0603); if (mode == OriginMode.LowerLeft && !ILU.FlipImage()) { return(null); } // create an appropriate PixImage instance var imageType = typeof(PixImage <>).MakeGenericType(type); var pix = (PixImage)Activator.CreateInstance(imageType, fmt, size, channels); // copy the data to the PixImage var gc = GCHandle.Alloc(pix.Data, GCHandleType.Pinned); try { var ptr = IL.GetData(); ptr.CopyTo(pix.Data, 0, pix.Data.Length); } finally { gc.Free(); } // unbind and delete the DevIL image IL.BindImage(0); IL.DeleteImage(img); return(pix); }
/// <summary> /// Computes an undistorted Grid with (count.X + 1, count.Y + 1) vertices /// the coordinates are roughly in the range [0..imageSize.X, 0..imageSize.Y]. /// These can be used for a piecewise linear undistortion using the graphics /// card. If count is not given, it is set to iamgeSize / 8, for one grid /// point each 8 pixels. /// </summary> Matrix <V2f> ComputeUndistortedGrid(V2i imageSize, V2i count = default(V2i)) { if (count == default(V2i)) { count = imageSize / 8; } var grid = new Matrix <V2f>(count + V2i.II); var delta = imageSize.ToV2d() / count.ToV2d(); var self = this; grid.SetByCoord((x, y) => self.UndistortPixel(new V2d(x * delta.X, y * delta.Y), imageSize).ToV2f()); return(grid); }
/// <summary> /// UnDistortion of pixel coordinates. /// </summary> public V2d UndistortPixel(V2d p2, V2i imageSize) { var maxSize = Fun.Max(imageSize.X, imageSize.Y); var y = (p2.Y - PrincipalPoint.Y * imageSize.Y) / (FocalLength.Y * maxSize); var x = (p2.X - PrincipalPoint.X * imageSize.X - Skew * maxSize * y) / (FocalLength.X * maxSize); ComputeUndistortionParamsForCameraPoint(x, y, out double rd, out double tx, out double ty); double xd = x * rd + tx; double yd = y * rd + ty; return(new V2d( FocalLength.X * maxSize * xd + Skew * maxSize * yd + PrincipalPoint.X * imageSize.X, FocalLength.Y * maxSize * yd + PrincipalPoint.Y * imageSize.Y)); }
/// <summary> /// Gets info about a PixImage without loading the entire image into memory. /// </summary> /// <returns>null if the file info could not be loaded.</returns> public static PixImageInfo InfoFromFileNameDevil( string fileName, PixLoadOptions options) { lock (s_devilLock) { try { var img = IL.GenImage(); IL.BindImage(img); if (!IL.LoadImage(fileName)) { throw new ArgumentException("fileName"); } var dataType = IL.GetDataType(); var format = IL.GetFormat(); var width = IL.GetInteger(IntName.ImageWidth); var height = IL.GetInteger(IntName.ImageHeight); //var channels = IL.GetInteger(IntName.ImageChannels); IL.BindImage(0); IL.DeleteImage(img); Type type; Col.Format fmt; if (!s_pixDataTypes.TryGetValue(dataType, out type)) { return(null); } if (!s_pixColorFormats.TryGetValue(format, out fmt)) { return(null); } var size = new V2i(width, height); return(new PixImageInfo(new PixFormat(type, fmt), size)); } catch (Exception) { return(null); } } }
/// <summary> /// Pixel position relative to specified bounds. /// For example, a typical render window has bounds [(0, 0), (width, height)]. /// </summary> public PixelPosition(V2i position, Box2i bounds) { Position = position; Bounds = bounds; }
/// <summary> /// Convert from normalized image position [0,1][0,1] to already rounded pixel-center position. /// </summary> /// <param name="pos">The pixel location defined in pixel space: (0,0)=center of upper left pixel, (w-1,h-1)=center of lower right pixel.</param> /// <param name="imgSizeInPixel">The size of the image (as V2d to safe lots of conversions).</param> /// <returns>A normalized image position in [0, imgSizeInPixel.X-1][0, imgSizeInPixel.Y-1].</returns> public static V2i NormalizedImagePosToPixelCenterRound(V2d pos, V2i imgSizeInPixel) => (V2i)NormalizedImagePosToPixelCenter(pos, imgSizeInPixel).Copy(v => Round(v));
public static V2d PixelCenterToNormalizedImgPos(V2i pos, V2d imgSizeInPixel) => PixelCenterToNormalizedImgPos((V2d)pos, imgSizeInPixel);
/// <summary> /// V2i to System.Drawing.Point. /// </summary> public static Point ToPoint(this V2i p) => new Point(p.X, p.Y);
/// <summary> /// PixelPosition from normalized device coordinates. /// </summary> public PixelPosition(Ndc3d ndc, V2i renderTargetSize) : this(ndc, Box2i.FromSize(renderTargetSize)) { }
public static V2i OK50PixelCoordinatesFromWgs84(V2i wgs84) { return(OK50PixelCoordinatesFromWgs84(wgs84.X, wgs84.Y)); }
/// <summary> /// V2i to System.Drawing.Size. /// </summary> public static Size ToSize(this V2i v) => new Size(v.X, v.Y);
public static V2d PixelCenterToNormalizedImgPos(V2i pos, V2d imgSizeInPixel) { return(PixelCenterToNormalizedImgPos((V2d)pos, imgSizeInPixel)); }
/// <summary> /// Convert from normalized image position [0,1][0,1] to already rounded pixel-center position. /// </summary> /// <param name="pos">The pixel location defined in pixel space: (0,0)=center of upper left pixel, (w-1,h-1)=center of lower right pixel.</param> /// <param name="imgSizeInPixel">The size of the image (as V2d to safe lots of conversions).</param> /// <returns>A normalized image position in [0, imgSizeInPixel.X-1][0, imgSizeInPixel.Y-1].</returns> public static V2i NormalizedImagePosToPixelCenterRound(V2d pos, V2i imgSizeInPixel) { return((V2i)NormalizedImagePosToPixelCenter(pos, imgSizeInPixel).Copy(v => System.Math.Round(v))); }
/// <summary> /// Rasterizes an array of polygons into a matrix of given size. /// First polygon is rasterized with label 1, second polygon with label 2, and so on. /// </summary> /// <param name="polygons">Polygons to rasterize.</param> /// <param name="resolution">Resolution of result matrix.</param> /// <param name="bb"></param> public static Matrix <int> RasterizeAsLabels(this IEnumerable <Polygon2d> polygons, V2i resolution, Box2d bb) { var matrix = new Matrix <int>(resolution); var scale = (V2d)resolution / bb.Size; int i = 1; foreach (var poly in polygons) { RasterizePolygon(poly.ToPolygon2d(p => (p - bb.Min) * scale), matrix, i++); } return(matrix); }
/// <summary> /// Cell with min corner at index*2^exponent and dimension 2^exponent. /// </summary> public Cell2d(V2i index, int exponent) : this(index.X, index.Y, exponent) { }
public PixImageInfo(PixFormat format, V2i size) { Format = format; Size = size; }
public Border2i(V2i min, V2i max) { Min = min; Max = max; }
public static V2i OK50PixelCoordinatesFromWgs84(V2i wgs84) => OK50PixelCoordinatesFromWgs84(wgs84.X, wgs84.Y);
/// <summary> /// Convert from normalized image position [0,1][0,1] to pixel-center position /// (The inverse of toNormalizedImgPos.) /// </summary> /// <param name="pos">The pixel location defined in pixel space: (0,0)=center of upper left pixel, (w-1,h-1)=center of lower right pixel.</param> /// <param name="imgSizeInPixel">The size of the image (as V2d to safe lots of conversions).</param> /// <returns>A image position in [-0.5,imgSizeInPixel.X-0.5][-0.5,imgSizeInPixel.Y-0.5].</returns> public static V2d NormalizedImagePosToPixelCenter(V2d pos, V2i imgSizeInPixel) { return(new V2d(pos.X * imgSizeInPixel.X, pos.Y * imgSizeInPixel.Y) - V2dHalf); }
/// <summary> /// Pixel position relative to specified bounds. /// For example, a typical render window has bounds [(0, 0), (width, height)]. /// </summary> public PixelPosition(V2i position, int width, int height) { Position = position; Bounds = Box2i.FromSize(width, height); }
/// <summary> /// Iterates along Bresenham Discrete Line Raster from P0 to P1. /// Yields each integer position V2i. /// </summary> public static IEnumerable <V2i> BresenhamLineIterator(V2i p0, V2i p1) { int x0 = p0.X, y0 = p0.Y, x1 = p1.X, y1 = p1.Y; int dx, dy; int incx, incy; int balance; if (x1 >= x0) { dx = x1 - x0; incx = 1; } else { dx = x0 - x1; incx = -1; } if (y1 >= y0) { dy = y1 - y0; incy = 1; } else { dy = y0 - y1; incy = -1; } if (dx >= dy) { dy <<= 1; balance = dy - dx; dx <<= 1; while (x0 != x1) { yield return(new V2i(x0, y0)); if (balance >= 0) { y0 += incy; balance -= dx; } balance += dy; x0 += incx; } yield return(new V2i(x0, y0)); } else { dx <<= 1; balance = dx - dy; dy <<= 1; while (y0 != y1) { yield return(new V2i(x0, y0)); if (balance >= 0) { x0 += incx; balance -= dy; } balance += dx; y0 += incy; } yield return(new V2i(x0, y0)); } }
/// <summary> /// Pixel position relative to specified bounds. /// For example, a typical render window has bounds [(0, 0), (width, height)]. /// </summary> public PixelPosition(int x, int y, int width, int height) { Position = new V2i(x, y); Bounds = Box2i.FromSize(width, height); }
public static V2i Wgs84FromOK50PixelCoordinates(V2i pos) { return(new V2i( pos.X * 5 + 68500, pos.Y * 5 + 5104320)); }
/// <summary> /// PixelPosition from normalized device coordinates. /// </summary> public PixelPosition(Ndc2d ndc, Box2i renderTargetRegion) { Position = new V2i(ndc.TextureCoordinate * (V2d)renderTargetRegion.Size); Bounds = renderTargetRegion; }