Example #1
0
        public DifferentialLine owner; // Just to pass the main random instance into the node


        //Constructor
        public DifferentialNode(double x, double y, double mF, double mS, DifferentialLine diff)
        {
            //Debug.WriteLine("Node: Constructor Called");
            acceleration = new Vector3d(0, 0, 0);
            owner        = diff; // Assign owner BEFORE calling Random2D vector since it uses owner.
            velocity     = Random2DVector();
            position     = new Point3d(x, y, 0);
            maxForce     = mF;
            maxSpeed     = mS;
        }
        //END - CUSTOM Component Fields

        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object can be used to retrieve data from input parameters and
        /// to store data in output parameters.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // ----------------- SET FIELD VALUES ---------------

            int runIterations = 0;

            if (!DA.GetData(0, ref runIterations))
            {
                return;
            }

            double _maxForce = 0.15; // Maximum steering force

            if (!DA.GetData(1, ref _maxForce))
            {
                return;
            }

            double _maxSpeed = 2; // Maximum speed

            if (!DA.GetData(2, ref _maxSpeed))
            {
                return;
            }

            double _desiredSeparation = 10;

            if (!DA.GetData(3, ref _desiredSeparation))
            {
                return;
            }

            double _separationCohesionRatio = 11;

            if (!DA.GetData(4, ref _separationCohesionRatio))
            {
                return;
            }

            double _maxEdgeLength = 10;

            if (!DA.GetData(5, ref _maxEdgeLength))
            {
                return;
            }


            if (!DA.GetData(6, ref resetComponent))
            {
                return;
            }

            if (!DA.GetData(7, ref runComponent))
            {
                return;
            }


            // ---------------- VALUE CHECKING ----------------

            if (!runComponent)
            {
                HasFinishedRunning = true;
            }
            else
            {
                HasFinishedRunning = false;
            }

            // Check for reset
            if (resetComponent)
            { //Erase previous results and null _diff_line
                runResults = new DataTree <Line>();
                _diff_line = null;
                actualRuns = 0;
            }

            //// Check if iterations has increased more than actual runs.
            //if (runIterations >= actualRuns)
            //{
            //    runIterations -= actualRuns;
            //}
            //else
            //{
            //    runIterations = 0;
            //}

            // Check if DifferentialLine has been instantiated
            if (_diff_line == null)
            {
                //Setup diff_line with starting points
                _diff_line =
                    new DifferentialLine(_maxForce,
                                         _maxSpeed,
                                         _desiredSeparation,
                                         _separationCohesionRatio,
                                         _maxEdgeLength);
                double nodeStart = 20;
                double angInc    = 2 * Math.PI / nodeStart;
                double rayStart  = 10;

                for (double a = 0; a < 2 * Math.PI; a += angInc)
                { // Create new Nodes
                    double tempX = 0 + Math.Cos(a) * rayStart;
                    double tempY = 0 + Math.Sin(a) * rayStart;

                    _diff_line.AddNode(new DifferentialNode(tempX,
                                                            tempY,
                                                            _diff_line.maxForce,
                                                            _diff_line.maxSpeed,
                                                            _diff_line));
                }
            }


            // ----------------------------- RUN DIFFERENTIAL LINE ------------------------------

            if (runComponent)
            {
                // Check if DifferentialLine hasn't run
                if (actualRuns == 0)
                { //OUTPUT Initial Curve
                    runResults.AddRange(_diff_line.RenderLine(), new Grasshopper.Kernel.Data.GH_Path(0));
                }

                if (actualRuns < runIterations)
                {
                    _diff_line.Run(); // Run diff line once
                    actualRuns++;     //Add +1 to actualRuns, component class value holder.
                }
                else
                {
                    HasFinishedRunning = true;
                }


                //Add iteration result to component class tree

                // ----------------------------- SET OUTPUT DATA ------------------------------------


                DA.SetData(0, actualRuns);
                DA.SetDataList(2, _diff_line.RenderLine());
            }
        }