|
| 1 | +package blob |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + |
| 6 | + uuid "github.com/satori/go.uuid" |
| 7 | +) |
| 8 | + |
| 9 | +// Blobies - Array of blobs |
| 10 | +type Blobies struct { |
| 11 | + Objects map[uuid.UUID]Blobie |
| 12 | + maxNoMatch int |
| 13 | + minThresholdDistance float64 |
| 14 | + maxPointsInTrack int |
| 15 | + |
| 16 | + DrawingOptions *DrawOptions |
| 17 | +} |
| 18 | + |
| 19 | +// NewBlobiesDefaults - Constructor for Blobies (default values) |
| 20 | +// |
| 21 | +// Default values are: |
| 22 | +// maxNoMatch = 5 |
| 23 | +// minThresholdDistance = 15 |
| 24 | +// maxPointsInTrack = 10 |
| 25 | +// |
| 26 | +func NewBlobiesDefaults() *Blobies { |
| 27 | + return &Blobies{ |
| 28 | + Objects: make(map[uuid.UUID]Blobie), |
| 29 | + maxNoMatch: 5, |
| 30 | + minThresholdDistance: 15, |
| 31 | + maxPointsInTrack: 10, |
| 32 | + DrawingOptions: NewDrawOptionsDefault(), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// MatchToExisting Check if some of blobs already exists |
| 37 | +func (bt *Blobies) MatchToExisting(blobies []Blobie) { |
| 38 | + bt.prepare() |
| 39 | + for i := range blobies { |
| 40 | + minUUID := uuid.UUID{} |
| 41 | + minDistance := math.MaxFloat64 |
| 42 | + for j := range (*bt).Objects { |
| 43 | + dist := distanceBetweenPoints(blobies[i].GetCenter(), (*bt).Objects[j].GetCenter()) |
| 44 | + distPredicted := distanceBetweenPoints(blobies[i].GetCenter(), (*bt).Objects[j].GetPredictedNextPosition()) |
| 45 | + dist = minf64(dist, distPredicted) |
| 46 | + if dist < minDistance { |
| 47 | + minDistance = dist |
| 48 | + minUUID = j |
| 49 | + } |
| 50 | + } |
| 51 | + if minDistance < blobies[i].GetDiagonal()*0.5 || minDistance < bt.minThresholdDistance { |
| 52 | + bt.Objects[minUUID].Update(blobies[i]) |
| 53 | + } else { |
| 54 | + bt.Register(blobies[i]) |
| 55 | + } |
| 56 | + } |
| 57 | + bt.RefreshNoMatch() |
| 58 | +} |
| 59 | + |
| 60 | +// RefreshNoMatch - Refresh state of each blob |
| 61 | +func (bt *Blobies) RefreshNoMatch() { |
| 62 | + for i, b := range (*bt).Objects { |
| 63 | + if b.Exists() == false { |
| 64 | + b.IncrementNoMatchTimes() |
| 65 | + } |
| 66 | + if b.NoMatchTimes() >= 5 { |
| 67 | + b.SetTracking(false) |
| 68 | + bt.deregister(i) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func (bt *Blobies) prepare() { |
| 74 | + for i := range bt.Objects { |
| 75 | + bt.Objects[i].SetExists(false) |
| 76 | + bt.Objects[i].PredictNextPosition(bt.maxNoMatch) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Register - Register new blob |
| 81 | +func (bt *Blobies) Register(b Blobie) error { |
| 82 | + newUUID := uuid.NewV4() |
| 83 | + b.SetID(newUUID) |
| 84 | + bt.Objects[newUUID] = b |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// deregister - deregister blob with provided uuid |
| 89 | +func (bt *Blobies) deregister(guid uuid.UUID) { |
| 90 | + delete(bt.Objects, guid) |
| 91 | +} |
0 commit comments