1 /** 2 Copyright: Copyright (c) 2016-2018 Andrey Penechko. 3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 module voxelman.geometry.rotation; 7 8 // rotates around Y axis clocwise or counter clockwise 9 // rotType is 0 for 0, 1 for 90, 2 for 180, 3 for 270 degrees 10 T rotatePointCCW(T)(T point, ubyte rotType) 11 { 12 if (rotType == 1) return rotateCCW90(point); 13 else if (rotType == 2) return rotateCCW180(point); 14 else if (rotType == 3) return rotateCCW270(point); 15 return point; 16 } 17 18 // ditto 19 T rotatePointCW(T)(T point, ubyte rotType) 20 { 21 if (rotType == 1) return rotateCCW270(point); 22 else if (rotType == 2) return rotateCCW180(point); 23 else if (rotType == 3) return rotateCCW90(point); 24 return point; 25 } 26 27 // ditto 28 T rotatePointShiftOriginCW(T)(T point, T size, ubyte rotType) 29 { 30 if (rotType == 1) return rotateCCW270ShiftOrigin(point, size); 31 else if (rotType == 2) return rotateCCW180ShiftOrigin(point, size); 32 else if (rotType == 3) return rotateCCW90ShiftOrigin(point, size); 33 return point; 34 } 35 36 T function(T) getCCWRotationFunction(T)(ubyte rotType) 37 { 38 if (rotType == 1) return &rotateCCW90!T; 39 else if (rotType == 2) return &rotateCCW180!T; 40 else if (rotType == 3) return &rotateCCW270!T; 41 return &rotateCCW0!T; 42 } 43 44 T function(T, T) getCCWRotationShiftOriginFunction(T)(ubyte rotType) 45 { 46 if (rotType == 1) return &rotateCCW90ShiftOrigin!T; 47 else if (rotType == 2) return &rotateCCW180ShiftOrigin!T; 48 else if (rotType == 3) return &rotateCCW270ShiftOrigin!T; 49 return &rotateCCW0ShiftOrigin!T; 50 } 51 52 T rotateCCW0(T)(T point) 53 { 54 return point; 55 } 56 57 T rotateCCW90(T)(T point) 58 { 59 T res = point; 60 res.x = point.z; 61 res.z = -point.x; 62 return res; 63 } 64 65 T rotateCCW180(T)(T point) 66 { 67 T res = point; 68 res.x = -point.x; 69 res.z = -point.z; 70 return res; 71 } 72 73 T rotateCCW270(T)(T point) 74 { 75 T res = point; 76 res.x = -point.z; 77 res.z = point.x; 78 return res; 79 } 80 81 T rotateCCW90ShiftOrigin(T)(T point, T size) 82 { 83 T res = point; 84 res.x = point.z; 85 res.z = -point.x + size.x; 86 return res; 87 } 88 89 T rotateCCW180ShiftOrigin(T)(T point, T size) 90 { 91 T res = point; 92 res.x = -point.x + size.x; 93 res.z = -point.z + size.z; 94 return res; 95 } 96 97 T rotateCCW270ShiftOrigin(T)(T point, T size) 98 { 99 T res = point; 100 res.x = -point.z + size.z; 101 res.z = point.x; 102 return res; 103 } 104 105 T rotateCCW0ShiftOrigin(T)(T point, T size) 106 { 107 return point; 108 }