Class: Geo3d::Plane
- Inherits:
-
Object
- Object
- Geo3d::Plane
- Defined in:
- lib/geo3d/plane.rb
Instance Attribute Summary collapse
-
#a ⇒ Object
(also: #x)
Returns the value of attribute a.
-
#b ⇒ Object
(also: #y)
Returns the value of attribute b.
-
#c ⇒ Object
(also: #z)
Returns the value of attribute c.
-
#d ⇒ Object
(also: #w)
Returns the value of attribute d.
Class Method Summary collapse
Instance Method Summary collapse
- #!=(vec) ⇒ Object
- #==(p) ⇒ Object
- #dot(v) ⇒ Object
-
#initialize(*args) ⇒ Plane
constructor
A new instance of Plane.
- #line_intersection(line_start, line_end) ⇒ Object
- #normal ⇒ Object
- #normalize ⇒ Object
- #normalize! ⇒ Object
- #to_a ⇒ Object
- #transform(matrix, use_inverse_transpose = true) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Plane
Returns a new instance of Plane.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/geo3d/plane.rb', line 9 def initialize *args @a = 0.0 @b = 0.0 @c = 0.0 @d = 0.0 @a = args[0].to_f if args.size > 0 @b = args[1].to_f if args.size > 1 @c = args[2].to_f if args.size > 2 @d = args[3].to_f if args.size > 3 end |
Instance Attribute Details
#a ⇒ Object Also known as: x
Returns the value of attribute a.
3 4 5 |
# File 'lib/geo3d/plane.rb', line 3 def a @a end |
#b ⇒ Object Also known as: y
Returns the value of attribute b.
3 4 5 |
# File 'lib/geo3d/plane.rb', line 3 def b @b end |
#c ⇒ Object Also known as: z
Returns the value of attribute c.
3 4 5 |
# File 'lib/geo3d/plane.rb', line 3 def c @c end |
#d ⇒ Object Also known as: w
Returns the value of attribute d.
3 4 5 |
# File 'lib/geo3d/plane.rb', line 3 def d @d end |
Class Method Details
.from_point_and_normal(point, normal) ⇒ Object
26 27 28 29 |
# File 'lib/geo3d/plane.rb', line 26 def self.from_point_and_normal point, normal point.w = 0 self.new normal.x, normal.y, normal.z, -point.dot(normal) end |
.from_points(pv1, pv2, pv3) ⇒ Object
20 21 22 23 24 |
# File 'lib/geo3d/plane.rb', line 20 def self.from_points pv1, pv2, pv3 edge1 = pv2 - pv1 edge2 = pv3 - pv1 from_point_and_normal pv1, edge1.cross(edge2).normalize end |
Instance Method Details
#!=(vec) ⇒ Object
39 40 41 |
# File 'lib/geo3d/plane.rb', line 39 def != vec !(self == vec) end |
#==(p) ⇒ Object
35 36 37 |
# File 'lib/geo3d/plane.rb', line 35 def == p Geo3d::Utils.float_cmp(a, p.a) && Geo3d::Utils.float_cmp(b, p.b) && Geo3d::Utils.float_cmp(c, p.c) && Geo3d::Utils.float_cmp(d, p.d) end |
#dot(v) ⇒ Object
43 44 45 |
# File 'lib/geo3d/plane.rb', line 43 def dot v a * v.x + b * v.y + c * v.z + d * v.w end |
#line_intersection(line_start, line_end) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/geo3d/plane.rb', line 72 def line_intersection line_start, line_end direction = line_end - line_start normal_dot_direction = normal.dot direction if (normal_dot_direction.zero?) nil else temp = (d + normal.dot(line_start)) / normal_dot_direction line_start - direction * temp end end |
#normal ⇒ Object
68 69 70 |
# File 'lib/geo3d/plane.rb', line 68 def normal Vector.new a, b, c end |
#normalize ⇒ Object
62 63 64 65 66 |
# File 'lib/geo3d/plane.rb', line 62 def normalize p = self.class.new a, b, c, d p.normalize! p end |
#normalize! ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/geo3d/plane.rb', line 47 def normalize! norm = Math.sqrt(a*a + b*b + c*c) if norm.zero? @a = 0 @b = 0 @c = 0 @d = 0 else @a /= norm @b /= norm @c /= norm @d /= norm end end |
#to_a ⇒ Object
31 32 33 |
# File 'lib/geo3d/plane.rb', line 31 def to_a [a,b,c,d] end |
#transform(matrix, use_inverse_transpose = true) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/geo3d/plane.rb', line 85 def transform matrix, use_inverse_transpose = true matrix = matrix.inverse.transpose if use_inverse_transpose p = self.class.new p.a = dot matrix.row(0) p.b = dot matrix.row(1) p.c = dot matrix.row(2) p.d = dot matrix.row(3) p end |