How to find a plane without creating face?

Hi,
I need a plan of a group of points (pts) and write the following code.

grp = Sketchup.active_model.active_entities.add_group
face1 = grp.entities.add_face pts
plane1 = face1.plane
grp.erase!

creating grp and erasing it comes to undo. How can I find plane1 without creating and erasing the group?

1 Like

Have you considered Geom.fit_plane_to_points()?

Edit: a more precise approach would be to use that method to find a plane for the first three points and then use Point3d.on_plane?() to test any other points. Geom.fit_plane_to_points() returns a “best fit” plane even if the points are not actually co-planar, incontrast to Entities#add_face() which will fail if the points are not co-planar.

5 Likes

https://ruby.sketchup.com/Geom.html
A plane can be represented as either an Array of a point and a vector, or as an Array of 4 numbers that give the coefficients of a plane equation.

Let’s assume the pts is an array of point3D and there are minimum 3 points and these are on a plane.
One of the possible definition of plane1:

vec0 = pts[0].vector_to( pts[1] )
vec1 = pts[0].vector_to( pts[2] )
plane1_normal = vec0 * vec1

plane1 = [pts[0], plane1_normal]

The #cross (or #*) method is used to compute the cross product between two vectors.

The cross product, also called the vector product, is an operation on two vectors. The cross product of two vectors produces a third vector which is perpendicular to the plane in which the first two lie.

2 Likes

Thank you Dezmo, Now I know different ways to find a plane. I used the following code.

plane1 = Geom.fit_plane_to_points(pts)

Hi Dezmo,
How can I find a plane vector?

https://ruby.sketchup.com/Geom.html
A plane can be represented as either an Array of a point and a vector, or as an Array of 4 numbers that give the coefficients of a plane equation.

plane1 = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 1)]
plane2 = [0, 0, 1, 0]

Plane (geometry) - Wikipedia
Conversely, it is easily shown that if a , b , c and d are constants and a , b , and c are not all zero, then the graph of the equation:
image

So e.g.:

def plane_as_point_and_vector(plane)
  if plane.length == 2
    return plane
  else
    a, b, c, d = plane
    normal = Geom::Vector3d.new(a,b,c)
    point = ORIGIN.offset(normal.reverse, d)
    return [point, normal]
  end
end

plane_p_v = plane_as_point_and_vector(plane)
plane_normal = plane_p_v[1]
3 Likes

Why do you have the normal vector reversed when offsetting from the ORIGIN ?

If plane1 is [Geom::Point3d.new(0, 0, 1), Geom::Vector3d.new(0, 0, 1)],
then is is also [ 0, 0, 1, 1 ], …
then the point on plane1 is 1 above the XY ground plane.

Your method seems to return a point 1 below the ground plane.

Actually, (and honestly :blush: ) I learned and used if from tt_lib2 a years before, and I didn’t think about it why it is like that. As far as I remember it worked okay when I used…
Perhaps it’s time to check! :thinking:

I had some time to math :innocent:
Also :

a=0; b=0; c=1
x0=0; y0=0; z0=1

0(x-0) + 0(y-0) + 1(z-1) = 0
0x + 0y + 1z -1 = 0
>>> [0, 0, 1,-1]
So it is NOT:

2 Likes

Nice explanation !

:clap:

1 Like

The SketchUp Ruby API documentation is notoriously lazy about documenting the two ways it can define a plane, referring you to unspecified texts on geometry. Here is my understanding of the two ways:

An array of a Point3d and a Vector3d: the point is any point on the plane and the vector is the normal to the plane. Another point is on the plane if p1-p0 is perpendicular to the normal vector.

The four floats array contains the coefficients [a,b,c,d] of the plane equation ax + by + cz + d = 0, where x, y, z are the components of a point on the plane. With some thought you can discover that a, b, and c are the x, y, and z components of the normal unit vector of the plane and d is the (signed) distance of the plane from the origin along that normal.

2 Likes

It is there:
Module: Geom — SketchUp Ruby API Documentation

Even, the doc give you an advise :
There are several good books on 3D math if you are new to the concepts of a line, plane, and vector. :stuck_out_tongue_winking_eye:

(But I agree, in addition to the fact that the documentation is getting better, it says a lot about the quality that there are 178 open tickets…yet. :blush: )