Class: Topolys::Edge

Inherits:
Object show all
Defined in:
lib/topolys/model.rb

Overview

Vertex

Instance Attribute Summary collapse

Attributes inherited from Object

#attributes, #children, #id, #parents

Instance Method Summary collapse

Methods inherited from Object

#debug, #hash, link, #link_child, #link_parent, #short_id, #short_name, #to_s, unlink, #unlink_child, #unlink_parent

Constructor Details

#initialize(v0, v1) ⇒ Edge

Initializes an Edge object, use Model.get_edge instead

Throws if v0 or v1 are incorrect type or refer to same vertex

Parameters:

  • v0 (Vertex)

    The origin Vertex

  • v1 (Vertex)

    The terminal Vertex



868
869
870
871
872
873
874
# File 'lib/topolys/model.rb', line 868

def initialize(v0, v1)
  super()
  @v0 = v0
  @v1 = v1

  recalculate
end

Instance Attribute Details

#lengthNumeric (readonly) Also known as: magnitude

Returns the length of this edge.

Returns:

  • (Numeric)

    the length of this edge



858
859
860
# File 'lib/topolys/model.rb', line 858

def length
  @length
end

#v0Vertex (readonly)

Returns the initial vertex, the edge origin.

Returns:

  • (Vertex)

    the initial vertex, the edge origin



852
853
854
# File 'lib/topolys/model.rb', line 852

def v0
  @v0
end

#v1Vertex (readonly)

Returns the second vertex, the edge terminal point.

Returns:

  • (Vertex)

    the second vertex, the edge terminal point



855
856
857
# File 'lib/topolys/model.rb', line 855

def v1
  @v1
end

Instance Method Details

#child_classObject



909
910
911
# File 'lib/topolys/model.rb', line 909

def child_class
  Vertex
end

#directed_edgesObject



921
922
923
# File 'lib/topolys/model.rb', line 921

def directed_edges
  @parents
end

#forward_edgeObject



913
914
915
# File 'lib/topolys/model.rb', line 913

def forward_edge
  @parents.first{|de| !de.inverted}
end

#parent_classObject



905
906
907
# File 'lib/topolys/model.rb', line 905

def parent_class
  DirectedEdge
end

#recalculateObject



883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# File 'lib/topolys/model.rb', line 883

def recalculate
  super()

  # TODO: should catch if 'origin' or 'terminal' are not Vertex objects
  # TODO: should also catch if 'origin' or 'terminal' refer to same object or are within tol of each other

  debug("before: ")

  # unlink from any previous vertices
  @children.reverse_each {|child| Object.unlink(self, child)}

  # link to current vertices
  Object.link(self, @v0)
  Object.link(self, @v1)

  debug("after: ")

  # recompute cached properties and check invariants
  vector = @v1.point - @v0.point
  @length = vector.magnitude
end

#reverse_edgeObject



917
918
919
# File 'lib/topolys/model.rb', line 917

def reverse_edge
  @parents.first{|de| de.inverted}
end

#to_jsonObject



876
877
878
879
880
881
# File 'lib/topolys/model.rb', line 876

def to_json
  result = super
  result[:v0] = @v0.id
  result[:v1] = @v1.id
  return result
end

#verticesObject



925
926
927
# File 'lib/topolys/model.rb', line 925

def vertices
  @children
end