diff --git a/scikits/image/graph/_mcp.pyx b/scikits/image/graph/_mcp.pyx index c1a6429e..47c704fc 100644 --- a/scikits/image/graph/_mcp.pyx +++ b/scikits/image/graph/_mcp.pyx @@ -476,19 +476,26 @@ cdef class MCP: # ignore this point if flat_cumulative_costs[new_index] != inf: continue - + # If the cost at this point is negative or infinite, ignore it + new_cost = flat_costs[new_index] + if new_cost < 0 or new_cost == inf: + continue + # Now we ask the heap to append or update the cost to # this new point, but only if that point isn't already # in the heap, or it is but the new cost is lower. travel_cost = self._travel_cost(flat_costs[index], - flat_costs[new_index], + new_cost, offset_lengths[i]) - costs_heap.push_if_lower_fast(cost + travel_cost, new_index) - # If we did perform an append or update, we should - # record the offset from the predecessor to this new - # point - if costs_heap._pushed: - traceback_offsets[new_index] = i + # don't push infs into the heap though! + new_cost = cost + travel_cost + if new_cost != inf: + costs_heap.push_if_lower_fast(new_cost, new_index) + # If we did perform an append or update, we should + # record the offset from the predecessor to this new + # point + if costs_heap._pushed: + traceback_offsets[new_index] = i # Un-flatten the costs and traceback arrays for human consumption. cumulative_costs = flat_cumulative_costs.reshape(self.costs_shape, diff --git a/scikits/image/graph/heap.pxd b/scikits/image/graph/heap.pxd index b86a0027..5f3b17ee 100644 --- a/scikits/image/graph/heap.pxd +++ b/scikits/image/graph/heap.pxd @@ -22,7 +22,7 @@ cdef class BinaryHeap: cdef void _remove(self, int i) cdef int push_fast(self, double value, int reference) - cdef float pop_fast(self) + cdef VALUE_T pop_fast(self) cdef class FastUpdateBinaryHeap(BinaryHeap): cdef readonly int max_reference @@ -30,6 +30,6 @@ cdef class FastUpdateBinaryHeap(BinaryHeap): cdef int _invalid_ref cdef int _pushed - cdef float value_of_fast(self, int reference) + cdef VALUE_T value_of_fast(self, int reference) cdef int push_if_lower_fast(self, double value, int reference) \ No newline at end of file diff --git a/scikits/image/graph/heap.pyx b/scikits/image/graph/heap.pyx index 2345f2f9..59822a26 100644 --- a/scikits/image/graph/heap.pyx +++ b/scikits/image/graph/heap.pyx @@ -339,7 +339,7 @@ cdef class BinaryHeap: return count - cdef float pop_fast(self): + cdef VALUE_T pop_fast(self): """The c-method for fast popping. Returns the minimum value. The reference is put in self._popped_ref""" @@ -366,7 +366,7 @@ cdef class BinaryHeap: # get values cdef int ir = i - ((1 << levels) - 1) #(2**self.levels-1) # LevelStart - cdef float value = values[i] + cdef VALUE_T value = values[i] self._popped_ref = self._references[ir] # remove it @@ -645,7 +645,7 @@ cdef class FastUpdateBinaryHeap(BinaryHeap): return ir - cdef float value_of_fast(self, int reference): + cdef VALUE_T value_of_fast(self, int reference): """Return the value corresponding to the given reference. If inf is returned, the reference may be invalid: check the _invaild_ref field in this case."""