io_scene_gltf2: quiet error running when context.space is None
[blender-addons.git] / object_fracture_cell / fracture_cell_calc.py
blobdc6bdafe2f64cbfcf551699f4f886bc44232eec1
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Script copyright (C) Blender Foundation 2012
6 def points_as_bmesh_cells(
7 verts,
8 points,
9 points_scale=None,
10 margin_bounds=0.05,
11 margin_cell=0.0,
13 from math import sqrt
14 import mathutils
15 from mathutils import Vector
17 cells = []
19 points_sorted_current = [p for p in points]
20 plane_indices = []
21 vertices = []
23 if points_scale is not None:
24 points_scale = tuple(points_scale)
25 if points_scale == (1.0, 1.0, 1.0):
26 points_scale = None
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
30 if 1:
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
38 convexPlanes = [
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)
70 nlength *= scalar
71 normal = normal_alt
73 if nlength > distance_max:
74 break
76 plane = normal.normalized()
77 plane.resize_4d()
78 plane[3] = (-nlength / 2.0) + margin_cell
79 planes.append(plane)
81 vertices[:], plane_indices[:] = mathutils.geometry.points_in_planes(planes)
82 if len(vertices) == 0:
83 break
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!
91 for v in vertices:
92 distance = v.length_squared
93 if distance_max < distance:
94 distance_max = distance
95 distance_max = sqrt(distance_max) # make real length
96 distance_max *= 2.0
98 if len(vertices) == 0:
99 continue
101 cells.append((point_cell_current, vertices[:]))
102 del vertices[:]
104 return cells