Move polygon along normal



  • On 28/03/2013 at 11:26, xxxxxxxx wrote:

    I want to move a polygon along its normal (normal move).

    So I calculate the cross vector of the polygon using two sides as vector.
    However, when I compare my result with the result of the Xpresso' polygon node - polygon normal' it is different. But the point normal from the Point node is the same as the cross vector I calculate.
    Strange?

    Is the point normal different then the polygon normal?
    Also when I start moving the polygon, my calculation seems to go wrong.
    I expect that my calculation of the cross vector is wrong.

    I get all the polygon points, normalize them and use them to calculate the vectors.
    This might be wrong, because I do not include the starting point?

    Note: There is only one polygon in my polygon object.
    I do not want to use the cinema 4d 'move normal' command.

      
    import c4d  
    from c4d import utils  
    import random  
      
    #  
    # move polygon along its normal  
    #  
        
    def main() :  
      
      polyobject = doc.SearchObject("MyObject")  
      polys = polyobject.GetAllPolygons()  
        
      p1 = polyobject.GetPoint(polys[0].a)  
      p2 = polyobject.GetPoint(polys[0].b)  
      p3 = polyobject.GetPoint(polys[0].c)  
      p4 = polyobject.GetPoint(polys[0].d)  
      print p1,p2,p3,p4  
      
      np1 = p1.GetNormalized()  
      np2 = p2.GetNormalized()  
      np3 = p3.GetNormalized()  
      np4 = p4.GetNormalized()  
      p1p2 = (np2-np1)  
      p1p3 = (np3-np1)  
        
      pcross = p1p2.Cross(p1p3)  
      pcross = pcross.GetNormalized()  
      print "cross: ", pcross  
    


  • On 28/03/2013 at 11:37, xxxxxxxx wrote:

    cpoly = polygonObject.GetPolygon(id)
    nrm = ((cpoly.a - cpoly.b) % (cpoly.a - cpoly.d)).GetNormalized()



  • On 28/03/2013 at 11:44, xxxxxxxx wrote:

    For a cube, I get Vector(0, 0, -1) which is correct. Btw, a bit shorter:

    def main() :
        p = op.GetPolygon(0)
        points = op.GetAllPoints()
      
        p1, p2, p4 = points[p.a], points[p.b], points[p.d]
        print (p2 - p1).Cross(p4 - p1).GetNormalized()
      
    main()
    

    Note that you need to take the forth point into account instead of the third, which might
    be the cause why you get "wrong values".



  • On 28/03/2013 at 11:47, xxxxxxxx wrote:

    lol, i forgot to access the vectors, cpolys just contains the ids , doh, i am a it dizzy, just 
    follow nikklas :)



  • On 28/03/2013 at 11:49, xxxxxxxx wrote:

    I just checked the polygon-node in XPresso and it gives the same result.

    @Ferdinand: You also computed the inversed normal. :) It's (B - A) cross (D - A) to compute
    the correct normal.
    Ehrm wait, I thought that it would be.. but it isn't? Just checked.. Gonna think about that 😂



  • On 28/03/2013 at 13:49, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    For a cube, I get Vector(0, 0, -1) which is correct. Btw, a bit shorter:

    def main() :
        p = op.GetPolygon(0)
        points = op.GetAllPoints()
     
        p1, p2, p4 = points[p.a], points[p.b], points[p.d]
        print (p2 - p1).Cross(p4 - p1).GetNormalized()
     
    main()
    

    Note that you need to take the forth point into account instead of the third, which might
    be the cause why you get "wrong values".

    Brilliant again, thanks.
    Indeed using p4, I do not get 'wrong values'.

    Here the code to get the polygon moving along the normal.
    Just add it to all the points of the polygon and do an update.

    Note: the calculated cross is now the same as the Xpresso one and Xpresso point normal is the same as the  polygon normal.

      
    import c4d  
    from c4d import utils  
    import random  
      
    #  
    # move polygon along its normal  
    #  
        
    def main() :  
      
      frame = doc.GetTime().GetFrame(doc.GetFps())  
      if (frame >= 5) :  
            
          polyobject = doc.SearchObject("MyObject")  
            
          p = polyobject.GetPolygon(0)        #get polygon  
          points = polyobject.GetAllPoints()  #get all points  
      
          p1, p2, p3, p4 = points[p.a], points[p.b], points[p.c], points[p.d]  
          pcross = (p2 - p1).Cross(p4 - p1).GetNormalized()  
          print pcross  
            
          p1 = p1 + pcross  
          p2 = p2 + pcross              
          p3 = p3 + pcross              
          p4 = p4 + pcross  
          #print p1,p2,p3,p4  
            
          p1 = polyobject.SetPoint(p.a,p1)  
          p2 = polyobject.SetPoint(p.b,p2)  
          p3 = polyobject.SetPoint(p.c,p3)  
          p4 = polyobject.SetPoint(p.d,p4)  
            
          polyobject.Message (c4d.MSG_UPDATE)  
          c4d.EventAdd()  
    

Log in to reply