1include <../std.scad>
2
3
4module test_point2d() {
5 assert(point2d([1,2,3])==[1,2]);
6 assert(point2d([2,3])==[2,3]);
7 assert(point2d([1])==[1,0]);
8}
9test_point2d();
10
11
12module test_path2d() {
13 assert(path2d([[1,2], [3,4], [5,6], [7,8]])==[[1,2],[3,4],[5,6],[7,8]]);
14 assert(path2d([[1,2,3], [2,3,4], [3,4,5], [4,5,6]])==[[1,2],[2,3],[3,4],[4,5]]);
15 assert(path2d([[1,2,3,4], [2,3,4,5], [3,4,5,6], [4,5,6,7]])==[[1,2],[2,3],[3,4],[4,5]]);
16}
17test_path2d();
18
19
20module test_point3d() {
21 assert(point3d([1,2,3,4,5])==[1,2,3]);
22 assert(point3d([1,2,3,4])==[1,2,3]);
23 assert(point3d([1,2,3])==[1,2,3]);
24 assert(point3d([2,3])==[2,3,0]);
25 assert(point3d([1])==[1,0,0]);
26}
27test_point3d();
28
29
30module test_path3d() {
31 assert(path3d([[1,2], [3,4], [5,6], [7,8]])==[[1,2,0],[3,4,0],[5,6,0],[7,8,0]]);
32 assert(path3d([[1,2], [3,4], [5,6], [7,8]],9)==[[1,2,9],[3,4,9],[5,6,9],[7,8,9]]);
33 assert(path3d([[1,2,3], [2,3,4], [3,4,5], [4,5,6]])==[[1,2,3],[2,3,4],[3,4,5],[4,5,6]]);
34 assert(path3d([[1,2,3,4], [2,3,4,5], [3,4,5,6], [4,5,6,7]])==[[1,2,3],[2,3,4],[3,4,5],[4,5,6]]);
35}
36test_path3d();
37
38
39module test_point4d() {
40 assert(point4d([1,2,3,4,5])==[1,2,3,4]);
41 assert(point4d([1,2,3,4])==[1,2,3,4]);
42 assert(point4d([1,2,3])==[1,2,3,0]);
43 assert(point4d([2,3])==[2,3,0,0]);
44 assert(point4d([1])==[1,0,0,0]);
45 assert(point4d([1,2,3],9)==[1,2,3,9]);
46 assert(point4d([2,3],9)==[2,3,9,9]);
47 assert(point4d([1],9)==[1,9,9,9]);
48}
49test_point4d();
50
51
52module test_path4d() {
53 assert(path4d([[1,2], [3,4], [5,6], [7,8]])==[[1,2,0,0],[3,4,0,0],[5,6,0,0],[7,8,0,0]]);
54 assert(path4d([[1,2,3], [2,3,4], [3,4,5], [4,5,6]])==[[1,2,3,0],[2,3,4,0],[3,4,5,0],[4,5,6,0]]);
55 assert(path4d([[1,2,3,4], [2,3,4,5], [3,4,5,6], [4,5,6,7]])==[[1,2,3,4],[2,3,4,5],[3,4,5,6],[4,5,6,7]]);
56 assert(path4d([[1,2,3,4,5], [2,3,4,5,6], [3,4,5,6,7], [4,5,6,7,8]])==[[1,2,3,4],[2,3,4,5],[3,4,5,6],[4,5,6,7]]);
57}
58test_path4d();
59
60
61module test_polar_to_xy() {
62 assert(approx(polar_to_xy(20,45), [20/sqrt(2), 20/sqrt(2)]));
63 assert(approx(polar_to_xy(20,135), [-20/sqrt(2), 20/sqrt(2)]));
64 assert(approx(polar_to_xy(20,-135), [-20/sqrt(2), -20/sqrt(2)]));
65 assert(approx(polar_to_xy(20,-45), [20/sqrt(2), -20/sqrt(2)]));
66 assert(approx(polar_to_xy(40,30), [40*sqrt(3)/2, 40/2]));
67 assert(approx(polar_to_xy([40,30]), [40*sqrt(3)/2, 40/2]));
68}
69test_polar_to_xy();
70
71
72module test_xy_to_polar() {
73 assert(approx(xy_to_polar([20/sqrt(2), 20/sqrt(2)]),[20,45]));
74 assert(approx(xy_to_polar([-20/sqrt(2), 20/sqrt(2)]),[20,135]));
75 assert(approx(xy_to_polar([-20/sqrt(2), -20/sqrt(2)]),[20,-135]));
76 assert(approx(xy_to_polar([20/sqrt(2), -20/sqrt(2)]),[20,-45]));
77 assert(approx(xy_to_polar([40*sqrt(3)/2, 40/2]),[40,30]));
78 assert(approx(xy_to_polar([-40*sqrt(3)/2, 40/2]),[40,150]));
79 assert(approx(xy_to_polar([-40*sqrt(3)/2, -40/2]),[40,-150]));
80 assert(approx(xy_to_polar([40*sqrt(3)/2, -40/2]),[40,-30]));
81}
82test_xy_to_polar();
83
84
85module test_project_plane() {
86 assert(approx(project_plane([[-10,0,-10], [0,0,0], [0,-10,-10]],[-5,0,-5]),[0,10*sqrt(2)/2]));
87 assert(approx(project_plane([[-10,0,-10], [0,0,0], [0,-10,-10]],[0,-5,-5]),[6.12372, 10.6066],eps=1e-5));
88 assert_approx(project_plane([[3,4,5],[1,3,9],[4,7,13]], [[3,4,5],[1,3,9],[5,3,2]]),[[0,0],[0,4.58257569496],[-0.911684611677,-3.27326835354]]);
89 assert_approx(project_plane([[3,4,5],[1,3,9],[4,7,13]], [[3,4,5],[1,3,9],[4,7,13]]),[[0,0],[0,4.58257569496],[6.26783170528,5.89188303637]]);
90
91 assert_approx(project_plane([2,3,4,2], [4,2,3]),[2.33181857677,-0.502272134844]);
92 assert_approx(project_plane([2,3,4,2], [[1,1,1],[0,0,0]]),[[0.430748825729,0.146123238594],[0,0]]);
93 assert_approx(project_plane([2,3,4,2]),[[0.920855800833,-0.11871629875,-0.371390676354,0],[-0.11871629875,0.821925551875,-0.557086014531,-2.77555756156e-17],[0.371390676354,0.557086014531,0.742781352708,-0.371390676354],[0,0,0,1]]);
94 assert_approx(project_plane([[1,1,1],[3,1,3],[1,1,4]]),[[-1/sqrt(2),1/sqrt(2),0,0],[0,0,1,-1],[1/sqrt(2),1/sqrt(2),0,-sqrt(2)],[0,0,0,1]]);
95
96 normal = rands(-1,1,3,seed=3)+[2,0,0];
97 offset = rands(-1,1,1,seed=4)[0];
98 assert_approx(project_plane([0,0,1,offset]),move([0,0,-offset]) );
99 assert_approx(project_plane([0,1,0,offset]),xrot(90)*move([0,-offset,0]) );
100}
101test_project_plane();
102
103
104module test_lift_plane() {
105 assert(approx(lift_plane([[-10,0,-10], [0,0,0], [0,-10,-10]],[0,10*sqrt(2)/2]),[-5,0,-5]));
106 assert(approx(lift_plane([[-10,0,-10], [0,0,0], [0,-10,-10]],[6.12372, 10.6066]),[0,-5,-5],eps=1e-5));
107
108 assert_approx(lift_plane([[3,4,5],[1,3,9],[4,7,13]], [[0,0],[0,4.58257569496],[6.26783170528,5.89188303637]]),[[3,4,5],[1,3,9],[4,7,13]]);
109
110 assert_approx(project_plane([2,3,4,2]),[[0.920855800833,-0.11871629875,-0.371390676354,0],[-0.11871629875,0.821925551875,-0.557086014531,-2.77555756156e-17],[0.371390676354,0.557086014531,0.742781352708,-0.371390676354],[0,0,0,1]]);
111 assert_approx(project_plane([[1,1,1],[3,1,3],[1,1,4]]),[[-1/sqrt(2),1/sqrt(2),0,0],[0,0,1,-1],[1/sqrt(2),1/sqrt(2),0,-sqrt(2)],[0,0,0,1]]);
112
113 N=30;
114 data2 = list_to_matrix(rands(0,10,3*N,seed=77),3);
115 data3 = [for (d=data2) [d.x,d.y,d.x*3+d.y*5+2]];
116 planept = select(data3,0,N-4);
117 testpt = select(data3, N-3,-1);
118 newdata = project_plane(planept,testpt);
119 assert_approx( lift_plane(planept, newdata), testpt);
120 assert_approx( lift_plane(planept, project_plane(planept, last(testpt))), last(testpt));
121 assert_approx( lift_plane(planept) * project_plane(planept) , ident(4));
122 assert_approx( lift_plane([1,2,3,4]) * project_plane([1,2,3,4]) , ident(4));
123 assert_approx( lift_plane([[1,1,1],[3,1,3],[1,1,4]]) * project_plane([[1,1,1],[3,1,3],[1,1,4]]) , ident(4));
124
125}
126test_lift_plane();
127
128
129module test_cylindrical_to_xyz() {
130 assert(approx(cylindrical_to_xyz(100,90,10),[0,100,10]));
131 assert(approx(cylindrical_to_xyz(100,270,-10),[0,-100,-10]));
132 assert(approx(cylindrical_to_xyz(100,-90,-10),[0,-100,-10]));
133 assert(approx(cylindrical_to_xyz(100,180,0),[-100,0,0]));
134 assert(approx(cylindrical_to_xyz(100,0,0),[100,0,0]));
135 assert(approx(cylindrical_to_xyz(100,45,10),[100*sqrt(2)/2,100*sqrt(2)/2,10]));
136 assert(approx(cylindrical_to_xyz([100,90,10]),[0,100,10]));
137 assert(approx(cylindrical_to_xyz([100,270,-10]),[0,-100,-10]));
138 assert(approx(cylindrical_to_xyz([100,-90,-10]),[0,-100,-10]));
139 assert(approx(cylindrical_to_xyz([100,180,0]),[-100,0,0]));
140 assert(approx(cylindrical_to_xyz([100,0,0]),[100,0,0]));
141 assert(approx(cylindrical_to_xyz([100,45,10]),[100*sqrt(2)/2,100*sqrt(2)/2,10]));
142}
143test_cylindrical_to_xyz();
144
145
146module test_xyz_to_cylindrical() {
147 assert(approx(xyz_to_cylindrical(0,100,10),[100,90,10]));
148 assert(approx(xyz_to_cylindrical(0,-100,-10),[100,-90,-10]));
149 assert(approx(xyz_to_cylindrical(-100,0,0),[100,180,0]));
150 assert(approx(xyz_to_cylindrical(100,0,0),[100,0,0]));
151 assert(approx(xyz_to_cylindrical(100*sqrt(2)/2,100*sqrt(2)/2,10),[100,45,10]));
152 assert(approx(xyz_to_cylindrical([0,100,10]),[100,90,10]));
153 assert(approx(xyz_to_cylindrical([0,-100,-10]),[100,-90,-10]));
154 assert(approx(xyz_to_cylindrical([-100,0,0]),[100,180,0]));
155 assert(approx(xyz_to_cylindrical([100,0,0]),[100,0,0]));
156 assert(approx(xyz_to_cylindrical([100*sqrt(2)/2,100*sqrt(2)/2,10]),[100,45,10]));
157}
158test_xyz_to_cylindrical();
159
160
161module test_spherical_to_xyz() {
162 assert(approx(spherical_to_xyz(100,90,45),100*[0,sqrt(2)/2,sqrt(2)/2]));
163 assert(approx(spherical_to_xyz(100,270,45),100*[0,-sqrt(2)/2,sqrt(2)/2]));
164 assert(approx(spherical_to_xyz(100,-90,45),100*[0,-sqrt(2)/2,sqrt(2)/2]));
165 assert(approx(spherical_to_xyz(100,90,90),100*[0,1,0]));
166 assert(approx(spherical_to_xyz(100,-90,90),100*[0,-1,0]));
167 assert(approx(spherical_to_xyz(100,180,90),100*[-1,0,0]));
168 assert(approx(spherical_to_xyz(100,0,90),100*[1,0,0]));
169 assert(approx(spherical_to_xyz(100,0,0),100*[0,0,1]));
170 assert(approx(spherical_to_xyz(100,0,180),100*[0,0,-1]));
171 assert(approx(spherical_to_xyz([100,90,45]),100*[0,sqrt(2)/2,sqrt(2)/2]));
172 assert(approx(spherical_to_xyz([100,270,45]),100*[0,-sqrt(2)/2,sqrt(2)/2]));
173 assert(approx(spherical_to_xyz([100,-90,45]),100*[0,-sqrt(2)/2,sqrt(2)/2]));
174 assert(approx(spherical_to_xyz([100,90,90]),100*[0,1,0]));
175 assert(approx(spherical_to_xyz([100,-90,90]),100*[0,-1,0]));
176 assert(approx(spherical_to_xyz([100,180,90]),100*[-1,0,0]));
177 assert(approx(spherical_to_xyz([100,0,90]),100*[1,0,0]));
178 assert(approx(spherical_to_xyz([100,0,0]),100*[0,0,1]));
179 assert(approx(spherical_to_xyz([100,0,180]),100*[0,0,-1]));
180}
181test_spherical_to_xyz();
182
183
184module test_xyz_to_spherical() {
185 assert(approx(xyz_to_spherical(0, 100*sqrt(2)/2,100*sqrt(2)/2),[100, 90,45]));
186 assert(approx(xyz_to_spherical(0,-100*sqrt(2)/2,100*sqrt(2)/2),[100,-90,45]));
187 assert(approx(xyz_to_spherical( 0, 100, 0),[100, 90, 90]));
188 assert(approx(xyz_to_spherical( 0,-100, 0),[100,-90, 90]));
189 assert(approx(xyz_to_spherical(-100, 0, 0),[100,180, 90]));
190 assert(approx(xyz_to_spherical( 100, 0, 0),[100, 0, 90]));
191 assert(approx(xyz_to_spherical( 0, 0, 100),[100, 0, 0]));
192 assert(approx(xyz_to_spherical( 0, 0,-100),[100, 0,180]));
193 assert(approx(xyz_to_spherical([0, 100*sqrt(2)/2,100*sqrt(2)/2]),[100, 90,45]));
194 assert(approx(xyz_to_spherical([0,-100*sqrt(2)/2,100*sqrt(2)/2]),[100,-90,45]));
195 assert(approx(xyz_to_spherical([ 0, 100, 0]),[100, 90, 90]));
196 assert(approx(xyz_to_spherical([ 0,-100, 0]),[100,-90, 90]));
197 assert(approx(xyz_to_spherical([-100, 0, 0]),[100,180, 90]));
198 assert(approx(xyz_to_spherical([ 100, 0, 0]),[100, 0, 90]));
199 assert(approx(xyz_to_spherical([ 0, 0, 100]),[100, 0, 0]));
200 assert(approx(xyz_to_spherical([ 0, 0,-100]),[100, 0,180]));
201}
202test_xyz_to_spherical();
203
204
205module test_altaz_to_xyz() {
206 assert(approx(altaz_to_xyz( 0, 0,100),[ 0,100, 0]));
207 assert(approx(altaz_to_xyz( 90, 0,100),[ 0, 0, 100]));
208 assert(approx(altaz_to_xyz(-90, 0,100),[ 0, 0,-100]));
209 assert(approx(altaz_to_xyz( 0, 90,100),[ 100, 0, 0]));
210 assert(approx(altaz_to_xyz( 0,-90,100),[-100, 0, 0]));
211 assert(approx(altaz_to_xyz( 45, 90,100),[100*sqrt(2)/2,0,100*sqrt(2)/2]));
212 assert(approx(altaz_to_xyz(-45, 90,100),[100*sqrt(2)/2,0,-100*sqrt(2)/2]));
213 assert(approx(altaz_to_xyz([ 0, 0,100]),[ 0,100, 0]));
214 assert(approx(altaz_to_xyz([ 90, 0,100]),[ 0, 0, 100]));
215 assert(approx(altaz_to_xyz([-90, 0,100]),[ 0, 0,-100]));
216 assert(approx(altaz_to_xyz([ 0, 90,100]),[ 100, 0, 0]));
217 assert(approx(altaz_to_xyz([ 0,-90,100]),[-100, 0, 0]));
218 assert(approx(altaz_to_xyz([ 45, 90,100]),[100*sqrt(2)/2,0,100*sqrt(2)/2]));
219 assert(approx(altaz_to_xyz([-45, 90,100]),[100*sqrt(2)/2,0,-100*sqrt(2)/2]));
220}
221test_altaz_to_xyz();
222
223
224module test_xyz_to_altaz() {
225 assert(approx(xyz_to_altaz( 0,100, 0),[ 0, 0,100]));
226 assert(approx(xyz_to_altaz( 0, 0, 100),[ 90, 0,100]));
227 assert(approx(xyz_to_altaz( 0, 0,-100),[-90, 0,100]));
228 assert(approx(xyz_to_altaz( 100, 0, 0),[ 0, 90,100]));
229 assert(approx(xyz_to_altaz(-100, 0, 0),[ 0,-90,100]));
230 assert(approx(xyz_to_altaz(100*sqrt(2)/2,0,100*sqrt(2)/2),[ 45, 90,100]));
231 assert(approx(xyz_to_altaz(100*sqrt(2)/2,0,-100*sqrt(2)/2),[-45, 90,100]));
232 assert(approx(xyz_to_altaz([ 0,100, 0]),[ 0, 0,100]));
233 assert(approx(xyz_to_altaz([ 0, 0, 100]),[ 90, 0,100]));
234 assert(approx(xyz_to_altaz([ 0, 0,-100]),[-90, 0,100]));
235 assert(approx(xyz_to_altaz([ 100, 0, 0]),[ 0, 90,100]));
236 assert(approx(xyz_to_altaz([-100, 0, 0]),[ 0,-90,100]));
237 assert(approx(xyz_to_altaz([100*sqrt(2)/2,0,100*sqrt(2)/2]),[ 45, 90,100]));
238 assert(approx(xyz_to_altaz([100*sqrt(2)/2,0,-100*sqrt(2)/2]),[-45, 90,100]));
239}
240test_xyz_to_altaz();
241
242
243// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap