Quantcast
Channel: Grasshopper Developer - McNeel Forum
Viewing all 3638 articles
Browse latest View live

One field with both attractors and repellents

$
0
0

@michellep wrote:

Help!

I’m trying to create a field of circles that vary in size creating a gradient. I need bigger circles to be attracted towards points but at the same time be repelled away from curves.

So far I have been able to create both scripts separately but I’m not able to combine them in a way that they both happen at the same time and on the same grid of circles.

Thanks!

Posts: 1

Participants: 1

Read full topic


Speed Contest - Triangle Normal And Center

$
0
0

@RIL wrote:

OK, so i’m trying to find the fastest way to calculate Face Normals and Face Center in my own C# code, but so far I’m not impressed by my achievements.


Edit: Ops. forgot the gh definition: TriNormalAndCenter.gh (17.4 KB)


Fig 1. Processing triangle legs “A” and “B” some 210.000 times:

The picture above is manually drawn, and the processing time is all internal, and the resulting normals and center points were output into two arrays (N and C)

The essential code I’ve tried.

    // =======
    // NORMALS (CROSS PRODUCT)
    // =======
    // Right hand rule of lines a\/b
    m_normals[i] = Vector3d.CrossProduct(line_b.Direction, line_a.Direction);
    m_normals[i].Unitize();

    // =======
    // CENTER
    // =======
    var mida = line_a.PointAt(0.5);
    var midb = line_b.PointAt(0.5);
    // Criss-cross lines for intersection point at triangle center
    var mida_c = (new Line(mida, line_b.To));
    var midb_b = (new Line(midb, line_a.To));
    // Line-Line Intersect
    var _a = double.MinValue;
    var _b = double.MinValue;
    if (!Rhino.Geometry.Intersect.Intersection.LineLine(mida_c, midb_b, out _a, out _b))
      return false;
    m_centers[i] = mida_c.PointAt(_a);    

(Far below some “manual” equivalents to Rhino’s Vector3d.CrossProduct and Line.PointAt(t) but that didn’t make much of a difference either).

The parallel loop was partitioned so as to not cause extra overhead for multiple small operations

    if (parallel)
    {
      var partitioner = System.Collections.Concurrent.Partitioner.Create(0, count);
      System.Threading.Tasks.Parallel.ForEach(partitioner, (range, loopState) =>
        {
        // Loop over each range element
        for (int j = range.Item1; j < range.Item2; j++)
        {
          NormalAndCenterFromLines(j, line_a, line_b);
        }
        });
    }

Placing the code meat inline in the loop didn’t make much of a difference. Also, since there are only three inputs and the loop just repeats itself, it shouldn’t matter much that this was tested in a C# ScriptComponent instead of VS component. Is it the Line-Line intersetion test which consumes all this processing time?

Anyway, the final solution is meant to traverse Mesh edges and do the same (Rhino Mesh of course has methods for all this, but I need to do some tweaks and also make my own classes or structs for some of my mesh manipulations).

Anyone?

// Rolf

Addendum: “Manual” equivalents to Rhino’s Vector3d.CrossProduct and Line.PointAt(t)

    // =======
    // NORMALS (CROSS PRODUCT)
    // =======
    // Right hand rule for lines a\/b
    var a = line_b.Direction;
    var b = line_a.Direction;
    m_normals[i].X = (a.Y * b.Z) - (a.Z * b.Y);
    m_normals[i].Y = (a.Z * b.X) - (a.X * b.Z);
    m_normals[i].Z = (a.X * b.Y) - (a.Y * b.X);
    m_normals[i].Unitize();

and Line.PointAt(t)

    // "point on line" in Vector form
    // (x,y,z) = (x0,y0,z0) + t(a,b,c)
    var mid_a = line_a.From + 0.5 * line_a.Direction;
    var mid_b = line_b.From + 0.5 * line_b.Direction;

Posts: 8

Participants: 3

Read full topic

C#_Multiple attractor points

$
0
0

@obhagwat29 wrote:

Hello,
I have a pattern which is responding to the point. I want to add more points so that pattern would respond to those multiple points. I could do that may be closest point component in grasshopper but how do I do that in c#?
find attached file pictures!

Any help would be great!

// finding distance between grid point to the attractor point
List<double> dist = new List<double>();

for(int i = 0; i < Grid.Count; i++)
{
  double d1 = x.DistanceTo(Grid[i]);
  dist.Add(d1);
}

// Remap Value formula = (value - from source)/(to source - from source)*(toTarget-fromTarget)+from target
List<double> tVal = new List<double>();
foreach(double d in dist)
{
  double dT = (d - dist.Min()) / (dist.Max() - dist.Min()) * (0.9 - 0) + 0;
  tVal.Add(dT);
}

pattern

06_Islamic Pattern.gh (10.4 KB)

Posts: 2

Participants: 2

Read full topic

Default values of ghpython inputs

$
0
0

@ivelin.peychev wrote:

Hi,

Is it possible to set the default values of a scripting component from inside the code in such a way that when hovering over the input the hint displays that default value?

Second question is related to the first one. Can I set the type?

NOTE: Assume that component will be compiled to .ghpy.

Thanks in advance.

Posts: 1

Participants: 1

Read full topic

Compound Transformations dissection

$
0
0

@ivelin.peychev wrote:

Hi @DavidRutten,

Could you give me a paper or website or something that explains what you describe in this post?

What is happening behind the scenes?

Posts: 3

Participants: 2

Read full topic

Direct access to struct fields for GPU compatibility?

$
0
0

@RIL wrote:

I’m trying to use Alea GPU and all is well as long as I convert data to structs and arrays with “simple” types (double, float, bool , etc). But the type most frequently being used where GPU programming would make a difference is Point3d, Vector, Planes, Line etc. Although Alea supports structs and .NET blittables it doesn’t support Properties. So an array of Point3d or Vector3d doesn’t work as custom types.

Is there any way around the properties so one could go directly at the internal fields for these structs?

A quick look at a few lines on this page makes the problöem quite clear:.
http://www.aleagpu.com/release/3_0_3/doc/gpu_programming_csharp.html#custom_types

// Rolf

Posts: 2

Participants: 2

Read full topic

Getting the same GUID after compiling ghpy

$
0
0

@ivelin.peychev wrote:

When using the definition from here:

How can I make sure the output ghpy has the same GUID so that I don’t get multiple components of the same thing added to the ribbon?

Posts: 4

Participants: 2

Read full topic

GhPython path Access denied?


C# _ new class instance_Animation

$
0
0

@obhagwat29 wrote:

Hello,
I am trying to fill the rectangular area with random circles which are growing from certain radius till the boundary of the rectangular area. I have made class initiated in custom code region. Custom class basically is a simple Circle which has functionality of showing and growing.

I am able to animate the circle growing till the edges of the rectangular area. What I’m trying to do is, that to have multiple circles growing from random location till they touch the boundary of rectangular area.

I have random function in class constructor. When I initiate the class it always give me one random value and circle grows from there. I am expecting a random values every time it recomputes. so that new circle will be added to the respected location

Any help would be great!

ps. Attached video link (I am following a tutorial made for processing)

attached gh_file!

    double width = 100; double height = 75;
    Rectangle3d boundary = new Rectangle3d(Plane.WorldXY, width, height);

    List<CPack> circles = new List<CPack>();
    circles.Add(cP);
    foreach(CPack c in circles)
    {

      if(reset == true)
      {
        if(c.edges())
        {
          c.Growing = false;
        }
        c.Show();
        c.Grow();
        B = c.Show();
        C = c.X;
      }
    }
    A = boundary;

  }

  // <Custom additional code> 
  CPack cP = new CPack(100, 75);

  public class CPack
  {
    public double X;
    public double Y;
    public int Width;
    public int Height;
    public double Radius;
    public bool Growing = true;


    public CPack(int width, int height)
    {
      Random rnd = new Random();
      Width = width;
      Height = height;
      X = rnd.Next(0, width);
      Y = rnd.Next(0, height);
      Radius = 1;
    }
    public Circle Show()
    {
      return new Circle(new Point3d(X, Y, 0), Radius);
    }
    public void Grow()
    {
      if(Growing){
        Radius = Radius + 0.1;
      }
    }
    public bool edges()
    {
      return (X + Radius > Width || X - Radius < 0 || Y + Radius > Height || Y - Radius < 0);
    }
  }

04_CirclePacking Algorithm.gh (5.6 KB)

Posts: 2

Participants: 2

Read full topic

C# Custom "using" statements

$
0
0

@LongNguyen wrote:

A quick question for @DavidRutten:

Can you tell me which exact release of Rhino 6 that starts to support custom “using” statements in the C# Script components. Thanks! :slight_smile:

Posts: 3

Participants: 3

Read full topic

Inserting ghpy into gha assembly?

$
0
0

@ivelin.peychev wrote:

Is that possible?
Assume I have created numerous components by compiling python then I finally learn enough csharp to switch. How can I build a common assembly without the need to re-write all my old components in csharp?

Posts: 7

Participants: 3

Read full topic

How to construct GH_Structure?

$
0
0

@coni wrote:

public GH_Structure<GH_Number> PointsLeftTree = new GH_Structure<GH_Number>();
public GH_Structure<double> AnglesTree = new GH_Structure<double>();
/*How do I convert double to GH_Number?  */

public GH_Structure<Gh_Number> MakePointsLeft()
        {
            for (int i = 0; i < originalPoints.Count; i++)
            {
                int b = originalPoints.IndexOf(originalPoints[i]);
              GH_Point PtB = new GH_Point(originalPoints[b]);

                int a = firstNei[i].Value;
                GH_Point PtA = new GH_Point(puncteOriginale[a]);

                var candidatePtsC = neiCandidates.Branches[i];
                var path = neiCandidates.Paths[i];
                List<Grasshopper.Kernel.Types.GH_Integer> tempPointsLeft = new List<Grasshopper.Kernel.Types.GH_Integer>();
                List<double > tempAngle = new List<double>();
                foreach (Grasshopper.Kernel.Types.GH_Integer cand in candidatePtsC)
                {
                    Grasshopper.Kernel.Types.GH_Point PtC = new Grasshopper.Kernel.Types.GH_Point(puncteOriginale[cand.Value]);
                    bool Flag = MakeCrossProduct(PtB, PtA, PtC);
                    double crosprodval = MakeCP(PtB, PtA, PtC);
                    if (Flag)
                        tempPointsLeft.Add(cand);
                        tempUnghiuri.Add(crosprodval);
                }
                puncteStanga.AppendRange(tempPuncteStanga, path);
                AnglesTree.Append(tempAngle, path);
            }
            return AnglesTree;
        }

Posts: 3

Participants: 2

Read full topic

GhPython Node to Code - Loft: input 2 options. error: Data conversion failed from Text to Loft Options

Is ghdoc of GhPython component not available in cluster?

Having boids bounce off surface ends

$
0
0

@youssef_aboualghar wrote:

Hello guys,
So I’ve almost finished this code about boids. I only couldn’t figure this part out:
I created boids randomly across a surface moved them by a certain vector and then I project
the points with time the position updates.
the problem is: I can’t make the point to bounce back to the surface with they reach boundary
of the surface.

Here is the code:

using System.Collections.Generic;
using Grasshopper.GUI.Widgets;
using Rhino.Geometry;

namespace SurfaceTrails2.FlockingOnSrf2
{
public class FlockAgent
{
public Point3d Position;
public Vector3d Velocity;
public FlockSystem FlockSystem;
private Vector3d desiredVelocity = new Vector3d(0,0,0);

    public FlockAgent(Point3d position, Vector3d velocity)
    {
        Position = position;
        Velocity = velocity;
    }
     
    public void UpdateVelocityAndPosition()
    {
        double maxVelocity = 0.02667 * FlockSystem.BoundingBox;
        double minVelocity = 0.1333 * FlockSystem.BoundingBox;


        Velocity = 0.97 * Velocity + 0.03 * desiredVelocity;

        if (Velocity.Length > maxVelocity) Velocity *= maxVelocity / Velocity.Length;
        else if (Velocity.Length < minVelocity) Velocity *= minVelocity / Velocity.Length;

        Position += Velocity * FlockSystem.Timestep;
        double u;
        double v;

        //var nu = (Position.X - 0) / (BoundingBox - 0);
        //var nv = (Position.Y - 0) / (BoundingBox - 0);

        FlockSystem.Surface.ClosestPoint(Position, out u, out v);
        Position = FlockSystem.Surface.PointAt(u, v);
    }



    public void ComputeDesiredVelocity(List<FlockAgent> neighbours/*, Surface surface*/)
    {
        // First, reset the desired velocity to 0
        desiredVelocity = new Vector3d(FlockSystem.Surface.Domain(0).T0, FlockSystem.Surface.Domain(1).T0, 0.0);
        var bounceMultiplier = 30;
        // ===============================================================================
        // Pull the agent back if it gets out of the bounding box 
        // ===============================================================================
        var xMin = FlockSystem.Surface.PointAt(FlockSystem.Surface.Domain(0).T0, FlockSystem.Surface.Domain(1).T0).X;
        var xMax = FlockSystem.Surface.PointAt(FlockSystem.Surface.Domain(0).T1, FlockSystem.Surface.Domain(1).T0).X;
        var yMin = FlockSystem.Surface.PointAt(FlockSystem.Surface.Domain(0).T0, FlockSystem.Surface.Domain(1).T0).Y;
        var yMax = FlockSystem.Surface.PointAt(FlockSystem.Surface.Domain(0).T0, FlockSystem.Surface.Domain(1).T1).Y;



        //if (Position.X < xMin)
        //    desiredVelocity += new Vector3d((FlockSystem.BoundingBox - Position.X) * bounceMultiplier, 0.0, 0.0);

        //else if (Position.X > xMax)
        //    desiredVelocity += new Vector3d(-Position.X * bounceMultiplier, 0.0, 0.0);


        //if (Position.Y < yMin)
        //    desiredVelocity += new Vector3d(0.0, (FlockSystem.BoundingBox - Position.Y) * bounceMultiplier, 0.0);

        //else if (Position.Y > yMax)
        //    desiredVelocity += new Vector3d(0.0, (-Position.Y) * bounceMultiplier, 0.0);
        //if (Position.X < xMin)
        //    desiredVelocity += new Vector3d((xMax - Position.X) * bounceMultiplier, 0.0, 0.0);

        //else if (Position.X > xMax)
        //    desiredVelocity += new Vector3d(-Position.X * bounceMultiplier, 0.0, 0.0);


        //if (Position.Y < yMin)
        //    desiredVelocity += new Vector3d(0.0, (yMax - Position.Y) * bounceMultiplier, 0.0);

        //else if (Position.Y > yMax)
        //    desiredVelocity += new Vector3d(0.0, (-Position.Y) * bounceMultiplier, 0.0);

        //if (Position.Z < 0.0)
        //    desiredVelocity += new Vector3d(0.0, 0.0, -Position.Z);
        //else if (Position.Z > boundingBoxSize)
        //    desiredVelocity += new Vector3d(0.0, 0.0, boundingBoxSize - Position.Z);
        double u;
        double v;
        FlockSystem.Surface.ClosestPoint(Position, out u, out v);
        var closestPoint = FlockSystem.Surface.PointAt(u, v);

        if (Position.DistanceTo(closestPoint) < 0.1)
        {
            //desiredVelocity.Reverse();
            ////desiredVelocity.Transform(FlockSystem.Orient);
            //desiredVelocity += desiredVelocity * bounceMultiplier;

            var reverse = Point3d.Subtract(Position, FlockSystem.Surface.PointAt(u, v));
            //reverse.Reverse();
            desiredVelocity += reverse * bounceMultiplier;

        }


        // ===============================================================================
        // 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 
            // -------------------------------------------------------------------------------

            Vector3d alignment = Vector3d.Zero;

            foreach (FlockAgent neighbour in neighbours)
                alignment += neighbour.Velocity;

            // We divide by the number of neighbours to actually get their average velocity
            alignment /= neighbours.Count;

            desiredVelocity += FlockSystem.AlignmentStrength * alignment;


            // -------------------------------------------------------------------------------
            // "Cohesion" behavior 
            // -------------------------------------------------------------------------------

            Point3d centre = Point3d.Origin;

            foreach (FlockAgent neighbour in neighbours)
                centre += neighbour.Position;

            // We divide by the number of neighbours to actually get their centre of mass
            centre /= neighbours.Count;

            Vector3d cohesion = centre - Position;

            desiredVelocity += FlockSystem.CohesionStrength * cohesion;


            // -------------------------------------------------------------------------------
            // "Separation" behavior 
            // -------------------------------------------------------------------------------

            Vector3d separation = Vector3d.Zero;

            foreach (FlockAgent neighbour in neighbours)
            {
                double distanceToNeighbour = Position.DistanceTo(neighbour.Position);

                if (distanceToNeighbour < FlockSystem.SeparationDistance)
                {
                    Vector3d getAway = Position - neighbour.Position;

                    /* We scale the getAway vector by inverse of distanceToNeighbour to make 
                       the getAway vector bigger as the agent gets closer to its neighbour */
                    separation += getAway / (getAway.Length * distanceToNeighbour);
                }


            }

            desiredVelocity += FlockSystem.SeparationStrength * separation;
        }


        // ===============================================================================
        // Avoiding the obstacles (repellers)
        // ===============================================================================

        foreach (Circle repeller in FlockSystem.Repellers)
        {
            double distanceToRepeller = Position.DistanceTo(repeller.Center);

            Vector3d repulsion = Position - repeller.Center;

            // Repulstion gets stronger as the agent gets closer to the repeller
            repulsion /= (repulsion.Length * distanceToRepeller);

            // Repulsion strength is also proportional to the radius of the repeller circle/sphere
            // This allows the user to tweak the repulsion strength by tweaking the radius
            repulsion *= 30.0 * repeller.Radius;

            desiredVelocity += repulsion;

        }
    }
}

}


Any help would be much appreaciated.
Thank you .

Posts: 1

Participants: 1

Read full topic


List access internal switch?

$
0
0

@ivelin.peychev wrote:

Is it possible from inside the code to create some sort of a switch so that if it is a single object the input to have a flag ‘Item Access’ and if it is a list of objects to act as ‘List Access’?

Posts: 4

Participants: 3

Read full topic

Wish: Toggle customizible output

$
0
0

@ivelin.peychev wrote:

Just like button has:
image

I would very much like if toggle also had such toggled state, not toggled state customizible outputs.

Posts: 1

Participants: 1

Read full topic

How can I create a list of nested lists (no coding)?

ZUI GHPY component?

Entwine virtual component something's wrong?

Viewing all 3638 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>