summaryrefslogtreecommitdiff
path: root/meme/mesh.c
blob: 9195885385215af62f62984c48880c06a2cb96c8 (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
/*
 * mesh.c - 3D mesh data structures
 *
 * Written 2014 by Werner Almesberger
 * Copyright 2014 by Werner Almesberger
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 */


#include "util.h"
#include "mesh.h"


#define	VHASH_TABLE	(1 << 20)
#define	EHASH_TABLE	(1 << 20)

#define	CHUNK	65536


struct v {
	struct vertex v;
	struct v *next;
};

struct e {
	struct edge e;
	struct e *next;
};


static struct v *vhash[VHASH_TABLE];
static struct e *ehash[EHASH_TABLE];


static struct edge *edges = NULL;
struct facet *facets = NULL;


#define	VHASH(x, y, z)	((x) % VHASH_TABLE)
#define	EHASH(a, b)	((long) (a) % EHASH_TABLE)


static int vused = 0;
static struct v *vchunk;

static int eused = 0;
static struct e *echunk;

static int fused = 0;
static struct facet *fchunk;


const struct vertex *vertex_add(int x, int y, int z)
{
	struct v **h = &vhash[VHASH(x, y, z)];
	struct v *v;

	for (v = *h; v; v = v->next)
		if (v->v.x == x && v->v.y == y && v->v.z == z)
			return &v->v;

	if (vused == CHUNK)
		vused= 0;
	if (!vused) {
		vchunk = calloc(CHUNK, sizeof(struct v));
	}
	v = &vchunk[vused++];
//	v = alloc_type(struct v);
	v->v.x = x;
	v->v.y = y;
	v->v.z = z;
	v->next = *h;
	*h = v;
	return &v->v;
}


void vertex_foreach(bool (*fn)(struct vertex *v, void *user), void *user)
{
	struct v *v;
	int i;

	for (i = 0; i != VHASH_TABLE; i++)
		for (v = vhash[i]; v; v = v->next)
			if (fn(&v->v, user))
				return;
}


void vertex_foreach_const(bool (*fn)(const struct vertex *v, void *user),
    void *user)
{
	const struct v *v;
	int i;

	for (i = 0; i != VHASH_TABLE; i++)
		for (v = vhash[i]; v; v = v->next)
			if (fn(&v->v, user))
				return;
}


void edge_foreach(bool (*fn)(const struct vertex *a, const struct vertex *b,
    void *user), void *user)
{
	const struct edge *e;

	for (e = edges; e; e = e->next)
		if (fn(e->a, e->b, user))
			return;
}


static void edge_add(const struct vertex *a, const struct vertex *b)
{
	const struct vertex *tmp;
	struct e **h;
	struct e *e;

	if (a > b) {
		tmp = a;
		a = b;
		b = tmp;
	}
	h = &ehash[EHASH(a, b)];
	for (e = *h; e; e = e->next)
		if (e->e.a == a && e->e.b == b)
			return;

	if (eused == CHUNK)
		eused = 0;
	if (!eused) {
		echunk = calloc(CHUNK, sizeof(struct e));
	}
	e = &echunk[eused++];
//	e = alloc_type(struct e);
	e->e.a = a;
	e->e.b = b;
	e->e.next = edges;
	edges = &e->e;
	e->next = *h;
	*h = e;
}


const struct facet *facet_add(const struct vertex *a, const struct vertex *b,
    const struct vertex *c)
{
	struct facet *f;

	if (fused == CHUNK)
		fused= 0;
	if (!fused) {
		fchunk = calloc(CHUNK, sizeof(struct facet));
	}
	f = &fchunk[fused++];
//	f = alloc_type(struct facet);
	f->v[0] = a;
	f->v[1] = b;
	f->v[2] = c;

	edge_add(a, b);
	edge_add(b, c);
	edge_add(c, a);

	f->next = facets;
	facets = f;
	return f;
}


void mesh_init(void)
{
}