Utils: add optimized min/max functions, use them in grid and triangle