Skip to content

Commit 0be2c0c

Browse files
MeshDeformer
1 parent c108ee3 commit 0be2c0c

File tree

11 files changed

+612
-257
lines changed

11 files changed

+612
-257
lines changed

source/inochi2d/core/math/deform.d

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ public:
4545
*/
4646
void deform(vec2[] deformed, bool absolute = false);
4747

48+
/**
49+
Deforms a single vertex in the IDeformable
50+
51+
Params:
52+
offset = The offset into the point list to deform.
53+
deform = The deformation delta.
54+
absolute = Whether the deformation is absolute,
55+
replacing the original deformation.
56+
*/
57+
void deform(size_t offset, vec2 deform, bool absolute = false);
58+
4859
/**
4960
Resets the deformation for the IDeformable.
5061
*/

source/inochi2d/core/math/package.d

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import std.json;
2424
import numem;
2525

2626
public import inochi2d.core.math.deform;
27+
public import inochi2d.core.math.trig;
2728

2829
/**
2930
A camera
@@ -222,26 +223,6 @@ private:
222223
mat4 trs = mat4.identity;
223224
}
224225

225-
226-
bool isPointInTriangle(vec2 pt, vec2[3] triangle) {
227-
float sign (ref vec2 p1, ref vec2 p2, ref vec2 p3) {
228-
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
229-
}
230-
vec2 p1 = triangle[0];
231-
vec2 p2 = triangle[1];
232-
vec2 p3 = triangle[2];
233-
234-
auto d1 = sign(pt, p1, p2);
235-
auto d2 = sign(pt, p2, p3);
236-
auto d3 = sign(pt, p3, p1);
237-
238-
auto hasNeg = (d1 < 0) || (d2 < 0) || (d3 < 0);
239-
auto hasPos = (d1 > 0) || (d2 > 0) || (d3 > 0);
240-
241-
return !(hasNeg && hasPos);
242-
}
243-
244-
245226
int[] findSurroundingTriangle(vec2 pt, ref MeshData bindingMesh) {
246227
bool isPointInTriangle(vec2 pt, int[] triangle) {
247228
float sign (ref vec2 p1, ref vec2 p2, ref vec2 p3) {

source/inochi2d/core/math/trig.d

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
Inochi2D Triangles and trigonometry
3+
4+
Copyright © 2020-2025, Inochi2D Project
5+
Distributed under the 2-Clause BSD License, see LICENSE file.
6+
7+
Authors: Luna Nielsen
8+
*/
9+
module inochi2d.core.math.trig;
10+
import inmath.linalg;
11+
12+
/**
13+
A 2D triangle
14+
*/
15+
struct Triangle {
16+
@nogc:
17+
vec2 p1;
18+
vec2 p2;
19+
vec2 p3;
20+
21+
/**
22+
Gets the barycentric coordinates of the given point.
23+
24+
Params:
25+
pt = The point to check.
26+
27+
Returns:
28+
The barycentric coordinates in relation to each
29+
vertex of the triangle.
30+
*/
31+
pragma(inline, true)
32+
vec3 barycentric(vec2 pt) nothrow pure {
33+
vec2 v1 = p2 - p1;
34+
vec2 v2 = p3 - p1;
35+
vec2 v3 = pt - p1;
36+
float den = v1.x * v2.y - v2.x * v1.y;
37+
float v = (v3.x * v2.y - v2.x * v3.y) / den;
38+
float w = (v1.x * v3.y - v3.x * v1.y) / den;
39+
return vec3(
40+
1.0 - v - w,
41+
v,
42+
w,
43+
);
44+
}
45+
46+
/**
47+
Whether the triangle contains the given point.
48+
49+
Params:
50+
pt = The point to check.
51+
52+
Returns:
53+
$(D true) if the given point lies within this
54+
triangle, $(D false) otherwise.
55+
*/
56+
pragma(inline, true)
57+
bool contains(vec2 pt) nothrow pure {
58+
float d1 = sign(pt, p1, p2);
59+
float d2 = sign(pt, p2, p3);
60+
float d3 = sign(pt, p3, p1);
61+
return !(
62+
((d1 < 0) || (d2 < 0) || (d3 < 0)) &&
63+
((d1 > 0) || (d2 > 0) || (d3 > 0))
64+
);
65+
}
66+
}
67+
68+
/**
69+
Gets the sign between 3 points.
70+
71+
Params:
72+
p1 = The first point
73+
p2 = The second point
74+
p3 = The third point.
75+
76+
Returns:
77+
A float determining the sign between p1, p2 and p3.
78+
*/
79+
pragma(inline, true)
80+
float sign (ref vec2 p1, ref vec2 p2, ref vec2 p3) @nogc nothrow pure {
81+
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
82+
}

0 commit comments

Comments
 (0)