public CloudPointDataset(int id, String name, String path) : base(id, name) { m_path = path; using (FileStream file = File.Open($"{Application.streamingAssetsPath}/Datasets/{name}", FileMode.Open, FileAccess.Read)) { if (file == null) { Debug.LogError($"Cannot open the file {m_path}."); return; } //Read the first 4 bytes to get the number of points stored in the file byte[] array = new byte[4]; file.Read(array, 0, 4); IntFloatUnion intFloatUnion = new IntFloatUnion(); intFloatUnion.FillWithByteArray(array, 0); m_nbPoints = (UInt32)intFloatUnion.IntField; //Add a point field descriptor corresponding to the unique data stored in the file PointFieldDescriptor desc = new PointFieldDescriptor(); desc.Format = VTKValueFormat.VTK_FLOAT; desc.ID = 0; desc.Name = "data"; desc.NbValuesPerTuple = 1; desc.NbTuples = (uint)m_nbPoints; m_ptFieldDescs.Add(desc); } }
/// <summary> /// Represent a VTKDataset /// </summary> /// <param name="id">The ID of the Dataset</param> /// <param name="name">The Dataset's name</param> /// <param name="parser">The VTKParser containing all the dataset.</param> /// <param name="ptFieldValues">Array of point field values to take account of</param> /// <param name="cellFieldValues">Array of cell field values to take account of</param> public VTKDataset(int id, String name, VTKParser parser, VTKFieldValue[] ptFieldValues, VTKFieldValue[] cellFieldValues) : base(id, name) { m_timesteps.Add(new VTKDatasetTimeStep { Parser = parser, PtFieldValues = new List <VTKFieldValue>(ptFieldValues), CellFieldValues = new List <VTKFieldValue>(cellFieldValues) }); m_nbTimesteps = 1; //Add all the field values to load for (int i = 0; i < PtFieldValues.Count; i++) { VTKFieldValue metaData = PtFieldValues[i]; PointFieldDescriptor desc = new PointFieldDescriptor(metaData); desc.ID = (UInt32)i; m_ptFieldDescs.Add(desc); } }
public override Task <int> LoadValues() { return(Task.Run(() => { bool succeed = true; //Load the mask if it exists. Look at only timestep 0 (normally every timesteps should be consistent) foreach (VTKFieldValue fv in Parser.GetPointFieldValueDescriptors()) { if (fv.Name == "vtkValidPointMask") { m_mask = Parser.ParseAllFieldValues(fv); break; } } for (int t = 0; t < m_nbTimesteps; t++) { for (int i = 0; i < m_ptFieldDescs.Count; i++) { //Get our working variables PointFieldDescriptor desc = m_ptFieldDescs[i]; VTKFieldValue fieldVal = m_timesteps[t].PtFieldValues[i]; //Load disk values VTKValue value = m_timesteps[t].Parser.ParseAllFieldValues(fieldVal); desc.Value.Add(value); object lockObject = new object(); //This if used as a lock //Case 1: scalar values if (desc.NbValuesPerTuple == 1) { //Compute min/max ranges Parallel.For(0, desc.NbTuples, () => new float[2] { float.MaxValue, float.MinValue }, (j, loopState, partialRes) => { //Read mask unsafe { if (m_mask != null && ((byte *)m_mask.Value)[j] == 0) { return partialRes; } } float val = value.ReadAsFloat((UInt64)j); if (float.IsNaN(val)) { if (m_mask != null) { unsafe { ((byte *)m_mask.Value)[j] = 0; } } return partialRes; } partialRes[0] = Math.Min(partialRes[0], val); partialRes[1] = Math.Max(partialRes[1], val); return partialRes; }, (partialRes) => { lock (lockObject) { desc.MinVal = Math.Min(desc.MinVal, partialRes[0]); desc.MaxVal = Math.Max(desc.MaxVal, partialRes[1]); } }); } //Case 2: vector values else { //Compute min/max ranges. Consider here only vector magnitude in the computation Parallel.For(0, desc.NbTuples, () => new float[2] { float.MaxValue, float.MinValue }, (j, loopState, partialRes) => { //Read mask unsafe { if (m_mask != null && ((byte *)m_mask.Value)[j] == 0) { return partialRes; } } float mag = 0; for (int k = 0; k < desc.NbValuesPerTuple; k++) { float val = value.ReadAsFloat((UInt64)(j * desc.NbValuesPerTuple + k)); if (float.IsNaN(val)) { if (m_mask != null) { unsafe { ((byte *)m_mask.Value)[j] = 0; } } return partialRes; } mag += val * val; } mag = (float)Math.Sqrt((double)mag); partialRes[0] = Math.Min(partialRes[0], mag); partialRes[1] = Math.Max(partialRes[1], mag); return partialRes; }, (partialRes) => { lock (lockObject) { desc.MinVal = Math.Min(desc.MinVal, partialRes[0]); desc.MaxVal = Math.Max(desc.MaxVal, partialRes[1]); } }); } } } m_isLoaded = true; return (succeed ? 1 : 0); })); }