Skip to content

Commit

Permalink
Fix: coordinates, bulge and cors
Browse files Browse the repository at this point in the history
  • Loading branch information
MrEliasen committed Sep 19, 2024
1 parent 41d4ed9 commit c82faa3
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/cors v1.7.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQw=
github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
Expand Down Expand Up @@ -66,6 +68,7 @@ github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQ
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
Expand Down Expand Up @@ -133,6 +136,7 @@ google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHh
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
2 changes: 2 additions & 0 deletions src/pkg/api/internal/templates/index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Galaxy Generator - Generate galaxies with stellar neighbourhoods, stars and planets</title>
<meta name="description" content="Generate galaxies with stellar neighbourhoods, stars and planets. The goal is to be scientifically believable, while trying to remain as scientifically accurate as possible, without needing to do a lot of (any) simulation.">
<link rel="icon" type="image/x-icon" href="/favicon.ico">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.10.0/styles/hybrid.min.css" integrity="sha512-ZVy0y7AnokL/xGtaRaWRgLYjhywJZdJwVFWXW9oihOpDIochH8JF0xWFK+Y1WJ5wTn3rn9LPZRFjxObuzvQUaQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />

Expand Down Expand Up @@ -63,6 +64,7 @@ curl --location 'https://galaxy-generator.oogabooga.dev/api/galaxy?seed=17259869
"type": "Spiral", // only supported type
"thickness": 1880, // the thickness of the galaxy, excluding the bulge, in Light Years
"radii": 42156, // The size of the galaxy in Light Years
"bulge_radii": 5058, // The size of the galaxy bulge in Light Years
"seed": 1725986991, // The seed used to generate the galaxy
"stellar_neighbourhoods": [ // The Stellar Neighbourhoods (Stellar Neighbourhood) which have been generated.
{
Expand Down
9 changes: 9 additions & 0 deletions src/pkg/api/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"

g "github.com/CAFxX/httpcompression/contrib/gin-gonic/gin"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/mreliasen/ihniwiad/pkg/api/internal"
)
Expand All @@ -23,6 +24,14 @@ func Run() {
r := gin.Default()
r.Use(compressor)

r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET"},
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: false,
}))

if os.Getenv("GIN_MODE") == "release" {
tmpl := template.Must(template.New("").ParseFS(templateFS, "internal/templates/*"))
r.SetHTMLTemplate(tmpl)
Expand Down
6 changes: 5 additions & 1 deletion src/pkg/galaxy/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ func New(rng *rand.Rand, seed int64) interfaces.GalaxyInterface {
g := internal.Galaxy{
Type: "Spiral",
}
g.SetRadius(utils.RoundFloat(rng.Float64()*(radiiMax-radiiMin)+radiiMin, 0))

radii := rng.Float64()*(radiiMax-radiiMin) + radiiMin

g.SetRadius(utils.RoundFloat(radii, 0))
g.SetThickness(utils.RoundFloat(rng.Float64()*(thicknessMax-thicknessMin)+thicknessMin, 0))
g.SetBulgeRadius(utils.RoundFloat(radii*(rng.Float64()*(0.15-0.08)+0.08), 0))
g.SetRNG(rng)
g.SetSeed(seed)
return &g
Expand Down
17 changes: 14 additions & 3 deletions src/pkg/galaxy/internal/galaxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Galaxy struct {
Type string `json:"type"`
Thickness float64 `json:"thickness"`
Radius float64 `json:"radii"`
BulgeRadius float64 `json:"bulge_radii"`
Seed int64 `json:"seed"`
Neighbourhoods []interfaces.StellarNeighbourhoodInterface `json:"stellar_neighbourhoods"`
}
Expand All @@ -37,6 +38,16 @@ func (g *Galaxy) SetRNG(rng *rand.Rand) interfaces.GalaxyInterface {
return g
}

/*
Sets size of the galaxy bulge, by its radius.
The radius is in LY
*/
func (g *Galaxy) SetBulgeRadius(r float64) interfaces.GalaxyInterface {
g.BulgeRadius = r
return g
}

/*
Sets size of the galaxy, by its radius.
Expand All @@ -63,8 +74,8 @@ This is a contested subject, let it serve as no more than a guide.
Returns the habitable zone by its inner and outer distance to the galactic centre.
*/
func (g *Galaxy) HabitableZone() (inner float64, outer float64) {
inner = utils.RoundFloat(g.Radius*0.47, 0) // ly
outer = utils.RoundFloat(g.Radius*0.6, 0) // ly
inner = utils.RoundFloat(g.Radius*0.18, 0) // ly
outer = utils.RoundFloat(g.Radius*0.66, 0) // ly
return inner, outer
}

Expand All @@ -80,7 +91,7 @@ func (g *Galaxy) GenerateStellarNeighbourhood(seed int64) interfaces.StellarNeig
rad := utils.RoundFloat(float64(nhRng.Intn(30-15)+15), 0) // 15 LY min, up to 30 LY
sDensity := utils.RoundFloat(nhRng.Float64()*((0.006-0.003)/10)+0.003, 5) // 0.003 min density, up to 0.006

location := utils.RandomCartesianCoord(nhRng, dist)
location := utils.RandomCartesianCoord(nhRng, dist, g.BulgeRadius+60, hzOuter)
location.SetZ(nhRng.Float64()*(g.Thickness-rad) + rad)

neighbourhood := &StellarNeighbourhood{
Expand Down
13 changes: 10 additions & 3 deletions src/pkg/utils/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ func RoundFloat(val float64, precision uint) float64 {
return math.Round(val*ratio) / ratio
}

func RandomCartesianCoord(rng *rand.Rand, dist float64) interfaces.CoordinateInterface {
func RandomCartesianCoord(rng *rand.Rand, dist float64, minDist, maxDist float64) interfaces.CoordinateInterface {
// Generate a random angle between 0 and 2π
theta := rng.Float64() * 2 * math.Pi
x := dist * math.Cos(theta)
y := dist * math.Sin(theta)

// Generate a random radius with uniform distribution within the annulus
u := rng.Float64()
r := math.Sqrt(u*(maxDist*maxDist-minDist*minDist) + minDist*minDist)

// Convert polar coordinates to Cartesian coordinates
x := r * math.Cos(theta)
y := r * math.Sin(theta)

return coordinate.New(x, y)
}
Expand Down

0 comments on commit c82faa3

Please sign in to comment.