X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=code%2Fnetwork-generator%2Fnetwork_generation%2Fmodel%2Fpython%2Fcube.py;fp=code%2Fnetwork-generator%2Fnetwork_generation%2Fmodel%2Fpython%2Fcube.py;h=0d4e2a348b64d8f14102345843af02a872e00fe0;hb=7004840bca352043aec43d36df79436b90bcbd5a;hp=0000000000000000000000000000000000000000;hpb=c8e3bd35883f540afe023fd077ad64cde327e18a;p=oam.git diff --git a/code/network-generator/network_generation/model/python/cube.py b/code/network-generator/network_generation/model/python/cube.py new file mode 100644 index 0000000..0d4e2a3 --- /dev/null +++ b/code/network-generator/network_generation/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 network_generation.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