<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharpGL
{
    // check http://www.cnblogs.com/bitzhuwei/p/CSharpGL-18-Picking-of-OneIndexBuffer.html
    partial class OneIndexPicker
    {
        /// <summary>
        /// 在所有可能的图元（lastVertexId匹配）中，
        /// 逐个测试，找到最接近摄像机的那个图元，
        /// 返回此图元的最后一个索引在<see cref="IndexBuffer"/>中的索引（位置）。
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="primitiveInfoList"></param>
        /// <returns></returns>
        private RecognizedPrimitiveInfo GetLastIndexId(
            PickingEventArgs arg,
            List<RecognizedPrimitiveInfo> primitiveInfoList)
        {
            if (primitiveInfoList == null || primitiveInfoList.Count == 0) { return null; }
#if DEBUG
            SameLengths(primitiveInfoList);
#endif
            if (primitiveInfoList[0].VertexIds.Length == 1)// picking a point.
            {
                return primitiveInfoList[0];
            }

            int current = 0;
#if DEBUG
            NoPrimitiveRestartIndex(primitiveInfoList);
#endif
            for (int i = 1; i < primitiveInfoList.Count; i++)
            {
                OneIndexBuffer twoPrimitivesIndexBuffer;
                uint lastIndex0, lastIndex1;
                AssembleIndexBuffer(
                    primitiveInfoList[current], primitiveInfoList[i], this.Renderer.PickingRenderUnit.VertexArrayObject.IndexBuffer.Mode,
                    out twoPrimitivesIndexBuffer, out lastIndex0, out lastIndex1);
                uint pickedIndex = Pick(arg, twoPrimitivesIndexBuffer);
                if (pickedIndex == lastIndex1)
                { current = i; }
                else if (pickedIndex == lastIndex0)
                { /* nothing to do */}
                else if (pickedIndex == uint.MaxValue)// 两个候选图元都没有被拾取到
                { /* nothing to do */}
                else
                { throw new Exception("This should not happen!"); }
            }

            return primitiveInfoList[current];
        }

        private void SameLengths(List<RecognizedPrimitiveInfo> primitiveInfoList)
        {
            int length = primitiveInfoList[0].VertexIds.Length;
            for (int i = 0; i < primitiveInfoList.Count; i++)
            {
                if (primitiveInfoList[i].VertexIds.Length != length)<commit_msg>fix bug in picking when picking on multiple primitives<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharpGL
{
    // check http://www.cnblogs.com/bitzhuwei/p/CSharpGL-18-Picking-of-OneIndexBuffer.html
    partial class OneIndexPicker
    {
        /// <summary>
        /// 在所有可能的图元（lastVertexId匹配）中，
        /// 逐个测试，找到最接近摄像机的那个图元，
        /// 返回此图元的最后一个索引在<see cref="IndexBuffer"/>中的索引（位置）。
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="primitiveInfoList"></param>
        /// <returns></returns>
        private RecognizedPrimitiveInfo GetLastIndexId(
            PickingEventArgs arg,
            List<RecognizedPrimitiveInfo> primitiveInfoList)
        {
            if (primitiveInfoList == null || primitiveInfoList.Count == 0) { return null; }
#if DEBUG
            SameLengths(primitiveInfoList);
#endif
            if (primitiveInfoList[0].VertexIds.Length == 1)// picking a point.
            {
                return primitiveInfoList[0];
            }

            int target = 0;
#if DEBUG
            NoPrimitiveRestartIndex(primitiveInfoList);
#endif
            for (int left = 0; left < primitiveInfoList.Count - 1; left++)
            {
                for (int right = left + 1; right < primitiveInfoList.Count; right++)
                {
                    OneIndexBuffer twoPrimitivesIndexBuffer;
                    uint leftLastIndex, rightLastIndex;
                    AssembleIndexBuffer(
                        primitiveInfoList[left], primitiveInfoList[right], this.Renderer.PickingRenderUnit.VertexArrayObject.IndexBuffer.Mode,
                        out twoPrimitivesIndexBuffer, out leftLastIndex, out rightLastIndex);
                    uint pickedIndex = Pick(arg, twoPrimitivesIndexBuffer);
                    if (pickedIndex == rightLastIndex)
                    {
                        target = right;
                        break;
                    }
                    else if (pickedIndex == leftLastIndex)
                    {
                        target = left;
                        break;
                    }
                    else if (pickedIndex == uint.MaxValue)// 两个候选图元都没有被拾取到
                    { /* nothing to do */}
                    else
                    { throw new Exception("This should not happen!"); }
                }
            }

            return primitiveInfoList[target];
        }

        private void SameLengths(List<RecognizedPrimitiveInfo> primitiveInfoList)
        {
            int length = primitiveInfoList[0].VertexIds.Length;
            for (int i = 0; i < primitiveInfoList.Count; i++)
            {
                if (primitiveInfoList[i].VertexIds.Length != length)
{'fraction_non_alphanumeric': 0.09797428905336969, 'fraction_numerical': 0.005453837164004675, 'mean_word_length': 1.6927110644992134, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3879160', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1392, 'n_words': 275}