Esempio n. 1
0
        //--------------------------------------------------------------
        #region Methods
        //--------------------------------------------------------------

        internal void Set(Vector3F[] vertices, Aabb aabb, Vector3F innerPoint, DirectionalLookupTableUInt16F directionalLookupTable, VertexAdjacency vertexAdjacency)
        {
            _vertices             = vertices;
            _aabbLocal            = aabb;
            _innerPoint           = innerPoint;
            _directionLookupTable = directionalLookupTable;
            _vertexAdjacency      = vertexAdjacency;
        }
Esempio n. 2
0
        /// <inheritdoc/>
        protected override void CloneCore(Shape sourceShape)
        {
            var source = (ConvexPolyhedron)sourceShape;

            _vertices             = source._vertices;
            _aabbLocal            = source._aabbLocal;
            _innerPoint           = source._innerPoint;
            _directionLookupTable = source._directionLookupTable;
            _vertexAdjacency      = source._vertexAdjacency;
        }
Esempio n. 3
0
        /// <summary>
        /// Colors graph vertices greedily processing them in ascending order of vertex degree.
        /// Variant of <see cref="ColorGreedily(Graph)"/> with another vertex processing order.
        /// </summary>
        /// <param name="graph">Graph to be colored.</param>
        /// <param name="adjacency"><paramref name="graph"/> adjacency to save calculations
        /// when doing multiple colorings.</param>
        /// <returns>Vertex coloring of provided graph.</returns>
        public static GraphColoring ColorSmallestFirst(this Graph graph, VertexAdjacency adjacency)
        {
            var verticesLf = graph.Vertices
                             .Select(v => v.ToDegreeTuple(adjacency))
                             .OrderBy(t => t.degree)
                             .Select(t => t.vertex)
                             .ToImmutableArray();

            return(graph.ColorGreedilyWithVertexOrder(verticesLf, adjacency));
        }
        /// <inheritdoc />
        /// <remarks>
        /// The <paramref name="current"/> instance will be replaced only if the factory was initialized with
        /// <c>canReplaceVertexInstanceOnModification</c> parameter set to <c>true</c>.
        /// </remarks>
        public bool AddEdge(int destVertexIndex, TEdgeData edgeData, ref VertexAdjacency <TEdgeData> current)
        {
            var res = GetReplaceable(current);

            if (!res.AddEdge(_factory, destVertexIndex, edgeData, out res))
            {
                return(false);
            }

            current = res;
            return(true);
        }
Esempio n. 5
0
        /// <summary>
        /// Colors graph vertices greedily processing them in order of <paramref name="verticesOrdered"/>.
        /// </summary>
        /// <param name="graph">Graph to be colored.</param>
        /// <param name="verticesOrdered">Ordering of graph vertices to use in algorithm.</param>
        /// <param name="adjacency"><paramref name="graph"/> adjacency to save calculations
        /// when doing multiple colorings.</param>
        /// <returns>Vertex coloring of provided graph.</returns>
        public static GraphColoring ColorGreedilyWithVertexOrder(this Graph graph, IEnumerable <Vertex> verticesOrdered,
                                                                 VertexAdjacency adjacency)
        {
            var maxDegree = adjacency.AdjacentVertices.Max(p => p.Value.Count);
            var colors    = Enumerable.Range(1, maxDegree + 2);
            var coloring  = new MutableGraphColoring(graph);

            foreach (var vertex in verticesOrdered)
            {
                var adjacentVertices = adjacency.AdjacentVertices[vertex];
                var usedColors       = adjacentVertices.Select(v => coloring[v]).Where(c => c != null).Select(c => c.Value);
                var color            = colors.Except(usedColors).First();
                coloring[vertex] = color;
            }
            return(coloring.ToImmutable());
        }
        private ReplaceableVertexAdjacency <TEdgeData> GetReplaceable(VertexAdjacency <TEdgeData> current)
        {
            if (!_canReplaceVertexInstanceOnModification)
            {
                return((ReplaceableVertexAdjacency <TEdgeData>)current);
            }

            if (current is ReplaceableVertexAdjacency <TEdgeData> replaceable)
            {
                return(replaceable);
            }

            return(current == null || current.EdgesCount <= 0
                                ? EmptyVertexAdjacency <TEdgeData> .Instance
                                : _factory.GetInstance(current, current.EdgesCount));
        }
        /// <inheritdoc />
        /// <remarks>
        /// The <paramref name="current"/> instance will be replaced only if the factory was initialized with
        /// <c>canReplaceVertexInstanceOnModification</c> parameter set to <c>true</c>.
        /// </remarks>
        public bool RemoveEdge(int destVertexIndex, ref VertexAdjacency <TEdgeData> current)
        {
            if (current == null || current.EdgesCount <= 0)
            {
                return(false);
            }

            var res = GetReplaceable(current);

            if (!res.RemoveEdge(_factory, destVertexIndex, out res))
            {
                return(false);
            }

            current = res;
            return(true);
        }
        /// <inheritdoc />
        /// <remarks>
        /// The <paramref name="current"/> instance will be replaced only if the factory was initialized with
        /// <c>canReplaceVertexInstanceOnModification</c> parameter set to <c>true</c>.
        /// </remarks>
        public int RemoveAllEdges(ref VertexAdjacency <TEdgeData> current)
        {
            var count = current?.EdgesCount ?? 0;

            if (count <= 0)
            {
                return(0);
            }

            if (!_canReplaceVertexInstanceOnModification)
            {
                var replaceable = GetReplaceable(current);
                replaceable.Reset();
                return(count);
            }

            current = Empty;
            return(count);
        }
        protected override void Write(ContentWriter output, ConvexPolyhedron value)
        {
            dynamic internals = value.Internals;
            DirectionalLookupTableF <ushort> directionalLookupTable = internals.DirectionalLookupTable;
            VertexAdjacency vertexAdjacency = internals.VertexAdjacency;

            var vertices         = value.Vertices;
            int numberOfVertices = vertices.Count;

            output.Write(numberOfVertices);
            for (int i = 0; i < numberOfVertices; i++)
            {
                output.WriteRawObject(vertices[i]);
            }

            output.WriteRawObject(value.GetAabb());
            output.WriteRawObject(value.InnerPoint);
            output.WriteObject(directionalLookupTable);
            output.WriteObject(vertexAdjacency);
        }
Esempio n. 10
0
        /// <summary>
        /// Colors graph using G.I.S (Greedy Independent Sets) algorithm. See remarks in <see cref="ColorGreedyIndependentSets(Graph)"/>.
        /// </summary>
        /// <param name="graph">Graph to be colored.</param>
        /// <param name="adjacency"><paramref name="graph"/> adjacency to save calculations
        /// when doing multiple colorings.</param>
        /// <returns>Vertex coloring of provided graph.</returns>
        public static GraphColoring ColorGreedyIndependentSets(this Graph graph, VertexAdjacency adjacency)
        {
            var maxDegree = adjacency.AdjacentVertices.Max(p => p.Value.Count);
            var color     = 1;
            var coloring  = new MutableGraphColoring(graph);

            while (coloring.VertexColors.Count < graph.Vertices.Count)
            {
                var induced          = adjacency.InducedSubgraphByRemovingVertices(coloring.VertexColors.Keys);
                var inducedAdjacency = induced.Adjacency();
                while (induced.Subgraph.Vertices.Count > 0)
                {
                    var vertex = induced.Subgraph.MinDegreeVertex(inducedAdjacency.VertexDegrees());
                    coloring[vertex] = color;
                    induced          = inducedAdjacency.InducedSubgraphByRemovingVertices(inducedAdjacency.AdjacentVertices[vertex].Add(vertex));
                    inducedAdjacency = induced.Adjacency();
                }
                color++;
            }
            return(coloring.ToImmutable());
        }
Esempio n. 11
0
        public void SerializationBinary()
        {
            // Create a dummy mesh.
              var randomPoints = Enumerable.Range(0, 100)
                                   .Select(i => RandomHelper.Random.NextVector3F(-100, 100));
              var mesh = GeometryHelper.CreateConvexHull(randomPoints);
              VertexAdjacency vertexAdjacency = new VertexAdjacency(mesh);

              // Serialize object.
              var stream = new MemoryStream();
              var formatter = new BinaryFormatter();
              formatter.Serialize(stream, vertexAdjacency);

              // Deserialize object.
              stream.Position = 0;
              var deserializer = new BinaryFormatter();
              var vertexAdjacency2 = (VertexAdjacency)deserializer.Deserialize(stream);

              Assert.That(vertexAdjacency2.ListIndices, Is.EqualTo(vertexAdjacency.ListIndices));
              Assert.That(vertexAdjacency2.Lists, Is.EqualTo(vertexAdjacency.Lists));
        }
Esempio n. 12
0
        public void SerializationBinary()
        {
            // Create a dummy mesh.
            var randomPoints = Enumerable.Range(0, 100)
                               .Select(i => RandomHelper.Random.NextVector3F(-100, 100));
            var             mesh            = GeometryHelper.CreateConvexHull(randomPoints);
            VertexAdjacency vertexAdjacency = new VertexAdjacency(mesh);

            // Serialize object.
            var stream    = new MemoryStream();
            var formatter = new BinaryFormatter();

            formatter.Serialize(stream, vertexAdjacency);

            // Deserialize object.
            stream.Position = 0;
            var deserializer     = new BinaryFormatter();
            var vertexAdjacency2 = (VertexAdjacency)deserializer.Deserialize(stream);

            Assert.That(vertexAdjacency2.ListIndices, Is.EqualTo(vertexAdjacency.ListIndices));
            Assert.That(vertexAdjacency2.Lists, Is.EqualTo(vertexAdjacency.Lists));
        }
        protected override ConvexPolyhedron Read(ContentReader input, ConvexPolyhedron existingInstance)
        {
            if (existingInstance == null)
            {
                existingInstance = new ConvexPolyhedron();
            }

            int numberOfVertices = input.ReadInt32();

            Vector3F[] vertices = new Vector3F[numberOfVertices];
            for (int i = 0; i < numberOfVertices; i++)
            {
                vertices[i] = input.ReadRawObject <Vector3F>();
            }

            Aabb            aabb                   = input.ReadRawObject <Aabb>();
            Vector3F        innerPoint             = input.ReadRawObject <Vector3F>();
            var             directionalLookupTable = input.ReadObject <DirectionalLookupTableUInt16F>();
            VertexAdjacency vertexAdjacency        = input.ReadObject <VertexAdjacency>();

            existingInstance.Set(vertices, aabb, innerPoint, directionalLookupTable, vertexAdjacency);

            return(existingInstance);
        }
Esempio n. 14
0
        /// <summary>
        /// Colors graph vertices greedily processing them in order of <see cref="Graph.Vertices"/>.
        /// </summary>
        /// <param name="graph">Graph to be colored.</param>
        /// <returns>Vertex coloring of provided graph.</returns>
        public static GraphColoring ColorGreedily(this Graph graph)
        {
            var adjacency = new VertexAdjacency(graph);

            return(graph.ColorGreedily(adjacency));
        }
Esempio n. 15
0
 private void BuildVertexAdjacencyLists(DcelMesh convexHull)
 {
     _vertexAdjacency = new VertexAdjacency(convexHull);
 }
Esempio n. 16
0
 public VertexAdjacencyMockAssembler <TEdgeData> Build(out VertexAdjacency <TEdgeData> mockedObject)
 {
     mockedObject = Build().Object;
     return(this);
 }
Esempio n. 17
0
 private static (Vertex vertex, int degree) ToDegreeTuple(this Vertex v, VertexAdjacency adjacency)
 {
     return(v, adjacency.AdjacentVertices[v].Count);
 }
Esempio n. 18
0
 /// <summary>
 /// Colors graph vertices greedily processing them in order of <see cref="Graph.Vertices"/>.
 /// </summary>
 /// <param name="graph">Graph to be colored.</param>
 /// <param name="adjacency"><paramref name="graph"/> adjacency to save calculations
 /// when doing multiple colorings.</param>
 /// <returns>Vertex coloring of provided graph.</returns>
 public static GraphColoring ColorGreedily(this Graph graph, VertexAdjacency adjacency)
 {
     return(graph.ColorGreedilyWithVertexOrder(graph.Vertices, adjacency));
 }