git: 9front

Download patch

ref: a5bb5907b253c43c90a40cc377e87bbf7001fc02
parent: 659f503804a1e9fbfef23bcac74acdf2f06458fd
author: rodri <rgl@antares-labs.eu>
date: Fri Jan 2 17:48:10 EST 2026

libgeometry: remove Triangle[23] structs

this change keeps the routines, but using Point[23]s instead of the
triangle structs.

i'm also adding a couple of helper globals ZP[23], analogous to
libdraw's ZP.

--- a/sys/include/geometry.h
+++ b/sys/include/geometry.h
@@ -10,8 +10,6 @@
 typedef struct Quaternion Quaternion;
 typedef struct RFrame RFrame;
 typedef struct RFrame3 RFrame3;
-typedef struct Triangle2 Triangle2;
-typedef struct Triangle3 Triangle3;
 
 struct Point2 {
 	double x, y, w;
@@ -35,15 +33,9 @@
 	Point3 bx, by, bz;
 };
 
-struct Triangle2
-{
-	Point2 p0, p1, p2;
-};
+extern Point2	ZP2;
+extern Point3	ZP3;
 
-struct Triangle3 {
-	Point3 p0, p1, p2;
-};
-
 /* utils */
 double flerp(double, double, double);
 double fberp(double, double, double, Point3);
@@ -61,6 +53,8 @@
 double dotvec2(Point2, Point2);
 double vec2len(Point2);
 Point2 normvec2(Point2);
+Point2 centroid(Point2, Point2, Point2);
+Point3 barycoords(Point2, Point2, Point2, Point2);
 int edgeptcmp(Point2, Point2, Point2);
 int ptinpoly(Point2, Point2*, ulong);
 
@@ -77,6 +71,7 @@
 Point3 crossvec3(Point3, Point3);
 double vec3len(Point3);
 Point3 normvec3(Point3);
+Point3 centroid3(Point3, Point3, Point3);
 int lineXsphere(Point3*, Point3, Point3, Point3, double, int);
 int ptincylinder(Point3, Point3, Point3, double);
 int ptincone(Point3, Point3, Point3, double);
@@ -135,13 +130,6 @@
 Point3 rframexform3(Point3, RFrame3);
 Point2 invrframexform(Point2, RFrame);
 Point3 invrframexform3(Point3, RFrame3);
-
-/* Triangle2 */
-Point2 centroid(Triangle2);
-Point3 barycoords(Triangle2, Point2);
-
-/* Triangle3 */
-Point3 centroid3(Triangle3);
 
 /* Fmt */
 #pragma varargck type "v" Point2
--- a/sys/man/2/geometry
+++ b/sys/man/2/geometry
@@ -21,8 +21,6 @@
 typedef struct Quaternion Quaternion;
 typedef struct RFrame RFrame;
 typedef struct RFrame3 RFrame3;
-typedef struct Triangle2 Triangle2;
-typedef struct Triangle3 Triangle3;
 .PB
 struct Point2 {
 	double x, y, w;
@@ -46,15 +44,9 @@
 	Point3 bx, by, bz;
 };
 .PB
-struct Triangle2
-{
-	Point2 p0, p1, p2;
-};
+extern Point2	ZP2;
+extern Point3	ZP3;
 .PB
-struct Triangle3 {
-	Point3 p0, p1, p2;
-};
-.PB
 /* utils */
 double flerp(double a, double b, double t)
 double fberp(double a, double b, double c, Point3 bc)
@@ -72,6 +64,8 @@
 double dotvec2(Point2 a, Point2 b)
 double vec2len(Point2 v)
 Point2 normvec2(Point2 v)
+Point2 centroid(Point2 p0, Point2 p1, Point2 p2)
+Point3 barycoords(Point2 p0, Point2 p1, Point2 p2, Point2 p)
 int edgeptcmp(Point2 e0, Point2 e1, Point2 p)
 int ptinpoly(Point2 p, Point2 *pts, ulong npts)
 .PB
@@ -88,6 +82,7 @@
 Point3 crossvec3(Point3 a, Point3 b)
 double vec3len(Point3 v)
 Point3 normvec3(Point3 v)
+Point3 centroid3(Point3 p0, Point3 p1, Point3 p2)
 int lineXsphere(Point3 *rp, Point3 p0, Point3 p1,
 		Point3 c, double rad, int isaray)
 int ptincylinder(Point3 p, Point3 p0, Point3 p1, double r)
@@ -148,13 +143,6 @@
 Point2 invrframexform(Point2 p, RFrame rf)
 Point3 invrframexform3(Point3 p, RFrame3 rf)
 .PB
-/* Triangle2 */
-Point2 centroid(Triangle2 t)
-Point3 barycoords(Triangle2 t, Point2 p)
-.PB
-/* Triangle3 */
-Point3 centroid3(Triangle3 t)
-.PB
 /* Fmt */
 #pragma varargck type "v" Point2
 #pragma varargck type "V" Point3
@@ -282,6 +270,15 @@
 .I v
 and returns a new 2D point.
 .TP
+.B centroid(\fIp0\fP,\fIp1\fP,\fIp2\fP)
+Returns the geometric center of a triangle defined by three points.
+.TP
+.B barycoords(\fIp0\fP,\fIp1\fP,\fIp2\fP,\fIp\fP)
+Returns a 3D point that represents the barycentric coordinates of the
+2D point
+.I p
+relative to the triangle defined by the first three points.
+.TP
 .B edgeptcmp(\fIe0\fP,\fIe1\fP,\fIp\fP)
 Performs a comparison between an edge, defined by a directed line from
 .I e0
@@ -373,6 +370,9 @@
 .I v
 and returns a new 3D point.
 .TP
+.B centroid3(\fIp0\fP,\fIp1\fP,\fIp2\fP)
+Returns the geometric center of a triangle defined by three points.
+.TP
 .B lineXsphere(\fIrp\fP,\fIp0\fP,\fIp1\fP,\fIc\fP,\fIrad\fP,\fIisaray\fP)
 Finds the intersection between the line defined by
 .I p0
@@ -728,27 +728,6 @@
 .IR rf ,
 into a point relative to the origin rframe O.
 It then returns the new 3D point.
-.SS Triangles
-.TP
-Name
-Description
-.TP
-.B centroid(\fIt\fP)
-Returns the geometric center of
-.B Triangle2
-.IR t .
-.TP
-.B barycoords(\fIt\fP,\fIp\fP)
-Returns a 3D point that represents the barycentric coordinates of the
-2D point
-.I p
-relative to the triangle
-.IR t .
-.TP
-.B centroid3(\fIt\fP)
-Returns the geometric center of
-.B Triangle3
-.IR t .
 .SH EXAMPLES
 .PP
 Rotate a point p by θ, scale it 2x, then translate it by vector v:
--- a/sys/src/libgeometry/mkfile
+++ b/sys/src/libgeometry/mkfile
@@ -7,7 +7,6 @@
 	matrix.$O\
 	quaternion.$O\
 	rframe.$O\
-	triangle.$O\
 	utils.$O\
 	fmt.$O\
 
--- a/sys/src/libgeometry/point.c
+++ b/sys/src/libgeometry/point.c
@@ -2,6 +2,9 @@
 #include <libc.h>
 #include <geometry.h>
 
+Point2 ZP2;
+Point3 ZP3;
+
 /* 2D */
 
 Point2
@@ -83,7 +86,34 @@
 	return (Point2){v.x/len, v.y/len, 0};
 }
 
+Point2
+centroid(Point2 p0, Point2 p1, Point2 p2)
+{
+	return divpt2(addpt2(p0, addpt2(p1, p2)), 3);
+}
+
 /*
+ * based on the implementation from:
+ *
+ * Dmitry V. Sokolov, “Tiny Renderer: Lesson 2”,
+ * https://github.com/ssloy/tinyrenderer/wiki/Lesson-2:-Triangle-rasterization-and-back-face-culling
+ */
+Point3
+barycoords(Point2 p0, Point2 p1, Point2 p2, Point2 p)
+{
+	Point2 p0p1 = subpt2(p1, p0);
+	Point2 p0p2 = subpt2(p2, p0);
+	Point2 pp0  = subpt2(p0, p);
+
+	Point3 v = crossvec3(Vec3(p0p2.x, p0p1.x, pp0.x), Vec3(p0p2.y, p0p1.y, pp0.y));
+
+	/* handle degenerate triangles—i.e. the ones where every point lies on the same line */
+	if(fabs(v.z) < 1)
+		return Pt3(-1,-1,-1,1);
+	return Pt3(1 - (v.x + v.y)/v.z, v.y/v.z, v.x/v.z, 1);
+}
+
+/*
  * the edge function, from:
  *
  * Juan Pineda, “A Parallel Algorithm for Polygon Rasterization”,
@@ -215,6 +245,12 @@
 	if(len == 0)
 		return (Point3){0,0,0,0};
 	return (Point3){v.x/len, v.y/len, v.z/len, 0};
+}
+
+Point3
+centroid3(Point3 p0, Point3 p1, Point3 p2)
+{
+	return divpt3(addpt3(p0, addpt3(p1, p2)), 3);
 }
 
 int
--- a/sys/src/libgeometry/triangle.c
+++ /dev/null
@@ -1,40 +1,0 @@
-#include <u.h>
-#include <libc.h>
-#include <geometry.h>
-
-/* 2D */
-
-Point2
-centroid(Triangle2 t)
-{
-	return divpt2(addpt2(t.p0, addpt2(t.p1, t.p2)), 3);
-}
-
-/*
- * based on the implementation from:
- *
- * Dmitry V. Sokolov, “Tiny Renderer: Lesson 2”,
- * https://github.com/ssloy/tinyrenderer/wiki/Lesson-2:-Triangle-rasterization-and-back-face-culling
- */
-Point3
-barycoords(Triangle2 t, Point2 p)
-{
-	Point2 p0p1 = subpt2(t.p1, t.p0);
-	Point2 p0p2 = subpt2(t.p2, t.p0);
-	Point2 pp0  = subpt2(t.p0, p);
-
-	Point3 v = crossvec3(Vec3(p0p2.x, p0p1.x, pp0.x), Vec3(p0p2.y, p0p1.y, pp0.y));
-
-	/* handle degenerate triangles—i.e. the ones where every point lies on the same line */
-	if(fabs(v.z) < 1)
-		return Pt3(-1,-1,-1,1);
-	return Pt3(1 - (v.x + v.y)/v.z, v.y/v.z, v.x/v.z, 1);
-}
-
-/* 3D */
-
-Point3
-centroid3(Triangle3 t)
-{
-	return divpt3(addpt3(t.p0, addpt3(t.p1, t.p2)), 3);
-}
--