模块: Geom



概述


注意: 直线和平面是无穷大的。

Geom模块定义了一些便于你执行不同的几何操作的方法。
这些方法将直线和平面视为arguments对象,没有用于表示他们的专门类,Array类可以用于表示两者。

直线的表示

直线可以用以下方式表示:

  • 一个由点组成的Array对象(表示直线通过的点)。
  • 一个由点和矢量组成的Array对象(表示直线通过的点和直线的方向。)
  1. line1 = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 1)]
  2. line2 = [Geom::Point3d.new(0, 0, 0), Geom::Point3d.new(0, 0, 100)]

平面的表示

平面可以用以下方式表示:

  • 一个由点和矢量组成的Array对象(表示平面通过点和其法向量)。
  • 一个由四个数值组成的Array对象,表示平面方程的四个系数。
  1. plane1 = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 1)]
  2. plane2 = [0, 0, 1, 0]

如果直线、平面以及矢量的知识对于你而言比较陌生,你可以参考市面上一些有关三维空间中的几何学的书籍。

软件版本:

  • SketchUp 6.0

命名空间下的定义


类:

  • [[BoundingBox]]
  • [[Bounds2d]]
  • [[LatLon]]
  • [[OrientedBounds2d]]
  • [Point2d]
  • [Point3d]
  • [PolygonMesh]
  • [Transformation]
  • [Transformation2d]
  • [UTM]
  • [Vector2d]
  • [Vector3d]

类方法目录


closest_points(line1, line2) ⇒ Array(Geom::Point3d, Geom::Point3d)

用于计算两条直线上的最近点位置。

fit_plane_to_points(args) ⇒ Object]

用于计算能够最佳适配一系列点的平面。

intersect_line_line(line1, line2) ⇒ Geom::Point3d

用于计算两条直线的交点

intersect_line_plane(line, plane) ⇒ Geom::Point3d

用于计算直线与平面的交点。

intersect_plane_plane(plane1, plane2) ⇒ Array(Geom::Point3d, Geom::Vector3d)

用于计算两个平面相交的结果。

linear_combination(weight1, pt_or_vect1, weight2, pt_or_vect2)

用于计算点或者矢量的线性组合。

point_in_polygon_2D(point, polygon, check_border) ⇒ Boolean

用于判定点是否在多边形内部。

tesselate(polygon_loop_points, *inner_loop_points) ⇒ ArrayGeom::Point3d

  1. Tessellates a polygon, represented as a collection of 3D points.

类方法细节


closest_points(line1, line2) ⇒ [Array]

用于计算两条直线上的最近点位置。

示例代码:

  1. line1 = [Geom::Point3d.new(0, 2, 0), Geom::Vector3d.new(1, 0, 0)]
  2. line2 = [Geom::Point3d.new(3, 0, 0), Geom::Vector3d.new(0, 1, 0)]
  3. # This will return a point Point3d(3, 2, 0).
  4. points = Geom.closest_points(line1, line2)

参数:

  • line1 ([Array]) — 第一条直线
  • line2 ([Array]) — 第二条直线

返回值:

  • ([Array]) — 一个由两个点组成的Array对象。第一个点在第一个直线上,第二个点在第二条直线上。

软件版本:

  • SketchUp 6.0

fit_plane_to_points方法

  1. fit_plane_to_points(point1, point2, point3, ...) Array(Geom::Point3d, Geom::Vector3d)
  2. fit_plane_to_points**(points) Array(Geom::Point3d, Geom::Vector3d)

用于计算能够适配一组点对现象的平面。

如果给出超过三个点,则计算出的平面可能不会通过其中一些点。

平面会以一个数组(Array)的形式返回,数组中的四个数值分别代表平面方程中的四个系数。

平面方程的形式: Ax + By + Cz + D = 0

示例代码:

  1. point1 = Geom::Point3d.new(0, 0, 0)
  2. point2 = Geom::Point3d.new(10, 10, 10)
  3. point3 = Geom::Point3d.new(25, 25, 25)
  4. plane = Geom.fit_plane_to_points(point1, point2, point3)

重载:

fit_plane_to_points(point1, point2, point3, …) ⇒ Array(Geom::Point3d, Geom::Vector3d)

返回一个平面.

参数:
  • point1 (Geom::Point3d)
  • point2 (Geom::Point3d)
  • point3 (Geom::Point3d)

返回值:
  • (Array)(Geom::Point3d, Geom::Vector3d)) — 表示一个平面的通过点和法向量。

fit_plane_to_points**(points) ⇒ Array(Geom::Point3d, Geom::Vector3d)

返回一个平面。

参数:

返回值:
  • (Array(Geom::Point3d, Geom::Vector3d)) — 表示一个平面的通过点和法向量。

软件版本:

  • SketchUp 6.0

intersect_line_line(line1, line2) ⇒ Geom::Point3d


用于计算两条直线的交点。

示例代码:

  1. # Defines a line parallel to the Y axis, offset 20 units.
  2. line1 = [Geom::Point3d.new(20, 0, 0), Geom::Vector3d.new(0, 1, 0)]
  3. # Defines a line parallel to the X axis, offset 10 units.
  4. line2 = [Geom::Point3d.new(0, 10, 0), Geom::Point3d.new(20, 10, 0)]
  5. # This will return a point Point3d(20, 10, 0).
  6. point = Geom.intersect_line_line(line1, line2)

参数:

  • line1 ([Array]) — 第一条直线。
  • line2 ([Array]) — 第二条直线。

直线以其通过点和方向矢量的形式定义。

返回值:

  • (Geom::Point3d, nil) — 相交点(如果直线不相交则返回’nil’)

相关内容:

  • [[Geom#直线的表示]]

软件版本:

  • SketchUp 6.0

intersect_line_plane(line, plane) ⇒ Geom::Point3d


用于计算直线与平面的交点。

示例代码:

  1. # Defines a line parallel to the X axis, offset 20 units.
  2. line = [Geom::Point3d.new(-10, 20, 0), Geom::Vector3d.new(1, 0, 0)]
  3. # Defines a plane with it's normal parallel to the x axis.
  4. plane = [Geom::Point3d.new(10, 0 ,0), Geom::Vector3d.new(1, 0, 0)]
  5. # This will return a point Point3d(10, 20, 0).
  6. point = Geom.intersect_line_plane(line, plane)

参数:

  • line (Array(Geom::Point3d, Geom::Vector3d))——直线
  • plane (Array(Geom::Point3d, Geom::Vector3d))——平面

返回值:

  • (Geom::Point3d, nil) — 一个点对象(Geom::Point3d)。(如果直线和平面不相交则返回’nil’)

相关内容:

  • [[Geom#直线的表示]]
  • [[Geom#平面的表示]]

软件版本:

  • SketchUp 6.0

intersect_plane_plane(plane1, plane2) ⇒ Array(Geom::Point3d, Geom::Vector3d)

用于计算两个平面相交的结果。

示例代码:

  1. # Defines a plane with it's normal parallel to the x axis.
  2. plane1 = [Geom::Point3d.new(10, 0 ,0), Geom::Vector3d.new(1, 0, 0)]
  3. # Defines a plane with it's normal parallel to the y axis.
  4. plane2 = [Geom::Point3d.new(0, 20 ,0), Geom::Vector3d.new(0, 1, 0)]
  5. # This will return a line [Point3d(10, 20, 0), Vector3d(0, 0, 1)].
  6. line = Geom.intersect_plane_plane(plane1, plane2)

参数:

  • plane1 ([Array]) — 第一个平面。
  • plane2 ([Array]) — 第二个平面。

返回值:

  • ([Array]) — 如果成功运行则返回一条直线;如果平面之间不相交,则返回’nil’。

软件版本:

  • SketchUp 6.0

linear_combination方法

  1. linear_combination(weight1, point1, weight2, point2) Geom::Point3d
  2. linear_combination(weight1, vector1, weight2, vector2) Geom::Vector3d

用于计算两个点或者这矢量的线性结合。

线性结合是矢量计算中的一种标准方法. 矢量的线性结合=矢量1×权重1+矢量2×权重2

示例代码:

  1. point1 = Geom::Point3d.new(1, 1, 1)
  2. point2 = Geom::Point3d.new(10, 10, 10)
  3. # Gets the point on the line segment connecting point1 and point2 that is
  4. # 3/4 the way from point1 to point2: Point3d(7.75, 7.75, 7.75).
  5. point = Geom.linear_combination(0.25, point1, 0.75, point2)

重载:

linear_combination(weight1, point1, weight2, point2) ⇒ Geom::Point3d

参数:
  • weight1 (Float)
  • point1 (Geom::Point3d)
  • weight2 (Float)
  • point2 (Geom::Point3d)

返回值:
  • (Geom::Point3d)

linear_combination**(weight1, vector1, weight2, vector2) ⇒ Geom::Vector3d

参数:
  • weight1 (Float)
  • vector1 (Geom::Vector3d)
  • weight2 (Float)
  • vector2 (Geom::Vector3d)

返回值:
  • (Geom::Vector3d)

软件版本:

  • SketchUp 6.0

point_in_polygon_2D(point, polygon, check_border) ⇒ Boolean


用于判定点是否在给定多边形内部。

由于需要判定的点和给定多边形中的点的z坐标值会被忽略,本方法实际上是一个二维判定方法。

示例代码:

  1. # Create a point that we want to check. (Note that the 3rd coordinate,
  2. # the z, is ignored for purposes of the check.)
  3. point = Geom::Point3d.new(5, 0, 10)
  4. # Create a series of points of a triangle we want to check against.
  5. triangle = []
  6. triangle << Geom::Point3d.new(0, 0, 0)
  7. triangle << Geom::Point3d.new(10, 0, 0)
  8. triangle << Geom::Point3d.new(0, 10, 0)
  9. # Test to see if our point is inside the triangle, counting hits on
  10. # the border as an intersection in this case.
  11. hits_on_border_count = true
  12. status = Geom.point_in_polygon_2D(point, triangle, hits_on_border_count)

参数:

  • point (Geom::Point3d)
  • polygon (ArrayGeom::Point3d) — 一系列的点,用于表示多边形的角点。
  • check_border (Boolean) — 如果该参数的值为’true’,则多边形边界上的点会被判定为在多边形内部。

返回值:

  • (Boolean) — 如果点在多边形内部则返回’true’。

软件版本:

  • SketchUp 6.0

tesselate(polygon_loop_points, inner_loop_points) ⇒ ArrayGeom::Point3d

注意:点的顺序非常重要。 外环上的点应当以逆时针顺序给出。 内环上的点以顺时针顺序给出时,会创建一个空洞。 内环上的点以逆时针顺序给出时,则会创建出一个以三角形填充的环。

注意: tesselation方法使用与SketchUp渲染管线相同的逻辑。 但是执行结果与软件的版本有关。 尤其需要注意的是退化多边形和非平面多边形并没有在API中定义。 这些多边形的常见例子有:所有点共线的多边形、有重复点的多边形、非平面多边形等。

注意: 如果你希望从已经存在的面对象(Sketchup::Face)中获取三角形,最好使用Sketchup::Face中的mesh方法。

通过一系列的三维点对象细分多边形。

可以通过给出描述内环的点序列的方式创建空洞。

本方法被设计用于平面多边形。

在使用Sketchup::View中的draw方法和draw2d方法绘制凹多边形时非常有用。

在需要导入文件时,如果你需要导入的文件格式仅仅描述了多边形上的环边,而你需要处理其三角细分的时候也会有所帮助。

示例代码:

以下代码遍历了返回值中的所有三角形。

  1. polygon = [
  2. Geom::Point3d.new(0, 0, 0),
  3. Geom::Point3d.new(90, 0, 0),
  4. Geom::Point3d.new(60, 40, 0),
  5. Geom::Point3d.new(90, 90, 0),
  6. Geom::Point3d.new(30, 70, 0),
  7. ]
  8. triangles = Geom.tesselate(polygon)
  9. triangles.each_slice(3) { |triangle|
  10. # Work with each triangle set...
  11. }
  12. # Or get an array of triangles:
  13. triangles_set = triangles.each_slice(3).to_a

以下代码使用了一个自定义的工具来绘制一个带有内部孔洞的多边形。

  1. class ExampleTool
  2. def initialize
  3. polygon = [
  4. Geom::Point3d.new(0, 0, 0),
  5. Geom::Point3d.new(90, 0, 0),
  6. Geom::Point3d.new(60, 40, 0),
  7. Geom::Point3d.new(90, 90, 0),
  8. Geom::Point3d.new(30, 70, 0),
  9. ] # Counter-clockwise order
  10. hole1 = [
  11. Geom::Point3d.new(20, 10, 0),
  12. Geom::Point3d.new(40, 10, 0),
  13. Geom::Point3d.new(45, 25, 0),
  14. Geom::Point3d.new(30, 20, 0),
  15. Geom::Point3d.new(25, 25, 0),
  16. ].reverse # Clockwise order - empty hole
  17. hole2 = [
  18. Geom::Point3d.new(30, 40, 0),
  19. Geom::Point3d.new(50, 40, 0),
  20. Geom::Point3d.new(50, 50, 0),
  21. Geom::Point3d.new(30, 50, 0),
  22. ].reverse # Counter-clockwise order - filled hole
  23. @triangles = Geom.tesselate(polygon, hole1, hole2)
  24. end
  25. def activate
  26. Sketchup.active_model.active_view.invalidate
  27. end
  28. def onMouseMove(flags, x, y, view)
  29. view.invalidate
  30. end
  31. def getExtents
  32. bounds = Geom::BoundingBox.new
  33. bounds.add(@triangles)
  34. bounds
  35. end
  36. def draw(view)
  37. view.drawing_color = Sketchup::Color.new(192, 0, 0)
  38. view.draw(GL_TRIANGLES, @triangles)
  39. end
  40. end
  41. Sketchup.active_model.select_tool(ExampleTool.new)

返回值

一个步长为3的点序列(Array),表示一系列的三角形。

参数:

  • polygon_loop_points (ArrayGeom::Point3d)
  • inner_loop_points (Array)

返回值:

  • (ArrayGeom::Point3d) — 一个步长为3的点序列(Array),表示一系列的三角形。

异常:

  • (ArgumentError) — 如果任意一个环包含少于三个点则抛出ArgumentError。
    if any of the loops contain less than three points.
  • (RuntimeError) —
    if the tesselator returned an error.

相关内容:

  • Sketchup::View的draw方法。
  • Sketchup::View#draw2d方法。

软件版本:

  • SketchUp 2020.0