{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "# 06. Meshing and Discretisation\n\nTriangulate a rectangle and inspect the indexed mesh of a box. This example avoids an external meshing engine.\n\n**Goal:** Complete one small workflow. Run the cells from top to bottom in a fresh kernel.\n\n**Extra dependencies:** None beyond the base environment.\n\nAll examples use metres unless stated otherwise.\n",
   "id": "tutorial-06-00"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "import os\nos.environ[\"TOPOLOGICPY_CORE_BACKEND\"] = \"pythonocc\"\n\nimport topologicpy\nfrom topologicpy.Core import Core\n\nassert topologicpy.__version__ == \"0.9.86\", \"Use TopologicPy 0.9.86 for these tutorials.\"\nassert type(Core.Backend()).__name__ == \"PythonOCCBackend\", \"Restart with the pythonocc backend.\"\nprint(\"TopologicPy\", topologicpy.__version__, \"—\", type(Core.Backend()).__name__)\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-06-01"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Split a face into triangles\n\nTriangulation replaces a face with triangular faces. Their total area should equal the original area.\n",
   "id": "tutorial-06-02"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "from topologicpy.Face import Face\nfrom topologicpy.Cell import Cell\nfrom topologicpy.Topology import Topology\n\nrectangle = Face.Rectangle(width=4, length=3)\ntriangles = Face.Triangulate(rectangle)\ntotal_area = 0\nfor triangle in triangles:\n    total_area = total_area + Face.Area(triangle)\nprint(\"Triangles:\", len(triangles), \"Total area:\", total_area)\nassert abs(total_area - Face.Area(rectangle)) < 0.0001\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-06-03"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Inspect an indexed mesh\n\nFaces contain indices into the vertex list. This compact representation is useful for display and exchange; it does not preserve all of the original analytical geometry.\n",
   "id": "tutorial-06-04"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "box = Cell.Prism(width=4, length=3, height=2)\nmesh = Topology.MeshData(box)\nprint(\"Mesh vertices:\", len(mesh[\"vertices\"]))\nprint(\"Mesh faces:\", len(mesh[\"faces\"]))\nprint(\"First face indices:\", mesh[\"faces\"][0])\nassert len(mesh[\"faces\"]) > 0\nfor face in mesh[\"faces\"]:\n    assert len(face) == 3\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-06-05"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Try it yourself\n\nChange the rectangle dimensions. Does triangulation still preserve its area?\n",
   "id": "tutorial-06-06"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (TopologicPy tutorials)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
