summaryrefslogtreecommitdiff
path: root/tikzit/src/common/util.m
blob: aa21a67add93bfd97bf72dc796fc208b6b1fb660 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//
//  util.m
//  TikZiT
//  
//  Copyright 2010 Aleks Kissinger. All rights reserved.
//  
//  
//  This file is part of TikZiT.
//  
//  TikZiT is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  TikZiT is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  

#import "util.h"
#import "math.h"

static BOOL fuzzyCompare(float f1, float f2) {
	return (ABS(f1 - f2) <= 0.00001f * MIN(ABS(f1), ABS(f2)));
}

NSRect NSRectWithPoint(NSRect rect, NSPoint p) {
	CGFloat minX = NSMinX(rect);
	CGFloat maxX = NSMaxX(rect);
	CGFloat minY = NSMinY(rect);
	CGFloat maxY = NSMaxY(rect);
	if (p.x < minX) {
		minX = p.x;
	} else if (p.x > maxX) {
		maxX = p.x;
	}
	if (p.y < minY) {
		minY = p.y;
	} else if (p.y > maxY) {
		maxY = p.y;
	}
	return NSMakeRect(minX, minY, maxX - minX, maxY - minY);
}

NSRect NSRectAroundPointsWithPadding(NSPoint p1, NSPoint p2, float padding) {
	return NSMakeRect(MIN(p1.x,p2.x)-padding,
					  MIN(p1.y,p2.y)-padding,
					  ABS(p2.x-p1.x)+(2.0f*padding),
					  ABS(p2.y-p1.y)+(2.0f*padding));
}

NSRect NSRectAroundPoints(NSPoint p1, NSPoint p2) {
	return NSRectAroundPointsWithPadding(p1, p2, 0.0f);
}

NSRect NSRectAround4PointsWithPadding(NSPoint p1, NSPoint p2, NSPoint p3, NSPoint p4, float padding) {
	float leftMost = MIN(p1.x, p2.x);
	leftMost = MIN(leftMost, p3.x);
	leftMost = MIN(leftMost, p4.x);
	float rightMost = MAX(p1.x, p2.x);
	rightMost = MAX(rightMost, p3.x);
	rightMost = MAX(rightMost, p4.x);
	float topMost = MIN(p1.y, p2.y);
	topMost = MIN(topMost, p3.y);
	topMost = MIN(topMost, p4.y);
	float bottomMost = MAX(p1.y, p2.y);
	bottomMost = MAX(bottomMost, p3.y);
	bottomMost = MAX(bottomMost, p4.y);
	return NSMakeRect(leftMost-padding,
					  topMost-padding,
					  (rightMost - leftMost)+(2.0f*padding),
					  (bottomMost - topMost)+(2.0f*padding));
}

NSRect NSRectAround4Points(NSPoint p1, NSPoint p2, NSPoint p3, NSPoint p4) {
	return NSRectAround4PointsWithPadding(p1, p2, p3, p4, 0.0f);
}

float NSDistanceBetweenPoints(NSPoint p1, NSPoint p2) {
	float dx = p2.x - p1.x;
	float dy = p2.y - p1.y;
	return sqrt(dx * dx + dy * dy);
}

float good_atan(float dx, float dy) {
	if (dx > 0) {
		return atan(dy/dx);
	} else if (dx < 0) {
		return M_PI + atan(dy/dx);
	} else {
		if (dy > 0) return 0.5 * M_PI;
		else if (dy < 0) return 1.5 * M_PI;
		else return 0;
	}
}

// interpolate on a cubic bezier curve
float bezierInterpolate(float dist, float c0, float c1, float c2, float c3) {
	float distp = 1 - dist;
	return	(distp*distp*distp) * c0 +
			3 * (distp*distp) * dist * c1 +
			3 * (dist*dist) * distp * c2 +
			(dist*dist*dist) * c3;
}

NSPoint bezierInterpolateFull (float dist, NSPoint c0, NSPoint c1, NSPoint c2, NSPoint c3) {
	return NSMakePoint (bezierInterpolate (dist, c0.x, c1.x, c2.x, c3.x),
	                    bezierInterpolate (dist, c0.y, c1.y, c2.y, c3.y));
}

static void lineCoeffsFromPoints(NSPoint p1, NSPoint p2, float *A, float *B, float *C) {
	*A = p2.y - p1.y;
	*B = p1.x - p2.x;
	*C = (*A) * p1.x + (*B) * p1.y;
}

static void lineCoeffsFromPointAndAngle(NSPoint p, float angle, float *A, float *B, float *C) {
	*A = sin (angle);
	*B = -cos (angle);
	*C = (*A) * p.x + (*B) * p.y;
}

static BOOL lineSegmentContainsPoint(NSPoint l1, NSPoint l2, float x, float y) {
	float minX = MIN(l1.x, l2.x);
	float maxX = MAX(l1.x, l2.x);
	float minY = MIN(l1.y, l2.y);
	float maxY = MAX(l1.y, l2.y);
	return (x >= minX || fuzzyCompare (x, minX)) &&
	       (x <= maxX || fuzzyCompare (x, maxX)) &&
		   (y >= minY || fuzzyCompare (y, minY)) &&
		   (y <= maxY || fuzzyCompare (y, maxY));
}

BOOL lineSegmentsIntersect(NSPoint l1start, NSPoint l1end, NSPoint l2start, NSPoint l2end, NSPoint *result) {
	// Ax + By = C
	float A1, B1, C1;
	lineCoeffsFromPoints(l1start, l1end, &A1, &B1, &C1);
	float A2, B2, C2;
	lineCoeffsFromPoints(l2start, l2end, &A2, &B2, &C2);

	float det = A1*B2 - A2*B1;
	if (det == 0.0f) {
		// parallel
		return NO;
	} else {
		float x = (B2*C1 - B1*C2)/det;
		float y = (A1*C2 - A2*C1)/det;

		if (lineSegmentContainsPoint(l1start, l1end, x, y) &&
				lineSegmentContainsPoint(l2start, l2end, x, y)) {
			if (result) {
				(*result).x = x;
				(*result).y = y;
			}
			return YES;
		}
	}
	return NO;
}

BOOL lineSegmentIntersectsBezier (NSPoint lstart, NSPoint lend, NSPoint c0, NSPoint c1, NSPoint c2, NSPoint c3, NSPoint *result) {
	NSRect curveBounds = NSRectAround4Points(c0, c1, c2, c3);
	if (!lineSegmentIntersectsRect(lstart, lend, curveBounds))
		return NO;

	const int divisions = 20;
	const float chunkSize = 1.0f/(float)divisions;
	float chunkStart = 0.0f;
	BOOL found = NO;

	for (int i = 0; i < divisions; ++i) {
		float chunkEnd = chunkStart + chunkSize;

		NSPoint p1 = bezierInterpolateFull (chunkStart, c0, c1, c2, c3);
		NSPoint p2 = bezierInterpolateFull (chunkEnd, c0, c1, c2, c3);

		NSPoint p;
		if (lineSegmentsIntersect (lstart, lend, p1, p2, &p)) {
			lstart = p;
			found = YES;
		}

		chunkStart = chunkEnd;
	}
	if (found && result) {
		*result = lstart;
	}
	return found;
}

BOOL lineSegmentIntersectsRect(NSPoint lineStart, NSPoint lineEnd, NSRect rect) {
	const float rectMaxX = NSMaxX(rect);
	const float rectMinX = NSMinX(rect);
	const float rectMaxY = NSMaxY(rect);
	const float rectMinY = NSMinY(rect);

	// check if the segment is entirely to one side of the rect
	if (lineStart.x > rectMaxX && lineEnd.x > rectMaxX) {
		return NO;
	}
	if (lineStart.x < rectMinX && lineEnd.x < rectMinX) {
		return NO;
	}
	if (lineStart.y > rectMaxY && lineEnd.y > rectMaxY) {
		return NO;
	}
	if (lineStart.y < rectMinY && lineEnd.y < rectMinY) {
		return NO;
	}

	// Now check whether the (infinite) line intersects the rect
	// (if it does, so does the segment, due to above checks)

	// Ax + By = C
	float A, B, C;
	lineCoeffsFromPoints(lineStart, lineEnd, &A, &B, &C);

	const float tlVal = A * rectMinX + B * rectMaxY - C;
	const float trVal = A * rectMaxX + B * rectMaxY - C;
	const float blVal = A * rectMinX + B * rectMinY - C;
	const float brVal = A * rectMaxX + B * rectMinY - C;

	if (tlVal < 0 && trVal < 0 && blVal < 0 && brVal < 0) {
		// rect below line
		return NO;
	}
	if (tlVal > 0 && trVal > 0 && blVal > 0 && brVal > 0) {
		// rect above line
		return NO;
	}

	return YES;
}

NSPoint findExitPointOfRay (NSPoint p, float angle_rads, NSRect rect) {
	const float rectMinX = NSMinX (rect);
	const float rectMaxX = NSMaxX (rect);
	const float rectMinY = NSMinY (rect);
	const float rectMaxY = NSMaxY (rect);

	const float angle = normaliseAngleRad (angle_rads);

	// special case the edges
	if (p.y == rectMaxY && angle > 0 && angle < M_PI) {
		// along the top of the box
		return p;
	}
	if (p.y == rectMinY && angle < 0 && angle > -M_PI) {
		// along the bottom of the box
		return p;
	}
	if (p.x == rectMaxX && angle > -M_PI/2.0f && angle < M_PI/2.0f) {
		// along the right of the box
		return p;
	}
	if (p.x == rectMinX && (angle > M_PI/2.0f || angle < -M_PI/2.0f)) {
		// along the left of the box
		return p;
	}

	float A1, B1, C1;
	lineCoeffsFromPointAndAngle(p, angle, &A1, &B1, &C1);
	//NSLog(@"Ray is %fx + %fy = %f", A1, B1, C1);

	const float tlAngle = normaliseAngleRad (good_atan (rectMinX - p.x, rectMaxY - p.y));
	const float trAngle = normaliseAngleRad (good_atan (rectMaxX - p.x, rectMaxY - p.y));
	if (angle <= tlAngle && angle >= trAngle) {
		// exit top
		float A2, B2, C2;
		lineCoeffsFromPoints(NSMakePoint (rectMinX, rectMaxY),
		                     NSMakePoint (rectMaxX, rectMaxY),
							 &A2, &B2, &C2);
		float det = A1*B2 - A2*B1;
		NSCAssert(det != 0.0f, @"Parallel lines?");
		NSPoint intersect = NSMakePoint ((B2*C1 - B1*C2)/det,
		                                 (A1*C2 - A2*C1)/det);
		return intersect;
	}

	const float brAngle = normaliseAngleRad (good_atan (rectMaxX - p.x, rectMinY - p.y));
	if (angle <= trAngle && angle >= brAngle) {
		// exit right
		float A2, B2, C2;
		lineCoeffsFromPoints(NSMakePoint (rectMaxX, rectMaxY),
		                     NSMakePoint (rectMaxX, rectMinY),
							 &A2, &B2, &C2);
		//NSLog(@"Edge is %fx + %fy = %f", A2, B2, C2);
		float det = A1*B2 - A2*B1;
		NSCAssert(det != 0.0f, @"Parallel lines?");
		NSPoint intersect = NSMakePoint ((B2*C1 - B1*C2)/det,
		                                 (A1*C2 - A2*C1)/det);
		return intersect;
	}

	const float blAngle = normaliseAngleRad (good_atan (rectMinX - p.x, rectMinY - p.y));
	if (angle <= brAngle && angle >= blAngle) {
		// exit bottom
		float A2, B2, C2;
		lineCoeffsFromPoints(NSMakePoint (rectMaxX, rectMinY),
		                     NSMakePoint (rectMinX, rectMinY),
							 &A2, &B2, &C2);
		float det = A1*B2 - A2*B1;
		NSCAssert(det != 0.0f, @"Parallel lines?");
		NSPoint intersect = NSMakePoint ((B2*C1 - B1*C2)/det,
		                                 (A1*C2 - A2*C1)/det);
		return intersect;
	} else {
		// exit left
		float A2, B2, C2;
		lineCoeffsFromPoints(NSMakePoint (rectMinX, rectMaxY),
		                     NSMakePoint (rectMinX, rectMinY),
							 &A2, &B2, &C2);
		float det = A1*B2 - A2*B1;
		NSCAssert(det != 0.0f, @"Parallel lines?");
		NSPoint intersect = NSMakePoint ((B2*C1 - B1*C2)/det,
		                                 (A1*C2 - A2*C1)/det);
		return intersect;
	}
}

float roundToNearest(float stepSize, float val) {
	if (stepSize==0.0f) return val;
	else return round(val/stepSize)*stepSize;
}

float radiansToDegrees (float radians) {
    return (radians * 180.0f) / M_PI;
}

float degreesToRadians(float degrees) {
    return (degrees * M_PI) / 180.0f;
}

int normaliseAngleDeg (int degrees) {
	while (degrees > 180) {
		degrees -= 360;
	}
	while (degrees <= -180) {
		degrees += 360;
	}
	return degrees;
}

float normaliseAngleRad (float rads) {
	while (rads > M_PI) {
		rads -= 2 * M_PI;
	}
	while (rads <= -M_PI) {
		rads += 2 * M_PI;
	}
	return rads;
}

static char ahex[] =
{'a','b','c','d','e','f','g','h','i','j',
 'A','B','C','D','E','F'};

NSString *alphaHex(unsigned short sh) {
	if (sh > 255) return @"!!";
	return [NSString stringWithFormat:@"%c%c", ahex[sh/16], ahex[sh%16]];
}

const char *find_start_of_nth_line (const char * string, int line) {
	int l = 0;
	const char *lineStart = string;
	while (*lineStart && l < line) {
		while (*lineStart && *lineStart != '\n') {
			++lineStart;
		}
		if (*lineStart) {
			++l;
			++lineStart;
		}
	}
	return lineStart;
}


// vi:ft=objc:noet:ts=4:sts=4:sw=4