-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_marcher.c
76 lines (66 loc) · 2.47 KB
/
test_marcher.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "marcher.h"
#include "unittest.h"
/****************************** Weight Functions *****************************/
// Weight function (1)
// The weight between two pixels is the euclidean distance between
// the (R,G,B) values of the 2 pixels. (If we think of them as vectors)
double similarColour(Image *im, int a, int b)
{
Pixel col1 = getPixel(im, a), col2 = getPixel(im, b);
double a1 = (col1.R - col2.R);
double b1 = (col1.G - col2.G);
double c1 = (col1.B - col2.B);
double sumSq = (a1 * a1) + (b1 * b1) + (c1 * c1);
return sqrt(sumSq) + 0.01;
}
// Weight function (2)
// The weight between two pixels is simply how close the 2nd pixel is
// from the colour white (255, 255, 255).
double howWhite(Image *im, int a, int b)
{
// Don't even use the current pixel's colur here... the weight
// functions are arbitrary :)
Pixel col2 = getPixel(im, b);
double a1 = (255 - col2.R);
double b1 = (255 - col2.G);
double c1 = (255 - col2.B);
double sumSq = (a1 * a1) + (b1 * b1) + (c1 * c1);
return sqrt(sumSq / 100.0) + 0.01;
}
/*****************************************************************************/
void run_test(char *filename, WeightFunc wf, double expectedCost)
{
Image *img = readPPMimage(filename);
int *path = calloc(sizeof(int), img->sx * img->sy);
double cost = findPath(img, wf, path);
outputPath(path, img);
if (fabs(cost - expectedCost) >= 10e-4)
TEST_FAIL("Cost (%f) did not match expected answer (%f).\n",
cost, expectedCost);
free(path);
freeImage(img);
}
TEST(water) { run_test("images/water.ppm", similarColour, 1280.81526); }
TEST(spiral) { run_test("images/spiral.ppm", similarColour, 991.255407); }
TEST(maze) { run_test("images/maze.ppm", howWhite, 12.400000); }
TEST(bigmaze) { run_test("images/bigmaze.ppm", howWhite, 8.620000); }
TEST(grad) { run_test("images/grad.ppm", similarColour, 278.751493); }
TEST(all_colour_weight)
{
Image *img = readPPMimage("images/25colours.ppm");
int *path = calloc(sizeof(int), img->sx * img->sy);
double cost = findPath(img, allColourWeight, path);
printf("(Outputting image: inspect this manually) ");
outputPath(path, img);
free(path);
freeImage(img);
}
int main(int argc, char *argv[])
{
unit_main(argc, argv);
printf("\n"
" - You have passed the test cases, your solution is on the right track.\n"
" - It is left up to you to verify that your path itself is correct, and\n"
" - that allColourWeight() works as intended...\n");
return 0;
}