Grasshopper

algorithmic modeling for Rhino

Hi,

I want to perform matrix operations (multiply, invert, etc.) and I know this can be done using two dimensional arrays and some custom functions. I tried this example code and it worked.

But it seems there is a special matrix class available in the Rhinocommon. Could anyone explain how to use it? I'm a beginner in programming and there're no examples in the SDK to start with...

Thanks,

Jacek

Views: 3712

Replies to This Discussion

i suppose that this is transformation matrix...

In the particular case I'm thinking to implement it -  yes, but I'm also interrested in the general (any) matrix operations.

Hi Jacek,

  • Rhino.Geometry.Matrix
    Represents an arbitrarily sized matrix of double-precision floating point numbers. If you are working with a 4x4 matrix, then you may want to use the Transform class instead
  • Rhino.Geometry.Transform
    Represents the values in a 4x4 transform matrix.

    This is parallel to C++ ON_Xform.

So Matrix is general and Transform is meant to be a transformation matrix only.

For an example, see: http://wiki.mcneel.com/developer/rhinocommonsamples/transformbrep

Is this helpful to get started?

Thanks,

- Giulio
_______________
giulio@mcneel.com

Hi Giulio,

This is very helpful, thanks.

Though it still is unclear to me how to:

1. declare a Matrix type with sepcifying it's dimensions

2. set data to a Matrix type (with Transform I can access each element directly, ie. M12, or use the predefined transformations such as translation, rotation, etc)

2. convert Rhino.Geometry.Transform to Rhino.Geometry.Matrix and back (for example to do an inversion)

It seems like Andrew below here answered all these questions. He wrote the answer in C#.

Instantiating and index retrieval is pretty much the language default (new keyword and indexers [i, j]). In Vb.Net it would be New and (i, j).

- Giulio
_______________
giulio@mcneel.com

Because the scripting components don't accept matrix types by default, if you want to operate on a matrix generated in GH, you'll have to bring it in as System.Object and cast it. At its simplest this looks a bit like this:

private void RunScript(System.Object x, ref object A)
{
Transform tx = (Transform) x;
}

To be a little more sophisticated you might want to build in some form of error checking to verify that the cast worked. You can convert this into a more generic matrix with the Matrix constructor that accepts a transform:

Matrix mx = new Matrix(tx);

 

 

Once you've got your matrix in whatever form you need it, you can call any of the functions in the SDK pages that Giulio linked to. If your goal is to invert one of the GH transformation matrices, something like the following will do the trick:

Transform tx = (Transform) x;
Transform Inverse = new Transform();
tx.TryGetInverse(out Inverse);
A = Inverse;

 

I hope that's helpful!

And just saw your other questions: 

1. Matrix mx = new Matrix(5, 6); will declare a new 5x6 Matrix 

2. using the "item" property you can set and get specific matrix values - mx[3,2] = 2.0; will set the value of the cell at 3,2.

3. You don't need to convert back and forth to do an inversion, as my example above shows. Not sure what the easiest way would be to convert a matrix back to a transform.

Here's a function that does it, though there might be an easier way. 


Transform TFromMatrix(Matrix m){
Transform T = new Transform();
if(m.ColumnCount == 4 && m.RowCount == 4){
   for(int i = 0;i < 4;i++){
      for(int j = 0;j < 4;j++){
        T[i, j] = m[i, j];
      }
   }
}
return T;
}

 

Andrew,

I'm amazed by your comprehensive response, this is great help, thank you!

another thing, I'm getting this:

error: Unable to find an entry point named 'ON_Matrix_New' in DLL 'rhcommon_c'. (line: 0)

when I declare the matrix type variable.

This happens whether I use C#
Matrix mx = new Matrix(5,6);
or VB
Dim mx As New Matrix(5, 6)

Any ideas what could be causing it?

I'm using Rhino 4.0 SR9 / GH 0.8.0066

Ah. Would seem to be a rhino 4.0 vs rhino 5.0 thing. Both expressions work fine for me in Rh5 - but not in rhino 4.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service