{ "cells": [ { "cell_type": "markdown", "id": "6f2aeb62", "metadata": {}, "source": [ "# Cloud vertices, What is it ?\n", "\n", "## Basics \n", "\n", "Cloud vertices is a synonym for point cloud. It is a group of points in a 3D space.\n", "\n", "It can be initialized from:\n", "\n", "- a list of vertices\n", "- a numpy array (shape (N, 3))\n", "- an Excel file\n", "- a CSV file (with/without header)\n", "- a Shape file (point type)\n", "- a DXF file (MTEXT or INSERT)\n", "\n", "Each point can have associated values.\n", "\n", "## Storage\n", "\n", "All vertices are stored in a dictionnary. The main key is the index of the point in the list. \n", "\n", "Each entry is a dictionnary with at least the following key : `vertex` (the `wolf_vertex` instance).\n", "\n", "Other keys can be added to store additional information.\n", "\n", "## KD-tree\n", "\n", "A KD-tree is a data structure that allows for efficient nearest neighbor searches in a multi-dimensional space. It is particularly useful for spatial queries, such as finding the closest points in a point cloud.\n", "\n", "Each instance of `cloud_vertices` has a KD-tree associated with it. The KD-tree is built using the vertices of the point cloud, and it allows for fast nearest neighbor searches.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "72cdbc36", "metadata": {}, "outputs": [], "source": [ "# import _add_path # this line can be deleted if wolfhece is installed as a package (-> debug mode only)\n", "\n", "from wolfhece import __version__\n", "assert __version__ > '2.2.8', f'Bad version of wolfhece: {__version__} <= 2.2.8'\n", "\n", "from wolfhece.PyVertex import cloud_vertices, wolfvertex\n", "\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "d7d31aa5", "metadata": {}, "source": [ "## Create a new cloud_vertices instance\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "1ef57f2b", "metadata": {}, "outputs": [], "source": [ "vertices = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]])\n", "\n", "cloud = cloud_vertices()\n", "\n", "cloud.init_from_nparray(vertices)" ] }, { "cell_type": "code", "execution_count": 3, "id": "bc2cf58f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "False\n", "[]\n" ] } ], "source": [ "# number of vertices\n", "print(cloud.nbvertices)\n", "\n", "# Header information\n", "print(cloud.has_values)\n", "print(cloud.header)" ] }, { "cell_type": "markdown", "id": "a4d6f4dc", "metadata": {}, "source": [ "## Get coordinates of the vertices" ] }, { "cell_type": "code", "execution_count": 4, "id": "856f63b4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 0. 0.]\n", " [1. 1. 1.]\n", " [2. 2. 2.]\n", " [3. 3. 3.]\n", " [4. 4. 4.]]\n" ] } ], "source": [ "xyz = cloud.xyz # -> np.ndarray with shape (n,3)\n", "\n", "print(xyz)" ] }, { "cell_type": "markdown", "id": "3e71f649", "metadata": {}, "source": [ "## Add a value" ] }, { "cell_type": "code", "execution_count": 5, "id": "55190f85", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['vertex', 'test']\n" ] } ], "source": [ "new_val = np.array([1, 2, 3, 4, 5])\n", "\n", "cloud.add_values_by_id_list('test', new_val)\n", "\n", "print(cloud.header)" ] }, { "cell_type": "markdown", "id": "555d0124", "metadata": {}, "source": [ "## Get coordinates but value as z" ] }, { "cell_type": "code", "execution_count": 6, "id": "4713f371", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 0. 1.]\n", " [1. 1. 2.]\n", " [2. 2. 3.]\n", " [3. 3. 4.]\n", " [4. 4. 5.]]\n" ] } ], "source": [ "xyz_val = cloud.get_xyz('test')\n", "\n", "print(xyz_val)" ] }, { "cell_type": "markdown", "id": "e08be8e1", "metadata": {}, "source": [ "## Find the nearest vertex" ] }, { "cell_type": "code", "execution_count": 7, "id": "320212ec", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "distance: 0.7071067811865476\n", "vertex: 0.0 0.0 0.0\n", "key: {'vertex': , 'test': np.int64(1)}\n" ] } ], "source": [ "nb = 1\n", "dist, vert, assoc_dict = cloud.find_nearest([[.5, .5, 0.]], nb= nb)\n", "\n", "# If the number of neighbors is 1, the output is a single list of vertices and distances\n", "# If the number of neighbors is greater than 1, the output is a list of lists of vertices and distances\n", "\n", "print('distance:', dist)\n", "print('vertex:', vert.x, vert.y, vert.z)\n", "print('key:', assoc_dict)" ] }, { "cell_type": "markdown", "id": "69c747ec", "metadata": {}, "source": [ "## Find the nearest vertices\n", "\n", "nb = 2" ] }, { "cell_type": "code", "execution_count": 8, "id": "cdb540cf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of neighbors: 2\n", "distance: 0.7071067811865476\n", "vertex: 0.0 0.0 0.0\n", "key: {'vertex': , 'test': np.int64(1)}\n", "distance: 1.224744871391589\n", "vertex: 1.0 1.0 1.0\n", "key: {'vertex': , 'test': np.int64(2)}\n" ] } ], "source": [ "nb = 2\n", "dist, vert, assoc_dict = cloud.find_nearest(np.asarray([[.5, .5, 0.]]), nb= nb)\n", "\n", "# If the number of neighbors is 1, the output is a single list of vertices and distances\n", "# If the number of neighbors is greater than 1, the output is a list of lists of vertices and distances\n", "\n", "print('number of neighbors:', len(dist))\n", "\n", "for i in range(nb):\n", " print('distance:', dist[i])\n", " print('vertex:', vert[i].x, vert[i].y, vert[i].z)\n", " print('key:', assoc_dict[i])\n" ] }, { "cell_type": "markdown", "id": "d819d9d1", "metadata": {}, "source": [ "## Find the nearest vertex for multiple points" ] }, { "cell_type": "code", "execution_count": 9, "id": "6644100b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "distance: 1.118033988749895\n", "vertex: 1.0 1.0 1.0\n", "key: {'vertex': , 'test': np.int64(2)}\n", "distance: 1.118033988749895\n", "vertex: 0.0 0.0 0.0\n", "key: {'vertex': , 'test': np.int64(1)}\n", "distance: 1.118033988749895\n", "vertex: 1.0 1.0 1.0\n", "key: {'vertex': , 'test': np.int64(2)}\n", "distance: 1.8027756377319946\n", "vertex: 2.0 2.0 2.0\n", "key: {'vertex': , 'test': np.int64(3)}\n" ] } ], "source": [ "nb = 2\n", "xyz1 = [.5, 1., 0.]\n", "xyz2 = [.5, 2., 1.]\n", "dist, vert, assoc_dict = cloud.find_nearest([xyz1, xyz2], nb= nb)\n", "\n", "# If the number of neighbors is 1, the output is a single list of vertices and distances\n", "# If the number of neighbors is greater than 1, the output is a list of lists of vertices and distances\n", "\n", "if nb ==1:\n", " for i_vert in range(2):\n", " print('distance:', dist[i_vert])\n", " print('vertex:', vert[i_vert].x, vert[i_vert].y, vert[i_vert].z)\n", " print('key:', assoc_dict[i_vert])\n", "else:\n", " for i_vert in range(2):\n", " for i in range(nb):\n", " dist_list = dist[i_vert]\n", " vert_list = vert[i_vert]\n", " assoc_dict_list = assoc_dict[i_vert]\n", "\n", " print('distance:', dist_list[i])\n", " print('vertex:', vert_list[i].x, vert_list[i].y, vert_list[i].z)\n", " print('key:', assoc_dict_list[i])" ] } ], "metadata": { "kernelspec": { "display_name": "python3.10", "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.10.11" } }, "nbformat": 4, "nbformat_minor": 5 }