Connected objects scanner: the class performing scanning and clearing connected objects, "drawn" on some n-dimensional updatable bit matrix.
More precisely, let's consider an AlgART bit matrix ( {@link Matrix}<? extends {@link UpdatableBitArray}>). The connected object is a connected component of the graph, built on the matrix, where unit matrix elements are vertices, and neighbour unit elements are connected by edges. In 2D case, we can consider that the matrix contains images of some objects: unit bits (1) are white pixels, belonging to objects, zero bits (0) are black pixels of the background. Then our term "connected objects" describes the white objects, separated by black space.
We define two kinds of connectivity: {@link ConnectivityType#STRAIGHT_ONLY straight} and {@link ConnectivityType#STRAIGHT_AND_DIAGONAL straight-and-diagonal}. In the first case (straight connectivity), two unit elements with coordinates
∑ |ik−jk|=1
For 2D matrices, this connectivity kind is also known as "4-connectivity". It the second case (straight-and-diagonal connectivity), two unit elements are neighbours if several (at least one) from their coordinates differ by 1 and all other coordinates are equal:
max (|ik−jk|)=1
For 2D matrices, this connectivity kind is also known as "8-connectivity". The connectivity kind is described by {@link ConnectivityType} class.
The instance of this class works with some concrete bit matrix ( {@link Matrix}<? extends {@link UpdatableBitArray}>), named the scanned matrix, and with some concrete connectivity type ( {@link ConnectivityType}). Both the scanned matrix and the connectivity type are specified while creating the instance.
This class allows to visit all elements of the scanner matrix, belonging to one connected object, by its main {@link #clear(ArrayContext,ConnectedObjectScanner.ElementVisitor,long[],boolean) clear} method."Visiting" means that this method calls {@link ConnectedObjectScanner.ElementVisitor#visit(long[],long)}method for every visited element, passing the index of the element in the {@link Matrix#array() underlying array} of the matrix to that method,maybe, together with its coordinates in the matrix (if getting coordinates does not slow down the scanning algorithm).
Besides, the {@link #clear clear} method clears (sets to 0) all visited elements in the bit matrix.But note: actual clearing may be not performed if forceClearing argument of this method is false.
There are following methods allowing to create the instance of this class:
You can switch to another scanned bit matrix with the same dimensions by {@link #matrix(Matrix)} method.
You can use this instance (call its {@link #clear clear} method)many times to scan different connected objects at one matrix. You may use {@link #nextUnitBit(long[])} method to find next connected objectafter scanning and clearing the previous one.
However, you must not use this instance after any modifications in the scanned matrix, performed by an external code. If you modify the matrix, you must create new instance of this class after this.
Note: this class works much faster (in several times) if the scanned matrix is created by {@link SimpleMemoryModel}. So, if the matrix is not created by {@link SimpleMemoryModel} and is not too large,we recommend to create its clone by {@link SimpleMemoryModel} and use this class for the clone.
Note: this class cannot process matrices with too large number of dimensions. The maximal allowed number of dimensions is {@link Matrix#MAX_DIM_COUNT_FOR_SOME_ALGORITHMS}, that is more than enough for most situations.
This class does not use multithreading optimization, unlike {@link Arrays#copy(ArrayContext,UpdatableArray,Array)} and similar methods.In other words, all methods of this class are executed in the current thread.
This class is not thread-safe, but is thread-compatible and can be synchronized manually, if multithread access is necessary. However, usually there are no reasons to use the same instance of this class in different threads: usually there is much better idea to create a separate instance for every thread.
AlgART Laboratory 2007–2014
@author Daniel Alievsky @version 1.2 @since JDK 1.5
|
|
|
|