{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "An example showing nearest point queries, sampling the\n",
    "volume of box primitives generated from the oriented bounds\n",
    "and using PointCloud objects for visualization.\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 large- ish PLY model with colors\n",
    "mesh = trimesh.load(\"../models/cycloidal.ply\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# we can sample the volume of Box primitives\n",
    "points = mesh.bounding_box_oriented.sample_volume(count=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# find the closest point on the mesh to each random point\n",
    "(closest_points, distances, triangle_id) = mesh.nearest.on_surface(points)\n",
    "# distance from point to surface of meshdistances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a PointCloud object out of each (n,3) list of points\n",
    "cloud_original = trimesh.points.PointCloud(points)\n",
    "cloud_close = trimesh.points.PointCloud(closest_points)\n",
    "\n",
    "# create a unique color for each point\n",
    "cloud_colors = np.array([trimesh.visual.random_color() for i in points])\n",
    "\n",
    "# set the colors on the random point and its nearest point to be the same\n",
    "cloud_original.vertices_color = cloud_colors\n",
    "cloud_close.vertices_color = cloud_colors\n",
    "\n",
    "# create a scene containing the mesh and two sets of points\n",
    "scene = trimesh.Scene([mesh, cloud_original, cloud_close])\n",
    "\n",
    "# show the scene we are using\n",
    "scene.show()"
   ]
  }
 ],
 "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": 2
}
