Grasshopper

algorithmic modeling for Rhino

Making a function that doesn't refresh until some inputs change (boolean)

Hi,

I have a function that generate a list of random vectors. This function is used in SolveInstance Sub once. The problem is that the natural component refreshment when some input values change, makes this vectors change all the time. 

One solution could be make an external component only to calculate this vectors (and I've concluded that is the best solution to make my plugin more versatile) but I want to know how to make that some function(only that function) updates only when a particular input changes or give us a determined value. 

Thanks in advance.

Ángel.

Views: 726

Replies to This Discussion

How are you creating the random vectors? If you are using the random class, make sure you are specifying a seed value, like:
dim rnd as new random(2716057)

or
dim rnd as new random(3370318)

 

If you want the script to update only when one of the inputs change, you can try the following:

Store a copy of the input data. Store a copy of the output data. Compare the new input data to the copy (that was stored the last time the component was run). If the data is the same then retun the previous output data.

There's probably a better way of doing it.

Yeah, that is the class I'm using. My function to calculate the vectors is:

Public Function randomvectors(ByVal range As Interval, ByVal intNumber As Integer)


  Dim listVectors As New List(Of Vector3d)
  Dim iVect As Vector3d
  Dim dLenght As Double
  Dim dTheta As Double
  Dim dGamma As Double

  Dim rLengthRandom As New Random(1)
  Dim rTheta As New Random(2)
  Dim rGamma As New Random(3)

  'Iterate to generate a list of vectors
  For i As Integer = 0 To intNumber - 1
    dLenght = rLengthRandom.NextDouble * (range.Item(1) - range.Item(0)) + range.Item(0)
    dTheta = rTheta.NextDouble * Math.PI
    dGamma = rTheta.NextDouble * (Math.PI * 2)
    iVect.X = (dLenght * Math.Sin(dTheta) * Math.Cos(dGamma))
    iVect.Y = (dLenght * Math.Sin(dTheta) * Math.Sin(dGamma))
    iVect.Z = (dLenght * Math.Cos(dTheta))
    listVectors.Add(iVect)
  Next

  Return listVectors

End Function

I tried your code and the generated vectors don't seem to change after the component is refreshed.

A side note, instead of:
dLenght = rLengthRandom.NextDouble * (range.Item(1) - range.Item(0)) + range.Item(0)

you can use:
dLenght = range.ParameterAt(rLengthRandom.NextDouble)

If i was going to write it from scratch, it would be like this:

Public Function randomvectors(ByVal range As Interval, ByVal intNumber As Integer)
Dim rnd As New random(1729)
Dim listVectors As New List(Of Vector3d)

For i As Integer = 1 To intNumber
Dim v As vector3d = vector3d.XAxis
v.Rotate(math.aCos(rnd.NextDouble * 2 - 1) - math.PI / 2, vector3d.YAxis)
v.Rotate(rnd.NextDouble * math.PI * 2, vector3d.ZAxis)
v *= range.ParameterAt(rnd.NextDouble)
listvectors.Add(v)
Next

Return listVectors
End Function

This should create a more even distribution without vectors concentrating in the poles of the imaginary sphere.

Nice to see other point of view about :) I've seen that you use rotate Vector3d class function instead of VB.NET clases and functions to perform the vector creation and I have a question about that: Is not better to keep the algorithm as free as possible of not System classes and methods to improve performance? It is something that I've always take care about and I don't know if it is false or it really improve your code performance.

It's true that you declare less variables (less memory and resources taked by them). Could be nice to make some test about code performance and see how every algorithm do the work, indeed could be nice to have some kind of repository of simple functions that anybody could edit and improve. A GitHub repository could do the work ;)

I don't think the rotate vector method is doing something very different than if you were going to implement trigonometry/matrix transformations to find out the rotation yourself. It might have a bit more overhead (or not, I have no idea, both scripts seems equally as fast), but I guess the puropose of scripting inside Rhino is to take advantage of all these functions and not have to do them ourselves.

 

Nevertheless, the main advantage of the last code I posted is that the distribution of points is more even:
left - your code (notice the concentration in the poles)
right - my last code

hi!, it seams that the problem wasn't the randomvectors function ;) Anyway loving the way you get a random number between interval extremes really economic to type and very straight forward. 

The that distribution I thin I've solved it changing the way I generate random numbers, but I must check it better.

Thanks for the answer ;)

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service