Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fixed division by zero bug in alpha compositing

Sridhar Rameshc2b9417c6a84
diff --git a/camera/camera.py b/camera/camera.py
index 1217f86f1..2943162cb 100644
--- a/camera/camera.py
+++ b/camera/camera.py
@@ -430,10 +430,15 @@ class Camera(object):
]
out_a = src_a + dst_a*(1.0-src_a)
+
+ # When the output alpha is 0 for full transparency,
+ # we have a choice over what RGB value to use in our
+ # output representation. We choose 0.0 here.
out_rgb = fdiv(
src_rgb*src_a[..., None] + \
dst_rgb*dst_a[..., None]*(1.0-src_a[..., None]),
- out_a[..., None]
+ out_a[..., None],
+ zero_over_zero_value = 0.0
)
self.pixel_array[..., :3] = out_rgb*self.rgb_max_val
diff --git a/helpers.py b/helpers.py
index b9c7f3878..878eb4e92 100644
--- a/helpers.py
+++ b/helpers.py
@@ -694,8 +694,19 @@ class DictAsObject(object):
self.__dict__ = dict
# Just to have a less heavyweight name for this extremely common operation
-def fdiv(a, b):
- return np.true_divide(a,b)
+#
+# We may wish to have more fine-grained control over division by zero behavior
+# in future (separate specifiable default values for 0/0 and x/0 with x != 0),
+# but for now, we just allow the option to handle 0/0.
+def fdiv(a, b, zero_over_zero_value = None):
+ if zero_over_zero_value != None:
+ out = np.full_like(a, zero_over_zero_value)
+ where = np.logical_or (a != 0, b != 0)
+ else:
+ out = None
+ where = True
+
+ return np.true_divide(a, b, out = out, where = where)
def add_extension_if_not_present(file_name, extension):
# This could conceivably be smarter about handling existing differing extensions