github.com/metaworking/channeld@v0.7.3/pkg/common/common.go (about) 1 package common 2 3 import ( 4 "fmt" 5 "math" 6 7 "google.golang.org/protobuf/proto" 8 ) 9 10 // Each channel uses a goroutine and we can have at most millions of goroutines at the same time. 11 // So we won't use 64-bit channel ID unless we use a distributed architecture for channeld itself. 12 type ChannelId uint32 13 14 type Message = proto.Message //protoreflect.ProtoMessage 15 16 type ChannelDataMessage = proto.Message //protoreflect.Message 17 18 // channeldpb.SpatialInfo is heavy with mutex lock and other allocations. 19 // We need a light struct for frequent value copy. 20 type SpatialInfo struct { 21 X float64 22 Y float64 23 Z float64 24 } 25 26 // Notifies the spatial/entity channel from the ChannelDataUpdate that the spatial info of an entity has changed, 27 // so the channel's SpatialController will check the handover. If the handover happpens, `handoverDataProvider` 28 // will be called to provide the data for the ChannelDataHandoverMessage. 29 type SpatialInfoChangedNotifier interface { 30 // The handover data provider has three parameters: 31 // - srcChannelId: the channel that an object is handed over from. 32 // - dstChannelId: the channel that an object is handed over to. 33 // - handoverData: 34 // if use group-based handover, the data should be the id of the migrating entity; 35 // if use the query-context handover, the data should be the message wrapped in ChannelDataHandoverMessage; 36 // if nil, no handover will happend. 37 Notify(oldInfo SpatialInfo, newInfo SpatialInfo, handoverDataProvider func(ChannelId, ChannelId, interface{})) 38 } 39 40 func (s *SpatialInfo) String() string { 41 return fmt.Sprintf("(%.4f, %.4f, %.4f)", s.X, s.Y, s.Z) 42 } 43 44 func (info1 *SpatialInfo) Dist2D(info2 *SpatialInfo) float64 { 45 return math.Sqrt((info1.X-info2.X)*(info1.X-info2.X) + (info1.Z-info2.Z)*(info1.Z-info2.Z)) 46 } 47 48 func (info1 *SpatialInfo) Dot2D(info2 *SpatialInfo) float64 { 49 return info1.X*info2.X + info1.Z*info2.Z 50 } 51 52 func (info1 *SpatialInfo) Magnitude2D() float64 { 53 return math.Sqrt(info1.X*info1.X + info1.Z*info1.Z) 54 } 55 56 func (info *SpatialInfo) Normalize2D() { 57 mag := info.Magnitude2D() 58 info.X /= mag 59 info.Z /= mag 60 } 61 62 func (info *SpatialInfo) Unit2D() SpatialInfo { 63 mag := info.Magnitude2D() 64 return SpatialInfo{X: info.X / mag, Y: info.Y, Z: info.Z / mag} 65 }