Skip to content

Instantly share code, notes, and snippets.

@thomthom
Created November 1, 2013 11:18
Show Gist options
  • Save thomthom/7264065 to your computer and use it in GitHub Desktop.
Save thomthom/7264065 to your computer and use it in GitHub Desktop.
Snippet for getting UVQ and UV data from 3D points
model = Sketchup.active_model
face = model.selection[0]
uvqh = UVQHelper.new(face)
face.vertices.size.times { |i| p uvqh.transform3DtoUVQ(true, face.vertices[i].position) }
# => Point3d(0, 0, 1)
# => Point3d(1, 0, 1)
# => Point3d(0.839757, 0.451277, 0.451277)
# => Point3d(-0.160243, 0.451277, 0.451277)
face.vertices.size.times { |i| p uvqh.transform3DtoUV(true, face.vertices[i].position) }
# => Point3d(0, 0, 1)
# => Point3d(1, 0, 1)
# => Point3d(1.86084, 1, 1)
# => Point3d(-0.355088, 1, 1)
class UVQHelper
# @param [Sketchup::Face] face
# @param [Boolean] front
# @param [Boolean] back
# @param [Sketchup::TextureWriter] texturewriter
def initialize(face, front = true, back = true,
texturewriter = nil)
args = [front, back]
args << texturewriter if texturewriter
@uvh = face.get_UVHelper(*args)
end
# Simple wrapper over UVHelper.get_front_UVQ. 3D points are projected to
# the plane of the face and then a UVQ value is returned.
#
# @param [Boolean] front
# @param [Geom::Point3d] point
#
# @return [Geom::Point3d] UVQ point
def transform3DtoUVQ(front, point)
if front
@uvh.get_front_UVQ(point)
else
@uvh.get_back_UVQ(point)
end
end
# 3D points are projected to the plane of the face and then a UV value is
# returned.
#
# @param [Boolean] front
# @param [Geom::Point3d] point
#
# @return [Geom::Point3d] UV point
def transform3DtoUV(front, point)
uvq = nil
if front
uvq = @uvh.get_front_UVQ(point)
else
uvq = @uvh.get_back_UVQ(point)
end
Geom::Point3d.new(uvq.x / uvq.z, uvq.y / uvq.z, 1.0)
end
end # class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment