private IEvent CheckPoints(IEvent evnt) { Vector2 point = evnt.point; int pointCount = _allPoints.Count; if (pointCount == 0) { return(null); } //find if this event has nodes that are affected by another event and elect such an event for merging int eventCount = _events.Count; for (int e = 0; e < eventCount; e++) { IEvent aevnt = _events[e]; if (aevnt == evnt) { continue; } if (aevnt.ContainsNodes(evnt.affectedNodes)) { float percentDiff = Mathf.Abs(evnt.percent - aevnt.percent); if (percentDiff > percentAccuracy) { continue; } float sqrMag = Vector2.SqrMagnitude(evnt.point - aevnt.point); OffsetShapeLog.AddLine("SQR MAG ", sqrMag, _sqrPointAccuracy); if (sqrMag < _sqrPointAccuracy) { return(aevnt); } } } //find any events that are close to this one physically. //this could be any logged event //as the subject event exists in the main _event list float lowestMag = Mathf.Infinity; int lowestMagIndex = -1; for (int p = 0; p < pointCount; p++) { float sqrMag = Vector2.SqrMagnitude(point - _allPoints[p]); if (sqrMag < lowestMag) { lowestMag = sqrMag; lowestMagIndex = p; } } if (lowestMagIndex != -1 && lowestMag < _sqrPointAccuracy) { float percentDiff = Mathf.Abs(evnt.percent - _allEvents[lowestMagIndex].percent); if (percentDiff <= percentAccuracy) { return(_allEvents[lowestMagIndex]); } } return(null); }
public void AddEvent(IEvent newEvent) { OffsetShapeLog.AddLine("AddEvent", newEvent); if (newEvent == null) { return; } // OffsetShapeLog.AddLine(newEvent.percent); // OffsetShapeLog.AddLine(newEvent.percent > 1.0f - Mathf.Epsilon); // if (newEvent.percent > 0.99999f) return; //percent accuracy... OffsetShapeLog.AddLine("AddEvent"); OffsetShapeLog.AddLine("current number of events: " + _events.Count); bool lowerPercent = _percent > newEvent.percent; bool closePercent = Mathf.Abs(_percent - newEvent.percent) < percentAccuracy; OffsetShapeLog.AddLine(lowerPercent + " " + closePercent + " " + _percent + " " + newEvent.percent); if (!lowerPercent && !closePercent)//if this event is later than the currently logged events, ignore { _discardedEvents.Add(newEvent); _allEvents.Add(newEvent); _allPoints.Add(newEvent.point); return; } if (lowerPercent && !closePercent)//if this event is sooner then clear the list and log these earlier events { _discardedEvents.AddRange(_events); _events.Clear(); } Vector2 point = newEvent.point; IEvent pEvent = null; if (closePercent) { pEvent = CheckPoints(newEvent); } if (pEvent != null) { OffsetShapeLog.AddLine("Merge existing event"); pEvent.Log(); OffsetShapeLog.AddLine("Merge existing event"); OffsetShapeLog.AddLine(pEvent); OffsetShapeLog.AddLine("With new event"); OffsetShapeLog.AddLine(newEvent); OffsetShapeLog.AddLine("Merged Outcome"); IEvent mergedEvent = MergeEvent(pEvent, newEvent); OffsetShapeLog.AddLine(mergedEvent); OffsetShapeLog.AddLine("percent set ", _percent, mergedEvent.percent); _percent = mergedEvent.percent; _allPoints.Add(point); _allEvents.Add(mergedEvent); int discardedListCount = _discardedEvents.Count; for (int a = 0; a < discardedListCount; a++) { IEvent e = _discardedEvents[a]; if (mergedEvent.ContainsNodes(e.affectedNodes) && !_events.Contains(e)) { float sqrDist = Vector2.SqrMagnitude(e.point - mergedEvent.point); if (sqrDist < _sqrPointAccuracy) { _events.Add(e); mergedEvent.affectedNodes.AddRange(e.affectedNodes); a = 0;//restart to pick up other straglers } } } } else { OffsetShapeLog.AddLine("Add new event"); newEvent.Log(); _events.Add(newEvent); _allPoints.Add(point); _allEvents.Add(newEvent); OffsetShapeLog.AddLine("percent set ", _percent, newEvent.percent); _percent = Mathf.Min(_percent, newEvent.percent); } }