[Code] Transforming in custom coordinate systems
-
Here's a snippet that I had to bend my mind to understand. I wanted to scale entities along the axis of the currently open group or component using one transformation on the entities.
Entities in SketchUp are transformed in global coordinates so I had to transform my local scaling transformation into one that worked in the global space.
I couldn't get it working for a long time, until I re-read this Wikipeadia article:
http://en.wikipedia.org/wiki/Transformation_matrix#Composing_and_inverting_transformations@unknownuser said:
(This is called the Associative property.) In other words, the matrix of the combined transformation A followed by B is simply the product of the individual matrices. Note that the multiplication is done in the opposite order from the English sentence: the matrix of "A followed by B" is BA, not AB.
<span class="syntaxdefault"><br /></span><span class="syntaxcomment"># This will scale selected face 2x in the X direction<br /># of the currently open group / component<br /></span><span class="syntaxdefault">face </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">local_coords </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">edit_transform<br />local_scale </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Transformation</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">scaling</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> 2</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 1 </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transform_entities</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> local_coords </span><span class="syntaxkeyword">*</span><span class="syntaxdefault"> local_scale </span><span class="syntaxkeyword">*</span><span class="syntaxdefault"> local_coords</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">inverse</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> face </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault"></span>
How to scale along a vector in one transformation?
Background I'm trying to understand matrix transformations. (Developing plugins for SketchUp using it's Ruby API) I've been setting up small experiments to split up the learning in small chunks s...
Stack Overflow (stackoverflow.com)
-
Hi,
This solves a big problem since at least for me, maintaining the local axis of the object I want to scale is important. Thanks for this great share!
-
Glad it helped.I spend a loong time on that - I just couldn't warp my head around what was going on. The key was the quote from the Wiki article. Everything makes so much more sense now.
Figured I'd post it here to remind myself.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register LoginAdvertisement