{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# MeshNode\n", "The meshNode class in Geomapi represents the data and metadata of polygonal mesh data. The data itself and methods build upon Open3D TriangleMesh and Trimesh concepts while the metadata builds upon the RDFlib framework:\n", "\n", "[http://www.open3d.org/docs/latest/tutorial/Basic/mesh.html#](http://www.open3d.org/docs/latest/tutorial/Basic/mesh.html#) \n", "\n", "[https://trimsh.org/trimesh.base.html](https://trimsh.org/trimesh.base.html)\n", "\n", "[https://rdflib.readthedocs.io/](https://rdflib.readthedocs.io/) \n", "\n", "The code below shows how to create a MeshNode from various inputs." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First the geomapi and external packages are imported" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "#IMPORT PACKAGES\n", "from rdflib import Graph\n", "import os\n", "import numpy as np\n", "\n", "#IMPORT MODULES\n", "from context import geomapi #context import for documentation only\n", "from geomapi.nodes import MeshNode" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## MeshNode Creation\n", "\n", "A MeshNode is constructed using the same parameters as the base Node. Please refer to [Node Tutorial](../tutorial/tutorial_nodes.ipynb) For more info about Node Creation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "MeshNode( subject = None, # (URIRef, optional) : A subject to use as identifier for the Node.\n", " graph = None, # (Graph, optional) : An RDF Graph to parse.\n", " graphPath = None, # (Path, optional) : The path of an RDF Graph to parse.\n", " name = None, # (str, optional) : A name of the Node.\n", " path = None, # (Path, optional) : A filepath to a resource.\n", " timestamp = None, # (str, optional) : Timestamp for the node.\n", " resource = None, # (optional) : Resource associated with the node.\n", " cartesianTransform = None, # (np.ndarray, optional) : The (4x4) transformation matrix.\n", " orientedBoundingBox = None, # (o3d.geometry.OrientedBoundingBox, optional) : The oriented bounding box of the node.\n", " convexHull = None, # (o3d.geometry.TriangleMesh, optional) : The convex hull of the node.\n", " loadResource = False, # Load the resource at initialization?\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ontology link\n", "\n", "The MeshNode has 2 new standard properties that are serialized to the graph:\n", "\n", "| python name | predicate |\n", "|----------- |-----------|\n", "| `pointCount` | `geomapi:pointCount` |\n", "| `faceCount` | `geomapi:faceCount` |\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## MeshNode Resource\n", "\n", "When creating a Node with a resource, it can be done either directly with the resource, or with the path to the resource.\n", "\n", "A resource can be a big piece of data, this is why it is not always wanted to load the whole resource at initialization. This is why the `loadResource` parameter is default to `False`\n", "\n", "For more info on specific resources, see the corresponding Node type" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Loading The Resource" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Resource not loaded, but path is defined, call `load_resource()` to access it.\n", "Resource not loaded, but path is defined, call `load_resource()` to access it.\n", "resource before loading: None\n", "resource after loading: TriangleMesh with 21868 points and 25000 triangles.\n" ] } ], "source": [ "node = MeshNode(path=r\"../../..\\tests\\testfiles\\mesh\\railway.obj\", loadResource=False)\n", "print(\"resource before loading:\",node.resource)\n", "node.load_resource() # Use specialized node fo each type of resource.\n", "print(\"resource after loading:\",node.resource)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Saving The Resource\n", "\n", "A Mesh resource can be saved to disk using the `save_resource()` function.\n", "Currently supports: .ply, .obj" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[Open3D WARNING] This file format currently does not support writing textures and uv coordinates. Consider using .obj\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "node = MeshNode(path=r\"../../..\\tests\\testfiles\\mesh\\railway.obj\", loadResource=True)\n", "node.save_resource(directory=r\"../../../tests/testfiles/resources\", extension=\".ply\") # Save the resource to the resourcePath" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## MeshNode Transformation\n", "\n", "Since every nod has a cartesian transform, it can be transformed using the `node.transform()` function.\n", "\n", "The transformation also updates the `convexHull` and `orientedBoundingBox`.\n", "\n", "Furthermore, if the MeshNode has a resource, that resource is also transformed." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 0. 0. 0.]\n", " [0. 1. 0. 0.]\n", " [0. 0. 1. 0.]\n", " [0. 0. 0. 1.]]\n", "applying transformation: (-1)\n", "[[0. 0. 1. 0.]\n", " [0. 1. 0. 0.]\n", " [1. 0. 0. 0.]\n", " [0. 0. 0. 1.]] \n", "\n", "applying rotation: (90,0,0)\n", "[[ 1.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00]\n", " [ 0.000000e+00 6.123234e-17 -1.000000e+00 0.000000e+00]\n", " [ 0.000000e+00 1.000000e+00 6.123234e-17 0.000000e+00]\n", " [ 0.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00]] \n", "\n", "applying translation: (1,2,3)\n", "[[1. 0. 0. 1.]\n", " [0. 1. 0. 2.]\n", " [0. 0. 1. 3.]\n", " [0. 0. 0. 1.]]\n" ] } ], "source": [ "node = MeshNode()\n", "print(node.cartesianTransform)\n", "transformation = np.array([[0,0,1,0],[0,1,0,0],[1,0,0,0],[0,0,0,1]])\n", "node.transform(transformation=transformation)\n", "print(\"applying transformation: (-1)\")\n", "print(node.cartesianTransform,\"\\n\")\n", "\n", "node = MeshNode()\n", "rotation = np.array([90,0,0]) #eulers in degrees\n", "node.transform(rotation=rotation)\n", "print(\"applying rotation: (90,0,0)\")\n", "print(node.cartesianTransform,\"\\n\")\n", "\n", "node = MeshNode()\n", "translation = np.array([1,2,3])\n", "node.transform(translation=translation)\n", "print(\"applying translation: (1,2,3)\")\n", "print(node.cartesianTransform)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## MeshNode Visualisation\n", "\n", "When a MeshNode has a resource, the `show()` function displays the mesh using either open3d or Trimesh, depending on the workspace.\n", "\n", "Use the `inline = True` parameter to display the mesh using the Trimesh viewer in your jupyter notebook file. Otherwise the function opens a new python window to display the open3d viewer" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "