summaryrefslogtreecommitdiff
path: root/liblogdiag/ld-types.c
blob: fb2a5ad49822dee6199d20959a413bca0e1100b2 (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
/*
 * ld-types.c
 *
 * This file is a part of logdiag.
 * Copyright 2010, 2011 Přemysl Eric Janouch
 *
 * See the file LICENSE for licensing information.
 *
 */

#include <math.h>
#include <string.h>

#include "liblogdiag.h"
#include "config.h"


/**
 * SECTION:ld-types
 * @short_description: Simple data types
 *
 * #LdPoint defines coordinates of a point.
 *
 * #LdPointArray defines an array of points.
 *
 * #LdRectangle defines the position and size of a rectangle.
 */

#define DEFINE_BOXED_TYPE(TypeName, type_name) \
GType \
type_name ## _get_type (void) \
{ \
	static GType our_type = 0; \
	if (our_type == 0) \
		our_type = g_boxed_type_register_static \
			(g_intern_static_string (#TypeName), \
			(GBoxedCopyFunc) type_name ## _copy, \
			(GBoxedFreeFunc) type_name ## _free); \
	return our_type; \
}

DEFINE_BOXED_TYPE (LdPoint, ld_point)
DEFINE_BOXED_TYPE (LdPointArray, ld_point_array)
DEFINE_BOXED_TYPE (LdRectangle, ld_rectangle)

#define DEFINE_BOXED_TRIVIAL_COPY(TypeName, type_name) \
TypeName * \
type_name ## _copy (const TypeName *self) \
{ \
	TypeName *new_copy; \
	g_return_val_if_fail (self != NULL, NULL); \
	new_copy = g_slice_new (TypeName); \
	*new_copy = *self; \
	return new_copy; \
}

#define DEFINE_BOXED_TRIVIAL_FREE(TypeName, type_name) \
void \
type_name ## _free (TypeName *self) \
{ \
	g_return_if_fail (self != NULL); \
	g_slice_free (TypeName, self); \
}

/**
 * ld_point_copy:
 * @self: an #LdPoint structure.
 *
 * Makes a copy of the structure.
 * The result must be freed by ld_point_free().
 *
 * Return value: a copy of @self.
 */
DEFINE_BOXED_TRIVIAL_COPY (LdPoint, ld_point)

/**
 * ld_point_free:
 * @self: an #LdPoint structure.
 *
 * Frees the structure created with ld_point_copy().
 */
DEFINE_BOXED_TRIVIAL_FREE (LdPoint, ld_point)

/**
 * ld_point_distance:
 * @self: an #LdPoint structure.
 * @x: the X coordinate of the second point.
 * @y: the Y coordinate of the second point.
 *
 * Compute the distance between two points.
 */
gdouble
ld_point_distance (const LdPoint *self, gdouble x, gdouble y)
{
	gdouble dx, dy;

	g_return_val_if_fail (self != NULL, -1);

	dx = self->x - x;
	dy = self->y - y;
	return sqrt (dx * dx + dy * dy);
}

/**
 * ld_point_array_new:
 *
 * Create a new #LdPointArray.
 *
 * Return value: (transfer full): an #LdPointArray structure.
 */
LdPointArray *
ld_point_array_new (void)
{
	return ld_point_array_sized_new (0);
}

/**
 * ld_point_array_sized_new:
 * @preallocated: the number of points preallocated.
 *
 * Create a new #LdPointArray and preallocate storage for @preallocated items.
 *
 * Return value: (transfer full): an #LdPointArray structure.
 */
LdPointArray *
ld_point_array_sized_new (guint preallocated)
{
	LdPointArray *new_array;

	new_array = g_slice_new (LdPointArray);
	new_array->length = 0;
	new_array->size = preallocated;
	new_array->points = g_malloc0 (preallocated * sizeof (LdPoint));
	return new_array;
}

/**
 * ld_point_array_copy:
 * @self: an #LdPointArray structure.
 *
 * Makes a copy of the structure.
 * The result must be freed by ld_point_array_free().
 *
 * Return value: (transfer full): a copy of @self.
 */
LdPointArray *
ld_point_array_copy (const LdPointArray *self)
{
	LdPointArray *new_array;

	g_return_val_if_fail (self != NULL, NULL);

	new_array = g_slice_new (LdPointArray);
	new_array->length = self->length;
	new_array->size = self->size;
	new_array->points = g_memdup (self->points, self->size * sizeof (LdPoint));
	return new_array;
}

/**
 * ld_point_array_free:
 * @self: an #LdPointArray structure.
 *
 * Frees the structure created with ld_point_array_copy().
 */
void
ld_point_array_free (LdPointArray *self)
{
	g_return_if_fail (self != NULL);

	g_free (self->points);
	g_slice_free (LdPointArray, self);
}

/**
 * ld_point_array_insert:
 * @self: an #LdPointArray structure.
 * @points: an array of points to be inserted.
 * @pos: the position at which the points should be inserted. This number
 *       must not be bigger than the number of points already present
 *       in the array. Negative values append to the array.
 * @length: count of points in @points.
 *
 * Insert points into the array.
 */
void
ld_point_array_insert (LdPointArray *self, LdPoint *points,
	gint pos, guint length)
{
	guint new_size;

	g_return_if_fail (self != NULL);
	g_return_if_fail (points != NULL);
	g_return_if_fail (pos <= (signed) self->length);

	new_size = self->size ? self->size : 1;
	while (self->length + length > new_size)
		new_size <<= 1;
	if (new_size != self->size)
		ld_point_array_set_size (self, new_size);

	if (pos < 0)
		pos = self->length;

	g_memmove (self->points + pos + length, self->points + pos,
		(self->length - pos) * sizeof (LdPoint));
	memcpy (self->points + pos, points, length * sizeof (LdPoint));
	self->length += length;
}

/**
 * ld_point_array_remove:
 * @self: an #LdPointArray structure.
 * @pos: the position at which the points should be removed.
 *       Negative values are relative to the end of the array.
 * @length: count of points to remove.
 *
 * Remove points from the array. The array may be resized as a result.
 */
void
ld_point_array_remove (LdPointArray *self, gint pos, guint length)
{
	guint new_size;

	g_return_if_fail (self != NULL);

	if (pos < 0)
	{
		pos += self->length;
		if (pos < 0)
		{
			length += pos;
			pos = 0;
		}
	}
	if ((unsigned) pos >= self->length)
		return;
	if (pos + length > self->length)
		length = self->length - pos;

	g_memmove (self->points + pos, self->points + pos + length,
		(self->length - pos) * sizeof (LdPoint));
	self->length -= length;

	new_size = self->size;
	while (new_size >> 2 > self->length)
		new_size >>= 1;
	if (new_size != self->size)
		ld_point_array_set_size (self, new_size);
}

/**
 * ld_point_array_set_size:
 * @self: an #LdPointArray structure.
 * @size: the new size.
 *
 * Change size of the array.
 */
void ld_point_array_set_size (LdPointArray *self, guint size)
{
	g_return_if_fail (self != NULL);

	if (self->size == size)
		return;

	self->points = g_realloc (self->points, size * sizeof (LdPoint));
	if (self->length > size)
		self->length = size;
	if (self->size < size)
		memset (self->points + self->length, 0, size - self->length);

	self->size = size;
}

/**
 * ld_rectangle_copy:
 * @self: an #LdRectangle structure.
 *
 * Makes a copy of the structure.
 * The result must be freed by ld_rectangle_free().
 *
 * Return value: a copy of @self.
 */
DEFINE_BOXED_TRIVIAL_COPY (LdRectangle, ld_rectangle)

/**
 * ld_rectangle_free:
 * @self: an #LdRectangle structure.
 *
 * Frees the structure created with ld_rectangle_copy().
 */
DEFINE_BOXED_TRIVIAL_FREE (LdRectangle, ld_rectangle)

/**
 * ld_rectangle_intersects:
 * @self: an #LdRectangle structure.
 * @rect: an #LdRectangle to be checked for intersection.
 *
 * Return value: %TRUE if the two rectangles intersect.
 */
gboolean
ld_rectangle_intersects (const LdRectangle *self, const LdRectangle *rect)
{
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (rect != NULL, FALSE);

	return !(self->x > rect->x + rect->width
	      || self->y > rect->y + rect->height
	      || self->x + self->width  < rect->x
	      || self->y + self->height < rect->y);
}

/**
 * ld_rectangle_contains:
 * @self: an #LdRectangle structure.
 * @rect: an #LdRectangle to be checked for containment.
 *
 * Return value: %TRUE if @self fully contains @rect.
 */
gboolean
ld_rectangle_contains (const LdRectangle *self, const LdRectangle *rect)
{
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (rect != NULL, FALSE);

	return (self->x <= rect->x
	     && self->y <= rect->y
	     && self->x + self->width  >= rect->x + rect->width
	     && self->y + self->height >= rect->y + rect->height);
}

/**
 * ld_rectangle_contains_point:
 * @self: an #LdRectangle structure.
 * @point: the point to be checked.
 *
 * Return value: %TRUE if the rectangle contains the specified point.
 */
gboolean
ld_rectangle_contains_point (const LdRectangle *self, const LdPoint *point)
{
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (point != NULL, FALSE);

	return (point->x >= self->x && point->x <= self->x + self->width
	     && point->y >= self->y && point->y <= self->y + self->height);
}

/**
 * ld_rectangle_extend:
 * @self: an #LdRectangle structure.
 * @border: the border by which the rectangle should be extended.
 *
 * Extend a rectangle on all sides.
 */
void
ld_rectangle_extend (LdRectangle *self, gdouble border)
{
	g_return_if_fail (self != NULL);

	self->x -= border;
	self->y -= border;
	self->width  += 2 * border;
	self->height += 2 * border;
}