Skip to content

Instantly share code, notes, and snippets.

@jenskutilek
Last active May 8, 2024 21:53
Show Gist options
  • Save jenskutilek/f328f2ca250e8b9ebad0fb398a5a64c1 to your computer and use it in GitHub Desktop.
Save jenskutilek/f328f2ca250e8b9ebad0fb398a5a64c1 to your computer and use it in GitHub Desktop.
Axis variation mapping generator for the slant axis
#!/usr/bin/env python
# coding: utf-8
from __future__ import division, print_function
from math import atan, pi, tan
# Spit out some mappings for the slant axis to get closer to actual degrees.
#print(angle,
# atan(
# tan(slant * pi / 180) / (slant / angle)
# ) * 180 / pi
#)
"""
MIT License
Copyright (c) 2017-2022 Jens Kutilek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
start_angle = -15
end_angle = 0
step = 0.1
tolerance = 0.04
DEBUG = False
def map_angle(phi, ref_angle):
return ref_angle * tan(pi * phi / 180) / tan(pi * ref_angle / 180)
def map_angle_back(phi, ref_angle):
return atan(tan(ref_angle * pi / 180) / (ref_angle / phi)) * 180 / pi
def bisect(t0, t1, ref_angle):
if t0 == t1:
return [t0]
start, mapped_start = t0
end, mapped_end = t1
# The linearly interpolated exact angle
actual_angle = (start + end) / 2
# The linearly interpolated mapped angle
half_average = (mapped_start + mapped_end) / 2
# The half angle when it would be exactly mapped
half_mapping = map_angle(actual_angle, ref_angle)
backward_mapped = map_angle_back(half_average, ref_angle)
if DEBUG:
print(" Exact angle: %0.2f" % actual_angle)
print(" Exactly mapped angle: %0.2f" % half_mapping)
print(" Mapped interpolated: %0.2f" % half_average)
print(" Interpolated backwards: %0.2f" % backward_mapped)
diff = abs(actual_angle - backward_mapped)
if diff < tolerance:
if DEBUG:
print(" Below abs. tolerance: %0.2f - %0.2f = %0.2f ? %0.2f" % (actual_angle, backward_mapped, diff, tolerance))
return [t0]
else:
if DEBUG:
print(" Adding mapping: %0.2f -> %0.2f (d = %0.2f)" % (actual_angle, half_mapping, abs(actual_angle - half_mapping)))
return [t0, (actual_angle, half_mapping)]
final_mappings = []
mappings = [(start_angle, start_angle), (0.0, 0.0)]
while True:
new_mappings = []
for i in range(0, len(mappings) - 1):
t0 = mappings[i]
t1 = mappings[i + 1]
new_mappings.extend(bisect(t0, t1, start_angle))
new_mappings.append(mappings[-1])
if len(new_mappings) == len(mappings):
final_mappings.extend(mappings)
final_mappings.pop()
break
else:
mappings = new_mappings
mappings = [(0.0, 0.0), (end_angle, end_angle)]
while True:
new_mappings = []
for i in range(0, len(mappings) - 1):
t0 = mappings[i]
t1 = mappings[i + 1]
new_mappings.extend(bisect(t0, t1, end_angle))
new_mappings.append(mappings[-1])
if len(new_mappings) == len(mappings):
final_mappings.extend(mappings)
break
else:
mappings = new_mappings
for mapping in sorted(set(final_mappings)):
print(' <map input="%0.6f" output="%0.6f" />' % mapping)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment