public ClosestPointToRetSplineResult CalcClosestPointOnRetSpline(Vector3 position, ClosestPointToRetSplineResult retval) { Vector3 temp = Vector3.zero, tempDif = Vector3.zero; float dist = float.MaxValue; for (int i = 0; i < GetNumNodes(); i++) { for (int j = 0; j < GetNode(i).GetNumReticulatedSegments(); j++) { ReticulatedSplineSegment seg = GetNode(i).GetReticulatedSegment(j); temp = AID.UTIL.ClosestPointToLine(seg.startPos, seg.endPos, position, true); tempDif = temp - position; if (dist > tempDif.sqrMagnitude) { //retval = temp; dist = tempDif.sqrMagnitude; retval.closestPoint = temp; retval.distSqrFromTarget = dist; retval.retPos = new SplineReticulatedPosition(); retval.retPos.splineNodeIndex = i; // = GetNode(i); retval.retPos.retSeg = j; // = GetNode(i).GetReticulatedSegment(j); retval.retPos.spline = this; retval.retPos.distanceFromRetStart = (temp - GetNode(i).reticulationPoints[j]).magnitude; } } } return(retval); }
private Vector3 _getReticulatedPosition() { //return new pos ReticulatedSplineSegment seg = spline.GetNode(curSplineSegment).GetReticulatedSegment(curRetSegment); return(seg.startPos + seg.dir * curRetProgression); }
public void Reticulate(SplineNode nextNode, int resolution) { ZeroReticulatedData(); resolution = Mathf.Max(2, resolution); reticulationPoints = new Vector3[resolution]; Vector3 t1 = inFragment + outFragment; Vector3 t2 = nextNode.inFragment + nextNode.outFragment; Vector3 p1 = transform.position; Vector3 p2 = nextNode.transform.position; float increment = 1.0f / (resolution - 1); bounds = new Bounds(); for (int i = 0; i < resolution; ++i) { reticulationPoints[i] = AID.UTIL.CubicHermite(t1, p1, p2, t2, increment * i); bounds.Encapsulate(reticulationPoints[i]); } distanceToNextNode = 0; reticulatedSegments = new ReticulatedSplineSegment[resolution - 1]; for (int i = 0; i < reticulatedSegments.Length; ++i) { ReticulatedSplineSegment retseg = new ReticulatedSplineSegment(); retseg.startPos = reticulationPoints[i]; retseg.endPos = reticulationPoints[i + 1]; retseg.dir = retseg.endPos - retseg.startPos; retseg.length = retseg.dir.magnitude; retseg.dir.Normalize(); reticulatedSegments[i] = retseg; distanceToNextNode += retseg.length; } }
public Vector3 _handleDistanceMoveForward(float dist) { float distLeft = dist; //HACK to prevent inf loops during development for (int i = 0; i < 10 && distLeft > 0f; i++) //while(distLeft > 0f) { //handle spline being capped and at end already if (loopMode == SplineTraversalLoopMode.Stop) { if ((directionOfTravel && curSplineSegment >= spline.GetNumSections() - 1 && curRetSegment >= spline.GetNode(curSplineSegment).reticulatedSegments.Length - 1 && curRetProgression >= spline.GetNode(curSplineSegment).reticulatedSegments[spline.GetNode(curSplineSegment).reticulatedSegments.Length - 1].length) || (!directionOfTravel && curSplineSegment <= 0 && curRetSegment <= 0 && curRetProgression <= 0)) { return(_getReticulatedPosition()); } } ReticulatedSplineSegment seg = spline.GetNode(curSplineSegment).GetReticulatedSegment(curRetSegment); float distLeftInSeg = 0; if (seg != null) { distLeftInSeg = directionOfTravel ? seg.length - curRetProgression : seg.length - (seg.length - curRetProgression); } //do we have more left than this segment has if (distLeft > distLeftInSeg) { distLeft -= distLeftInSeg; //advance if (directionOfTravel) { curRetProgression = 0; ++curRetSegment; } else { --curRetSegment; //be safe if (curRetSegment < spline.GetNode(curSplineSegment).GetNumReticulatedSegments() && curRetSegment >= 0) { curRetProgression = spline.GetNode(curSplineSegment).GetReticulatedSegment(curRetSegment).length; } } //we move the segment bool validRetSeg = curRetSegment < spline.GetNode(curSplineSegment).GetNumReticulatedSegments() && curRetSegment >= 0; if (validRetSeg == false) { //end of spline segment move to next/handle end of spline curSplineSegment += directionOfTravel ? 1 : -1; ProcessSplineNodeChange(); } } else { //more space than we need curRetProgression = directionOfTravel ? curRetProgression + distLeft : curRetProgression - distLeft; distLeft = 0; break; //force break out } } return(_getReticulatedPosition()); }
public float DistanceBetween(SplineReticulatedPosition other) { //choose closest if (other == null || this.spline != other.spline) { return(float.MaxValue); } float sign = 1f; SplineReticulatedPosition near = null, far = null; if (this.splineNodeIndex < other.splineNodeIndex) { near = this; far = other; } else if (this.splineNodeIndex > other.splineNodeIndex) { far = this; near = other; sign = -1; } else if (this.retSeg < other.retSeg) { near = this; far = other; } else if (this.retSeg > other.retSeg) { far = this; near = other; sign = -1; } else//same node and ret seg { //if start seg and end seg the same return dif in distance traveled return(other.distanceFromRetStart - this.distanceFromRetStart); } //add rem distance in this seg float distAccum = near.spline.GetNode(near.splineNodeIndex).GetReticulatedSegment(near.retSeg).length - near.distanceFromRetStart; //add the distance the final seg as already traveled distAccum += far.distanceFromRetStart; int curIndex = near.splineNodeIndex, curSeg = near.retSeg + 1; //run until we are on the same node and same seg while (curIndex < far.splineNodeIndex || curSeg < far.retSeg) { ReticulatedSplineSegment ret = near.spline.GetNode(curIndex).GetReticulatedSegment(curSeg); if (ret != null) { distAccum += ret.length; //move from its seg forward curSeg++; } else { //end of ret info move forward curIndex++; curSeg = 0; } } return(distAccum * sign); }