Ejemplo n.º 1
0
        public static INewGraph KruskalMST(INewGraph graph)
        {
            LogTool.AssertIsTrue(graph.Edges.First() is IWeightedEdge);

            var ret          = graph.Factory.CreateGraph();
            var edges        = graph.Edges.OrderBy(e => (e as IWeightedEdge).Weight);
            var currentEdges = new List <IEdge>();

            foreach (var e in edges)
            {
                currentEdges.Add(e);
                if (GraphTools.HasCircle(currentEdges))
                {
                    currentEdges.RemoveAt(currentEdges.Count - 1);
                }
                else
                {
                    var nv1 = ret.AddVertex(e.Vertex.Clone() as IVertex);
                    var nv2 = ret.AddVertex(e.OtherVertex.Clone() as IVertex);
                    ret.AddEdge(nv1, nv2);
                }
            }

            return(ret);
        }
Ejemplo n.º 2
0
        public void RunTest()
        {
            var p1 = new Point()
            {
                Position = new float3(0, 0, 0)
            };
            var p2 = new Point()
            {
                Position = new float3(1, 1, 0)
            };
            var p3 = new Point()
            {
                Position = new float3(0, 1, 0)
            };
            var p4 = new Point()
            {
                Position = new float3(1, 0, 0)
            };


            var p5 = new Point()
            {
                Position = new float3(1, 0, 0)
            };
            var p6 = new Point()
            {
                Position = new float3(2, 0, 0)
            };

            LogTool.AssertIsTrue(GeometryTools.IsLineSegmentIntersection(p1, p2, p3, p4));
            LogTool.AssertIsFalse(GeometryTools.IsLineSegmentIntersection(p1, p2, p5, p6));
        }
Ejemplo n.º 3
0
        public static bool IsLineSegmentIntersection(IPoint a1, IPoint a2, IPoint b1, IPoint b2)
        {
            LogTool.AssertIsTrue(a1 != a2);
            LogTool.AssertIsTrue(b1 != b2);

            LogTool.AssertIsFalse(a1 == b1 && a2 == b2);

            var da = a2.Position - a1.Position;
            var db = b2.Position - b1.Position;
            var dc = b1.Position - a1.Position;

            if (math.dot(dc, math.cross(da, db)) != 0)
            {
                return(false);
            }

            var n2 = Norm2(math.cross(da, db));

            var s = math.dot(math.cross(dc, db), math.cross(da, db)) / n2;
            var t = math.dot(math.cross(dc, da), math.cross(da, db)) / n2;

            if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
            {
                return(true);
            }


            return(false);
        }
Ejemplo n.º 4
0
        public int Random()
        {
            LogTool.AssertIsTrue(this.sum.Count > 0);

            var total = this.sum.Last();
            var rand  = UnityEngine.Random.Range(0, total);

            var l = 0;
            var r = this.sum.Count;

            while (l < r)
            {
                var mid = l + (r - l) / 2;
                if (this.sum[mid] < rand)
                {
                    l = mid + 1;
                }
                else
                {
                    r = mid;
                }
            }

            return(r);
        }
Ejemplo n.º 5
0
        protected void UpdateReductionBuffer(Texture source)
        {
            LogTool.AssertIsTrue(Mathf.IsPowerOfTwo(source.width));
            LogTool.AssertIsTrue(Mathf.IsPowerOfTwo(source.height));

            this.CleanUp();

            var w = source.width;
            var h = source.height;

            for (; w > 0 || h > 0; w = w >> 1, h = h >> 1)
            {
                // Debug.Log(w + " " + h);

                var tw = math.max(1, w);
                var th = math.max(1, h);
                var p  = math.max(w, h);

                // Debug.Log(p + " with " + tw + "x" + th);

                var desc = new RenderTextureDescriptor(tw, th, RenderTextureFormat.ARGBFloat);
                desc.enableRandomWrite = true;
                var tex = TextureManager.Create(desc);

                this.reductionBuffer.Add(p, tex);
            }
            Graphics.CopyTexture(source, this.reductionBuffer[math.max(source.width, source.height)]);

            this.resultBuffer.InitBuffer(1, true, false);
        }
Ejemplo n.º 6
0
        public override void InitBuffer(GPUBufferVariable <T> other)
        {
            LogTool.Log("Use InitAppendBuffer(GPUBufferAppendConsume<T> other)", LogLevel.Warning);
            LogTool.AssertIsTrue(other is GPUBufferAppendConsume <T>);

            this.InitAppendBuffer(other as GPUBufferAppendConsume <T>);
        }
Ejemplo n.º 7
0
        public void ResetCounter(uint counter = 0)
        {
            LogTool.AssertIsTrue(this.Size > 0);
            this.Data.SetCounterValue(counter);

            this.CounterBuffer.ClearData();
        }
        public Intension Generate(Perception perception, Habits habits, MentalState mental, PhysicalState ps, float t)
        {
            Intension intension  = default;
            var       sensorData = perception.GetSensorData();
            var       distance   = sensorData.closestDangerObj == null ? -1 : sensorData.closestDangerObj.distance;

            if (distance > 0)
            {
                var mono = sensorData.closestDangerObj.obj as MonoBehaviour;
                intension = new AvoidIntension(mono.gameObject);

                if (this.intensionStack.Count == 0 || this.intensionStack.Last.Value.IntensionType != Intension.Type.Avoid)
                {
                    this.intensionStack.AddLast(intension);
                }
            }
            else
            {
                //fear of most dangerous predator m
                var p  = sensorData.GetClosestByType(ObjectType.Predator);
                var Fm = p != null?ps.Fi(p.distance) : 0;

                var F = mental.fear;
                if (F > this.f0)
                {
                    if (Fm < this.f1 && habits.schooling)
                    {
                        intension = new SchoolIntension();
                    }
                    else
                    {
                        var mono = p.obj as MonoBehaviour;
                        intension = new EscapedIntension(mono.gameObject);
                    }
                }
                else
                {
                    if (this.IsLastEatOrMate())
                    {
                        intension = this.intensionStack.Last.Value;
                        this.intensionStack.RemoveLast();
                    }
                    else
                    {
                        intension = this.GenerateNewIntension(perception, habits, mental, ps, t);
                    }
                }
            }

            if (this.intensionStack.Count > 1)
            {
                this.intensionStack.RemoveFirst();
            }


            LogTool.AssertIsTrue(intension != null);

            return(intension);
        }
        protected void OnEnable()
        {
            LogTool.AssertIsTrue(this.computeShader != null);

            this.dispatcher = new ComputeShaderDispatcher(this.computeShader);
            this.dispatcher.AddParameter(WindowKernel, this.data);
            this.dispatcher.AddParameter(MaxKernel, this.data);
        }
Ejemplo n.º 10
0
        public void InitAppendBuffer(int size, bool autoSet = false)
        {
            LogTool.AssertIsTrue(size > 0);

            //no cpu data for append buffer
            base.InitBuffer(size, false, autoSet, ComputeBufferType.Append);
            this.ResetCounter();
        }
Ejemplo n.º 11
0
        public void InitAppendBuffer(GPUBufferAppendConsume <T> other)
        {
            LogTool.AssertIsTrue(other.Size > 0);

            //no cpu data for append buffer
            base.InitBuffer(other);
            this.ResetCounter();
        }
Ejemplo n.º 12
0
 public static void CalculateVerticeWeight(INewGraph graph)
 {
     debug.Clear();
     foreach (var v in graph.Vertices)
     {
         LogTool.AssertIsTrue(v is IVertex);
         UpdateEdgeCost(graph, v as IVertex);
     }
 }
Ejemplo n.º 13
0
        public static int2 SpaceToPixelSize(ISpace space, int pixelSize)
        {
            LogTool.AssertIsTrue(space.Scale.x > 0);
            LogTool.AssertIsTrue(pixelSize > 2);

            var aspect = space.Scale.y / space.Scale.x;

            return(new int2(pixelSize, Mathf.CeilToInt(pixelSize * aspect)));
        }
Ejemplo n.º 14
0
 public WeightRandom(List <int> weights)
 {
     this.sum.Clear();
     foreach (var i in Enumerable.Range(0, weights.Count))
     {
         LogTool.AssertIsTrue(weights[i] > 0);
         var s = weights[i] + (i > 0 ? this.sum[i - 1] : 0);
         this.sum.Add(s);
     }
 }
Ejemplo n.º 15
0
        protected const int MIN_INDIRECT_BUFFER_SIZE = 5;//5 ints
        public static void SwapBuffer(GPUBufferVariable <T> lhs, GPUBufferVariable <T> rhs)
        {
            LogTool.AssertIsTrue(lhs.Size == rhs.Size);
            LogTool.AssertIsTrue(lhs.type == rhs.type);

            var temp = lhs.gpuBuffer;

            lhs.gpuBuffer = rhs.gpuBuffer;
            rhs.gpuBuffer = temp;
        }
Ejemplo n.º 16
0
        public void Scan(SensorData sensorData)
        {
            var from   = this as ISensorableObject;
            var o      = new float3(this.Position);
            var dir    = new float3(this.transform.forward);
            var minDis = new Dictionary <ObjectType, float>();

            foreach (ObjectType t in Enum.GetValues(typeof(ObjectType)))
            {
                minDis.Add(t, this.Or);
            }

            var minDangerDis = this.Dr;

            LogTool.AssertIsTrue(this.Manager != null);
            foreach (var f in this.Manager.SensorableObjects)
            {
                if (from == f)
                {
                    continue;
                }
                var distance = from.GetDistance(f);
                if (distance < this.Or)
                {
                    sensorData.AddSensorable(f, distance);

                    var p     = f.Position;
                    var angle = math.dot(math.normalize(p - o), math.normalize(dir));
                    if (distance < this.Vr && angle > math.cos(math.radians(this.angle / 2)))
                    {
                        sensorData.AddVisiable(f, distance);

                        if (distance < this.Dr)
                        {
                            sensorData.AddDanger(f, distance);

                            if (distance < minDangerDis)
                            {
                                minDangerDis = distance;
                                sensorData.closestDangerObj = new SensorObject()
                                {
                                    obj = f, distance = distance
                                };
                            }
                        }
                    }
                }
                if (distance < minDis[f.ObjType])
                {
                    minDis[f.ObjType] = distance;
                    sensorData.SetClosest(f, distance);
                }
            }
        }
Ejemplo n.º 17
0
        public float4 GetReductionResult()
        {
            LogTool.AssertIsTrue(this.Inited);
            if (!this.Inited)
            {
                return(0);
            }

            this.DoReduction(this.source, this.reductionBuffer);
            this.resultBuffer.GetToCPUData();
            return(this.resultBuffer.CPUData[0]);
        }
Ejemplo n.º 18
0
        public virtual void InitBuffer(int size, bool cpuData = false, bool autoSet = true, ComputeBufferType type = ComputeBufferType.Default)
        {
            LogTool.AssertIsTrue(size > 0);

            this.Release();

            this.size    = size;
            this.type    = type;
            this.autoSet = autoSet;
            this.cpuData = cpuData ? new T[this.size] : null;

            this.inited = true;
        }
Ejemplo n.º 19
0
        protected void Start()
        {
            this.source.InitBuffer(1024 * 512, true);
            foreach (var i in Enumerable.Range(0, this.source.Size))
            {
                this.source.CPUData[i] = Random.Range(-this.source.Size, this.source.Size);
                // this.source.CPUData[i] = (uint)(this.source.Size - i);
            }

            this.source.SetToGPUBuffer();
            this.Sort(ref this.source);
            this.source.GetToCPUData();

            foreach (var i in Enumerable.Range(0, this.source.Size - 1))
            {
                LogTool.AssertIsTrue(this.source.CPUData[i] <= this.source.CPUData[i + 1]);
            }
        }
Ejemplo n.º 20
0
        // Start is called before the first frame update
        void Start()
        {
            graph = new HGraph();
            var v1 = graph.Factory.CreateVertex();
            var v2 = graph.Factory.CreateVertex();
            var v3 = graph.Factory.CreateVertex();

            v1 = graph.AddVertex(v1);
            v2 = graph.AddVertex(v2);
            v3 = graph.AddVertex(v3);
            var e1 = graph.AddEdge(v1, v2);
            var e2 = graph.AddEdge(v2, v3);
            var e3 = graph.AddEdge(v3, v1);


            var f = graph.GetFace(e1);

            LogTool.AssertIsTrue(f != NoneHalfEdgeObject.None);
        }
Ejemplo n.º 21
0
        /// <summary>
        /// This function will get all ComputeShaderParameterBase parameters
        /// Stores them into list and used to update GPU data
        /// </summary>
        protected void InitVariableList()
        {
            if (this.inited)
            {
                return;
            }

            LogTool.AssertIsTrue(this.variableList == null);
            var bindingFlags = BindingFlags.Instance |
                               BindingFlags.NonPublic |
                               BindingFlags.Public;

            this.variableList = this.GetType()
                                .GetFields(bindingFlags)
                                .Where(field
                                       => typeof(IComputeShaderParameter).IsAssignableFrom(field.FieldType) &&
                                       !Attribute.IsDefined(field, typeof(NoneGPUAttribute)))
                                .Select(field => field.GetValue(this) as IComputeShaderParameter)
                                .ToList();


            var noneCSParameter = this.GetType()
                                  .GetFields(bindingFlags)
                                  .Where(field => !field.FieldType.IsSubclassOf(typeof(IComputeShaderParameter)) &&
                                         !Attribute.IsDefined(field, typeof(NoneGPUAttribute)))
                                  .ToList();


            foreach (var p in noneCSParameter)
            {
                var csp = this.CreateParameter(p);
                if (csp == default)
                {
                    continue;
                }

                this.noneCSVariables.Add(p, csp);
            }
            LogTool.AssertIsTrue(this.variableList != null);

            this.inited = true;
        }
Ejemplo n.º 22
0
        static public void UpdateGradientTexture(Texture2D tex, List <Gradient> from, int size = 128)
        {
            var w = size;
            var h = from.Count;

            LogTool.AssertNotNull(tex);
            LogTool.AssertIsTrue(w == tex.width);
            LogTool.AssertIsTrue(h == tex.height);

            foreach (var y in Enumerable.Range(0, h))
            {
                foreach (var x in Enumerable.Range(0, w))
                {
                    var key   = x * 1f / (w - 1);
                    var color = from[y].Evaluate(key);
                    tex.SetPixel(x, y, color);
                }
            }

            tex.Apply();
        }
Ejemplo n.º 23
0
        protected void DoReduction(Texture source, Dictionary <int, Texture> reductionBuffer)
        {
            LogTool.AssertNotNull(source);
            LogTool.AssertNotNull(reductionBuffer);

            Graphics.CopyTexture(source, this.reductionBuffer[math.max(source.width, source.height)]);

            var w = source.width;
            var h = source.height;

            for (; w > 0 || h > 0; w = w >> 1, h = h >> 1)
            {
                var k = this.computeShader.FindKernel(ReductionKernelName);
                var p = math.max(w, h);

                if (p > 1)
                {
                    var from = p;
                    var to   = p >> 1;
                    var tw   = math.max(1, w >> 1);
                    var th   = math.max(1, h >> 1);

                    // Debug.Log("from " + from + " with " + reductionBuffer[from].width + "x" + reductionBuffer[from].height);
                    // Debug.Log("to " + to + " with " + reductionBuffer[to].width + "x" + reductionBuffer[to].height);
                    // Debug.Log("Dispatch " + tw + "x" + th);

                    this.computeShader.SetTexture(k, "_From", reductionBuffer[from]);
                    this.computeShader.SetTexture(k, "_To", reductionBuffer[to]);
                    this.computeShader.Dispatch(k, tw, th, 1);
                }
                else
                {
                    LogTool.AssertIsTrue(p == 1);
                    k = this.computeShader.FindKernel(ResultKernelName);
                    this.computeShader.SetTexture(k, "_From", reductionBuffer[p]);
                    this.computeShader.SetBuffer(k, this.resultBuffer.ShaderName, this.resultBuffer);
                    this.computeShader.Dispatch(k, 1, 1, 1);
                }
            }
        }
Ejemplo n.º 24
0
        public virtual void InitBuffer(GPUBufferVariable <T> other)
        {
            LogTool.AssertIsTrue(other.Size > 0);
            LogTool.AssertIsTrue(this != other);//self assignment is dangerous

            this.Release();

            this.size    = other.Size;
            this.type    = other.type;
            this.autoSet = other.autoSet;
            if (other.cpuData != null)
            {
                if (this.cpuData == null || this.cpuData.Length != other.cpuData.Length)
                {
                    this.cpuData = new T[this.size];
                }
                Array.Copy(other.cpuData, this.cpuData, this.size);
            }
            this.gpuBuffer = other.Data;

            this.inited = true;
        }
Ejemplo n.º 25
0
        protected void CheckFace(IHalfEdge edge)
        {
            var e      = edge.Next;
            var ecount = 0;

            while (e != edge)
            {
                if (e.Face != NoneHalfEdgeObject.None)
                {
                    edge.Face = e.Face;
                    return;
                }
                e = e.Next;
                ecount++;

                if (ecount > 100000)
                {
                    LogTool.Log("Error loop for edge " + edge, LogLevel.Error);
                    return;
                }
            }
            if (ecount >= 3)
            {
                var newFace = (this.Factory as IHalfEdgeFactory).CreateFace();
                newFace.HalfEdge = edge;
                edge.Face        = newFace;
                var ret = this.faces.Add(newFace);
                LogTool.AssertIsTrue(ret);

                e = newFace.HalfEdge.Next;
                var str = " ";
                while (e != edge)
                {
                    str += " " + e.ToString();
                    e    = e.Next;
                }
                LogTool.Log("Add face " + str);
            }
        }
Ejemplo n.º 26
0
        protected void TestNewGraph(NewGraph <IndexVertex, DefaultEdge, IndexGraphFactory> graph)
        {
            foreach (var e in Enumerable.Range(0, 20))
            {
                graph.Add(graph.Factory.CreateVertex());
            }
            var nodes = graph.Vertices.OrderBy(v => v.index).ToList();
            var e01   = graph.AddEdge(nodes[0], nodes[1]);
            var e02   = graph.Factory.CreateEdge(nodes[3], nodes[4]);


            var e56 = graph.AddEdge(nodes[5], nodes[6]);

            graph.AddEdge(nodes[5], nodes[7]);
            graph.AddEdge(nodes[5], nodes[8]);
            graph.AddEdge(nodes[5], nodes[5]);
            graph.AddEdge(nodes[5], nodes[1]);
            graph.AddEdge(nodes[5], nodes[11], true);

            graph.AddEdge(nodes[1], nodes[15]);

            LogTool.AssertIsTrue(graph.Contains(e01));
            LogTool.AssertIsFalse(graph.Contains(e02));
            LogTool.AssertIsTrue(e56 == graph.GetEdge(nodes[5], nodes[6]));
            LogTool.AssertIsTrue(e56 == graph.GetEdge(nodes[6], nodes[5]));
            LogTool.AssertIsFalse(e56 == graph.GetEdge(nodes[5], nodes[5]));

            foreach (var e in graph.Edges)
            {
                LogTool.Log(e.ToString());
            }

            LogTool.Log("Neighbor 5");
            foreach (var e in graph.GetNeighborVertices(nodes[5]))
            {
                LogTool.Log(e.ToString());
            }
        }
        public void GenerateFFTData()
        {
            var vector = this.sourceFunction.ToYVector();

            var array = vector.Select(s => (double)s).ToArray();
            var dft   = new DFT();

            dft.Initialize((uint)array.Length);
            Complex[] cSpectrum = dft.Execute(array);

            var An = DSP.ConvertComplex.ToMagnitude(cSpectrum);
            var Pn = DSP.ConvertComplex.ToPhaseRadians(cSpectrum);

            LogTool.AssertIsTrue(An.Length == Pn.Length);

            this.cosData.Clear();
            for (var i = 0; i < An.Length; ++i)
            {
                this.cosData.Add(new CosData()
                {
                    amplitude = (float)An[i], frequency = i, phase = (float)Pn[i]
                });
            }
        }
Ejemplo n.º 28
0
        public void Resize(int x, int y)
        {
            LogTool.AssertIsTrue(x >= 0 && y >= 0);

            this.Init(x, y);
        }
Ejemplo n.º 29
0
        public void Resize(int size)
        {
            LogTool.AssertIsTrue(size >= 0);

            this.data = new T[size];
        }
Ejemplo n.º 30
0
        public override IEdge AddEdge(IVertex v1, IVertex v2, bool isDirectional = false)
        {
            if (this.GetEdge(v1, v2) != default)
            {
                return(this.GetEdge(v1, v2));
            }
            //Half edge is always directional
            isDirectional = true;
            var v12Edge = base.AddEdge(v1, v2, isDirectional) as IHalfEdge;
            var v21Edge = base.AddEdge(v2, v1, isDirectional) as IHalfEdge;
            var from    = v1 as IHalfEdgeVertex;
            var to      = v2 as IHalfEdgeVertex;

            if (from.Outgoing == null)
            {
                from.Outgoing = NoneHalfEdgeObject.None;
            }
            if (to.Outgoing == null)
            {
                to.Outgoing = NoneHalfEdgeObject.None;
            }

            if (v12Edge.Face == null)
            {
                v12Edge.Face = NoneHalfEdgeObject.None;
            }
            if (v12Edge.Next == null)
            {
                v12Edge.Next = v21Edge;
            }
            if (v12Edge.Opposite == null)
            {
                v12Edge.Opposite = v21Edge;
            }
            if (v12Edge.Previous == null)
            {
                v12Edge.Previous = v21Edge;
            }

            if (v21Edge.Face == null)
            {
                v21Edge.Face = NoneHalfEdgeObject.None;
            }
            if (v21Edge.Next == null)
            {
                v21Edge.Next = v12Edge;
            }
            if (v21Edge.Opposite == null)
            {
                v21Edge.Opposite = v12Edge;
            }
            if (v21Edge.Previous == null)
            {
                v21Edge.Previous = v12Edge;
            }


            if (from.Outgoing != NoneHalfEdgeObject.None)
            {
                LogTool.AssertIsTrue(from.Outgoing != null);
                LogTool.AssertIsTrue(v21Edge.Vertex == to);
                LogTool.AssertIsTrue(v21Edge.OtherVertex == from);
                var prev = from.Outgoing.Previous;
                var next = from.Outgoing.Previous.Next;

                prev.Next        = v12Edge;
                v12Edge.Previous = prev;

                next.Previous = v21Edge;
                v21Edge.Next  = next;
            }
            if (to.Outgoing != NoneHalfEdgeObject.None)
            {
                LogTool.AssertIsTrue(to.Outgoing != null);
                LogTool.AssertIsTrue(v12Edge.Vertex == from);
                LogTool.AssertIsTrue(v12Edge.OtherVertex == to);

                var prev = to.Outgoing.Previous;
                var next = to.Outgoing.Previous.Next;

                prev.Next        = v21Edge;
                v21Edge.Previous = prev;

                next.Previous = v12Edge;
                v12Edge.Next  = next;
            }

            from.Outgoing = v12Edge;
            to.Outgoing   = v21Edge;

            this.CheckFace(v12Edge);
            this.CheckFace(v21Edge);

            LogTool.AssertIsTrue(v12Edge.Opposite == v21Edge);
            LogTool.AssertIsTrue(v21Edge.Opposite == v12Edge);
            LogTool.AssertNotNull(v12Edge.Face);
            LogTool.AssertNotNull(v21Edge.Face);
            return(v12Edge);
        }