Пример #1
0
        public override void Encode(DetectionResult result)
        {
            int tagChannel = m_NumChannels * m_CrntStackIndex;

            m_Grid.ClearChannels(tagChannel, m_NumChannels);

            for (int iTag = 0, nTag = result.DetectableTags.Count; iTag < nTag; iTag++)
            {
                string tag    = result.DetectableTags[iTag];
                bool   hasObs = m_Settings.HasObservations(tag, out IList <int> obsIndices);

                if (result.TryGetItems(tag, out IList <DetectionResult.Item> items))
                {
                    ShapeModifier modifier = m_ModifiersByTag[tag];

                    for (int iItem = 0, nItem = items.Count; iItem < nItem; iItem++)
                    {
                        var item    = items[iItem];
                        int channel = tagChannel;
                        modifier.Clear();

                        // Distance.
                        for (int iPoint = 0, nPoint = item.Points.Count; iPoint < nPoint; iPoint++)
                        {
                            Vector3    p   = item.Points[iPoint];
                            Vector2Int pos = m_Grid.NormalizedToGridPos(p);

                            // Inverts z -> 0 (far) to 1 (near).
                            float norm = m_DistanceNormalization.Evaluate(p.z);
                            float prev = m_Grid.Read(channel, pos);
                            // Override if closer.
                            if (norm > prev)
                            {
                                m_Grid.Write(channel, pos, norm);
                                modifier.AddPoint(pos, p.z); // z: 0 (near) to 1 (far).
                            }
                            // else: ignore occluded.
                        }

                        modifier.Process(m_Grid, channel);
                        channel++;

                        // Additional observations.
                        if (hasObs)
                        {
                            for (int iObs = 0, nObs = obsIndices.Count; iObs < nObs; iObs++)
                            {
                                // The modifier already contains the required grid positions,
                                // even if we haven't applied any modifications (ShapeModNone).
                                modifier.Write(m_Grid, channel,
                                               item.Detectable.Observations.Evaluate(obsIndices[iObs]));
                                channel++;
                            }
                        }
                    }
                }

                tagChannel += hasObs ? obsIndices.Count + 1 : 1;
            }

            IncrementStackIndex();
        }
Пример #2
0
        /// <inheritdoc/>
        public void Encode(DetectionResult result)
        {
            m_GridBuffer.Clear();

            if (m_Debug_IsEnabled)
            {
                m_Debug_ChannelData.ClearGridPositions();
            }

            int firstTagChannel = 0;

            // TODO Too many nested loops.

            foreach (var tag in result.DetectableTags)
            {
                // Observables for current tag.
                var tagObs = m_Settings.GetObservables(tag);

                if (result.TryGetItems(tag, out IList <DetectionResult.Item> tagItems))
                {
                    // Has detection result for current tag -> get matching modifier.
                    var modifier = m_PointModifiersByTag[tag];

                    foreach (var item in tagItems)
                    {
                        modifier.Reset();
                        int channel = firstTagChannel;

                        // Iterate observables for current result item.
                        foreach (var obs in tagObs)
                        {
                            // We evaluate the observable here and write obsValue
                            // to all grid positions below, UNLESS it's distance.
                            bool encodeDistance = obs.Evaluate(
                                item.Detectable, out float obsValue)
                                                  == ObservableType.Distance;

                            if (!modifier.HasGridPositions)
                            {
                                if (item.HasPoints)
                                {
                                    // Is first observable we have any points for.

                                    foreach (var point in item.NormPoints)
                                    {
                                        // Normalized item point -> grid position.
                                        Vector2Int pos = m_GridBuffer.NormalizedToGridPos(point);

                                        if (encodeDistance)
                                        {
                                            // Ignore obsValue (is 0 for distance).

                                            // (Weighted) inverse distance, 1 (near) - 0 (far).
                                            float proximity = m_Normalization.Evaluate(point.z);

                                            // Override if closer.
                                            if (proximity > m_GridBuffer.Read(channel, pos))
                                            {
                                                // Write unmodified position to buffer.
                                                m_GridBuffer.Write(channel, pos, proximity);
                                                // Write unmodified position to modifier.
                                                modifier.AddPosition(pos, 1 - point.z); // unweighted
                                            }
                                            // else: ignore occluded.
                                        }
                                        else
                                        {
                                            // Write unmodified position to buffer.
                                            m_GridBuffer.Write(channel, pos, obsValue);

                                            // Write unmodified position to modifier.
                                            // NOTE Need some proximity value for PointModDilation.
                                            // Passing 0.5 to get a visible result, 0 does nothing.
                                            modifier.AddPosition(pos, 0.5f);
                                        }
                                    }

                                    // Will write additional positions to buffer.
                                    // NOTE So far, all point modifiers are only ADDING positions,
                                    // but that might not be the case for future modifiers.
                                    modifier.Process(m_GridBuffer, channel);

                                    if (m_Debug_IsEnabled)
                                    {
                                        // Store modified grid positions.
                                        m_Debug_ChannelData.AddGridPositions(channel, modifier.GridPositions);
                                    }
                                }
                            }
                            else
                            {
                                // Is additional observable.
                                //
                                // The modifier already contains the required grid positions,
                                // even if we haven't applied any modifications (PointModNone).
                                //
                                // The reason for requiring distance as the first observable,
                                // is that we write obsValue to ALL stored grid positions here.
                                // Distance is the only observable with individual position values.
                                //
                                // Will write all positions to buffer.
                                modifier.Write(m_GridBuffer, channel, obsValue);

                                if (m_Debug_IsEnabled)
                                {
                                    // Store modified grid positions.
                                    m_Debug_ChannelData.AddGridPositions(channel, modifier.GridPositions);
                                }
                            }

                            // Next observable...
                            channel++;
                        }
                    }
                }

                firstTagChannel += tagObs.Count();
            }
        }