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
9 10 11 12 13 14 15 |
# File 'lib/geo3d/plane.rb', line 9 def initialize *args @a, @b, @c, @d = 0.0, 0.0, 0.0, 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
23 24 25 26 |
# File 'lib/geo3d/plane.rb', line 23 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
17 18 19 20 21 |
# File 'lib/geo3d/plane.rb', line 17 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
36 37 38 |
# File 'lib/geo3d/plane.rb', line 36 def != vec !(self == vec) end |
#==(p) ⇒ Object
32 33 34 |
# File 'lib/geo3d/plane.rb', line 32 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
40 41 42 |
# File 'lib/geo3d/plane.rb', line 40 def dot v a * v.x + b * v.y + c * v.z + d * v.w end |
#line_intersection(line_start, line_end) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/geo3d/plane.rb', line 69 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 |
#normalize ⇒ Object
59 60 61 62 63 |
# File 'lib/geo3d/plane.rb', line 59 def normalize p = self.class.new a, b, c, d p.normalize! p end |
#normalize! ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/geo3d/plane.rb', line 44 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
28 29 30 |
# File 'lib/geo3d/plane.rb', line 28 def to_a [a,b,c,d] end |
#transform(matrix, use_inverse_transpose = true) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/geo3d/plane.rb', line 82 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 |