Skip to content

Commit 49caea1

Browse files
committed
Add geometry classes
1 parent dd5a409 commit 49caea1

File tree

7 files changed

+454
-14
lines changed

7 files changed

+454
-14
lines changed

generateProto.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ protoc -I"./proto" -I"$GOPATH/src" --go_out="$GOPATH/src" proto/ssl_game_event.p
1919
# referee message
2020
protoc -I"./proto" -I"$GOPATH/src" --go_out="$GOPATH/src" proto/ssl_referee.proto
2121

22+
# geometry
23+
protoc -I"./proto" -I"$GOPATH/src" --go_out="$GOPATH/src" proto/ssl_gc_geometry.proto
24+
2225
# remote communication
2326
protoc -I"./proto" -I"$GOPATH/src" --go_out="$GOPATH/src" proto/ssl_gc_rcon.proto
2427
protoc -I"./proto" -I"$GOPATH/src" --go_out="$GOPATH/src" proto/ssl_gc_rcon_autoref.proto

internal/app/geom/defensearea.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package geom
2+
3+
import (
4+
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/config"
5+
)
6+
7+
func NewDefenseArea(geometry config.Geometry, onPositiveHalf bool) *Rectangle {
8+
if onPositiveHalf {
9+
return NewDefenseAreaBySign(geometry, 1)
10+
}
11+
return NewDefenseAreaBySign(geometry, -1)
12+
}
13+
14+
func NewDefenseAreaBySign(geometry config.Geometry, sign float64) *Rectangle {
15+
center := NewVector2(sign*(geometry.FieldLength/2.0-geometry.DefenseAreaDepth/2.0), 0)
16+
return NewRectangleFromCenter(center, geometry.DefenseAreaDepth, geometry.DefenseAreaWidth)
17+
}

internal/app/geom/geometry.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package geom
2+
3+
import "math"
4+
5+
func NewVector2(x, y float64) *Vector2 {
6+
return &Vector2{X: &x, Y: &y}
7+
}
8+
9+
func (m *Vector2) Length() float64 {
10+
x := *m.X
11+
y := *m.Y
12+
return math.Sqrt(x*x + y*y)
13+
}
14+
15+
func (m *Vector2) Sub(o *Vector2) *Vector2 {
16+
return NewVector2(*m.X-*o.X, *m.Y-*o.Y)
17+
}
18+
19+
func (m *Vector2) DistanceTo(o *Vector2) float64 {
20+
return m.Sub(o).Length()
21+
}

internal/app/geom/rectangle.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package geom
2+
3+
import "math"
4+
5+
type Rectangle struct {
6+
center *Vector2
7+
xExtent float64
8+
yExtent float64
9+
}
10+
11+
func NewRectangleFromCenter(center *Vector2, xExtent, yExtent float64) (r *Rectangle) {
12+
return &Rectangle{
13+
center: center,
14+
xExtent: xExtent,
15+
yExtent: yExtent,
16+
}
17+
}
18+
19+
func NewRectangleFromPoints(p1, p2 *Vector2) (r *Rectangle) {
20+
r = new(Rectangle)
21+
r.xExtent = math.Abs(*p1.X - *p2.X)
22+
r.yExtent = math.Abs(*p1.Y - *p2.Y)
23+
r.center = NewVector2(
24+
math.Min(*p1.X, *p2.X)+r.xExtent/2.0,
25+
math.Min(*p1.Y, *p2.Y)+r.yExtent/2.0,
26+
)
27+
return
28+
}
29+
30+
func (r *Rectangle) WithMargin(margin float64) *Rectangle {
31+
return &Rectangle{
32+
center: r.center,
33+
xExtent: math.Max(0, r.xExtent+2*margin),
34+
yExtent: math.Max(0, r.yExtent+2*margin),
35+
}
36+
}
37+
38+
func (r *Rectangle) MaxX() float64 {
39+
return *r.center.X + r.xExtent/2.0
40+
}
41+
42+
func (r *Rectangle) MinX() float64 {
43+
return *r.center.X - r.xExtent/2.0
44+
}
45+
46+
func (r *Rectangle) MaxY() float64 {
47+
return *r.center.Y + r.yExtent/2.0
48+
}
49+
50+
func (r *Rectangle) MinY() float64 {
51+
return *r.center.Y + r.yExtent/2.0
52+
}
53+
54+
func (r *Rectangle) IsPointInside(p *Vector2) bool {
55+
return isBetween(*p.X, r.MinX(), r.MaxX()) &&
56+
isBetween(*p.Y, r.MinY(), r.MaxY())
57+
}
58+
59+
func isBetween(x, min, max float64) bool {
60+
if max > min {
61+
return (x >= min) && (x <= max)
62+
}
63+
return (x >= max) && (x <= min)
64+
}

internal/app/geom/ssl_gc_geometry.pb.go

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/ssl_gc_geometry.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
syntax = "proto2";
2+
3+
option go_package = "github.com/RoboCup-SSL/ssl-game-controller/internal/app/geom";
4+
5+
message Vector2 {
6+
required double x = 1;
7+
required double y = 2;
8+
}

0 commit comments

Comments
 (0)