private void findBestPoints(int x, int y, List<Point2D_F64> pts , List<Integer> selected ) { double bestDist = clickDistance*clickDistance; GrowQueue_I32 bestIndexes = new GrowQueue_I32(20); for( int i = 0; i < pts.size(); i++ ) { if( !isValidPoint(i) ) continue; Point2D_F64 p = pts.get(i); double d = UtilPoint2D_F64.distanceSq(p.x, p.y, x, y); if( d < bestDist ) { bestDist = d; bestIndexes.reset(); bestIndexes.add(i); } else if( Math.abs(d - bestDist) < 0.01 ) { bestIndexes.add(i); } } if( bestIndexes.size() > 0 ) { int indexRight = bestIndexes.get(0); } for (int i = 0; i < bestIndexes.size(); i++) { selected.add(bestIndexes.get(i)); } }
public GrowQueue_I32 checkSource( FastQueue<AssociatedIndex> matches , int num ) { matched.resize(num); for( int i = 0; i < matched.size; i++ ) { matched.data[i] = 0; } for( int i = 0; i < matches.size; i++ ) { matched.data[matches.get(i).src] = 1; } unassociatedSrc.reset(); for( int i = 0; i < matched.size; i++ ) { if( matched.data[i] == 0 ) { unassociatedSrc.add(i); } } return unassociatedSrc; }
public void add(int value) { push(value); }
@Override public GrowQueue_I32 copy() { GrowQueue_I32 ret = new GrowQueue_I32(size); ret.setTo(this); return ret; }
/** * Removes the specified view from the list of views. If it's not contained in the list * an exception is thrown * @param which Index of the view which is to be removed */ public void removeView( int which ) { int index = views.indexOf(which); if( index == -1 ) throw new RuntimeException("BUG. Could not find in list of views. which="+which); views.remove(index); }
skippedCorners.reset(); int contourIndexPrevious = contourCorners.get(anchor0); for (int i = 1; i < numLines; i++) { LineGeneral2D_F64 line0 = lines.get(i - 1); } else { contourCorners.set(cornerIndex, contourIndex); contourIndexPrevious = contourIndex; skippedCorners.add( cornerIndex ); Point2D_I32 b = contour.get(contourCorners.get(cornerIndex)); if( a.x == b.x && a.y == b.y ) { skippedCorners.add( cornerIndex ); int index = skippedCorners.get(i); contourCorners.remove(index); int c0 = CircularIndex.addOffset(anchor0, i, contourCorners.size); int c1 = CircularIndex.addOffset(anchor0, i+1, contourCorners.size); a = contour.get(contourCorners.get(c0)); b = contour.get(contourCorners.get(c1)); return contourCorners.size()>=3;
@Override public boolean process(List<Point2D_I32> list) { splits.reset(); this.contour = list; if( list.size() <= 2 ) { // can't do anything with two or less points return false; } this.minimumSideLengthPixel = (int)Math.ceil(contour.size()* minimumSideLengthFraction); // initial segmentation splits.add(0); splitPixels(0, list.size() - 1); splits.add(list.size()-1); for( int i = 0; i < maxIterations; i++ ) { boolean changed = mergeSegments(); if( !changed && !splitSegments() ) break; if( splits.size() <= 2 || splits.size() >= abortSplits ) break; } return true; }
/** * Gets the value at the index which corresponds to the specified fraction * @param fraction 0 to 1 inclusive * @return value at fraction */ public int getFraction( double fraction ) { return get( (int)((size-1)*fraction) ); }
/** * Very fast histogram based sorting. Index of each pixel is placed inside a list for its intensity level. */ protected void sortPixels(GrayU8 input) { // initialize histogram for( int i = 0; i < histogram.length; i++ ) { histogram[i].reset(); } // sort by creating a histogram for( int y = 0; y < input.height; y++ ) { int index = input.startIndex + y*input.stride; int indexOut = (y+1)*output.stride + 1; for (int x = 0; x < input.width; x++ , index++ , indexOut++) { int value = input.data[index] & 0xFF; histogram[value].add(indexOut); } } }
/** * Scans through the image and record the array index of all marked pixels */ protected void findOnePixels(GrowQueue_I32 ones) { for (int y = 0; y < binary.height; y++) { int index = binary.startIndex + y* binary.stride; for (int x = 0; x < binary.width; x++, index++) { if( binary.data[index] != 0 ) { ones.add(index); } } } }
/** * Does much of the work needed to remove the redundant segments that are being merged into their root node. * The list of member count is updated. mergeList is updated with the new segment IDs. */ protected void setToRootNodeNewID( GrowQueue_I32 regionMemberCount ) { tmpMemberCount.reset(); for( int i = 0; i < mergeList.size; i++ ) { int p = mergeList.data[i]; if( p == i ) { mergeList.data[i] = rootID.data[i]; tmpMemberCount.add( regionMemberCount.data[i] ); } else { mergeList.data[i] = rootID.data[mergeList.data[i]]; } } regionMemberCount.reset(); regionMemberCount.addAll(tmpMemberCount); }
/** * If not touching the border then the number of corners must be 4. If touching the border there must be * at least 3 corners not touching the border. 7 corners at most. If there were 8 then all sides of a square * would be touching the border. No more than 3 corners since that's the most number of non-border corners * a square can have. */ @Override public boolean filterPixelPolygon(List<Point2D_I32> externalUndist, List<Point2D_I32> externalDist, GrowQueue_I32 splits, boolean touchesBorder) { if( touchesBorder ) { if( splits.size() > 7 || splits.size() < 3) return false; int totalRegular = 0; for (int i = 0; i < splits.size(); i++) { Point2D_I32 p = externalDist.get(splits.get(i)); if( !(p.x == 0 || p.y == 0 || p.x == width-1 || p.y == height-1)) totalRegular++; } return totalRegular > 0 && totalRegular <= 4; // should be 3, but noise/imprecision in corner can make it 4 } else { return splits.size() == 4; } } }
/** * Look for two corners which point to the same point and removes one of them from the corner list */ void removeDuplicates( GrowQueue_I32 corners ) { // remove duplicates for (int i = 0; i < corners.size(); i++) { Point2D_I32 a = contour.get(corners.get(i)); // start from the top so that removing a corner doesn't mess with the for loop for (int j = corners.size()-1; j > i; j--) { Point2D_I32 b = contour.get(corners.get(j)); if( a.x == b.x && a.y == b.y ) { // this is still ok if j == 0 because it wrapped around. 'i' will now be > size corners.remove(j); } } } }
@SuppressWarnings("unchecked") @Override public void setPoints(List<double[]> points, boolean trackIndicies) { // Make a copy because we mutate the lists this.items = points.toArray(new double[0][]); this.indexes = new GrowQueue_I32(); indexes.resize(points.size()); for (int i = 0; i < points.size(); i++) { indexes.data[i] = i; } this.root = buildFromPoints(0, items.length); }
@Override public void findNearest(P point, double maxDistance, int numNeighbors, FastQueue<NnData<P>> results) { results.reset(); if( maxDistance < 0 ) maxDistance = Double.MAX_VALUE; outputIndex.reset(); outputDistance.reset(); alg.findClosestN(point,maxDistance,numNeighbors,outputIndex,outputDistance); for( int i = 0; i < outputIndex.size; i++ ) { int index = outputIndex.get(i); NnData<P> r = results.grow(); r.distance = outputDistance.get(i); r.point = points.get(index); r.index = index; } } }
@Test public void removeHead() { GrowQueue_I32 alg = new GrowQueue_I32(10); alg.push(1); alg.push(3); alg.push(4); alg.push(5); alg.removeHead(0); assertEquals(4,alg.size); assertEquals(1,alg.get(0)); alg.removeHead(2); assertEquals(2,alg.size); assertEquals(4,alg.get(0)); }