{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# GeometryNode\n",
"The GeometryNode class in Geomapi is the abstract metadata class that governs geometry concepts such as the cartesianBounds and the OrientedBoundingBox. While this node should not be frequently used (unless to govern unknown geospatial data), it houses the geometry function for the MeshNode, BIMNode and PointCloudNode.\n",
"\n",
"**NOTE**: the serialisation of the geometry concepts is mostly based on the [OPENLABEL](https://www.asam.net/index.php?eID=dumpFile&t=f&f=3876&token=413e8c85031ae64cc35cf42d0768627514868b2f#) concept and [Open3D](http://www.open3d.org/docs/release/python_api/open3d.geometry.html) geometry definitions \n",
"\n",
"The code below shows how to create a GeometryNode class works and how it interacts with RDF Graphs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First the geomapi and external packages are imported"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [],
"source": [
"#IMPORT PACKAGES\n",
"from rdflib import Graph, URIRef, Literal\n",
"import open3d as o3d\n",
"import os\n",
"from pathlib import Path\n",
"import numpy as np\n",
"\n",
"#IMPORT MODULES\n",
"from context import geomapi \n",
"from geomapi.nodes import *\n",
"import geomapi.utils as ut\n",
"from geomapi.utils import geometryutils as gmu\n",
"import geomapi.tools as tl"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n"
]
}
],
"source": [
"%load_ext autoreload"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [],
"source": [
"%autoreload 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## GeometryNode with cartesianBounds\n",
"\n",
"cartesianBounds (np.array [6x1]) [xMin,xMax,yMin,yMax,zMin,zMax], can be set from 5 different inputs:\n"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"PointCloud with 8 points."
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"path = os.path.join(Path(os.getcwd()).parents[2],'test','testfiles','MESH','week22.obj')\n",
"\n",
"mesh=o3d.io.read_triangle_mesh(path)\n",
"box=mesh.get_oriented_bounding_box()\n",
"box.color=np.array([1,0,0])\n",
"alignedBox=mesh.get_axis_aligned_bounding_box()\n",
"alignedBox.color=np.array([0,0,1])\n",
"vectorlist=box.get_box_points()\n",
"pcd=o3d.geometry.PointCloud()\n",
"pcd.points=o3d.utility.Vector3dVector(np.vstack((mesh.get_min_bound(),mesh.get_max_bound())))\n",
"pcd.paint_uniform_color([0,0,1])\n",
"pcd2=o3d.geometry.PointCloud()\n",
"pcd2.points=vectorlist\n",
"pcd2.paint_uniform_color([1,0,0])\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. np.array(6x1), list (6 elements)
"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4 5 6]\n"
]
}
],
"source": [
"cartesianBounds=np.array([1,2,3,4,5,6])\n",
"node= GeometryNode(cartesianBounds=cartesianBounds)\n",
"print(node.cartesianBounds)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Vector3dVector (n elements)
\n"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"std::vector with 8 elements.\n",
"Use numpy.asarray() to access data.\n",
"[-52.61117755 122.50257926 12.05279306 165.88417048 -0.70982552\n",
" 24.03564393]\n"
]
}
],
"source": [
"\n",
"print(vectorlist)\n",
"node= GeometryNode(cartesianBounds=vectorlist)\n",
"print(node.cartesianBounds)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. orientedBounds (np.array(8x3))
\n"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-1.96025705e+01 1.65884170e+02 2.22874728e+01]\n",
" [ 1.22465481e+02 1.23859452e+02 2.29468259e+01]\n",
" [-5.26111776e+01 5.43129171e+01 2.33762909e+01]\n",
" [-1.95654721e+01 1.65648765e+02 -7.09825518e-01]\n",
" [ 8.94939722e+01 1.20527931e+01 1.03834556e+00]\n",
" [-5.25740791e+01 5.40775120e+01 3.78992525e-01]\n",
" [ 1.22502579e+02 1.23624046e+02 -5.04724793e-02]\n",
" [ 8.94568738e+01 1.22881982e+01 2.40356439e+01]]\n",
"[-52.61117755 122.50257926 12.05279306 165.88417048 -0.70982552\n",
" 24.03564393]\n"
]
}
],
"source": [
"array=np.asarray(vectorlist)\n",
"print(array)\n",
"node= GeometryNode(cartesianBounds=array)\n",
"print(node.cartesianBounds)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Open3D.geometry.OrientedBoundingBox
\n"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OrientedBoundingBox: center: (34.9457, 88.9685, 11.6629), extent: 148.155, 116.357, 22.9985)\n",
"[-52.61117755 122.50257926 12.05279306 165.88417048 -0.70982552\n",
" 24.03564393]\n"
]
}
],
"source": [
"print(box)\n",
"node= GeometryNode(cartesianBounds=box)\n",
"print(node.cartesianBounds)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5. Open3D geometry
"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TriangleMesh with 330263 points and 485077 triangles.\n",
"[-37.36532974 106.94235229 16.87863541 130.69406128 0.71651864\n",
" 23.73304558]\n"
]
}
],
"source": [
"print(mesh)\n",
"node= GeometryNode(cartesianBounds=mesh)\n",
"print(node.cartesianBounds)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### CartesianBounds serialization\n",
"\n",
"cartesianBounds is serialized conform E57 as a string formatted np.array [xMin,xMax,yMin,yMax,zMin,zMax]"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"@prefix e57: .\n",
"@prefix v4d: .\n",
"\n",
" a v4d:GeometryNode ;\n",
" e57:cartesianBounds \"[1 2 3 4 5 6]\" ;\n",
" v4d:name \"9aa338d7-1a46-11ed-b3cf-c8f75043ce59\" .\n",
"\n",
"\n"
]
}
],
"source": [
"node= GeometryNode(cartesianBounds=cartesianBounds)\n",
"node.to_graph()\n",
"print(node.graph.serialize())\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## GeometryNode with orientedBounds\n",
"\n",
"orientedBounds (np.array [8x3]), can be set from 3 different inputs:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. np.array(6x1), list (6 elements)
"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-1.96025705e+01 1.65884170e+02 2.22874728e+01]\n",
" [ 1.22465481e+02 1.23859452e+02 2.29468259e+01]\n",
" [-5.26111776e+01 5.43129171e+01 2.33762909e+01]\n",
" [-1.95654721e+01 1.65648765e+02 -7.09825518e-01]\n",
" [ 8.94939722e+01 1.20527931e+01 1.03834556e+00]\n",
" [-5.25740791e+01 5.40775120e+01 3.78992525e-01]\n",
" [ 1.22502579e+02 1.23624046e+02 -5.04724793e-02]\n",
" [ 8.94568738e+01 1.22881982e+01 2.40356439e+01]]\n"
]
}
],
"source": [
"orientedBounds=np.asarray(vectorlist)\n",
"node= GeometryNode(orientedBounds=orientedBounds)\n",
"print(node.orientedBounds)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Open3D.geometry.OrientedBoundingBox
\n"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OrientedBoundingBox: center: (34.9457, 88.9685, 11.6629), extent: 148.155, 116.357, 22.9985)\n",
"[[-1.96025705e+01 1.65884170e+02 2.22874728e+01]\n",
" [ 1.22465481e+02 1.23859452e+02 2.29468259e+01]\n",
" [-5.26111776e+01 5.43129171e+01 2.33762909e+01]\n",
" [-1.95654721e+01 1.65648765e+02 -7.09825518e-01]\n",
" [ 8.94939722e+01 1.20527931e+01 1.03834556e+00]\n",
" [-5.25740791e+01 5.40775120e+01 3.78992525e-01]\n",
" [ 1.22502579e+02 1.23624046e+02 -5.04724793e-02]\n",
" [ 8.94568738e+01 1.22881982e+01 2.40356439e+01]]\n"
]
}
],
"source": [
"print(box)\n",
"node= GeometryNode(orientedBounds=box)\n",
"print(node.orientedBounds)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Open3D geometry
"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TriangleMesh with 330263 points and 485077 triangles.\n",
"[[-1.96025705e+01 1.65884170e+02 2.22874728e+01]\n",
" [ 1.22465481e+02 1.23859452e+02 2.29468259e+01]\n",
" [-5.26111776e+01 5.43129171e+01 2.33762909e+01]\n",
" [-1.95654721e+01 1.65648765e+02 -7.09825518e-01]\n",
" [ 8.94939722e+01 1.20527931e+01 1.03834556e+00]\n",
" [-5.25740791e+01 5.40775120e+01 3.78992525e-01]\n",
" [ 1.22502579e+02 1.23624046e+02 -5.04724793e-02]\n",
" [ 8.94568738e+01 1.22881982e+01 2.40356439e+01]]\n"
]
}
],
"source": [
"print(mesh)\n",
"node= GeometryNode(orientedBounds=mesh)\n",
"print(node.orientedBounds)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### orientedBounds serialization\n",
"\n",
"orientedBounds is serialized conform V4D as a string formatted np.array (8x3)"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"@prefix v4d: .\n",
"\n",
" a v4d:GeometryNode ;\n",
" v4d:name \"eb728b67-1a46-11ed-8d02-c8f75043ce59\" ;\n",
" v4d:orientedBounds \"\"\"[[-1.96025705e+01 1.65884170e+02 2.22874728e+01]\n",
" [ 1.22465481e+02 1.23859452e+02 2.29468259e+01]\n",
" [-5.26111776e+01 5.43129171e+01 2.33762909e+01]\n",
" [-1.95654721e+01 1.65648765e+02 -7.09825518e-01]\n",
" [ 8.94939722e+01 1.20527931e+01 1.03834556e+00]\n",
" [-5.25740791e+01 5.40775120e+01 3.78992525e-01]\n",
" [ 1.22502579e+02 1.23624046e+02 -5.04724793e-02]\n",
" [ 8.94568738e+01 1.22881982e+01 2.40356439e+01]]\"\"\" .\n",
"\n",
"\n"
]
}
],
"source": [
"node= GeometryNode(orientedBounds=mesh)\n",
"node.to_graph()\n",
"print(node.graph.serialize())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## GeometryNode with orientedBoundingBox\n",
"\n",
"orientedBoundingBox (Open3D.geometry.OriendtedBoundingBox), can be set from 3 different inputs:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Open3D.geometry.OrientedBoundingBox
"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OrientedBoundingBox: center: (34.9457, 88.9685, 11.6629), extent: 148.155, 116.357, 22.9985)\n",
"OrientedBoundingBox: center: (34.9457, 88.9685, 11.6629), extent: 148.155, 116.357, 22.9985)\n"
]
}
],
"source": [
"print(box)\n",
"node= GeometryNode(orientedBoundingBox=box)\n",
"print(node.orientedBoundingBox)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Open3D geometry
"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TriangleMesh with 330263 points and 485077 triangles.\n",
"OrientedBoundingBox: center: (34.9457, 88.9685, 11.6629), extent: 148.155, 116.357, 22.9985)\n"
]
}
],
"source": [
"print(mesh)\n",
"node= GeometryNode(orientedBoundingBox=mesh)\n",
"print(node.orientedBoundingBox)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. orientedBounds (np.array(8x3))
\n"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-1.96025705e+01 1.65884170e+02 2.22874728e+01]\n",
" [ 1.22465481e+02 1.23859452e+02 2.29468259e+01]\n",
" [-5.26111776e+01 5.43129171e+01 2.33762909e+01]\n",
" [-1.95654721e+01 1.65648765e+02 -7.09825518e-01]\n",
" [ 8.94939722e+01 1.20527931e+01 1.03834556e+00]\n",
" [-5.25740791e+01 5.40775120e+01 3.78992525e-01]\n",
" [ 1.22502579e+02 1.23624046e+02 -5.04724793e-02]\n",
" [ 8.94568738e+01 1.22881982e+01 2.40356439e+01]]\n",
"OrientedBoundingBox: center: (34.9457, 88.9685, 11.6629), extent: 148.155, 116.357, 22.9985)\n"
]
}
],
"source": [
"array=np.asarray(vectorlist)\n",
"print(array)\n",
"node= GeometryNode(orientedBoundingBox=array)\n",
"print(node.orientedBoundingBox)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**IMPORTANT** orientedBoundingBox is currently not serialized as it can be easily reconstructed from orientedBounds."
]
}
],
"metadata": {
"interpreter": {
"hash": "801b4083378541fd050d6c91abf6ec053c863905e8162e031d57b83e7cdb3051"
},
"kernelspec": {
"display_name": "Python 3.8.13 ('conda_environment3')",
"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.13 | packaged by conda-forge | (default, Mar 25 2022, 05:59:00) [MSC v.1929 64 bit (AMD64)]"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "832bf5fd5c6660b37dd2a90a45a57b02f877bb0a297fe89ca9c261a7f9e32997"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}