| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Drawing; |
| | 7 | | using System.Linq; |
| | 8 | |
|
| | 9 | | namespace Azure.AI.FormRecognizer.Models |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// A sequence of four <see cref="PointF"/> representing a quadrilateral that outlines |
| | 13 | | /// the text of an element in a recognized form. Coordinates are specified relative to the |
| | 14 | | /// top-left of the original image, and points are ordered clockwise from the top-left corner |
| | 15 | | /// relative to the text orientation. Units are in pixels for images and inches for PDF. The |
| | 16 | | /// <see cref="LengthUnit"/> type of a recognized page can be found at <see cref="FormPage.Unit"/>. |
| | 17 | | /// </summary> |
| | 18 | | public readonly struct BoundingBox |
| | 19 | | { |
| | 20 | | private readonly PointF[] _points; |
| | 21 | |
|
| | 22 | | internal BoundingBox(IReadOnlyList<float> boundingBox) |
| | 23 | | { |
| 24840 | 24 | | if (boundingBox.Count == 0) |
| | 25 | | { |
| 4 | 26 | | _points = Array.Empty<PointF>(); |
| 4 | 27 | | return; |
| | 28 | | } |
| | 29 | |
|
| 24836 | 30 | | int count = boundingBox.Count / 2; |
| | 31 | |
|
| 24836 | 32 | | _points = new PointF[count]; |
| 248360 | 33 | | for (int i = 0; i < count; i++) |
| | 34 | | { |
| 99344 | 35 | | _points[i] = new PointF(boundingBox[2 * i], boundingBox[(2 * i) + 1]); |
| | 36 | | } |
| 24836 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// </summary> |
| 41848 | 41 | | internal PointF[] Points => _points ?? Array.Empty<PointF>(); |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Gets one of the points that set the limits of this <see cref="BoundingBox"/>. |
| | 45 | | /// Coordinates are specified relative to the top-left of the original image, and points |
| | 46 | | /// are ordered clockwise from the top-left corner relative to the text orientation. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="index">The 0-based index of the point to be retrieved.</param> |
| | 49 | | /// <returns>A <see cref="PointF"/> corresponding to the specified <paramref name="index"/>.</returns> |
| 4 | 50 | | public PointF this[int index] => Points[index]; |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Returns string representation for <see cref="BoundingBox"/>. |
| | 54 | | /// </summary> |
| 0 | 55 | | public override string ToString() => string.Join(",", Points.Select(p => p.ToString()).ToArray()); |
| | 56 | | } |
| | 57 | | } |