Skip to content

Commit

Permalink
Math required to implement MeshGroup.
Browse files Browse the repository at this point in the history
Three parts:
- Testing whether a point is in a triangle.
- Efficient (but not guaranteed correct) testing of a point is in which triangle among those in a given mesh, with a bitmask constructed for the mesh.
- Given parent mesh and parent deformation, deform child mesh. Essentially letting one parent triangle drag a child vertex that is contained in it, and this drag can be chacterized by an inverse matrix, which can be reused for efficiency.
Intensive test for bitmask included (with a .png depicting the base test case), testing that bitmask yields correct test results for points with the whole test space transforming.
However, as mentioned, this bitmask method is not guaranteed to be correct, thus it is hard to define a "good enough" test case. It is easy to construct counterexamples.
An experimental attempt on dynamic bitmask step size is commented out cause test suites do not like it. A step size of `1` should be usable in actual rendering anyways.
  • Loading branch information
Richardn2002 committed Sep 30, 2024
1 parent 2c05a25 commit 7fa2acf
Show file tree
Hide file tree
Showing 4 changed files with 393 additions and 1 deletion.
1 change: 1 addition & 0 deletions inox2d/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod deform;
pub mod interp;
pub mod matrix;
pub mod transform;
pub(crate) mod triangle;
Binary file added inox2d/src/math/bit_mask_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 44 additions & 1 deletion inox2d/src/math/deform.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use glam::Vec2;
use glam::{Mat2, Vec2};

/// Different kinds of deform.
// TODO: Meshgroup.
Expand All @@ -22,3 +22,46 @@ pub(crate) fn linear_combine<'deforms>(direct_deforms: impl Iterator<Item = &'de
.for_each(|(sum, addition)| *sum += *addition);
}
}

/// Input: two basis vectors `b0` and `b1`.
///
/// If the returned matrix is to be applied on a Vec2 V and X is obtained,
/// then `X.x * b0 + X.y * b1 = V`.
///
/// Panics if either basis is zero or they are not independent of each other.
pub fn vector_decompose_matrix(b0: Vec2, b1: Vec2) -> Mat2 {
// B X = V where:
// B: [ b0.x b1.x
// b0.y b1.y ]
// X: [ x
// y ]
// V: [ v.x
// v.y ]
// thus X = B^-1 V
let mat = Mat2::from_cols(b0, b1).inverse();
debug_assert_ne!(mat.determinant(), 0.0, "Provided two basis do not span the 2D plane.");
mat
}

/// Provide a parent triangle and its deforms by 3 points,
/// calculate how far should the provided points be moved by the triangle's deform.
///
/// For optimization, the "decompose_matrix" of parent should be provided, see `vector_decompose_matrix()`.
/// It is assumed that `parent[0]` is taken as the origin,
/// `parent[1] - parent[0]` is the first basis vector, and `parent[2] - parent[0]` the second.
#[inline]
pub fn deform_by_parent_triangle<'a>(
decompose_matrix: &'a Mat2,
parent_p0: Vec2,
parent_deforms: &'a [Vec2; 3],
points: impl Iterator<Item = &'a Vec2> + 'a,
) -> impl Iterator<Item = Vec2> + 'a {
let basis_0_deform = parent_deforms[1] - parent_deforms[0];
let basis_1_deform = parent_deforms[2] - parent_deforms[0];

points.map(move |p| {
let decomposed_coeffs = *decompose_matrix * (*p - parent_p0);
// deform by parent[0] + deform by basis change
parent_deforms[0] + decomposed_coeffs.x * basis_0_deform + decomposed_coeffs.y * basis_1_deform
})
}
Loading

0 comments on commit 7fa2acf

Please sign in to comment.