1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Script copyright (C) Blender Foundation 2012
6 def points_as_bmesh_cells(
15 from mathutils
import Vector
19 points_sorted_current
= [p
for p
in points
]
23 if points_scale
is not None:
24 points_scale
= tuple(points_scale
)
25 if points_scale
== (1.0, 1.0, 1.0):
28 # there are many ways we could get planes - convex hull for eg
29 # but it ends up fastest if we just use bounding box
31 xa
= [v
[0] for v
in verts
]
32 ya
= [v
[1] for v
in verts
]
33 za
= [v
[2] for v
in verts
]
35 xmin
, xmax
= min(xa
) - margin_bounds
, max(xa
) + margin_bounds
36 ymin
, ymax
= min(ya
) - margin_bounds
, max(ya
) + margin_bounds
37 zmin
, zmax
= min(za
) - margin_bounds
, max(za
) + margin_bounds
39 Vector((+1.0, 0.0, 0.0, -xmax
)),
40 Vector((-1.0, 0.0, 0.0, +xmin
)),
41 Vector((0.0, +1.0, 0.0, -ymax
)),
42 Vector((0.0, -1.0, 0.0, +ymin
)),
43 Vector((0.0, 0.0, +1.0, -zmax
)),
44 Vector((0.0, 0.0, -1.0, +zmin
)),
47 for i
, point_cell_current
in enumerate(points
):
48 planes
= [None] * len(convexPlanes
)
49 for j
in range(len(convexPlanes
)):
50 planes
[j
] = convexPlanes
[j
].copy()
51 planes
[j
][3] += planes
[j
].xyz
.dot(point_cell_current
)
52 distance_max
= 10000000000.0 # a big value!
54 points_sorted_current
.sort(key
=lambda p
: (p
- point_cell_current
).length_squared
)
56 for j
in range(1, len(points
)):
57 normal
= points_sorted_current
[j
] - point_cell_current
58 nlength
= normal
.length
60 if points_scale
is not None:
61 normal_alt
= normal
.copy()
62 normal_alt
.x
*= points_scale
[0]
63 normal_alt
.y
*= points_scale
[1]
64 normal_alt
.z
*= points_scale
[2]
66 # rotate plane to new distance
67 # should always be positive!! - but abs incase
68 scalar
= normal_alt
.normalized().dot(normal
.normalized())
69 # assert(scalar >= 0.0)
73 if nlength
> distance_max
:
76 plane
= normal
.normalized()
78 plane
[3] = (-nlength
/ 2.0) + margin_cell
81 vertices
[:], plane_indices
[:] = mathutils
.geometry
.points_in_planes(planes
)
82 if len(vertices
) == 0:
85 if len(plane_indices
) != len(planes
):
86 planes
[:] = [planes
[k
] for k
in plane_indices
]
88 # for comparisons use length_squared and delay
89 # converting to a real length until the end.
90 distance_max
= 10000000000.0 # a big value!
92 distance
= v
.length_squared
93 if distance_max
< distance
:
94 distance_max
= distance
95 distance_max
= sqrt(distance_max
) # make real length
98 if len(vertices
) == 0:
101 cells
.append((point_cell_current
, vertices
[:]))