Boundary value ray tracing in a heterogeneous medium: a simple and versatile algorithm