The optimal segmentation of three-dimensional permanent-magnet systems into uniformly magnetized blocks to generate a desired field is a question that has been studied extensively in recent years. We present a procedure that generates the theoretically optimal shape and magnetization direction of all the magnet segments in the system, given only the total number of blocks as a constraint. Materials of arbitrary magnetic permeability can be included in this optimization framework. As an optimization objective we consider any functional that is linear with respect to the magnetic field distribution. We furthermore assume that all magnetic materials obey a linear constitutive relation. We show that with these assumptions calculating an optimal three-dimensional segmentation is equivalent to determining a centroidal Voronoi tessellation. We use the most-well-known and most-straightforward algorithm for the generation of centroidal Voronoi tessellations, known as "Lloyd's method," which can also be thought of as a fixed-point iteration. We show that the procedure is guaranteed to lead to a configuration that is at least locally optimal with respect to the assumed linear objective functional. However, we present results providing a strong indication that for most design problems applying our technique a few times starting from different random configurations is very likely to lead to the globally optimal solution.