(works on Mac OMG) Voxel problem: 'Volume Cube' equivalent in gh

Not very hard to replicate the you tube video in Grasshopper, just seems less fast



using System.Threading.Tasks;

/// <summary>
/// This class will be instantiated on demand by the Script component.
/// </summary>
public class Script_Instance : GH_ScriptInstance
{
#region Utility functions
  /// <summary>Print a String to the [Out] Parameter of the Script component.</summary>
  /// <param name="text">String to print.</param>
  private void Print(string text) { /* Implementation hidden. */ }
  /// <summary>Print a formatted String to the [Out] Parameter of the Script component.</summary>
  /// <param name="format">String format.</param>
  /// <param name="args">Formatting parameters.</param>
  private void Print(string format, params object[] args) { /* Implementation hidden. */ }
  /// <summary>Print useful information about an object instance to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj) { /* Implementation hidden. */ }
  /// <summary>Print the signatures of all the overloads of a specific method to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }
#endregion

#region Members
  /// <summary>Gets the current Rhino document.</summary>
  private readonly RhinoDoc RhinoDocument;
  /// <summary>Gets the Grasshopper document that owns this script.</summary>
  private readonly GH_Document GrasshopperDocument;
  /// <summary>Gets the Grasshopper script component that owns this script.</summary>
  private readonly IGH_Component Component;
  /// <summary>
  /// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.
  /// Any subsequent call within the same solution will increment the Iteration count.
  /// </summary>
  private readonly int Iteration;
#endregion

  /// <summary>
  /// This procedure contains the user code. Input parameters are provided as regular arguments,
  /// Output parameters as ref arguments. You don't have to assign output parameters,
  /// they will have a default value.
  /// </summary>
  private void RunScript(List<Point3d> pts, double w, int itterations, List<double> cValues, ref object A)
  {

    Point4d c = new Point4d(cValues[0], cValues[1], cValues[2], cValues[3]);

    double[] cValuesOut = new double[pts.Count];
    Parallel.For(0, pts.Count, i =>
      {
      cValuesOut[i] = Julia(new Point4d(pts[i].X, pts[i].Y, pts[i].Z, w), c, itterations);
      }
      );
    A = cValuesOut;
  }

  // <Custom additional code> 

  double Julia (Point4d q, Point4d c, int itterations)
  {
    double output = 0;
    for (int i = 0; i < itterations; i++)
    {
      q = SquarePoint4dQuaternion(q) + c;
      if ((q.X * q.X + q.Y * q.Y + q.Z * q.Z) > 100) break;
    }
    return Math.Sqrt(q.X * q.X + q.Y * q.Y + q.Z * q.Z);
  }
  Point4d SquarePoint4dQuaternion(Point4d pt)
  {
    double x = pt.X * pt.X - (pt.Y * pt.Y + pt.Z * pt.Z + pt.W * pt.W);
    double y = 2 * pt.X * pt.Y;
    double z = 2 * pt.X * pt.Z;
    double w = 2 * pt.X * pt.W;
    return new Point4d(x, y, z, w);
  }


  // </Custom additional code> 
}
2 Likes