Class: Topolys::Transformation
- Inherits:
-
Object
- Object
- Topolys::Transformation
- Defined in:
- lib/topolys/transformation.rb
Overview
Transformations can be applied to Geometry. Ported from OpenStudio Transformation library.
Instance Attribute Summary collapse
-
#matrix ⇒ Matrix
readonly
Internal 4x4 matrix.
Class Method Summary collapse
-
.rotation(axis, radians) ⇒ Object
Initializes a rotation about origin defined by axis and angle (radians).
-
.translation(translation) ⇒ Object
translation along vector.
Instance Method Summary collapse
-
#*(obj) ⇒ Obj
Multiplies a Transformation by geometry class.
-
#initialize(matrix = Matrix.identity(4)) ⇒ Transformation
constructor
Initializes an Transformation object.
Constructor Details
#initialize(matrix = Matrix.identity(4)) ⇒ Transformation
Initializes an Transformation object
22 23 24 25 |
# File 'lib/topolys/transformation.rb', line 22 def initialize(matrix=Matrix.identity(4)) raise "Incorrect argument for Transformation, expected Matrix but got #{matrix.class}" unless matrix.is_a?(Matrix) @matrix = matrix end |
Instance Attribute Details
#matrix ⇒ Matrix (readonly)
Returns internal 4x4 matrix.
16 17 18 |
# File 'lib/topolys/transformation.rb', line 16 def matrix @matrix end |
Class Method Details
.rotation(axis, radians) ⇒ Object
Initializes a rotation about origin defined by axis and angle (radians)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/topolys/transformation.rb', line 42 def Transformation.rotation(axis, radians) return nil if !axis.is_a?(Vector3D) return nil if !radians.is_a?(Numeric) return nil if (axis.magnitude < Float::EPSILON) normal = axis normal.normalize! # Rodrigues' rotation formula / Rotation matrix from Euler axis/angle # I*cos(radians) + I*(1-cos(radians))*axis*axis^T + Q*sin(radians) # Q = [0, -axis[2], axis[1]; axis[2], 0, -axis[0]; -axis[1], axis[0], 0] p = normal.outer_product(normal) i = Matrix.identity(3) q = Matrix.zero(3) q[0,1] = -normal.z q[0,2] = normal.y q[1,0] = normal.z q[1,2] = -normal.x q[2,0] = -normal.y q[2,1] = normal.x # rotation matrix r = i*Math.cos(radians) + (1-Math.cos(radians))*p + q*Math.sin(radians) matrix = Matrix.identity(4) matrix[0,0] = r[0,0] matrix[0,1] = r[0,1] matrix[0,2] = r[0,2] matrix[1,0] = r[1,0] matrix[1,1] = r[1,1] matrix[1,2] = r[1,2] matrix[2,0] = r[2,0] matrix[2,1] = r[2,1] matrix[2,2] = r[2,2] return Transformation.new(matrix) end |
.translation(translation) ⇒ Object
translation along vector
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/topolys/transformation.rb', line 28 def Transformation.translation(translation) return nil if !translation.is_a?(Vector3D) matrix = Matrix.identity(4) matrix[0,3] = translation.x matrix[1,3] = translation.y matrix[2,3] = translation.z return Transformation.new(matrix) end |
Instance Method Details
#*(obj) ⇒ Obj
Multiplies a Transformation by geometry class
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/topolys/transformation.rb', line 85 def *(obj) if obj.is_a?(Point3D) return mult_point(obj) elsif obj.is_a?(Vector3D) return mult_vector(obj) elsif obj.is_a?(Array) return mult_array(obj) elsif obj.is_a?(Transformation) return mult_transformation(obj) end return nil end |