Generalized coarea formula and fractal sets