{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "An example showing simple ray-mesh queries.\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": [
    "# test on a sphere primitive\n",
    "mesh = trimesh.creation.icosphere()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create some rays\n",
    "ray_origins = np.array([[0, 0, -3], [2, 2, -3]])\n",
    "ray_directions = np.array([[0, 0, 1], [0, 0, 1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# check out the docstring for intersects_location queries\n",
    "mesh.ray.intersects_location.__doc__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# run the mesh- ray query\n",
    "locations, index_ray, index_tri = mesh.ray.intersects_location(\n",
    "    ray_origins=ray_origins, ray_directions=ray_directions\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the rays hit the mesh at coordinateslocations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the rays with index_ray hit the triangles stored at mesh.faces[index_tri]len(index_ray)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# stack rays into line segments for visualization as Path3D\n",
    "ray_visualize = trimesh.load_path(\n",
    "    np.hstack((ray_origins, ray_origins + ray_directions * 5.0)).reshape(-1, 2, 3)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# unmerge so viewer doesn't smooth\n",
    "mesh.unmerge_vertices()\n",
    "# make mesh white- ish\n",
    "mesh.visual.face_colors = [255, 255, 255, 255]\n",
    "mesh.visual.face_colors[index_tri] = [255, 0, 0, 255]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a visualization scene with rays, hits, and mesh\n",
    "scene = trimesh.Scene([mesh, ray_visualize])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# show the visualization\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
}
