Author Topic: comparing two polygons are exactly over lapping or not  (Read 2102 times)

0 Members and 1 Guest are viewing this topic.

subbup

  • Guest
comparing two polygons are exactly over lapping or not
« on: November 01, 2004, 07:35:41 AM »
Hello all,

I have one polygon and another one is image frame. I want to check the polygon is excatly matching with image frame or not (means co-ordinates).

Any Ideas :idea:

SMadsen

  • Guest
comparing two polygons are exactly over lapping or not
« Reply #1 on: November 03, 2004, 05:37:27 AM »
In the following, I take it that none of your images are clipped but appear with rectangular frames that are equal to the extents of any image.

Image coordinates are not readily available because they are depending on various factors such as image resolution, scale and rotation. Thus the coordinates are available by calculation only. What you need is an insertion point, an image dimension and a pair of unit vectors. They come in the form of dxf codes 10, 13 and 11/12, respectively, within the image entity list.

Once you have these properties, the calculation is pretty simple. Insertion point is given in WCS coordinates. Image dimension is given in pixels. Unit vectors are given in drawing units per pixel. One unit vector runs along the bottom edge of the image. That's the code 11. The other runs along the left edge. That's the code 12. In image mapping terms, such directions are known as U and V, so I'll use those letters in the calculations.

Let's get the data. Suppose imgl is the entity list of an image:

(setq ins (cdr (assoc 10 imgl)))
(setq uvec (cdr (assoc 11 imgl)))
(setq vvec (cdr (assoc 12 imgl)))
(setq pdim (cdr (assoc 13 imgl)))

First you need to figure out how to expand the U & V vectors into the full dimension of the image. Easy. Simply multiply the U dimension with the U vector, and the V dimension with the V vector:

(setq udim (mapcar '(lambda (u)(* u (car pdim))) uvec))
(setq vdim (mapcar '(lambda (v)(* v (cadr pdim))) vvec))

Now you'll have the extent in both directions. Move these coordinates with the insertion point and you'll have the actual corners in WCS:

(setq u_pt (mapcar '+ ins udim))
(setq v_pt (mapcar '+ ins vdim))

The upper right corner is a bit more tricky. Unless, of course, that your image is in WCS - then you can simply extract X and Y from u_pt and v_pt. But if the image is rotated from WCS you need to add both vectors and the insertion point:

(setq uv_pt (mapcar '+ ins udim vdim))

And there you have it. The four corners will be (ins u_pt v_pt uv_pt).

The whole thing could be written like this:

Code: [Select]
(defun getImageCoords (/ img imgl ins pdim udim vdim uvec vvec
                       uv_pt u_pt v_pt)
  (setq img (car (entsel "\nSelect image: ")))
  (cond ((and img
              (= (cdr (assoc 0 (setq imgl (entget img)))) "IMAGE")
         )
         (setq ins  (cdr (assoc 10 imgl))
               uvec (cdr (assoc 11 imgl))
               vvec (cdr (assoc 12 imgl))
               pdim (cdr (assoc 13 imgl))
         )
         (setq udim (mapcar '(lambda (u) (* u (car pdim))) uvec)
               vdim (mapcar '(lambda (v) (* v (cadr pdim))) vvec)
         )
         (setq u_pt  (mapcar '+ udim ins)
               v_pt  (mapcar '+ vdim ins)
               uv_pt (mapcar '+ ins udim vdim)
         )
         (list ins u_pt v_pt uv_pt)
        )
        ((princ "No image selected"))
  )
)


Once having the image coordinates (translated to whichever UCS you need) and the polyline coordinates, it should not be a complicated task to compare them.
Tip: use EQUAL, preferrably with a fuzz factor, to compare points.

SMadsen

  • Guest
comparing two polygons are exactly over lapping or not
« Reply #2 on: November 03, 2004, 05:58:38 AM »
By the way, if you're interested in more talk about images in AutoCAD you can pick up Kenny's latest newsletter (I think it's the latest?) here

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
comparing two polygons are exactly over lapping or not
« Reply #3 on: November 03, 2004, 08:05:36 AM »
Very informative Stig.
Thank you.
CAB
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

SMadsen

  • Guest
comparing two polygons are exactly over lapping or not
« Reply #4 on: November 03, 2004, 08:07:10 AM »
Thanks CAB