Simplify template code

This commit is contained in:
Chris Greening 2022-10-09 20:56:02 +01:00
parent fcb3b55df7
commit aa9e328944

View file

@ -105,7 +105,8 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"template_f = [\n", "# templates must be simetric around the X axis and must include the center points on both size (e.g. (X1, 0).... (X2, 0) )\n",
"template = [\n",
" (-0.6, 0),\n", " (-0.6, 0),\n",
" (-0.6, -0.6),\n", " (-0.6, -0.6),\n",
" (0.5, -1.2),\n", " (0.5, -1.2),\n",
@ -114,12 +115,7 @@
" (0.95, 0.4),\n", " (0.95, 0.4),\n",
" (0.5, 1.2),\n", " (0.5, 1.2),\n",
" (-0.6, 0.6),\n", " (-0.6, 0.6),\n",
"]\n", "]"
"\n",
"template_b = []\n",
"for i in range(len(template_f)):\n",
" template_b.append(template_f[len(template_f) - i - len(template_f) // 2])\n",
"template_b = flip_x(template_b)"
] ]
}, },
{ {
@ -129,11 +125,9 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# plot the template shape wrapping around to the first point\n", "# plot the template shape wrapping around to the first point\n",
"df = pd.DataFrame(template_f + [template_f[0]], columns=[\"x\", \"y\"])\n", "df = pd.DataFrame(template + [template[0]], columns=[\"x\", \"y\"])\n",
"ax = df.plot.line(x=\"x\", y=\"y\", color=\"blue\")\n", "ax = df.plot.line(x=\"x\", y=\"y\", color=\"blue\")\n",
"ax.axis(\"equal\")\n", "ax.axis(\"equal\")"
"df = pd.DataFrame(template_b + [template_b[0]], columns=[\"x\", \"y\"])\n",
"ax = df.plot.line(x=\"x\", y=\"y\", color=\"red\", ax=ax)"
] ]
}, },
{ {
@ -142,7 +136,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def calculate_point(point, point1, point2, spacing, turn, layer):\n", "def calculate_point(point, point1, point2, spacing, turn):\n",
" reference_vector = Vector([-100, 0])\n", " reference_vector = Vector([-100, 0])\n",
" angle = np.rad2deg(Vector(point).angle_between(reference_vector))\n", " angle = np.rad2deg(Vector(point).angle_between(reference_vector))\n",
" if point[1] > 0:\n", " if point[1] > 0:\n",
@ -157,7 +151,7 @@
" return (coil_point[0], coil_point[1])\n", " return (coil_point[0], coil_point[1])\n",
"\n", "\n",
"\n", "\n",
"def get_points(template, turns, spacing, layer=Layer.FRONT):\n", "def get_points(template, turns, spacing):\n",
" coil_points = []\n", " coil_points = []\n",
" reference_vector = Vector([-100, 0])\n", " reference_vector = Vector([-100, 0])\n",
" template_index = 0\n", " template_index = 0\n",
@ -168,15 +162,10 @@
"\n", "\n",
" # calculate the new positions of the points\n", " # calculate the new positions of the points\n",
" coil_point1 = calculate_point(\n", " coil_point1 = calculate_point(\n",
" point1, point1, point2, spacing, template_index // template_length, layer\n", " point1, point1, point2, spacing, template_index // template_length\n",
" )\n", " )\n",
" coil_point2 = calculate_point(\n", " coil_point2 = calculate_point(\n",
" point2,\n", " point2, point1, point2, spacing, (template_index + 1) // template_length\n",
" point1,\n",
" point2,\n",
" spacing,\n",
" (template_index + 1) // template_length,\n",
" layer,\n",
" )\n", " )\n",
" # add an intermediate point which is the intersection of this new line with the previous line (if there is one)\n", " # add an intermediate point which is the intersection of this new line with the previous line (if there is one)\n",
" if len(coil_points) >= 2:\n", " if len(coil_points) >= 2:\n",
@ -250,19 +239,18 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"if not USE_SPIRAL:\n", "if not USE_SPIRAL:\n",
" template_f = []\n",
" for i in range(len(template)):\n",
" template_f.append(template[len(template) - i - len(template) // 2])\n",
" template_f = flip_x(template_f)\n",
" points_f = chaikin(\n", " points_f = chaikin(\n",
" optimize_points(\n", " optimize_points(\n",
" flip_x(\n", " flip_x(get_points(template_f, TURNS, TRACK_SPACING + TRACK_WIDTH))\n",
" get_points(template_b, TURNS, TRACK_SPACING + TRACK_WIDTH, Layer.FRONT)\n",
" )\n",
" ),\n", " ),\n",
" 2,\n", " 2,\n",
" )\n", " )\n",
" points_b = chaikin(\n", " points_b = chaikin(\n",
" optimize_points(\n", " optimize_points(get_points(template, TURNS, TRACK_SPACING + TRACK_WIDTH)), 2\n",
" get_points(template_f, TURNS, TRACK_SPACING + TRACK_WIDTH, Layer.BACK)\n",
" ),\n",
" 2,\n",
" )\n", " )\n",
"\n", "\n",
" points_f = [(0, 0)] + points_f\n", " points_f = [(0, 0)] + points_f\n",