summaryrefslogtreecommitdiff
path: root/src/common/geom.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/geom.c')
-rw-r--r--src/common/geom.c27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/common/geom.c b/src/common/geom.c
index 219270f..eb35727 100644
--- a/src/common/geom.c
+++ b/src/common/geom.c
@@ -1,6 +1,12 @@
1#include "common/geom.h" 1#include "common/geom.h"
2#include <math.h>
3#include <common/geom.h>
2#include "morpher/matrix.h" 4#include "morpher/matrix.h"
3 5
6static inline IntVector int_round(RealVector x) {
7 return (IntVector) round(x);
8}
9
4CartesianMapping m(int x, int y) { 10CartesianMapping m(int x, int y) {
5 return (CartesianMapping) {{x, y}, 11 return (CartesianMapping) {{x, y},
6 {x, y}}; 12 {x, y}};
@@ -10,6 +16,10 @@ CartesianVector v(int x, int y) {
10 return (CartesianVector) {x, y}; 16 return (CartesianVector) {x, y};
11} 17}
12 18
19BarycentricVector b(double a, double b) {
20 return (BarycentricVector) {a, b};
21}
22
13bool mappings_equals(CartesianMapping m1, CartesianMapping m2) { 23bool mappings_equals(CartesianMapping m1, CartesianMapping m2) {
14 return vector_equals(m1.origin, m2.origin) && vector_equals(m1.target, m2.target); 24 return vector_equals(m1.origin, m2.origin) && vector_equals(m1.target, m2.target);
15} 25}
@@ -18,7 +28,22 @@ bool vector_equals(CartesianVector v1, CartesianVector v2) {
18 return v1.x == v2.x && v1.y == v2.y; 28 return v1.x == v2.x && v1.y == v2.y;
19} 29}
20 30
21IntVector triangle_area(CartesianVector v1, CartesianVector v2, CartesianVector v3) { 31bool barycentric_vector_equals(BarycentricVector b1, BarycentricVector b2) {
32 return b1.a == b2.a && b1.b == b2.b;
33}
34
35IntVector square_area(CartesianVector v1, CartesianVector v2, CartesianVector v3) {
22 return matrix_int_det2(v1.x - v3.x, v2.x - v3.x, 36 return matrix_int_det2(v1.x - v3.x, v2.x - v3.x,
23 v1.y - v3.y, v2.y - v3.y); 37 v1.y - v3.y, v2.y - v3.y);
24} 38}
39
40BarycentricVector cartesian_to_barycentric(Triangle t, CartesianVector p) {
41 RealVector total_area = square_area(t.v[0], t.v[1], t.v[2]);
42 return (BarycentricVector) {square_area(t.v[1], t.v[2], p) / total_area,
43 square_area(t.v[2], t.v[0], p) / total_area};
44}
45
46CartesianVector barycentric_to_cartesian(Triangle t, BarycentricVector p) {
47 return (CartesianVector) {int_round(p.a * (t.v[0].x - t.v[2].x) + p.b * (t.v[1].x - t.v[2].x) + t.v[2].x),
48 int_round(p.a * (t.v[0].y - t.v[2].y) + p.b * (t.v[1].y - t.v[2].y) + t.v[2].y)};
49}