Casimir effects: an optical approach I. Foundations and examples