Class: Aims::Plane

Inherits:
Object
  • Object
show all
Includes:
Vectorize
Defined in:
lib/aims/plane.rb

Overview

A Class representing a plane. Internally stored in Hessian Normal Form. 0 = Ax + By + Cz - D

(A,B,C) is the plane normal. D is the distance from the origin.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Vectorize

#cross, #dot

Constructor Details

#initialize(a, b, c, x = 0, y = 0, z = 0) ⇒ Plane

Initialize this plane with the normal (a,b,c) and a point (x,y,z) on the plane



16
17
18
19
20
21
22
23
24
# File 'lib/aims/plane.rb', line 16

def initialize(a, b, c, x=0, y=0, z=0)
  @a = a
  @b = b
  @c = c
  if (@a == 0 and @b == 0 and @c == 0)
    raise "Invalid definition of plane."
  end
  @d = a*x + b*y + c*z
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



10
11
12
# File 'lib/aims/plane.rb', line 10

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



10
11
12
# File 'lib/aims/plane.rb', line 10

def b
  @b
end

#cObject (readonly)

Returns the value of attribute c.



10
11
12
# File 'lib/aims/plane.rb', line 10

def c
  @c
end

#dObject (readonly)

Returns the value of attribute d.



10
11
12
# File 'lib/aims/plane.rb', line 10

def d
  @d
end

Instance Method Details

#==(aPlane) ⇒ Object Also known as: eql?

Two planes are equal if there ABCD parameters are equal



95
96
97
# File 'lib/aims/plane.rb', line 95

def ==(aPlane)
  @a == aPlane.a and @b == aPlane.b and @c == aPlane.c and @d == aPlane.d
end

#any_point_on_planeObject

return some arbitrary point on the plane



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/aims/plane.rb', line 27

def any_point_on_plane
  
  unless (@c == 0)
    return Vector[0, 0, @d/@c]
  end
  
  unless (@b == 0)
    return Vector[0, @d/@b, 0]
  end
  
  unless (@a == 0)
    return Vector[@d/@a, 0, 0]
  end
  
  # Actually if we get to this point, the plane undetermined and all of R3 satisfies the definition
  return Vector[0,0,0]
end

#displace_along_normal(distance) ⇒ Object

Displace this plane a distance in the direction of its normal



90
91
92
# File 'lib/aims/plane.rb', line 90

def displace_along_normal(distance)
  @d += distance
end

#distance_to_point(x, y, z) ⇒ Object

Return the distance to point (x,y,z)

distance = D - Ax - By - Cz



48
49
50
# File 'lib/aims/plane.rb', line 48

def distance_to_point(x, y, z)
  a*x + b*y + c*z - d
end

#intersection_with_ray(a, b) ⇒ Object

The equation for the interstion of ray defined by r(t) = (a + b*t)

a, b

are vectors where a is the tail of the ray and b points in the direction of the ray and t > 0

and the plane normal Pn = [A B C]

Substituting r(t) into the equation of the plane gives:

A(ax + bx*t) + B(ay + by*t) + C(az + bz*t) + D = 0

Solve for t t = (Pn dot a + D)/(Pn dot b) = V0 / Vd if:

Vd = 0, then no intersection
t < 0, then intersection behind ray origin

Find point of intersection

(ax + bx*t) (ay + by*t) (az + bz*t)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/aims/plane.rb', line 73

def intersection_with_ray(a, b)
  n = self.unit_normal
  vd = dot(n, b)
  if vd == 0
    return nil
  else
    v0 = dot(n, a) + @d
    t = -v0/vd
    if t < 0
      return nil
    else
      return a + b*t
    end
  end
end

#unit_normalObject

Return the unit normal Vector[a, b, c]



53
54
55
56
# File 'lib/aims/plane.rb', line 53

def unit_normal
  v = Vector[@a, @b, @c]
  v*(1/v.r)
end