Code cleanup: White space and dead code.

This commit is contained in:
Tamito Kajiyama 2015-07-10 22:53:58 +09:00
parent eeeb845d33
commit 25638a9656
3 changed files with 40 additions and 51 deletions

View File

@ -50,7 +50,6 @@ __all__ = (
"tripplewise",
)
# module members
from _freestyle import (
ContextFunctions,
@ -71,6 +70,7 @@ from functools import lru_cache, namedtuple
from math import cos, sin, pi, atan2
from itertools import tee, compress
# -- types -- #
# A named tuple primitive used for storing data that has an upper and
@ -92,6 +92,7 @@ def rgb_to_bw(r, g, b):
"""Method to convert rgb to a bw intensity value."""
return 0.35 * r + 0.45 * g + 0.2 * b
def bound(lower, x, higher):
"""Returns x bounded by a maximum and minimum value. Equivalent to:
return min(max(x, lower), higher)
@ -99,6 +100,7 @@ def bound(lower, x, higher):
# this is about 50% quicker than min(max(x, lower), higher)
return (lower if x <= lower else higher if x >= higher else x)
def get_strokes():
"""Get all strokes that are currently available"""
return tuple(map(Operators().get_stroke_from_index, range(Operators().get_strokes_size())))
@ -132,6 +134,7 @@ def material_from_fedge(fe):
material = right if (right.priority > left.priority) else left
return material
def bounding_box(stroke):
"""
Returns the maximum and minimum coordinates (the bounding box) of the stroke's vertices
@ -139,9 +142,10 @@ def bounding_box(stroke):
x, y = zip(*(svert.point for svert in stroke))
return (Vector((min(x), min(y))), Vector((max(x), max(y))))
def normal_at_I0D(it: Interface0DIterator) -> Vector:
"""Normal at an Interface0D object. In contrast to Normal2DF0D this
function uses the actual data instead of underlying Fedge objects.
"""Normal at an Interface0D object. In contrast to Normal2DF0D this
function uses the actual data instead of underlying Fedge objects.
"""
if it.at_last and it.is_begin:
# corner-case
@ -165,18 +169,20 @@ def normal_at_I0D(it: Interface0DIterator) -> Vector:
it.decrement()
return (b.point - a.point).orthogonal().normalized()
def angle_x_normal(it: Interface0DIterator):
"""unsigned angle between a Point's normal and the X axis, in radians"""
normal = normal_at_I0D(it)
return abs(atan2(normal[1], normal[0]))
def curvature_from_stroke_vertex(svert):
"""The 3D curvature of an stroke vertex' underlying geometry
The result is None or in the range [-inf, inf]"""
c1 = svert.first_svertex.curvatures
c2 = svert.second_svertex.curvatures
if c1 is None and c2 is None:
Kr = None
Kr = None
elif c1 is None:
Kr = c2[4]
elif c2 is None:
@ -185,6 +191,7 @@ def curvature_from_stroke_vertex(svert):
Kr = c1[4] + svert.t2d * (c2[4] - c1[4])
return Kr
# -- General helper functions -- #
@lru_cache(maxsize=32)
@ -201,9 +208,8 @@ def phase_to_direction(length):
return results
# -- simplification of a set of points; based on simplify.js by Vladimir Agafonkin --
# https://mourner.github.io/simplify-js/
# -- simplification of a set of points; based on simplify.js by Vladimir Agafonkin --
# https://mourner.github.io/simplify-js/
def getSquareSegmentDistance(p, p1, p2):
"""
@ -260,11 +266,12 @@ def simplifyDouglasPeucker(points, tolerance):
first_stack.append(index)
last_stack.append(last)
first = first_stack.pop() if first_stack else None
last = last_stack.pop() if last_stack else None
first = first_stack.pop() if first_stack else None
last = last_stack.pop() if last_stack else None
return tuple(compress(points, markers))
def simplify(points, tolerance):
"""Simplifies a set of points"""
return simplifyDouglasPeucker(points, tolerance * tolerance)
@ -475,6 +482,7 @@ def iter_distance_along_stroke(stroke):
distance += (prev - curr).length
yield distance
# -- mathematical operations -- #
def stroke_curvature(it):
@ -520,24 +528,9 @@ def stroke_normal(stroke):
for use in geometry modifiers it is advised to
cast this generator function to a tuple or list
"""
# n = len(stroke) - 1
it = iter(stroke)
yield from (normal_at_I0D(it) for _ in it)
#for i, svert in enumerate(stroke):
# if i == 0:
# e = stroke[i + 1].point - svert.point
# yield Vector((e[1], -e[0])).normalized()
# elif i == n:
# e = svert.point - stroke[i - 1].point
# yield Vector((e[1], -e[0])).normalized()
# else:
# e1 = stroke[i + 1].point - svert.point
# e2 = svert.point - stroke[i - 1].point
# n1 = Vector((e1[1], -e1[0])).normalized()
# n2 = Vector((e2[1], -e2[0])).normalized()
# yield (n1 + n2).normalized()
def get_test_stroke():
"""Returns a static stroke object for testing """

View File

@ -190,7 +190,7 @@ class CurveMappingModifier(ScalarBlendModifier):
curve.initialize()
result = curve.curves[0].evaluate(t)
# float precision errors in t can give a very weird result for evaluate.
# therefore, bound the result by the curve's min and max values
# therefore, bound the result by the curve's min and max values
return bound(curve.clip_min_y, result, curve.clip_max_y)
@ -234,7 +234,6 @@ class ThicknessBlenderMixIn(ThicknessModifierMixIn):
thickness = sum(thickness)
self.blend_thickness_symmetric(svert, thickness)
def blend_thickness_symmetric(self, svert, v):
"""Blends and sets the thickness. Thickness is equal on each side of the backbone"""
outer, inner = svert.attribute.thickness
@ -443,6 +442,7 @@ class ThicknessDistanceFromObjectShader(ThicknessBlenderMixIn, CurveMappingModif
b = self.value.min + self.evaluate(t) * self.value.delta
self.blend_thickness(svert, b)
# Material modifiers
class ColorMaterialShader(ColorRampModifier):
"""Assigns a color to the vertices based on their underlying material."""
@ -458,7 +458,7 @@ class ColorMaterialShader(ColorRampModifier):
for svert in it:
material = self.func(it)
if self.attribute == 'LINE':
b = material.line[0:3]
b = material.line[0:3]
elif self.attribute == 'DIFF':
b = material.diffuse[0:3]
else:
@ -471,6 +471,7 @@ class ColorMaterialShader(ColorRampModifier):
b = self.evaluate(value)
svert.attribute.color = self.blend_ramp(a, b)
class AlphaMaterialShader(CurveMappingModifier):
"""Assigns an alpha value to the vertices based on their underlying material."""
def __init__(self, blend, influence, mapping, invert, curve, material_attribute):
@ -503,7 +504,6 @@ class ThicknessMaterialShader(ThicknessBlenderMixIn, CurveMappingModifier):
# Calligraphic thickness modifier
class CalligraphicThicknessShader(ThicknessBlenderMixIn, ScalarBlendModifier):
"""Thickness modifier for achieving a calligraphy-like effect."""
def __init__(self, thickness_position, thickness_ratio,
@ -526,6 +526,7 @@ class CalligraphicThicknessShader(ThicknessBlenderMixIn, ScalarBlendModifier):
b = self.thickness.min
self.blend_thickness(svert, b)
# - Tangent Modifiers - #
class TangentColorShader(ColorRampModifier):
@ -535,7 +536,6 @@ class TangentColorShader(ColorRampModifier):
for svert in it:
angle = angle_x_normal(it)
fac = self.evaluate(angle / pi)
a = svert.attribute.color
svert.attribute.color = self.blend_ramp(a, fac)
@ -547,14 +547,13 @@ class TangentAlphaShader(CurveMappingModifier):
for svert in it:
angle = angle_x_normal(it)
fac = self.evaluate(angle / pi)
a = svert.attribute.alpha
svert.attribute.alpha = self.blend(a, fac)
class TangentThicknessShader(ThicknessBlenderMixIn, CurveMappingModifier):
"""Thickness based on the direction of the stroke"""
def __init__(self, thickness_position, thickness_ratio, blend, influence, mapping, invert, curve,
def __init__(self, thickness_position, thickness_ratio, blend, influence, mapping, invert, curve,
thickness_min, thickness_max):
ThicknessBlenderMixIn.__init__(self, thickness_position, thickness_ratio)
CurveMappingModifier.__init__(self, blend, influence, mapping, invert, curve)
@ -567,13 +566,15 @@ class TangentThicknessShader(ThicknessBlenderMixIn, CurveMappingModifier):
thickness = self.thickness.min + self.evaluate(angle / pi) * self.thickness.delta
self.blend_thickness(svert, thickness)
# - Noise Modifiers - #
class NoiseShader:
"""Base class for noise shaders"""
def __init__(self, amplitude, period, seed=512):
self.amplitude = amplitude
self.scale = 1 / period / seed
self.seed = seed
self.seed = seed
def noisegen(self, stroke, n1=Noise(), n2=Noise()):
"""Produces two noise values per StrokeVertex for every vertex in the stroke"""
@ -584,7 +585,7 @@ class NoiseShader:
a = n1.turbulence_smooth(self.scale * svert.curvilinear_abscissa + initU1, 2)
b = n2.turbulence_smooth(self.scale * svert.curvilinear_abscissa + initU2, 2)
yield (svert, a, b)
class ThicknessNoiseShader(ThicknessBlenderMixIn, ScalarBlendModifier, NoiseShader):
"""Thickness based on pseudo-noise"""
@ -608,7 +609,7 @@ class ColorNoiseShader(ColorRampModifier, NoiseShader):
ColorRampModifier.__init__(self, blend, influence, ramp)
NoiseShader.__init__(self, amplitude, period, seed)
def shade(self, stroke):
def shade(self, stroke):
for svert, noiseval1, noiseval2 in self.noisegen(stroke):
position = abs(noiseval1 + noiseval2)
svert.attribute.color = self.blend_ramp(svert.attribute.color, self.evaluate(position))
@ -625,6 +626,7 @@ class AlphaNoiseShader(CurveMappingModifier, NoiseShader):
position = abs(noiseval1 + noiseval2)
svert.attribute.alpha = self.blend(svert.attribute.alpha, self.evaluate(position))
# - Crease Angle Modifiers - #
def crease_angle(svert):
@ -678,7 +680,6 @@ class CreaseAngleThicknessShader(ThicknessBlenderMixIn, CurveMappingModifier):
# angles are (already) in radians
self.angle = BoundedProperty(angle_min, angle_max)
self.thickness = BoundedProperty(thickness_min, thickness_max)
def shade(self, stroke):
for svert in stroke:
@ -689,14 +690,15 @@ class CreaseAngleThicknessShader(ThicknessBlenderMixIn, CurveMappingModifier):
thickness = self.thickness.min + self.evaluate(t) * self.thickness.delta
self.blend_thickness(svert, thickness)
# - Curvature3D Modifiers - #
def normalized_absolute_curvature(svert, bounded_curvature):
"""
Gives the absolute curvature in range [0, 1].
The actual curvature (Kr) value can be anywhere in the range [-inf, inf], where convex curvature
yields a positive value, and concave a negative one. These shaders only look for the magnitude
yields a positive value, and concave a negative one. These shaders only look for the magnitude
of the 3D curvature, hence the abs()
"""
curvature = curvature_from_stroke_vertex(svert)
@ -704,6 +706,7 @@ def normalized_absolute_curvature(svert, bounded_curvature):
return 0.0
return bounded_curvature.interpolate(abs(curvature))
class Curvature3DColorShader(ColorRampModifier):
"""Color based on the 3D curvature of the underlying geometry"""
def __init__(self, blend, influence, ramp, curvature_min, curvature_max):
@ -713,7 +716,6 @@ class Curvature3DColorShader(ColorRampModifier):
def shade(self, stroke):
for svert in stroke:
t = normalized_absolute_curvature(svert, self.curvature)
a = svert.attribute.color
b = self.evaluate(t)
svert.attribute.color = self.blend_ramp(a, b)
@ -1041,8 +1043,8 @@ class AngleLargerThanBP1D(BinaryPredicate1D):
x = (dir1 * dir2) / denom
return acos(bound(-1.0, x, 1.0)) > self.angle
# predicates for selection
# predicates for selection
class LengthThresholdUP1D(UnaryPredicate1D):
def __init__(self, length_min=None, length_max=None):
@ -1490,13 +1492,13 @@ def process(layer_name, lineset_name):
m.orientation, m.thickness_min, m.thickness_max))
elif m.type == 'TANGENT':
shaders_list.append(TangentThicknessShader(
thickness_position, linestyle.thickness_ratio,
thickness_position, linestyle.thickness_ratio,
m.blend, m.influence, m.mapping, m.invert, m.curve,
m.thickness_min, m.thickness_max))
elif m.type == 'NOISE':
shaders_list.append(ThicknessNoiseShader(
thickness_position, linestyle.thickness_ratio,
m.blend, m.influence,
m.blend, m.influence,
m.amplitude, m.period, m.seed, m.use_asymmetric))
elif m.type == 'CREASE_ANGLE':
shaders_list.append(CreaseAngleThicknessShader(

View File

@ -57,7 +57,6 @@ typedef struct LineStyleModifier {
float influence;
int flags;
int blend;
} LineStyleModifier;
/* LineStyleModifier::type */
@ -200,10 +199,9 @@ typedef struct LineStyleColorModifier_Curvature_3D {
struct LineStyleModifier modifier;
float min_curvature, max_curvature;
struct ColorBand *color_ramp;
float range_min, range_max;
}LineStyleColorModifier_Curvature_3D;
} LineStyleColorModifier_Curvature_3D;
typedef struct LineStyleAlphaModifier_Curvature_3D {
struct LineStyleModifier modifier;
@ -212,17 +210,16 @@ typedef struct LineStyleAlphaModifier_Curvature_3D {
int flags;
float min_curvature, max_curvature;
int pad;
}LineStyleAlphaModifier_Curvature_3D;
} LineStyleAlphaModifier_Curvature_3D;
typedef struct LineStyleThicknessModifier_Curvature_3D {
struct LineStyleModifier modifier;
struct CurveMapping *curve;
int flags, pad;
float min_curvature, max_curvature;
float min_thickness, max_thickness;
}LineStyleThicknessModifier_Curvature_3D;
} LineStyleThicknessModifier_Curvature_3D;
/* Noise modifiers (for color, alpha and thickness) */
@ -249,7 +246,6 @@ typedef struct LineStyleThicknessModifier_Noise {
float period, amplitude;
int flags;
int seed;
} LineStyleThicknessModifier_Noise;
/* Crease Angle modifiers */
@ -275,7 +271,6 @@ typedef struct LineStyleThicknessModifier_CreaseAngle {
struct CurveMapping *curve;
int flags, pad;
float min_angle, max_angle;
float min_thickness, max_thickness;
} LineStyleThicknessModifier_CreaseAngle;
@ -568,8 +563,7 @@ typedef struct FreestyleLineStyle {
short use_nodes, pad[3];
unsigned short dash1, gap1, dash2, gap2, dash3, gap3;
int panel; /* for UI */
struct MTex *mtex[18]; /* MAX_MTEX */
struct MTex *mtex[18]; /* MAX_MTEX */
/* nodes */
struct bNodeTree *nodetree;