1 /**
2 Copyright: Copyright (c) 2017-2018 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 module voxelman.algorithm.arraycopy2d;
7 
8 import voxelman.math;
9 
10 /// writes source subrect to a dest subrect
11 void setSubArray2d(T)(
12 	in T[] source, in ivec2 sourceSize, in irect sourceSubRect,
13 	T[] dest, in ivec2 destSize, in ivec2 destSubRectPos) @nogc
14 {
15 	if (sourceSize.x == sourceSubRect.width && sourceSize.x == destSize.x)
16 	{
17 		assert(sourceSubRect.x == 0);
18 		assert(destSubRectPos.x == 0);
19 
20 		auto rectArea = sourceSubRect.width * sourceSubRect.height;
21 
22 		auto fromSource = sourceSubRect.y * sourceSize.x;
23 		auto toSource = fromSource + rectArea;
24 
25 		auto fromDest = destSubRectPos.y * destSize.x;
26 		auto toDest = fromDest + rectArea;
27 
28 		dest[fromDest..toDest] = source[fromSource..toSource];
29 	}
30 	else
31 	{
32 		foreach(y; 0..sourceSubRect.height)
33 		{
34 			auto fromSource = (sourceSubRect.y + y) * sourceSize.x + sourceSubRect.x;
35 			auto toSource = fromSource + sourceSubRect.width;
36 
37 			auto fromDest = (destSubRectPos.y + y) * destSize.x + destSubRectPos.x;
38 			auto toDest = fromDest + sourceSubRect.width;
39 
40 			dest[fromDest..toDest] = source[fromSource..toSource];
41 		}
42 	}
43 }
44 
45 // fill dest sub rect with item
46 void setSubArray2d(T)(T item, T[] dest, ivec2 destSize, irect destSubRect) @nogc
47 {
48 	if (destSize.x == destSubRect.width)
49 	{
50 		assert(destSubRect.x == 0);
51 
52 		auto fromDest = destSubRect.y * destSize.x;
53 		auto toDest = fromDest + destSubRect.area;
54 
55 		dest[fromDest..toDest] = item;
56 	}
57 	else
58 	{
59 		foreach(y; 0..destSubRect.height)
60 		{
61 			auto fromDest = (destSubRect.y + y) * destSize.x + destSubRect.x;
62 			auto toDest = fromDest + destSubRect.width;
63 
64 			dest[fromDest..toDest] = item;
65 		}
66 	}
67 }