Geometries and Algebras Refreshment SzirmayKalos Lszl Euclidean planar
Geometries and Algebras Refreshment Szirmay-Kalos László
• • Euclidean planar geometry Axioms of Euclidean geometry of the plane: 1. Two points define a line. 2. A line has at least two points. 3. There is exactly one line that goes through a point and does not intersect a Axioms given line (parallelism). – Basic truth (experience) 4. Movement does not modify size – Implicit definition of basic concepts 5. Size of the whole is the sum of the sizes of its parts Definitions and theorems 6. … T 1. Two different lines intersect each other at most one point T 3. The sum of angles of a triangle is the half angle, i. e. 180 degrees. T 2. Two lines are parallel if and only if the angles in which a third line intersects them are equal. T 4. Pythagoras: a 2+b 2 = c 2 Is Euclidean geometry good for earth (Geo) measuring (meter)?
Curvature of curves + = 1/r - f = f ’’(x 0) • Centripetal acceleration of a motion of unit speed (acp = v 2/r) • Reciprocal of the radius of the osculating circle • If the first derivative is zero, then the second derivative:
Curvature of Surfaces: Principal curvature directions and Gaussian curvature = 1 2
Gauss curvature >0 = 1 2 <0
Spherical geometry • Line = main circle (shortest path) • Lines always intersect • Sum of triangles > 180 deg (proportionally to the area) • Pythagoras: a 2+b 2 > c 2 • Similarity = Congruence Mercator Only the parallel axiom changes: Euclidean axioms are wrong: 1. Two points (=diameter) Two points not always define a line. determine a 2. line (=geodesic, A line has at least two points. main circle). 2. 3. A line has at least two points. Two lines always intersect. 3. 4. Two Movement does not modify size lines always intersect (=diameter). 4. 5. Movement does not modify size Size of the whole is the sum of the sizes 5. Size of the whole is the sum of the sizes of its parts 6. of its parts … 6. …
Hyperbolic geometry Negative curvature Line = shortest path Many parallels Sum of triangles < 180 deg (proportionally to the area) • Pythagoras: a 2+b 2 < c 2 • Similarity = Congruence • • Beltrami Poincaré disc Esher: Circle Limit III Axioms of hyperbolic geometry: 1. Two points define a line (shortest path). 2. A line has at least two points. 3. There are many lines that go through a point and do not intersect a given line (parallelism). 4. Movement does not modify size 5. Size of the whole is the sum of the sizes of its parts 6. …
Tiling with regular, congruent polygons (Tessellation) • Euclidean geometry 36 120 108 60 (3, 6) 90 (6, 3) (4, 4) • Hyperbolic geometry (3, 7) (3, 8) (5, 4) (5, 5)
Projective geometry • • • Axioms of projective geometry: 1. Two points define a line. 2. A line has at least two points. 3. Two different lines always intersect in a single point. 4. … Infinity is also part of our world There are no parallel lines Programming advantage: no singularity Cartesian or polar coordinates are not good (no distance) Geometry of shadows or of the straight edge without scale
With numbers: Analytic geometry axioms • Two points define a line. • A line has at least two points. • If a is a line and A is a point not on this line, then there is exactly one other line that crosses point A but not line a. point numbers plane line intersect operations equations is on functions geometry C++ class: variables+ operations algebra objects program
Vector: algeabric base of Euclidean geometry point • Vector = translation: v • Direction and length (|v|) • Position vector BUT vector ≠ point !!! origin • Vector addition v = v 1 + v 2 (commutative, assoc. ) v 1 = v - v 2 (has inverse) • Scaling (multiplication with a scalar) v v 1 = a v (distributive) Position vector v 1 v 2 a v
Scalar (dot, inner) product v 1 v 2 Algebraic definition: v 1 v 2 = |v 1| |v 2| cos |v 1| cos Geometric meaning: Projection of one vector onto the other * other’s length Properties: Not associative!!! Commutative v 1 v 2 = v 2 v 1 Distributive v 3 (v 2+v 1) = v 3 v 2 + v 3 v 1 v 2 v 1 v 3 |v 1| cos v v = |v|2 Two vectors are orthogonal if their dot product is zero.
Definition: Vector (cross) product v 1 v 2 |v 1 v 2| = |v 1| |v 2| sin Perpendicular, right hand rule v 2 v 1 Geometric meanings: 1. Area and perpendicular vector, 2. (Projection of one vector onto the plane that is perpendicular to the other + rotation by 90 degrees) * other’s length v 2 v 1 Properties: Not associative!!! Not commutative (Anti-symmetric) v 1 v 2 = - v 2 v 1 Distributive v 3 (v 2+v 1) = v 3 v 2 + v 3 v 1 |v 2| sin 90 degrees v 1 v 2 Two vectors are parallel if their cross product is zero.
Cartesian coordinate system • Origin + two perpendicular unit vectors: i 2= j 2=1, i j = 0 v = xi + yj • Any vector v can be unambiguously given yj as a linear combination (x = v i, y = v j) • Operations with coordinates Addition: v 1 + v 2 = (x 1+x 2)i + (y 1+y 2)j j Dot product: v 1 v 2 = (x 1 i + y 1 j) (x 2 i + y 2 j) = (x 1 x 2 + y 1 y 2) Cross product: v 1 v 2 = (x 1 i + y 1 j + z 1 k) (x 2 i + y 2 j + z 2 k) = (y 1 z 2 – y 2 z 1) i + (x 2 z 1 – x 1 z 2) j + (x 1 y 2 – y 1 x 2)k Length: |v| = v v = x 2 + y 2 + z 2 xi origin i i j k x 1 y 1 z 1 x 2 y 2 z 2 Phythagoras theorem for free.
struct vec 3 { float x, y, z; Vector/Point/Color class vec 3(float x 0, float y 0, float z 0) { x = x 0; y = y 0; z = z 0; } vec 3 operator*(float a) { return vec 3(x * a, y * a, z * a); } vec 3 operator+(const vec 3& return vec 3(x + v. x, y } vec 3 operator-(const vec 3& return vec 3(x - v. x, y } vec 3 operator*(const vec 3& return vec 3(x * v. x, y } v) { // vector, color, point + vector + v. y, z + v. z); v) { // vector, color, point - v. y, z - v. z); v) { * v. y, z * v. z); }; float length() { return sqrtf(x * x + y * y + z * z); } float dot(const vec 3& v 1, const vec 3& v 2) { return (v 1. x * v 2. x + v 1. y * v 2. y + v 1. z * v 2. z); } vec 3 cross(const vec 3& return vec 3( v 1. y * v 1. z * v 1. x * } v 1, const vec 3& v 2) { v 2. z - v 1. z * v 2. y, v 2. x – v 1. x * v 2. z, v 2. y – v 1. y * v 2. x);
Combination of points r 3 r 2 r r 1 m 2 m 3 r 4 m 4 From physics: Torque is zero S (ri – r) mi g = 0 S miri r = S mj r is the combination of points r 1, r 2, …, rn Weights are the barycentric coordinates If weights are non negative: convex combination Convex combination is in the convex hull Line (line segment) = Two points’ (convex) combination Plane (triangle) = Three points’ (convex) combination
Line (segment) as combination of two points (x 2, y 2) (x(t), y(t)) (x 1, y 1) v m 2=t y x m 1=1 -t x(t)=x 1(1 -t)+ x 2 t y(t)=y 1(1 -t)+ y 2 t S miri r = S mj Parametric equation r(t) = r 0 + v t, t [-∞, ∞] x(t) = x 1 + vx t y(t) = y 1 + vy t
2 D line normal vector n direction vector v y r 0 r Implicit equation n (r – r 0) = 0 nx (x – x 0) + ny (y – y 0) = 0 ax + by + c = 0 (x, y, 1) (a, b, c) = 0 x Distance from a 2 D line: n (r – r 0) = Projection onto n-re * length of n n r r 0 If n is a unit vector: n (r – r 0) is the signed distance!
Plane normal vector n z r 0 n (r – r 0) = 0 r y nx (x–x 0) + ny (y–y 0) + nz (z–z 0) = 0 ax + by + cz + d = 0 (x, y, z, 1) (a, b, c, d) = 0 x If n is unit vector: n (r – r 0) is the signed distance from the plane!
struct vec 4 { float x, y, z, w; Vector/Point/Plane/RGBA class vec 4(float x 0, float y 0, float z 0, float w 0) { x = x 0; y = y 0; z = z 0; w = w 0; // vector: 0, point: 1, plane: d, RGBA: Opacity } vec 4 operator*(float a) { return vec 3(x * a, y * a, z * a, w * a); } vec 4 operator+(const vec 4& return vec 4(x + v. x, y } vec 4 operator-(const vec 4& return vec 4(x - v. x, y } vec 4 operator*(const vec 4& return vec 4(x * v. x, y } v) { // vector, color, point + vector + v. y, z + v. z, w + v. w); v) { // vector, color, point - v. y, z - v. z, w – v. w); v) { * v. y, z * v. z, w * v. w); }; float length() { return sqrtf(x * x + y * y + z * z + w * w); } float dot(const vec 4& v 1, const vec 4& v 2) { return (v 1. x * v 2. x + v 1. y * v 2. y + v 1. z * v 2. z + v 1. w * v 2. w); } vec 4 cross(const vec 4& v 1, const vec 4& v 2) { return vec 4(v 1. y * v 2. z - v 1. z * v 2. y, v 1. z * v 2. x – v 1. x * v 2. z, v 1. x * v 2. y – v 1. y * v 2. x, 0); }
Circle on a plane Implicit equation: Set of points r(x, y) of distance R from center c(cx, cy): |r – c| = R (r – c)2 = R 2 (x – cx)2 + (y – cy)2 = R 2 Parametric equation: Definition of sin( ) and cos( ) : x( ) = cx + R cos( ) y( ) = cy + R sin( )
Application of vector algebra: Pixel-driven 2 D rendering Model Image viewport Camera window World coordinate system Unit=pixel
2 D containment test • Ellipse: set of those points r (z=0, w=1) where the sum of distances from points f 1, f 2 (z=0, w=1) is smaller than constant C: bool in. Ellipse(vec 4 r, vec 4 f 1, vec 4 f 2, float C) { return (length(r – f 1) + length(r – f 2) < C); } • Parabola: set of those points r (z=0, w=1) which are closer to focus point f (z=0, w=1) than to the line (x=n. x, y=n. y, z=0, w=dot(n, p)) of point p (z=0, w=1) and normal vector n (z=0, w=0): bool in. Parabola (vec 4 r, vec 4 f, vec 4 p, vec 4 n) { n = n * (1/length(n)); vec 4 line(n. x, n. y, 0, -dot(n, p)); return (fabs(dot(line, r)) > length(r – f)); }
Convex polygon/polyhedron containment + - bool in. Poly(vec 4 r, vector<vec 4>& boundary) { for (vec 4 limit : boundary) { if (dot(r, limit) > 0) return false; } return true; }
Not only translation: Affine transformations • Translation, rotation, scaling, shearing, mirroring, … • Line to line, parallels are preserved p (0, y) (0, 1) (0, 0) T(p) T(0, y) T(0, 1) (1, 0) (x, 0) T(0, 0) T(1, 0) T(p) = T(0, 0)+(T(x, 0)-T(0, 0))+(T(0, y)-T(0, 0)) = T(0, 0)+x(T(1, 0)-T(0, 0))+y(T(0, 1)-T(0, 0)) = 1 o’ +xi’ +yj’ T(x, 0)
Affine transformations • Matrix multiplication: i’ a 11 a 12 0 j’ a 21 a 22 0 [x’, y’, 1] = [x, y, 1] a 31 a 32 1 o’ – Associative, distributive – Not commutative -sin • Example: rotation j’ j cos sin 0 cos -sin 0 cos 0 0 1 -i cos i’ sin i
mat 4 : transpose and inverse cleverly struct vec 4 { float x, y, z, w; float& operator[](int j) { return *(&x + j); } }; struct mat 4 { vec 4 rows[4]; mat 4(vec 4 it, vec 4 jt, vec 4 kt, vec 4 ot) { rows[0] = it; rows[1] = jt; rows[2] = kt; rows[3] = ot; } vec 4& operator[](int i) { return rows[i]; } }; vec 4 operator*(vec 4 v, mat 4 mat) { return v[0]*mat[0] + v[1]*mat[1] + v[2]*mat[2] + v[3]*mat[3]; } mat 4 operator*(mat 4 l, mat 4 r) { mat 4 res; for (int i = 0; i < 4; i++) res. rows[i] = l. rows[i] * r; return res; }
mat 4: translation, scaling, Z-rotation mat 4 Translate. Matrix(vec 3 return mat 4( vec 4(1, vec 4(0, vec 4(t. x, } t) { 0, 1, 0, t. y, 0, 0, 1, t. z, 0), 0), 1)); mat 4 Scale. Matrix(vec 3 s) { // with respect to the origin return mat 4( vec 4(s. x, 0, 0, 0), vec 4(0, s. y, 0, 0), vec 4(0, 0, s. z, 0), vec 4(0, 0, 0, 1)); } mat 4 Rotate. ZMatrix(float phi) { // around return mat 4( vec 4( cos(phi), sin(phi), vec 4(-sin(phi), cos(phi), vec 4(0, 0, } axis Z 0, 0), 1, 0), 0, 1));
CPU init Green triangle gl. Gen. Vertex. Arrays(1, &vao); gl. Bind. Vertex. Array(vao); gl. Gen. Buffers(1, &vbo); gl. Bind. Buffer(GL_ARRAY_BUFFER, vbo); float vtx[] = {-0. 8, 0, 1, -0. 6, 1, 0. 8, -0. 2, 0, 1}; gl. Buffer. Data(GL_ARRAY_BUFFER, sizeof(vtx), vtx, GL_STATIC_DRAW); gl. Enable. Vertex. Attrib. Array(0); // attribute array 0 gl. Vertex. Attrib. Pointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL); CPU draw gl. Uniform 4 f(gl. Get. Uniform. Location(gpu, “color"), 0, 1); float mat[] = {1, 0, 0, 1, 0, 0, 1}; gl. Uniform. Matrix 4 fv(gl. Get. Uniform. Location(gpu, "MVP"), 1, 1, mat); gl. Viewport(0, 0, 600); gl. Bind. Vertex. Array(vao); gl. Draw. Arrays(GL_TRIANGLES, 0, 3); Vertex shader Pixel shader uniform mat 4 MVP; layout(location = 0) in vec 4 v; uniform vec 4 color; out vec 4 out. Color; void main() { gl_Position = v * MVP; } void main() { out. Color = color; }
Algebra of complex numbers • struct Complex { float x, y; Complex(float x 0, float y 0) { x = x 0, y = y 0; } // Constructor Complex operator+(Complex r) { return Complex(x + r. x, y + r. y); } Complex operator-(Complex r) { return Complex(x - r. x, y - r. y); } Complex operator*(Complex r) { return Complex(x * r. x - y * r. y, x * r. y + y * r. x); } Complex operator/(Complex r) { float l = r. x * r. x + r. y * r. y; return (*this) * Complex(r. x / l, -r. y / l); // conjugate } }; Complex Polar(float r, float phi) { return Complex(r * cosf(phi), r * sinf(phi)); } // Constructor
Application of complex numbers Ex. : 2 D transformations Around pivot point (1, -1) point p is scaled by 2 and is rotated by angle t, then translated by vector (2, 3), and finally scaled by 0. 8 and rotated –t/2 radians with respect to the origin: Complex p, tp; Complex pivot(1, -1); tp = (((p - pivot) * Polar(2, t) + pivot) + Complex(2, 3)) * Polar(0. 8, -t/2);
Derivation is important
Example: shading, normal vector
How NOT to derivate! Subtraction: 7 valid digits 1 valid digit
(William) Clifford algebra: Hyper number • Let us teach C++ to derivate! function derivative • Hyper number: z = x + yi, where – i 2 = -1: complex; i 2 = 1: hyperbolic; … – i 2 = 0 : dual number sum/ difference function sum/dif Derivate sum/dif product division function product Product derivate Function division Division derivate
Dual number class struct Dnum { float f, d; // function and derivative values Dnum(float f 0, float d 0 = 0) { f = f 0, d = d 0; } Dnum operator+(Dnum r) {return Dnum(f + r. f, d + r. d); } Dnum operator*(Dnum r) { return Dnum(f * r. f, f * r. d + d * r. f); } Dnum operator/(Dnum r) { return Dnum(f / r. f, (r. f * d-r. d * f) / r. f); } }; Without derivation: float t = value; float F = t * a / (t * t + b); With derivation: Dnum F = Dnum(t, 1)* Dnum(a, 0)/(Dnum(t, 1)* Dnum(t, 1)+Dnum(b, 0)); Taking advantage of the default constructor: Dnum t(value, 1); Dnum F = t * a / (t * t + b);
Math functions, nested functions float F, x, y, a; … F = 3 * t + a * sin(t) + cos(y * log(t) + 2); struct Dnum { float f, d; // function and derivative values Dnum(float f 0, float d 0 = 0) { f = f 0, d = d 0; } … }; Dnum Sin(float t) { return Dnum(sinf(t), cosf(t)); } Dnum Cos(float t) { return Dnum(cosf(t), -sinf(t)); } …
Math functions, nested functions float F, x, y, a; … F = 3 * t + a * sin(t) + cos(y * log(t) + 2); struct Dnum { float f, d; // function and derivative values Dnum(float f 0, float d 0 = 0) { f = f 0, d = d 0; } … }; Dnum Sin(Dnum g) { return Dnum(sinf(g. f), cosf(g. f) * g. d); } Dnum Cos(Dnum g) { return Dnum(cosf(g. f), -sinf(g. f) * g. d); } Dnum Tan(Dnum g) { return Sin(g)/Cos(g); } Dnum Log(Dnum g) { return Dnum(logf(g. f), 1/g. f * g. d); } Dnum Exp(Dnum g) { return Dnum(expf(g. f), expf(g. f) * g. d); } Dnum Pow(Dnum g, float n) { return Dnum(powf(g. f, n), n * powf(g. f, n - 1) * g. d); }
Multi-variate functions template<class T> struct Dnum { float f; // function value T d; // derivatives Dnum(float f 0, T d 0 = T(0)) { f Dnum operator+(Dnum r) { return Dnum operator*(Dnum r) { return Dnum operator/(Dnum r) { return Dnum(f / r. f, (r. f * } }; = f 0, d = d 0; } Dnum(f+r. f, d+r. d); } Dnum(f*r. f, f*r. d + d*r. f); } d - r. d * f) / r. f); template<class T> Dnum<T> Exp(Dnum<T> g) { return Dnum<T>(expf(g. f), expf(g. f)*g. d); } … F(x, y, z) gradient: float x, y, z; Dnum<vec 3> X(x, vec 3(1, 0, 0)), Y(y, vec 3(0, 1, 0)), Z(z, vec 3(0, 0, 1)); Dnum F = X*X/a + Y*Y/b + Z*Z/c – 1; vec 3 grad = F. d;
Application: Path animation jt it Path: void Animate(float tt) { Dnum t(tt, 1) Dnum x = Sin(t)*(Sin(t)+3)*4 / (Tan(Cos(t))+2); Dnum y = (Cos(Sin(t)*8+1)*12+2)/(Pow(Sin(t)*Sin(t), 3)+2); float v = sqrtf(x. d * x. d + y. d * y. d); // velocity float mat[] = ( x. d/v, y. d/v, 0, // i’ -y. d/v, x. d/v, 0, // j’ x. f, y. f, 1); // o’ Draw(mat); }
Execises • Which vector operations are commutative, associative or distributive? Proof? • 2 D: – Implicit equation: points are in equal distance from point f and line defined by position vector r 0 and normal n, – Implicit equation: sum of the distances from two points p 1, p 2 is a constant C, – Parametric equation of the ellipse of main axis parallel with x (scaling of a circle). • 3 D – Implicit equation: points that are in distance R from a line defined by position vector r 0 and direction vector v, – Distance of two lines defined by position vector and direction vector pairs, (r 1, v 1) and (r 2, v 2), – Implicit equation: the sum of squared distances from given reference points p 1, p 2, etc. is constant C, – Prove that (a×b) c = a (b×c) // Hint: volume of parallelepiped.
- Slides: 41