dasbob/case/to_scad.sh
2024-12-02 17:35:39 +01:00

84 lines
2.6 KiB
Bash

#!/bin/bash
points="$(cat dasbob-Edge_Cuts.svg | grep '<path d=' | sed -e 's/^.*"\(.*\)".*$/\1/' | tr -d '[:alpha:]' | awk '{{ print $1":"$2":"$3":"$4 }}')"
path="$(echo "$points" | head -n 1 | sed -e's/:.*$//')"
one=1000000
function to_fixed() {
echo "$(( $(echo "$1" | sed -e's/\..*$//') ))$(echo "$1" | sed -e's/^.*\.//')"
}
function to_float() {
printf "%07d" "$1" | sed -e 's/^\([0-9]*\)\([0-9][0-9][0-9][0-9][0-9][0-9]\)/\1.\2/'
}
function to_point() {
x="$(to_fixed $(get_x "$1"))"
y="$(to_fixed $(get_y "$1"))"
echo "$x,$y"
}
function to_str() {
x="$(to_float $(get_x "$1"))"
y="$(to_float $(get_y "$1"))"
echo "$x,$y"
}
function get_x() {
echo "$1" | sed -e's/,.*$//'
}
function get_y() {
echo "$1" | sed -e's/^.*,//'
}
function cubic_bezier_curve() {
p_s="$(to_point $1)"
p_a="$(to_point $(echo "$2" | sed -e's/:.*$//'))"
p_b="$(to_point $(echo "$2" | sed -e's/^.*:\([^:]*\):.*$/\1/'))"
p_e="$(to_point $(echo "$2" | sed -e's/^.*://'))"
for t in $(seq $(( $one / 10 )) $(( $one / 10 )) $(( $one - 1)))
do
# B(t)=(1-t)^{3} * P_{0}+3(1-t)^{2}t * P_{1}+3(1-t)t^{2} * P_{2}+t^{3} * P_{3}
one_min_t=$(( $one - $t ))
b_a_x=$(( $one_min_t * $one_min_t * $one_min_t / $one / $one * $(get_x $p_s) / $one ))
b_a_y=$(( $one_min_t * $one_min_t * $one_min_t / $one / $one * $(get_y $p_s) / $one ))
b_b_x=$(( 3 * $one_min_t * $one_min_t * $t / $one / $one * $(get_x $p_a) / $one ))
b_b_y=$(( 3 * $one_min_t * $one_min_t * $t / $one / $one * $(get_y $p_a) / $one ))
b_c_x=$(( 3 * $one_min_t * $t * $t / $one / $one * $(get_x $p_b) / $one ))
b_c_y=$(( 3 * $one_min_t * $t * $t / $one / $one * $(get_y $p_b) / $one ))
b_d_x=$(( $t * $t * $t / $one / $one * $(get_x $p_e) / $one ))
b_d_y=$(( $t * $t * $t / $one / $one * $(get_y $p_e) / $one ))
x=$(( b_a_x + b_b_x + b_c_x + b_d_x ))
y=$(( b_a_y + b_b_y + b_c_y + b_d_y ))
to_str "$x,$y" | tr -d "\n\r"
echo -n ":"
done
to_str "$p_e" | tr -d "\n\r"
}
echo "module dasbob() {"
for i in $(seq 0 $(( $(echo "$points" | wc -l) + 2 )))
do
last="$(echo "$path" | sed -e 's/^.*://')"
next="$(echo "$points" | grep "^${last}:" | sed -e's/^[^:]*://')"
if [ -z "$next" ]; then
echo -e "\tpolygon(\n\t\t[\n\t\t\t[$path]\n\t\t]\n\t);" | sed -e 's/:/],\n\t\t\t[/g'
path="$(echo "$points" | grep -v '^u ' | head -n 1 | sed -e's/:.*$//')"
else
if [ "$(echo "$next" | sed -e 's/:.*$//')" = "$last" ]
then
# add line
path="${path}:$(echo "$next" | sed -e's/^.*://')"
else
# add curve
path="${path}:$(cubic_bezier_curve $last $next)"
fi
fi
points="$(echo "$points" | sed -e "s/^${last}:.*$/u \0/")"
done
echo "}"