Exemple #1
0
        public void PerformPointOperation(PointOperations operation, decimal value)
        {
            if (_image == null)
            {
                return;
            }
            switch (operation)
            {
            case PointOperations.Add:
                _image.AddValue(value);
                break;

            case PointOperations.Substract:
                _image.SubstractValue(value);
                break;

            case PointOperations.Divide:
                _image.DivideValue(value);
                break;

            case PointOperations.Multiply:
                _image.MultiplyValue(value);
                break;
            }
        }
Exemple #2
0
        private void button11_Click(object sender, EventArgs e)
        {
            savedBitmap.Push(new Bitmap(picture));
            if (savedBitmap.Count() >= 0)
            {
                button1.Enabled = true;
            }
            Graphics g = Graphics.FromImage(picture);

            g.SmoothingMode     = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode   = PixelOffsetMode.HighQuality;

            string[]      parametrers = textBox1.Text.Split('|');
            PointF        wektor      = new PointF(int.Parse(parametrers[0]), int.Parse(parametrers[1]));
            float         k           = float.Parse(parametrers[2]);
            List <PointF> figure      = new List <PointF>();

            figure = PointOperations.ScalePointList(kwadrat, wektor, k);
            midX   = 0;
            midY   = 0;
            foreach (var item in figure)
            {
                midX += item.X;
                midY += item.Y;
            }
            midX /= figure.Count;
            midY /= figure.Count;

            figure.Sort((new Comparison <PointF>(SortCornersClockwise)));
            g.DrawPolygon(Pens.Black, figure.ToArray());
            Picture = picture;
        }
        // ===============================================================================================
        // method to add behaviours to agents
        // ===============================================================================================
        public void ComputeDesiredVelocity(List <IFlockAgent> neighbours)
        {
            // First, reset the desired velocity to 0
            _desiredVelocity = new Vector3d(0.0, 0.0, 0.0);
            // Pull the agent back if it gets out of the bounding box
            foreach (var icontainment in Containment)
            {
                _desiredVelocity += icontainment.DesiredVector(Position, _desiredVelocity);
            }
            // If there are no neighbours nearby, the agent will maintain its veloctiy,
            // else it will perform the "alignment", "cohension" and "separation" behaviours
            if (neighbours.Count == 0)
            {
                _desiredVelocity += Velocity; // maintain the current velocity
            }
            else
            {
                // -------------------------------------------------------------------------------
                // "Alignment" behavior
                // -------------------------------------------------------------------------------
                _desiredVelocity += AgentBehaviours.Alignment(neighbours, _desiredVelocity, FlockSystem);
                // -------------------------------------------------------------------------------
                // "Cohesion" behavior
                // -------------------------------------------------------------------------------
                _desiredVelocity += AgentBehaviours.Cohesion(neighbours, Position, _desiredVelocity, FlockSystem);
                // -------------------------------------------------------------------------------
                // "Separation" behavior
                // -------------------------------------------------------------------------------
                _desiredVelocity += AgentBehaviours.Separation(neighbours, Position, _desiredVelocity, FlockSystem);
            }
            // -------------------------------------------------------------------------------
            //// Interactionbehaviours
            // -------------------------------------------------------------------------------
            foreach (var interaction in Interactions)
            {
                switch (interaction.BehaviourType)
                {
                //=======================================================================================
                //if interaction is repeller
                case BehaviourType.Repeller:
                    if (Containment[0].Label == 's')
                    {
                        _container = (SurfaceContainment)Containment[0];

                        foreach (var repeller in interaction.Circles)
                        {
                            // Remap repellers
                            double u;
                            double v;
                            _container.Surface.ClosestPoint(repeller.Center, out u, out v);

                            var nu = NumberOperations.remap(SrfBoudningBox.Min.X,
                                                            SrfBoudningBox.Max.X, XMin, XMax, u);
                            var nv = NumberOperations.remap(SrfBoudningBox.Min.Y,
                                                            SrfBoudningBox.Max.Y, YMin, YMax, v);
                            Point3d remappedCenter = new Point3d(nu, nv, 0);
                            Circle  remappedCircle = new Circle(remappedCenter, repeller.Radius);
                            _surfaceRepller.Add(remappedCircle);
                        }
                        FlockSystem.Repellers = _surfaceRepller;
                    }
                    else
                    {
                        FlockSystem.Repellers = interaction.Circles;
                    }

                    break;

                //=======================================================================================
                //if interaction is Attractor
                case BehaviourType.Attractor:
                    if (Containment[0].Label == 's')
                    {
                        _container = (SurfaceContainment)Containment[0];

                        foreach (var attractor in interaction.Circles)
                        {
                            // Remap Attractors
                            double u;
                            double v;
                            _container.Surface.ClosestPoint(attractor.Center, out u, out v);

                            var nu = NumberOperations.remap(SrfBoudningBox.Min.X,
                                                            SrfBoudningBox.Max.X, XMin, XMax, u);
                            var nv = NumberOperations.remap(SrfBoudningBox.Min.Y,
                                                            SrfBoudningBox.Max.Y, YMin, YMax, v);
                            Point3d remappedCenter = new Point3d(nu, nv, 0);
                            Circle  remappedCircle = new Circle(remappedCenter, attractor.Radius);
                            _surfaceAttractors.Add(remappedCircle);
                        }
                        FlockSystem.Attractors = _surfaceAttractors;
                    }
                    else
                    {
                        FlockSystem.Attractors = interaction.Circles;
                    }

                    var closestPoint = PointOperations.ClosestPoints(Position, FlockSystem.Attractors.Select(p => p.Center).ToList(), 1);
                    interaction.ClosestPoint = closestPoint[0];
                    break;

                //=======================================================================================
                //if interaction is Attractor curve
                case BehaviourType.AttractorCurve:

                    if (Containment[0].Label == 's')
                    {
                        //getting curve data: points and degree
                        _container = (SurfaceContainment)Containment[0];
                        var interactionAttractorCurve = (AttractorCurve)interaction;
                        var attractorcurve            = interactionAttractorCurve.Curves[0].ToNurbsCurve();
                        var controlpoints             = attractorcurve.Points;
                        var degree = attractorcurve.Degree;

                        foreach (var controlpoint in controlpoints)
                        {
                            double u;
                            double v;
                            _container.Surface.ClosestPoint(controlpoint.Location, out u, out v);

                            var nu = NumberOperations.remap(_container.Surface.Domain(0).T0,
                                                            _container.Surface.Domain(0).T1, XMin, XMax, u);
                            var nv = NumberOperations.remap(_container.Surface.Domain(1).T0,
                                                            _container.Surface.Domain(1).T1, YMin, YMax, v);
                            Point3d remappedControlPoint = new Point3d(nu, nv, 0);
                            _remappedPoints.Add(remappedControlPoint);
                        }
                        var remappedCurve = Curve.CreateControlPointCurve(_remappedPoints, degree);
                        _remappedCurves.Add(remappedCurve);
                        FlockSystem.AttractorCurves = _remappedCurves;
                    }
                    else
                    {
                        FlockSystem.AttractorCurves = interaction.Curves;
                    }


                    var attractorCast = (AttractorCurve)interaction;
                    FlockSystem.AttractorCurvesSwitch = attractorCast.AttractorCurveSwitch;

                    //FlockSystem.AttractorCurves[0].ClosestPoint(StartPosition, out t);
                    //var curveClosestPoint = FlockSystem.AttractorCurves[0].PointAt(t);
                    Point3d curveClosestPoint;

                    if (FlockSystem.AttractorCurvesSwitch)
                    {
                        List <Point3d> curveClosestPoints = new List <Point3d>();
                        foreach (var attractorCurve in FlockSystem.AttractorCurves)
                        {
                            double t;
                            attractorCurve.ClosestPoint(StartPosition, out t);
                            curveClosestPoints.Add(attractorCurve.PointAt(t));
                        }
                        curveClosestPoint = PointOperations.ClosestPoints(StartPosition, curveClosestPoints, 1)[0];
                    }
                    else
                    {
                        List <Point3d> curveClosestPoints = new List <Point3d>();
                        foreach (var attractorCurve in FlockSystem.AttractorCurves)
                        {
                            double t;
                            attractorCurve.ClosestPoint(Position, out t);
                            curveClosestPoints.Add(attractorCurve.PointAt(t));
                        }
                        curveClosestPoint = PointOperations.ClosestPoints(Position, curveClosestPoints, 1)[0];
                    }


                    interaction.ClosestPoint = curveClosestPoint;
                    break;

                //=======================================================================================
                //if interaction is Repeller curve
                case BehaviourType.RepellerCurve:

                    if (Containment[0].Label == 's')
                    {
                        //getting curve data: points and degree
                        _container = (SurfaceContainment)Containment[0];
                        var interactionRepellerCurve = (RepellerCurve)interaction;
                        var attractorcurve           = interactionRepellerCurve.Curves[0].ToNurbsCurve();
                        var controlpoints            = attractorcurve.Points;
                        var degree = attractorcurve.Degree;

                        foreach (var controlpoint in controlpoints)
                        {
                            double u;
                            double v;
                            _container.Surface.ClosestPoint(controlpoint.Location, out u, out v);

                            var nu = NumberOperations.remap(_container.Surface.Domain(0).T0,
                                                            _container.Surface.Domain(0).T1, XMin, XMax, u);
                            var nv = NumberOperations.remap(_container.Surface.Domain(1).T0,
                                                            _container.Surface.Domain(1).T1, YMin, YMax, v);
                            Point3d remappedControlPoint = new Point3d(nu, nv, 0);
                            _remappedPoints.Add(remappedControlPoint);
                        }
                        var remappedCurve = Curve.CreateControlPointCurve(_remappedPoints, degree);
                        _remappedCurves.Add(remappedCurve);
                        FlockSystem.RepllerCurves = _remappedCurves;
                    }
                    else
                    {
                        FlockSystem.RepllerCurves = interaction.Curves;
                    }

                    //FlockSystem.RepllerCurves[0].ClosestPoint(Position, out t);
                    //var repellerCurveClosestPoint = FlockSystem.RepllerCurves[0].PointAt(t);
                    List <Point3d> repellerCurveClosestPoints = new List <Point3d>();
                    foreach (var repellerCurve in FlockSystem.RepllerCurves)
                    {
                        double t;
                        repellerCurve.ClosestPoint(Position, out t);
                        repellerCurveClosestPoints.Add(repellerCurve.PointAt(t));
                    }
                    var repellerCurveClosestPoint = PointOperations.ClosestPoints(Position, repellerCurveClosestPoints, 1)[0];

                    interaction.ClosestPoint = repellerCurveClosestPoint;
                    break;

                //=======================================================================================
                //if interaction is Follow Points
                case BehaviourType.FollowPoints:
                    FlockSystem.FollowAttractors = interaction.Circles;
                    break;

                //=======================================================================================
                //if interaction is follow curve
                case BehaviourType.FollowCurve:
                    FlockSystem.FollowCurveAttractors = interaction.Circles;
                    break;

                //=======================================================================================
                //if interaction is Wind
                case BehaviourType.Wind:
                    FlockSystem.Wind = interaction.WindVec;
                    break;
                }
                interaction.Position        = Position;
                interaction.FlockSystem     = FlockSystem;
                interaction.DesiredVelocity = _desiredVelocity;
                _desiredVelocity           += interaction.ComputeDesiredVelocity();
            }
        }
Exemple #4
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
// ===============================================================================================
// Read input parameters
// ===============================================================================================
            var    mesh            = new Mesh();
            var    attractorPoints = new List <Point3d>();
            var    interval        = 0;
            double threshold       = 0;
            double distance        = 0;
            var    run             = true;
            var    reset           = true;

            //get values from grasshopper
            DA.GetData("Input Mesh", ref mesh);
            DA.GetDataList("Attractor Points", attractorPoints);
            DA.GetData("Interval", ref interval);
            DA.GetData("Threshold", ref threshold);
            DA.GetData("Distance", ref distance);
            DA.GetData("Run", ref run);
            DA.GetData("Reset", ref reset);
// ===============================================================================================
// Applying Values to Class
// ===============================================================================================
            _pts        = attractorPoints;
            _iThreshold = threshold;
            _iInterval  = interval;

            if (run)
            {
                _count++;
            }
            else
            {
                _count = 0;
            }
            //Recursion Loop
            mesh.Weld(0.01);
            if (_tempMesh2 == null)
            {
                _tempMesh = mesh;
            }
            else
            {
                _tempMesh = _tempMesh2;
            }

            var points    = _tempMesh.Vertices;
            var faces     = _tempMesh.Faces;
            var pointList = points.ToPoint3dArray().ToList();

            for (int i = 0; i < _count; i++)
            {
                foreach (Point3d pt in _pts)
                {
                    int index;
                    PointOperations.ClosestPointWithIndex(pt, pointList, out index);

                    if (pt.DistanceTo(pointList[index]) <= _iThreshold)
                    {
                        //Move Point
                        Vector3d moveVector = (pointList[index] - pt) / pt.DistanceTo(pointList[index]);
                        moveVector = moveVector * distance;
                        Transform move       = Transform.Translation(moveVector);
                        var       movedPoint = pointList[index];
                        movedPoint.Transform(move);

                        pointList[index] = movedPoint;
                    }
                }
                //Build constructedMesh
                Mesh constructedMesh = new Mesh();
                constructedMesh.Vertices.AddVertices(pointList);
                constructedMesh.Faces.AddFaces(faces);
                constructedMesh.FaceNormals.ComputeFaceNormals();
                constructedMesh.Normals.ComputeNormals();
                constructedMesh.Compact();

                _tempMesh2 = constructedMesh;
            }
            if (reset)
            {
                _tempMesh2 = mesh;
                _count     = 0;
            }
// ===============================================================================================
// Exporting Data to Grasshopper
// ===============================================================================================
            var a = _tempMesh2;

            DA.SetData("Carved Mesh", a);
        }
Exemple #5
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
// ===============================================================================================
// Read input parameters
// ===============================================================================================
            var allCurvesEdges     = new List <Curve>();
            int valence            = 0;
            var allBrepOuterPoints = new List <Point3d>();
            var dupPoints          = new List <Point3d>();
            var dupPointCount      = new List <int>();
            var pointsOfValence    = new List <Point3d>();

            //get variables from grasshopper
            if (!DA.GetDataList(0, allCurvesEdges))
            {
                return;
            }
            if (!DA.GetData(1, ref valence))
            {
                return;
            }
// ===============================================================================================
// Applying Values to Class
// ===============================================================================================
            //get all brep points in a list
            foreach (Curve allCurvesEdge in allCurvesEdges)
            {
                allBrepOuterPoints.Add(allCurvesEdge.PointAtStart);
                allBrepOuterPoints.Add(allCurvesEdge.PointAtEnd);
            }
            //compare each point in the list of all point to new list of a duplicate from the all point list
            foreach (Point3d brepPoint in allBrepOuterPoints)
            {
                bool exists = false;
                for (int i = 0; i < dupPoints.Count; i++)
                {
                    if (PointOperations.PointDifference(brepPoint, dupPoints[i]) < DocumentTolerance())
                    {
                        exists = true;
                        dupPointCount[i]++;
                    }
                }
                if (!exists)
                {
                    dupPointCount.Add(1);
                    dupPoints.Add(brepPoint);
                }
            }
            //putting resuls of comparison in their corresponding lists
            for (int i = 0; i < dupPoints.Count; i++)
            {
                if (dupPointCount[i] == valence)
                {
                    pointsOfValence.Add(dupPoints[i]);
                }
            }
// ===============================================================================================
// Exporting Data to Grasshopper
// ===============================================================================================
            var a = pointsOfValence;

            DA.SetDataList(0, a);
        }