Beispiel #1
0
        private void EnsurePositionsAndDerived()
        {
            if (m_ensuredPositionsAndDerived)
            {
                return;
            }

            var result = GetSubArray(Octree.PositionsLocal3f, Node.Positions);

            m_cache[Octree.PositionsLocal3f.Id] = result;
            var psLocal = result.Value;

            var c        = Center;
            var psGlobal = psLocal.Map(p => (V3d)p + c);

            m_cache[Octree.PositionsGlobal3d.Id] = psGlobal;

            var bboxLocal = psLocal.Length > 0 ? new Box3f(psLocal) : Box3f.Invalid;

            m_cache[Octree.BoundingBoxExactLocal.Id] = bboxLocal;

            var kd     = psLocal.BuildKdTree();
            var pRefKd = new PersistentRef <PointRkdTreeF <V3f[], V3f> >(Guid.NewGuid().ToString(), _ => kd, _ => (true, kd));

            m_cache[Octree.PointRkdTreeFData.Id] = pRefKd;

            m_ensuredPositionsAndDerived = true;
        }
Beispiel #2
0
 /// <summary>
 /// Creates pointset from given root cell.
 /// </summary>
 public PointSet(Storage storage, string key, Guid?rootCellId, long splitLimit)
 {
     Storage    = storage;
     Id         = key ?? throw new ArgumentNullException(nameof(key));
     SplitLimit = splitLimit;
     if (rootCellId.HasValue)
     {
         Root = new PersistentRef <PointSetNode>(rootCellId.ToString(), storage.GetPointSetNode);
     }
 }
Beispiel #3
0
        private PersistentRef <T[]> GetSubArray <T>(Def def, PersistentRef <T[]> originalValue)
        {
            if (m_cache.TryGetValue(def.Id, out var o) && o is PersistentRef <T[]> x)
            {
                return(x);
            }

            if (originalValue == null)
            {
                return(null);
            }
            // should be empty not null, right?
            if (m_activePoints == null)
            {
                return(originalValue);
            }

            var key    = (Id + originalValue.Id).ToGuid().ToString();
            var xs     = originalValue.Value.Where((_, i) => m_activePoints.Contains(i)).ToArray();
            var result = new PersistentRef <T[]>(key, _ => xs, _ => (true, xs));

            m_cache[def.Id] = result;
            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Creates pointset from given root cell.
        /// </summary>
        public PointSet(Storage storage, string key, IPointCloudNode root, int splitLimit)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }

            Storage    = storage;
            Id         = key ?? throw new ArgumentNullException(nameof(key));
            SplitLimit = splitLimit;

            if (key != null)
            {
                Root = new PersistentRef <IPointCloudNode>(root.Id.ToString(), storage.GetPointCloudNode, storage.TryGetPointCloudNode);
            }
        }
        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
                           ));
            }
        }