aboutsummaryrefslogtreecommitdiffstats
path: root/buch/chapters/95-homologie/torus/torus.m
blob: 64a615e3f041f70a5f3368a972f486ee60637fd9 (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
#
# torus.m
#
# (c) 2021 Prof Dr Andreas Müller, OST Ostschweizer Fachhochschule
#

global n;
n = 32;
global m;
m = 18;
global R;
R = 3;
global r;
r = 1;

# Knoten, Kanten, Dreiecke
global nvertices;
nvertices = n * m;
global nedges;
nedges = 3 * nvertices;
global ntriangles;
ntriangles = 2 * nvertices;

edges = zeros(nedges, 2);
global edges;
triangles = zeros(ntriangles, 3);
global triangles;

function retval = torus(x, y)
	global n;
	global m;
	global r;
	global R;
	phi = x * 2 * pi / n;
	theta = y * 2 * pi / m;
	z = -r * sin(theta);
	x = (R + r * cos(theta)) * cos(phi);
	y = -(R + r * cos(theta)) * sin(phi);
	retval = [x, y, z];
endfunction

coordinates = zeros(nvertices, 3);
for x = (0:n-1)
	for y = (0:m-1)
		k = x + n * y;
		coordinates(k+1,:) = torus(x, y);
	endfor
endfor
coordinates

function insert_edges(ll, lr, ul, ur)
	global edges;
	k = 3 * ll + 1;
	edges(k+1,1) = ll;
	edges(k+1,2) = lr;
	edges(k+0,1) = ll;
	edges(k+0,2) = ur;
	edges(k+2,1) = ll;
	edges(k+2,2) = ul;
endfunction

function insert_triangles(ll, lr, ul, ur)
	global triangles;
	k = 2 * ll;
printf("ll=%d, k=%d\n", ll, k);
	triangles(k+1,1) = ll;
	triangles(k+1,2) = lr;
	triangles(k+1,3) = ur;
	triangles(k+2,1) = ll;
	triangles(k+2,2) = ur;
	triangles(k+2,3) = ul;
endfunction

# normal squares
for x = (0:n-2)
	for y = (0:m-2)
		ll = x + n * y;
		lr = ll + 1;
		ul = ll + n;
		ur = ul + 1;
		insert_edges(ll, lr, ul, ur);
		insert_triangles(ll, lr, ul, ur);
	endfor
endfor

# right border
x = n-1;
for y = (0:m-2)
	ll = x + n * y;
	lr = ll - (n-1);
	ul = ll + n;
	ur = lr + n;
	insert_edges(ll, lr, ul, ur);
	insert_triangles(ll, lr, ul, ur);
endfor

# top border
y = m-1;
for x = (0:n-2)
	ll = x + n * y;
	lr = ll + 1;
	ul = x;
	ur = ul + 1;
	insert_edges(ll, lr, ul, ur);
	insert_triangles(ll, lr, ul, ur);
endfor

# upper right corner
x = n-1;
y = m-1;
ll = n * m - 1;
lr = n * (m-1);
ur = 0;
ul = n - 1;
insert_edges(ll, lr, ul, ur);
insert_triangles(ll, lr, ul, ur);

edges;
triangles;

d1 = zeros(nvertices, nedges);
for i = (1:nedges)
	d1(edges(i,1) + 1, i) = -1;
	d1(edges(i,2) + 1, i) = +1;
endfor

function retval = find_edge(from, to)
	global edges;
	global nedges;
	retval = 0;
	for i = (1:nedges)
		if ((edges(i,1) == from) && (edges(i,2) == to))
			retval = i;
			printf("    test (%d,%d) == (%d,%d) -> %d\n", from, to,
				edges(i,1), edges(i,2), retval);
		elseif ((edges(i,1) == to) && (edges(i,2) == from))
			retval = -i;
			printf("    test (%d,%d) == (%d,%d) -> %d\n", from, to,
				edges(i,1), edges(i,2), retval);
		endif
	endfor
endfunction

global d2;
d2 = zeros(nedges, ntriangles);
function triangle_edge(i, pointa, pointb)
	global d2;
	edge = find_edge(pointa, pointb);
	if (edge > 0)
		d2(edge, i) = +1;
	elseif (edge < 0)
		d2(-edge, i) = -1;
	endif
endfunction

for i = (1:ntriangles)
	point1 = triangles(i,1);
	point2 = triangles(i,2);
	point3 = triangles(i,3);
	#printf("triangle %d: %d, %d, %d\n", i-1, point1, point2, point3);
	triangle_edge(i, point1, point2);
	triangle_edge(i, point2, point3);
	triangle_edge(i, point3, point1);
endfor

B = d2';

global zyklentableau
zyklentableau = rref(d1)
i=((zyklentableau != 0) .* (1:nedges));
global determined;
determined = min((i+nedges*(i==0))')';
determined = determined(1:nvertices-1,1);

function retval = is_determined(i)
	global nvertices;
	global determined;
#	printf("test i = %d\n", i);
	retval = 0;
	for k = (1:nvertices-1)
#printf("check k=%d, i=%d\n", k, i);
		if (determined(k,1) == i)
			retval = 1;
		endif
	endfor
endfunction

function retval = zyklus(i)
	global zyklentableau
	global nedges
	global nvertices
	global determined
	retval = zeros(nedges,1);
	retval(i,1) = 1;
	for j = (1:nvertices-1)
		retval(determined(j,1),1) = -zyklentableau(j,i);
	endfor
endfunction

Z = zeros(nedges, nedges - nvertices)
current = 1;
for i = (1:nedges)
	if 1 == is_determined(i)
		# printf("skipping %d\n", i);
	else
		Z(:,current) = zyklus(i);
		current = current + 1;
	endif
endfor

Z;

zh = size(Z)(2);
H = zeros(ntriangles + zh,nedges);
H(1:ntriangles,:) = B;
H((ntriangles+1):(ntriangles+zh),:) = Z';

H;
size(H)

rref(B');

function retval = pivotindex(zeile)
	w = size(zeile)(2);
	for k = (1:w)
		if (zeile(1,k) != 0)
			retval = k;
			return;
		endif
	endfor
	retval = 0;
endfunction

h = size(H)(1)
w = size(H)(2)
j = 1;

for i = (1:h-1)
	j = pivotindex(H(i,:));
	printf("reduction for i = %d, j = %d\n", i, j);
	if (j != 0) 
		# find pivot index
		pivot = H(i,j);
		if (pivot != 0)
			H(i,:) = H(i,:) / pivot;
			for k = ((i+1):h)
				H(k,:) = H(k,:) - H(k,j) * H(i,:);
			endfor
		endif
	endif
endfor
H;

Hbasis = zeros(2,nedges);
current = 1;
for i = ((ntriangles+1):h)
	if norm(H(i,:)) > 0
		Hbasis(current,:) = H(i,:)
		current = current + 1;
	endif
endfor
#size(Hbasis)
#nedges
#Hbasis
#Hbasis(1,nedges-2)=0;
#Hbasis(1,nedges-1)=1;
Hbasis

edges(71,:)
coordinates

fn = fopen("torus.inc", "w");

# torusflaeche

fprintf(fn, "#macro torusflaeche()\n");
fprintf(fn, "    mesh {\n");
for k = (1:ntriangles)
	fprintf(fn, "\ttriangle { ");
	punkt1 = coordinates(triangles(k,1)+1,:);
	punkt2 = coordinates(triangles(k,2)+1,:);
	punkt3 = coordinates(triangles(k,3)+1,:);
	fprintf(fn, "<%.4f,%.4f,%.4f>, ", punkt1(1,1), punkt1(1,3), punkt1(1,2));
	fprintf(fn, "<%.4f,%.4f,%.4f>, ", punkt2(1,1), punkt2(1,3), punkt2(1,2));
	fprintf(fn, "<%.4f,%.4f,%.4f> }\n", punkt3(1,1), punkt3(1,3), punkt3(1,2));
endfor
fprintf(fn, "    }\n")
fprintf(fn, "#end\n");

fprintf(fn, "#macro zyklus1()\n");
fprintf(fn, "    union {\n");
for i = (1:nedges)
	h = Hbasis(1,i);
	if h != 0
		if h > 0
			punkt1 = coordinates(edges(i,1)+1,:);
printf("i=%d\n", i);
			punkt2 = coordinates(edges(i,2)+1,:);
		else
			punkt2 = coordinates(edges(i,1)+1,:);
			punkt1 = coordinates(edges(i,2)+1,:);
		endif
		fprintf(fn, "\tcylinder { ");
		fprintf(fn, "<%.4f,%.4f,%.4f>, ", punkt1(1,1), punkt1(1,3), punkt1(1,2));
		fprintf(fn, "<%.4f,%.4f,%.4f>, r }\n", punkt2(1,1), punkt2(1,3), punkt2(1,2));
		fprintf(fn, "\tsphere { <%.4f,%.4f,%.4f>, r }\n", punkt1(1,1), punkt1(1,3), punkt1(1,2));
	endif
endfor
fprintf(fn, "    }\n");
fprintf(fn, "#end\n");

fprintf(fn, "#macro zyklus2()\n");
fprintf(fn, "    union {\n");
for i = (1:nedges)
	h = Hbasis(2,i);
	if h != 0
		if h > 0
			punkt1 = coordinates(edges(i,1)+1,:);
			punkt2 = coordinates(edges(i,2)+1,:);
		else
			punkt2 = coordinates(edges(i,1)+1,:);
			punkt1 = coordinates(edges(i,2)+1,:);
		endif
		fprintf(fn, "\tcylinder { ");
		fprintf(fn, "<%.4f,%.4f,%.4f>, ", punkt1(1,1), punkt1(1,3), punkt1(1,2));
		fprintf(fn, "<%.4f,%.4f,%.4f>, r }\n", punkt2(1,1), punkt2(1,3), punkt2(1,2));
		fprintf(fn, "\tsphere { <%.4f,%.4f,%.4f>, r }\n", punkt1(1,1), punkt1(1,3), punkt1(1,2));
	endif
endfor
fprintf(fn, "    }\n");
fprintf(fn, "#end\n");

fprintf(fn, "#macro triangulation()\n");
fprintf(fn, "    union {\n");
for i = (1:nvertices)
	punkt = coordinates(i,:);
	fprintf(fn, "\tsphere { <%.4f,%.4f,%.4f>, r/3 }\n", punkt(1,1), punkt(1,3), punkt(1,2));
endfor
for i = (1:nedges)
	punkt1 = coordinates(edges(i,1)+1,:);
	punkt2 = coordinates(edges(i,2)+1,:);
	fprintf(fn, "\tcylinder { <%.4f,%.4f,%.4f>, ", punkt1(1,1), punkt1(1,3), punkt1(1,2));
	fprintf(fn, "<%.4f,%.4f,%.4f>, r/3 }\n", punkt2(1,1), punkt2(1,3), punkt2(1,2));
endfor
fprintf(fn, "    }\n");
fprintf(fn, "#end\n");

fclose(fn);