Move vertex using absolute coordinate
-
Hi, i'm new to ruby. I'd like to know how to move vertex using absolute coordinate ? i used transform_by_vectors and transform_entities , they only provide relative transformation.
Thanx
-
Calculate the final destination based on the original position.
vector = vertex.position.vector_to( new_position ) entities.transform_by_vectors( [vertex], [vector] )
-
trt=Geom:Transformation.translation(vector)
then
obj.transform!(trt)
moves the object by length of the vector in the direction of the vector.
trn=Geom:Transformation.new(point)
then
obj.transform!(trn)
moves the object to the specified point irrespective of where is is beforehand.
To move an array of vertices you can use
entities.transform_entities(trt, vertices)
when they'll all shunt over in the direction of vector by the length of vector.
If you have a collection of different vectors for different vertices then use
entities.transform_by_vectors(vertices_array, vectors_array)
You don't want to to apply a 'move'
trn
type of transformation as they'd all end up coincident !If you have an array of 'vertices' and a matching array of their new absolute 'positions'... then use an iteration...
vertices.each_with_index{|v, i| pt=positions[i] tr=Geom:Transformation.new(pt) entities.transform_entities(tr, v) }
Each vertex in turn is then assigned the new position [point]... -
@tig said:
If you have an array of 'vertices' and a matching array of their new absolute 'positions'... then use an iteration...
vertices.each_with_index{|v, i| pt=positions[i] tr=Geom:Transformation.new(pt) entities.transform_entities(tr, v) }
Each vertex in turn is then assigned the new position [point]...Very inefficient! You want to transform all at once.
What I do for vertex tools is by usingtransform_by_vectors
<span class="syntaxdefault"><br />vectors </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[]<br /></span><span class="syntaxdefault">vertices</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each_with_index</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">vertex</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> i</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault">  new_position </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> new_positions_array</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">  vectors </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> vertex</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">vector_to</span><span class="syntaxkeyword">( </span><span class="syntaxdefault">new_position </span><span class="syntaxkeyword">)<br />}<br /></span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transform_by_vectors</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> vertices</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vectors </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span>
At all times try to use bulk methods that updates multiple entities at the same time. Even for selections, don't use add/remove within an iterator - use a cache array and update in bulk afterwards. It's the difference between minutes and seconds.
-
That is a much more efficient way of doing it [transforming [or erasing] entities en mass is often preferable]... But in my defense... the title of the post is
move **vertex**...
NOTmove **vertices**...
- so there is probably no significant benefit in the en mass way when he's moving just one or two or three vertices - obviously if a whole load of things are changing a time lag will be noticeable...
Advertisement