/// <summary> /// Calculates the pixel coordinates from a set of Tvg coordinates and the resolution of the image /// </summary> /// <returns>The rect.</returns> /// <param name="tvgBounds">Tvg bounds.</param> /// <param name="resx">Full X resoultion of image.</param> /// <param name="resy">Full Y resolution of image.</param> public int [] GetRect(float[] tvgBounds, int resx, int resy, ExportResult log = null) { float tvgCanvasWidth = TVGX2 - TVGX1; //5000 float tvgCanvasHeight = TVGY2 - TVGY1; //3750 //Offset the coordinates so they exist within a 0 -> DIMENSIONSIZE range. float [] offsetTvgBounds = new float [4]; offsetTvgBounds [0] = tvgBounds [0] - TVGX1; offsetTvgBounds [2] = tvgBounds [2] - TVGX1; offsetTvgBounds [1] = tvgBounds [1] - TVGY1; offsetTvgBounds [3] = tvgBounds [3] - TVGY1; //Now we determine the scaling factor (Currently for horizontal FOV fit, maybe update this later to allow both) bool horizontalFOV = true; float scale = horizontalFOV ? ((float)resx / tvgCanvasWidth) : ((float)resy / tvgCanvasHeight); int [] pixelRect = new int [4]; for (int i = 0; i < 4; i++) { pixelRect [i] = (int)Math.Round(offsetTvgBounds[i] * scale); } //In toonboom Up = positive so we need to swap adjust to account for this. pixelRect [1] = resy - pixelRect [1]; pixelRect [3] = resy - pixelRect [3]; int tmp = pixelRect [1]; pixelRect [1] = pixelRect [3]; pixelRect [3] = tmp; //Check for errors if (pixelRect [0] < 0) { if (log != null) { log.Warn("TVG bound exceeds image range! x1: {0}", pixelRect [0]); } pixelRect [0] = 0; } if (pixelRect [1] < 0) { if (log != null) { log.Warn("TVG bound exceeds image range! y1: {0}", pixelRect [1]); } pixelRect [1] = 0; } if (pixelRect [2] > resx) { if (log != null) { log.Warn("TVG bound exceeds image range! x2: {0}", pixelRect [2]); } pixelRect [2] = 0; } if (pixelRect [3] > resy) { if (log != null) { log.Warn("TVG bound exceeds image range! y2: {0}", pixelRect [3]); } pixelRect [3] = 0; } return(pixelRect); }