Esempio n. 1
0
        IEnumerator BuildFragments()
        {
            GenerateCodeBooks();

            allocator.Allocate(codeBooks.Count, ref Binary.codeBooks);

            int codeBookIndex = 0;

            foreach (var codeBook in codeBooks)
            {
                var metricIndex = codeBook.metric.index;
                var traitIndex  = codeBook.traitIndex;

                MemoryRef <Binary.CodeBook> destination = new MemoryRef <Binary.CodeBook>(ref Binary.codeBooks[codeBookIndex]);

                destination.Ref.metricIndex = metricIndex;
                destination.Ref.traitIndex  = traitIndex;

                var numIntervals = codeBook.intervals.Count;

                allocator.Allocate(numIntervals, ref destination.Ref.intervals);

                int intervalIndex = 0;

                foreach (var interval in codeBook.intervals)
                {
                    destination.Ref.intervals[intervalIndex] = interval.index;

                    intervalIndex++;
                }

                Assert.IsTrue(intervalIndex == codeBook.intervals.Count);

                {
                    FragmentFactory fragmentFactory = PoseFragmentFactory.Create();

                    using (CodeBook.FragmentEncoder factory = codeBook.CreateEncoder(ref Binary))
                    {
                        IEnumerator fragmentEncoder = factory.EncodeFragments(binary, codeBook.intervals.ToArray(), fragmentFactory, "pose", progressInfo => progressFeedback?.Invoke(progressInfo));
                        while (fragmentEncoder.MoveNext())
                        {
                            yield return(null);

                            if (bCancel)
                            {
                                factory.CancelEncoding();
                                fragmentEncoder.MoveNext(); // release resources

                                yield break;
                            }
                        }

                        int numFragments = factory.numFragments;

                        Assert.IsTrue(destination.Ref.numFragments == 0);

                        destination.Ref.numFragments = numFragments;

                        var numFeatures = PoseFragment.GetNumFeatures(ref Binary, metricIndex);

                        var numQuantizedFeatures = PoseFragment.GetNumQuantizedFeatures(ref Binary, metricIndex);

                        var numNormalizedFeatures = PoseFragment.GetNumNormalizedFeatures(ref Binary, metricIndex);

                        var numTransformedFeatures = numFeatures - numQuantizedFeatures - numNormalizedFeatures;

                        destination.Ref.poses.numFragments           = numFragments;
                        destination.Ref.poses.numFeatures            = (short)numFeatures;
                        destination.Ref.poses.numFeaturesQuantized   = (short)numQuantizedFeatures;
                        destination.Ref.poses.numFeaturesNormalized  = (short)numNormalizedFeatures;
                        destination.Ref.poses.numFeaturesTransformed = (short)numTransformedFeatures;

                        var numCodes = factory.Codes.Length + factory.QuantizedValues.Length;

                        allocator.Allocate(numCodes, ref destination.Ref.poses.codes);

                        int writeIndex         = 0;
                        int readIndexQuantized = 0;
                        int readIndex          = 0;

                        for (int i = 0; i < numFragments; ++i)
                        {
                            for (int j = 0; j < numQuantizedFeatures; ++j)
                            {
                                var quantizedValue = factory.QuantizedValues[readIndexQuantized++];

                                destination.Ref.poses.codes[writeIndex++] = quantizedValue;
                            }

                            for (int j = 0; j < numFeatures; ++j)
                            {
                                var code = factory.Codes[readIndex++];

                                destination.Ref.poses.codes[writeIndex++] = code;
                            }
                        }

                        Assert.IsTrue(readIndexQuantized == factory.QuantizedValues.Length);
                        Assert.IsTrue(readIndex == factory.Codes.Length);
                        Assert.IsTrue(writeIndex == numCodes);

                        var numCodeWords = factory.CodeWords.Length;

                        allocator.Allocate(numCodeWords, ref destination.Ref.poses.centroids);

                        for (int i = 0; i < numCodeWords; ++i)
                        {
                            destination.Ref.poses.centroids[i] = factory.CodeWords[i];
                        }

                        allocator.Allocate(numTransformedFeatures, ref destination.Ref.poses.boundingBoxes);

                        Assert.IsTrue(numTransformedFeatures == factory.BoundingBoxes.Length);

                        for (int i = 0; i < numTransformedFeatures; ++i)
                        {
                            destination.Ref.poses.boundingBoxes[i].transform       = factory.BoundingBoxes[i].transform;
                            destination.Ref.poses.boundingBoxes[i].extent          = factory.BoundingBoxes[i].extent;
                            destination.Ref.poses.boundingBoxes[i].inverseDiagonal = factory.BoundingBoxes[i].inverseDiagonal;
                        }

                        allocator.Allocate(numQuantizedFeatures, ref destination.Ref.poses.quantizers);

                        Assert.IsTrue(numQuantizedFeatures == factory.Quantizers.Length);

                        for (int i = 0; i < numQuantizedFeatures; ++i)
                        {
                            destination.Ref.poses.quantizers[i].minimum = factory.Quantizers[i].minimum;
                            destination.Ref.poses.quantizers[i].range   = factory.Quantizers[i].range;
                        }
                    }
                }

                {
                    FragmentFactory fragmentFactory = TrajectoryFragmentFactory.Create();

                    using (CodeBook.FragmentEncoder factory = codeBook.CreateEncoder(ref Binary))
                    {
                        IEnumerator fragmentEncoder = factory.EncodeFragments(binary, codeBook.intervals.ToArray(), fragmentFactory, "trajectory", progressInfo => progressFeedback?.Invoke(progressInfo));
                        while (fragmentEncoder.MoveNext())
                        {
                            yield return(null);

                            if (bCancel)
                            {
                                factory.CancelEncoding();
                                fragmentEncoder.MoveNext(); // release resources

                                yield break;
                            }
                        }

                        int numFragments = factory.numFragments;

                        Assert.IsTrue(destination.Ref.numFragments == numFragments);

                        var numFeatures = TrajectoryFragment.GetNumFeatures(ref Binary, metricIndex);

                        var numQuantizedFeatures = TrajectoryFragment.GetNumQuantizedFeatures(ref Binary, metricIndex);

                        var numNormalizedFeatures = TrajectoryFragment.GetNumNormalizedFeatures(ref Binary, metricIndex);

                        var numTransformedFeatures = numFeatures - numQuantizedFeatures - numNormalizedFeatures;

                        destination.Ref.trajectories.numFragments           = numFragments;
                        destination.Ref.trajectories.numFeatures            = (short)numFeatures;
                        destination.Ref.trajectories.numFeaturesQuantized   = (short)numQuantizedFeatures;
                        destination.Ref.trajectories.numFeaturesNormalized  = (short)numNormalizedFeatures;
                        destination.Ref.trajectories.numFeaturesTransformed = (short)numTransformedFeatures;

                        var numCodes = factory.Codes.Length + factory.QuantizedValues.Length;

                        allocator.Allocate(numCodes, ref destination.Ref.trajectories.codes);

                        int writeIndex         = 0;
                        int readIndexQuantized = 0;
                        int readIndex          = 0;

                        for (int i = 0; i < numFragments; ++i)
                        {
                            for (int j = 0; j < numQuantizedFeatures; ++j)
                            {
                                var quantizedValue = factory.QuantizedValues[readIndexQuantized++];

                                destination.Ref.trajectories.codes[writeIndex++] = quantizedValue;
                            }

                            for (int j = 0; j < numFeatures; ++j)
                            {
                                var code = factory.Codes[readIndex++];

                                destination.Ref.trajectories.codes[writeIndex++] = code;
                            }
                        }

                        Assert.IsTrue(readIndexQuantized == factory.QuantizedValues.Length);
                        Assert.IsTrue(readIndex == factory.Codes.Length);
                        Assert.IsTrue(writeIndex == numCodes);

                        var numCodeWords = factory.CodeWords.Length;

                        allocator.Allocate(numCodeWords, ref destination.Ref.trajectories.centroids);

                        for (int i = 0; i < numCodeWords; ++i)
                        {
                            destination.Ref.trajectories.centroids[i] = factory.CodeWords[i];
                        }

                        allocator.Allocate(numTransformedFeatures, ref destination.Ref.trajectories.boundingBoxes);

                        Assert.IsTrue(numTransformedFeatures == factory.BoundingBoxes.Length);

                        for (int i = 0; i < numTransformedFeatures; ++i)
                        {
                            destination.Ref.trajectories.boundingBoxes[i].transform       = factory.BoundingBoxes[i].transform;
                            destination.Ref.trajectories.boundingBoxes[i].extent          = factory.BoundingBoxes[i].extent;
                            destination.Ref.trajectories.boundingBoxes[i].inverseDiagonal = factory.BoundingBoxes[i].inverseDiagonal;
                        }

                        allocator.Allocate(numQuantizedFeatures, ref destination.Ref.trajectories.quantizers);

                        Assert.IsTrue(numQuantizedFeatures == factory.Quantizers.Length);

                        for (int i = 0; i < numQuantizedFeatures; ++i)
                        {
                            destination.Ref.trajectories.quantizers[i].minimum = factory.Quantizers[i].minimum;
                            destination.Ref.trajectories.quantizers[i].range   = factory.Quantizers[i].range;
                        }
                    }
                }

                codeBookIndex++;
            }

            Assert.IsTrue(codeBookIndex == codeBooks.Count);
        }
Esempio n. 2
0
 public int GetNumNormalizedFeatures(ref Binary binary, MetricIndex metricIndex)
 {
     return(TrajectoryFragment.GetNumNormalizedFeatures(ref binary, metricIndex));
 }