Recurrent 3D Geometry Formulas

Introduction

This article is a summary of most recurrent formulas that I use when dealing with 3D geometry. Most of them are basic 3D computing, learned through courses or free material found online (guess where): the goal here is not to present something extremely complex, but rather to gather all those simple and often used formulas, so that I don’t need to search for them everytime in many different places! You can thank me later :wink: As a plus, I will leave a sample python implementation, so that you can copy-paste or just having a starting point for your own implementation in a spicy language. Let’s get started!

Vector - Vector projection

Projecting vector U on vector V is defined as: \[ proj_{\overrightarrow{v}} (\overrightarrow{u}) = \frac{\overrightarrow{u} \cdot \overrightarrow{v}}{||v||^2} \cdot \overrightarrow{v} \]

Implementation with NumPy:

import numpy as np

def projection_vec2vec(u, v):
   # Projection of vector u on vector v
   return (np.dot(u, v) / (np.linalg.norm(v) ** 2)) * v

Vector - Plane projection

Projecting vector U on a plane defined by its normal N is defined as: \[ proj_{plane} (\overrightarrow{u}) = \overrightarrow{u} - proj_{\overrightarrow{n}}(\overrightarrow{u}) = \overrightarrow{u} - \frac{\overrightarrow{u} \cdot \overrightarrow{n}}{||n||^2} \cdot \overrightarrow{n} \]

Note that Vector - Vector projection is involved, so we can re-use code above.

Implementation requires NumPy and projection_vec2vec, that is described above:

def projection_vec2plane(u, n):
   # Projection of vector u on plane orthogonal to vector n
   return u - projection_vec2vec(u, n)

Cosine distance between two vectors

The cosine distance between vector U and vector V is defined as \[ cos_{dist}(\overrightarrow{u}, \overrightarrow{v}) = 1 - cos_{sim}(\overrightarrow{u}, \overrightarrow{v}) = 1 - \frac{\overrightarrow{u} \cdot \overrightarrow{v}}{||u|| * ||v||} \]

Implementation with NumPy:

import numpy as np

def cosine_distance(u, v):
   # Cosine distance between vector u and vector v
   cosine_similarity = np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v))
   return 1 - cosine_similarity

Line - Plane intersection

Assuming the line is represented by a point on the line L0 and the direction of the line L and the plane is represented by a point on the plane P0 and its normal P, the intersection between line L and plane P is defined as:

  • if \(\overrightarrow{l} \cdot \overrightarrow{n} \neq 0\) a point p defined as \(p = l_0 + l * d\), where \(d = \frac{(p_0 - l_0) \cdot \overrightarrow{n}}{\overrightarrow{l} \cdot \overrightarrow{n}}\)
  • if \(\overrightarrow{l} \cdot \overrightarrow{n} \neq 0\), then:
    • if \((p_0 - l_0) \cdot n = 0\), the line is contained in the plane, i.e. the line intersects the plane at each point of the line
    • otherwise, line and plane are parallel and there is no intersection between the two.

Implementation with NumPy:

import numpy as np

def intersection_vec2plane(vec_point, vec_direction,
                            plane_point, plane_normal):
   # Intersection between vector and plane
   d = np.dot(plane_normal, plane_point - vec_point) /
        np.dot(vec_direction, plane_normal)
   return vec_point + vec_direction * d

Offset of a point in a line

Assuming the line is represented by a point on the line L0 and the direction of the line L and given a point P, the offset of point P in line L is defined as:

\[ \texttt{line_offset} (p) = \overrightarrow{p - l0} \cdot \overrightarrow{l} \]

This can also be described as the distance between point P and L0 on the line L.

Implementation with NumPy:

import numpy as np

def offset_in_line(line_point, line_direction, point):
   # Offset of point on a line
   return np.dot(p - line_point, line_direction)

Bonus: Euclidean distance between two points

This is a bonus paragraph as euclidean distance is pretty straightforward, but I wanted to point out a fast and concise implementation of it (requiring NumPy though). The euclidean distance between two points P and Q is defined as:

\[ d(p, q) = \sum_{i=1}^{n} \sqrt{(p_i - q_i)^2} \]

The L2-norm is defined as:

\[ ||x||_{2} := \sqrt{x_1^2 + \dots + x_n^2} \]

Then it’s easy to reformulate as:

import numpy as np

def euclidean_dist(p, q):
   # Euclidean distance between point p and point q
   return np.linalg.norm(p - q)


This list will be updated whenever I find myself searching for some formulas repeatedly. If you found any error or want to add some formulas, maybe if you use this as a vademecum (as I personally do), just let me know with a comment below!

Comments