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