Class: Mittsu::Triangle
- Inherits:
-
Object
- Object
- Mittsu::Triangle
- Defined in:
- lib/mittsu/math/triangle.rb
Instance Attribute Summary collapse
-
#a ⇒ Object
Returns the value of attribute a.
-
#b ⇒ Object
Returns the value of attribute b.
-
#c ⇒ Object
Returns the value of attribute c.
Class Method Summary collapse
-
.barycoord_from_point(point, a, b, c, target = Mittsu::Vector3.new) ⇒ Object
static/instance method to calculate barycoordinates based on: www.blackpawn.com/texts/pointinpoly/default.html.
- .contains_point?(point, a, b, c) ⇒ Boolean
- .normal(a, b, c, target = Mittsu::Vector3.new) ⇒ Object
Instance Method Summary collapse
- #area ⇒ Object
- #barycoord_from_point(point, target = Mittsu::Vector3.new) ⇒ Object
- #clone ⇒ Object
- #contains_point?(point) ⇒ Boolean
- #copy(triangle) ⇒ Object
- #equals(triangle) ⇒ Object
-
#initialize(a = Mittsu::Vector3.new, b = Mittsu::Vector3.new, c = Mittsu::Vector3.new) ⇒ Triangle
constructor
A new instance of Triangle.
- #midpoint(target = Mittsu::Vector3.new) ⇒ Object
- #normal(target = Mittsu::Vector3.new) ⇒ Object
- #plane(target = Mittsu::Plane.new) ⇒ Object
- #set(a, b, c) ⇒ Object
- #set_from_points_and_indices(points, i0, i1, i2) ⇒ Object
Constructor Details
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
3 4 5 |
# File 'lib/mittsu/math/triangle.rb', line 3 def a @a end |
#b ⇒ Object
Returns the value of attribute b.
3 4 5 |
# File 'lib/mittsu/math/triangle.rb', line 3 def b @b end |
#c ⇒ Object
Returns the value of attribute c.
3 4 5 |
# File 'lib/mittsu/math/triangle.rb', line 3 def c @c end |
Class Method Details
.barycoord_from_point(point, a, b, c, target = Mittsu::Vector3.new) ⇒ Object
static/instance method to calculate barycoordinates based on: www.blackpawn.com/texts/pointinpoly/default.html
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/mittsu/math/triangle.rb', line 82 def self.(point, a, b, c, target = Mittsu::Vector3.new) v0 = Mittsu::Vector3.new v1 = Mittsu::Vector3.new v2 = Mittsu::Vector3.new v0.sub_vectors(c, a) v1.sub_vectors(b, a) v2.sub_vectors(point, a) dot00 = v0.dot(v0) dot01 = v0.dot(v1) dot02 = v0.dot(v2) dot11 = v1.dot(v1) dot12 = v1.dot(v2) denom = dot00 * dot11 - dot01 * dot01 # colinear or singular triangle if denom.zero? # arbitrary location outside of triangle? # not sure if this is the best idea, maybe should be returning undefined return target.set(-2.0, -1.0, -1.0) end inv_denom = 1.0 / denom u = (dot11 * dot02 - dot01 * dot12) * inv_denom v = (dot00 * dot12 - dot01 * dot02) * inv_denom target.set(1.0 - u - v, v, u) end |
.contains_point?(point, a, b, c) ⇒ Boolean
113 114 115 116 117 |
# File 'lib/mittsu/math/triangle.rb', line 113 def self.contains_point?(point, a, b, c) v1 = Mittsu::Vector3.new result = Mittsu::Triangle.(point, a, b, c, v1) result.x >= 0 && result.y >= 0 && result.x + result.y <= 1 end |
.normal(a, b, c, target = Mittsu::Vector3.new) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mittsu/math/triangle.rb', line 66 def self.normal(a, b, c, target = Mittsu::Vector3.new) v0 = Mittsu::Vector3.new target.sub_vectors(c, b) v0.sub_vectors(a, b) target.cross(v0) result_length_sq = target.length_sq if (result_length_sq > 0) target.multiply_scalar(1.0 / ::Math.sqrt(result_length_sq)) else target.set(0.0, 0.0, 0.0) end end |
Instance Method Details
#area ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/mittsu/math/triangle.rb', line 30 def area v0 = Mittsu::Vector3.new v1 = Mittsu::Vector3.new v0.sub_vectors(@c, @b) v1.sub_vectors(@a, @b) v0.cross(v1).length * 0.5 end |
#barycoord_from_point(point, target = Mittsu::Vector3.new) ⇒ Object
50 51 52 |
# File 'lib/mittsu/math/triangle.rb', line 50 def (point, target = Mittsu::Vector3.new) Mittsu::Triangle.(point, @a, @b, @c, target) end |
#clone ⇒ Object
62 63 64 |
# File 'lib/mittsu/math/triangle.rb', line 62 def clone Mittsu::Triangle.new.copy(self) end |
#contains_point?(point) ⇒ Boolean
54 55 56 |
# File 'lib/mittsu/math/triangle.rb', line 54 def contains_point?(point) Mittsu::Triangle.contains_point?(point, @a, @b, @c) end |
#copy(triangle) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/mittsu/math/triangle.rb', line 23 def copy(triangle) @a.copy(triangle.a) @b.copy(triangle.b) @c.copy(triangle.c) self end |
#equals(triangle) ⇒ Object
58 59 60 |
# File 'lib/mittsu/math/triangle.rb', line 58 def equals(triangle) triangle.a.equals(@a) && triangle.b.equals(@b) && triangle.c.equals(@c) end |
#midpoint(target = Mittsu::Vector3.new) ⇒ Object
38 39 40 |
# File 'lib/mittsu/math/triangle.rb', line 38 def midpoint(target = Mittsu::Vector3.new) target.add_vectors(@a, @b).add(@c).multiply_scalar(1.0 / 3.0) end |
#normal(target = Mittsu::Vector3.new) ⇒ Object
42 43 44 |
# File 'lib/mittsu/math/triangle.rb', line 42 def normal(target = Mittsu::Vector3.new) Mittsu::Triangle.normal(@a, @b, @c, target) end |
#plane(target = Mittsu::Plane.new) ⇒ Object
46 47 48 |
# File 'lib/mittsu/math/triangle.rb', line 46 def plane(target = Mittsu::Plane.new) target.set_from_coplanar_points(@a, @b, @c) end |
#set(a, b, c) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/mittsu/math/triangle.rb', line 9 def set(a, b, c) @a.copy(a) @b.copy(b) @c.copy(c) self end |
#set_from_points_and_indices(points, i0, i1, i2) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/mittsu/math/triangle.rb', line 16 def set_from_points_and_indices(points, i0, i1, i2) @a.copy(points[i0]) @b.copy(points[i1]) @c.copy(points[i2]) self end |