summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpacien2017-12-28 01:19:45 +0100
committerpacien2017-12-28 01:19:45 +0100
commitc29e4ecb7de4cb10f48b2526bc1abae847c718e2 (patch)
tree03b439e2ed82ddab30823a8782cfdb8ebce1a349 /src
parentf5ff85f3c7e7d6bf11a423c497d2b3ce76cfafd8 (diff)
downloadmorpher-c29e4ecb7de4cb10f48b2526bc1abae847c718e2.tar.gz
Add new geometry common types and functions
Signed-off-by: pacien <pacien.trangirard@pacien.net>
Diffstat (limited to 'src')
-rw-r--r--src/common/geom.c27
-rw-r--r--src/morpher/trianglemap.c2
2 files changed, 27 insertions, 2 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}
diff --git a/src/morpher/trianglemap.c b/src/morpher/trianglemap.c
index e2f3eb9..ad526bc 100644
--- a/src/morpher/trianglemap.c
+++ b/src/morpher/trianglemap.c
@@ -30,7 +30,7 @@ TriangleMap *trianglemap_to(TriangleMap *t, CartesianVector v) {
30 int edge; 30 int edge;
31 31
32 for (edge = 0; edge < 3; ++edge) 32 for (edge = 0; edge < 3; ++edge)
33 if (triangle_area(t->vertices[edge].origin, t->vertices[(edge + 1) % 3].origin, v) > 0) 33 if (square_area(t->vertices[edge].origin, t->vertices[(edge + 1) % 3].origin, v) > 0)
34 return t->neighbors[edge]; 34 return t->neighbors[edge];
35 35
36 return t; 36 return t;