Skip to content

Commit

Permalink
Fix bug that allowed alpha to go negative
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysyu committed Sep 25, 2014
1 parent f5bf171 commit 57ad01e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
11 changes: 6 additions & 5 deletions ensemble/ctf/editor_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from ensemble.ctf.piecewise import PiecewiseFunction
from ensemble.ctf.utils import (FunctionUIAdapter, AlphaFunctionUIAdapter,
ColorFunctionUIAdapter, clip_to_unit)
ColorFunctionUIAdapter, clip, clip_to_unit)


class ValueMapper(object):
Expand Down Expand Up @@ -94,9 +94,10 @@ def set_delta(self, value, delta_x, delta_y):
value_at = self.function.value_at
x_limits = tuple([value_at(i)[0] for i in (index-1, index+1)])

x_val = min(max(x_limits[0], start_value[0] + delta_x), x_limits[1])
new_value = (x_val, start_value[1] + delta_y)
self.function.update(index, new_value)
x = clip(start_value[0] + delta_x, x_limits)
y = clip_to_unit(start_value[1] + delta_y)

self.function.update(index, (x, y))
self.function_updated = True


Expand All @@ -117,7 +118,7 @@ def set_delta(self, value, delta_x, delta_y):
value_at = self.function.value_at
x_limits = tuple([value_at(i)[0] for i in (index-1, index+1)])

x_pos = min(max(x_limits[0], start_value[0] + delta_x), x_limits[1])
x_pos = clip(start_value[0] + delta_x, x_limits)
new_value = (x_pos,) + start_value[1:]
self.function.update(index, new_value)
self.function_updated = True
54 changes: 54 additions & 0 deletions ensemble/ctf/tests/test_editor_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from ensemble.ctf.editor import ALPHA_DEFAULT, create_function

from ensemble.ctf.editor_tools import AlphaFunctionEditorTool


def init_alpha_function_tool():
alpha_function = create_function(ALPHA_DEFAULT)
return AlphaFunctionEditorTool(function=alpha_function)


def test_move_first_point():
# The first point in the alpha curve cannot move in the x-direction
tool = init_alpha_function_tool()

dy = 0.5 # Note the first point is at y = 0, so we need 0 < dy < 1
index = 0
init_position = tool.function.value_at(index)
tool.set_delta((index, init_position), 0.1, dy)

final_position = tool.function.value_at(index)
# The x-position is pinned
assert final_position[0] == init_position[0]
assert final_position[1] == (init_position[1] + dy)


def test_move_last_point():
# The last point in the alpha curve cannot move in the x-direction
tool = init_alpha_function_tool()

dy = -0.5 # Note the last point is at y = 1, so we need 0 < dy < -1
index = tool.function.size() - 1
init_position = tool.function.value_at(index)
tool.set_delta((index, init_position), -0.1, dy)

final_position = tool.function.value_at(index)
# The x-position is pinned
assert final_position[0] == init_position[0]
assert final_position[1] == (init_position[1] + dy)


def test_node_doesnt_move_below_zero():
tool = init_alpha_function_tool()

index = 1
middle_point = (0.5, 0)
tool.function.insert(middle_point)

assert tool.function.size() == 3
assert tool.function.value_at(index) == middle_point

eps = 0.001
tool.set_delta((index, middle_point), 0, -eps)

assert tool.function.value_at(index) == middle_point
7 changes: 6 additions & 1 deletion ensemble/ctf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ def func(pos):


def clip_to_unit(value):
return min(max(value, 0.0), 1.0)
return clip(value, (0.0, 1.0))


def clip(value, limits):
v_min, v_max = limits
return min(max(value, v_min), v_max)


def point_dist(pos0, pos1):
Expand Down

0 comments on commit 57ad01e

Please sign in to comment.