Can you explain << operator?

Hello, I am studying code of TIG from his extension TextureTools. His function for rotate of texture:uses << operator often:

def process(face=nil, angle=0.0)
  return nil unless face && face.is_a?(Sketchup::Face)
  return nil unless mat=face.material
  return nil unless mat.texture
  ###
  tw=Sketchup.create_texture_writer
  samples=[face.outer_loop.vertices[0].position]  ### 0,0 | Origin
  samples << samples[0].offset(face.normal.axes.x)### 1,0 | Offset Origin in X
  samples << samples[1].offset(face.normal.axes.y)### 1,1 | Offset X in Y
  samples << samples[0].offset(face.normal.axes.y)### 0,1 | Offset Origin in Y
  pts=[]### Array containing 3D points.
  uvs=[]### Array containing UV points.
  uvh=face.get_UVHelper(true, false, tw)
  samples.each{|pt|
    pts << pt                                      ### XYZ 3D coordinates 
    uvs << self.normalizeUVQ(uvh.get_front_UVQ(pt))### UVQ 2D coordinates
  }
  ###
  tr=Geom::Transformation.rotation(pts[0], face.normal, angle.degrees)
  tpts=[pts[0], uvs[0]]
  (1..3).each{|i|
    tpts << pts[i].transform(tr)
    tpts << uvs[i]
  }
  face.position_material(mat, tpts, true)
  @model.active_view.refresh
end

I would appreciate very much if you can explain step by step what is happening there from line #8 to #10 and from #13 to #18.

Also the part

  tpts=[pts[0], uvs[0]]
  (1..3).each{|i|
    tpts << pts[i].transform(tr)
    tpts << uvs[i]
  }

is mysterious.

There is also normalization function

def normalizeUVQ(uvq)
    uvq[0] /= uvq[2]
    uvq[1] /= uvq[2]
    uvq[2] = 1.0
    return uvq
end

It’s not clear to me what does this do and why it is needed in the code.

<< is being used an on Array - it is the append operator. It is the same as:

array.push(value)

See: Class: Array (Ruby 2.2.4)

/= is a shortcut for division and assignment. for example:

 a /= b

is the same as

a = a / b

Similarly:

a = a + 1 

is the same as:

a += 1
1 Like

I know the rest of operators, but the append operator is new to me. Thank you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.