1

I'm in the process of porting a collection of RhinoScript (Python) functions to C# in order to develop a collection of custom Grasshopper components.

My problem is I'm having trouble accessing some RhinoScript methods such as VectorUnitize(), VectorScale() and PointAdd().

I can't seem to find any references that include these in C#. Does anyone have any experience with this kind of thing as to point me in the right direction?

RhinoScript I'm working from:

# FIND THE ALIGNMENT VECTOR
aVec = self.AlignmentVector(neighborAgents, neighborAgentsDistances)
if rs.VectorLength(aVec) > 0:
    aVec = rs.VectorUnitize(aVec)
aVec = rs.VectorScale(aVec, self.alignment)

# FIND THE SEPARATION VECTOR
sVec = self.SeparationVector(neighborAgents, neighborAgentsDistances)
if rs.VectorLength(sVec) > 0:
    sVec = rs.VectorUnitize(sVec)
sVec = rs.VectorScale(sVec, self.separation)

# FIND THE COHESION VECTOR
cVec = self.CohesionVector(neighborAgents)
if rs.VectorLength(cVec) > 0:
    cVec = rs.VectorUnitize(cVec)
cVec = rs.VectorScale(cVec, self.cohesion)

# ADD ALL OF THE VECTOR TOGETHER to find the new position of the agent
acc = [0, 0, 0]
acc = rs.PointAdd(acc, aVec)
acc = rs.PointAdd(acc, sVec)
acc = rs.PointAdd(acc, cVec)

# update the self vector
self.vec = rs.PointAdd(self.vec, acc)
self.vec = rs.VectorUnitize(self.vec)

What I've got so far (not much :/):

// Find the alignment Vector
Vector3d aVec = AlignmentVector(neighborAgents, neighborAgentsDistances);
if (aVec.Length > 0)
{
    aVec.Unitize();
}
aVec = ????
Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
greyBow
  • 1,298
  • 3
  • 28
  • 62

2 Answers2

2

Per the Vector3d documentation of the Add function you can also use the overloaded + operator of Vector3d. For most of these geometry types RhinoCommon provides the expected overloads.

Thus to scale a vector you multiply it with a scalar, in your case with alignment, separation and cohesion.

Vector3d vec1 = getyourvector();
vec1.Unitize();
vec1 *= alignment;

Vector3d vec2 = getyourvector();
vec2.Unitize();
vec2 *= cohesion;

Vector3d vec3 = getyourvector();
vec3.Unitize();
vec3 *= separation;

Vector3d acc;
acc += vec1;
acc += vec2;
acc += vec3;
jesterKing
  • 446
  • 4
  • 10
2

Here you can see how all the rhino script functions are implemented using Rhino Common: https://github.com/mcneel/rhinoscriptsyntax/tree/rhino-6.x/Scripts/rhinoscript

Goswin Rothenthal
  • 2,244
  • 1
  • 19
  • 32