优化力导向图

时间:2017-07-04 15:06:23

标签: c# unity3d graph

我已经根据C#for Unity中的EpForceDirectedGraph.cs图编写了一个强制定向图形实现,但它只能处理多达1000个节点,低于我需要的大约4000个节点。我已经查看了几天的代码,但似乎无论如何都无法加速它!看看,任何帮助将不胜感激!我在400个节点上运行这些函数85次以生成正确的图形,这意味着应用程序函数在启动时运行了27,000,000次!

请注意我使用WebGL作为我的构建目标,因此多线程和SIMD可能无济于事,但我愿意接受使用这些工具的建议。

这些是主要的应用功能:

    * @Description     applied coulombs law to determine the push force between 2 points as well as*
    *                  the attraction to center and updating of the velocity and acceleration      *
    *                                                                                              *
    * @parameters      iTimeStep - the time since last frame                                       *
    ************************************************************************************************/
    protected override void applyNodeLaws(float iTimeStep)
    {
        for (int i = 0; i < graph.nodes.Count; i++)
        {
            //applies coulomb's law to each node 
            Point3D point1 = GetPoint(graph.nodes[i]);
            for (int j = 0; j < graph.nodes.Count; j++)
            {
                Point3D point2 = GetPoint(graph.nodes[j]);
                if (point1 != point2)
                {
                    Vector3 d = point1.position - point2.position;
                    float distance = d.magnitude + 0.1f;
                    Vector3 direction = d.normalized;

                    point1.ApplyForce((direction * Repulsion) / (distance * 0.5f));
                    point2.ApplyForce((direction * Repulsion) / (distance * -0.5f));

                }
            }
            //Applies centre attraction
            Vector3 centreDirection = point1.position * -1.0f;
            float displacement = centreDirection.magnitude;
            centreDirection = centreDirection.normalized;
            point1.ApplyForce(centreDirection * (Stiffness * displacement * 0.4f));

            //converts acceleration to velocity
            point1.velocity += (point1.acceleration * iTimeStep);
            point1.velocity *= Damping;
            point1.acceleration = Vector3.zero;
            //applies velocity to position
            point1.position += (point1.velocity * iTimeStep);


        }
    }

   /************************************************************************************************
    * @Description     applied hookes law to determine the spring force between 2 points           *
    ************************************************************************************************/
    protected override void applyHookesLaw()
    {
        for (int i = 0; i < graph.edges.Count; i++)
        {
            Spring3D spring = GetSpring(graph.edges[i]);
            Vector3 d = spring.point2.position - spring.point1.position;
            float displacement = spring.Length - d.magnitude;
            Vector3 direction = d.normalized;

            spring.point1.ApplyForce(direction * (spring.K * displacement * -0.5f));
            spring.point2.ApplyForce(direction * (spring.K * displacement * 0.5f));
        }
    }

1 个答案:

答案 0 :(得分:0)

最后,我们决定将所有处理卸载到基于外部AWS的Heroku服务器。如果标注超时为40秒,我们可以完成数百万次计算,通常需要30分钟来生成!

我们还实施了一个基于八叉树的节点系统,使我们能够实时运行它!