Class Contours

java.lang.Object
net.algart.contours.Contours

public final class Contours extends Object

Array of 2D contours.

Every contour is a sequence of 32-bit int values, describing N integer points (xk,yk). Every contours starts from the header, containing K 32-bit special values. In current version, K is 8 or 10, Format:

      MAGIC_WORD,        2*N+K,        (MAGIC_WORD = 2135109896 or 2135109898)
      minX,              maxX,
      minY,              maxY,
      RESERVED | FLAGS,  objectLabel,  (RESERVED = 2147418112)
      RESERVED,          frameID       (optional pair)
      x1,                y1,
      . . .
      xN,                yN

 

Here MAGIC_WORD contains (in low bits) the value K. Value 2*N+K is the total number of 32-bit integers, occupied by this contour: the next contours starts at the position "offset of this contour"+2*N+K.

minX, maxX, minY, maxY are the mininal/maximal coordinates of all points in this contour, i.e. the rectangle, containing all this contour. (It is allowed to store here some greater rectangle, containing it, but if the contour is created by standard methods, minX, maxX, minY, maxY will contain correct minimums and maximums.)

RESERVED | FLAGS contains in low bits some additional information (see ContourHeader class), in particular, whether this contour is external or internal and whether this header contains frameID (see below). objectLabel is an integer label of the object, the boundary of which is represented by this contour.

The last pair is optional. RESERVED is a special indicator 0x7FFF0000; frameID is some integer ID, that can be stored together with contour and should have some unique ID of the frame in a multi-frame map, from which this contour has been scanned.

  • Field Details

    • BLOCK_LENGTH

      public static final int BLOCK_LENGTH
      Contours array consists of blocks (lines), where each block contains BLOCK_LENGTH = 2 elements. Typical block contains two coordinates (x, y).
      See Also:
    • MIN_ABSOLUTE_COORDINATE

      public static final int MIN_ABSOLUTE_COORDINATE
      Coordinates of points, saved in the contour, cannot be out of range -0x40000000x, y0x3FFFFFFF It allows to guarantee that containing rectangle of each contour has sizes, less than Integer.MAX_VALUE. Also it allows to use positive values with bit #30 = 1 as reserved.
      See Also:
    • MAX_ABSOLUTE_COORDINATE

      public static final int MAX_ABSOLUTE_COORDINATE
      See Also:
    • MAX_NUMBER_OF_CONTOURS

      public static final int MAX_NUMBER_OF_CONTOURS
      Maximal allowed number of contours: 500000000.
      It is essentially less than Integer.MAX_VALUE, that allows to simplify arithmetic operations with number of contours.

      More precisely, we guarantee that this value is less than Integer.MAX_VALUE / 4: for example, you can freely multiply number of contours by 4.

      See Also:
    • MAX_CONTOUR_NUMBER_OF_POINTS

      public static final int MAX_CONTOUR_NUMBER_OF_POINTS
      Number of points, saved in the contour, cannot be greater than 1073741567 ~ 230. It allows to guarantee that each contour can be successfully stored in simple Java int[] array.
      See Also:
  • Method Details

    • newInstance

      public static Contours newInstance()
    • isOptimizeCollinearSteps

      public boolean isOptimizeCollinearSteps()
    • setOptimizeCollinearSteps

      public Contours setOptimizeCollinearSteps(boolean optimizeCollinearSteps)
    • isSerializedContours

      public static boolean isSerializedContours(int[] serialized)
    • isSerializedContour

      public static boolean isSerializedContour(int[] serialized, int offset)
    • deserialize

      public static Contours deserialize(int[] serialized)
    • serialize

      public int[] serialize()
    • isEmpty

      public boolean isEmpty()
    • numberOfContours

      public int numberOfContours()
    • clear

      public void clear()
    • clear

      public void clear(boolean freeResources)
    • openContourForAdding

      public void openContourForAdding(ContourHeader header)
      Starts adding points of new contour.

      Note: most fields in the header may be not filled yet (they will be written in closeContour(ContourHeader) method). But frame ID it is an exception and must be filled now; it cannot be added while closing contour.

      Parameters:
      header - header of new contour.
    • addPoint

      public void addPoint(Boundary2DScanner scanner, long startX, long startY)
    • addPoint

      public void addPoint(long x, long y)
    • addPoint

      public void addPoint(long x, long y, boolean xAtMinXMatrixBoundary, boolean xAtMaxXMatrixBoundary, boolean yAtMinYMatrixBoundary, boolean yAtMaxYMatrixBoundary)
    • closeContour

      public void closeContour(ContourHeader header)
      Finishes adding points and closes the contour.

      Note: touching matrix boundary flags, stored in the contour array, are defined by previous addPoint calls, not by these 4 flags from the header. Corresponding 4 flags in the passed header are ignored. Containing rectangle in the passed header is also ignored; instead, this function stores in the contour array the rectangle, calculated while previous addPoint calls.

      Parameters:
      header - additional information about contour.
    • isContourOpened

      public boolean isContourOpened()
    • addContour

      public void addContour(ContourHeader header, IntArray contour)
    • addContour

      public void addContour(ContourHeader header, IntArray contour, boolean packAddedContour)
    • addContour

      public void addContour(ContourHeader header, int[] contour)
    • addContour

      public void addContour(ContourHeader header, int[] contour, int contourOffset, int contourLength)
    • addContoursRange

      public void addContoursRange(Contours contours, int from, int to)
    • addContour

      public void addContour(Contours contours, int contourIndex)
    • contoursRange

      public Contours contoursRange(int from, int to)
    • addContours

      public void addContours(Contours contours)
    • addTransformedContours

      public void addTransformedContours(Contours contours, double scaleX, double scaleY, double shiftX, double shiftY, boolean removeDegeneratedContours)
    • transformContours

      public Contours transformContours(double scaleX, double scaleY, double shiftX, double shiftY, boolean removeDegeneratedContours)
    • removeContoursRange

      public void removeContoursRange(int from, int to)
    • removeSingleContour

      public void removeSingleContour(int contourIndex)
    • removeLastContour

      public void removeLastContour()
    • getContourNumberOfPoints

      public int getContourNumberOfPoints(int contourIndex)
    • getContourLength

      public int getContourLength(int contourIndex)
    • getContourOffset

      public int getContourOffset(int contourIndex)
    • getContourLengthAndOffset

      public long getContourLengthAndOffset(int contourIndex)
    • extractLength

      public static int extractLength(long lengthAndOffset)
    • extractOffset

      public static int extractOffset(long lengthAndOffset)
    • getSerializedHeaderOffset

      public int getSerializedHeaderOffset(int contourIndex)
    • getContour

      public IntArray getContour(int contourIndex)
    • getContourPoints

      public int getContourPoints(int[] resultPoints, int contourIndex)
    • getContourPointX

      public double getContourPointX(int contourIndex, int pointIndex)
    • getContourPointY

      public double getContourPointY(int contourIndex, int pointIndex)
    • getContourPointsAndReallocateResult

      public int[] getContourPointsAndReallocateResult(int[] resultPoints, ContourLength resultLength, int contourIndex)
    • unpackContour

      public MutableIntArray unpackContour(int contourIndex)
    • unpackContour

      public MutableIntArray unpackContour(int contourIndex, boolean processDiagonalSegments)
    • unpackContourAndReallocateResult

      public int[] unpackContourAndReallocateResult(int[] resultPoints, ContourLength resultLength, int contourIndex)
    • unpackContourAndReallocateResult

      public int[] unpackContourAndReallocateResult(int[] resultPoints, ContourLength resultLength, int contourIndex, boolean processDiagonalSegments)
    • packContours

      public Contours packContours()
    • unpackContours

      public Contours unpackContours()
    • unpackContours

      public Contours unpackContours(boolean processDiagonalSegments)
    • unpackContours

      public Contours unpackContours(boolean processDiagonalSegments, long[] doubledAreas)
    • getHeader

      public ContourHeader getHeader(int contourIndex)
    • getHeader

      public ContourHeader getHeader(ContourHeader resultHeader, int contourIndex)
    • getObjectLabel

      public int getObjectLabel(int contourIndex)
    • setObjectLabel

      public void setObjectLabel(int contourIndex, int label)
    • getFrameIdOrNull

      public Integer getFrameIdOrNull(int contourIndex)
    • isInternalContour

      public boolean isInternalContour(int contourIndex)
    • getAllObjectLabels

      public int[] getAllObjectLabels()
    • getAllInternalContour

      public boolean[] getAllInternalContour()
    • getAllFrameId

      public int[] getAllFrameId(int absentId)
    • isPointStrictlyInside

      public boolean isPointStrictlyInside(int contourIndex, double x, double y)
    • isPointStrictlyInside

      public boolean isPointStrictlyInside(int contourIndex, double x, double y, boolean surelyUnpacked)
    • pointInsideContourStatus

      public InsideContourStatus pointInsideContourStatus(int contourIndex, double x, double y, boolean surelyUnpacked)
    • pointInsideContourInformation

      public double pointInsideContourInformation(int contourIndex, double x, double y, boolean surelyUnpacked)
    • findSomePointInside

      public Point findSomePointInside(int contourIndex)
    • findSomePointInside

      public Point findSomePointInside(int contourIndex, boolean surelyUnpacked)
    • findSomePointInside

      public boolean findSomePointInside(Point2D result, int contourIndex)
    • findSomePointInside

      public boolean findSomePointInside(Point2D result, int contourIndex, boolean surelyUnpacked)
    • strictPerimeter

      public double strictPerimeter(int contourIndex)
    • segmentCentersPerimeter

      public double segmentCentersPerimeter(int contourIndex)
    • strictArea

      public double strictArea(int contourIndex)
    • segmentCentersArea

      public double segmentCentersArea(int contourIndex)
    • preciseDoubledArea

      public long preciseDoubledArea(int contourIndex)
    • sortIndexesByLabels

      public void sortIndexesByLabels(int[] indexes)
    • sortIndexesByLabels

      public void sortIndexesByLabels(int[] indexes, boolean internalContoursFirst)
    • sortIndexesByPreciseArea

      public void sortIndexesByPreciseArea(int[] indexes, boolean absoluteValueOfArea)
    • sortIndexesByPreciseArea

      public void sortIndexesByPreciseArea(int[] indexes, boolean absoluteValueOfArea, boolean greaterAreasFirst)
    • equalsToSerialized

      public boolean equalsToSerialized(int[] serialized)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getContourNumberOfPoints

      public static int getContourNumberOfPoints(IntArray contour)
    • getContourPoints

      public static int getContourPoints(int[] resultPoints, IntArray contour)
    • getContourPointsAndReallocateResult

      public static int[] getContourPointsAndReallocateResult(int[] resultPoints, ContourLength resultLength, IntArray contour)
    • packContour

      public static int packContour(MutableIntArray resultContour, IntArray nonOptimizedContour)
    • packContourAndReallocateResult

      public static int[] packContourAndReallocateResult(int[] resultPoints, ContourLength resultLength, IntArray nonOptimizedContour)
    • packContourAndReallocateResult

      public static int[] packContourAndReallocateResult(int[] resultPoints, ContourLength resultLength, int[] nonOptimizedContour, int nonOptimizedOffset, int n)
    • unpackContour

      public static MutableIntArray unpackContour(IntArray optimizedContour, boolean processDiagonalSegments)
    • unpackContour

      public static int unpackContour(MutableIntArray resultContour, IntArray optimizedContour, boolean processDiagonalSegments)
    • unpackContourAndReallocateResult

      public static int[] unpackContourAndReallocateResult(int[] resultPoints, ContourLength resultLength, IntArray optimizedContour, boolean processDiagonalSegments)
    • reverseContour

      public static MutableIntArray reverseContour(MutableIntArray resultContour, IntArray contour)
    • transformContour

      public static MutableIntArray transformContour(MutableIntArray resultContour, IntArray contour, double scaleX, double scaleY, double shiftX, double shiftY)
    • transformContour

      public static void transformContour(int[] contour, int offset, int n, double scaleX, double scaleY, double shiftX, double shiftY)
    • pointInsideContourInformation

      public static double pointInsideContourInformation(int[] contour, int offset, int n, double x, double y, boolean surelyUnpacked)
    • findSomePointInside

      public static boolean findSomePointInside(Point2D result, int[] contour, int offset, int n, boolean surelyUnpacked)
    • strictPerimeter

      public static double strictPerimeter(IntArray contour)
    • strictPerimeter

      public static double strictPerimeter(int[] contour, int offset, int n)
    • segmentCentersPerimeter

      public static double segmentCentersPerimeter(IntArray contour)
    • segmentCentersPerimeter

      public static double segmentCentersPerimeter(int[] contour, int offset, int n)
    • strictArea

      public static double strictArea(IntArray contour)
    • strictArea

      public static double strictArea(int[] contour, int offset, int n)
    • segmentCentersArea

      public static double segmentCentersArea(IntArray contour)
    • segmentCentersArea

      public static double segmentCentersArea(int[] contour, int offset, int n)
    • preciseDoubledArea

      public static long preciseDoubledArea(int[] contour, int offset, int n)