public static float GetClipPosition(Extrapolation extrapolation, float time, float duration) { if (Mathf.Approximately(duration, 0.0f)) { return(0.0f); } float t = time / duration; switch (extrapolation) { case Extrapolation.Loop: { return(Mathf.Abs(t) - Mathf.Floor(t)); } case Extrapolation.PingPong: { t = Mathf.Abs(t); int n = Mathf.FloorToInt(t); t -= (float)n; if (n % 2 == 1) { t = 1.0f - t; } return(t); } case Extrapolation.Hold: default: return(Mathf.Clamp01(t)); } }
public FixedLocalVolSurface(Date referenceDate, List <Date> dates, List <double> strikes, Matrix localVolMatrix, DayCounter dayCounter, Extrapolation lowerExtrapolation = Extrapolation.ConstantExtrapolation, Extrapolation upperExtrapolation = Extrapolation.ConstantExtrapolation) : base(referenceDate, null, BusinessDayConvention.Following, dayCounter) { maxDate_ = dates.Last(); localVolMatrix_ = localVolMatrix; strikes_ = new InitializedList <List <double> >(dates.Count, new List <double>(strikes)); localVolInterpol_ = new List <Interpolation>(); lowerExtrapolation_ = lowerExtrapolation; upperExtrapolation_ = upperExtrapolation; Utils.QL_REQUIRE(dates[0] >= referenceDate, () => "cannot have dates[0] < referenceDate"); times_ = new InitializedList <double>(dates.Count); for (int j = 0; j < times_.Count; j++) { times_[j] = timeFromReference(dates[j]); } checkSurface(); setInterpolation <Linear>(); }
public FixedLocalVolSurface(Date referenceDate, List <double> times, List <double> strikes, Matrix localVolMatrix, DayCounter dayCounter, Extrapolation lowerExtrapolation = Extrapolation.ConstantExtrapolation, Extrapolation upperExtrapolation = Extrapolation.ConstantExtrapolation) : base(referenceDate, null, BusinessDayConvention.Following, dayCounter) { maxDate_ = Utils.time2Date(referenceDate, dayCounter, times.Last()); times_ = times; localVolMatrix_ = localVolMatrix; strikes_ = new InitializedList <List <double> >(times.Count, new List <double>(strikes)); localVolInterpol_ = new List <Interpolation>(); lowerExtrapolation_ = lowerExtrapolation; upperExtrapolation_ = upperExtrapolation; Utils.QL_REQUIRE(times[0] >= 0.0, () => "cannot have times[0] < 0"); checkSurface(); setInterpolation <Linear>(); }
public LogLinearInterpolator(IEnumerable <Tuple <double, double> > keyPoints, Extrapolation extrapolation = Extrapolation.Natural) { _keyPoints = keyPoints.OrderBy(x => x.Item1).ToList(); _extrapolation = extrapolation; if (!_keyPoints.Any()) { throw new PricingLibraryException("Loglinear interpolation requires at least 1 point"); } _slopes = new double[_keyPoints.Count - 1]; for (var i = 0; i < _slopes.Length; ++i) { _slopes[i] = (Math.Log(_keyPoints[i + 1].Item2) - Math.Log(_keyPoints[i].Item2)) / (_keyPoints[i + 1].Item1 - _keyPoints[i].Item1); } }
public BlackVarianceSurface(Date referenceDate, Calendar calendar, List <Date> dates, List <double> strikes, Matrix blackVolMatrix, DayCounter dayCounter, Extrapolation lowerExtrapolation = Extrapolation.InterpolatorDefaultExtrapolation, Extrapolation upperExtrapolation = Extrapolation.InterpolatorDefaultExtrapolation) : base(referenceDate, calendar) { dayCounter_ = dayCounter; maxDate_ = dates.Last(); strikes_ = strikes; lowerExtrapolation_ = lowerExtrapolation; upperExtrapolation_ = upperExtrapolation; dates_ = dates; volatilities_ = blackVolMatrix; Utils.QL_REQUIRE(dates.Count == blackVolMatrix.columns(), () => "mismatch between date vector and vol matrix colums"); Utils.QL_REQUIRE(strikes_.Count == blackVolMatrix.rows(), () => "mismatch between money-strike vector and vol matrix rows"); Utils.QL_REQUIRE(dates[0] >= referenceDate, () => "cannot have dates[0] < referenceDate"); int i, j; times_ = new InitializedList <double>(dates.Count + 1); times_[0] = 0.0; variances_ = new Matrix(strikes_.Count, dates.Count + 1); for (i = 0; i < blackVolMatrix.rows(); i++) { variances_[i, 0] = 0.0; } for (j = 1; j <= blackVolMatrix.columns(); j++) { times_[j] = timeFromReference(dates[j - 1]); Utils.QL_REQUIRE(times_[j] > times_[j - 1], () => "dates must be sorted unique!"); for (i = 0; i < blackVolMatrix.rows(); i++) { variances_[i, j] = times_[j] * blackVolMatrix[i, j - 1] * blackVolMatrix[i, j - 1]; } } // default: bilinear interpolation setInterpolation <Bilinear>(); }
public LinearInterpolator(Tuple <double, double>[] keyPoints, Extrapolation extrapolation = Extrapolation.Flat) { _keyPoints = keyPoints.ToList(); if (_keyPoints.Count < 1) { throw new PricingLibraryException("LinearInterpolator must have at least 1 point"); } _extrapolation = extrapolation; _integrals = new List <double> { 0.0 }; for (var i = 1; i < _keyPoints.Count; ++i) { var dx = _keyPoints[i].Item1 - _keyPoints[i - 1].Item1; var slope = (_keyPoints[i].Item2 - _keyPoints[i - 1].Item2) / dx; _integrals.Add(_integrals[i - 1] + dx * (_keyPoints[i - 1].Item2 + 0.5 * dx * slope)); } }
public void StartCameraShot(CinematicCameraShot shot, Extrapolation extrapolation, float blendTime = -1.0f, InterpolationType blendType = InterpolationType.InOutCubic) { if (blendTime <= 0.0f) { _currentShot._weight = 1.0f; _blendingShots = new ShotInfo[0]; } else if (_currentShot._shot != null) { ArrayUtils.Add(ref _blendingShots, _currentShot); _currentShot._weight = 0.0f; _currentShot._blendType = blendType; _currentShotBlendSpeed = 1.0f / blendTime; } _currentShot._shot = shot; _currentShot._extrapolation = extrapolation; _currentShot._time = 0.0f; }
public override void ProcessFrame(Playable playable, FrameData info, object playerData) { _trackBinding = playerData as CinematicCamera; if (_trackBinding == null) { return; } if (!_firstFrameHappened) { _defaultState = _trackBinding.GetState(); _firstFrameHappened = true; } int numInputs = playable.GetInputCount(); float[] inputWeights = new float[numInputs]; float totalWeights = 0.0f; CinematicCameraState[] states = new CinematicCameraState[numInputs]; for (int i = 0; i < numInputs; i++) { ScriptPlayable <CinematicCameraPlayableBehaviour> scriptPlayable = (ScriptPlayable <CinematicCameraPlayableBehaviour>)playable.GetInput(i); CinematicCameraPlayableBehaviour inputBehaviour = scriptPlayable.GetBehaviour(); if (inputBehaviour != null && (inputBehaviour._cameraShot != null || inputBehaviour._path != null)) { float inputWeight = playable.GetInputWeight(i); if (inputWeight > 0.0f) { TimelineClip clip = _trackAsset.GetClip(inputBehaviour._clipAsset); if (clip != null) { double clipStart = clip.hasPreExtrapolation ? clip.extrapolatedStart : clip.start; double clipDuration = clip.hasPreExtrapolation || clip.hasPostExtrapolation ? clip.extrapolatedDuration : clip.duration; if (_director.time >= clipStart && _director.time <= clipStart + clipDuration) { inputWeights[i] = inputWeight; Extrapolation extrapolation = Extrapolation.Hold; if (clip.hasPreExtrapolation && _director.time < clip.start) { extrapolation = GetExtrapolation(clip.preExtrapolationMode); } else if (clip.hasPostExtrapolation && _director.time > clip.start + clip.duration) { extrapolation = GetExtrapolation(clip.postExtrapolationMode); } float timeInClip = (float)(_director.time - clip.start); //Single shot if (inputBehaviour._cameraShot != null) { states[i] = inputBehaviour._cameraShot.GetState(timeInClip, (float)clip.duration); } //Camera path else if (inputBehaviour._path != null) { float clipPosition = CinematicCameraMixer.GetClipPosition(extrapolation, timeInClip, (float)clip.duration); float pathT = (float)MathUtils.Interpolate(inputBehaviour._pathInterpolation, 0d, 1f, clipPosition); //Work out path position PathPosition pos = inputBehaviour._path.GetPoint(pathT); //Work out a lerp between to path nodes inputBehaviour._path.GetNodeSection(pathT, out int startNode, out int endNode, out float sectionT); //Lerp camera shot state based on the relevant section of the path GetPathNodeStateInfo(timeInClip, (float)clip.duration, inputBehaviour._path, startNode, out CinematicCameraState startNodeState, out Quaternion startNodeFromTo, out Vector3 startNodeCamFor, out Vector3 startNodeCamUp); GetPathNodeStateInfo(timeInClip, (float)clip.duration, inputBehaviour._path, endNode, out CinematicCameraState endNodeState, out Quaternion endNodeFromTo, out Vector3 endNodeCamFor, out Vector3 endNodeCamUp); Vector3 cameraForward; Vector3 cameraUp; if (sectionT <= 0f) { states[i] = startNodeState; //cameraForward = startNodeFromTo * pos._pathForward; cameraForward = startNodeCamFor; cameraUp = startNodeCamUp; } else if (sectionT >= 1f) { states[i] = endNodeState; //cameraForward = endNodeFromTo * pos._pathForward; cameraForward = endNodeCamFor; cameraUp = endNodeCamUp; } else { states[i] = CinematicCameraState.Interpolate(_trackBinding, startNodeState, endNodeState, InterpolationType.Linear, sectionT); //Work out rotation based on Quaternion cameraRotation = Quaternion.Slerp(startNodeFromTo, endNodeFromTo, sectionT); cameraForward = cameraRotation * pos._pathForward; cameraForward = Vector3.Lerp(startNodeCamFor, endNodeCamFor, sectionT); cameraUp = Vector3.Lerp(startNodeCamUp, endNodeCamUp, sectionT); } //Set camera position from path pos states[i]._position = pos._pathPosition; states[i]._rotation = Quaternion.LookRotation(cameraForward, cameraUp); } totalWeights += inputWeights[i]; } } } } } if (totalWeights > 0.0f) { CinematicCameraState blendedState = _defaultState; float weightAdjust = 1.0f / totalWeights; bool firstBlend = true; for (int i = 0; i < numInputs; i++) { if (inputWeights[i] > 0.0f) { if (firstBlend) { blendedState = states[i]; firstBlend = false; } else { blendedState = CinematicCameraState.Interpolate(_trackBinding, blendedState, states[i], InterpolationType.Linear, inputWeights[i] * weightAdjust); } } } _trackBinding.SetState(blendedState); } else { _trackBinding.SetState(_defaultState); } }
public Form1() { InitializeComponent(); Extr = new Extrapolation(); Extr.SetLogging(WriteHorizontal, WriteVertical, Log); // включаем логирование из модуля }
public LogCubicInterpolator(IEnumerable <Tuple <double, double> > keyPoints, Extrapolation extrapolation = Extrapolation.Natural) { _keyPoints = keyPoints.OrderBy(x => x.Item1).ToList(); _interpolator = new CubicHermiteMonotonicInterpolator(_keyPoints.Select(x => Tuple.Create(x.Item1, Math.Log(x.Item2)))); }