{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "display_name": "OneSimulation (venv)",
   "language": "python",
   "name": "onesim"
  },
  "language_info": {
   "name": "python"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Wrapping a CAD geometry\n",
    "\n",
    "Lettering on a bottle, a grip pattern on a handle, a heating track on a pipe, a label strip on a\n",
    "shaft: a flat outline is laid onto a curved face so that its lengths are kept. On a cylinder or a\n",
    "cone the face can be unrolled into the plane; the outline is drawn there and rolled up with the\n",
    "face. On a sphere, which cannot be unrolled, distances from the anchor point are measured along\n",
    "the surface (geodesic polar coordinates).\n",
    "\n",
    "`part.wrap(profile, face, height, at=point)` wraps the profile onto one face around the point\n",
    "`at` and raises it by `height` (sinks it for a negative height). The faces of the wrapped\n",
    "feature form the group `wrap`. For comparison, `part.emboss(...)` projects the profile straight\n",
    "onto the face, which stretches it where the face turns away.\n",
    "\n",
    "**Reference.** A strip of width $w$ (around) and height $h$ (along) on a cylinder of radius $R$\n",
    "covers the angle $w/R$. Raised by $t$ it adds $V=\\tfrac12\\,\\tfrac{w}{R}\\,\\big((R+t)^2-R^2\\big)\\,h$,\n",
    "sunk by $t$ it removes $\\tfrac12\\,\\tfrac{w}{R}\\,\\big(R^2-(R-t)^2\\big)\\,h$. A cone unrolls without\n",
    "stretching, so a patch covers exactly its own area on the side face. A circle of radius $\\rho$ on a\n",
    "sphere of radius $R_s$ covers the cap $2\\pi R_s^2\\,(1-\\cos(\\rho/R_s))$, not the projected\n",
    "$\\pi\\rho^2$."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "from onesim.cad.d2 import Profile\n",
    "from onesim.cad.d3 import Cone, Cylinder, Sphere\n",
    "from onesim.meshing import TetrahedralMesher\n",
    "from onesim.meshing.quality import quality\n",
    "from onesim.visualization import plots, theme\n",
    "\n",
    "theme.set()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A label strip and lettering on a shaft\n",
    "\n",
    "A shaft of 20 mm radius and 60 mm length. A strip of 30 x 10 mm is wrapped around it across the\n",
    "seam of the wall and raised by 2 mm; the same strip sunk by 2 mm; and the letters \"ONE\", 12 mm\n",
    "high, raised by 1 mm on the other side."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "radius, length, width, height, rise = 0.02, 0.06, 0.03, 0.01, 0.002\n",
    "shaft = Cylinder(radius=radius, length=length)\n",
    "strip = Profile.rectangle(width, height, origin=(-width / 2, -height / 2))\n",
    "raised = shaft.wrap(\n",
    "    strip, shaft.boundaries.outer_wall, rise, at=(radius, 0.0, length / 2)\n",
    ")\n",
    "sunk = shaft.wrap(\n",
    "    strip, shaft.boundaries.outer_wall, -rise, at=(radius, 0.0, length / 2)\n",
    ")\n",
    "projected = shaft.emboss(\n",
    "    strip, shaft.boundaries.outer_wall, rise, at=(radius, 0.0, length / 2)\n",
    ")\n",
    "letters = Profile.text(\"ONE\", 0.012)\n",
    "lettered = raised.wrap(\n",
    "    letters, raised.boundaries.outer_wall, 0.001, at=(-radius, 0.0, length / 2)\n",
    ")\n",
    "\n",
    "added = 0.5 * (width / radius) * ((radius + rise) ** 2 - radius**2) * height\n",
    "removed = 0.5 * (width / radius) * (radius**2 - (radius - rise) ** 2) * height\n",
    "lettering = (\n",
    "    sum(item.area for item in letters)\n",
    "    * ((radius + 0.001) ** 2 - radius**2)\n",
    "    / (2 * radius)\n",
    ")\n",
    "print(\n",
    "    f\"raised strip:  {(raised.measure - shaft.measure) * 1e9:9.3f} mm3 added,   closed form {added * 1e9:9.3f} mm3\"\n",
    ")\n",
    "print(\n",
    "    f\"sunk strip:    {(shaft.measure - sunk.measure) * 1e9:9.3f} mm3 removed, closed form {removed * 1e9:9.3f} mm3\"\n",
    ")\n",
    "print(\n",
    "    f\"projected:     {(projected.measure - shaft.measure) * 1e9:9.3f} mm3 added ({(projected.measure - shaft.measure) / added - 1:+.1%}: \"\n",
    "    f\"the projection stretches the strip)\"\n",
    ")\n",
    "print(\n",
    "    f\"lettering:     {(lettered.measure - raised.measure) * 1e9:9.3f} mm3 added,   closed form {lettering * 1e9:9.3f} mm3\"\n",
    ")\n",
    "print(\n",
    "    f\"faces of the wrapped strip: {len(raised.boundaries.wrap.members)}; lettered shaft: {len(lettered.faces)} faces\"\n",
    ")\n",
    "\n",
    "mesh = TetrahedralMesher(target_size=0.004).generate(lettered)\n",
    "view = plots.Camera(elevation=15, azimuth=150)\n",
    "fig = plots.figure(1, 2, size=(16, 7))\n",
    "plots.geometry(\n",
    "    lettered,\n",
    "    selection={\"wrapped strip and letters\": lettered.boundaries.wrap},\n",
    "    labels=False,\n",
    "    camera=view,\n",
    "    title=\"Shaft with a wrapped strip and lettering\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.mesh(\n",
    "    mesh,\n",
    "    groups=(\"all\",),\n",
    "    legend=False,\n",
    "    camera=view,\n",
    "    title=f\"Mesh: {mesh.cell_count} tetrahedra\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## On a cone and on a sphere\n",
    "\n",
    "A patch of 20 x 12 mm is sunk 1 mm into the side of a cone (30 mm to 15 mm radius, 50 mm high);\n",
    "a disc of 12 mm radius is raised 1 mm on a ball of 30 mm radius."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "cone = Cone(bottom_radius=0.03, top_radius=0.015, height=0.05)\n",
    "grooved = cone.wrap(\n",
    "    Profile.rectangle(0.02, 0.012, origin=(-0.01, -0.006)), cone.boundaries.side, -0.001\n",
    ")\n",
    "ball = Sphere(radius=0.03)\n",
    "skin = ball.faces[0].id\n",
    "reach = 0.012\n",
    "capped = ball.wrap(\n",
    "    Profile.circle(reach), ball.boundaries[skin], 0.001, at=(0.03, 0.0, 0.0)\n",
    ")\n",
    "covered = ball.topology.face(skin).area - capped.topology.face(skin).area\n",
    "print(\n",
    "    f\"cone: side face area covered {(cone.topology.face('side').area - grooved.topology.face('side').area) * 1e6:.4f} mm2, \"\n",
    "    f\"patch area {0.02 * 0.012 * 1e6:.4f} mm2\"\n",
    ")\n",
    "print(\n",
    "    f\"sphere: cap covered {covered * 1e6:.4f} mm2, geodesic circle {2 * np.pi * 0.03**2 * (1 - np.cos(reach / 0.03)) * 1e6:.4f} mm2, \"\n",
    "    f\"flat circle {np.pi * reach**2 * 1e6:.4f} mm2\"\n",
    ")\n",
    "\n",
    "cone_mesh = TetrahedralMesher(target_size=0.004).generate(grooved)\n",
    "ball_mesh = TetrahedralMesher(target_size=0.004).generate(capped)\n",
    "for name, body, mesh in ((\"cone\", grooved, cone_mesh), (\"sphere\", capped, ball_mesh)):\n",
    "    print(\n",
    "        f\"{name}: {mesh.cell_count} cells, mesh volume {quality(mesh)['size'].sum() * 1e6:.4f} cm3, CAD {body.measure * 1e6:.4f} cm3\"\n",
    "    )"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Figure\n",
    "\n",
    "The raised and the sunk strip against the projected one, and the wrapped cone and sphere."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "fig = plots.figure(2, 2, size=(16, 12))\n",
    "plots.render(\n",
    "    raised,\n",
    "    camera=plots.Camera(elevation=15, azimuth=0),\n",
    "    material=\"aluminium\",\n",
    "    title=\"Strip wrapped and raised by 2 mm\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.render(\n",
    "    sunk,\n",
    "    camera=plots.Camera(elevation=15, azimuth=0),\n",
    "    material=\"aluminium\",\n",
    "    title=\"Strip wrapped and sunk by 2 mm\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.geometry(\n",
    "    grooved,\n",
    "    selection={\"wrapped patch\": grooved.boundaries.wrap},\n",
    "    labels=False,\n",
    "    camera=plots.Camera(elevation=20, azimuth=-30),\n",
    "    title=\"Patch sunk into the side of a cone\",\n",
    "    ax=fig[2],\n",
    ")\n",
    "plots.render(\n",
    "    capped,\n",
    "    camera=plots.Camera(elevation=10, azimuth=20),\n",
    "    material=\"ceramic\",\n",
    "    title=\"Disc raised by 1 mm on a sphere\",\n",
    "    ax=fig[3],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Going further\n",
    "\n",
    "Wrap a grip pattern of many small profiles (`Profile.slot`) around a handle, wrap along the axis\n",
    "with `direction=(0, 0, 1)`, or use the wrapped faces as a heated surface in a thermal model."
   ]
  }
 ]
}
