openjscad-aurora-webapp

Форк
0
190 строк · 5.3 Кб
1
/*
2
   License: This code is placed in the public Domain
3
	Contributed By: Willliam A Adams
4
	September 2011
5
	Adapted for OpenJSCAD.org by Rene K. Mueller, 2013/04/01
6
*/
7

8
// A couple of useful constants
9
Cpi = 3.14159;       // global!
10
Cphi = 1.61803399;
11
Cepsilon = 0.00000001;
12
//var Cpi = 3.14159;    // local!
13
//var Cphi = 1.61803399;
14
//var Cepsilon = 0.00000001;
15

16
 
17
// Function: clean
18
//
19
// Parameters:
20
//	n - A number that might be very close to zero
21
// Description:  
22
//	There are times when you want a very small number to 
23
// 	just be zero, instead of being that very small number.
24
//	This function will compare the number to an arbitrarily small 
25
//	number.  If it is smaller than the 'epsilon', then zero will be 
26
// 	returned.  Otherwise, the original number will be returned.
27
//
28

29
function clean(n) { return (n < 0) ? ((n < -Cepsilon) ? n : 0) : 
30
	(n < Cepsilon) ? 0 : n; };
31

32
// Function: safediv
33
//
34
// Parameters
35
//	n - The numerator
36
//	d - The denominator
37
//
38
// Description:
39
//	Since division by zero is generally not a desirable thing, safediv
40
//	will return '0' whenever there is a division by zero.  Although this will
41
//	mask some erroneous division by zero errors, it is often the case
42
//	that you actually want this behavior.  So, it makes it convenient.
43
function safediv(n,d) { return (d==0) ? 0 : n/d; }
44

45

46
//==================================
47
// Degrees
48
//==================================
49

50
function DEGREES(radians) { return (180/Cpi) * radians; }
51

52
function RADIANS(degrees) { return Cpi/180 * degrees; }
53

54
function deg(deg, min, sec) { return [deg, min===undefined?0:min, sec===undefined?0:sec]; }
55

56
function deg_to_dec(d) { return d[0] + d[1]/60 + d[2]/60/60; }
57

58

59
//==================================
60
//  Spherical coordinates
61
//==================================
62

63
// create an instance of a spherical coordinate
64
// long - rotation around z -axis
65
// lat - latitude, starting at 0 == 'north pole'
66
// rad - distance from center
67
function sph(long, lat, rad) { return [long, lat, rad===undefined?1:rad] }
68

69
// Convert spherical to cartesian
70
//function sph_to_cart(s) { return [
71
//	clean(s[2]*sin(s[1])*cos(s[0])),  
72
//	clean(s[2]*sin(s[1])*sin(s[0])),
73
//	clean(s[2]*cos(s[1]))
74
//	]; }
75

76
sph_to_cart = function sph_to_cart(s) { 
77
   return [
78
	clean(s[2]*sin(s[1])*cos(s[0])),  
79

80
	clean(s[2]*sin(s[1])*sin(s[0])),
81

82
	clean(s[2]*cos(s[1]))
83
	]; }
84

85
// Convert from cartesian to spherical
86
sph_from_cart = function sph_from_cart(c) { 
87
   return sph(
88
	atan2(c[1],c[0]), 
89
	atan2(sqrt(c[0]*c[0]+c[1]*c[1]), c[2]), 
90
	sqrt(c[0]*c[0]+c[1]*c[1]+c[2]*c[2])
91
	); }
92

93
sphu_from_cart = function sphu_from_cart(c, rad) { 
94
   return sph(
95
	atan2(c[1],c[0]), 
96
	atan2(sqrt(c[0]*c[0]+c[1]*c[1]), c[2]), 
97
	rad===undefined?1:rad
98
	); }
99

100
// compute the chord distance between two points on a sphere
101
sph_dist = function sph_dist(c1, c2) { 
102
   return sqrt(
103
	c1[2]*c1[2] + c2[2]*c2[2] - 
104
	2*c1[2]*c2[2]*
105
	((cos(c1[1])*cos(c2[1])) + cos(c1[0]-c2[0])*sin(c1[1])*sin(c2[1]))   
106
	); }
107

108

109
//==========================================
110
//	Geodesic calculations
111
// 
112
//  Reference: Geodesic Math and How to Use It
113
//  By: Hugh Kenner
114
//  Second Paperback Edition (2003), p.74-75
115
//  http://www.amazon.com/Geodesic-Math-How-Hugh-Kenner/dp/0520239318
116
//
117
//  The book was used for reference, so if you want to check the math, 
118
//  you can plug in various numbers to various routines and see if you get
119
//  the same numbers in the book.
120
//
121
//  In general, there are enough routines here to implement the various
122
//  pieces necessary to make geodesic objects.
123
//==========================================
124

125
function poly_sum_interior_angles(sides) { return (sides-2)*180; }
126
function poly_single_interior_angle(pq) { return poly_sum_interior_angles(pq[0])/pq[0]; }
127

128

129
// Calculate angular deficiency of each vertex in a platonic solid
130
// p - sides
131
// q - number of edges per vertex
132
function angular_defect(pq) { return 360 - (poly_single_interior_angle(pq)*pq[1]); }
133
function plat_deficiency(pq) { return DEGREES(2*Cpi - pq[1]*Cpi*(1-2/pq[0])); }
134

135
function plat_dihedral(pq) { return 2 * asin( cos(180/pq[1])/sin(180/pq[0])); }
136

137
// Given a set of coordinates, return the frequency
138
// Simply calculated by adding up the values of the coordinates
139
function geo_freq(xyz) { return xyz[0]+xyz[1]+xyz[2]; }
140

141
// Convert between the 2D coordinates of vertices on the face triangle
142
// to the 3D vertices needed to calculate spherical coordinates
143
function geo_tri2_tri3(xyf) { return [xyf[1], xyf[0]-xyf[1], xyf[2]-xyf[0]]; }
144

145
// Given coordinates for a vertex on the octahedron face
146
// return the spherical coordinates for the vertex
147
// class 1, method 1
148
function octa_class1(c) { 
149
   return sph(
150
	atan(safediv(c[0], c[1])),
151
	atan(sqrt(c[0]*c[0]+c[1]*c[1])/c[2]),
152
	1 
153
	); }
154

155
function octa_class2(c) { 
156
   return sph(
157
	atan(c[0]/c[1]),
158
	atan( sqrt( 2*(c[0]*c[0]+c[1]*c[1])) /c[2]),
159
	1 
160
	); }
161

162
function icosa_class1(c) { 
163
   return 
164
   octa_class1(
165
	[
166
		c[0]*sin(72),  
167
		c[1]+c[0]*cos(72),  
168
		geo_freq(c)/2+c[2]/Cphi
169
	]); }
170

171
function icosa_class2(c) { 
172
   return sph(
173
	atan([c0]/c[1]), 
174
	atan(sqrt(c[0]*c[0]+c[1]*c[1]))/cos(36)*c[2],
175
	1
176
	); }
177
 
178
function tetra_class1(c) { 
179
   return octa_class1(
180
	[
181
		sqrt(3*c[0]),  
182
		2*c[1]-c[0],  
183
		(3*c[2]-c[0]-c[1])/sqrt(2)
184
	]); }
185

186
function class1_icosa_chord_factor(v1, v2, freq) { 
187
   return sph_dist( 
188
		icosa_class1(geo_tri2_tri3( [v1[0], v1[1], freq])),
189
		icosa_class1(geo_tri2_tri3( [v2[0], v2[1], freq]))
190
	); }
191

192

193

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.