Skip to content

Commit

Permalink
Fix logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
portnov committed Sep 11, 2022
1 parent f9322e4 commit 144fe26
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
5 changes: 4 additions & 1 deletion nodes/curve/reduce_degree.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ def process(self):
kwargs = dict(delta = degree)
else:
kwargs = dict(target = degree)
curve = curve.reduce_degree(tolerance=tolerance, if_possible = self.if_possible, **kwargs)
curve = curve.reduce_degree(tolerance=tolerance,
if_possible = self.if_possible,
logger = self.get_logger(),
**kwargs)
new_curves.append(curve)
if flat_output:
curves_out.extend(new_curves)
Expand Down
5 changes: 4 additions & 1 deletion nodes/surface/reduce_degree.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ def process(self):
kwargs = dict(delta = degree)
else:
kwargs = dict(target = degree)
surface = surface.reduce_degree(self.direction, tolerance=tolerance, **kwargs)
surface = surface.reduce_degree(self.direction,
tolerance = tolerance,
logger = self.get_logger(),
**kwargs)
new_surfaces.append(surface)
if flat_output:
surfaces_out.extend(new_surfaces)
Expand Down
14 changes: 9 additions & 5 deletions utils/curve/nurbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def elevate_degree(self, delta=None, target=None):
return result
#raise UnsupportedCurveTypeException("Degree elevation is not implemented for non-bezier curves yet")

def reduce_degree(self, delta=None, target=None, tolerance=1e-6, return_error=False, if_possible=False):
def reduce_degree(self, delta=None, target=None, tolerance=1e-6, return_error=False, if_possible=False, logger=None):
orig_delta, orig_target = delta, target
if delta is None and target is None:
delta = 1
Expand All @@ -391,6 +391,9 @@ def reduce_degree(self, delta=None, target=None, tolerance=1e-6, return_error=Fa
if delta == 0:
return self

if logger is None:
logger = getLogger()

def reduce_degree_once(curve, tolerance):
if curve.is_bezier():
old_control_points = curve.get_homogenous_control_points()
Expand All @@ -413,7 +416,7 @@ def reduce_degree_once(curve, tolerance):
for i, segment in enumerate(segments):
try:
s, error, ok = reduce_degree_once(segment, tolerance)
print(f"Curve segment #{i}: error = {error}")
logger.debug(f"Curve segment #{i}: error = {error}")
except CantReduceDegreeException as e:
raise CantReduceDegreeException(f"At segment #{i}: {e}") from e
max_error = max(max_error, error)
Expand All @@ -435,7 +438,7 @@ def reduce_degree_once(curve, tolerance):
raise CantReduceDegreeException(f"At iteration #{i}: {e}") from e
if not ok: # if if_possible would be false, we would get an exception
break
print(f"Iteration #{i}, error = {error}")
logger.debug(f"Iteration #{i}, error = {error}")
total_error += error
remaining_tolerance -= error
if total_error > tolerance:
Expand All @@ -446,7 +449,7 @@ def reduce_degree_once(curve, tolerance):
return result
else:
raise CantReduceDegreeException(f"Tolerance exceeded at iteration #{i}, error is {total_error}")
print(f"Curve degree reduction error: {total_error}")
logger.debug(f"Curve degree reduction error: {total_error}")
if return_error:
return result, total_error
else:
Expand Down Expand Up @@ -1365,7 +1368,8 @@ def remove_one_knot(curve):

if not if_possible and (removed_count < count):
raise CantRemoveKnotException(f"Asked to remove knot t={u} for {count} times, but could remove it only {removed_count} times")
#print(f"Removed knot t={u} for {removed_count} times")
if logger is not None:
logger.debug(f"Removed knot t={u} for {removed_count} times")
return curve


Expand Down
15 changes: 9 additions & 6 deletions utils/surface/nurbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sverchok.utils.curve.algorithms import unify_curves_degree, SvCurveFrameCalculator
from sverchok.utils.surface.core import UnsupportedSurfaceTypeException
from sverchok.utils.surface import SvSurface, SurfaceCurvatureCalculator, SurfaceDerivativesData
from sverchok.utils.logging import info
from sverchok.utils.logging import info, getLogger
from sverchok.data_structure import repeat_last_for_length
from sverchok.dependencies import geomdl

Expand Down Expand Up @@ -249,7 +249,7 @@ def elevate_degree(self, direction, delta=None, target=None):
self.get_knotvector_u(), fixed_u_knotvector,
new_points, new_weights)

def reduce_degree(self, direction, delta=None, target=None, tolerance=1e-6):
def reduce_degree(self, direction, delta=None, target=None, tolerance=1e-6, logger=None):
if delta is None and target is None:
delta = 1
if delta is not None and target is not None:
Expand All @@ -265,6 +265,9 @@ def reduce_degree(self, direction, delta=None, target=None, tolerance=1e-6):
if delta == 0:
return self

if logger is None:
logger = getLogger()

implementation = self.get_nurbs_implementation()

if direction == SvNurbsSurface.U:
Expand All @@ -280,7 +283,7 @@ def reduce_degree(self, direction, delta=None, target=None, tolerance=1e-6):
self.get_degree_u(), self.get_knotvector_u(),
fixed_v_points, fixed_v_weights)
try:
fixed_v_curve, error = fixed_v_curve.reduce_degree(delta=delta, tolerance=remaining_tolerance, return_error=True)
fixed_v_curve, error = fixed_v_curve.reduce_degree(delta=delta, tolerance=remaining_tolerance, return_error=True, logger=logger)
except CantReduceDegreeException as e:
raise CantReduceDegreeException(f"At parallel #{i}: {e}") from e
max_error = max(max_error, error)
Expand All @@ -294,7 +297,7 @@ def reduce_degree(self, direction, delta=None, target=None, tolerance=1e-6):
new_points = np.transpose(np.array(new_points), axes=(1,0,2))
new_weights = np.array(new_weights).T

print(f"Surface degree reduction error: {max_error}")
logger.debug(f"Surface degree reduction error: {max_error}")

return SvNurbsSurface.build(self.get_nurbs_implementation(),
new_u_degree, self.get_degree_v(),
Expand All @@ -314,7 +317,7 @@ def reduce_degree(self, direction, delta=None, target=None, tolerance=1e-6):
self.get_degree_v(), self.get_knotvector_v(),
fixed_u_points, fixed_u_weights)
try:
fixed_u_curve, error = fixed_u_curve.reduce_degree(delta=delta, tolerance=remaining_tolerance, return_error=True)
fixed_u_curve, error = fixed_u_curve.reduce_degree(delta=delta, tolerance=remaining_tolerance, return_error=True, logger=logger)
except CantReduceDegreeException as e:
raise CantReduceDegreeException(f"At parallel #{i}: {e}") from e
max_error = max(max_error, error)
Expand All @@ -328,7 +331,7 @@ def reduce_degree(self, direction, delta=None, target=None, tolerance=1e-6):
new_points = np.array(new_points)
new_weights = np.array(new_weights)

print(f"Surface degree reduction error: {max_error}")
logger.debug(f"Surface degree reduction error: {max_error}")

return SvNurbsSurface.build(implementation,
self.get_degree_u(), new_v_degree,
Expand Down

0 comments on commit 144fe26

Please sign in to comment.