public void Operate(IList <MotionObjectInfo> selectedInfoList, IList <ProcParam <MotionProcEnv> > args, MotionDataSet dataSet, ProgressInformation progressInfo) { SingleSelectParameter mode = args[0] as SingleSelectParameter; NumberParameter limit2 = args[1] as NumberParameter; bool addMode = mode.Value == 1; int limit = (int)limit2.Value; progressInfo.Initialize(selectedInfoList.Count, "Interpolate"); foreach (var info in selectedInfoList) { // 欠落範囲の集合 RangeSet <int> missings = new RangeSet <int>(); bool exist = true; int begin = 0; // 最初のフレームからかけている部分,最後のかけている部分は無視 for (int i = 0; i < dataSet.FrameLength; i++) { if (dataSet.GetFrameByIndex(i)[info] == null) { if (exist) { begin = i; exist = false; } } else { if (!exist) { if (begin != 0) { missings.Add(new RangeSet <int> .Range(begin, i)); exist = true; } } } } // 別オブジェクトにするとき用に入力オブジェクトと出力オブジェクトを分ける MotionObjectInfo addInfo = info; if (addMode) { // 別オブジェクトにするオプション addInfo = new MotionObjectInfo(info.ObjectType, info); addInfo.Name = PathEx.GiveName("interpolate", info.Name); dataSet.AddObject(addInfo); } // 線形補間 foreach (var range in missings) { if (limit == 0 || range.End - range.Start <= limit) { int pre = range.Start - 1; int post = range.End; MotionFrame preFrame = dataSet.GetFrameByIndex(pre); MotionFrame postFrame = dataSet.GetFrameByIndex(post); MotionObject preObject = preFrame[info]; if (preObject != null) { for (int index = range.Start; index < range.End; index++) { float interpolater = (float)(index - pre) / (post - pre); MotionFrame frame = dataSet.GetFrameByIndex(index); frame[addInfo] = preObject.InterpolateLinear(postFrame[info], interpolater); } } } } progressInfo.CurrentValue++; } dataSet.DoFrameListChanged(); if (addMode) { dataSet.DoObjectInfoSetChanged(); } }
public ScriptVariable Call(IList <ScriptVariable> args, ScriptConsole console) { _progress.Initialize(0, "Initializing..."); if (args == null) { throw new ArgumentNullException("args", "args cannot be null"); } if (args.Count < 1) { args = new List <ScriptVariable> { null } } ; if (args[0] == null) { args[0] = new ListVariable(); } IList <ScriptVariable> objectNamesVar = args[0].ToList(); if (objectNamesVar.Any(n => n.IsNull())) { throw new ArgumentException(global::MotionDataHandler.Properties.Settings.Default.Msg_ObjectNameCannotBeNull, "args"); } List <string> objectNames = objectNamesVar.Select(n => n.ToString()).ToList(); MotionDataSet dataSet = console.MotionDataSet; MotionProcEnv env2 = new MotionProcEnv(console); List <MotionObjectInfo> infoList = new List <MotionObjectInfo>(); foreach (string objectName in objectNames) { MotionObjectInfo info = dataSet.GetObjectInfoByName(objectName); if (info == null) { throw new ArgumentException(global::MotionDataHandler.Properties.Settings.Default.Msg_ObjectNotFound + ": " + objectName, "args"); } infoList.Add(info); } foreach (MotionObjectInfo info in infoList) { if (!_operation.FilterSelection(info)) { throw new ArgumentException(global::MotionDataHandler.Properties.Settings.Default.Msg_InvalidTargetObjectSpecified + ": " + info.Name, "args"); } } string errorMessage = ""; if (!_operation.ValidateSelection(infoList, ref errorMessage)) { if (errorMessage == null) { errorMessage = ""; } throw new ArgumentException(global::MotionDataHandler.Properties.Settings.Default.Msg_ImproperObjectSelection + ": " + errorMessage, "args"); } IList <ProcParam <MotionProcEnv> > parameters = _operation.GetParameters() ?? new ProcParam <MotionProcEnv> [0]; if (args.Count != parameters.Count + 1) { throw new ArgumentException(string.Format(global::MotionDataHandler.Properties.Settings.Default.Msg_NumberOfArgumentsRequired, parameters.Count + 1)); } for (int i = 0; i < parameters.Count; i++) { if (!parameters[i].FromScriptVariable(env2, args[i + 1], ref errorMessage)) { throw new ArgumentException(string.Format(global::MotionDataHandler.Properties.Settings.Default.Msg_InvalidNthArgument + ": {1}", i + 1, errorMessage ?? ""), "args"); } } if (!_operation.ValidateArguments(parameters, ref errorMessage)) { throw new ArgumentException(string.Format(global::MotionDataHandler.Properties.Settings.Default.Msg_InvalidArgument + ": {0}", errorMessage ?? ""), "args"); } IMotionOperationGeneral general = _operation as IMotionOperationGeneral; if (general != null) { _progress.Initialize(0, "Operation"); general.Operate(infoList, parameters, dataSet, _progress); return(new ListVariable(infoList.Select(info => new StringVariable(info.Name)))); } IMotionOperationEditObject edit = _operation as IMotionOperationEditObject; if (edit != null) { _progress.Initialize(dataSet.FrameLength, "Edit Object"); foreach (MotionFrame frame in dataSet.EnumerateFrame()) { IList <MotionObject> results = edit.EditObject(infoList, parameters, new ReadOnlyMotionFrame(frame), false); int count = Math.Min(results.Count, infoList.Count); for (int i = 0; i < count; i++) { frame[infoList[i]] = results[i]; } _progress.CurrentValue++; } dataSet.DoFrameListChanged(); return(new ListVariable(infoList.Select(info => new StringVariable(info.Name)))); } IMotionOperationOutputSequence output = _operation as IMotionOperationOutputSequence; if (output != null) { _progress.Initialize(0, "Output"); IList <Sequence.SequenceData> sequences = output.OutputSequence(infoList, parameters, dataSet.EnumerateFrame().Select(frame => new ReadOnlyMotionFrame(frame)), _progress); foreach (Sequence.SequenceData sequence in sequences) { console.SequenceController.AddSequence(sequence); } return(new ListVariable(sequences.Select(s => new StringVariable(s.Title)))); } IMotionOperationCreateObject create = _operation as IMotionOperationCreateObject; if (create != null) { _progress.Initialize(dataSet.FrameLength, "Create Object"); IList <MotionObjectInfo> newInfoList = create.GetNewObjectInfoList(infoList, parameters); MotionFrame firstFrame = dataSet.GetFrameByIndex(0); if (firstFrame != null) { IList <MotionObject> newObjects = create.CreateObjects(infoList, parameters, new ReadOnlyMotionFrame(firstFrame), false) ?? new MotionObject[0]; if (newObjects.Count != newInfoList.Count) { throw new InvalidOperationException(global::MotionDataHandler.Properties.Settings.Default.Msg_CreateObjectLengthMismatch); } } foreach (MotionObjectInfo newInfo in newInfoList) { dataSet.AddObject(newInfo); } foreach (MotionFrame frame in dataSet.EnumerateFrame()) { IList <MotionObject> newObjects = create.CreateObjects(infoList, parameters, new ReadOnlyMotionFrame(frame), false) ?? new MotionObject[0]; int count = Math.Min(newObjects.Count, newInfoList.Count); for (int i = 0; i < count; i++) { frame[newInfoList[i]] = newObjects[i]; } _progress.CurrentValue++; } dataSet.DoObjectInfoSetChanged(); dataSet.DoFrameListChanged(); return(new ListVariable(newInfoList.Select(info => new StringVariable(info.Name)))); } return(null); }
private void loadEVaRTTrc(TrcReader reader) { WaitForForm waitForm = new WaitForForm(ctrl => { try { _dataSet.ClearFrame(); _dataSet.ClearObject(); Dictionary <int, uint> index2id = new Dictionary <int, uint>(); int length = reader.Header.NumFrames; int count = 0; for (int i = 0; i < reader.Header.NumMarkers; i++) { MotionObjectInfo newInfo = new MotionObjectInfo(typeof(PointObject)); newInfo.Name = reader.Header.Markers[i]; _dataSet.AddObject(newInfo); index2id[i] = newInfo.Id; } while (!reader.EndOfStream) { TrcFrame inFrame = reader.ReadFrame(); MotionFrame outFrame = new MotionFrame(_dataSet, inFrame.Time); for (int i = 0; i < inFrame.Markers.Length; i++) { uint id; if (!index2id.TryGetValue(i, out id)) { MotionObjectInfo newInfo = new MotionObjectInfo(typeof(PointObject)); newInfo.Name = PathEx.CombineName("unnamed", (i + 1).ToString()); _dataSet.AddObject(newInfo); id = index2id[i] = newInfo.Id; } if (inFrame.Markers[i].HasValue) { float x, y, z; if (float.TryParse(inFrame.Markers[i].Value.X, out x) && float.TryParse(inFrame.Markers[i].Value.Y, out y) && float.TryParse(inFrame.Markers[i].Value.Z, out z)) { outFrame[id] = new PointObject(new Vector3(x, y, z)); } } } _dataSet.AddFrame(outFrame); if (length < count) { length = count; } if (length <= 0) { length = 1; } ctrl.ReportProgress(100 * count / length, string.Format("Load Frame: {0}/{1} ({2} sec)", count, length, inFrame.Time.ToString("0.00"))); count++; } ctrl.ReportProgress(100, string.Format("Done")); ctrl.DialogResult = DialogResult.OK; } catch (Exception) { _dataSet.ClearObject(); _dataSet.ClearFrame(); _dataSet.DoObjectInfoSetChanged(); _dataSet.DoFrameListChanged(); throw; } }); if (waitForm.ShowDialog() == DialogResult.OK) { _dataSet.DoObjectInfoSetChanged(); _dataSet.DoFrameListChanged(); } }