Some Basics#
[1]:
import os, sys
sys.path.insert(0, os.path.abspath(".."))
import geoclide as gc
import numpy as np
Create a point and a vector#
[2]:
p1 = gc.Point(0., 0., 0.) # create a point
v1 = gc.normalize(gc.Vector(0.5, 0.5, 0.1)) # create a vector and normalize it
p1, v1
[2]:
(Point(0.0, 0.0, 0.0),
Vector(0.7001400420140049, 0.7001400420140049, 0.140028008402801))
[3]:
v1.length()
[3]:
1.0
Create a ray from the created point and vector#
[4]:
r1 = gc.Ray(o=p1, d=v1)
r1
[4]:
r(t) = (0.0, 0.0, 0.0) + t*(0.7001400420140049, 0.7001400420140049, 0.140028008402801) with t ∈ [0,inf[
[5]:
r1(8)
[5]:
Point(5.601120336112039, 5.601120336112039, 1.120224067222408)
Create a simple triangle mesh composed of 2 triangles#
[6]:
v0 = np.array([-5, -5, 0.])
v1 = np.array([5, -5, 0.])
v2 = np.array([-5, 5, 0.])
v3 = np.array([5, 5, 0.])
vertices = np.array([v0, v1, v2, v3])
f0 = np.array([0, 1, 2]) # the vertices indices of triangle 0 / face 0
f1 = np.array([2, 3, 1]) # the vertices indices of triangle 1 / face 1
faces = np.array([f0, f1])
# We can create a transformation to translate and rotate it
translate = gc.get_translate_tf(gc.Vector(2.5, 0., 0.)) # translation of 2.5 in x axis
rotate = gc.get_rotateY_tf(-90.) # rotation of -90 degrees around the y axis
oTw = translate*rotate # object to world transformation to apply to the triangle mesh
tri_mesh = gc.TriangleMesh(vertices, faces, oTw=oTw) # create the triangle mesh
ds = gc.calc_intersection(tri_mesh, r1) # see if the ray r1 intersect the triangle mesh
ds
[6]:
<xarray.Dataset> Size: 865B
Dimensions: (xyz: 3, nvertices: 4, ntriangles: 2, p0p1p2: 3, dim_0: 4,
dim_1: 4)
Coordinates:
* xyz (xyz) int64 24B 0 1 2
Dimensions without coordinates: nvertices, ntriangles, p0p1p2, dim_0, dim_1
Data variables: (12/18)
o (xyz) float64 24B 0.0 0.0 0.0
d (xyz) float64 24B 0.7001 0.7001 0.14
mint int64 8B 0
maxt float64 8B inf
is_intersection bool 1B True
thit float64 8B 3.571
... ...
vertices (nvertices, xyz) float64 96B -5.0 -5.0 0.0 ... 5.0 5.0 0.0
faces (ntriangles, p0p1p2) int64 48B 0 1 2 2 3 1
wTo_m (dim_0, dim_1) float64 128B 6.123e-17 0.0 1.0 ... 0.0 1.0
wTo_mInv (dim_0, dim_1) float64 128B 6.123e-17 0.0 -1.0 ... 0.0 1.0
oTw_m (dim_0, dim_1) float64 128B 6.123e-17 0.0 -1.0 ... 0.0 1.0
oTw_mInv (dim_0, dim_1) float64 128B 6.123e-17 0.0 1.0 ... 0.0 1.0
Attributes:
shape: TriangleMesh
date: 2025-03-10
version: 2.0.2
ntriangles: 2
nvertices: 4[7]:
ds['phit']
[7]:
<xarray.DataArray 'phit' (xyz: 3)> Size: 24B
array([2.5, 2.5, 0.5])
Coordinates:
* xyz (xyz) int64 24B 0 1 2
Attributes:
type: Point
description: the x, y and z components of the intersection point