# irfpy.util.triangle¶

Module for triangle in 3-D space.

Code author: Yoshifumi Futaana

irfpy.util.triangle module provides a Triangle to express a triangle in a 3-dimensional space. Internally, most of the calculation and the returned type is implemented using irfpy.util.vector3d.Vector3d class for Triangle.

NpTriangle class provides almost same functionality as Triangle, though for performance reason the internal calculation and interface are mostly used by numpy module.

The Triangle and NpTriangle provide basic methods for treating triangle.

Associated functions are also implemented.

The precision is in float. Numerical effects (e.g. cancellation of significant digits), are note treate.

class irfpy.util.triangle.Triangle(point0, point1, point2)[source]

Bases: object

A class for triangle.

Triangle is defined by three points: point0 -> point1 -> point2 -> point0. Order of the points is important if one calculate the normal vector. See also normalVector() method.

pointN should be an index-based array: pointN[0], [1], and [2]. Either List, numpy.ndarray, or irfpy.util.vector3d.Vector3d is acceptable.

>>> p0 = [1, 2, 3]
>>> p1 = [1, 2, 5]
>>> p2 = [2, 4, 6]
>>> t = Triangle(p0, p1, p2)

>>> p0 = np.array(p0)
>>> p1 = np.array(p1)
>>> p2 = np.array(p2)
>>> t = Triangle(p0, p1, p2)

>>> v0 = vector3d.Vector3d(p0[0], p0[1], p0[2])
>>> v1 = vector3d.Vector3d(p1[0], p1[1], p1[2])
>>> v2 = vector3d.Vector3d(p2[0], p2[1], p2[2])
>>> t = Triangle(v0, v1, v2)

asNp()[source]

Return NpTriangle instance

>>> p0 = [1, 2, 3]
>>> p1 = [1, 2, 5]
>>> p2 = [2, 4, 6]
>>> t = Triangle(p0, p1, p2)
>>> print(t)
Triangle(Vector3d( 1, 2, 3 ) Vector3d( 1, 2, 5 ) Vector3d( 2, 4, 6 ))
>>> nt = t.asNp()
>>> print(nt)
NpTriangle([1.  2.  3.] [1.  2.  5.] [2.  4.  6.])

getArea()[source]

Return the area.

>>> p0 = [1, 0, 0]
>>> p1 = [0, 1, 0]
>>> p2 = [0, 0, 1]

>>> t = Triangle(p0, p1, p2)
>>> print('%.3f' % t.getArea())
0.866

normalVector()[source]

Returns the normal vector.

Returns the normal vector in irfpy.util.vector3d.Vector3d instance. The length is the area.

>>> p0 = [1, 0, 0]
>>> p1 = [0, 1, 0]
>>> p2 = [0, 0, 1]

>>> t = Triangle(p0, p1, p2)
>>> norm = t.normalVector()
>>> print(norm.x, norm.y, norm.z)
0.5 0.5 0.5

>>> t = Triangle(p0, p2, p1)
>>> norm = t.normalVector()
>>> print(norm.x, norm.y, norm.z)
-0.5 -0.5 -0.5

>>> p0 = [1, 0, 0]
>>> p1 = [1, 2, 0]
>>> p2 = [3, 0, 0]
>>> norm = Triangle(p0, p1, p2).normalVector()
>>> print(norm.x, norm.y, norm.z)
0.0 0.0 -2.0

centerOfGravity()[source]

Return the center of gravity in Vector3d instance.

>>> p0 = [7, -2, 3]
>>> p1 = [-1, 3, 5]
>>> p2 = [3, -7, 1]

>>> t = Triangle(p0, p1, p2)

>>> v = t.centerOfGravity()
>>> print(v.x)
3.0
>>> print(v.y)
-2.0
>>> print(v.z)
3.0

getVertices()[source]

Returns three vertices in the specified order in the constructor

>>> p0 = [1, 0, 0]
>>> p1 = [1, 2, 0]
>>> p2 = [3, 0, 0]
>>> v0, v1, v2 = Triangle(p0, p1, p2).getVertices()
>>> print(v0)
Vector3d( 1, 0, 0 )
>>> print(v1)
Vector3d( 1, 2, 0 )
>>> print(v2)
Vector3d( 3, 0, 0 )

class irfpy.util.triangle.NpTriangle(point0, point1, point2)[source]

Bases: object

A class for triangle.

Triangle is defined by three points: point0 -> point1 -> point2 -> point0. Order of the points is important if one calculate the normal vector. See also normalVector() method.

pointN should be an index-based array: pointN[0], [1], and [2]. Either iterable, numpy.ndarray, or irfpy.util.vector3d.Vector3d is acceptable.

>>> p0 = [1, 2, 3]
>>> p1 = [1, 2, 5]
>>> p2 = [2, 4, 6]
>>> t = NpTriangle(p0, p1, p2)

>>> p0 = np.array(p0)
>>> p1 = np.array(p1)
>>> p2 = np.array(p2)
>>> t = NpTriangle(p0, p1, p2)

>>> v0 = vector3d.Vector3d(p0[0], p0[1], p0[2])
>>> v1 = vector3d.Vector3d(p1[0], p1[1], p1[2])
>>> v2 = vector3d.Vector3d(p2[0], p2[1], p2[2])
>>> t = NpTriangle(v0, v1, v2)

getArea()[source]

Return the area.

>>> p0 = [1, 0, 0]
>>> p1 = [0, 1, 0]
>>> p2 = [0, 0, 1]

>>> t = NpTriangle(p0, p1, p2)
>>> print('%.3f' % t.getArea())
0.866

normalVector()[source]

Returns the normal vector.

Returns the normal vector in irfpy.util.vector3d.Vector3d instance. The length is the area.

>>> p0 = [1, 0, 0]
>>> p1 = [0, 1, 0]
>>> p2 = [0, 0, 1]

>>> t = NpTriangle(p0, p1, p2)
>>> norm = t.normalVector()
>>> print(norm[0], norm[1], norm[2])
0.5 0.5 0.5

>>> t = NpTriangle(p0, p2, p1)
>>> norm = t.normalVector()
>>> print(norm[0], norm[1], norm[2])
-0.5 -0.5 -0.5

>>> p0 = [1, 0, 0]
>>> p1 = [1, 2, 0]
>>> p2 = [3, 0, 0]
>>> norm = NpTriangle(p0, p1, p2).normalVector()
>>> print(norm[0], norm[1], norm[2])
0.0 0.0 -2.0

centerOfGravity()[source]

Return the center of gravity in Vector3d instance.

>>> p0 = [7, -2, 3]
>>> p1 = [-1, 3, 5]
>>> p2 = [3, -7, 1]

>>> t = NpTriangle(p0, p1, p2)

>>> v = t.centerOfGravity()
>>> print(v[0])
3.0
>>> print(v[1])
-2.0
>>> print(v[2])
3.0

getVertices()[source]

Returns three vertices in the specified order in the constructor

>>> p0 = [1, 0, 0]
>>> p1 = [1, 2, 0]
>>> p2 = [3, 0, 0]
>>> v0, v1, v2 = NpTriangle(p0, p1, p2).getVertices()
>>> print(v0)
[1.  0.  0.]
>>> print(v1)
[1.  2.  0.]
>>> print(v2)
[3.  0.  0.]

irfpy.util.triangle.distance(tri, point)[source]

Returns the distance from a given point to a triangle.

Parameters:
Returns:

The distance from point to tri. This is a general distance which means that a negative value can be returned if the point is behind the triangle plane.

Return type:

float

Triangle in x-y plane is defined. The normal is +z diretion.

>>> t = Triangle([1.5, 0, 0], [0, 1.2, 0], [0, 0, 0])
>>> p = [0, 0, 5]
>>> d = distance(t, p)
>>> print('%.2f' % d)
5.00


If the point is behind the triangle plane (i.e. -z area), e.g. (0, 0, -1), negative value is returned. Absolute value is the distance.

>>> p = [0, 0, -2]
>>> d = distance(t, p)
>>> print('%.2f' % d)
-2.00


It works with NpTriangle object.

>>> t = NpTriangle([1.5, 0, 0], [0, 1.2, 0], [0, 0, 0])
>>> p = [0, 0, 5]
>>> d = distance(t, p)
>>> print('%.2f' % d)
5.00
>>> p = [0, 0, -2]
>>> d = distance(t, p)
>>> print('%.2f' % d)
-2.00

irfpy.util.triangle.foot_of_perpendicular(tri, point)[source]

Returns the position of the foot of perpendicular.

Parameters:
Returns:

The foot-of-perpendicular for the given point to the given tri.

Foot of perpendicular of the point P to the triangle ABC can be calculate as follows. Write n as a normal vector of ABC and d as a general distance of between P and ABC. The vector from H, the foot of perpendicular, to the point P can be written as d n. Thus, H can be written as P - d n.

>>> t = Triangle([1.5, 0, 0], [0, 1.2, 0], [0, 0, 0])
>>> p = [3, 2, 5]
>>> h = foot_of_perpendicular(t, p)
>>> print('%.2f %.2f %.2f' % (h.x, h.y, h.z))
3.00 2.00 0.00

>>> p = [-3, 4, -5]
>>> h = foot_of_perpendicular(t, p)
>>> print('%.2f %.2f %.2f' % (h.x, h.y, h.z))
-3.00 4.00 0.00


It works with NpTriangle object. >>> t = NpTriangle([1.5, 0, 0], [0, 1.2, 0], [0, 0, 0]) >>> p = [3, 2, 5] >>> h = foot_of_perpendicular(t, p) >>> print(‘%.2f %.2f %.2f’ % (h[0], h[1], h[2])) 3.00 2.00 0.00

>>> p = [-3, 4, -5]
>>> h = foot_of_perpendicular(t, p)
>>> print('%.2f %.2f %.2f' % (h[0], h[1], h[2]))
-3.00 4.00 0.00

irfpy.util.triangle.doctests()[source]