Beispiel #1
0
    void IndexFrames()
    {
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();

        using (var fs = File.OpenRead(@"../../data/light sheet/converted.dat"))
        {
            var br = new BinaryReader(fs);
            dataShape = new ushort[5];
            for (var i = 0; i < 5; i++)
            {
                dataShape[i] = br.ReadUInt16();
            }

            var maxDim = (float)Mathf.Max(dataShape[2], dataShape[3], dataShape[4]);
            transform.localScale = new Vector3(
                dataShape[4] / maxDim,
                dataShape[3] / maxDim,
                1.0f // dataShape[2] / maxDim
                );

            frameSize = (long)dataShape[2] * dataShape[3] * dataShape[4] * sizeof(short);
            Debug.Log(string.Format("Frame size: {0}", frameSize));

            long memoryLimit     = 8L * 1024 * 1024 * 1024; // 8GB
            var  numCachedFrames = (int)Math.Max(memoryLimit / frameSize, 1);
            frames = new LRUDictionary <int, RenderTexture>(
                numCachedFrames,
                (f) =>
            {
                //f.Dispose();
            }
                );

            for (var i = 0L; i < dataShape[1]; i++)
            {
                frameOffsets.Add(br.BaseStream.Position + i * (frameSize));
            }

            frameSlider.maxValue = dataShape[1] - 1;
            ChangeFrame(0);
        }

        sw.Stop();
        Debug.Log("Frames indexed in " + ((double)sw.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency) + "s");
    }
Beispiel #2
0
        static void loadBookmarks(XmlReader r,
				  LRUDictionary<string, int> bookmarks)
        {
            bookmarks.Clear();

            using (XmlReader sub = r.ReadSubtree())
            while (sub.Read())
            if (sub.IsStartElement("item"))
            {
            string key = sub.GetAttribute("file");
            int pos = sub.ReadElementContentAsInt();

            bookmarks.Put(key, pos);
            }
        }
Beispiel #3
0
    void IndexFrames()
    {
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();


        //using (var mmf = MemoryMappedFile.OpenExisting("spatial_cell_buf"))
        //{
        //    mmf.CreateViewStream();
        //    using (var accessor = mmf.CreateViewAccessor(0, 2 * 1024 * 1024 * 1023))
        //    {
        //        int colorSize = Marshal.SizeOf(typeof(MyColor));
        //        MyColor color;

        //        // Make changes to the view.
        //        for (long i = 0; i < 1500000; i += colorSize)
        //        {
        //            accessor.Read(i, out color);
        //            color.Brighten(20);
        //            accessor.Write(i, ref color);
        //        }
        //    }
        //}

        using (var fs = File.OpenRead(@"../spatial_cell_sim/results/frames.dat"))
        //using (var mmf = MemoryMappedFile.OpenExisting("spatial_cell_buf"))
        {
            //using (var accessor = mmf.CreateViewAccessor(0, 2 * 1024 * 1024 * 1023))
            //{
            //var br = new BinaryReader(fs);
            //var memStream = mmf.CreateViewStream();
            var br = new BinaryReader(fs);
            simSize = br.ReadSingle();
            particleVisibleRangeXSlider.MaxValue  = simSize;
            particleVisibleRangeXSlider.HighValue = particleVisibleRangeXSlider.MaxValue;
            particleVisibleRangeYSlider.MaxValue  = simSize;
            particleVisibleRangeYSlider.HighValue = particleVisibleRangeYSlider.MaxValue;
            particleVisibleRangeZSlider.MaxValue  = simSize;
            particleVisibleRangeZSlider.HighValue = particleVisibleRangeZSlider.MaxValue;

            particleBufferSize = br.ReadInt32();
            particleStructSize = Marshal.SizeOf(typeof(Particle));
            Debug.Log("numParticles " + particleBufferSize);
            Debug.Log("particleStructSize " + particleStructSize);
            //particleFrameData.Init(particleBufferSize, particleStructSize);
            metabolicParticleBufferSize = br.ReadInt32();
            metabolicParticleStructSize = Marshal.SizeOf(typeof(MetabolicParticle));
            Debug.Log("numMetabolicParticles " + metabolicParticleBufferSize);
            Debug.Log("metabolicParticleStructSize " + metabolicParticleStructSize);
            //metabolicParticleFrameData.Init(metabolicParticleBufferSize, metabolicParticleStructSize);

            long memoryLimit     = 8L * 1024 * 1024 * 1024; // 8GB
            var  numCachedFrames = (int)Math.Max(memoryLimit / ((long)particleBufferSize * particleStructSize + (long)metabolicParticleBufferSize * metabolicParticleStructSize), 1);
            Debug.Log("numCachedFrames " + numCachedFrames);

            frames = new LRUDictionary <int, Nullable <SimFrame> >(
                numCachedFrames,
                (f) => {
                f.Value.particles.Dispose();
                foreach (var entry in f.Value.particleMap)
                {
                    entry.Value.Dispose();
                }
                f.Value.metabolicParticles.Dispose();
            }
                );

            var particleMaxCountByType = new SortedDictionary <int, int>();
            particleMaxCountByType.Add(0, particleBufferSize);

            while (br.BaseStream.Position != br.BaseStream.Length)
            {
                try
                {
                    //var frame = new SimFrame();
                    var numParticles = br.ReadUInt32();
                    //frame.numParticles = br.ReadUInt32();
                    if (numParticles < 1)
                    {
                        break;
                    }
                    //frame.particles = new NativeArray<Particle>((int)frame.numParticles, Allocator.Persistent);
                    int frameSize = (int)(particleStructSize * numParticles);
                    frameOffsets.Add(br.BaseStream.Position - sizeof(uint));
                    //var bytes = br.ReadBytes(frameSize);

                    br.BaseStream.Position += frameSize;

                    var numMetabolicParticles = br.ReadUInt32();
                    frameSize = (int)(metabolicParticleStructSize * numMetabolicParticles);

                    br.BaseStream.Position += frameSize;

                    //unsafe
                    //{
                    //    //byte* ptr = (byte*)0;
                    //    //accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
                    //    fixed (void* bytesPointer = bytes)
                    //    {
                    //        //UnsafeUtility.CopyStructureToPtr((byte*)bytes[0], frame.particles.GetUnsafePtr());
                    //        UnsafeUtility.MemCpy(frame.particles.GetUnsafePtr(), bytesPointer, UnsafeUtility.SizeOf<Particle>() * frame.numParticles);
                    //        //UnsafeUtility.MemCpy(frame.particles.GetUnsafePtr(), ptr + br.BaseStream.Position, UnsafeUtility.SizeOf<Particle>() * frame.numParticles);
                    //    }
                    //    //accessor.SafeMemoryMappedViewHandle.ReleasePointer();


                    //    //fixed (Particle* bytesPointer = frame.particles.ToArray())
                    //    //{
                    //    //    NativeList<Particle> typeList;
                    //    //    frame.particleMap.TryGetValue(p.type, out typeList);
                    //    //}
                    //}
                    ////br.BaseStream.Position += frameSize;

                    //frame.particleMap = new Dictionary<int, NativeList<Particle>>();
                    //foreach (var p in frame.particles.ToArray())
                    //{
                    //    //Debug.Log("p " + p.id + ", " + p.pos.UnityVector().ToString("F4"));
                    //    NativeList<Particle> typeList;
                    //    if (!frame.particleMap.ContainsKey(p.type))
                    //    {
                    //        typeList = new NativeList<Particle>(0, Allocator.Persistent);
                    //        frame.particleMap.Add(p.type, typeList);
                    //    }
                    //    else
                    //    {
                    //        typeList = frame.particleMap[p.type];
                    //    }
                    //    typeList.Add(p);
                    //}

                    //foreach(var entry in frame.particleMap)
                    //{
                    //    int particleCount = 0;
                    //    particleCount = particleMaxCountByType.TryGetValue(entry.Key, out particleCount) ? particleCount : 0;
                    //    particleCount = Math.Max(particleCount, entry.Value.Length);
                    //    particleMaxCountByType[entry.Key] = particleCount;
                    //}

                    //frame.numMetabolicParticles = br.ReadUInt32();
                    //frame.metabolicParticles = new NativeArray<MetabolicParticle>((int)frame.numMetabolicParticles, Allocator.Persistent);
                    //frameSize = (int)(metabolicParticleStructSize * frame.numMetabolicParticles);
                    //bytes = br.ReadBytes(frameSize);

                    //unsafe
                    //{
                    //    fixed (void* bytesPointer = bytes)
                    //    {
                    //        UnsafeUtility.MemCpy(frame.metabolicParticles.GetUnsafePtr(), bytesPointer, UnsafeUtility.SizeOf<MetabolicParticle>() * frame.numMetabolicParticles);
                    //    }
                    //}

                    ////UnsafeUtility.
                    ////NativeArray.
                    ////for (var i = 0; i < frame.numParticles; i++)
                    ////    frame.particles[i] = Marshal.PtrToStructure<Particle>(Marshal.UnsafeAddrOfPinnedArrayElement(bytes, particleStructSize * i));

                    //frames.Add(frame);

                    ////unsafe
                    ////{
                    ////    fixed (float* m = frame.metabolicParticles.ToArray()[0].metabolites)
                    ////    {
                    ////        Debug.Log("frame.metabolicParticles " + m[0] + ", " + m[1] + ", " + m[2] + ", " + m[3]);
                    ////    }
                    ////}
                    ////return;
                }
                catch (Exception e)
                {
                    Debug.LogError("Caught exception while loading frames " + e);
                    // The last loaded frame may've been corrupted - drop it
                    if (frameOffsets.Count > 0)
                    {
                        frameOffsets.RemoveAt(frameOffsets.Count - 1);
                    }
                    break;
                }
            }

            foreach (var entry in particleMaxCountByType)
            {
                Debug.Log(string.Format("Max count of type {0} particles: {1}", entry.Key, entry.Value));
                var frameData = Instantiate(baseParticleFrameData.gameObject, baseParticleFrameData.transform.parent).GetComponent <FrameData>();
                frameData.gameObject.name = string.Format("{0}-{1}", baseParticleFrameData.gameObject.name, entry.Key);
                //frameData.Init(entry.Value, particleStructSize);
                frameData.gameObject.SetActive(true);
                particleFrameData.Add(entry.Key, frameData);

                var particleRenderer = Instantiate(baseParticleRenderer.gameObject, baseParticleRenderer.transform.parent).GetComponent <Particles>();
                particleRenderer.gameObject.name = string.Format("{0}-{1}", baseParticleRenderer.gameObject.name, entry.Key);
                particleRenderer.frameData       = particleFrameData[entry.Key];
                //if (entry.Key == 0)
                //    particleRenderer.SetModel(particleType0Model);
                particleRenderer.gameObject.SetActive(true);
                particleRenderers.Add(entry.Key, particleRenderer);
            }

            frameSlider.maxValue = frameOffsets.Count - 1;
            ChangeFrame(0);
            //}
        }

        sw.Stop();
        Debug.Log("Frames indexed in " + ((double)sw.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency) + "s");
    }