1 module mem_util;
2 
3 import vector_2d;
4 import vector_2i;
5 import vector_3d;
6 import vector_3i;
7 import vector_4d;
8 import vector_4i;
9 
10 import matrix_2d;
11 import matrix_3d;
12 import matrix_3x2d;
13 import matrix_4d;
14 import matrix_4x3d;
15 
16 
17 
18 
19 
20 /*
21 * The MIT License
22 *
23 * Copyright (c) 2016-2021 Kai Burjack
24 %$%% Translated by jordan4ibanez
25 *
26 * Permission is hereby granted, free of charge, to any person obtaining a copy
27 * of this software and associated documentation files (the "Software"), to deal
28 * in the Software without restriction, including without limitation the rights
29 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30 * copies of the Software, and to permit persons to whom the Software is
31 * furnished to do so, subject to the following conditions:
32 *
33 * The above copyright notice and this permission notice shall be included in
34 * all copies or substantial portions of the Software.
35 *
36 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
42 * THE SOFTWARE.
43 */
44 
45 /**
46 * Helper class to do efficient memory operations on all DOML objects, NIO buffers and primitive arrays.
47 * This class is used internally throughout DOML, is undocumented and is subject to change.
48 * Use with extreme caution!
49 * 
50 * @author The LWJGL authors
51 * @author Kai Burjack
52 */
53 
54 
55 
56 public double get(Matrix4d m, int column, int row) {
57     switch (column) {
58     case 0:
59         switch (row) {
60         case 0:
61             return m.m00;
62         case 1:
63             return m.m01;
64         case 2:
65             return m.m02;
66         case 3:
67             return m.m03;
68         default:
69             break;
70         }
71         break;
72     case 1:
73         switch (row) {
74         case 0:
75             return m.m10;
76         case 1:
77             return m.m11;
78         case 2:
79             return m.m12;
80         case 3:
81             return m.m13;
82         default:
83             break;
84         }
85         break;
86     case 2:
87         switch (row) {
88         case 0:
89             return m.m20;
90         case 1:
91             return m.m21;
92         case 2:
93             return m.m22;
94         case 3:
95             return m.m23;
96         default:
97             break;
98         }
99         break;
100     case 3:
101         switch (row) {
102         case 0:
103             return m.m30;
104         case 1:
105             return m.m31;
106         case 2:
107             return m.m32;
108         case 3:
109             return m.m33;
110         default:
111             break;
112         }
113         break;
114     default:
115         break;
116     }
117 
118     return 0;
119 }
120 
121 public Matrix4d set(ref Matrix4d m, int column, int row, double value) {
122     switch (column) {
123     case 0:
124         switch (row) {
125         case 0:
126             m.m00 = value;
127             break;
128         case 1:
129             m.m01 = value;
130             break;
131         case 2:
132             m.m02 = value;
133             break;
134         case 3:
135             m.m03 = value;
136             break;
137         default:
138             break;
139         }
140         break;
141     case 1:
142         switch (row) {
143         case 0:
144             m.m10 = value;
145             break;
146         case 1:
147             m.m11 = value;
148             break;
149         case 2:
150             m.m12 = value;
151             break;
152         case 3:
153             m.m13 = value;
154             break;
155         default:
156             break;
157         }
158         break;
159     case 2:
160         switch (row) {
161         case 0:
162             m.m20 = value;
163             break;
164         case 1:
165             m.m21 = value;
166             break;
167         case 2:
168             m.m22 = value;
169             break;
170         case 3:
171             m.m23 = value;
172             break;
173         default:
174             break;
175         }
176         break;
177     case 3:
178         switch (row) {
179         case 0:
180             m.m30 = value;
181             break;
182         case 1:
183             m.m31 = value;
184             break;
185         case 2:
186             m.m32 = value;
187             break;
188         case 3:
189             m.m33 = value;
190             break;
191         default:
192             break;
193         }
194         break;
195     default:
196         break;
197     }
198     
199     return m;
200 }
201 
202 
203 public double get(Matrix3d m, int column, int row) {
204     switch (column) {
205     case 0:
206         switch (row) {
207         case 0:
208             return m.m00;
209         case 1:
210             return m.m01;
211         case 2:
212             return m.m02;
213         default:
214             break;
215         }
216         break;
217     case 1:
218         switch (row) {
219         case 0:
220             return m.m10;
221         case 1:
222             return m.m11;
223         case 2:
224             return m.m12;
225         default:
226             break;
227         }
228         break;
229     case 2:
230         switch (row) {
231         case 0:
232             return m.m20;
233         case 1:
234             return m.m21;
235         case 2:
236             return m.m22;
237         default:
238             break;
239         }
240         break;
241     default:
242         break;
243     }
244 
245     return 0;
246 }
247 
248 public Matrix3d set(ref Matrix3d m, int column, int row, double value) {
249     switch (column) {
250     case 0:
251         switch (row) {
252         case 0:
253             m.m00 = value;
254             break;
255         case 1:
256             m.m01 = value;
257             break;
258         case 2:
259             m.m02 = value;
260             break;
261         default:
262             break;
263         }
264         break;
265     case 1:
266         switch (row) {
267         case 0:
268             m.m10 = value;
269             break;
270         case 1:
271             m.m11 = value;
272             break;
273         case 2:
274             m.m12 = value;
275             break;
276         default:
277             break;
278         }
279         break;
280     case 2:
281         switch (row) {
282         case 0:
283             m.m20 = value;
284             break;
285         case 1:
286             m.m21 = value;
287             break;
288         case 2:
289             m.m22 = value;
290             break;
291         default:
292             break;
293         }
294         break;
295     default:
296         break;
297     }
298     
299     return m;
300 }
301 
302 
303 public void copy(Matrix3x2d src, ref Matrix3x2d dest) {
304     dest._m00 = src.m00;
305     dest._m01 = src.m01;
306     dest._m10 = src.m10;
307     dest._m11 = src.m11;
308     dest._m20 = src.m20;
309     dest._m21 = src.m21;
310 }
311 
312 public void copy(Matrix2d src, ref Matrix2d dest) {
313     dest._m00 = src.m00;
314     dest._m01 = src.m01;
315     dest._m10 = src.m10;
316     dest._m11 = src.m11;
317 }
318 
319 public void copy(Matrix2d src, ref Matrix3d dest) {
320     dest._m00 = src.m00;
321     dest._m01 = src.m01;
322     dest._m02 = 0.0;
323     dest._m10 = src.m10;
324     dest._m11 = src.m11;
325     dest._m12 = 0.0;
326     dest._m20 = 0.0;
327     dest._m21 = 0.0;
328     dest._m22 = 1.0;
329 }
330 
331 public void copy(Matrix3d src, ref Matrix2d dest) {
332     dest._m00 = src.m00;
333     dest._m01 = src.m01;
334     dest._m10 = src.m10;
335     dest._m11 = src.m11;
336 }
337 
338 public void copy(Matrix2d src, ref Matrix3x2d dest) {
339     dest._m00 = src.m00;
340     dest._m01 = src.m01;
341     dest._m10 = src.m10;
342     dest._m11 = src.m11;
343     dest._m20 = 0.0;
344     dest._m21 = 0.0;
345 }
346 
347 public void copy(Matrix3x2d src, ref Matrix2d dest) {
348     dest._m00 = src.m00;
349     dest._m01 = src.m01;
350     dest._m10 = src.m10;
351     dest._m11 = src.m11;
352 }
353 
354 public void copy(double[] arr, int off, ref Matrix3x2d dest) {
355     dest._m00 = arr[off+0];
356     dest._m01 = arr[off+1];
357     dest._m10 = arr[off+2];
358     dest._m11 = arr[off+3];
359     dest._m20 = arr[off+4];
360     dest._m21 = arr[off+5];
361 }
362 
363 public void copy(double[] arr, int off, ref Matrix2d dest) {
364     dest._m00 = arr[off+0];
365     dest._m01 = arr[off+1];
366     dest._m10 = arr[off+2];
367     dest._m11 = arr[off+3];
368 }
369 
370 public void copy(Matrix3x2d src, ref double[] dest, int off) {
371     dest[off+0] = src.m00;
372     dest[off+1] = src.m01;
373     dest[off+2] = src.m10;
374     dest[off+3] = src.m11;
375     dest[off+4] = src.m20;
376     dest[off+5] = src.m21;
377 }
378 
379 public void copy(Matrix2d src, ref double[] dest, int off) {
380     dest[off+0] = src.m00;
381     dest[off+1] = src.m01;
382     dest[off+2] = src.m10;
383     dest[off+3] = src.m11;
384 }
385 
386 public void copy4x4(Matrix4x3d src, ref double[] dest, int off) {
387     dest[off+0]  = src.m00;
388     dest[off+1]  = src.m01;
389     dest[off+2]  = src.m02;
390     dest[off+3]  = 0.0;
391     dest[off+4]  = src.m10;
392     dest[off+5]  = src.m11;
393     dest[off+6]  = src.m12;
394     dest[off+7]  = 0.0;
395     dest[off+8]  = src.m20;
396     dest[off+9]  = src.m21;
397     dest[off+10] = src.m22;
398     dest[off+11] = 0.0;
399     dest[off+12] = src.m30;
400     dest[off+13] = src.m31;
401     dest[off+14] = src.m32;
402     dest[off+15] = 1.0;
403 }
404 
405 public void copy3x3(Matrix3x2d src, ref double[] dest, int off) {
406     dest[off+0] = src.m00;
407     dest[off+1] = src.m01;
408     dest[off+2] = 0.0;
409     dest[off+3] = src.m10;
410     dest[off+4] = src.m11;
411     dest[off+5] = 0.0;
412     dest[off+6] = src.m20;
413     dest[off+7] = src.m21;
414     dest[off+8] = 1.0;
415 }
416 
417 public void copy4x4(Matrix3x2d src, ref double[] dest, int off) {
418     dest[off+0]  = src.m00;
419     dest[off+1]  = src.m01;
420     dest[off+2]  = 0.0;
421     dest[off+3]  = 0.0;
422     dest[off+4]  = src.m10;
423     dest[off+5]  = src.m11;
424     dest[off+6]  = 0.0;
425     dest[off+7]  = 0.0;
426     dest[off+8]  = 0.0;
427     dest[off+9]  = 0.0;
428     dest[off+10] = 1.0;
429     dest[off+11] = 0.0;
430     dest[off+12] = src.m20;
431     dest[off+13] = src.m21;
432     dest[off+14] = 0.0;
433     dest[off+15] = 1.0;
434 }
435 
436 public void identity(ref Matrix3x2d dest) {
437     dest._m00 = 1.0;
438     dest._m01 = 0.0;
439     dest._m10 = 0.0;
440     dest._m11 = 1.0;
441     dest._m20 = 0.0;
442     dest._m21 = 0.0;
443 }
444 
445 public void swap(ref Matrix2d m1, ref Matrix2d m2) {
446     double tmp;
447 
448     tmp = m1.m00;
449     m1._m00 = m2.m00;
450     m2._m00 = tmp;
451 
452     tmp = m1.m01;
453     m1._m00 = m2.m01;
454     m2._m01 = tmp;
455 
456     tmp = m1.m10;
457     m1._m00 = m2.m10;
458     m2._m10 = tmp;
459 
460     tmp = m1.m11;
461     m1._m00 = m2.m11;
462     m2._m11 = tmp;
463 }
464 
465 public void zero(ref Matrix3x2d dest) {
466     dest._m00 = 0.0;
467     dest._m01 = 0.0;
468     dest._m10 = 0.0;
469     dest._m11 = 0.0;
470     dest._m20 = 0.0;
471     dest._m21 = 0.0;
472 }
473 
474 public void zero(ref Matrix2d dest) {
475     dest._m00 = 0.0;
476     dest._m01 = 0.0;
477     dest._m10 = 0.0;
478     dest._m11 = 0.0;
479 }