{ "cells": [ { "cell_type": "markdown", "id": "0cd50cf6", "metadata": {}, "source": [ "# Some Basics" ] }, { "cell_type": "code", "execution_count": 1, "id": "ff42de68", "metadata": { "execution": { "iopub.execute_input": "2026-08-05T19:41:39.860650Z", "iopub.status.busy": "2026-08-05T19:41:39.860445Z", "iopub.status.idle": "2026-08-05T19:41:43.596215Z", "shell.execute_reply": "2026-08-05T19:41:43.595350Z" } }, "outputs": [], "source": [ "import os\n", "import sys\n", "\n", "sys.path.insert(0, os.path.abspath(\"..\"))\n", "\n", "import numpy as np\n", "\n", "import geoclide as gc" ] }, { "cell_type": "markdown", "id": "01cc681b", "metadata": {}, "source": [ "## Create a point and a vector" ] }, { "cell_type": "code", "execution_count": 2, "id": "0e2af292", "metadata": { "execution": { "iopub.execute_input": "2026-08-05T19:41:43.598239Z", "iopub.status.busy": "2026-08-05T19:41:43.597893Z", "iopub.status.idle": "2026-08-05T19:41:43.603320Z", "shell.execute_reply": "2026-08-05T19:41:43.602485Z" } }, "outputs": [ { "data": { "text/plain": [ "(Point(0.0, 0.0, 0.0),\n", " Vector(0.7001400420140049, 0.7001400420140049, 0.140028008402801))" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 = gc.Point(0., 0., 0.) # create a point\n", "# create a vector and normalize it\n", "v1 = gc.normalize(gc.Vector(0.5, 0.5, 0.1))\n", "p1, v1" ] }, { "cell_type": "code", "execution_count": 3, "id": "0cf0d279", "metadata": { "execution": { "iopub.execute_input": "2026-08-05T19:41:43.604743Z", "iopub.status.busy": "2026-08-05T19:41:43.604571Z", "iopub.status.idle": "2026-08-05T19:41:43.607945Z", "shell.execute_reply": "2026-08-05T19:41:43.607320Z" } }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v1.length()" ] }, { "cell_type": "markdown", "id": "e72a051a", "metadata": {}, "source": [ "## Create a ray from the created point and vector" ] }, { "cell_type": "code", "execution_count": 4, "id": "570c70eb", "metadata": { "execution": { "iopub.execute_input": "2026-08-05T19:41:43.609688Z", "iopub.status.busy": "2026-08-05T19:41:43.609528Z", "iopub.status.idle": "2026-08-05T19:41:43.612786Z", "shell.execute_reply": "2026-08-05T19:41:43.612102Z" } }, "outputs": [ { "data": { "text/plain": [ "r(t) = (0.0, 0.0, 0.0) + t*(0.7001400420140049, 0.7001400420140049, 0.140028008402801) with t ∈ [0,inf[" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r1 = gc.Ray(o=p1, d=v1)\n", "r1" ] }, { "cell_type": "code", "execution_count": 5, "id": "2908e3f8", "metadata": { "execution": { "iopub.execute_input": "2026-08-05T19:41:43.614273Z", "iopub.status.busy": "2026-08-05T19:41:43.614088Z", "iopub.status.idle": "2026-08-05T19:41:43.617405Z", "shell.execute_reply": "2026-08-05T19:41:43.616698Z" } }, "outputs": [ { "data": { "text/plain": [ "Point(5.601120336112039, 5.601120336112039, 1.120224067222408)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r1(8)" ] }, { "cell_type": "markdown", "id": "8b901d23", "metadata": {}, "source": [ "## Create a simple triangle mesh composed of 2 triangles" ] }, { "cell_type": "code", "execution_count": 6, "id": "a02102b5", "metadata": { "execution": { "iopub.execute_input": "2026-08-05T19:41:43.618918Z", "iopub.status.busy": "2026-08-05T19:41:43.618751Z", "iopub.status.idle": "2026-08-05T19:41:43.644598Z", "shell.execute_reply": "2026-08-05T19:41:43.643854Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 865B\n",
       "Dimensions:          (xyz: 3, nvertices: 4, ntriangles: 2, p0p1p2: 3, dim_0: 4,\n",
       "                      dim_1: 4)\n",
       "Coordinates:\n",
       "  * xyz              (xyz) int64 24B 0 1 2\n",
       "Dimensions without coordinates: nvertices, ntriangles, p0p1p2, dim_0, dim_1\n",
       "Data variables: (12/18)\n",
       "    o                (xyz) float64 24B 0.0 0.0 0.0\n",
       "    d                (xyz) float64 24B 0.7001 0.7001 0.14\n",
       "    mint             int64 8B 0\n",
       "    maxt             float64 8B inf\n",
       "    is_intersection  bool 1B True\n",
       "    thit             float64 8B 3.571\n",
       "    ...               ...\n",
       "    vertices         (nvertices, xyz) float64 96B -5.0 -5.0 0.0 ... 5.0 5.0 0.0\n",
       "    faces            (ntriangles, p0p1p2) int64 48B 0 1 2 2 3 1\n",
       "    wto_m            (dim_0, dim_1) float64 128B 6.123e-17 0.0 1.0 ... 0.0 1.0\n",
       "    wto_m_inv        (dim_0, dim_1) float64 128B 6.123e-17 0.0 -1.0 ... 0.0 1.0\n",
       "    otw_m            (dim_0, dim_1) float64 128B 6.123e-17 0.0 -1.0 ... 0.0 1.0\n",
       "    otw_m_inv        (dim_0, dim_1) float64 128B 6.123e-17 0.0 1.0 ... 0.0 1.0\n",
       "Attributes:\n",
       "    shape:       TriangleMesh\n",
       "    date:        2026-08-05\n",
       "    version:     4.0.0\n",
       "    ntriangles:  2\n",
       "    nvertices:   4
" ], "text/plain": [ " Size: 865B\n", "Dimensions: (xyz: 3, nvertices: 4, ntriangles: 2, p0p1p2: 3, dim_0: 4,\n", " dim_1: 4)\n", "Coordinates:\n", " * xyz (xyz) int64 24B 0 1 2\n", "Dimensions without coordinates: nvertices, ntriangles, p0p1p2, dim_0, dim_1\n", "Data variables: (12/18)\n", " o (xyz) float64 24B 0.0 0.0 0.0\n", " d (xyz) float64 24B 0.7001 0.7001 0.14\n", " mint int64 8B 0\n", " maxt float64 8B inf\n", " is_intersection bool 1B True\n", " thit float64 8B 3.571\n", " ... ...\n", " vertices (nvertices, xyz) float64 96B -5.0 -5.0 0.0 ... 5.0 5.0 0.0\n", " faces (ntriangles, p0p1p2) int64 48B 0 1 2 2 3 1\n", " wto_m (dim_0, dim_1) float64 128B 6.123e-17 0.0 1.0 ... 0.0 1.0\n", " wto_m_inv (dim_0, dim_1) float64 128B 6.123e-17 0.0 -1.0 ... 0.0 1.0\n", " otw_m (dim_0, dim_1) float64 128B 6.123e-17 0.0 -1.0 ... 0.0 1.0\n", " otw_m_inv (dim_0, dim_1) float64 128B 6.123e-17 0.0 1.0 ... 0.0 1.0\n", "Attributes:\n", " shape: TriangleMesh\n", " date: 2026-08-05\n", " version: 4.0.0\n", " ntriangles: 2\n", " nvertices: 4" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v0 = np.array([-5, -5, 0.])\n", "v1 = np.array([5, -5, 0.])\n", "v2 = np.array([-5, 5, 0.])\n", "v3 = np.array([5, 5, 0.])\n", "vertices = np.array([v0, v1, v2, v3])\n", "f0 = np.array([0, 1, 2]) # the vertices indices of triangle 0 / face 0\n", "f1 = np.array([2, 3, 1]) # the vertices indices of triangle 1 / face 1\n", "faces = np.array([f0, f1])\n", "# We can create a transformation to translate and rotate it\n", "# translation of 2.5 in x axis\n", "translate = gc.get_translate_tf(gc.Vector(2.5, 0., 0.))\n", "# rotation of -90 degrees around the y axis\n", "rotate = gc.get_rotate_y_tf(-90.)\n", "# object to world transformation to apply to the triangle mesh\n", "otw = translate*rotate\n", "# create the triangle mesh\n", "tri_mesh = gc.TriangleMesh(vertices, faces, otw=otw)\n", "# see if the ray r1 intersect the triangle mesh\n", "ds = gc.calc_intersection(tri_mesh, r1)\n", "ds" ] }, { "cell_type": "code", "execution_count": 7, "id": "d7295ca6", "metadata": { "execution": { "iopub.execute_input": "2026-08-05T19:41:43.646103Z", "iopub.status.busy": "2026-08-05T19:41:43.645942Z", "iopub.status.idle": "2026-08-05T19:41:43.651393Z", "shell.execute_reply": "2026-08-05T19:41:43.650677Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray 'phit' (xyz: 3)> Size: 24B\n",
       "array([2.5, 2.5, 0.5])\n",
       "Coordinates:\n",
       "  * xyz      (xyz) int64 24B 0 1 2\n",
       "Attributes:\n",
       "    type:         Point\n",
       "    description:  the x, y and z components of the intersection point
" ], "text/plain": [ " Size: 24B\n", "array([2.5, 2.5, 0.5])\n", "Coordinates:\n", " * xyz (xyz) int64 24B 0 1 2\n", "Attributes:\n", " type: Point\n", " description: the x, y and z components of the intersection point" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds['phit']" ] } ], "metadata": { "kernelspec": { "display_name": "geoclide-py313", "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.14.6" } }, "nbformat": 4, "nbformat_minor": 5 }