Triangle 3D

There are two data structures to represent a triangle in three-dimensinal space, of points \((A, B, C)\):

digraph Triangle3D {
Triangle3D -> CTriangle3D
CTriangle3D -> "CPoint3D* A"
CTriangle3D -> "CPoint3D* B"
CTriangle3D -> "CPoint3D* C"
CTriangle3D [fillcolor=gray,style="rounded,filled"]
"CPoint3D* A" [fillcolor=gray,style="rounded,filled"]
"CPoint3D* B" [fillcolor=gray,style="rounded,filled"]
"CPoint3D* C" [fillcolor=gray,style="rounded,filled"]
}

CTriangle3D C structure

A CTriangle3D has just three members, pointing to triangle vertices:

CTriangle3D
CPoint3D* A
CPoint3D* B
CPoint3D* C
In [2]:
%%cython

from libc.stdio cimport printf
cimport geomalgo as ga

cdef:
    ga.CPoint3D A, B, C
    ga.CTriangle3D ABC

A.x, A.y, A.z = 0, 0, 0
B.x, B.y, B.z = 1, 1, 0
C.x, C.y, C.z = 1, 1, 0

ABC.A = &A
ABC.B = &B
ABC.C = &C

printf("ABC.B.x: %.1f\n", ABC.B.x)
ABC.B.x: 1.0

Triangle3D Python extension type

A Triangle3D takes the three vertices A, B and C as arguments (Point3D), and an optional index.

class Triangle3D(A, B, C, index=0)

Attributes:

ctri3d: CTriangle3D
index: int
In [3]:
A = ga.Point3D(0, 0, 0, name='A')
B = ga.Point3D(1, 1, 0, name='B')
C = ga.Point3D(0, 0, 1, name='C')

ABC = ga.Triangle3D(A, B, C)

fig = plt.figure(figsize=(8,8))

for obj in [ABC, A, B, C]:
    obj.plot()

ax = gca(projection='3d')
ax.set_aspect('equal')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
Out[3]:
<matplotlib.text.Text at 0x7f129317a358>
_images/triangle3d_4_1.png

the wrapped C structure Triangle3D.ctri3D is accessible only using Cython

In [4]:
%%cython

from libc.stdio cimport printf
cimport geomalgo as ga

cdef:
    ga.Point3D A = ga.Point3D(0, 0, 0)
    ga.Point3D B = ga.Point3D(1, 1, 0)
    ga.Point3D C = ga.Point3D(0, 0, 1)
    ga.Triangle3D ABC = ga.Triangle3D(A, B, C)
    ga.CTriangle3D* ptr

ABC = ga.Triangle3D(A, B, C)

ptr = &ABC.ctri3d
printf('ABC.B.x: %.1f\n', ptr.B.x)
ABC.B.x: 1.0

Intersections with segments

In [ ]:
P = ga.Point3D(0.2, 0., 0.4, name='P')
Q = ga.Point3D(0.2, 1., 0.4, name='Q')
PQ = ga.Segment3D(P, Q)

I = ga.intersec3d_triangle_segment(ABC, PQ)
I.name = 'I'

fig = plt.figure(figsize=(8,8))

for obj in [ABC, A, B, C, PQ, P, Q]:
    obj.plot()

I.plot(color='r')

ax = gca(projection='3d')
ax.set_aspect('equal')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
<matplotlib.text.Text at 0x7f1290621550>
_images/triangle3d_8_1.png
In [ ]: