Skip to content

Commit

Permalink
point: fix scale method
Browse files Browse the repository at this point in the history
  • Loading branch information
arrufat committed Sep 26, 2024
1 parent a54ec94 commit 66cf41a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
12 changes: 7 additions & 5 deletions examples/src/face_alignment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ pub fn extractAlignedFace(
.{ .x = 0.3340850613712, .y = 0.2290642403242 },
.{ .x = 0.4901123135679, .y = 0.6277975316475 },
};
const scale_cols: f32 = @floatFromInt(image.cols - 1);
const scale_rows: f32 = @floatFromInt(image.rows - 1);

// These are the detected points from MediaPipe.
const to_points: [5]Point2d = .{
landmarks[alignment[0]].scale(image.cols, image.rows),
landmarks[alignment[1]].scale(image.cols, image.rows),
landmarks[alignment[2]].scale(image.cols, image.rows),
landmarks[alignment[3]].scale(image.cols, image.rows),
landmarks[alignment[4]].scale(image.cols, image.rows),
landmarks[alignment[0]].scale(scale_cols, scale_rows),
landmarks[alignment[1]].scale(scale_cols, scale_rows),
landmarks[alignment[2]].scale(scale_cols, scale_rows),
landmarks[alignment[3]].scale(scale_cols, scale_rows),
landmarks[alignment[4]].scale(scale_cols, scale_rows),
};
assert(from_points.len == to_points.len);
assert(out.cols == out.rows);
Expand Down
26 changes: 6 additions & 20 deletions src/point.zig
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
const std = @import("std");
const assert = std.debug.assert;

/// Denormalize the coordinate to be in the range [0, dim-1].
fn scaleDimension(val: f32, dim: usize) f32 {
return @max(0, @min(@as(f32, @floatFromInt(dim)) - 1, val * @as(f32, @floatFromInt(dim)) - 1));
}

/// A simple 2D point with floating point coordinates.
pub fn Point2d(comptime T: type) type {
assert(@typeInfo(T) == .float);
return packed struct {
x: T,
y: T,

/// Assumes the point's coordinates are normalized between 0 and 1. Then it denormalizes
/// them by rescaling with the appropriate dims.
pub fn scale(self: Point2d(T), x_dim: usize, y_dim: usize) Point2d(T) {
return .{
.x = scaleDimension(self.x, x_dim),
.y = scaleDimension(self.y, y_dim),
};
/// Scales self x and y coordinates with x_scale and y_scale, respectively.
pub fn scale(self: Point2d(T), x_scale: T, y_scale: T) Point2d(T) {
return .{ .x = self.x * x_scale, .y = self.y * y_scale };
}

/// Adds self and other.
Expand Down Expand Up @@ -81,14 +72,9 @@ pub fn Point3d(comptime T: type) type {
y: T,
z: T,

/// Assumes the point's coordinates are normalized between 0 and 1. Then it denormalizes
/// them by rescaling with the appropriate dims.
pub fn scale(self: Point3d(T), x_dim: usize, y_dim: usize, z_dim: usize) Point3d(T) {
return .{
.x = scaleDimension(self.x, x_dim),
.y = scaleDimension(self.y, y_dim),
.z = scaleDimension(self.z, z_dim),
};
/// Scales self x, y and z coordinates with x_scale, y_scale and z_scale, respectively.
pub fn scale(self: Point2d(T), x_scale: T, y_scale: T, z_scale: T) Point2d(T) {
return .{ .x = self.x * x_scale, .y = self.y * y_scale, .z = self.z * z_scale };
}

/// Adds self and other.
Expand Down

0 comments on commit 66cf41a

Please sign in to comment.