Help please. How to get transformation for the face

Help please. How to get transformation for the face

Postby cesaro36 » Fri Feb 24, 2017 7:48 pm

Hi everyone
I need help to get the transformation of the current face under the cursor.
I'm using the input point method but my problem emerges when the cursor is Inferenced.

It is weird because my @ip.face is correct but @ip.transformation doesnt match.

I dont use a pick helper because I need to get faces outside p.e. when editing inside a component.

The only plugin that I've seen it managing well is AngleInpector from Freddo tools.

Any ideas?

Thanks
0
Last edited by cesaro36 on Fri Feb 24, 2017 10:57 pm, edited 1 time in total.

cesaro36 
Premium Member
Premium Member
 

Re: Help please. How to get transformation for the face

Postby sdmitch » Fri Feb 24, 2017 8:08 pm

@ip.transformation would be the transformation of the parent of the face not the face itself.
0
Nothing is worthless, it can always be used as a bad example.

http://sdmitch.blogspot.com/
User avatar
sdmitch 
PluginStore Author
PluginStore Author
 

Re: Help please. How to get transformation for the face

Postby cesaro36 » Fri Feb 24, 2017 8:37 pm

thanks, but... what I mean is how to get the correct transformation for the face bellow when the cursor is inferenced. Is there something I'm missing?
0

cesaro36 
Premium Member
Premium Member
 

Re: Help please. How to get transformation for the face

Postby TIG » Fri Feb 24, 2017 8:48 pm

A face doesn't have a "transformation" per se.
The "input point" your identify can give the transformation of where it was picked - which you can then use to modify entities' transformations later...
You can "transform" a group/instance, face, edge or vertex...

You can gather various info about any picked vertex/edge/face [e.g. position/line/center/normal ] - and devise transformations to modify these [bearing in mind the parent's transformation you have also collected...]
BUT what do you want to do ?

Please explain what you want to 'find' and what you want to do with it...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Help please. How to get transformation for the face

Postby cesaro36 » Fri Feb 24, 2017 9:08 pm

Thanks TIG
My intention is to highlight the face that the cursor is passing over.
In the Animation GIF you can see that when the interfering is happening the highlighted face is been drawn where it should'n, meaning that the transformation given by @ip.transformation is not the same as it was before the iterferencing while the @ip.face is still the same. (is that make sense)
I'm doing my best trying to explain this in english.
0

cesaro36 
Premium Member
Premium Member
 

Re: Help please. How to get transformation for the face

Postby Dan Rathbun » Sat Feb 25, 2017 8:11 pm

cesaro36 wrote:Thanks TIG
My intention is to highlight the face that the cursor is passing over.

You can do this easily by pushing the face's reference into the model selection set.

old_set = model.selection.to_a
model.selection.clear
model.selection.add(@ip.face)
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: Help please. How to get transformation for the face

    Postby Anton_S » Sun Feb 26, 2017 12:11 pm

    cesaro36,

    Like TIG and others mentioned a face doesn't have a transformation, but its point positions are bound to the transformation of a group/component instance containing the face. In that case, you will need to transform all points of the face into global space before highlighting the face.

    Here is a sample face highligher tool:
    Code: Select all
    class FaceHighlighterTool

      def initialize
        @ip = Sketchup::InputPoint.new
        @triplets = []
        @edge_points = []
        @hovered_face = nil
        @hovered_face_tra = nil
        @face_color = Sketchup::Color.new(0,40,255,80)
        @edge_color = Sketchup::Color.new(0,0,255,255)
        @edge_width = 3
      end

      def deactivate(view)
        @hovered_face = nil
        view.invalidate
      end

      def onMouseMove(flags, x, y, view)
        @ip.pick(view, x, y)
        face = @ip.face
        if face.nil?
          if @hovered_face
            @hovered_face = nil
            view.invalidate
          end
          return
        end
        if face != @hovered_face
          @hovered_face = face
          @hovered_face_tra = @ip.transformation
          # In order to draw a face with holes, we must draw its mesh
          mesh = face.mesh
          polygons_size = mesh.count_polygons
          @triplets = Array.new(polygons_size)
          for i in 0...polygons_size
            # Obtain one of the triangles making up the face
            triplet = mesh.polygon_points_at(i+1)
            # Transform to global space
            triplet.each { |pt| pt.transform!(@hovered_face_tra) }
            # Store for drawing
            @triplets[i] = triplet
          end
          # Get all edges for drawing a border
          @edge_points.clear
          face.edges.each { |edge|
            @edge_points << edge.start.position
            @edge_points << edge.end.position
          }
          # Transform all edge points to global space
          @edge_points.each { |point| point.transform!(@hovered_face_tra) }
          # Trigger the drawing
          view.invalidate
        end
      end

      def draw(view)
        return unless @hovered_face
        view.drawing_color = @face_color
        @triplets.each { |triplet|
          view.draw(GL_POLYGON, triplet)
        }
        view.drawing_color = @edge_color
        view.line_width = @edge_width
        view.line_stipple = ''
        view.draw(GL_LINES, @edge_points)
      end

    end # class FaceHighlighterTool

    Sketchup.active_model.select_tool(FaceHighlighterTool.new)

    If you paste it all into Ruby Console you will see the effect.

    Anton
    0
    Last edited by Anton_S on Sun May 14, 2017 8:02 am, edited 1 time in total.

    Anton_S 
    PluginStore Author
    PluginStore Author
     

    Re: Help please. How to get transformation for the face

    Postby cesaro36 » Sun Feb 26, 2017 7:03 pm

    Thanks Anton
    I'll try it as soon as I get to my laptop.
    0

    cesaro36 
    Premium Member
    Premium Member
     

    Re: Help please. How to get transformation for the face

    Postby TBoy » Tue May 09, 2017 4:03 pm

    Anton_S,
    Do you have similar code to highlight local bounding boxes (including those inside groups and components)?
    0

    TBoy 
     

    Re: Help please. How to get transformation for the face

    Postby Anton_S » Sun May 14, 2017 9:28 am

    There it is, TBoy:
    Code: Select all
    class BoundsHighlighterTool

      def initialize
        @ip = Sketchup::InputPoint.new
        @hovered_inst = nil
        @global_parent_tra = nil
        @global_bb = nil
        @labb_global_faces = [] # Faces for the local axes aligned bounding box.
        @gabb_global_faces = [] # Faces for the global axes aligned bounding box.
        @labb_global_edges = []
        @gabb_global_edges = []
        @labb_face_color = Sketchup::Color.new(255,40,0,80)
        @gabb_face_color = Sketchup::Color.new(0,40,255,80)
        @labb_edge_color = Sketchup::Color.new(255,0,0,255)
        @gabb_edge_color = Sketchup::Color.new(0,0,255,255)
        @edge_width = 3
      end

      def deactivate(view)
        reset(view)
      end

      def onMouseMove(flags, x, y, view)
        @ip.pick(view, x, y)
        ip_path = @ip.instance_path
        if ip_path.empty?
          reset(view)
          return
        end
        inst = nil
        gptra = nil
        ip_path.each { |ent|
          break if !ent.is_a?(Sketchup::Group) && !ent.is_a?(Sketchup::ComponentInstance)
          if gptra
            gptra = gptra * inst.transformation
          elsif inst
            gptra = inst.transformation
          end
          inst = ent
        }
        unless inst
          reset(view)
          return
        end
        return if inst == @hovered_inst
        @hovered_inst = inst
        @global_parent_tra = gptra
        local_bb = @hovered_inst.bounds
        # Obtain corners of local axes aligned bounding box in global space
        lagc = []
        for i in 0..7
          lagc << local_bb.corner(i)
        end
        if @global_parent_tra
          lagc.each { |point| point.transform!(@global_parent_tra) }
        end
        @labb_global_faces = [
          [lagc[0], lagc[2], lagc[3], lagc[1]],
          [lagc[4], lagc[6], lagc[7], lagc[5]],
          [lagc[1], lagc[0], lagc[4], lagc[5]],
          [lagc[2], lagc[3], lagc[7], lagc[6]],
          [lagc[0], lagc[2], lagc[6], lagc[4]],
          [lagc[3], lagc[1], lagc[5], lagc[7]]
        ]
        @labb_global_edges = [
          lagc[0], lagc[2],
          lagc[2], lagc[6],
          lagc[6], lagc[4],
          lagc[4], lagc[0],
          lagc[3], lagc[1],
          lagc[1], lagc[5],
          lagc[5], lagc[7],
          lagc[7], lagc[3],
          lagc[0], lagc[1],
          lagc[2], lagc[3],
          lagc[4], lagc[5],
          lagc[6], lagc[7]
        ]
        # Create global axes aligned bounding box
        @global_bb = Geom::BoundingBox.new()
        @global_bb.add(lagc)
        # Obtain corners of global axes aligned bounding box in global space
        gagc = []
        for i in 0..7
          gagc << @global_bb.corner(i)
        end
        @gabb_global_faces = [
          [gagc[0], gagc[2], gagc[3], gagc[1]],
          [gagc[4], gagc[6], gagc[7], gagc[5]],
          [gagc[1], gagc[0], gagc[4], gagc[5]],
          [gagc[2], gagc[3], gagc[7], gagc[6]],
          [gagc[0], gagc[2], gagc[6], gagc[4]],
          [gagc[3], gagc[1], gagc[5], gagc[7]]
        ]
        @gabb_global_edges = [
          gagc[0], gagc[2],
          gagc[2], gagc[6],
          gagc[6], gagc[4],
          gagc[4], gagc[0],
          gagc[3], gagc[1],
          gagc[1], gagc[5],
          gagc[5], gagc[7],
          gagc[7], gagc[3],
          gagc[0], gagc[1],
          gagc[2], gagc[3],
          gagc[4], gagc[5],
          gagc[6], gagc[7]
        ]
        view.invalidate
      end

      def draw(view)
        return unless @hovered_inst
        # Draw local axes aligned global bounding box
        view.drawing_color = @labb_face_color
        @labb_global_faces.each { |face|
          view.draw(GL_POLYGON, face)
        }
        view.drawing_color = @labb_edge_color
        view.line_width = @edge_width
        view.line_stipple = ''
        view.draw(GL_LINES, @labb_global_edges)
        # Draw global axes aligned global bounding box
        view.drawing_color = @gabb_face_color
        @gabb_global_faces.each { |face|
          view.draw(GL_POLYGON, face)
        }
        view.drawing_color = @gabb_edge_color
        view.line_width = @edge_width
        view.line_stipple = ''
        view.draw(GL_LINES, @gabb_global_edges)
      end

      def reset(view)
        return false unless @hovered_inst
        @hovered_inst = nil
        @global_parent_tra = nil
        @global_bb = nil
        @labb_global_faces.clear
        @gabb_global_faces.clear
        @labb_global_edges.clear
        @gabb_global_edges.clear
        view.invalidate
        return true
      end

    end # class BoundsHighlighterTool

    Sketchup.active_model.select_tool(BoundsHighlighterTool.new)

    This time we iterate through the InputPoint's instance_path and transform the bounding box of the deepest instance across all the the parent groups/component instances in the path.
    1

    Anton_S 
    PluginStore Author
    PluginStore Author
     

    SketchUcation One-Liner Adverts

    by Ad Machine » 5 minutes ago



    Ad Machine 
    Robot
     



     

    Return to Developers' Forum

    Who is online

    Users browsing this forum: Google Bot and 1 guest

    Visit our sponsors: