code: plan9front

Download patch

ref: e5561c364d58f2b93b4c281382cc8609dbade7be
parent: f97cf2ac238cb993721405e5c7641f3995d8a468
author: rodri <rgl@antares-labs.eu>
date: Thu Mar 21 07:38:02 EDT 2024

libgeometry: add barycentric interpolation routines

--- a/sys/include/geometry.h
+++ b/sys/include/geometry.h
@@ -46,6 +46,7 @@
 
 /* utils */
 double flerp(double, double, double);
+double fberp(double, double, double, Point3);
 double fclamp(double, double, double);
 
 /* Point2 */
@@ -56,6 +57,7 @@
 Point2 mulpt2(Point2, double);
 Point2 divpt2(Point2, double);
 Point2 lerp2(Point2, Point2, double);
+Point2 berp2(Point2, Point2, Point2, Point3);
 double dotvec2(Point2, Point2);
 double vec2len(Point2);
 Point2 normvec2(Point2);
@@ -70,6 +72,7 @@
 Point3 mulpt3(Point3, double);
 Point3 divpt3(Point3, double);
 Point3 lerp3(Point3, Point3, double);
+Point3 berp3(Point3, Point3, Point3, Point3);
 double dotvec3(Point3, Point3);
 Point3 crossvec3(Point3, Point3);
 double vec3len(Point3);
--- a/sys/man/2/geometry
+++ b/sys/man/2/geometry
@@ -1,6 +1,6 @@
 .TH GEOMETRY 2
 .SH NAME
-Flerp, fclamp, Pt2, Vec2, addpt2, subpt2, mulpt2, divpt2, lerp2, dotvec2, vec2len, normvec2, edgeptcmp, ptinpoly, Pt3, Vec3, addpt3, subpt3, mulpt3, divpt3, lerp3, dotvec3, crossvec3, vec3len, normvec3, identity, addm, subm, mulm, smulm, transposem, detm, tracem, minorm, cofactorm, adjm, invm, xform, identity3, addm3, subm3, mulm3, smulm3, transposem3, detm3, tracem3, minorm3, cofactorm3, adjm3, invm3, xform3, Quat, Quatvec, addq, subq, mulq, smulq, sdivq, dotq, invq, qlen, normq, slerp, qrotate, rframexform, rframexform3, invrframexform, invrframexform3, centroid, barycoords, centroid3, vfmt, Vfmt, GEOMfmtinstall \- computational geometry library
+Flerp, fberp, fclamp, Pt2, Vec2, addpt2, subpt2, mulpt2, divpt2, lerp2, berp2, dotvec2, vec2len, normvec2, edgeptcmp, ptinpoly, Pt3, Vec3, addpt3, subpt3, mulpt3, divpt3, lerp3, berp3, dotvec3, crossvec3, vec3len, normvec3, identity, addm, subm, mulm, smulm, transposem, detm, tracem, minorm, cofactorm, adjm, invm, xform, identity3, addm3, subm3, mulm3, smulm3, transposem3, detm3, tracem3, minorm3, cofactorm3, adjm3, invm3, xform3, Quat, Quatvec, addq, subq, mulq, smulq, sdivq, dotq, invq, qlen, normq, slerp, qrotate, rframexform, rframexform3, invrframexform, invrframexform3, centroid, barycoords, centroid3, vfmt, Vfmt, GEOMfmtinstall \- computational geometry library
 .SH SYNOPSIS
 .de PB
 .PP
@@ -57,6 +57,7 @@
 .PB
 /* utils */
 double flerp(double a, double b, double t);
+double fberp(double a, double b, double c, Point3 bc);
 double fclamp(double n, double min, double max);
 .PB
 /* Point2 */
@@ -67,6 +68,7 @@
 Point2 mulpt2(Point2 p, double s);
 Point2 divpt2(Point2 p, double s);
 Point2 lerp2(Point2 a, Point2 b, double t);
+Point2 berp2(Point2 a, Point2 b, Point2 c, Point3 bc);
 double dotvec2(Point2 a, Point2 b);
 double vec2len(Point2 v);
 Point2 normvec2(Point2 v);
@@ -81,6 +83,7 @@
 Point3 mulpt3(Point3 p, double s);
 Point3 divpt3(Point3 p, double s);
 Point3 lerp3(Point3 a, Point3 b, double t);
+Point3 berp3(Point3 a, Point3 b, Point3 c, Point3 bc);
 double dotvec3(Point3 a, Point3 b);
 Point3 crossvec3(Point3 a, Point3 b);
 double vec3len(Point3 v);
@@ -177,6 +180,15 @@
 .IR b ,
 and returns the result.
 .TP
+.B fberp(\fIa\fP,\fIb\fP,\fIc\fP,\fIbc\fP)
+Performs a barycentric interpolation of the values in
+.IR a ,
+.I b
+and
+.IR c ,
+based on the weights provided by
+.IR bc .
+.TP
 .B fclamp(\fIn\fP,\fImin\fP,\fImax\fP)
 Constrains
 .I n
@@ -238,6 +250,15 @@
 .IR t ,
 and returns the result.
 .TP
+.B berp2(\fIa\fP,\fIb\fP,\fIc\fP,\fIbc\fP)
+Performs a barycentric interpolation between the 2D points
+.IR a ,
+.I b
+and
+.IR c ,
+using the weights in
+.IR bc .
+.TP
 .B dotvec2(\fIa\fP,\fIb\fP)
 Computes the dot product of vectors
 .I a
@@ -313,6 +334,15 @@
 by a factor of
 .IR t ,
 and returns the result.
+.TP
+.B berp3(\fIa\fP,\fIb\fP,\fIc\fP,\fIbc\fP)
+Performs a barycentric interpolation between the 3D points
+.IR a ,
+.I b
+and
+.IR c ,
+using the weights in
+.IR bc .
 .TP
 .B dotvec3(\fIa\fP,\fIb\fP)
 Computes the dot product of vectors
--- a/sys/src/libgeometry/point.c
+++ b/sys/src/libgeometry/point.c
@@ -51,6 +51,15 @@
 	);
 }
 
+Point2
+berp2(Point2 a, Point2 b, Point2 c, Point3 bc)
+{
+	return addpt2(addpt2(
+		mulpt2(a, bc.x),
+		mulpt2(b, bc.y)),
+		mulpt2(c, bc.z));
+}
+
 double
 dotvec2(Point2 a, Point2 b)
 {
@@ -163,6 +172,15 @@
 		flerp(a.z, b.z, t),
 		flerp(a.w, b.w, t)
 	);
+}
+
+Point3
+berp3(Point3 a, Point3 b, Point3 c, Point3 bc)
+{
+	return addpt3(addpt3(
+		mulpt3(a, bc.x),
+		mulpt3(b, bc.y)),
+		mulpt3(c, bc.z));
 }
 
 double
--- a/sys/src/libgeometry/utils.c
+++ b/sys/src/libgeometry/utils.c
@@ -9,6 +9,12 @@
 }
 
 double
+fberp(double a, double b, double c, Point3 bc)
+{
+	return dotvec3(Vec3(a,b,c), bc);
+}
+
+double
 fclamp(double n, double min, double max)
 {
 	return n < min? min: n > max? max: n;