| 1 |
/* Copyright (c) 2013 Scott Lembcke and Howling Moon Software |
| 2 |
* |
| 3 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
* of this software and associated documentation files (the "Software"), to deal |
| 5 |
* in the Software without restriction, including without limitation the rights |
| 6 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
* copies of the Software, and to permit persons to whom the Software is |
| 8 |
* furnished to do so, subject to the following conditions: |
| 9 |
* |
| 10 |
* The above copyright notice and this permission notice shall be included in |
| 11 |
* all copies or substantial portions of the Software. |
| 12 |
* |
| 13 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
* SOFTWARE. |
| 20 |
*/ |
| 21 |
|
| 22 |
#import <XCTest/XCTest.h> |
| 23 |
#import "ObjectiveChipmunk/ObjectiveChipmunk.h" |
| 24 |
#import "ObjectiveChipmunk/ChipmunkAutogeometry.h" |
| 25 |
|
| 26 |
@interface ConvexTest : XCTestCase {} |
| 27 |
@end |
| 28 |
|
| 29 |
@implementation ConvexTest |
| 30 |
|
| 31 |
//MARK: QuickHull tests |
| 32 |
|
| 33 |
static inline cpFloat frand(void){return (cpFloat)rand()/(cpFloat)RAND_MAX;} |
| 34 |
|
| 35 |
static int |
| 36 |
FirstExpected(int expectedCount, cpVect *expectedVerts, cpVect *verts) |
| 37 |
{ |
| 38 |
for(int i=0; i<expectedCount; i++){ |
| 39 |
if(cpveql(verts[0], expectedVerts[i])){ |
| 40 |
return i; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return -1; |
| 45 |
} |
| 46 |
|
| 47 |
static void |
| 48 |
AssertExpected(id self, cpVect *verts, int expectedCount, cpVect *expectedVerts) |
| 49 |
{ |
| 50 |
int firstExpected = FirstExpected(expectedCount, expectedVerts, verts); |
| 51 |
XCTAssertNotEqual(firstExpected, -1, @""); |
| 52 |
|
| 53 |
// Check the verts |
| 54 |
for(int i=0; i<expectedCount; i++){ |
| 55 |
XCTAssertTrue(cpveql(verts[i], expectedVerts[(firstExpected + i)%expectedCount]), @""); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
static void |
| 60 |
AssertHullsEqual(id self, int resultCount, cpVect *resultVerts) |
| 61 |
{ |
| 62 |
// Check that running the convex hull again returns the same value |
| 63 |
cpVect result2Verts[resultCount]; |
| 64 |
int result2Count = cpConvexHull(resultCount, resultVerts, result2Verts, NULL, 0.0); |
| 65 |
|
| 66 |
XCTAssertEqual(resultCount, result2Count, @""); |
| 67 |
for(int i=0; i<resultCount; i++){ |
| 68 |
XCTAssertTrue(cpveql(resultVerts[i], result2Verts[i]), @""); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
static void |
| 73 |
AssertHull(id self, int count, cpVect *verts, int expectedCount, cpVect *expectedVerts) |
| 74 |
{ |
| 75 |
// Rotate the input array all the way around to check for index issues when selecting maximal points. |
| 76 |
for(int rotate=0; rotate<count; rotate++){ |
| 77 |
cpVect rotated[count]; |
| 78 |
for(int i=0; i<count; i++){ |
| 79 |
rotated[i] = verts[(i + rotate)%count]; |
| 80 |
} |
| 81 |
|
| 82 |
int first = -1; |
| 83 |
cpVect *resultVerts = alloca(count*sizeof(cpVect)); |
| 84 |
int resultCount = cpConvexHull(count, rotated, resultVerts, &first, 0.0); |
| 85 |
|
| 86 |
// Check the count |
| 87 |
XCTAssertEqual(resultCount, expectedCount, @""); |
| 88 |
|
| 89 |
// Check that the windings are positive. |
| 90 |
XCTAssertTrue(cpAreaForPoly(expectedCount, expectedVerts, 0.0f) >= 0.0f, @""); |
| 91 |
XCTAssertTrue(cpAreaForPoly(resultCount, resultVerts, 0.0f) >= 0.0f, @""); |
| 92 |
|
| 93 |
XCTAssertTrue(cpveql(resultVerts[0], rotated[first]), @""); |
| 94 |
|
| 95 |
AssertExpected(self, resultVerts, expectedCount, expectedVerts); |
| 96 |
AssertHullsEqual(self, resultCount, resultVerts); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
static void |
| 101 |
AssertRandomHull(id self, int count) |
| 102 |
{ |
| 103 |
cpVect verts[count]; |
| 104 |
for(int i=0; i<count; i++){ |
| 105 |
verts[i] = cpv(frand(), frand()); |
| 106 |
} |
| 107 |
|
| 108 |
cpVect resultVerts[count]; |
| 109 |
int resultCount = cpConvexHull(count, verts, resultVerts, NULL, 0.0f); |
| 110 |
|
| 111 |
AssertHullsEqual(self, resultCount, resultVerts); |
| 112 |
XCTAssertTrue(cpAreaForPoly(resultCount, resultVerts, 0.0f) > 0.0f, @""); |
| 113 |
} |
| 114 |
|
| 115 |
-(void)testConvexHull |
| 116 |
{ |
| 117 |
{ |
| 118 |
// Trivial cases, reversed windings up to a square. |
| 119 |
cpVect verts[] = {{0,0}, {1,0}}; |
| 120 |
cpVect expected[] = {{0,0}, {1,0}}; |
| 121 |
|
| 122 |
AssertHull(self, 1, verts, 1, expected); |
| 123 |
AssertHull(self, 2, verts, 2, expected); |
| 124 |
} |
| 125 |
|
| 126 |
{ |
| 127 |
cpVect verts[] = {{0,0}, {1,1}, {1,0}}; |
| 128 |
cpVect expected[] = {{0,0}, {1,0}, {1,1}}; |
| 129 |
|
| 130 |
AssertHull(self, 3, verts, 3, expected); |
| 131 |
} |
| 132 |
|
| 133 |
{ |
| 134 |
cpVect verts[] = {{0,0}, {0,1}, {1,1}, {1,0}}; |
| 135 |
cpVect expected[] = {{0,0}, {1,0}, {1,1}, {0,1}}; |
| 136 |
|
| 137 |
AssertHull(self, 4, verts, 4, expected); |
| 138 |
} |
| 139 |
|
| 140 |
{ |
| 141 |
// Degenerate hourglass shaped square. |
| 142 |
cpVect verts[] = {{0,0}, {1,0}, {0,1}, {1,1}}; |
| 143 |
cpVect expected[] = {{0,0}, {1,0}, {1,1}, {0,1}}; |
| 144 |
|
| 145 |
AssertHull(self, 4, verts, 4, expected); |
| 146 |
} |
| 147 |
|
| 148 |
{ |
| 149 |
// A unit square and a bunch of random vertexes inside it. |
| 150 |
cpVect verts[] = { |
| 151 |
{0.9, 0.5}, {0.2, 0.3}, {1.0, 0.0}, {0.8, 0.6}, {0.8, 0.4}, {0.1, 0.4}, |
| 152 |
{0.4, 0.1}, {0.0, 0.0}, {0.1, 0.9}, {0.9, 0.6}, {0.6, 0.2}, {0.7, 0.3}, |
| 153 |
{0.1, 0.3}, {0.1, 0.5}, {0.8, 0.3}, {0.3, 0.8}, {0.0, 1.0}, {0.6, 0.7}, |
| 154 |
{1.0, 1.0}, {0.6, 0.8}, {0.1, 0.7}, {0.7, 0.9}, {0.9, 0.2}, {0.1, 0.1} |
| 155 |
}; |
| 156 |
cpVect expected[] = {{0,0}, {1,0}, {1,1}, {0,1}}; |
| 157 |
|
| 158 |
AssertHull(self, 24, verts, 4, expected); |
| 159 |
} |
| 160 |
|
| 161 |
{ |
| 162 |
// A bunch of colinear points on X |
| 163 |
cpVect verts[] = {{0,0}, {0,1}, {0,2}, {0,3}, {0,4}, {0,5}}; |
| 164 |
cpVect expected[] = {{0,0}, {0,5}}; |
| 165 |
|
| 166 |
AssertHull(self, 6, verts, 2, expected); |
| 167 |
} |
| 168 |
|
| 169 |
{ |
| 170 |
// A bunch of colinear points on Y |
| 171 |
cpVect verts[] = {{0,0}, {1,0}, {2,0}, {3,0}, {4,0}, {5,0}}; |
| 172 |
cpVect expected[] = {{0,0}, {5,0}}; |
| 173 |
|
| 174 |
AssertHull(self, 6, verts, 2, expected); |
| 175 |
} |
| 176 |
|
| 177 |
{ |
| 178 |
// A bunch of colinear points on XY |
| 179 |
cpVect verts[] = {{0,0}, {1,1}, {2,2}, {3,3}, {4,4}, {5,5}}; |
| 180 |
cpVect expected[] = {{0,0}, {5,5}}; |
| 181 |
|
| 182 |
AssertHull(self, 6, verts, 2, expected); |
| 183 |
} |
| 184 |
|
| 185 |
{ |
| 186 |
// A bunch of equivalent points |
| 187 |
cpVect verts[] = {{0,0}, {0,0}, {0,0}, {0,0}, {0,0}}; |
| 188 |
cpVect expected[] = {{0,0}}; |
| 189 |
|
| 190 |
AssertHull(self, 1, verts, 1, expected); |
| 191 |
AssertHull(self, 2, verts, 1, expected); |
| 192 |
AssertHull(self, 3, verts, 1, expected); |
| 193 |
AssertHull(self, 4, verts, 1, expected); |
| 194 |
AssertHull(self, 5, verts, 1, expected); |
| 195 |
} |
| 196 |
|
| 197 |
{ |
| 198 |
// Unit square with reversed winding and colinear face points. |
| 199 |
cpVect verts[] = { |
| 200 |
{1.0f, 0.0f}, {1.0f, 0.5f}, {1.0f, 1.0f}, {0.5f, 1.0f}, |
| 201 |
{0.0f, 1.0f}, {0.0f, 0.5f}, {0.0f, 0.0f}, {0.5f, 0.0f}, |
| 202 |
}; |
| 203 |
cpVect expected[] = {{0,0}, {1,0}, {1,1}, {0,1}}; |
| 204 |
|
| 205 |
AssertHull(self, 8, verts, 4, expected); |
| 206 |
} |
| 207 |
|
| 208 |
{ |
| 209 |
// Unit square with duplicate vertexes. |
| 210 |
cpVect verts1[] = {{0,0}, {0,1}, {1,1}, {1,0}, {0,0}, {0,1}, {1,1}, {1,0}}; |
| 211 |
cpVect verts2[] = {{0,0}, {0,0}, {0,1}, {0,1}, {1,1}, {1,1}, {1,0}, {1,0}}; |
| 212 |
cpVect expected[] = {{0,0}, {1,0}, {1,1}, {0,1}}; |
| 213 |
|
| 214 |
AssertHull(self, 8, verts1, 4, expected); |
| 215 |
AssertHull(self, 8, verts2, 4, expected); |
| 216 |
} |
| 217 |
|
| 218 |
// Check that a convex hull returns itself using (seeded) random hulls. |
| 219 |
srand(5594); |
| 220 |
AssertRandomHull(self, 100); |
| 221 |
AssertRandomHull(self, 1000); |
| 222 |
AssertRandomHull(self, 10000); |
| 223 |
|
| 224 |
// TODO Need tolerance tests. |
| 225 |
} |
| 226 |
|
| 227 |
-(void)testValidate |
| 228 |
{ |
| 229 |
cpVect a = cpv(3.354, 2.546); |
| 230 |
cpVect b = cpv(55.213, 77.1232); |
| 231 |
|
| 232 |
int numVerts = 113; |
| 233 |
// cpVect verts[] = {a, cpvadd(cpvlerp(a, b, 0.5), cpv(0, -1e-7)), b}; |
| 234 |
|
| 235 |
cpVect verts[numVerts]; |
| 236 |
for(int i=0; i<numVerts; i++){ |
| 237 |
verts[i] = cpvadd(cpvlerp(a, b, (cpFloat)i/(cpFloat)(numVerts + 1)), cpv(0, 0)); |
| 238 |
} |
| 239 |
|
| 240 |
CP_CONVEX_HULL(numVerts, verts, hullCount, hullVerts); |
| 241 |
XCTAssertTrue(cpAreaForPoly(hullCount, hullVerts, 0.0f) >= 0.0f, @""); |
| 242 |
} |
| 243 |
|
| 244 |
static cpPolyline * |
| 245 |
MakeLoopedPolyline(id self, int count, cpVect *verts) |
| 246 |
{ |
| 247 |
cpPolylineSet *set = cpPolylineSetNew(); |
| 248 |
|
| 249 |
cpPolylineSetCollectSegment(verts[count - 1], verts[0], set); |
| 250 |
for(int i=1; i<count; i++){ |
| 251 |
cpPolylineSetCollectSegment(verts[i - 1], verts[i], set); |
| 252 |
} |
| 253 |
|
| 254 |
XCTAssert(set->count == 1, @""); |
| 255 |
XCTAssert(cpPolylineIsClosed(set->lines[0]), @""); |
| 256 |
|
| 257 |
cpPolyline *line = set->lines[0]; |
| 258 |
cpPolylineSetFree(set, cpFalse); |
| 259 |
|
| 260 |
return line; |
| 261 |
} |
| 262 |
|
| 263 |
static void |
| 264 |
AssertPolyline(id self, cpPolyline *line, int expectedCount, cpVect *expectedVerts) |
| 265 |
{ |
| 266 |
XCTAssertEqual(line->count, expectedCount + 1, @""); |
| 267 |
XCTAssert(cpPolylineIsClosed(line), @""); |
| 268 |
AssertExpected(self, line->verts, expectedCount, expectedVerts); |
| 269 |
} |
| 270 |
|
| 271 |
-(void)testConvexDecomposition |
| 272 |
{ |
| 273 |
{ |
| 274 |
cpVect verts[] = {{0,0}}; |
| 275 |
|
| 276 |
cpPolyline *line = MakeLoopedPolyline(self, 1, verts); |
| 277 |
cpPolylineSet *set = cpPolylineConvexDecomposition_BETA(line, 0.0); |
| 278 |
|
| 279 |
XCTAssertEqual(set->count, 1, @""); |
| 280 |
AssertPolyline(self, set->lines[0], 1, verts); |
| 281 |
} |
| 282 |
|
| 283 |
{ |
| 284 |
cpVect verts[] = {{0,0}, {1,0}}; |
| 285 |
|
| 286 |
cpPolyline *line = MakeLoopedPolyline(self, 2, verts); |
| 287 |
cpPolylineSet *set = cpPolylineConvexDecomposition_BETA(line, 0.0); |
| 288 |
|
| 289 |
XCTAssertEqual(set->count, 1, @""); |
| 290 |
AssertPolyline(self, set->lines[0], 2, verts); |
| 291 |
} |
| 292 |
|
| 293 |
{ |
| 294 |
cpVect verts[] = {{0,0}, {1,0}, {0,1}}; |
| 295 |
|
| 296 |
cpPolyline *line = MakeLoopedPolyline(self, 3, verts); |
| 297 |
cpPolylineSet *set = cpPolylineConvexDecomposition_BETA(line, 0.0); |
| 298 |
|
| 299 |
XCTAssertEqual(set->count, 1, @""); |
| 300 |
AssertPolyline(self, set->lines[0], 3, verts); |
| 301 |
} |
| 302 |
|
| 303 |
{ |
| 304 |
cpVect verts[] = {{0,0}, {1,0}, {1,1}, {0,1}}; |
| 305 |
|
| 306 |
cpPolyline *line = MakeLoopedPolyline(self, 4, verts); |
| 307 |
cpPolylineSet *set = cpPolylineConvexDecomposition_BETA(line, 0.0); |
| 308 |
|
| 309 |
XCTAssertEqual(set->count, 1, @""); |
| 310 |
AssertPolyline(self, set->lines[0], 4, verts); |
| 311 |
} |
| 312 |
|
| 313 |
{ |
| 314 |
cpVect verts[] = {{0,0}, {1,1}, {2,0}, {1,2}}; |
| 315 |
cpVect expected1[] = {{1,1}, {2,0}, {1,2}}; |
| 316 |
cpVect expected2[] = {{0,0}, {1,1}, {1,2}}; |
| 317 |
|
| 318 |
cpPolyline *line = MakeLoopedPolyline(self, 4, verts); |
| 319 |
cpPolylineSet *set = cpPolylineConvexDecomposition_BETA(line, 0.0); |
| 320 |
|
| 321 |
XCTAssertEqual(set->count, 2, @""); |
| 322 |
AssertPolyline(self, set->lines[0], 3, expected1); |
| 323 |
AssertPolyline(self, set->lines[1], 3, expected2); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
@end |