Пример #1
0
        /// <summary>
        /// Iterates over all video sources in the current study and hands them over to the ViewContainerManager
        /// </summary>
        /// <param name="currentStudy">The <see cref="StudyData"/> of the current data.</param>
        private void PrepareMedia(StudyData currentStudy)
        {
            List <MediaSource> media = new List <MediaSource>();

            foreach (var mediaSource in currentStudy.MediaSources)
            {
                if (mediaSource.File.StartsWith("https"))
                {
                    media.Add(mediaSource);
                }
                else
                {
                    string filePath = Path.Combine(dataPath, mediaSource.File);
                    if (System.IO.File.Exists(filePath))
                    {
#if UNITY_WSA && !UNITY_EDITOR
                        // As per https://forum.unity.com/threads/url-for-videoplayer-in-uwp.503331/ we copy the file to Application.persistentDataPath, otherwise we get file access violations on UWP/WSA.
                        string wsaFilePath = Path.Combine(Application.persistentDataPath, Math.Abs(filePath.GetHashCode()).ToString() + "_" + Path.GetFileName(filePath));
                        System.IO.File.Copy(filePath, wsaFilePath, true);
                        mediaSource.File = wsaFilePath;
                        media.Add(mediaSource);
#else
                        mediaSource.File = filePath;
                        media.Add(mediaSource);
#endif
                    }
                }
            }

            Services.ContainerManager().UpdateVideoSources(media);
        }
Пример #2
0
        private void GenerateAnchors(StudyData currentStudy)
        {
            var visContainers = ParseAnchors(currentStudy.Anchors);

            Services.VisManager().DeleteAllViewContainers(false);
            foreach (var container in visContainers)
            {
                Services.VisManager().CreateViewContainer(container, false);
            }
        }
Пример #3
0
        private List <string> GenerateFileList(StudyData studyXml)
        {
            List <string> fileList = new List <string>();

            foreach (var objectSource in studyXml.ObjectSources)
            {
                string fileName = objectSource.File;
                if (fileList.Contains(fileName) == false)
                {
                    fileList.Add(fileName);
                }
            }

            return(fileList);
        }
Пример #4
0
        private void ReadAxisMapping(StudyData studyXml)
        {
            try
            {
                AxisDirectionX = GetDirectionVectorFromString(studyXml.AxisDirectionX);
                AxisDirectionY = GetDirectionVectorFromString(studyXml.AxisDirectionY);
                AxisDirectionZ = GetDirectionVectorFromString(studyXml.AxisDirectionZ);
            }
            catch (ArgumentException)
            {
                AxisDirectionX = Vector3.right;
                AxisDirectionY = Vector3.up;
                AxisDirectionZ = Vector3.forward;
            }

            AxisTransformationMatrix4x4 = new Matrix4x4(AxisDirectionX, AxisDirectionY, AxisDirectionZ, new Vector4(0, 0, 0, 1));
        }
Пример #5
0
        /// <summary>
        /// Loads/imports the data for a specific study, defined by the StudyData object
        /// </summary>
        /// <param name="studyXml"> The <see cref="StudyData"/> containing the meta data of the study.</param>
        /// <returns>A <see cref="Task"/> object.</returns>
        private async Task LoadStudyDataAsync(StudyData studyXml)
        {
            // read axis mapping
            ReadAxisMapping(studyXml);

            // 0. read static fields
            DataSets = ReadStaticData(studyXml);

            // 1. generate list of all files from the object sources
            List <string> fileList = GenerateFileList(studyXml);

            // 2. for each file, start a task to read all the data
            List <Task <List <CsvImportBlock> > > tasks = new List <Task <List <CsvImportBlock> > >();

            ////var ImportResults = new List<List<CsvImportBlock>>();
            foreach (var file in fileList)
            {
                tasks.Add(Task.Run(() => ImportFromFile(file, studyXml)));
                ////ImportResults.Add(ImportFromFile(File, studyXml));
            }

            // 3. wait for all tasks to finish
            var importResults = await Task.WhenAll(tasks);

            // 4. combine results
            DataSets = CombineResults(importResults, DataSets);
            foreach (var dataSet in DataSets.Values)
            {
                dataSet.RecomputeBounds();
            }
            ////foreach (var Task in Tasks)
            ////{
            ////    ImportResults.Add(Task.Result);
            ////}
            NormalizeTimestamps();
        }
Пример #6
0
        private List <CsvImportBlock> ImportFromFile(string file, StudyData studyXml)
        {
            // find all object sources for this file
            List <ObjectImportHelper> objectImportHelpers = new List <ObjectImportHelper>();

            foreach (var objectSource in studyXml.ObjectSources)
            {
                if (objectSource.File.Equals(file))
                {
                    objectImportHelpers.Add(new ObjectImportHelper(objectSource, DataSets[objectSource.ObjectId]));
                }
            }

            using (FileStream fs = new FileStream(Path.Combine(dataPath, file), FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    // read header (first line in CSV)
                    string   header    = sr.ReadLine();
                    string[] lineArray = SplitCsvLine(header);

                    // parse the header: iterate over object importers to extract necessary field ids from the header
                    foreach (var objectImporter in objectImportHelpers)
                    {
                        // get object
                        var studyObject = studyXml.Objects[objectImporter.ObjectId];
                        int rot_w = -1, rot_x = -1, rot_y = -1, rot_z = -1, pos_x = -1, pos_y = -1, pos_z = -1, scale_x = -1, scale_y = -1, scale_z = -1, time = -1, state = -1;

                        // for each column, let's find out what is saved in there.
                        for (int i = 0; i < lineArray.Length; i++)
                        {
                            if (lineArray[i].Equals(studyObject.TimestampSource))
                            {
                                time = i;
                            }
                            else if (lineArray[i].Equals(studyObject.PositionXSource))
                            {
                                pos_x = i;
                            }
                            else if (lineArray[i].Equals(studyObject.PositionYSource))
                            {
                                pos_y = i;
                            }
                            else if (lineArray[i].Equals(studyObject.PositionZSource))
                            {
                                pos_z = i;
                            }
                            else if (lineArray[i].Equals(studyObject.RotationWSource))
                            {
                                rot_w = i;
                            }
                            else if (lineArray[i].Equals(studyObject.RotationXSource))
                            {
                                rot_x = i;
                            }
                            else if (lineArray[i].Equals(studyObject.RotationYSource))
                            {
                                rot_y = i;
                            }
                            else if (lineArray[i].Equals(studyObject.RotationZSource))
                            {
                                rot_z = i;
                            }
                            else if (lineArray[i].Equals(studyObject.ScaleXSource))
                            {
                                scale_x = i;
                            }
                            else if (lineArray[i].Equals(studyObject.ScaleYSource))
                            {
                                scale_y = i;
                            }
                            else if (lineArray[i].Equals(studyObject.ScaleZSource))
                            {
                                scale_z = i;
                            }
                            else if (lineArray[i].Equals(studyObject.StateSource))
                            {
                                state = i;
                            }
                            else if (lineArray[i].Equals(objectImporter.Source.ConditionFilterColumn))
                            {
                                objectImporter.ConditionFilterId = i;
                            }
                            else if (lineArray[i].Equals(objectImporter.Source.SessionFilterColumn))
                            {
                                objectImporter.SessionFilterId = i;
                            }

                            // could look for other data here...
                        }

                        if (time != -1)
                        {
                            objectImporter.Parsers.Add(objectImporter.ParseTimestamp);
                            objectImporter.ParserIndices.Add(new int[] { time });
                        }

                        if (state != -1)
                        {
                            DataSets[studyObject.Id].HasStateData = true;
                            objectImporter.Parsers.Add(objectImporter.ParseState);
                            objectImporter.ParserIndices.Add(new int[] { state });
                        }

                        if (pos_x != -1 && pos_y != -1 && pos_z != -1)
                        {
                            objectImporter.Parsers.Add(objectImporter.ParsePosition);
                            objectImporter.ParserIndices.Add(new int[] { pos_x, pos_y, pos_z });
                        }

                        if (scale_x != -1 && scale_y != -1 && scale_z != -1)
                        {
                            objectImporter.Parsers.Add(objectImporter.ParseScale);
                            objectImporter.ParserIndices.Add(new int[] { scale_x, scale_y, scale_z });
                        }

                        if (rot_x != -1 && rot_y != -1 && rot_z != -1)
                        {
                            if (rot_w != -1)
                            {
                                objectImporter.Parsers.Add(objectImporter.ParseRotationWXYZ);
                                objectImporter.ParserIndices.Add(new int[] { rot_w, rot_x, rot_y, rot_z });
                            }
                            else
                            {
                                objectImporter.Parsers.Add(objectImporter.ParseRotationXYZ);
                                objectImporter.ParserIndices.Add(new int[] { rot_x, rot_y, rot_z });
                            }
                        }
                    }

                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        lineArray = SplitCsvLine(line);
                        //foreach (var objectImporter in objectImportHelpers)
                        //{
                        //    objectImporter.ParseSample(lineArray);
                        //}
                        for (int i = 0; i < objectImportHelpers.Count; i++)
                        {
                            objectImportHelpers[i].ParseSample(lineArray);
                        }
                    }
                }
            }

            // fill list with imported data
            List <CsvImportBlock> results = new List <CsvImportBlock>();

            foreach (var objectImporter in objectImportHelpers)
            {
                // pre-compute speed in samples
                if (objectImporter.ImportData.Samples.Count > 1)
                {
                    float maxSpeed = 0f;
                    var   samples  = objectImporter.ImportData.Samples;
                    for (int i = 1; i < samples.Count; i++)
                    {
                        float deltaPosition = (samples[i].Position - samples[i - 1].Position).magnitude;
                        float deltaTime     = (float)(samples[i].Timestamp - samples[i - 1].Timestamp) / TimeSpan.TicksPerSecond;
                        samples[i].Speed = deltaPosition / deltaTime;
                        if (float.IsNaN(samples[i].Speed) || float.IsInfinity(samples[i].Speed))
                        {
                            samples[i].Speed = samples[i - 1].Speed;
                        }

                        if (samples[i].Speed > maxSpeed)
                        {
                            maxSpeed = samples[i].Speed;
                            objectImporter.ImportData.MaxSpeed = maxSpeed;
                        }
                    }
                }

                // add samples
                results.Add(objectImporter.ImportData);
            }
            return(results);
        }
Пример #7
0
        private Dictionary <int, AnalysisObject> ReadStaticData(StudyData studyXml)
        {
            var result = new Dictionary <int, AnalysisObject>();

            foreach (var studyObject in studyXml.Objects)
            {
                int    id   = studyObject.Id;
                string name = studyObject.Name;
                if (!Enum.TryParse(studyObject.ObjectType, true, out ObjectType Type))
                {
                    Type = ObjectType.UNKNOWN;
                }

                string source = studyObject.Source;
                int    parent = studyObject.ParentId;
                Color  color  = Color.HSVToRGB(studyObject.ColorHue, studyObject.ColorSaturation, studyObject.ColorValue);

                // rotation format
                RotationFormat rotationFormat = RotationFormat.QUATERNION;
                switch (studyObject.RotationFormat)
                {
                case "euler_deg":
                    rotationFormat = RotationFormat.EULER_DEG;
                    break;

                case "euler_rad":
                    rotationFormat = RotationFormat.EULER_RAD;
                    break;

                case "quaternion":
                    rotationFormat = RotationFormat.QUATERNION;
                    break;

                case "direction_vector":
                    rotationFormat = RotationFormat.DIRECTION_VECTOR;
                    break;
                }

                // time format
                TimeFormat timeFormat = TimeFormat.FLOAT;
                switch (studyObject.TimeFormat)
                {
                case "float":
                    timeFormat = TimeFormat.FLOAT;
                    break;

                case "long":
                    timeFormat = TimeFormat.LONG;
                    break;

                case "string":
                    timeFormat = TimeFormat.STRING;
                    break;
                }

                // scale factor depending on units; we need m
                float unitScaleFactor = 1.0f;
                switch (studyObject.Units)
                {
                case "mm":
                    unitScaleFactor = 0.001f;
                    break;

                case "cm":
                    unitScaleFactor = 0.01f;
                    break;

                case "m":
                    unitScaleFactor = 1f;
                    break;
                }

                var analysisObject = new AnalysisObject(name, id, Type, parent, source, unitScaleFactor, timeFormat, rotationFormat, studyXml.Conditions, studyXml.Sessions, color);

                // parse properties of the object description to get static data
                analysisObject = ParseStaticVariables(studyObject, analysisObject);

                // is the dataset/object static, i.e., not time-dependent?
                analysisObject.IsStatic = studyObject.IsStatic;

                // load model mesh if available
                Mesh objectMesh = null;
                if (studyObject.ModelFile != string.Empty)
                {
                    objectMesh = BasicObjImporter.ImportFromFile(Path.Combine(dataPath, studyObject.ModelFile));
                }

                analysisObject.ObjectModel = objectMesh;

                // add to dictionary
                result.Add(id, analysisObject);
            }

            return(result);
        }