![]() |
The algebra library and three dimensional objects | ![]() |
Graphics/Mathematics/
is a small header-only library of generic mathematical containers. Only matrices and polynomials are used by the rest of TeXmacs; the other classes are an experiment in generic numerical programming, exercised only by the self test test_math (
test_math.cpp
), which is called from texmacs_entrypoint only when ENABLE_TESTS is defined in
Texmacs/Texmacs/texmacs.cpp
(it is commented out).
matrix<T> (
matrix.hpp
) is a concrete reference counted structure holding NR (m) rows and NC (m) columns in row major order; m (i, j) accesses an entry. The constructor matrix<T> (c, rows, cols) creates a matrix with c on the diagonal and zeros elsewhere, so matrix<double> (1.0, 3, 3) is the identity. The operations are:
componentwise +, - and unary operations (unary and binary with an operator class from
operators.hpp
);
the matrix product, and the product of a matrix with a vector<T> or an array<T> (both assert matching dimensions);
transpose, matrix_2D (a, b, c, d), and invert, which uses the closed formula for \(2 \times 2\) matrices and Gauss-Jordan elimination with partial pivoting otherwise (asserting that the matrix is invertible);
projective_apply (m, v), which applies an \((n + 1) \times (n + 1)\) matrix to an \(n\)-dimensional point in homogeneous coordinates (or to an array of points);
conversion to and from trees: as_tree produces a tuple of row tuples, and as_matrix<T> parses one (asserting that it is a non empty tuple of tuples).
Matrices are used by the linear_2D and affine_2D frames (see points, frames, curves and grids), by the glyph transformations of
Graphics/Bitmap_fonts/glyph_transforms.cpp
, by the (tuple "linear" …) transformations of gr-transform and the three dimensional graphics in
Typeset/Concat/concat_graphics.cpp
, and by the (unused) least squares Bezier fitting of
Graphics/Types/curve_extras.cpp
.
polynomial<T> (
polynomial.hpp
) stores its coefficients by increasing degree. p (x) evaluates it with Horner's scheme and p (x, k) evaluates its \(k\)-th derivative; there are +, -, *, derive and products with scalars and arrays. The spline curves of
Graphics/Types/curve.cpp
represent each basis function piece as a polynomial<double> (dpol), and each piece of the curve as a vector of such polynomials (dpols), one per coordinate.
vector<T> (
vector.hpp
) is a fixed size vector with componentwise arithmetic, elementary functions, scalar operations, square_norm, norm and derive. Note that the geometric point type is not a vector but an array<double>.
(
ball.hpp
) Ball (interval) arithmetic: a center and a radius, with arithmetic and elementary functions which enclose the exact result.
(
function.hpp
,
function_extra.hpp
) Abstract functions which can be evaluated at a point or on a ball, built from constants, coordinates, arithmetic and elementary functions, piecewise definitions (pw_function), vectors of functions and polynomials.
(
operators.hpp
,
properties.hpp
) Operator classes such as add_op or neg_op, with an op method and a diff method for the derivative, and traits classes giving the scalar, norm and product types of a type. The operator classes are also used by the raster pictures (
Graphics/Pictures/raster_operators.hpp
).
(
math_tree.hpp
,
math_tree.cpp
) Arithmetic on trees (add, mul, sqrt, …), which lets the generic code run on symbolic values, and as_math_string to print them.
The experimental tags object-3d, triangle-3d, transform-3d and light-3d describe a scene made of colored triangles, which is drawn inside a graphics. They are listed among the graphical primitives and declared in
Data/Drd/drd_std.cpp
:
Three points with three coordinates and a color.
A list of triangle-3ds.
An object and a \(4 \times 4\) matrix (a tuple of row tuples) acting on homogeneous coordinates.
An object and a light, which is either (light-diffuse point shadow-color light-color) or (light-specular point point color). A light position with four coordinates is taken in homogeneous coordinates.
concater_rep::typeset_graphics_3d (
concat_graphics.cpp
) evaluates the tag, converts it with as_spacial and composes the result with a \(4 \times 4\) matrix built from the frame env->fr of the picture and the variable gr-transformation; the \(z\) coordinate is kept for depth sorting. Invalid input gives the error "bad spacial object". The result is a spacial_box (
Typeset/Boxes/Graphics/graphics_boxes.cpp
), which draws itself by renderer_rep::draw_spacial.
A spacial (
Graphics/Spacial/spacial.hpp
) is an abstract handle with the methods get_extents, draw (renderer), transform (matrix) and enlighten (light). There are three implementations:
(
triangulated.cpp
) An array of triangles with one color each. Before drawing, the triangles are sorted by increasing mean \(z\) coordinate and drawn in that order (a painter's algorithm, without splitting of intersecting triangles); each is drawn with renderer_rep::draw_triangle. transform applies projective_apply to all vertices; enlighten recomputes the colors: a diffuse light mixes the shadow and light colors according to the angle between the normal of the triangle and the direction of the light, a specular light adds a highlight with an eighth power falloff; the computations use true_color and source_over (see colors).
(
transformed.cpp
,
enlightened.cpp
) Lazy wrappers which apply the transformation or the light on first use.
The renderers do not know about three dimensional objects: the generic renderer_rep::draw_spacial just calls obj->draw, which ends up in ordinary filled triangles.
The products of a matrix with a vector or an array initialize the result with a loop over the number of columns instead of rows (Graphics/Mathematics/matrix.hpp:200-201, 213-214). For a non square matrix with more rows than columns, the extra entries start from uninitialized values; with fewer rows, the loop writes past the end of the result. All current callers use square matrices.
The diagonal constructor sets the entries whose linear index is a multiple of \(\operatorname{cols}+ 1\) (
matrix.hpp:54
), which also hits off-diagonal entries when a matrix has more than \(\operatorname{cols}+ 1\) rows.
invert asserts invertibility for large matrices but divides by a zero determinant without warning for \(2 \times 2\) matrices (
matrix.hpp:249
); a singular (tuple "linear" …) transformation therefore produces infinite coordinates.
The depth sorting of triangulated does not resolve intersecting or cyclically overlapping triangles,. Lights are evaluated in the coordinates of the light-3d tag, before the transformation into the picture.