How to know if a 3D point is seen from another position in space?

sketchup
ruby
position
camera

#1

I would like to know when a 3D point is directly seen from any other position in space, i.e., when there is no obstacle (wall, object…) in between. Is it possible to achieve using the Ruby API?

Basically I’m trying to return a true flag when the point is seen, and a false flag when it’s not.


#2

Let’s assume you have your two points already defined [eye and target]

vector = point_for_eye.vector_to( point_for_target )
ray = [ point_for_eye, vector ] 
hit = Sketchup.active_model.raytest( ray, false )
if hit
  ### there is something in the way
else ### it's nil
  ### there is nothing in the way
end

#3

Have a look at model#raytest. It may be the core of what you want.

TIG typed faster than me!


#4

Thank you guys, I’ll dig a bit more into it.


#5

Note that the raytest also returns truthy when the ray hits something beyond the target point. You also need to check if a hit is closer or further away from the eye point than the target is.


#6

@eneroth3 makes a good point.
Modify the code to trap for this…

  '''
  if hit
      ### there is perhaps something in the way
      if point_for_eye.distance(hit[0]) < point_for_eye.distance(point_for_target)
        ### the hit point is between the eye and target, so you can't see the target
      else
         ### the hit is beyond the target, so you can still see the target
      end
      ...