InHalfspace?
-
I couldn't see a method to find out if a point is in a halfspace defined by a plane or not, so I made this toy one.
#setup gubbins mod = Sketchup.active_model ent = mod.entities sel = mod.selection #draw a face and select it, this checks that you have if sel[0].is_a? Sketchup;;Face #if you have, throw 100 points into the air for i in (0..100) ent.add_cpoint [rand*100,rand*100,rand*100] end #get the normal of the face normal = sel[0].normal #get the centroid of the face too offset = Geom;;Vector3d.new(sel[0].bounds.center.to_a) #make an empty collection to put your points into if they ARE in the halfspace pointsInPositiveHalfspace = [] #loop through all the things in the model, ent.each{|e| # if they are points... if e.is_a? Sketchup;;ConstructionPoint #move them so that they are relative to the centroid of the face, not to the origin. #this one is the big mental shift shiftedPt = Geom;;Vector3d.new(e.position.to_a) - offset #The dot product can be used as a half space test. A half space test can be used to #determine if an object, lets say a zombie, is in front of the player (or camera) or #behind it. If my player's forward vector is P and the vector from the player to the #zombie (zombie's position - player's position) is Z, then dotting P and Z would give #me the angle between the two vectors. If the angle is positive our player is facing #the zombie, otherwise the zombie is behind us (never turn your back on a zombie!!!). if (normal.dot shiftedPt) > 0 #put that point into the colleciton pointsInPositiveHalfspace << e end end } #for all the points in the collection... for i in (1..pointsInPositiveHalfspace.length) if pointsInPositiveHalfspace[i].is_a? Sketchup;;ConstructionPoint #Draw a line from it's predecesor to it ent.add_line pointsInPositiveHalfspace[i-1].position, pointsInPositiveHalfspace[i].position end end end
There's a lot to do to it before it is actually useful, but it may help me with my quest if there isn't a better way. -
http://code.google.com/apis/sketchup/docs/ourdoc/point3d.html#on_plane?
tells you if a point is on a givenplane
plane = [Geom;;Point3d.new(0,0,0),Geom;;Vector3d.new(0,0,1)] point = Geom;;Point3d.new(10,10,10) status = point.on_plane?(plane)
You can get a plane from a point and a vector, or direct from a face as in
face.plane
So now you know that the point is on the face's plane, BUT really you probably need to test if the point is actually on the face itself, so this is probably better...
http://code.google.com/apis/sketchup/docs/ourdoc/face.html#classify_pointresult = face.classify_point(test_point)
The result can be are
* 0: Sketchup::Face::PointUnknown, * 1: Sketchup::Face::PointInside, * 2: Sketchup::Face::PointOnVertex, * 4: Sketchup::Face::PointOnEdge, * 8: Sketchup::Face::PointOnFace, * 16: Sketchup::Face::PointOutside, * 32: Sketchup::Face::PointNotOnPlane.
So by seeing what result you get you can tell where the point is/isn't on the face ?
-
I'm not interested in finding out if the point is on the plane, simply if it is in the +ve half or the universe bisected by the plane.
It's a pretty classic test that can be stacked up to do selection sets. i.e. to test if a point is in a cube you test to see if it is in the -ve halfspace of the planes of each of the faces. A fail on any test shows that it isn't in that halfspace, and therefore you can bail out.
I've got to put some effort into packaging this up so that it takes a plane and a point (or a collection thereof)
I think that there is probably something to be gained by pulling out the proper equation of the plane rather than doing the offset, but I'm not all that hot on maths. (not sure why they start you at university afterthe legal drinking age) -
Don't forget the Geom module methods.
Geom.point_in_polygon_2D can be used to narrow down even further where the point is, once you know whether it's ventral (anterior) or dorsal (posterior.)
For instance, reference your view from the other thread, where you show the viewing cone(s) looking downward. Getting the 3 vertices of the horizontal conic section defines a triangular face that can be used with Geom.point_in_polygon_2D, to see if a point should be within the projected section (without regard to z position.)
Repeat the exercise using the vertical conic section, and if both return true, then the point is inside the viewing pyramid that curcumscribes the viewing cone.
Advertisement