matrix datatype in Rhinocommon - Grasshopper2024-03-29T16:00:01Zhttps://www.grasshopper3d.com/forum/topics/matrix-datatype-in-rhinocommon?commentId=2985220%3AComment%3A540177&xg_source=activity&feed=yes&xn_auth=noAndrew thanks for your code e…tag:www.grasshopper3d.com,2013-06-21:2985220:Comment:8756042013-06-21T14:48:26.802ZDaniël Smidthttps://www.grasshopper3d.com/profile/DanielSmidt
<p>Andrew thanks for your code examples, it helps in comprehension of the Matrix issue in GH.</p>
<p></p>
<p>However, since the Transform data type is a 4x4 matrix it can't be used to get in bigger Matrices.</p>
<p>Does anybody know another smart way of getting a matrix in a custom component (C#)?</p>
<p>Andrew thanks for your code examples, it helps in comprehension of the Matrix issue in GH.</p>
<p></p>
<p>However, since the Transform data type is a 4x4 matrix it can't be used to get in bigger Matrices.</p>
<p>Does anybody know another smart way of getting a matrix in a custom component (C#)?</p> I just committed a fix for th…tag:www.grasshopper3d.com,2012-02-20:2985220:Comment:5408502012-02-20T17:15:46.386ZSteve Baerhttps://www.grasshopper3d.com/profile/SteveBaer
<p>I just committed a fix for this so you should be able to get Matrix support in V4 SR9 when the next grasshopper version is released.</p>
<p>Thanks,</p>
<p>-Steve</p>
<p>I just committed a fix for this so you should be able to get Matrix support in V4 SR9 when the next grasshopper version is released.</p>
<p>Thanks,</p>
<p>-Steve</p> Ah. Would seem to be a rhino…tag:www.grasshopper3d.com,2012-02-19:2985220:Comment:5399062012-02-19T16:56:18.581ZAndrew Heumannhttps://www.grasshopper3d.com/profile/AndrewHeumann
<p>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.</p>
<p>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.</p> I'm using Rhino 4.0 SR9 / GH…tag:www.grasshopper3d.com,2012-02-19:2985220:Comment:5400002012-02-19T16:35:29.514ZJacek Jaskólskihttps://www.grasshopper3d.com/profile/JacekJaskolski
<p>I'm using Rhino 4.0 SR9 / GH 0.8.0066</p>
<p>I'm using Rhino 4.0 SR9 / GH 0.8.0066</p> another thing, I'm getting th…tag:www.grasshopper3d.com,2012-02-19:2985220:Comment:5401872012-02-19T16:13:24.639ZJacek Jaskólskihttps://www.grasshopper3d.com/profile/JacekJaskolski
<p>another thing, I'm getting this:</p>
<p><em>error: Unable to find an entry point named 'ON_Matrix_New' in DLL 'rhcommon_c'. (line: 0)</em></p>
<p>when I declare the matrix type variable.</p>
<p>This happens whether I use C#<br/><code>Matrix mx = new Matrix(5,6);</code><br/>or VB<br/><code>Dim mx As New Matrix(5, 6)</code></p>
<p>Any ideas what could be causing it?</p>
<p>another thing, I'm getting this:</p>
<p><em>error: Unable to find an entry point named 'ON_Matrix_New' in DLL 'rhcommon_c'. (line: 0)</em></p>
<p>when I declare the matrix type variable.</p>
<p>This happens whether I use C#<br/><code>Matrix mx = new Matrix(5,6);</code><br/>or VB<br/><code>Dim mx As New Matrix(5, 6)</code></p>
<p>Any ideas what could be causing it?</p> Andrew,
I'm amazed by your co…tag:www.grasshopper3d.com,2012-02-19:2985220:Comment:5399032012-02-19T16:08:00.079ZJacek Jaskólskihttps://www.grasshopper3d.com/profile/JacekJaskolski
<p>Andrew,</p>
<p>I'm amazed by your comprehensive response, this is great help, thank you!</p>
<p>Andrew,</p>
<p>I'm amazed by your comprehensive response, this is great help, thank you!</p> It seems like Andrew below he…tag:www.grasshopper3d.com,2012-02-19:2985220:Comment:5401792012-02-19T15:43:18.365ZGiulio Piacentinohttps://www.grasshopper3d.com/profile/GiulioPiacentino
<p>It seems like Andrew below here answered all these questions. He wrote the answer in C#.</p>
<p>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).</p>
<p>- Giulio<br/>_______________<br/>giulio@mcneel.com</p>
<p>It seems like Andrew below here answered all these questions. He wrote the answer in C#.</p>
<p>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).</p>
<p>- Giulio<br/>_______________<br/>giulio@mcneel.com</p> Here's a function that does i…tag:www.grasshopper3d.com,2012-02-19:2985220:Comment:5401772012-02-19T15:38:00.879ZAndrew Heumannhttps://www.grasshopper3d.com/profile/AndrewHeumann
<p>Here's a function that does it, though there might be an easier way. </p>
<p><code><br/> Transform TFromMatrix(Matrix m){<br/> Transform T = new Transform();<br/> if(m.ColumnCount == 4 && m.RowCount == 4){<br/> for(int i = 0;i < 4;i++){<br/> for(int j = 0;j < 4;j++){<br/> T[i, j] = m[i, j];<br/> }<br/> }<br/> }<br/> return T;<br/> }<br/><br/></code></p>
<p> </p>
<p>Here's a function that does it, though there might be an easier way. </p>
<p><code><br/> Transform TFromMatrix(Matrix m){<br/> Transform T = new Transform();<br/> if(m.ColumnCount == 4 && m.RowCount == 4){<br/> for(int i = 0;i < 4;i++){<br/> for(int j = 0;j < 4;j++){<br/> T[i, j] = m[i, j];<br/> }<br/> }<br/> }<br/> return T;<br/> }<br/><br/></code></p>
<p> </p> And just saw your other quest…tag:www.grasshopper3d.com,2012-02-19:2985220:Comment:5399822012-02-19T15:30:00.967ZAndrew Heumannhttps://www.grasshopper3d.com/profile/AndrewHeumann
<p>And just saw your other questions: </p>
<p></p>
<p>1. Matrix mx = new Matrix(5, 6); will declare a new 5x6 Matrix </p>
<p>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.</p>
<p>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.</p>
<p>And just saw your other questions: </p>
<p></p>
<p>1. Matrix mx = new Matrix(5, 6); will declare a new 5x6 Matrix </p>
<p>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.</p>
<p>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.</p> Because the scripting compone…tag:www.grasshopper3d.com,2012-02-19:2985220:Comment:5401042012-02-19T15:25:24.740ZAndrew Heumannhttps://www.grasshopper3d.com/profile/AndrewHeumann
<p>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:</p>
<p><code>private void RunScript(System.Object x, ref object A)<br></br> {<br></br> Transform tx = (Transform) x;<br></br>}</code></p>
<p>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…</p>
<p>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:</p>
<p><code>private void RunScript(System.Object x, ref object A)<br/> {<br/> Transform tx = (Transform) x;<br/>}</code></p>
<p>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:</p>
<p><code>Matrix mx = new Matrix(tx);</code></p>
<p> </p>
<p> </p>
<p>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:</p>
<p><code>Transform tx = (Transform) x;<br/> Transform Inverse = new Transform();<br/> tx.TryGetInverse(out Inverse);<br/> A = Inverse;</code></p>
<p> </p>
<p>I hope that's helpful!</p>