kml representation of O-DU O-Cloud resource pool 53/11953/1
authorMartin Skorupski <martin.skorupski@highstreet-technologies.com>
Sun, 22 Oct 2023 10:13:01 +0000 (12:13 +0200)
committerMartin Skorupski <martin.skorupski@highstreet-technologies.com>
Sun, 22 Oct 2023 10:13:06 +0000 (12:13 +0200)
- creating a Cube Class for related Heagon-Grid functions

Issue-ID: OAM-371
Change-Id: I92226e37a4279437a21d4f95ebaf91137a166f15
Signed-off-by: Martin Skorupski <martin.skorupski@highstreet-technologies.com>
code/network-generator/model/python/cube.py [new file with mode: 0644]

diff --git a/code/network-generator/model/python/cube.py b/code/network-generator/model/python/cube.py
new file mode 100644 (file)
index 0000000..f709bd7
--- /dev/null
@@ -0,0 +1,73 @@
+# Copyright 2023 highstreet technologies GmbH
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# inspired by http://www.redblobgames.com/grids/hexagons/
+
+#!/usr/bin/python
+from model.python.hexagon import Hex
+
+
+class Cube:
+    @staticmethod
+    def direction_vectors() -> list[Hex]:
+        q, r, s = 1, 0, -1
+        return [
+            Hex(q, r, s),
+            Hex(-s, -q, -r),
+            Hex(r, s, q),
+            Hex(-q, -r, -s),
+            Hex(s, q, r),
+            Hex(-r, -s, -q),
+        ]
+
+    @staticmethod
+    def direction(direction: int) -> Hex:
+        if direction < 0 or direction > 5:
+            raise ValueError(
+                "Invalid direction. The direction value must be in the range of [0..5]."
+            )
+        return Cube.direction_vectors()[direction]
+
+    @staticmethod
+    def add(hex: Hex, vec: Hex) -> Hex:
+        return Hex(hex.q + vec.q, hex.r + vec.r, hex.s + vec.s)
+
+    @staticmethod
+    def neighbor(cube: Hex, direction: int) -> Hex:
+        return Cube.add(cube, Cube.direction(direction))
+
+    @staticmethod
+    def scale(hex: Hex, factor: int) -> Hex:
+        return Hex(hex.q * factor, hex.r * factor, hex.s * factor)
+
+    @staticmethod
+    def ring(center: Hex, radius: int) -> list[Hex]:
+        if not (radius > 0):
+            raise ValueError(
+                "Invalid radius. The radius around the hex center must be greater than 0 rings."
+            )
+        results: list[Hex] = []
+        hex: Hex = Cube.add(center, Cube.scale(Cube.direction(4), radius))
+        for i in range(6):
+            for j in range(radius):
+                results.append(hex)
+                hex = Cube.neighbor(hex, i)
+        return results
+
+    @staticmethod
+    def spiral(center: Hex, radius: int) -> list[Hex]:
+        result: list[Hex] = [center]
+        for k in range(1, radius + 1):
+            result.extend(Cube.ring(center, k))
+        return result