- 模块: Geom
- 概述
- 命名空间下的定义
- 类方法目录
- 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) ⇒ Array Geom::Point3d
- 类方法细节
- closest_points(line1, line2) ⇒ [Array]
- fit_plane_to_points方法
- 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方法
- point_in_polygon_2D(point, polygon, check_border) ⇒ Boolean
- tesselate(polygon_loop_points, inner_loop_points) ⇒ Array Geom::Point3d
模块: Geom
概述
注意: 直线和平面是无穷大的。
Geom模块定义了一些便于你执行不同的几何操作的方法。
这些方法将直线和平面视为arguments对象,没有用于表示他们的专门类,Array类可以用于表示两者。
直线的表示
直线可以用以下方式表示:
- 一个由点组成的Array对象(表示直线通过的点)。
- 一个由点和矢量组成的Array对象(表示直线通过的点和直线的方向。)
line1 = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 1)]line2 = [Geom::Point3d.new(0, 0, 0), Geom::Point3d.new(0, 0, 100)]
平面的表示
平面可以用以下方式表示:
- 一个由点和矢量组成的Array对象(表示平面通过点和其法向量)。
- 一个由四个数值组成的Array对象,表示平面方程的四个系数。
plane1 = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 1)]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
Tessellates a polygon, represented as a collection of 3D points.
类方法细节
closest_points(line1, line2) ⇒ [Array]
用于计算两条直线上的最近点位置。
示例代码:
line1 = [Geom::Point3d.new(0, 2, 0), Geom::Vector3d.new(1, 0, 0)]line2 = [Geom::Point3d.new(3, 0, 0), Geom::Vector3d.new(0, 1, 0)]# This will return a point Point3d(3, 2, 0).points = Geom.closest_points(line1, line2)
参数:
返回值:
- ([Array]) — 一个由两个点组成的Array对象。第一个点在第一个直线上,第二个点在第二条直线上。
软件版本:
- SketchUp 6.0
fit_plane_to_points方法
fit_plane_to_points(point1, point2, point3, ...) ⇒ Array(Geom::Point3d, Geom::Vector3d)fit_plane_to_points**(points) ⇒ Array(Geom::Point3d, Geom::Vector3d)
用于计算能够适配一组点对现象的平面。
如果给出超过三个点,则计算出的平面可能不会通过其中一些点。
平面会以一个数组(Array)的形式返回,数组中的四个数值分别代表平面方程中的四个系数。
平面方程的形式: Ax + By + Cz + D = 0
示例代码:
point1 = Geom::Point3d.new(0, 0, 0)point2 = Geom::Point3d.new(10, 10, 10)point3 = Geom::Point3d.new(25, 25, 25)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)
返回一个平面。
参数:
- points (ArrayGeom::Point3d)
返回值:
- (Array(Geom::Point3d, Geom::Vector3d)) — 表示一个平面的通过点和法向量。
软件版本:
- SketchUp 6.0
intersect_line_line(line1, line2) ⇒ Geom::Point3d
用于计算两条直线的交点。
示例代码:
# Defines a line parallel to the Y axis, offset 20 units.line1 = [Geom::Point3d.new(20, 0, 0), Geom::Vector3d.new(0, 1, 0)]# Defines a line parallel to the X axis, offset 10 units.line2 = [Geom::Point3d.new(0, 10, 0), Geom::Point3d.new(20, 10, 0)]# This will return a point Point3d(20, 10, 0).point = Geom.intersect_line_line(line1, line2)
参数:
直线以其通过点和方向矢量的形式定义。
返回值:
- (Geom::Point3d, nil) — 相交点(如果直线不相交则返回’nil’)
相关内容:
- [[Geom#直线的表示]]
软件版本:
- SketchUp 6.0
intersect_line_plane(line, plane) ⇒ Geom::Point3d
用于计算直线与平面的交点。
示例代码:
# Defines a line parallel to the X axis, offset 20 units.line = [Geom::Point3d.new(-10, 20, 0), Geom::Vector3d.new(1, 0, 0)]# Defines a plane with it's normal parallel to the x axis.plane = [Geom::Point3d.new(10, 0 ,0), Geom::Vector3d.new(1, 0, 0)]# This will return a point Point3d(10, 20, 0).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)
用于计算两个平面相交的结果。
示例代码:
# Defines a plane with it's normal parallel to the x axis.plane1 = [Geom::Point3d.new(10, 0 ,0), Geom::Vector3d.new(1, 0, 0)]# Defines a plane with it's normal parallel to the y axis.plane2 = [Geom::Point3d.new(0, 20 ,0), Geom::Vector3d.new(0, 1, 0)]# This will return a line [Point3d(10, 20, 0), Vector3d(0, 0, 1)].line = Geom.intersect_plane_plane(plane1, plane2)
参数:
返回值:
- ([Array]) — 如果成功运行则返回一条直线;如果平面之间不相交,则返回’nil’。
软件版本:
- SketchUp 6.0
linear_combination方法
linear_combination(weight1, point1, weight2, point2) ⇒ Geom::Point3dlinear_combination(weight1, vector1, weight2, vector2) ⇒ Geom::Vector3d
用于计算两个点或者这矢量的线性结合。
线性结合是矢量计算中的一种标准方法. 矢量的线性结合=矢量1×权重1+矢量2×权重2
示例代码:
point1 = Geom::Point3d.new(1, 1, 1)point2 = Geom::Point3d.new(10, 10, 10)# Gets the point on the line segment connecting point1 and point2 that is# 3/4 the way from point1 to point2: Point3d(7.75, 7.75, 7.75).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坐标值会被忽略,本方法实际上是一个二维判定方法。
示例代码:
# Create a point that we want to check. (Note that the 3rd coordinate,# the z, is ignored for purposes of the check.)point = Geom::Point3d.new(5, 0, 10)# Create a series of points of a triangle we want to check against.triangle = []triangle << Geom::Point3d.new(0, 0, 0)triangle << Geom::Point3d.new(10, 0, 0)triangle << Geom::Point3d.new(0, 10, 0)# Test to see if our point is inside the triangle, counting hits on# the border as an intersection in this case.hits_on_border_count = truestatus = 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方法绘制凹多边形时非常有用。
在需要导入文件时,如果你需要导入的文件格式仅仅描述了多边形上的环边,而你需要处理其三角细分的时候也会有所帮助。
示例代码:
以下代码遍历了返回值中的所有三角形。
polygon = [Geom::Point3d.new(0, 0, 0),Geom::Point3d.new(90, 0, 0),Geom::Point3d.new(60, 40, 0),Geom::Point3d.new(90, 90, 0),Geom::Point3d.new(30, 70, 0),]triangles = Geom.tesselate(polygon)triangles.each_slice(3) { |triangle|# Work with each triangle set...}# Or get an array of triangles:triangles_set = triangles.each_slice(3).to_a
以下代码使用了一个自定义的工具来绘制一个带有内部孔洞的多边形。
class ExampleTooldef initializepolygon = [Geom::Point3d.new(0, 0, 0),Geom::Point3d.new(90, 0, 0),Geom::Point3d.new(60, 40, 0),Geom::Point3d.new(90, 90, 0),Geom::Point3d.new(30, 70, 0),] # Counter-clockwise orderhole1 = [Geom::Point3d.new(20, 10, 0),Geom::Point3d.new(40, 10, 0),Geom::Point3d.new(45, 25, 0),Geom::Point3d.new(30, 20, 0),Geom::Point3d.new(25, 25, 0),].reverse # Clockwise order - empty holehole2 = [Geom::Point3d.new(30, 40, 0),Geom::Point3d.new(50, 40, 0),Geom::Point3d.new(50, 50, 0),Geom::Point3d.new(30, 50, 0),].reverse # Counter-clockwise order - filled hole@triangles = Geom.tesselate(polygon, hole1, hole2)enddef activateSketchup.active_model.active_view.invalidateenddef onMouseMove(flags, x, y, view)view.invalidateenddef getExtentsbounds = Geom::BoundingBox.newbounds.add(@triangles)boundsenddef draw(view)view.drawing_color = Sketchup::Color.new(192, 0, 0)view.draw(GL_TRIANGLES, @triangles)endendSketchup.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
