|
| 1 | +# Copyright:: Copyright 2022 Trimble Inc. |
| 2 | +# License:: The MIT License (MIT) |
| 3 | + |
| 4 | +# The Geom module defines a number of Module methods that let you perform |
| 5 | +# different geometric operations. |
| 6 | +# |
| 7 | +# The methods in this module take lines |
| 8 | +# and planes as arguments. There is no special class for representing lines or |
| 9 | +# planes. Arrays are used for both. |
| 10 | +# |
| 11 | +# A line can be represented as either an Array of a point and a |
| 12 | +# vector, or as an Array of two points. |
| 13 | +# line1 = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 1)] |
| 14 | +# line2 = [Geom::Point3d.new(0, 0, 0), Geom::Point3d.new(0, 0, 100)] |
| 15 | +# |
| 16 | +# A plane can be represented as either an Array |
| 17 | +# of a point and a vector, or as an Array of 4 numbers that give the |
| 18 | +# coefficients of a plane equation. |
| 19 | +# plane1 = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 1)] |
| 20 | +# plane2 = [0, 0, 1, 0] |
| 21 | +# |
| 22 | +# There are several good books on 3D math if you are new to |
| 23 | +# the concepts of a line, plane, and vector. |
| 24 | +# |
| 25 | +# @note Lines and Planes are infinite. |
| 26 | +# |
| 27 | +# @version SketchUp 6.0 |
| 28 | +module Geom |
| 29 | + |
| 30 | + # Class Methods |
| 31 | + |
| 32 | + # The {.closest_points} method is used to compute the closest points on two |
| 33 | + # lines. |
| 34 | + # |
| 35 | + # line. |
| 36 | + # |
| 37 | + # @example |
| 38 | + # line1 = [Geom::Point3d.new(0, 2, 0), Geom::Vector3d.new(1, 0, 0)] |
| 39 | + # line2 = [Geom::Point3d.new(3, 0, 0), Geom::Vector3d.new(0, 1, 0)] |
| 40 | + # # This will return a point Point3d(3, 2, 0). |
| 41 | + # points = Geom.closest_points(line1, line2) |
| 42 | + # |
| 43 | + # @param [Array(Geom::Point3d, Geom::Vector3d)] line1 |
| 44 | + # The first line to |
| 45 | + # intersect |
| 46 | + # |
| 47 | + # @param [Array(Geom::Point3d, Geom::Vector3d)] line2 |
| 48 | + # The second line to |
| 49 | + # intersect |
| 50 | + # |
| 51 | + # @return [Array(Geom::Point3d, Geom::Point3d)] An array of two points. The |
| 52 | + # first point is on the first line and the second point is on the second |
| 53 | + # |
| 54 | + # @version SketchUp 6.0 |
| 55 | + def self.closest_points(line1, line2) |
| 56 | + end |
| 57 | + |
| 58 | + # The {.fit_plane_to_points} method is used to compute a plane that is a best |
| 59 | + # fit to an array of points. If more than three points are given some of the |
| 60 | + # points may not be on the plane. |
| 61 | + # |
| 62 | + # The plane is returned as an {Array} of 4 numbers which are the |
| 63 | + # coefficients of the plane equation <code>Ax + By + Cz + D = 0</code>. |
| 64 | + # |
| 65 | + # @example |
| 66 | + # point1 = Geom::Point3d.new(0, 0, 0) |
| 67 | + # point2 = Geom::Point3d.new(10, 10, 10) |
| 68 | + # point3 = Geom::Point3d.new(25, 25, 25) |
| 69 | + # plane = Geom.fit_plane_to_points(point1, point2, point3) |
| 70 | + # |
| 71 | + # @overload fit_plane_to_points(point1, point2, point3, ...) |
| 72 | + # |
| 73 | + # @param [Geom::Point3d] point1 |
| 74 | + # @param [Geom::Point3d] point2 |
| 75 | + # @param [Geom::Point3d] point3 |
| 76 | + # @return [Array(Geom::Point3d, Geom::Vector3d)] A plane |
| 77 | + # |
| 78 | + # @overload fit_plane_to_points(points) |
| 79 | + # |
| 80 | + # @param [Array<Geom::Point3d>] points |
| 81 | + # @return [Array(Geom::Point3d, Geom::Vector3d)] A plane |
| 82 | + # |
| 83 | + # @version SketchUp 6.0 |
| 84 | + def self.fit_plane_to_points(*args) |
| 85 | + end |
| 86 | + |
| 87 | + # The {.intersect_line_line} computes the intersection of two lines. |
| 88 | + # |
| 89 | + # @example |
| 90 | + # # Defines a line parallel to the Y axis, offset 20 units. |
| 91 | + # line1 = [Geom::Point3d.new(20, 0, 0), Geom::Vector3d.new(0, 1, 0)] |
| 92 | + # # Defines a line parallel to the X axis, offset 10 units. |
| 93 | + # line2 = [Geom::Point3d.new(0, 10, 0), Geom::Point3d.new(20, 10, 0)] |
| 94 | + # # This will return a point Point3d(20, 10, 0). |
| 95 | + # point = Geom.intersect_line_line(line1, line2) |
| 96 | + # |
| 97 | + # @param [Array(Geom::Point3d, Geom::Vector3d)] line1 |
| 98 | + # The first line to |
| 99 | + # intersect. |
| 100 | + # |
| 101 | + # @param [Array(Geom::Point3d, Geom::Vector3d)] line2 |
| 102 | + # The second line to |
| 103 | + # intersect. |
| 104 | + # |
| 105 | + # @return [Geom::Point3d, nil] The intersection point. Returns +nil+ if they |
| 106 | + # do not intersect. |
| 107 | + # |
| 108 | + # @see Geom |
| 109 | + # The Geom module for alternative versions of defining a line. |
| 110 | + # |
| 111 | + # @version SketchUp 6.0 |
| 112 | + def self.intersect_line_line(line1, line2) |
| 113 | + end |
| 114 | + |
| 115 | + # The {.intersect_line_plane} method is used to compute the intersection of a |
| 116 | + # line and a plane. |
| 117 | + # |
| 118 | + # @example |
| 119 | + # # Defines a line parallel to the X axis, offset 20 units. |
| 120 | + # line = [Geom::Point3d.new(-10, 20, 0), Geom::Vector3d.new(1, 0, 0)] |
| 121 | + # # Defines a plane with it's normal parallel to the x axis. |
| 122 | + # plane = [Geom::Point3d.new(10, 0 ,0), Geom::Vector3d.new(1, 0, 0)] |
| 123 | + # # This will return a point Point3d(10, 20, 0). |
| 124 | + # point = Geom.intersect_line_plane(line, plane) |
| 125 | + # |
| 126 | + # @param [Array(Geom::Point3d, Geom::Vector3d)] line |
| 127 | + # |
| 128 | + # @param [Array(Geom::Point3d, Geom::Point3d)] plane |
| 129 | + # |
| 130 | + # @return [Geom::Point3d, nil] A Point3d object. Returns +nil+ if they do not |
| 131 | + # intersect. |
| 132 | + # |
| 133 | + # @see Geom |
| 134 | + # The Geom module for alternative versions of defining lines and |
| 135 | + # planes. |
| 136 | + # |
| 137 | + # @version SketchUp 6.0 |
| 138 | + def self.intersect_line_plane(line, plane) |
| 139 | + end |
| 140 | + |
| 141 | + # The {.intersect_plane_plane} method is used to compute the intersection of |
| 142 | + # two planes. |
| 143 | + # |
| 144 | + # @example |
| 145 | + # # Defines a plane with it's normal parallel to the x axis. |
| 146 | + # plane1 = [Geom::Point3d.new(10, 0 ,0), Geom::Vector3d.new(1, 0, 0)] |
| 147 | + # # Defines a plane with it's normal parallel to the y axis. |
| 148 | + # plane2 = [Geom::Point3d.new(0, 20 ,0), Geom::Vector3d.new(0, 1, 0)] |
| 149 | + # # This will return a line [Point3d(10, 20, 0), Vector3d(0, 0, 1)]. |
| 150 | + # line = Geom.intersect_plane_plane(plane1, plane2) |
| 151 | + # |
| 152 | + # @param [Array(Geom::Point3d, Geom::Point3d)] plane1 |
| 153 | + # The first plane to |
| 154 | + # intersect |
| 155 | + # |
| 156 | + # @param [Array(Geom::Point3d, Geom::Point3d)] plane2 |
| 157 | + # The second plane to |
| 158 | + # intersect |
| 159 | + # |
| 160 | + # @return [Array(Geom::Point3d, Geom::Vector3d)] A line where the planes |
| 161 | + # intersect if successful. Returns +nil+ if the planes do not intersect. |
| 162 | + # |
| 163 | + # @version SketchUp 6.0 |
| 164 | + def self.intersect_plane_plane(plane1, plane2) |
| 165 | + end |
| 166 | + |
| 167 | + # The {.linear_combination} method is used to compute the linear combination of |
| 168 | + # points or vectors. |
| 169 | + # |
| 170 | + # A linear combination is a standard term for vector math. It is defined as |
| 171 | + # vector = weight1 * vector1 + weight2 * vector2. |
| 172 | + # |
| 173 | + # @example |
| 174 | + # point1 = Geom::Point3d.new(1, 1, 1) |
| 175 | + # point2 = Geom::Point3d.new(10, 10, 10) |
| 176 | + # # Gets the point on the line segment connecting point1 and point2 that is |
| 177 | + # # 3/4 the way from point1 to point2: Point3d(7.75, 7.75, 7.75). |
| 178 | + # point = Geom.linear_combination(0.25, point1, 0.75, point2) |
| 179 | + # |
| 180 | + # @overload linear_combination(weight1, point1, weight2, point2) |
| 181 | + # |
| 182 | + # @param [Float] weight1 |
| 183 | + # @param [Geom::Point3d] point1 |
| 184 | + # @param [Float] weight2 |
| 185 | + # @param [Geom::Point3d] point2 |
| 186 | + # @return [Geom::Point3d] |
| 187 | + # |
| 188 | + # @overload linear_combination(weight1, vector1, weight2, vector2) |
| 189 | + # |
| 190 | + # @param [Float] weight1 |
| 191 | + # @param [Geom::Vector3d] vector1 |
| 192 | + # @param [Float] weight2 |
| 193 | + # @param [Geom::Vector3d] vector2 |
| 194 | + # @return [Geom::Vector3d] |
| 195 | + # |
| 196 | + # @version SketchUp 6.0 |
| 197 | + def self.linear_combination(weight1, pt_or_vect1, weight2, pt_or_vect2) |
| 198 | + end |
| 199 | + |
| 200 | + # The {.point_in_polygon_2D} method is used to determine whether a point is |
| 201 | + # inside a polygon. The z component of both the point you're checking and |
| 202 | + # the points in the polygon are ignored, effectively making it a 2-d check. |
| 203 | + # |
| 204 | + # @example |
| 205 | + # # Create a point that we want to check. (Note that the 3rd coordinate, |
| 206 | + # # the z, is ignored for purposes of the check.) |
| 207 | + # point = Geom::Point3d.new(5, 0, 10) |
| 208 | + # |
| 209 | + # # Create a series of points of a triangle we want to check against. |
| 210 | + # triangle = [] |
| 211 | + # triangle << Geom::Point3d.new(0, 0, 0) |
| 212 | + # triangle << Geom::Point3d.new(10, 0, 0) |
| 213 | + # triangle << Geom::Point3d.new(0, 10, 0) |
| 214 | + # |
| 215 | + # # Test to see if our point is inside the triangle, counting hits on |
| 216 | + # # the border as an intersection in this case. |
| 217 | + # hits_on_border_count = true |
| 218 | + # status = Geom.point_in_polygon_2D(point, triangle, hits_on_border_count) |
| 219 | + # |
| 220 | + # @param [Geom::Point3d] point |
| 221 | + # |
| 222 | + # @param [Array<Geom::Point3d>] polygon |
| 223 | + # An array of points that represent the |
| 224 | + # corners of the polygon you are checking against. |
| 225 | + # |
| 226 | + # @param [Boolean] check_border |
| 227 | + # Pass true if a point on the border should be |
| 228 | + # counted as inside the polygon. |
| 229 | + # |
| 230 | + # @return [Boolean] +true+ if the point is inside the polygon. |
| 231 | + # |
| 232 | + # @version SketchUp 6.0 |
| 233 | + def self.point_in_polygon_2D(point, polygon, check_border) |
| 234 | + end |
| 235 | + |
| 236 | + # Tessellates a polygon, represented as a collection of 3D points. Can include |
| 237 | + # holes by providing collections of points describing the inner loops. This is |
| 238 | + # intended to be used for planar polygons. |
| 239 | + # |
| 240 | + # Useful to draw concave polygons using {Sketchup::View#draw} or |
| 241 | + # {Sketchup::View#draw2d}. |
| 242 | + # |
| 243 | + # It can also be useful for importers where the input format describes only the |
| 244 | + # loops for a polygon and you want to work with a collection of triangles. |
| 245 | + # |
| 246 | + # <b>Polygon with two holes, one empty and one filled:</b> |
| 247 | + # |
| 248 | + # <i>(See "Drawing a polygon with holes from a custom tool" example)</i> |
| 249 | + # |
| 250 | + # rdoc-image:images/geom-tesselation-polygon-with-holes.png |
| 251 | + # |
| 252 | + # @example Iterate over each triangle in the returned set |
| 253 | + # polygon = [ |
| 254 | + # Geom::Point3d.new(0, 0, 0), |
| 255 | + # Geom::Point3d.new(90, 0, 0), |
| 256 | + # Geom::Point3d.new(60, 40, 0), |
| 257 | + # Geom::Point3d.new(90, 90, 0), |
| 258 | + # Geom::Point3d.new(30, 70, 0), |
| 259 | + # ] |
| 260 | + # triangles = Geom.tesselate(polygon) |
| 261 | + # triangles.each_slice(3) { |triangle| |
| 262 | + # # Work with each triangle set... |
| 263 | + # } |
| 264 | + # # Or get an array of triangles: |
| 265 | + # triangles_set = triangles.each_slice(3).to_a |
| 266 | + # |
| 267 | + # @example Drawing a polygon with holes from a custom tool |
| 268 | + # class ExampleTool |
| 269 | + # |
| 270 | + # def initialize |
| 271 | + # polygon = [ |
| 272 | + # Geom::Point3d.new(0, 0, 0), |
| 273 | + # Geom::Point3d.new(90, 0, 0), |
| 274 | + # Geom::Point3d.new(60, 40, 0), |
| 275 | + # Geom::Point3d.new(90, 90, 0), |
| 276 | + # Geom::Point3d.new(30, 70, 0), |
| 277 | + # ] # Counter-clockwise order |
| 278 | + # hole1 = [ |
| 279 | + # Geom::Point3d.new(20, 10, 0), |
| 280 | + # Geom::Point3d.new(40, 10, 0), |
| 281 | + # Geom::Point3d.new(45, 25, 0), |
| 282 | + # Geom::Point3d.new(30, 20, 0), |
| 283 | + # Geom::Point3d.new(25, 25, 0), |
| 284 | + # ].reverse # Clockwise order - empty hole |
| 285 | + # hole2 = [ |
| 286 | + # Geom::Point3d.new(30, 40, 0), |
| 287 | + # Geom::Point3d.new(50, 40, 0), |
| 288 | + # Geom::Point3d.new(50, 50, 0), |
| 289 | + # Geom::Point3d.new(30, 50, 0), |
| 290 | + # ].reverse # Counter-clockwise order - filled hole |
| 291 | + # @triangles = Geom.tesselate(polygon, hole1, hole2) |
| 292 | + # end |
| 293 | + # |
| 294 | + # def activate |
| 295 | + # Sketchup.active_model.active_view.invalidate |
| 296 | + # end |
| 297 | + # |
| 298 | + # def onMouseMove(flags, x, y, view) |
| 299 | + # view.invalidate |
| 300 | + # end |
| 301 | + # |
| 302 | + # def getExtents |
| 303 | + # bounds = Geom::BoundingBox.new |
| 304 | + # bounds.add(@triangles) |
| 305 | + # bounds |
| 306 | + # end |
| 307 | + # |
| 308 | + # def draw(view) |
| 309 | + # view.drawing_color = Sketchup::Color.new(192, 0, 0) |
| 310 | + # view.draw(GL_TRIANGLES, @triangles) |
| 311 | + # end |
| 312 | + # |
| 313 | + # end |
| 314 | + # |
| 315 | + # Sketchup.active_model.select_tool(ExampleTool.new) |
| 316 | + # |
| 317 | + # @note The winding order of the polygons matter. The outer loop should be |
| 318 | + # in counter-clockwise order. To cut an empty hole the inner loops should be |
| 319 | + # in clockwise order, otherwise they will create a loop filled with |
| 320 | + # triangles. |
| 321 | + # |
| 322 | + # @note The tesselation is using the same logic as SketchUp its rendering |
| 323 | + # pipeline. But the exact result is an implementation detail which may change |
| 324 | + # between versions. In particularly the results of degenerate polygons and |
| 325 | + # non-planar polygons is undefined as part of the API contract. Such |
| 326 | + # polygons are for example polygons where all points are colinear, polygons |
| 327 | + # with duplicate points, non-planar polygons. |
| 328 | + # |
| 329 | + # @note If you want the triangles from an existing {Sketchup::Face} it's better |
| 330 | + # to use {Sketchup::Face#mesh}. |
| 331 | + # |
| 332 | + # @param [Array<Geom::Point3d>] polygon_loop_points |
| 333 | + # |
| 334 | + # @param [Array<Array<Geom::Point3d>>] inner_loop_points |
| 335 | + # |
| 336 | + # @raise [ArgumentError] if any of the loops contain less than three points. |
| 337 | + # |
| 338 | + # @raise [RuntimeError] if the tesselator returned an error. |
| 339 | + # |
| 340 | + # @return [Array<Geom::Point3d>] an array of points with a stride of three |
| 341 | + # representing a set of triangles. |
| 342 | + # |
| 343 | + # @see Sketchup::View#draw |
| 344 | + # |
| 345 | + # @see Sketchup::View#draw2d |
| 346 | + # |
| 347 | + # @version SketchUp 2020.0 |
| 348 | + def self.tesselate(polygon_loop_points, *inner_loop_points) |
| 349 | + end |
| 350 | + |
| 351 | +end |
0 commit comments