SBSDKPolygon

@interface SBSDKPolygon : NSObject <NSCopying>

This class represents a 4-gon, a polygon with 4 edges. The polygons points are stored in a normalized unit coordinate system from {0, 0} to {1, 1}. To convert to absolute coordinates use the methods that take a size and return absolute values. An absolute space can be any 2-dimensional space with a size, e.g. an image.

  • When polygon was detected: contains the percentual width of the receiver in the image.

    Declaration

    Objective-C

    @property (nonatomic) double widthPercentage;

    Swift

    var widthPercentage: Double { get set }
  • When polygon was detected: contains the percentual height of the receiver in the image.

    Declaration

    Objective-C

    @property (nonatomic) double heightPercentage;

    Swift

    var heightPercentage: Double { get set }
  • When polygon was detected: contains the detection score of the receiver.

    Declaration

    Objective-C

    @property (nonatomic) double detectionScore;

    Swift

    var detectionScore: Double { get set }
  • When polygon was detected: contains the detection status of the receiver.

    Declaration

    Objective-C

    @property (nonatomic) SBSDKDocumentDetectionStatus detectionStatus;

    Swift

    var detectionStatus: SBSDKDocumentDetectionStatus { get set }
  • One of the desinated initializer methods.

    Declaration

    Objective-C

    - (instancetype)initWithNormalizedPointA:(CGPoint)a
                                      pointB:(CGPoint)b
                                      pointC:(CGPoint)c
                                      pointD:(CGPoint)d;

    Swift

    init!(normalizedPointA a: CGPoint, pointB b: CGPoint, pointC c: CGPoint, pointD d: CGPoint)

    Parameters

    a

    The first point of the polygon in unit coordinates.

    b

    The second point of the polygon in unit coordinates.

    c

    The third point of the polygon in unit coordinates.

    d

    The fourth point of the polygon in unit coordinates.

    Return Value

    A new instance of SBSDKPolygon class.

  • One of the desinated initializer methods.

    Declaration

    Objective-C

    - (instancetype)initWithNormalizedRect:(CGRect)rect;

    Swift

    init!(normalizedRect rect: CGRect)

    Parameters

    rect

    A rectangle in normalized unit space coordinates forming the new polygon.

    Return Value

    A new instance of SBSDKPolygon class.

  • One of the desinated initializer methods.

    Declaration

    Objective-C

    - (instancetype)initWithAbsolutePointA:(CGPoint)a
                                    pointB:(CGPoint)b
                                    pointC:(CGPoint)c
                                    pointD:(CGPoint)d
                                   forSize:(CGSize)size;

    Swift

    init!(absolutePointA a: CGPoint, pointB b: CGPoint, pointC c: CGPoint, pointD d: CGPoint, for size: CGSize)

    Parameters

    a

    The first point of the polygon in absolute coordinates.

    b

    The second point of the polygon in absolute coordinates.

    c

    The third point of the polygon in absolute coordinates.

    d

    The fourth point of the polygon in absolute coordinates.

    size

    The size to use to convert the points to unit coordinates.

    Return Value

    A new instance of SBSDKPolygon class.

  • One of the desinated initializer methods.

    Declaration

    Objective-C

    - (instancetype)initWithNormalizedDoubleValues:(NSArray<NSNumber *> *)values;

    Swift

    init!(normalizedDoubleValues values: [NSNumber]!)

    Parameters

    values

    An array of 8 NSNumber double values containing the normalized coordinates in the form [x1, y1, x2, y2, x3, y3, x4, y4].

  • Compares the receiver to another instance of SBSDKPolygon.

    Declaration

    Objective-C

    - (BOOL)isEqualToPolygon:(SBSDKPolygon *)polygon;

    Swift

    func isEqual(to polygon: SBSDKPolygon!) -> Bool

    Parameters

    polygon

    The polygon instance the receiver is compared to.

    Return Value

    YES, if the points of polygon are equal to the points of the receiver, NO otherwise.

  • Compares the receiver to another instance of SBSDKPolygon.

    Declaration

    Objective-C

    - (BOOL)isSimilarToPolygon:(SBSDKPolygon *)polygon;

    Swift

    func isSimilar(to polygon: SBSDKPolygon!) -> Bool

    Parameters

    polygon

    The polygon instance the receiver is compared to.

    Return Value

    YES, if the points of polygon are similar to the points of the receiver, NO otherwise. Similar here means the distance of the normalized corresponding points is less than 0.023.

  • Calculates the standard deviation in the corresponding points of the receiver and the given SBSDKPolygon.

    Declaration

    Objective-C

    - (double)standardDeviationToPolygon:(SBSDKPolygon *)polygon;

    Swift

    func standardDeviation(to polygon: SBSDKPolygon!) -> Double

    Parameters

    polygon

    The polygon instance the receiver is compared to.

    Return Value

    Standard deviation.

  • Clips the receiver the unit space {0, 0} - {1, 1}.

    Declaration

    Objective-C

    - (void)normalize;

    Swift

    func normalize()
  • Orders the receivers points in clockwise order. The first point is the point right next to 12 o'clock. This method uses the atan2 operator to sort the points clockwise.

    Declaration

    Objective-C

    - (void)sortClockwise;

    Swift

    func sortClockwise()
  • Helper method to transform a normalized point to an absolute coordinate system with given size.

    Declaration

    Objective-C

    + (CGPoint)normalizedPointFromAbsolutePoint:(CGPoint)point
                                       withSize:(CGSize)size;

    Swift

    class func normalizedPoint(fromAbsolutePoint point: CGPoint, with size: CGSize) -> CGPoint

    Parameters

    point

    The absolute point to convert into normalized space.

    size

    The size of the absolute coordinate system.

    Return Value

    The normalized point.

  • Helper method to transform an absolute point to the normalized unit coordinate system with given size.

    Declaration

    Objective-C

    + (CGPoint)absolutePointFromNormalizedPoint:(CGPoint)point
                                       withSize:(CGSize)size;

    Swift

    class func absolutePoint(fromNormalizedPoint point: CGPoint, with size: CGSize) -> CGPoint

    Parameters

    point

    The normalized point to convert into absolute space.

    size

    The size of the absolute coordinate system.

    Return Value

    The converted point.

  • Returns the normalized point at the given index.

    Declaration

    Objective-C

    - (CGPoint)normalizedPointWithIndex:(NSUInteger)index;

    Swift

    func normalizedPoint(with index: UInt) -> CGPoint

    Parameters

    index

    The index of the point in the receiver. Must be in range {0-3}.

    Return Value

    The normalized point.

  • Returns the absolute point at the given index.

    Declaration

    Objective-C

    - (CGPoint)absolutePointWithIndex:(NSUInteger)index forSize:(CGSize)size;

    Swift

    func absolutePoint(with index: UInt, for size: CGSize) -> CGPoint

    Parameters

    index

    The index of the point in the receiver. Must be in range {0-3}.

    size

    The size of the absolute coordinate system to transform the point to.

    Return Value

    The converted point.

  • Returns the normalized edge at the given index. The edge at index n is built from points at indices n and (n+1) % 4.

    Declaration

    Objective-C

    - (SBSDKPolygonEdge *)normalizedEdgeWithIndex:(NSUInteger)index;

    Swift

    func normalizedEdge(with index: UInt) -> SBSDKPolygonEdge!

    Parameters

    index

    The index of the edge in the receiver. Must be in range {0-3}.

    Return Value

    The normalized edge.

  • Returns the absolute edge at the given index. The edge at index n is built from points at indices n and (n+1) % 4.

    Declaration

    Objective-C

    - (SBSDKPolygonEdge *)absoluteEdgeWithIndex:(NSUInteger)index
                                        forSize:(CGSize)size;

    Swift

    func absoluteEdge(with index: UInt, for size: CGSize) -> SBSDKPolygonEdge!

    Parameters

    index

    The index of the edge in the receiver. Must be in range {0-3}.

    size

    The size of the absolute coordinate system to transform the edge to.

    Return Value

    The absolute edge.

  • Returns the normalized points as an array of 8 double value NSNumbers in the form [x1, y1, x2, y2, x3, y3, x4, y4].

    Declaration

    Objective-C

    - (NSArray<NSNumber *> *)normalizedDoubleValues;

    Swift

    func normalizedDoubleValues() -> [NSNumber]!
  • Sets the receivers point at the given index to the value of point.

    Declaration

    Objective-C

    - (void)setNormalizedPoint:(CGPoint)point forIndex:(NSUInteger)index;

    Swift

    func setNormalizedPoint(_ point: CGPoint, for index: UInt)

    Parameters

    point

    The normalized point.

    index

    The index of the point the set within the receiver in range {0-3}.

  • Sets the receivers point at the given index to the normalized value of point.

    Declaration

    Objective-C

    - (void)setAbsolutePoint:(CGPoint)point
                    withSize:(CGSize)size
                    forIndex:(NSUInteger)index;

    Swift

    func setAbsolutePoint(_ point: CGPoint, with size: CGSize, for index: UInt)

    Parameters

    point

    The absolute point.

    size

    The size of the absolute coordinate system.

    index

    The index of the point the set within the receiver in range {0-3}.

  • Rotates the points of the receiver 90 degrees clockwise, transforming it from one absolute space to another.

    Declaration

    Objective-C

    - (void)rotate90CWWithOldSize:(CGSize)oldSize newSize:(CGSize)newSize;

    Swift

    func rotate90CW(withOldSize oldSize: CGSize, newSize: CGSize)

    Parameters

    oldSize

    The size of the absolute coordinate system before rotating it.

    newSize

    The size of the absolute coordinate system after rotating it.

  • Rotates the points of the receiver 90 degrees counterclockwise, transforming it from one absolute space to another.

    Declaration

    Objective-C

    - (void)rotate90CCWWithOldSize:(CGSize)oldSize newSize:(CGSize)newSize;

    Swift

    func rotate90CCW(withOldSize oldSize: CGSize, newSize: CGSize)

    Parameters

    oldSize

    The size of the absolute coordinate system before rotating it.

    newSize

    The size of the absolute coordinate system after rotating it.

  • Rotates the points of the receiver 180 degrees, transforming it from one absolute space to another.

    Declaration

    Objective-C

    - (void)rotate180WithOldSize:(CGSize)oldSize newSize:(CGSize)newSize;

    Swift

    func rotate180(withOldSize oldSize: CGSize, newSize: CGSize)

    Parameters

    oldSize

    The size of the absolute coordinate system before rotating it.

    newSize

    The size of the absolute coordinate system after rotating it.

  • Performs times counter-clockwise rotations on the receiver.

    Declaration

    Objective-C

    - (void)rotateCCW:(NSUInteger)times withSize:(CGSize)size;

    Swift

    func rotateCCW(_ times: UInt, with size: CGSize)

    Parameters

    size

    The size of the absolute coordinate system before rotating it.

  • Performs times clockwise rotations on the receiver.

    Declaration

    Objective-C

    - (void)rotateCW:(NSUInteger)times withSize:(CGSize)size;

    Swift

    func rotateCW(_ times: UInt, with size: CGSize)

    Parameters

    size

    The size of the absolute coordinate system before rotating it.

  • Performs a horizontal flip of the polygon points.

    Declaration

    Objective-C

    - (void)flipHorizontally:(CGSize)size;

    Swift

    func flipHorizontally(_ size: CGSize)

    Parameters

    size

    The size of the absolute coordinate system to transform the points to.

  • Returns the axis-oriented bounding box of the receiver.

    Declaration

    Objective-C

    - (CGRect)boundingBoxWithSize:(CGSize)size;

    Swift

    func boundingBox(with size: CGSize) -> CGRect

    Parameters

    size

    The absolute size the polygon is scaled to.

    Return Value

    The bounding box of the polygon in sbolute image space coordinates.

  • Calculates the geometric center (mass point) of the polygon in normalized unit coordinates.

    Declaration

    Objective-C

    - (CGPoint)center;

    Swift

    func center() -> CGPoint
  • Calculates the estimated size of the perspective-corrected and cropped image when applying the receiver to an image with given size.

    Declaration

    Objective-C

    - (CGSize)sizeWhenApplyingToImageOfSize:(CGSize)size
                                 imageScale:(double)imageScale
                                focalLength:(double)focalLength
                                sensorWidth:(double)sensorWidth;

    Swift

    func sizeWhenApplying(toImageOf size: CGSize, imageScale: Double, focalLength: Double, sensorWidth: Double) -> CGSize

    Parameters

    size

    The size of the original image the receiver should be applied to.

    imageScale

    The scaling factor of the image.

    focalLength

    The focal length of the lens the image was taken with in millimeters.

    sensorWidth

    The width of the sensor the image was taken with in millimeters.

    Return Value

    The estimated size of the cropped and perspective-corrected image, when the receiver was applied.

  • One of the desinated initializer methods.

    Declaration

    Objective-C

    - (instancetype)initWithMetadataObject:
        (AVMetadataMachineReadableCodeObject *)codeObject;

    Swift

    init!(metadataObject codeObject: AVMetadataMachineReadableCodeObject!)

    Parameters

    codeObject

    An AVMetadataMachineReadableCodeObject as returned from QR code detection.

  • Creates a UIBezierPath instance of the receiver for drawing purposes.

    Declaration

    Objective-C

    - (UIBezierPath *)bezierPathForSize:(CGSize)size;

    Swift

    func bezierPath(for size: CGSize) -> UIBezierPath!

    Parameters

    size

    The size to use to convert the normalized points of the receiver to absolute coordinates.

    Return Value

    An UIBezierPath instance representing the polygon converted to absolute space.

  • Creates a UIBezierPath instance of the receiver for drawing purposes.

    Declaration

    Objective-C

    - (UIBezierPath *)bezierPathForSize:(CGSize)size
                           cornerRadius:(CGFloat)cornerRadius
                          withTransform:(CGAffineTransform)transform;

    Swift

    func bezierPath(for size: CGSize, cornerRadius: CGFloat, with transform: CGAffineTransform) -> UIBezierPath!

    Parameters

    size

    The size to use to convert the normalized points of the receiver to absolute coordinates.

    cornerRadius

    The corner radius of the bezier path.

    transform

    The transform to apply to the bezier path;

    Return Value

    An UIBezierPath instance representing the polygon converted to absolute space.

  • Creates a UIBezierPath instance of the receiver for drawing purposes.

    Declaration

    Objective-C

    - (UIBezierPath *)bezierPathForPreviewLayer:
        (AVCaptureVideoPreviewLayer *)previewLayer;

    Swift

    func bezierPath(for previewLayer: AVCaptureVideoPreviewLayer!) -> UIBezierPath!

    Parameters

    previewLayer

    The preview layer the path is going to be presented on.

    Return Value

    An UIBezierPath instance representing the polygon.

  • Creates a UIBezierPath instance of the receiver for drawing purposes.

    Declaration

    Objective-C

    - (UIBezierPath *)bezierPathWithCornerRadius:(CGFloat)cornerRadius
                                 forPreviewLayer:
                                     (AVCaptureVideoPreviewLayer *)previewLayer
                                     orientation:(UIDeviceOrientation)orientation
                                   isFrontCamera:(BOOL)isFrontCamera;

    Swift

    func bezierPath(withCornerRadius cornerRadius: CGFloat, for previewLayer: AVCaptureVideoPreviewLayer!, orientation: UIDeviceOrientation, isFrontCamera: Bool) -> UIBezierPath!

    Parameters

    cornerRadius

    The radius to use when drawing rounded corners.

    previewLayer

    The preview layer the path is going to be presented on.

    orientation

    Is currently used camera - front camera. Used to calculate starting point.

    orientation

    Is currently used camera - front camera. Used to calculate starting point.

    Return Value

    An UIBezierPath instance representing the polygon.