GeometRi is a simple and lightweight 3D geometry library for .Net. Its main purpose is manipulations with basic geometrical primitives, such as point, line, plane, segment in 3D space: translation and rotation operations, distance calculation, intersections, orthogonal projections of one object into another, etc. The basic objects can be defined in global or in one of the local coordinate systems and converted form one coordinate system into another. Each object stores its reference coordinate system and all necessary transformations are performed implicitly when needed. Compatible with .Net Framework 2.0, .Net Standard 1.0, .Net Core 3.1 and .Net 5.


Keywords
computational, geometry, math, 3D
License
MIT
Install
Install-Package GeometRi -Version 1.4.0.5

Documentation

GeometRi

Simple and lightweight computational geometry library for .Net

Main purpose of the GeometRi library is manipulations with basic geometrical primitives, such as point, line, plane, sphere, triangle in 3D space: translation and rotation operations, distance calculation, intersections, orthogonal projections of one object into another, etc. The objects can be defined in global or in one of the local coordinate systems and converted form one coordinate system into another.

The library was build to be as simple and intuitive as posible. Users do not have to remember the reference coordinate system of each object. The objects store the coordinate system they are defined in and all transformations will be caried out implicitly when necessary.

The main goal was simplisity and readability of the code, therefore speed and robustness was not a priority. Global tolerance property is used for proximity checking, not an exact robust algorithms.

Release notes

Installation

Use NuGet to install library. Search for GeometRi in NuGet package manager or type in the Package Manager Console:

Install-Package GeometRi

Examples

  • Ptolemy's construction of a pentagon inside the circle: C#
  • Mascheroni construction of a regular pentagon: C#
  • Rytz's construction: C#, VB.Net

Classes

  • Point3d and Vector3d are two base classes, representing points and vectors in 3D space. Objects of type Point3d or Vector3d can be defined in global or in local coordinate systems.

  • Line3d, Ray3d, Segment3d, Plane3d, Circle3d, Sphere, Ellipse, Ellipsoid, Box3d, Triangle and Tetrahedron are compound classes, which are defined in terms of points and vectors.

  • Coord3d, Rotation, Quaternion and Matrix3d are auxiliary classes.

  • GeometRi3d is an abstract class, which defines some common functionality, for example global tolerance property (GeometRi3d.Tolerance) used in proximity operations by other classes. Implements tolerance based equality methods: AlmostEqual(double, double), NotEqual(double,double), Greater(double, double) and Smaller(double, double).

Point3d

One of the base classes, can be constructed by three double numbers (X, Y and Z) or from double array. Each constructor has optional parameter 'coord' for local coordinate system in which point will be defined. By default all points are defined in global coordinate system.

Properties

  • X - X coordinate in reference coordinate system
  • Y - Y coordinate in reference coordinate system
  • Z - Z coordinate in reference coordinate system
  • Coord - reference coordinate system
  • ToVector - radius vector of point

Methods

  • Copy - Creates copy of the object
  • ConvertTo - convert point to local coordinate system
  • ConvertToGlobal - convert point to global coordinate system
  • Add - add two points
  • Subtract - subtract one point from other
  • Scale - scale point by given number
  • DistanceTo - shortest distance from point to point to other objects
  • ClosestPoint - closest point on circle, box, triangle, sphere, ellipse, ellipsoid, tetrahedron
  • ProjectionTo - orthogonal projection of point to line, plane or sphere
  • BelongsTo - test if point is located in the epsilon neighborhood of the object
  • IsInside - test if point is located strictly inside (not in the epsilon neighborhood of the boundary) of the object
  • IsOutside - test if point is located outside of the epsilon neighborhood of the object
  • IsOnBoundary - test if point is located in the epsilon neighborhood of the object's boundary
  • Translate - translate point by vector
  • Rotate - rotate point around origin or other point
  • Reflect - reflect point in point, line or plane
  • Equals - check if two points are equals
  • ToString - string representation of point in global or local coordinate system

Static methods

  • CollinearPoints - check if three points are collinear

Overloaded operators

  • + - add two points
  • - - subtract one point from other
  • - - unary operator
  • * - scale point by number
  • / - scale point by number
  • = - equality check
  • <> - unequality check

Vector3d

Second base class, representing vector in 3D space. Constructed by three components (X, Y and Z) or from double array (with optional 'coord' parameter for local cordinate system). Additionally, can be constructed by point, representing radius vector of that point, or by two points, representing vector from first point to another. In this cases the vector will be defined in the same coordinate system as the first operand.

Properties

  • X - X component in reference coordinate system
  • Y - Y component in reference coordinate system
  • Z - Z component in reference coordinate system
  • Coord - reference coordinate system
  • Norm - Norm of a vector
  • ToPoint - point, represented by vector starting in origin
  • OrthogonalVector - return arbitrary vector, orthogonal to the current vector

Methods

  • Copy - Creates copy of the object
  • ConvertTo - convert vector to local coordinate system
  • ConvertToGlobal - convert vector to global coordinate system
  • Normalize - normalize the current vector
  • Normalized - return normalized vector
  • IsParallelTo - check if two objects are parallel
  • IsNotParallelTo - check if two objects are NOT parallel
  • IsOrthogonalTo - check if two objects are orthogonal
  • AngleTo - angle between two objects
  • AngleToDeg - angle between two objects (in degrees)
  • Add - overloaded, add number or vector
  • Subtract - oveloaded, subtract number or vector
  • Mult - overloaded, multiply by number or vector
  • Dot - dot product of two vectors
  • ProjectionTo - return projection of the current vector to the second vector
  • Rotate - rotate vector around origin
  • Reflect - reflect vector in point, line or plane
  • Equals - check if two vectors are equals
  • ToString - string representation of vector in global or local coordinate system

Line3d

Infinite line in 3D space and defined by any point lying on the line and a direction vector.

Properties

  • Point - base point of the line
  • Direction - direction vector of the line

Methods

  • Copy - Creates copy of the object
  • DistanceTo - shortest distance to point, line, ray or segment
  • PerpendicularTo - point on the perpendicular to the second line
  • IntersectionWith - intersection of line with plane, ellipsoid, ellipse, circle, triangle, segment or sphere
  • ProjectionTo - orthogonal projection of a line to the plane
  • IsParallelTo - check if two objects are parallel
  • IsNotParallelTo - check if two objects are NOT parallel
  • IsOrthogonalTo - check if two objects are orthogonal
  • IsCoplanarTo - check if line is coplanar to other linear or planar object
  • AngleTo - angle between two objects
  • AngleToDeg - angle between two objects (in degrees)
  • Translate - translate line by vector
  • Rotate - rotate line around origin or other point
  • Reflect - reflect line in point, line or plane
  • Equals - check if two lines are equals
  • ToString - string representation of line in global or local coordinate system

Ray3d

Represent ray in 3D space and is defined by starting point and direction vector.

Properties

  • Point - base point of the ray
  • Direction - direction vector of the ray
  • ToLine - convert ray to line

Methods

  • Copy - Creates copy of the object
  • DistanceTo - shortest distance to point, line, segment or other ray
  • PerpendicularTo - point on the perpendicular to the line
  • IntersectionWith - intersection of ray with plane
  • ProjectionTo - orthogonal projection of ray to the plane
  • IsParallelTo - check if two objects are parallel
  • IsNotParallelTo - check if two objects are NOT parallel
  • IsOrthogonalTo - check if two objects are orthogonal
  • IsCoplanarTo - check if ray is coplanar to other linear or planar object
  • AngleTo - angle between two objects
  • AngleToDeg - angle between two objects (in degrees)
  • Translate - translate ray by vector
  • Rotate - rotate ray around origin or other point
  • Reflect - reflect ray in point, line or plane
  • Equals - check if two rays are equals
  • ToString - string representation of ray in global or local coordinate system

Segment3d

Represent a line segment in 3D space and is defined by two points.

Properties

  • P1 - first point of the segment
  • P2 - second point of the segment
  • Length - length of the segment
  • ToVector - convert segment to vector
  • ToRay - convert segment to ray
  • ToLine - convert segment to line
  • MinimumBoundingBox - minimum bounding box of the object
  • BoundingSphere - bounding sphere of the object

Methods

  • Copy - Creates copy of the object
  • BoundingBox - Axis Aligned Bounding Box (AABB) in given coordinate system
  • DistanceTo - shortest distance to point, line, ray, plane or other segment
  • IntersectionWith - intersection of segment with line, plane, ellipse, triangle, ellipsoid, sphere, circle or other segment
  • ProjectionTo - orthogonal projection of segment to the line or plane
  • IsParallelTo - check if two objects are parallel
  • IsNotParallelTo - check if two objects are NOT parallel
  • IsOrthogonalTo - check if two objects are orthogonal
  • IsCoplanarTo - check if segment is coplanar to other linear or planar object
  • AngleTo - angle between two objects
  • AngleToDeg - angle between two objects (in degrees)
  • Translate - translate segment by vector
  • Rotate - rotate segment around origin or other point
  • Reflect - reflect segment in point, line or plane
  • Equals - check if two segments are equals
  • ToString - string representation of serment in global or local coordinate system

Plane3d

3D plane defined by arbutrary point on the plane and a normal vector. Optionally can be defined by coefficients in general equation of plane (Ax + By + Cz + D = 0), by three points or by point and two vectors in the plane.

Properties

  • Point - point on the plane
  • Normal - normal vector of the plane
  • A/B/C/D - coefficients A, B, C and D in the general plane equation

Methods

  • Copy - Creates copy of the object
  • SetCoord - set reference coordinate system for general plane equation
  • IntersectionWith - intersection of plane with line, plane, segment, sphere, ellipse, ellipsoid, circle or two other planes
  • IsParallelTo - check if two objects are parallel
  • IsNotParallelTo - check if two objects are NOT parallel
  • IsOrthogonalTo - check if two objects are orthogonal
  • IsCoplanarTo - check if plane is coplanar to other linear or planar object
  • AngleTo - angle between two objects
  • AngleToDeg - angle between two objects (in degrees)
  • Translate - translate plane by vector
  • Rotate - rotate plane around origin or other point
  • Reflect - reflect plane in point, line or plane
  • Equals - check if two planes are equals
  • ToString - string representation of plane in global or local coordinate system

Sphere

Defines a sphere in 3D space. Implements intersection with line, plane and other sphere, projection to line and plane, as well as common translation, rotation and reflection methods.

Properties

  • Center - center of the sphere
  • R - radius of the sphere
  • Area - area of the sphere
  • Volume - volume of the sphere
  • MinimumBoundingBox - minimum bounding box of the object
  • BoundingSphere - bounding sphere of the object

Methods

  • Copy - Creates copy of the object
  • BoundingBox - Axis Aligned Bounding Box (AABB) in given coordinate system
  • IsInside - check if sphere is located inside box
  • ClosestPoint - point on sphere's surface closest to target point
  • DistanceTo - shortest distance to point, line, ray, segment, plane, circle, sphere or box
  • Intersects - intersection check with circle and tetrahedron
  • IntersectionWith - intersection of sphere with line, plane, segment or other sphere
  • ProjectionTo - orthogonal projection of sphere to the line or plane
  • Translate - translate sphere by vector
  • Rotate - rotate sphere around origin or other point
  • Reflect - reflect sphere in point, line or plane
  • Equals - check if two spheres are equals
  • ToString - string representation of sphere in global or local coordinate system

Circle3d

Defines a circle in 3D space by center point, radius and normal vector.

Properties

  • Center - center of the circle
  • R - radius of the circle
  • Normal - normal of the circle
  • Perimeter - perimeter of the circle
  • Area - area of the circle
  • ToEllipse - convert circle to equivalent ellipse
  • MinimumBoundingBox - minimum bounding box of the object
  • BoundingSphere - bounding sphere of the object

Methods

  • Copy - Creates copy of the object
  • BoundingBox - Axis Aligned Bounding Box (AABB) in given coordinate system
  • IsInside - check if circle is located inside box
  • ClosestPoint - point on circle's surface closest to target point
  • ParametricForm - return point on circle for given parameter 't'
  • ProjectionTo - orthogonal projection of circle to plane or line
  • DistanceTo - shortest distance to point, plane, circle, sphere, box or triangle
  • Intersects - intersection check with box, triangle, circle and sphere
  • IntersectionWith - intersection of circle with line, plane, segment or other circle
  • IsParallelTo - check if two objects are parallel
  • IsNotParallelTo - check if two objects are NOT parallel
  • IsOrthogonalTo - check if two objects are orthogonal
  • IsCoplanarTo - check if circle is coplanar to other linear or planar object
  • AngleTo - angle between two objects
  • AngleToDeg - angle between two objects (in degrees)
  • Translate - translate circle by vector
  • Rotate - rotate circle around origin or other point
  • Reflect - reflect circle in point, line or plane
  • Equals - check if two circles are equals
  • ToString - string representation of circle in global or local coordinate system

Ellipse

Ellipse in 3D space, defined by center point and two orthogonal vectors, major and minor semiaxes.

Properties

  • Center - center of the ellipse
  • MajorSemiaxis - major semiaxis of the ellipse
  • MinorSemiaxis - minor semiaxis of the ellipse
  • Normal - normal of the ellipse
  • A - length of major semiaxis
  • B - length of minor semiaxis
  • F - distance from center to focus
  • F1 - first focus
  • F2 - second focus
  • e - eccentricity of the ellipse
  • Perimeter - approximate circumference of the ellipse
  • Area - area of the ellipse
  • MinimumBoundingBox - minimum bounding box of the object
  • BoundingSphere - bounding sphere of the object

Methods

  • Copy - Creates copy of the object
  • BoundingBox - Axis Aligned Bounding Box (AABB) in given coordinate system
  • ParametricForm - return point on ellipse for given parameter 't'
  • ProjectionTo - orthogonal projection of ellipse to plane or line
  • IntersectionWith - intersection of ellipse with line, plane or segment
  • ClosestPoint - calculates the point on the ellipse's boundary closest to given point
  • IsParallelTo - check if two objects are parallel
  • IsNotParallelTo - check if two objects are NOT parallel
  • IsOrthogonalTo - check if two objects are orthogonal
  • IsCoplanarTo - check if ellipse is coplanar to other linear or planar object
  • AngleTo - angle between two objects
  • AngleToDeg - angle between two objects (in degrees)
  • Translate - translate ellipse by vector
  • Rotate - rotate ellipse around origin or other point
  • Reflect - reflect ellipse in point, line or plane
  • Equals - check if two ellipses are equals
  • ToString - string representation of ellipse in global or local coordinate system

Ellipsoid

Ellipsoid object defined by center point and three mutually orthogonal vectors.

Properties

  • Center - center of the ellipsoid
  • SemiaxisA/B/C - semiaxes of the ellipsoid
  • A/B/C - length of the semiaxes of the ellipsoid
  • Area - approximate surface area of the ellipsoid
  • Volume - volume of the ellipsoid
  • MinimumBoundingBox - minimum bounding box of the object
  • BoundingSphere - bounding sphere of the object

Methods

  • Copy - Creates copy of the object
  • BoundingBox - Axis Aligned Bounding Box (AABB) in given coordinate system
  • ProjectionTo - orthogonal projection of ellipsoid to line
  • IntersectionWith - intersection of ellipsoid with line, plane or segment
  • ClosestPoint - calculates the point on the ellipsoid's boundary closest to given point
  • Translate - translate ellipsoid by vector
  • Rotate - rotate ellipsoid around origin or other point
  • Reflect - reflect ellipsoid in point, line or plane
  • Equals - check if two ellipsoids are equals
  • ToString - string representation of ellipsoid in global or local coordinate system

Box3d

Box object defined by center point, three dimensions and orientation in space.

Properties

  • Center - center point of the box
  • L1/L2/L3 - dimensions of the box
  • V1/V2/V3 - orientation vectors of the box
  • Orientation - box orientation
  • P1/P2/P3/P4/P5/P6/P7/P8 - corner points of the box
  • ListOfPoints - list of corner points of the box
  • ListOfTriangles - list of triangles forming the box's surface
  • ListOfPlanes - list of planes forming the box's surface
  • ListOfEdges - list of edges
  • Area - area of the box
  • Volume - volume of the box
  • MinimumBoundingBox - minimum bounding box of the object
  • BoundingSphere - bounding sphere of the object
  • IsAxisAligned - check if box is AABB

Methods

  • Copy - Creates copy of the object
  • BoundingBox - Axis Aligned Bounding Box (AABB) in given coordinate system
  • DistanceTo - shortest distance to point, circle or sphere
  • Intersects - intersection check with box, circle, tetrahedron or triangle
  • IntersectionWith - intersection of box with line, ray or segment
  • Translate - translate box by vector
  • Rotate - rotate box around origin or other point
  • Reflect - reflect box in point, line or plane
  • Equals - check if two ellipsoids are equals
  • ToString - string representation of ellipsoid in global or local coordinate system

Static Methods

  • AABB - axis aligned bounding box for a cloud of points

Triangle

Defines a triangle in 3D space. Implements common translation, rotation and reflection methods. Calculates most of the standard triangle properties: bisectors, meadians, altitudes, incenter, circumcenter, centroid, orthocenter, etc.

Properties

  • A/B/C - vertices of the triangle
  • AB/AC/BC - length of the triangles' sides
  • Perimeter - perimeter of the triangle
  • Area - area of the triangle
  • Circumcircle - circumcircle of the triangle
  • Angle_A/B/C - angle at the vertex A/B/C
  • Bisector_A/B/C - angle bisector at the vertex A/B/C
  • Incenter - incenter of the triangle
  • Centroid - centroid of the triangle
  • Orthocenter - orthocenter of the triangle
  • Circumcenter - circumcenter of the triangle
  • Incircle - incircle of the triangle
  • Altitude_A/B/C - altitude at the vertex A/B/C
  • Median_A/B/C - median at the vertex A/B/C
  • IsEquilateral - check if all sides of the triangle are the same length
  • IsIsosceles - check if two sides of the triangle are the same length
  • IsScalene - check if all sides are unequal
  • IsRight - check if one angle is equal 90 degrees
  • IsObtuse - check if one angle is greater than 90 degrees
  • IsAcute - check if all angles are less than 90 degrees
  • MinimumBoundingBox - minimum bounding box of the object
  • BoundingSphere - bounding sphere of the object

Methods

  • Copy - Creates copy of the object
  • BoundingBox - Axis Aligned Bounding Box (AABB) in given coordinate system
  • DistanceTo - shortest distance to point, segment, triangle and circle
  • IntersectionWith - intersection of triangle with line, plane, ray or segment
  • Intersects - intersection check with triangle, box, tetrahedron, sphere or circle
  • ProjectionTo - orthogonal projection of triangle to line
  • IsParallelTo - check if two objects are parallel
  • IsNotParallelTo - check if two objects are NOT parallel
  • IsOrthogonalTo - check if two objects are orthogonal
  • IsCoplanarTo - check if triangle is coplanar to other linear or planar object
  • AngleTo - angle between two objects
  • AngleToDeg - angle between two objects (in degrees)
  • Translate - translate triangle by vector
  • Rotate - rotate triangle around origin or other point
  • Reflect - reflect triangle in point, line or plane
  • Equals - check if two triangles are equals
  • ToString - string representation of triangle in global or local coordinate system

Tetrahedron

Defines a tetrahedron in 3D space. Implements common translation, rotation and reflection methods.

Properties

  • A/B/C/D - vertices of the tetrahedron
  • Center - center of the mass of tetrahedron
  • ListOfEdges - list of edges
  • ListOfFaces - list of faces
  • Area - area of the tetrahedron
  • Volume - volume of the tetrahedron

Methods

  • Copy - Creates copy of the object
  • BoundingBox - Axis Aligned Bounding Box (AABB)
  • DistanceTo - shortest distance to point and tetrahedron
  • ClosestPoint - calculates the point on the terahedron's boundary closest to given point
  • Intersects - intersection check with triangle, tetrahedron, line, ray, segment, box and sphere
  • IsInside - check if object is located inside given box
  • Translate - translate triangle by vector
  • Rotate - rotate triangle around origin or other point
  • Reflect - reflect triangle in point, line or plane
  • Scale - scale tetrahedron
  • Equals - check if two triangles are equals
  • ToString - string representation of triangle in global or local coordinate system

Coord3d

Class representing orthogonal cartesian 3D coordinate system. Defined by an origin point and transformation matrix (three orthogonal unit vectors stored in row format). One global coordinate system (Coord3d.GlobalCS) is defined by default, any number of local coordinate systems can be defined by users.

Properties

  • Origin - origin of the coordinate system
  • Axes - unit vectors of the axes, stored as row-matrix(3x3)
  • Name - name of the coordinate system
  • Counts - total number of defined coordinate systems
  • X/Y/Zaxis - X/Y/Z-axis of the coordinate system
  • XY/XZ/YZ_plane - planes of the coordinate system

Methods

  • Copy - Creates copy of the object
  • Rotate/RotateDeg - rotate coordinate system around rotation axis
  • Equals - check if two coordinate systems are equals
  • ToString - string representation of coordinate system

Matrix3d

Properties

  • Item(i,j) - element of the matrix
  • Row1/2/3 - rows of the matrix
  • Column1/2/3 - columns of the matrix
  • Det - determinant of the matrix
  • MaxNorm - Max norm of the matrix
  • IsZero - check if matrix is zero matrix
  • IsIdentity - check if matrix is identity matrix
  • IsOrthogonal - check if matrix is orthogonal

Methods

  • Inverse - inverse of the matrix
  • Transpose - transpose of the matrix
  • RotationMatrix - rotation around given axis
  • Equals - check if two matrix are equals
  • ToString - string representation of matrix

Static methods

  • Identity - creates new identity matrix
  • DiagonalMatrix - creates diagonal matrix

Quaternion

Unit quaternion (W + Xi + Yj + Z*k)

Properties

  • W/X/Y/Z - components of quaternion in reference coordinate system
  • Coord - reference coordinate system
  • Norm - norm of a quaternion
  • SquareNorm - square of the norm of a quaternion
  • Conjugate - conjugate of a quaternion
  • ToAxis - axis of rotation in reference coordinate system
  • ToAngle - angle of rotation in reference coordinate system
  • Normalized - return normalized quaternion

Methods

  • Copy - Creates copy of the object
  • ConvertTo - convert quaternion to local coordinate system
  • ConvertToGlobal - convert quaternion to global coordinate system
  • Normalize - normalize the current quaternion
  • Add/Subtract/Mult - arithmetic operations
  • Scale - scale quaternion by number
  • Inverse - inverse quaternion
  • ToRotationMatrix - convert to rotation matrix
  • Equals - check if two quaternions are equals
  • ToString - string representation of quaternion in global or local coordinate system

Static methods

  • SLERP - Spherical Linear intERPolation of two quaternions

Rotation

Rotation in 3D space defined in global or local reference frame (internally represented by rotation matrix)

Properties

  • Coord - reference coordinate system
  • ToAxis - axis of rotation in reference coordinate system
  • ToAngle - angle of rotation in reference coordinate system
  • ToRotationMatrix - rotation matrix
  • ToQuaternion - convert to quaternion

Methods

  • Copy - Creates copy of the object
  • ConvertTo - convert rotation to local coordinate system
  • ConvertToGlobal - convert rotation to global coordinate system
  • Mult - apply rotation to point or vector
  • ToEulerAngles - factor rotation matrix as product of three elemental rotations
  • Equals - check if two rotations are equals
  • ToString - string representation of rotation in global or local coordinate system

Static methods

  • FromEulerAngles - creates rotation object from Euler or Tait-Bryan angles
  • SLERP - Spherical Linear intERPolation of two rotations

GeometRi3D

Properties

  • Tolerance - tolerance used for comparison operations (default 1e-12)

Methods

  • AlmostEqual - tolerance based equality check
  • NotEqual - tolerance based unequality check
  • Greater - tolerance based comparison
  • Smaller - tolerance based comparison