Class: TT::Ray

Inherits:
Object
  • Object
show all
Defined in:
TT_Lib2/ray.rb

Overview

Wrapper class for Model.raytest with enhanced features.

Since:

  • 2.0.0

Instance Method Summary collapse

Constructor Details

#new(ray) ⇒ Ray #new(ray) ⇒ Ray #new(point, vector) ⇒ Ray #new(point1, point2) ⇒ Ray

Returns a new instance of Ray

Overloads:

  • #new(ray) ⇒ Ray

    Parameters:

    • value (Array<Sketchup::Point3d,Sketchup::Vector3d>)
  • #new(ray) ⇒ Ray

    Parameters:

    • value (Array<Sketchup::Point3d,Sketchup::Point3d>)
  • #new(point, vector) ⇒ Ray

    Parameters:

    • point (Sketchup::Point3d)
    • vector (Sketchup::Vector3d)
  • #new(point1, point2) ⇒ Ray

    Parameters:

    • point1 (Sketchup::Point3d)
    • point2 (Sketchup::Point3d)

Since:

  • 2.0.0



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

def initialize( *args )
  if args.size == 1
    @ray = args[0]
  elsif args.size == 2
    raise ArgumentError, 'First argument must be Point3d' unless args[0].is_a?( Geom::Point3d )
    if args[1].is_a?( Geom::Point3d )
      @ray = [ args[0], args[0].vector_to(args[1]) ]
    elsif args[1].is_a?( Geom::Vector3d )
      @ray = [ args[0], args[1] ]
    else
      raise ArgumentError, "Second argument must be Point3d or Vector3d.\n#{args[1].class.to_s}"
    end
  else
    raise ArgumentError, 'One or Two arguments'
  end
end

Instance Method Details

#directionSketchup::Vector3d

Gets the direction of the ray.

Returns:

  • (Sketchup::Vector3d)

Since:

  • 2.0.0



69
70
71
# File 'TT_Lib2/ray.rb', line 69

def direction
  @ray[1]
end

#direction=(point) ⇒ Sketchup::Vector3d #direction=(vector) ⇒ Sketchup::Vector3d

Sets the origin of the ray.

Overloads:

  • #direction=(point) ⇒ Sketchup::Vector3d

    Parameters:

    • point (Sketchup::Point3d)
  • #direction=(vector) ⇒ Sketchup::Vector3d

    Parameters:

    • vector (Sketchup::Vector3d)

Returns:

  • (Sketchup::Vector3d)

Since:

  • 2.0.0



82
83
84
85
86
87
88
89
90
# File 'TT_Lib2/ray.rb', line 82

def direction=(value)
  if value.is_a?( Geom::Point3d )
    @ray[1] = @ray[0].vector_to(value)
  elsif value.is_a?( Geom::Vector3d )
    @ray[1] = value
  else
    raise ArgumentError
  end
end

#inspectString

Returns:

  • (String)

Since:

  • 2.0.0



108
109
110
# File 'TT_Lib2/ray.rb', line 108

def inspect
  return "Ray(#{@ray[0].inspect}, #{@ray[1].inspect})"
end

#originSketchup::Point3d

Gets the origin of the ray.

Returns:

  • (Sketchup::Point3d)

Since:

  • 2.0.0



50
51
52
# File 'TT_Lib2/ray.rb', line 50

def origin
  @ray[0]
end

#origin=(point) ⇒ Sketchup::Point3d

Sets the origin of the ray.

Parameters:

  • point (Sketchup::Point3d)

Returns:

  • (Sketchup::Point3d)

Since:

  • 2.0.0



60
61
62
# File 'TT_Lib2/ray.rb', line 60

def origin=(point)
  @ray[0] = point
end

#test(model = Sketchup.active_model, stop_at_ground = false, wysiwyg = true) ⇒ Array<Point3d, Array>?

Parameters:

  • model (Sketchup::Model) (defaults to: Sketchup.active_model)
  • stop_at_ground (Boolean) (defaults to: false)

Returns:

Since:

  • 2.0.0 - fixed in 2.5.0



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'TT_Lib2/ray.rb', line 127

def test( model = Sketchup.active_model, stop_at_ground = false, wysiwyg = true )
  ground = [ORIGIN, Z_AXIS]
  origin, direction = @ray
  ray = @ray
  
  support_wysiwyg = TT::SketchUp.support?( TT::SketchUp::RAYTEST_WYSIWYG )
  
  while true

    # Shot ray. Make use of the wysiwyg in version Su8.0M1 and newer.
    if support_wysiwyg
      test_result = model.raytest( ray, wysiwyg )
    else
      test_result = model.raytest( ray )
    end
    
    # Check if the ray should stop at ground
    # Stop the ray at the ground if it misses any geometry.
    if test_result.nil?
      if stop_at_ground
        # Intersect ray with ground plane.
        pt = Geom.intersect_line_plane( @ray, ground )
        return nil if pt.nil? # Plane not hit.
        # Validate direction.
        v = origin.vector_to( pt )
        if v.valid? && v.samedirection?( direction )
          # Hit ground plane.
          return [ pt, [] ]
        else
          # Ground plane hit, but in the wrong direction. Means nothing was hit.
          return nil
        end
      else
        # Nothing hit.
        return nil
      end
    end
    
    # No need to process the ray further if it is SU8.0M1 or higher that runs
    # the raytest. These versions support control for visible/hidden tests.
    return test_result if support_wysiwyg 
    
    # This point means that an older SketchUp version did the raytest and the
    # results may need further testing if only visible entities is supposed 
    # to be hit.
    return test_result unless wysiwyg
    
    # SU8.0M1 added a new flag that control if raytest stops on hidden entities.
    # In earlier versions this was not availible. To work around this the ray
    # is recast if the ray stopped on a hidden entity.
    # Verify visibility
    point, path = test_result
    if path.all? { |e| e.visible? && e.layer.visible? }
      return test_result
    else
      ray = [ point, direction ]
    end
    
  end # while
end

#to_aArray<Sketchup::Point3d, Sketchup::Vector3d>

Returns:

  • (Array<Sketchup::Point3d, Sketchup::Vector3d>)

Since:

  • 2.0.0



95
96
97
# File 'TT_Lib2/ray.rb', line 95

def to_a
  return @ray
end

#to_sString

Returns:

  • (String)

Since:

  • 2.0.0



101
102
103
# File 'TT_Lib2/ray.rb', line 101

def to_s
  return "#{@ray[0]}, #{@ray[1]}"
end