Elementary curves & surfaces
The ten geometries a kernel knows in closed form: a line, four conics, and five surfaces. Each is a short formula placed in a frame — a canonical shape plus a placement — with a domain, an inverse map, and known defects at the seams. This page is the reference; the full story, with measured checks against a production kernel, is The mathematics behind geometry, from which the formulas and figures here are adapted.
Foundations
Point, vector, direction are three separate types. A point has
position and nothing else; a vector has length and orientation but no
position, so translations do not apply to it; a direction is a vector
constrained to length 1. geomcore enforces this in its type system
(Point, Vector), because mixing them produces bugs that look
like geometry errors — the classic one is translating a normal.
Canonical shape plus placement. Nothing is stored as a general shape in space. A circle is a radius plus an origin and a frame; every geometry below follows the same division. Placements come in three sizes: a point and one direction (an axis — lines, surfaces of revolution), a point and two directions (a plane frame — the conics are planar curves), a point and three directions (a full frame — the surfaces). Frames are right-handed: flip the third direction and every normal flips with it.
A curve is a map \(C: I \subset \mathbb{R} \to \mathbb{R}^3\) from an interval; a surface is a map \(S: D \subset \mathbb{R}^2 \to \mathbb{R}^3\) from a rectangle. The domain is part of the geometry: the circle formula on a quarter turn is an arc, and a closed domain \([0, 2\pi]\) whose ends evaluate to the same point is what periodic means. Forward evaluation is cheap; the way back — point to parameter — is a separate algorithm, closed-form for all ten below, which is largely why they are kept as special cases.
The five curves
Line
The only elementary curve with no frame: a point and a direction, and the parameter is arc length — equal steps in \(u\) are equal steps in space.
from geomcore import Line, Point, Vector
line = Line(Point.origin(), Vector.unit_z())
p = line.point_at(2.0) # parameter -> 3D point
assert (p.x, p.y, p.z) == (0.0, 0.0, 2.0)
assert line.parameter_of(p) == 2.0 # 3D point -> parameter
use geomcore::prelude::*;
use geomcore::{Line, Point, Tolerance, Vector};
let line = Line::new(Point::ORIGIN, Vector::Z).unwrap();
let p = line.point_at(2.0); // parameter -> 3D point
assert_eq!(p, Point::new(0.0, 0.0, 2.0));
assert_eq!(line.parameter_of(p, Tolerance::DEFAULT), 2.0); // 3D point -> parameter
The way back: \(u = (P - O) \cdot \vec{D}\).
Circle
The parameter is the angle from X, and arc length is \(R\,u\) — the only conic whose parameter is proportional to distance travelled.
from geomcore import Circle
circle = Circle(Point.origin(), Vector.unit_z(), 2.0)
assert abs(circle.length() - 4.0 * 3.141592653589793) < 1e-12
p = circle.point_at(1.0) # parameter -> 3D point
assert abs(circle.parameter_of(p) - 1.0) < 1e-12 # 3D point -> parameter
use geomcore::Circle;
let circle = Circle::new(Point::ORIGIN, Vector::Z, 2.0).unwrap();
let p = circle.point_at(1.0); // parameter -> 3D point
assert!((circle.parameter_of(p, Tolerance::DEFAULT) - 1.0).abs() < 1e-12); // 3D point -> parameter
The way back: \(u = \mathrm{atan2}(y, x)\), with \(x, y\) the point resolved in the placement frame. Periodic: \(C(u + 2\pi) = C(u)\).
Ellipse
Reads like the circle with two radii, but the parameter is the eccentric angle — taken on the circle of radius \(a\), projected onto the curve — not the polar angle from the centre. It is not arc length either: speed runs from \(b\) to \(a\), so uniform steps bunch at the ends of the major axis.
from geomcore import Ellipse
ellipse = Ellipse(Point.origin(), Vector.unit_z(), Vector.unit_x(), 3.0, 1.0)
p = ellipse.point_at(1.0) # parameter -> 3D point
assert abs(ellipse.parameter_of(p) - 1.0) < 1e-12 # 3D point -> parameter
use geomcore::Ellipse;
let ellipse = Ellipse::new(Point::ORIGIN, Vector::Z, Vector::X, 3.0, 1.0).unwrap();
let p = ellipse.point_at(1.0); // parameter -> 3D point
assert!((ellipse.parameter_of(p, Tolerance::DEFAULT) - 1.0).abs() < 1e-12); // 3D point -> parameter
The way back: \(u = \mathrm{atan2}(y/b,\; x/a)\) — divide out the radii and the ellipse becomes the circle whose angle is \(u\).
The foci sit at \(O \pm c\,\vec{X}\) with \(c = \sqrt{a^2 - b^2}\), and
the eccentricity is \(e = c/a\): foci(), eccentricity() and
directrices() give them, the directrices being the lines across the
major axis at \(a/e\) from the centre. A point of the curve is \(e\) times
as far from a focus as from that focus's directrix. A circle has
\(e = 0\), both foci at its centre and no directrices.
Hyperbola
\(\cosh\) is never below 1, so this map reaches one branch only; the
mirror branch is a second curve no value of \(u\) reaches, which
other_branch() builds.
from geomcore import Hyperbola
hyp = Hyperbola(Point.origin(), Vector.unit_z(), Vector.unit_x(), 2.0, 1.0)
p = hyp.point_at(1.0) # parameter -> 3D point
assert abs(hyp.parameter_of(p) - 1.0) < 1e-12 # 3D point -> parameter
use geomcore::Hyperbola;
let hyp = Hyperbola::new(Point::ORIGIN, Vector::Z, Vector::X, 2.0, 1.0).unwrap();
let p = hyp.point_at(1.0); // parameter -> 3D point
assert!((hyp.parameter_of(p, Tolerance::DEFAULT) - 1.0).abs() < 1e-12); // 3D point -> parameter
The way back: \(u = \mathrm{arsinh}(y/b)\). Implicit form: \(\frac{x^2}{a^2} - \frac{y^2}{b^2} = 1\).
The foci sit at \(O \pm c\,\vec{X}\) with \(c = \sqrt{a^2 + b^2}\), this
branch's own focus first in foci(); eccentricity() is \(c/a > 1\) and
directrices() are across the axis at \(a/e\) from the centre.
Parabola
The transpose of the schoolbook \(y = x^2\): the parameter runs along \(Y\), the curve opens along \(X\). \(f\) is the focal distance — focus at \((f, 0)\), directrix \(x = -f\).
from geomcore import Parabola
parab = Parabola(Point.origin(), Vector.unit_z(), Vector.unit_x(), 1.5)
p = parab.point_at(2.0) # parameter -> 3D point
assert abs(parab.parameter_of(p) - 2.0) < 1e-12 # 3D point -> parameter
use geomcore::Parabola;
let parab = Parabola::new(Point::ORIGIN, Vector::Z, Vector::X, 1.5).unwrap();
let p = parab.point_at(2.0); // parameter -> 3D point
assert!((parab.parameter_of(p, Tolerance::DEFAULT) - 2.0).abs() < 1e-12); // 3D point -> parameter
The way back: \(u = y\).
focus() and directrix() return those two, and
Parabola.from_focus_and_directrix(focus, directrix) builds the
parabola from them; its parameter then runs along the directrix's
direction. The eccentricity is always 1.
The five surfaces
Four are surfaces of revolution — a profile curve turned about an axis — so the two horizontal components are always some radius times \(\cos u\) and \(\sin u\). They differ only in that radius and in the third component. With \(\rho = \sqrt{x^2 + y^2}\) the distance from the axis, four of the five inverses start with the same \(u = \mathrm{atan2}(y, x)\).
Plane
Both parameters are lengths; the only surface with no angles, no periodicity, no degenerate point.
from geomcore import Plane
plane = Plane(Point.origin(), Vector.unit_z())
p = plane.point_at(1.0, 2.0) # parameters -> 3D point
assert (p.x, p.y, p.z) == (1.0, 2.0, 0.0)
assert plane.parameters_of(p) == (1.0, 2.0) # 3D point -> parameters
use geomcore::Plane;
let plane = Plane::new(Point::ORIGIN, Vector::Z).unwrap();
let p = plane.point_at(1.0, 2.0); // parameters -> 3D point
assert_eq!(p, Point::new(1.0, 2.0, 0.0));
assert_eq!(plane.parameters_of(p, Tolerance::DEFAULT), (1.0, 2.0)); // 3D point -> parameters
The way back: \(u = x,\; v = y\).
The plane is also how a 2D curve is placed in space: uv_curve_to_3d
maps any 2D curve through the plane's frame, so a Circle2D drawn in
the plane's \((u, v)\) coordinates comes back as the 3D Circle.
from geomcore import Circle2D, Plane, Point2D
plane = Plane(Point(0.0, 0.0, 5.0), Vector.unit_z())
circle = plane.uv_curve_to_3d(Circle2D(Point2D(1.0, 2.0), 0.5))
# Circle(center=Point(1.0, 2.0, 5.0), normal=Vector(0.0, 0.0, 1.0), radius=0.5)
Cylinder
\(u\) is an angle, \(v\) a length — the only surface whose parameters are not the same kind of quantity.
from geomcore import Cylinder
cyl = Cylinder(Point.origin(), Vector.unit_z(), 2.0)
p = cyl.point_at(1.0, 2.0) # parameters -> 3D point
u, v = cyl.parameters_of(p) # 3D point -> parameters
assert abs(u - 1.0) < 1e-12 and abs(v - 2.0) < 1e-12
use geomcore::Cylinder;
let cyl = Cylinder::new(Point::ORIGIN, Vector::Z, 2.0).unwrap();
let p = cyl.point_at(1.0, 2.0); // parameters -> 3D point
let (u, v) = cyl.parameters_of(p, Tolerance::DEFAULT); // 3D point -> parameters
assert!((u - 1.0).abs() < 1e-12 && (v - 2.0).abs() < 1e-12);
The way back: \(u = \mathrm{atan2}(y, x),\; v = z\).
Cone
The cylinder's formula with a radius that grows along the axis:
\(\rho = R + v\sin\alpha\). geomcore measures \(v\) along the ruling,
so \(\lVert \partial S/\partial v \rVert = 1\) exactly — a step of 1 in
\(v\) is a step of 1 across the surface. The alternative height
convention (\(v = z\), \(\rho = R + v\tan\alpha\)) shares the cylinder's
parameter plane instead; the article compares both.
from geomcore import Cone
import math
cone = Cone(Point.origin(), Vector.unit_z(), math.radians(28), 4.0)
p = cone.point_at(1.0, 2.0) # parameters -> 3D point
u, v = cone.parameters_of(p) # 3D point -> parameters
assert abs(u - 1.0) < 1e-9 and abs(v - 2.0) < 1e-9
use geomcore::Cone;
let cone = Cone::new(Point::ORIGIN, Vector::Z, 0.4887, 4.0).unwrap();
let p = cone.point_at(1.0, 2.0); // parameters -> 3D point
let (u, v) = cone.parameters_of(p, Tolerance::DEFAULT); // 3D point -> parameters
assert!((u - 1.0).abs() < 1e-9 && (v - 2.0).abs() < 1e-9);
The way back: \(u = \mathrm{atan2}(y, x),\; v = \sin\alpha\,(\rho - R) + \cos\alpha\,z\).
At \(v_{\mathrm{apex}} = -R / \sin\alpha\) the radius reaches zero: a
whole side of the rectangle is one point, the normal vanishes, and past
it \(\rho\) goes negative into the opposite nappe. The frame sits on the
reference circle (\(v = 0\)), not at the apex — the only point with no
normal is a bad datum, it is often hundreds of radii from the part, and
anchoring there would break the cone→cylinder limit as \(\alpha \to 0\).
When the apex is the datum at hand anyway, Cone.from_apex(apex,
axis_direction, half_angle) builds the cone whose reference circle is
the apex itself (\(R = 0\)).
Sphere
Both parameters are angles: longitude \(u\), latitude \(v\) from the equator. Both ends of \(v\) are degenerate — at the poles the whole circle of longitudes collapses onto the axis.
from geomcore import Sphere
sphere = Sphere(Point.origin(), 2.0)
assert abs(sphere.volume() - 4.0 / 3.0 * 3.141592653589793 * 8.0) < 1e-9
p = sphere.point_at(0.5, 0.3) # parameters -> 3D point
u, v = sphere.parameters_of(p) # 3D point -> parameters
assert abs(u - 0.5) < 1e-12 and abs(v - 0.3) < 1e-12
use geomcore::Sphere;
let sphere = Sphere::new(Point::ORIGIN, 2.0).unwrap();
let p = sphere.point_at(0.5, 0.3); // parameters -> 3D point
let (u, v) = sphere.parameters_of(p, Tolerance::DEFAULT); // 3D point -> parameters
assert!((u - 0.5).abs() < 1e-12 && (v - 0.3).abs() < 1e-12);
The way back: \(u = \mathrm{atan2}(y, x),\; v = \mathrm{atan2}(z, \rho)\).
Torus
The cone's shape with \(v\) turning the radius instead of growing it: \(\rho = R + r\cos v\). Both parameters close, so the domain — both pairs of opposite sides glued — is itself a torus, the most faithful rectangle of the five.
from geomcore import Torus
torus = Torus(Point.origin(), Vector.unit_z(), 5.0, 1.5)
p = torus.point_at(0.5, 1.0) # parameters -> 3D point
u, v = torus.parameters_of(p) # 3D point -> parameters
assert abs(u - 0.5) < 1e-12 and abs(v - 1.0) < 1e-12
use geomcore::Torus;
let torus = Torus::new(Point::ORIGIN, Vector::Z, 5.0, 1.5).unwrap();
let p = torus.point_at(0.5, 1.0); // parameters -> 3D point
let (u, v) = torus.parameters_of(p, Tolerance::DEFAULT); // 3D point -> parameters
assert!((u - 0.5).abs() < 1e-12 && (v - 1.0).abs() < 1e-12);
The way back: \(u = \mathrm{atan2}(y, x),\; v = \mathrm{atan2}(z, \rho - R)\). If \(r > R\), \(\rho\) has two zeros and the spindle self-intersects — the same five numbers describe a ring or a spindle, and only \(R > r\) says which.
What the parameters measure
The derivative magnitude \(\lVert \partial S/\partial u \rVert\) is the unit conversion of a parameter: where it is 1 the parameter is arc length; where it is a radius the parameter is an angle.
| Surface | \(\lVert \partial S/\partial u \rVert\) | \(u\) is | \(\lVert \partial S/\partial v \rVert\) | \(v\) is |
|---|---|---|---|---|
| Plane | 1 | a length | 1 | a length |
| Cylinder | \(R\) | an angle | 1 | a length |
| Cone | \(\rho = R + v\sin\alpha\) | an angle | 1 | a length along the ruling |
| Sphere | \(R\cos v\) | an angle | \(R\) | an angle |
| Torus | \(\rho = R + r\cos v\) | an angle | \(r\) | an angle |
Where the \(u\)-speed depends on \(v\), a uniform \((u, v)\) grid is not uniform on the surface — triangles crowd at the poles. Every mesher corrects for exactly this table.
Which way the normal points
normal(u, v) is the direction of
\(\partial S/\partial u \times \partial S/\partial v\). With the
parametrizations above that gives:
| Surface | The normal points |
|---|---|
| Plane | along the normal it was built with (\(\vec{X} \times \vec{Y}\)) |
| Cylinder | away from the axis |
| Cone | away from the axis |
| Sphere | out of the sphere |
| Torus | out of the tube |
Curvature signs follow the normal: a surface bending away from it has
negative curvatures, so a sphere of radius 2 reports \(-0.5\) for both.
reversed_u() and reversed_v() flip the normal, and with it the sign
of every curvature.
Converting to a B-spline changes the parameters
to_bspline() and to_bspline_patch() are exact as point sets, but a
rational quadratic arc does not run at constant angular speed: between
the knots the same parameter lands on a different point, up to 6% of
the radius away on a torus. Compare the two shapes through the inverse
map, not through the parameter:
import math
from geomcore import Point, Torus, Vector
torus = Torus(Point.origin(), Vector.unit_z(), 3.0, 1.0)
bspline = torus.to_bspline()
point = torus.point_at(0.3, 1.1)
u, v = bspline.parameters_of(point)
assert bspline.point_at(u, v).distance(point) < 1e-9
The plane, the line and the Bézier shapes keep their parameters.
Seams and collapsed boundaries
Three things go wrong, and all the special cases in surface code live at these three places:
- Seam — two parameters, one point. On a cylinder, \(u = 0\) and \(u =
2\pi\) evaluate \(9.8\times10^{-16}\) apart. The surface is fine; the
inverse must choose, and the geometry does not determine the
choice.
geomcorereports seam parameters insidebounds()and normalises hits there to 0. - Collapsed boundary — a whole parameter line that is one point: the
cone apex, the sphere poles. \(\lVert \partial S/\partial u \rVert\)
measures \(1.5\times10^{-15}\) at a pole and the normal cross product is
the zero vector.
normalrecovers the limiting normal where it is unique (a pole reports the axis) and returnsNoneat the apex, whose meridians each have their own;curvaturesreturnsNoneat both. - Self-intersection — the spindle torus above.
Fitting to points
Measured points rarely lie on a shape exactly. Plane.fit,
Circle.fit, Circle2D.fit, Sphere.fit and Cylinder.fit give the
shape that minimizes the sum of the squared distances from the points,
with two numbers to accept or reject it by: the root mean square and the
largest of those distances, the residuals. They are lengths, so they
scale with the points.
import math
from geomcore import Circle2D, Cylinder, Point, Point2D
# Points on an arc of radius 2, alternately 0.01 in and out.
radii = [2 + 0.01 * (-1) ** k for k in range(10)]
arc = [Point2D(r * math.cos(0.2 * k), r * math.sin(0.2 * k)) for k, r in enumerate(radii)]
fit = Circle2D.fit(arc)
assert abs(fit.shape.radius() - 2) < 1e-3
assert abs(fit.root_mean_square_residual - 0.01) < 1e-3
assert fit.maximum_residual < 0.015
# A quarter of a cylinder of radius 3 along z.
patch = [
Point(3 * math.cos(a), 3 * math.sin(a), z)
for a in (0.0, 0.4, 0.8, 1.2, 1.5)
for z in (0.0, 1.0, 2.0)
]
cylinder = Cylinder.fit(patch).shape
assert abs(cylinder.radius() - 3) < 1e-9
use geomcore::{Plane, Point, Tolerance};
let points = [
Point::new(0.0, 0.0, 0.1),
Point::new(1.0, 0.0, -0.1),
Point::new(1.0, 1.0, 0.1),
Point::new(0.0, 1.0, -0.1),
];
let fit = Plane::fit(&points, Tolerance::DEFAULT).unwrap();
assert!((fit.maximum_residual() - 0.1).abs() < 1e-12);
let plane = fit.into_shape();
The plane is closed form: through the centroid, across the axis along which the points spread least (the smallest eigenvector of their covariance). A circle or a sphere starts from its algebraic fit, whose equation \(x^2 + y^2 + Dx + Ey + F = 0\) is linear in its unknowns, and is then refined on the true distance by Levenberg–Marquardt steps. A circle in space first takes the plane of the points, and its refinement lets them lie off it. A cylinder has no linear start: its axis direction is searched over a hemisphere, one direction every 15 degrees, each scored by the algebraic circle of the points seen along it, and the best three are refined with the radius and the axis position; the closest wins. Points over an arc of the cylinder fit, not only whole rings, but a narrow arc of a few points can settle in a local minimum — the residuals then say so.
A fit refuses fewer points than the shape's degrees of freedom (3 for a
plane or a circle, 4 for a sphere, 5 for a cylinder), and points that do
not determine the shape: within the confusion tolerance of one point,
of one line (no plane, no circle), or of one plane (a circle lies on
many spheres, an ellipse on two cylinders), with a FitError that
says which.
The shape follows the points' order where it has an orientation: a circle runs through them in order, starting at the first, and the normal of a plane or the axis of a cylinder is the one they turn counterclockwise about.
Everything above can be written as NURBS — conics exactly, as rational quadratics — so why keep ten shapes? The explanation covers the four things the ten buy, and what the intersector pays for them.