| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| |
| 6 |
|
| #include "hexgrid.h" |
| 7 |
|
| |
| 8 |
|
| MODULE = Math::Geometry::HexGrid PACKAGE = Math::Geometry::HexGrid |
| 9 |
|
| |
| 10 |
|
| PROTOTYPES: DISABLE |
| 11 |
|
| |
| 12 |
|
| void |
| 13 |
|
| oddr_to_cube(int col, int row) |
| 14 |
|
| PPCODE: |
| 15 |
8 |
| HexOddr o = { col, row }; |
| 16 |
8 |
| HexCube c = hex_oddr_to_cube(o); |
| 17 |
8 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
8 |
8 |
|
| 18 |
8 |
| PUSHs(sv_2mortal(newSViv(c.q))); |
| 19 |
8 |
| PUSHs(sv_2mortal(newSViv(c.r))); |
| 20 |
8 |
| PUSHs(sv_2mortal(newSViv(c.s))); |
| 21 |
8 |
| XSRETURN(3); |
| 22 |
|
| |
| 23 |
|
| void |
| 24 |
|
| cube_to_oddr(int q, int r, int s) |
| 25 |
|
| PPCODE: |
| 26 |
8 |
| HexCube c = { q, r, s }; |
| 27 |
8 |
| HexOddr o = hex_cube_to_oddr(c); |
| 28 |
8 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
8 |
8 |
|
| 29 |
8 |
| PUSHs(sv_2mortal(newSViv(o.col))); |
| 30 |
8 |
| PUSHs(sv_2mortal(newSViv(o.row))); |
| 31 |
8 |
| XSRETURN(2); |
| 32 |
|
| |
| 33 |
|
| int |
| 34 |
|
| hex_distance(int col1, int row1, int col2, int row2) |
| 35 |
|
| CODE: |
| 36 |
6 |
| HexOddr a = { col1, row1 }; |
| 37 |
6 |
| HexOddr b = { col2, row2 }; |
| 38 |
6 |
| RETVAL = hex_distance(a, b); |
| 39 |
|
| OUTPUT: |
| 40 |
|
| RETVAL |
| 41 |
|
| |
| 42 |
|
| bool |
| 43 |
|
| hex_is_collinear(int col1, int row1, int col2, int row2) |
| 44 |
|
| CODE: |
| 45 |
2 |
| HexOddr a = { col1, row1 }; |
| 46 |
2 |
| HexOddr b = { col2, row2 }; |
| 47 |
2 |
| RETVAL = hex_is_collinear(a, b); |
| 48 |
|
| OUTPUT: |
| 49 |
|
| RETVAL |
| 50 |
|
| |
| 51 |
|
| void |
| 52 |
|
| hex_neighbors(int col, int row) |
| 53 |
|
| PPCODE: |
| 54 |
3 |
| HexOddr o = { col, row }; |
| 55 |
|
| HexOddr nbs[6]; |
| 56 |
3 |
| hex_neighbors(o, nbs); |
| 57 |
|
| |
| 58 |
3 |
▶ | EXTEND(SP, 6); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 6); |
0 |
3 |
3 |
|
| 59 |
21 |
▶ | for (int i = 0; i < 6; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < 6; i++) { |
18 |
3 |
21 |
|
| 60 |
18 |
| AV* av = newAV(); |
| 61 |
18 |
| av_push(av, newSViv(nbs[i].col)); |
| 62 |
18 |
| av_push(av, newSViv(nbs[i].row)); |
| 63 |
18 |
| PUSHs(sv_2mortal(newRV_noinc((SV*)av))); |
| 64 |
|
| } |
| 65 |
3 |
| XSRETURN(6); |
| 66 |
|
| |
| 67 |
|
| void |
| 68 |
|
| hex_collinear_ray(int col1, int row1, int col2, int row2, int max_length = 100) |
| 69 |
|
| PPCODE: |
| 70 |
3 |
| HexOddr from = { col1, row1 }; |
| 71 |
3 |
| HexOddr to = { col2, row2 }; |
| 72 |
|
| |
| 73 |
3 |
▶ | if (max_length <= 0) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (max_length <= 0) XSRETURN_EMPTY; |
0 |
3 |
3 |
|
| 74 |
|
| |
| 75 |
|
| HexOddr stack_buf[128]; |
| 76 |
3 |
▶ | HexOddr* buf = (max_length <= 128) ? stack_buf : (HexOddr*)malloc(max_length * sizeof(HexOddr)); |
Branch: 1 branch
| Branch | True | False | Total |
| HexOddr* buf = (max_length <= 128) ? stack_buf : (HexOddr*)malloc(max_length * sizeof(HexOddr)); |
3 |
0 |
3 |
|
| 77 |
|
| |
| 78 |
3 |
| int count = hex_collinear_ray(from, to, buf, max_length); |
| 79 |
3 |
▶ | if (count <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count <= 0) { |
1 |
2 |
3 |
|
| 80 |
1 |
▶ | if (buf != stack_buf) free(buf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (buf != stack_buf) free(buf); |
0 |
1 |
1 |
|
| 81 |
1 |
| XSRETURN_EMPTY; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
2 |
▶ | EXTEND(SP, count); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, count); |
2 |
0 |
2 |
| EXTEND(SP, count); |
0 |
2 |
2 |
|
| 85 |
8 |
▶ | for (int i = 0; i < count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < count; i++) { |
6 |
2 |
8 |
|
| 86 |
6 |
| AV* av = newAV(); |
| 87 |
6 |
| av_push(av, newSViv(buf[i].col)); |
| 88 |
6 |
| av_push(av, newSViv(buf[i].row)); |
| 89 |
6 |
| PUSHs(sv_2mortal(newRV_noinc((SV*)av))); |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
2 |
▶ | if (buf != stack_buf) free(buf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (buf != stack_buf) free(buf); |
0 |
2 |
2 |
|
| 93 |
2 |
| XSRETURN(count); |
| 94 |
|
| |
| 95 |
|
| |
| 96 |
|
| MODULE = Math::Geometry::HexGrid PACKAGE = Math::Geometry::HexGrid::Hex |
| 97 |
|
| |
| 98 |
|
| SV* |
| 99 |
|
| new(const char* class, int col, int row) |
| 100 |
|
| CODE: |
| 101 |
11 |
| AV* av = newAV(); |
| 102 |
11 |
| av_push(av, newSViv(col)); |
| 103 |
11 |
| av_push(av, newSViv(row)); |
| 104 |
11 |
| SV* rv = newRV_noinc((SV*)av); |
| 105 |
11 |
| RETVAL = sv_bless(rv, gv_stashpv(class, GV_ADD)); |
| 106 |
|
| OUTPUT: |
| 107 |
|
| RETVAL |
| 108 |
|
| |
| 109 |
|
| int |
| 110 |
|
| col(SV* self) |
| 111 |
|
| CODE: |
| 112 |
4 |
▶ | if (!sv_isobject(self)) croak("col called on non-object"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(self)) croak("col called on non-object"); |
0 |
4 |
4 |
|
| 113 |
4 |
| AV* av = (AV*)SvRV(self); |
| 114 |
4 |
▶ | RETVAL = SvIV(*av_fetch(av, 0, 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = SvIV(*av_fetch(av, 0, 0)); |
0 |
4 |
4 |
|
| 115 |
|
| OUTPUT: |
| 116 |
|
| RETVAL |
| 117 |
|
| |
| 118 |
|
| int |
| 119 |
|
| row(SV* self) |
| 120 |
|
| CODE: |
| 121 |
4 |
▶ | if (!sv_isobject(self)) croak("row called on non-object"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(self)) croak("row called on non-object"); |
0 |
4 |
4 |
|
| 122 |
4 |
| AV* av = (AV*)SvRV(self); |
| 123 |
4 |
▶ | RETVAL = SvIV(*av_fetch(av, 1, 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = SvIV(*av_fetch(av, 1, 0)); |
0 |
4 |
4 |
|
| 124 |
|
| OUTPUT: |
| 125 |
|
| RETVAL |
| 126 |
|
| |
| 127 |
|
| void |
| 128 |
|
| to_cube(SV* self) |
| 129 |
|
| PPCODE: |
| 130 |
0 |
▶ | if (!sv_isobject(self)) croak("to_cube called on non-object"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(self)) croak("to_cube called on non-object"); |
0 |
0 |
0 |
|
| 131 |
0 |
| AV* av = (AV*)SvRV(self); |
| 132 |
0 |
| HexOddr o = { SvIV(*av_fetch(av, 0, 0)), SvIV(*av_fetch(av, 1, 0)) }; |
| 133 |
0 |
| HexCube c = hex_oddr_to_cube(o); |
| 134 |
0 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
0 |
0 |
|
| 135 |
0 |
| PUSHs(sv_2mortal(newSViv(c.q))); |
| 136 |
0 |
| PUSHs(sv_2mortal(newSViv(c.r))); |
| 137 |
0 |
| PUSHs(sv_2mortal(newSViv(c.s))); |
| 138 |
0 |
| XSRETURN(3); |
| 139 |
|
| |
| 140 |
|
| int |
| 141 |
|
| distance_to(SV* self, SV* other) |
| 142 |
|
| CODE: |
| 143 |
1 |
▶ | if (!sv_isobject(self) || !sv_isobject(other)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sv_isobject(self) || !sv_isobject(other)) |
1 |
0 |
1 |
| if (!sv_isobject(self) || !sv_isobject(other)) |
0 |
1 |
1 |
|
| 144 |
0 |
| croak("distance_to called on non-object"); |
| 145 |
1 |
| AV* av_self = (AV*)SvRV(self); |
| 146 |
1 |
| AV* av_other = (AV*)SvRV(other); |
| 147 |
1 |
| HexOddr a = { SvIV(*av_fetch(av_self, 0, 0)), SvIV(*av_fetch(av_self, 1, 0)) }; |
| 148 |
1 |
| HexOddr b = { SvIV(*av_fetch(av_other, 0, 0)), SvIV(*av_fetch(av_other, 1, 0)) }; |
| 149 |
1 |
| RETVAL = hex_distance(a, b); |
| 150 |
|
| OUTPUT: |
| 151 |
|
| RETVAL |
| 152 |
|
| |
| 153 |
|
| bool |
| 154 |
|
| is_collinear_with(SV* self, SV* other) |
| 155 |
|
| CODE: |
| 156 |
1 |
▶ | if (!sv_isobject(self) || !sv_isobject(other)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sv_isobject(self) || !sv_isobject(other)) |
1 |
0 |
1 |
| if (!sv_isobject(self) || !sv_isobject(other)) |
0 |
1 |
1 |
|
| 157 |
0 |
| croak("is_collinear_with called on non-object"); |
| 158 |
1 |
| AV* av_self = (AV*)SvRV(self); |
| 159 |
1 |
| AV* av_other = (AV*)SvRV(other); |
| 160 |
1 |
| HexOddr a = { SvIV(*av_fetch(av_self, 0, 0)), SvIV(*av_fetch(av_self, 1, 0)) }; |
| 161 |
1 |
| HexOddr b = { SvIV(*av_fetch(av_other, 0, 0)), SvIV(*av_fetch(av_other, 1, 0)) }; |
| 162 |
1 |
| RETVAL = hex_is_collinear(a, b); |
| 163 |
|
| OUTPUT: |
| 164 |
|
| RETVAL |
| 165 |
|
| |
| 166 |
|
| bool |
| 167 |
|
| equals(SV* self, SV* other, ...) |
| 168 |
|
| CODE: |
| 169 |
2 |
▶ | if (!sv_isobject(self) || !sv_isobject(other)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sv_isobject(self) || !sv_isobject(other)) { |
2 |
0 |
2 |
| if (!sv_isobject(self) || !sv_isobject(other)) { |
0 |
2 |
2 |
|
| 170 |
0 |
| RETVAL = false; |
| 171 |
|
| } else { |
| 172 |
2 |
| AV* av_self = (AV*)SvRV(self); |
| 173 |
2 |
| AV* av_other = (AV*)SvRV(other); |
| 174 |
2 |
| IV c1 = SvIV(*av_fetch(av_self, 0, 0)); |
| 175 |
2 |
| IV r1 = SvIV(*av_fetch(av_self, 1, 0)); |
| 176 |
2 |
| IV c2 = SvIV(*av_fetch(av_other, 0, 0)); |
| 177 |
2 |
| IV r2 = SvIV(*av_fetch(av_other, 1, 0)); |
| 178 |
2 |
▶ | RETVAL = (c1 == c2 && r1 == r2); |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = (c1 == c2 && r1 == r2); |
1 |
1 |
2 |
| RETVAL = (c1 == c2 && r1 == r2); |
1 |
0 |
1 |
|
| 179 |
|
| } |
| 180 |
|
| OUTPUT: |
| 181 |
|
| RETVAL |
| 182 |
|
| |
| 183 |
|
| const char* |
| 184 |
|
| to_string(SV* self, ...) |
| 185 |
|
| PREINIT: |
| 186 |
|
| static char buf[64]; |
| 187 |
|
| CODE: |
| 188 |
3 |
▶ | if (!sv_isobject(self)) croak("to_string called on non-object"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(self)) croak("to_string called on non-object"); |
0 |
3 |
3 |
|
| 189 |
3 |
| AV* av = (AV*)SvRV(self); |
| 190 |
3 |
| IV c = SvIV(*av_fetch(av, 0, 0)); |
| 191 |
3 |
| IV r = SvIV(*av_fetch(av, 1, 0)); |
| 192 |
3 |
| snprintf(buf, sizeof(buf), "%ld,%ld", (long)c, (long)r); |
| 193 |
3 |
| RETVAL = buf; |
| 194 |
|
| OUTPUT: |
| 195 |
|
| RETVAL |