openjscad-aurora-webapp
526 строк · 10.5 Кб
1/*
2Contributed By: Willliam A Adams
3September 2011
4Adapted for OpenJSCAD.org by Rene K. Mueller, 2013/04/01
5*/
6include("maths_geodesic.jscad");7
8//var Cpi = 3.14159;
9//var Cphi = 1.61803399;
10//var Cepsilon = 0.00000001;
11
12// Information about platonic solids
13// This information is useful in constructing the various solids
14// can be found here: http://en.wikipedia.org/wiki/Platonic_solid
15// V - vertices
16// E - edges
17// F - faces
18// number, V, E, F, schlafli symbol, dihedral angle, element, name
19//tetrahedron = [1, 4, 6, 4, [3,3], 70.5333, "fire", "tetrahedron"];
20//hexahedron = [2, 8, 12, 6, [4,3], 90, "earth", "cube"];
21//octahedron = [3, 6, 12, 8, [3,4], 109.467, "air", "air"];
22//dodecahedron = [4, 20, 30, 12, [5,3], 116.565, "ether", "universe"];
23//icosahedron = [5, 12, 30, 20, [3,5], 138.190, "water", "water"];
24
25// Schlafli representation for the platonic solids
26// Given this representation, we have enough information
27// to derive a number of other attributes of the solids
28var tetra_sch = [3,3];29var hexa_sch = [4,3];30var octa_sch = [3,4];31var dodeca_sch = [5,3];32var icosa_sch = [3,5];33
34// Given the schlafli representation, calculate
35// the number of edges, vertices, and faces for the solid
36function plat_edges(pq) { return (2*pq[0]*pq[1])/37((2*pq[0])-(pq[0]*pq[1])+(2*pq[1])); }38function plat_vertices(pq) { return (2*plat_edges(pq))/pq[1]; }39function plat_faces(pq) { return (2*plat_edges(pq))/pq[0]; }40
41
42// Calculate angular deficiency of each vertex in a platonic solid
43// p - sides
44// q - number of edges per vertex
45//function angular_defect(pq) = 360 - (poly_single_interior_angle(pq)*pq[1]);
46function plat_deficiency(pq) { return DEGREES(2*Cpi - pq[1]*Cpi*(1-2/pq[0])); }47
48function plat_dihedral(pq) { return 2 * asin( cos(180/pq[1])/sin(180/pq[0])); }49
50function plat_circumradius(pq, a) {51return (a/2)*52tan(Cpi/pq[1])*53tan(plat_dihedral(pq)/2);54}
55
56function plat_midradius(pq, a) {57return (a/2)*58cot(Cpi/pq[0])*59tan(plat_dihedral(pq)/2);60}
61
62function plat_inradius(pq,a) {63return a/(2*tan(DEGREES(Cpi/pq[0])))*64sqrt((1-cos(plat_dihedral(pq)))/(1+cos(plat_dihedral(pq))));65}
66
67//================================================
68// Tetrahedron
69//================================================
70var tetra_cart = [71[+1, +1, +1],72[-1, -1, +1],73[-1, +1, -1],74[+1, -1, -1]75];76
77function tetra_unit(rad) {78rad = rad==='undefined'?1:rad;79return [80sph_to_cart(sphu_from_cart(tetra_cart[0], rad)),81sph_to_cart(sphu_from_cart(tetra_cart[1], rad)),82sph_to_cart(sphu_from_cart(tetra_cart[2], rad)),83sph_to_cart(sphu_from_cart(tetra_cart[3], rad)),84];85}
86
87var tetrafaces = [88[0, 3, 1],89[0,1,2],90[2,1,3],91[0,2,3]92];93
94var tetra_edges = [95[0,1],96[0,2],97[0,3],98[1,2],99[1,3],100[2,3],101];102
103tetrahedron = function tetrahedron(rad) {104//tetrahedron = function(rad) {
105rad = rad==='undefined'?1:rad;106return [tetra_unit(rad), tetrafaces, tetra_edges];107}
108
109
110
111//================================================
112// Hexahedron - Cube
113//================================================
114// vertices for a unit cube with sides of length 1
115var hexa_cart = [116[0.5, 0.5, 0.5],117[-0.5, 0.5, 0.5],118[-0.5, -0.5, 0.5],119[0.5, -0.5, 0.5],120[0.5, 0.5, -0.5],121[-0.5, 0.5, -0.5],122[-0.5, -0.5, -0.5],123[0.5, -0.5, -0.5],124];125
126function hexa_unit(rad) {127rad = rad==='undefined'?1:rad;128return [129sph_to_cart(sphu_from_cart(hexa_cart[0], rad)),130sph_to_cart(sphu_from_cart(hexa_cart[1], rad)),131sph_to_cart(sphu_from_cart(hexa_cart[2], rad)),132sph_to_cart(sphu_from_cart(hexa_cart[3], rad)),133sph_to_cart(sphu_from_cart(hexa_cart[4], rad)),134sph_to_cart(sphu_from_cart(hexa_cart[5], rad)),135sph_to_cart(sphu_from_cart(hexa_cart[6], rad)),136sph_to_cart(sphu_from_cart(hexa_cart[7], rad)),137];138}
139
140// enumerate the faces of a cube
141// vertex order is clockwise winding
142var hexafaces = [143[0,3,2,1], // top144[0,1,5,4],145[1,2,6,5],146[2,3,7,6],147[3,0,4,7],148[4,5,6,7], // bottom149];150
151var hexa_edges = [152[0,1],153[0,3],154[0,4],155[1,2],156[1,5],157[2,3],158[2,6],159[3,7],160[4,5],161[4,7],162[5,4],163[5,6],164[6,7],165];166
167
168hexahedron = function hexahedron(rad) {169rad = rad!=='undefined'?rad:1;170return [hexa_unit(rad), hexafaces, hexa_edges];171}
172
173
174//================================================
175// Octahedron
176//================================================
177
178var octa_cart = [179[+1, 0, 0], // + x axis180[-1, 0, 0], // - x axis181[0, +1, 0], // + y axis182[0, -1, 0], // - y axis183[0, 0, +1], // + z axis184[0, 0, -1] // - z axis185];186
187function octa_unit(rad) {188rad = rad!=='undefined'?rad:1;189return [190sph_to_cart(sphu_from_cart(octa_cart[0], rad)),191sph_to_cart(sphu_from_cart(octa_cart[1], rad)),192sph_to_cart(sphu_from_cart(octa_cart[2], rad)),193sph_to_cart(sphu_from_cart(octa_cart[3], rad)),194sph_to_cart(sphu_from_cart(octa_cart[4], rad)),195sph_to_cart(sphu_from_cart(octa_cart[5], rad)),196];197}
198
199var octafaces = [200[4,2,0],201[4,0,3],202[4,3,1],203[4,1,2],204[5,0,2],205[5,3,0],206[5,1,3],207[5,2,1]208];209
210var octa_edges = [211[0,2],212[0,3],213[0,4],214[0,5],215[1,2],216[1,3],217[1,4],218[1,5],219[2,4],220[2,5],221[3,4],222[3,5],223];224
225octahedron = function octahedron(rad) {226rad = rad!=='undefined'?rad:1;227return [octa_unit(rad), octafaces, octa_edges];228}
229
230//================================================
231// Dodecahedron
232//================================================
233// (+-1, +-1, +-1)
234// (0, +-1/Cphi, +-Cphi)
235// (+-1/Cphi, +-Cphi, 0)
236// (+-Cphi, 0, +-1/Cphi)
237
238var dodeca_cart = [239[+1, +1, +1], // 0, 0240[+1, -1, +1], // 0, 1241[-1, -1, +1], // 0, 2242[-1, +1, +1], // 0, 3243
244[+1, +1, -1], // 1, 4245[-1, +1, -1], // 1, 5246[-1, -1, -1], // 1, 6247[+1, -1, -1], // 1, 7248
249[0, +1/Cphi, +Cphi], // 2, 8250[0, -1/Cphi, +Cphi], // 2, 9251[0, -1/Cphi, -Cphi], // 2, 10252[0, +1/Cphi, -Cphi], // 2, 11253
254[-1/Cphi, +Cphi, 0], // 3, 12255[+1/Cphi, +Cphi, 0], // 3, 13256[+1/Cphi, -Cphi, 0], // 3, 14257[-1/Cphi, -Cphi, 0], // 3, 15258
259[-Cphi, 0, +1/Cphi], // 4, 16260[+Cphi, 0, +1/Cphi], // 4, 17261[+Cphi, 0, -1/Cphi], // 4, 18262[-Cphi, 0, -1/Cphi], // 4, 19263];264
265function dodeca_unit(rad) {266rad = rad!=='undefined'?rad:1;267return [268sph_to_cart(sphu_from_cart(dodeca_cart[0], rad)),269sph_to_cart(sphu_from_cart(dodeca_cart[1], rad)),270sph_to_cart(sphu_from_cart(dodeca_cart[2], rad)),271sph_to_cart(sphu_from_cart(dodeca_cart[3], rad)),272sph_to_cart(sphu_from_cart(dodeca_cart[4], rad)),273sph_to_cart(sphu_from_cart(dodeca_cart[5], rad)),274sph_to_cart(sphu_from_cart(dodeca_cart[6], rad)),275sph_to_cart(sphu_from_cart(dodeca_cart[7], rad)),276sph_to_cart(sphu_from_cart(dodeca_cart[8], rad)),277sph_to_cart(sphu_from_cart(dodeca_cart[9], rad)),278sph_to_cart(sphu_from_cart(dodeca_cart[10], rad)),279sph_to_cart(sphu_from_cart(dodeca_cart[11], rad)),280sph_to_cart(sphu_from_cart(dodeca_cart[12], rad)),281sph_to_cart(sphu_from_cart(dodeca_cart[13], rad)),282sph_to_cart(sphu_from_cart(dodeca_cart[14], rad)),283sph_to_cart(sphu_from_cart(dodeca_cart[15], rad)),284sph_to_cart(sphu_from_cart(dodeca_cart[16], rad)),285sph_to_cart(sphu_from_cart(dodeca_cart[17], rad)),286sph_to_cart(sphu_from_cart(dodeca_cart[18], rad)),287sph_to_cart(sphu_from_cart(dodeca_cart[19], rad)),288];289}
290
291
292// These are the pentagon faces
293// but CGAL has a problem rendering if things are
294// not EXACTLY coplanar
295// so use the triangle faces instead
296//dodeca_faces=[
297// [1,9,8,0,17],
298// [9,1,14,15,2],
299// [9,2,16,3,8],
300// [8,3,12,13,0],
301// [0,13,4,18,17],
302// [1,17,18,7,14],
303// [15,14,7,10,6],
304// [2,15,6,19,16],
305// [16,19,5,12,3],
306// [12,5,11,4,13],
307// [18,4,11,10,7],
308// [19,6,10,11,5]
309// ];
310var dodeca_faces = [311[1,9,8],312[1,8,0],313[1,0,17],314
315[9,1,14],316[9,14,15],317[9,15,2],318
319[9,2,16],320[9,16,3],321[9,3,8],322
323[8,3,12],324[8,12,13],325[8,13,0],326
327[0,13,4],328[0,4,18],329[0,18,17],330
331[1,17,18],332[1,18,7],333[1,7,14],334
335[15,14,7],336[15,7,10],337[15,10,6],338
339[2,15,6],340[2,6,19],341[2,19,16],342
343[16,19,5],344[16,5,12],345[16,12,3],346
347[12,5,11],348[12,11,4],349[12,4,13],350
351[18,4,11],352[18,11,10],353[18,10,7],354
355[19,6,10],356[19,10,11],357[19,11,5]358];359
360var dodeca_edges=[361[0,8],362[0,13],363[0,17],364
365[1,9],366[1,14],367[1,17],368
369[2,9],370[2,15],371[2,16],372
373[3,8],374[3,12],375[3,16],376
377[4,11],378[4,13],379[4,18],380
381[5,11],382[5,12],383[5,19],384
385[6,10],386[6,15],387[6,19],388
389[7,10],390[7,14],391[7,18],392
393[8,9],394[10,11],395[12,13],396[14,15],397[16,19],398[17,18],399];400
401dodecahedron = function dodecahedron(rad) {402rad = rad!=='undefined'?rad:1;403return [dodeca_unit(rad), dodeca_faces, dodeca_edges];404}
405
406//================================================
407// Icosahedron
408//================================================
409//
410// (0, +-1, +-Cphi)
411// (+-Cphi, 0, +-1)
412// (+-1, +-Cphi, 0)
413
414var icosa_cart = [415[0, +1, +Cphi], // 0416[0, +1, -Cphi], // 1417[0, -1, -Cphi], // 2418[0, -1, +Cphi], // 3419
420[+Cphi, 0, +1], // 4421[+Cphi, 0, -1], // 5422[-Cphi, 0, -1], // 6423[-Cphi, 0, +1], // 7424
425[+1, +Cphi, 0], // 8426[+1, -Cphi, 0], // 9427[-1, -Cphi, 0], // 10428[-1, +Cphi, 0] // 11429];430
431function icosa_sph(rad) {432rad = rad!=='undefined'?rad:1;433return [434sphu_from_cart(icosa_cart[0], rad),435sphu_from_cart(icosa_cart[1], rad),436sphu_from_cart(icosa_cart[2], rad),437sphu_from_cart(icosa_cart[3], rad),438sphu_from_cart(icosa_cart[4], rad),439sphu_from_cart(icosa_cart[5], rad),440sphu_from_cart(icosa_cart[6], rad),441sphu_from_cart(icosa_cart[7], rad),442sphu_from_cart(icosa_cart[8], rad),443sphu_from_cart(icosa_cart[9], rad),444sphu_from_cart(icosa_cart[10], rad),445sphu_from_cart(icosa_cart[11], rad),446];447}
448
449function icosa_unit(rad) {450rad = rad!=='undefined'?rad:1;451return [452sph_to_cart(sphu_from_cart(icosa_cart[0], rad)),453sph_to_cart(sphu_from_cart(icosa_cart[1], rad)),454sph_to_cart(sphu_from_cart(icosa_cart[2], rad)),455sph_to_cart(sphu_from_cart(icosa_cart[3], rad)),456sph_to_cart(sphu_from_cart(icosa_cart[4], rad)),457sph_to_cart(sphu_from_cart(icosa_cart[5], rad)),458sph_to_cart(sphu_from_cart(icosa_cart[6], rad)),459sph_to_cart(sphu_from_cart(icosa_cart[7], rad)),460sph_to_cart(sphu_from_cart(icosa_cart[8], rad)),461sph_to_cart(sphu_from_cart(icosa_cart[9], rad)),462sph_to_cart(sphu_from_cart(icosa_cart[10], rad)),463sph_to_cart(sphu_from_cart(icosa_cart[11], rad)),464];465}
466
467var icosa_faces = [468[3,0,4],469[3,4,9],470[3,9,10],471[3,10,7],472[3,7,0],473[0,8,4],474[0,7,11],475[0,11,8],476[4,8,5],477[4,5,9],478[7,10,6],479[7,6,11],480[9,5,2],481[9,2,10],482[2,6,10],483[1,5,8],484[1,8,11],485[1,11,6],486[5,1,2],487[2,1,6]488];489
490var icosa_edges = [491[0,3],492[0,4],493[0,7],494[0,8],495[0,11],496[1,5],497[1,8],498[1,11],499[1,6],500[1,2],501[2,5],502[2,6],503[2,9],504[2,10],505[3,4],506[3,9],507[3,10],508[3,7],509[4,5],510[4,8],511[4,9],512[5,8],513[5,9],514[6,7],515[6,10],516[6,11],517[7,10],518[7,11],519[8,11],520[9,10],521];522
523icosahedron = function icosahedron(rad) {524rad = rad!=='undefined'?rad:1;525return [icosa_unit(rad), icosa_faces, icosa_edges];526}
527
528
529