From 4b30eaf5405adb661396c9d378b2cbdc11f06e03 Mon Sep 17 00:00:00 2001 From: Martin Skorupski Date: Sun, 22 Oct 2023 12:13:01 +0200 Subject: [PATCH] kml representation of O-DU O-Cloud resource pool - creating a Cube Class for related Heagon-Grid functions Issue-ID: OAM-371 Change-Id: I92226e37a4279437a21d4f95ebaf91137a166f15 Signed-off-by: Martin Skorupski --- code/network-generator/model/python/cube.py | 73 +++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 code/network-generator/model/python/cube.py diff --git a/code/network-generator/model/python/cube.py b/code/network-generator/model/python/cube.py new file mode 100644 index 0000000..f709bd7 --- /dev/null +++ b/code/network-generator/model/python/cube.py @@ -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 -- 2.16.6