Class: CATransform3D

Inherits:
Object
  • Object
show all
Defined in:
lib/geomotion/ca_transform_3d.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.identityCATransform3D

Returns the CATransform3D identity matrix

Returns:



109
110
111
# File 'lib/geomotion/ca_transform_3d.rb', line 109

def self.identity
  CATransform3DMakeTranslation(0, 0, 0)
end

.make(options = {}) ⇒ Object

CATransform3D.make # default transform: identity matrix # make a transform from scratch. please don’t ever do this ;-) CATransform3D.make(

m11: 1, m12: 0, m13: 0, m14: 0,
m21: 0, m22: 1, m23: 0, m24: 0,
m31: 0, m32: 0, m33: 1, m34: 0,
m41: 0, m42: 0, m43: 0, m44: 1,

)

# make a transform using primitives CATransform3D.make(scale: 2, translate: [10, 10, 0], rotate: Math::PI)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/geomotion/ca_transform_3d.rb', line 13

def self.make(options = {})
  if options[:m11]
    args = [
      :m11, :m12, :m13, :m14,
      :m21, :m22, :m23, :m24,
      :m31, :m32, :m33, :m34,
      :m41, :m42, :m43, :m44,
    ].map do |key|
      raise "#{key.inspect} is required" unless options.key? key
      options[key]
    end
    return self.new(*args)
  else
    retval = self.identity
    if options[:translate]
      retval = retval.translate(options[:translate])
    end
    if options[:scale]
      retval = retval.scale(options[:scale])
    end
    if options[:rotate]
      retval = retval.rotate(options[:rotate])
    end
    return retval
  end
end

.perspective(x, y) ⇒ Object

A perspective transform is a lot like a rotation… but different. These numbers should be very small, like 0.002 is a decent perspective shift.

Returns:

  • CATransform3D



103
104
105
# File 'lib/geomotion/ca_transform_3d.rb', line 103

def self.perspective(x, y)
  CATransform3D.new(1,0,0,x, 0,1,0,y, 0,0,1,0, 0,0,0,1)
end

.rotate(angle, point = nil, y = nil, z = nil) ⇒ CATransform3D

Returns a transform that is rotated by ‘angle` (+ => counterclockwise, - => clockwise)

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/geomotion/ca_transform_3d.rb', line 75

def self.rotate(angle, point=nil, y=nil, z=nil)
  if point && y && z
    x = point
  elsif point
    x = point[0]
    y = point[1]
    z = point[2]
  else
    # default: spins around z-axis
    x = 0
    y = 0
    z = 1
  end
  CATransform3DMakeRotation(angle, x, y, z)
end

.scale(scale, sy = nil, sz = nil) ⇒ Object

Returns a transform that is scaled. Accepts one or three arguments. One argument can be an Array with three items or a number that will be used to scale both x and y directions (z will be scale 1). Three arguments should be the x, y, z values.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/geomotion/ca_transform_3d.rb', line 59

def self.scale(scale, sy=nil, sz=nil)
  if sy
    sx = scale
  elsif scale.is_a?(Numeric)
    sx = sy = scale
    sz = 1
  else
    sx = scale[0]
    sy = scale[1]
    sz = scale[2]
  end
  CATransform3DMakeScale(sx, sy, sz)
end

.shear(px, py) ⇒ Object

A “shear” translation turns a rectangle into a parallelogram. Delegates to the CGAffineTransform with the same name

Parameters:

  • px (Numeric)

    how much to shear in x direction

  • py (Numeric)

    how much to shear in x direction

Returns:

  • CATransform3D



96
97
98
# File 'lib/geomotion/ca_transform_3d.rb', line 96

def self.shear(px, py)
  CGAffineTransform.shear(px, py).to_transform3d
end

.translate(point, ty = nil, tz = nil) ⇒ CATransform3D

Returns a transform that is translated. Accepts one or three arguments. One argument can be an Array with three numbers, three arguments should be the x, y, z values.

Returns:



44
45
46
47
48
49
50
51
52
53
# File 'lib/geomotion/ca_transform_3d.rb', line 44

def self.translate(point, ty=nil, tz=nil)
  if ty
    tx = point
  else
    tx = point[0]
    ty = point[1]
    tz = point[2]
  end
  CATransform3DMakeTranslation(tx, ty, tz)
end

Instance Method Details

#+@CATransform3D

Returns self

Returns:



137
138
139
# File 'lib/geomotion/ca_transform_3d.rb', line 137

def +@
  self
end

#-(transform) ⇒ CATransform3D

Inverts the second transform and adds the result to ‘self`

Returns:



158
159
160
# File 'lib/geomotion/ca_transform_3d.rb', line 158

def -(transform)
  self.concat transform.invert
end

#==(transform) ⇒ Boolean

Returns true if the two matrices are equal.

Returns:

  • (Boolean)

    true if the two matrices are equal



131
132
133
# File 'lib/geomotion/ca_transform_3d.rb', line 131

def ==(transform)
  CATransform3DEqualToTransform(self, transform)
end

#affine?Boolean

Return true if the receiver can be represented as an affine transform

Returns:

  • (Boolean)


121
122
123
# File 'lib/geomotion/ca_transform_3d.rb', line 121

def affine?
  CATransform3DIsAffine(self)
end

#concat(transform) ⇒ CATransform3D Also known as: +, <<

Concatenates the two transforms

Returns:



143
144
145
# File 'lib/geomotion/ca_transform_3d.rb', line 143

def concat(transform)
  CATransform3DConcat(self, transform)
end

#identity?Boolean

Return true if the receiver is the identity matrix, false otherwise

Returns:

  • (Boolean)


115
116
117
# File 'lib/geomotion/ca_transform_3d.rb', line 115

def identity?
  CATransform3DIsIdentity(self)
end

#invertCATransform3D Also known as: -@

Inverts the transform

Returns:



151
152
153
# File 'lib/geomotion/ca_transform_3d.rb', line 151

def invert
  CATransform3DInvert(self)
end

#perspective(x, y) ⇒ Object

Applies a perspective transform to the receiver

Returns:

  • CATransform3D



215
216
217
# File 'lib/geomotion/ca_transform_3d.rb', line 215

def perspective(x, y)
  self.concat CATransform3D.perspective(x, y)
end

#rotate(angle, point = nil, y = nil, z = nil) ⇒ CATransform3D

Applies a rotation transform to the receiver

Returns:



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/geomotion/ca_transform_3d.rb', line 193

def rotate(angle, point=nil, y=nil, z=nil)
  if point && y && z
    x = point
  elsif point
    x = point[0]
    y = point[1]
    z = point[2]
  else
    # default: spins around z-axis
    x = 0
    y = 0
    z = 1
  end
  CATransform3DRotate(self, angle, x, y, z)
end

#scale(scale, sy = nil, sz = nil) ⇒ CATransform3D

Applies a scale transform to the receiver

Returns:



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/geomotion/ca_transform_3d.rb', line 177

def scale(scale, sy=nil, sz=nil)
  if sy
    sx = scale
  elsif scale.is_a?(Numeric)
    sx = sy = scale
    sz = 1
  else
    sx = scale[0]
    sy = scale[1]
    sz = scale[2]
  end
  CATransform3DScale(self, sx, sy, sz)
end

#shear(px, py) ⇒ Object



209
210
211
# File 'lib/geomotion/ca_transform_3d.rb', line 209

def shear(px, py)
  self.concat CGAffineTransform.shear(px, py).to_transform3d
end

#to_aObject



223
224
225
# File 'lib/geomotion/ca_transform_3d.rb', line 223

def to_a
  [self.m11, self.m12, self.m13, self.m14, self.m21, self.m22, self.m23, self.m24, self.m31, self.m32, self.m33, self.m34, self.m41, self.m42, self.m43, self.m44]
end

#to_affine_transformCGAffineTransform

Returns:



126
127
128
# File 'lib/geomotion/ca_transform_3d.rb', line 126

def to_affine_transform
  CATransform3DGetAffineTransform(self)
end

#to_ns_valueObject



219
220
221
# File 'lib/geomotion/ca_transform_3d.rb', line 219

def to_ns_value
  NSValue.valueWithCATransform3D(self)
end

#translate(point, ty = nil, tz = nil) ⇒ CATransform3D

Applies a translation transform to the receiver

Returns:



164
165
166
167
168
169
170
171
172
173
# File 'lib/geomotion/ca_transform_3d.rb', line 164

def translate(point, ty=nil, tz=nil)
  if ty
    tx = point
  else
    tx = point[0]
    ty = point[1]
    tz = point[2]
  end
  CATransform3DTranslate(self, tx, ty, tz)
end