Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Polygon dimensions

    Cinema 4D SDK
    python r20
    3
    4
    1.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      pim
      last edited by pim

      I want to fit images in selected polygons.
      To do so, I get the polygon and then I like to get the smallest width and the smallest height of the polygon.
      With this rectangle information, I create a plane and place one image on the plane.
      The plane is then placed using the normal of the polygon.

      Question: is there an easy way to get the smallest width and height of a polygon.

      See for example this picture
      e56e0a8a-b0fc-4108-86e9-928158dff26e-image.png

      Here how I calculate the width and height.

      p = obj.GetPolygon(index)
      
      a = allp[p.a]           
      b = allp[p.b]
      c = allp[p.c]
      d = allp[p.d]
      
      w1 = (a - d).GetLength()
      w2 = (b - c).GetLength()
      h1 = (a - b).GetLength()
      h2 = (d - c).GetLength()
      
      width = w1
      if (w2 < w1): width = w2
      height = h1
      if (h2 < h1): height = h2
      

      This code does not seem correct, because the Coordinate Manager show a different size.

      Also, the sequence of the points (a,b,c,d) is not always the same.
      Sometimes it starts in the lower left corner and sometime in the upper right corner.

      1 Reply Last reply Reply Quote 0
      • CairynC
        Cairyn
        last edited by

        First, the coordinate manager is not showing the dimensions of the polygon. It's showing the dimensions of the enclosing axis-parallel cuboid, where the axes are either the axes of the object or the world system. So you will not get the width and height information from that (at least not in the way your image suggests).

        The point order is arbitrary. I'm not sure why you expect the points to start in a certain corner?

        Second, your code is extremely specific. A polygon may be nonplanar or rotated in space. The sides of a polygon may not be parallel (as in square, rectangle, parallelogram or trapeze), and even if some sides are parallel, they don't necessarily are parallel to any axis system (e.g. to the ground). I do not know how you place your plane and image, but it looks as if the code would only work if the two sides used as "width" are parallel to ground level (and even then the height is wrong for a trapeze as shown in the image).

        To solve the projection, you need to define first what "width" and "height" should be for an arbitary polygon (a parallelogram e.g. has two heights...), and what kind of restrictions you make on the input (I assume you will only handle planar quads, but do you want to rotate the image, or should it always be parallel to the ground? Do you want to maximize image size on the polygon? Do you want the quads to be trapezes only? Do some quad edges need to be parallel to the ground or not?). Once you have that parameters, you can start thinking about how these widths and heights can be calculated.

        (For measures of classic polygons, you can always google "height parallelogram" e.g.)

        1 Reply Last reply Reply Quote 3
        • P
          pim
          last edited by

          Thanks for the detailed answer.
          And yes, I guess my approach is way to simple.
          However, I use the normal of the polygon to place the plane and getting the minimal width and height, best fits a planar polygon.

          "The point order is arbitrary. I'm not sure why you expect the points to start in a certain corner?"
          For consistency I would expect the points to be in a predefined sequence.
          Now it seems randomly (clockwise, anti clockwise, were is point a located?).

          1 Reply Last reply Reply Quote 0
          • r_giganteR
            r_gigante
            last edited by

            Hi Pim, thanks for reaching out us.

            With regard to your question, given for comments from Cairyn, I recommend to refine your approach and to look on the web since there are a few different strategies to find the largest inscribed rectangle in a non-convex polygon (among them I recommend having a look at this).

            With regard to the points walking direction for a given polygon this is defined by the polygon normal and the left-hand rule and it should definitively not be randomly defined.

            Cheers, Riccardo

            1 Reply Last reply Reply Quote 0
            • First post
              Last post