Bring general sketch commits upstream

Move the general parts of PR #1178 upstream decrease the maintenance work.
If you don't want it to be inline? Rectangle operations are somewhat a hot code path, no?