{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "A simple example showing various properties of `Trimesh` objects.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "import trimesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load a file by name or from a buffer\n",
    "mesh = trimesh.load_mesh(\"../models/featuretype.STL\")\n",
    "# to keep the raw data intact, disable any automatic processing\n",
    "# mesh = trimesh.load_mesh('../models/featuretype.STL', process=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# is the current mesh watertight?\n",
    "mesh.is_watertight"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# what's the euler number for the mesh?\n",
    "mesh.euler_number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the convex hull is another Trimesh object that is available as a property\n",
    "# lets compare the volume of our mesh with the volume of its convex hull\n",
    "np.divide(mesh.volume, mesh.convex_hull.volume)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# since the mesh is watertight, it means there is a\n",
    "# volumetric center of mass which we can set as the origin for our mesh\n",
    "mesh.vertices -= mesh.center_mass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# what's the moment of inertia for the mesh?\n",
    "mesh.moment_inertia"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# if there are multiple bodies in the mesh we can split the mesh by\n",
    "# connected components of face adjacency\n",
    "# since this example mesh is a single watertight body we get a list of one mesh\n",
    "mesh.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# preview mesh in a pyglet window from a terminal, or inline in a notebook\n",
    "mesh.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# facets are groups of coplanar adjacent faces\n",
    "# set each facet to a random color\n",
    "# colors are 8 bit RGBA by default (n,4) np.uint8\n",
    "for facet in mesh.facets:\n",
    "    mesh.visual.face_colors[facet] = trimesh.visual.random_color()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# transform method can be passed a (4,4) matrix and will cleanly apply the transform\n",
    "mesh.apply_transform(trimesh.transformations.random_rotation_matrix())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# an axis aligned bounding box is available\n",
    "mesh.bounding_box.primitive.extents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# a minimum volume oriented bounding box is available\n",
    "mesh.bounding_box_oriented.primitive.extents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mesh.bounding_box_oriented.primitive.transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the bounding box is a trimesh.primitives.Box object, which subclasses\n",
    "# Trimesh and lazily evaluates to fill in vertices and faces when requested\n",
    "mesh.bounding_box_oriented.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# bounding spheres and bounding cylinders of meshes are also\n",
    "# available, and will be the minimum volume version of each\n",
    "# except in certain degenerate cases, where they will be no worse\n",
    "# than a least squares fit version of the primitive.\n",
    "(\n",
    "    mesh.bounding_box_oriented.volume,\n",
    "    mesh.bounding_cylinder.volume,\n",
    "    mesh.bounding_sphere.volume,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
