Пример #1
0
        private PointSetNode(Guid id,
                             Cell cell, long pointCountTree,
                             Guid?psId, Guid?csId, Guid?kdId, Guid?nsId, Guid?isId,
                             Guid?lodPsId, Guid?lodCsId, Guid?lodKdId, Guid?lodNsId, Guid?lodIsId,
                             Guid?[] subnodeIds, Storage storage, bool writeToStore
                             )
        {
            if (subnodeIds != null && subnodeIds.Count(x => x.HasValue) > 0 && pointCountTree == 0)
            {
                throw new ArgumentException(nameof(pointCountTree), "Must not be 0 for inner nodes.");
            }

            Storage        = storage;
            Id             = id;
            Cell           = cell;
            PointCountTree = pointCountTree;
            SubnodeIds     = subnodeIds;

            if (psId.HasValue)
            {
                Attributes[C_POSITIONS] = psId.Value;
            }
            if (csId.HasValue)
            {
                Attributes[C_COLORS] = csId.Value;
            }
            if (kdId.HasValue)
            {
                Attributes[C_KDTREE] = kdId.Value;
            }
            if (nsId.HasValue)
            {
                Attributes[C_NORMALS] = nsId.Value;
            }
            if (isId.HasValue)
            {
                Attributes[C_INTENSITIES] = isId.Value;
            }
            if (lodPsId.HasValue)
            {
                Attributes[C_LOD_POSITIONS] = lodPsId.Value;
            }
            if (lodCsId.HasValue)
            {
                Attributes[C_LOD_COLORS] = lodCsId.Value;
            }
            if (lodKdId.HasValue)
            {
                Attributes[C_LOD_KDTREE] = lodKdId.Value;
            }
            if (lodNsId.HasValue)
            {
                Attributes[C_LOD_NORMALS] = lodNsId.Value;
            }
            if (lodIsId.HasValue)
            {
                Attributes[C_LOD_INTENSITIES] = lodIsId.Value;
            }

            if (IsLeaf && PointCount != PointCountTree)
            {
                throw new InvalidOperationException();
            }

            if (psId != null)
            {
                Positions = new PersistentRef <V3f[]>(psId.ToString(), storage.GetV3fArray);
            }
            if (csId != null)
            {
                Colors = new PersistentRef <C4b[]>(csId.ToString(), storage.GetC4bArray);
            }
            if (kdId != null)
            {
                KdTree = new PersistentRef <PointRkdTreeD <V3f[], V3f> >(kdId.ToString(), LoadKdTree);
            }
            if (nsId != null)
            {
                Normals = new PersistentRef <V3f[]>(nsId.ToString(), storage.GetV3fArray);
            }
            if (isId != null)
            {
                Intensities = new PersistentRef <int[]>(isId.ToString(), storage.GetIntArray);
            }
            if (lodPsId != null)
            {
                LodPositions = new PersistentRef <V3f[]>(lodPsId.ToString(), storage.GetV3fArray);
            }
            if (lodCsId != null)
            {
                LodColors = new PersistentRef <C4b[]>(lodCsId.ToString(), storage.GetC4bArray);
            }
            if (lodKdId != null)
            {
                LodKdTree = new PersistentRef <PointRkdTreeD <V3f[], V3f> >(lodKdId.ToString(), LoadKdTree);
            }
            if (lodNsId != null)
            {
                LodNormals = new PersistentRef <V3f[]>(lodNsId.ToString(), storage.GetV3fArray);
            }
            if (lodIsId != null)
            {
                LodIntensities = new PersistentRef <int[]>(lodIsId.ToString(), storage.GetIntArray);
            }

            if (subnodeIds != null)
            {
                Subnodes = new PersistentRef <PointSetNode> [8];
                for (var i = 0; i < 8; i++)
                {
                    if (subnodeIds[i] == null)
                    {
                        continue;
                    }
                    var pRef = new PersistentRef <PointSetNode>(subnodeIds[i].ToString(), storage.GetPointSetNode);
                    Subnodes[i] = pRef;

#if DEBUG && PEDANTIC
                    var subNodeIndex = pRef.Value.Cell;
                    if (Cell.GetOctant(i) != subNodeIndex)
                    {
                        throw new InvalidOperationException();
                    }
                    if (!Cell.Contains(subNodeIndex))
                    {
                        throw new InvalidOperationException();
                    }
                    if (Cell.Exponent != subNodeIndex.Exponent + 1)
                    {
                        throw new InvalidOperationException();
                    }
#endif
                }
#if DEBUG && PEDANTIC
                if (PointCountTree != PointCount + Subnodes.Map(x => x?.Value != null ? x.Value.PointCountTree : 0).Sum())
                {
                    throw new InvalidOperationException();
                }
#endif
            }

            BoundingBox = Cell.BoundingBox;
            Center      = BoundingBox.Center;
            Corners     = BoundingBox.ComputeCorners();

            if (writeToStore)
            {
                storage.Add(Id.ToString(), this, CancellationToken.None);
            }

#if DEBUG
            if (PositionsId == null && PointCount != 0)
            {
                throw new InvalidOperationException();
            }
#if PEDANTIC
            if (PositionsId != null && Positions.Value.Length != PointCount)
            {
                throw new InvalidOperationException();
            }
#endif
            if (IsLeaf)
            {
                if (PositionsId == null)
                {
                    throw new InvalidOperationException();
                }
                if (KdTreeId == null)
                {
                    throw new InvalidOperationException();
                }
            }
            else
            {
                if (PositionsId != null)
                {
                    throw new InvalidOperationException();
                }
                if (ColorsId != null)
                {
                    throw new InvalidOperationException();
                }
                if (KdTreeId != null)
                {
                    throw new InvalidOperationException();
                }
                if (NormalsId != null)
                {
                    throw new InvalidOperationException();
                }
                if (IntensitiesId != null)
                {
                    throw new InvalidOperationException();
                }
            }
#endif
            PointRkdTreeD <V3f[], V3f> LoadKdTree(string key, CancellationToken ct)
            {
                var data = Storage.GetPointRkdTreeDData(key, ct);
                var ps   = Positions.Value;

                return(new PointRkdTreeD <V3f[], V3f>(
                           3, ps.Length, ps,
                           (xs, i) => xs[(int)i], (v, i) => (float)v[i],
                           (a, b) => V3f.Distance(a, b), (i, a, b) => b - a,
                           (a, b, c) => VecFun.DistanceToLine(a, b, c), VecFun.Lerp, 1e-9,
                           data
                           ));
            }
        }