{"id":283,"date":"2017-11-24T22:01:38","date_gmt":"2017-11-24T14:01:38","guid":{"rendered":"http:\/\/119.29.206.68\/?p=283"},"modified":"2018-12-02T17:42:48","modified_gmt":"2018-12-02T09:42:48","slug":"%e7%ba%bf%e6%ae%b5%e4%ba%a4%e7%82%b9-geos%e5%ae%9e%e7%8e%b0%e7%9a%84%e5%88%86%e6%9e%90%e4%bb%a5%e5%8f%8a%e6%88%91%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/www.whudj.cn\/?p=283","title":{"rendered":"\u7ebf\u6bb5\u4ea4\u70b9\/geos\u5b9e\u73b0\u7684\u5206\u6790\u4ee5\u53ca\u6211\u7684\u5b9e\u73b0"},"content":{"rendered":"<p>\u4e24\u7ebf\u6bb5\uff08\u76f4\u7ebf\/\u5c04\u7ebf\uff09\u6c42\u4ea4\u70b9\u662f\u51e0\u4f55\u8ba1\u7b97\u4e2d\u7684\u4e00\u4e2a\u6700\u57fa\u672c\u7684\u7b97\u6cd5\u3002\u867d\u7136\u5b83\u5927\u90e8\u5206\u60c5\u51b5\u90fd\u4e0d\u662f\u5f71\u54cd\u7a0b\u5e8f\u6548\u7387\u7684\u4e3b\u8981\u56e0\u7d20\uff0c\u4f46\u662f\u5b83\u7684\u6267\u884c\u6b21\u6570\u53ef\u80fd\u975e\u5e38\u7684\u9ad8\uff0c\u56e0\u6b64\u63d0\u5347\u5b83\u7684\u6267\u884c\u6548\u7387\u4ecd\u7136\u5177\u6709\u4ef7\u503c\u3002\u672c\u6587\u5c06\u5206\u4e3a\u4e24\u90e8\u5206\uff0c\u4e00\u90e8\u5206\u4ee5geos\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u4e3b\u6d41\u7684\u76f4\u7ebf\u4ea4\u70b9\u7684\u5224\u65ad\u4e0e\u8ba1\u7b97\u7b97\u6cd5\uff0c\u53e6\u4e00\u90e8\u5206\u7ed9\u51fa\u6211\u7684\u5b9e\u73b0\uff0c\u6211\u76f8\u4fe1\u5b83\u662f\u4e00\u79cd\u66f4\u52a0\u7b80\u5355\u548c\u9ad8\u6548\u7684\u7b97\u6cd5\uff0c\u5e76\u4e14\u53ef\u4ee5\u76f4\u63a5\u5e94\u7528\u4e8e\u66f4\u9ad8\u7684\u7ef4\u5ea6\u3002<!--more--><\/p>\n<p>geos\u00a0\u4e2d\u6267\u884c\u7ebf\u6bb5\u6c42\u4ea4\u7684\u7c7b\u662f<span class=\"lang:c++ decode:true crayon-inline\">geos::algorithm::LineIntersector::computeIntersection(const geom::Coordinate&amp; p1, const geom::Coordinate&amp; p2, const geom::Coordinate&amp; p3, const geom::Coordinate&amp; p4)<\/span>\u00a0\u3002\u6c42\u4ea4\u7684\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<p>1\uff09\u5224\u65ad\u00a0p1p2\u6784\u6210\u7684\u77e9\u5f62\u4e0ep3p4\u00a0\u6784\u6210\u7684\u77e9\u5f62\u662f\u5426\u76f8\u4ea4\uff0c\u5982\u679c\u4e0d\u60f3\u4ea4\uff0c\u4e24\u4e2a\u7ebf\u6bb5\u4e0d\u5b58\u5728\u4ea4\u70b9\u3002\u8fd9\u4e2a\u79f0\u4e3a\u201c\u77ed\u8def\u6d4b\u8bd5\u201d\u3002\u53ef\u4ee5\u6709\u6548\u7684\u8fc7\u6ee4\u4e0d\u5b58\u5728\u4ea4\u70b9\u7684\u60c5\u51b5\u3002<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ first try a fast test to see if the envelopes of the lines intersect\r\nif (!Envelope::intersects(p1,p2,q1,q2))\r\n{\r\n  return NO_INTERSECTION;\r\n}<\/pre>\n<p>2) \u8ba1\u7b97p1,p2\u5728\u00a0\u6709\u5411\u76f4\u7ebf\\( \\vec {p_3p_4}\\)\u00a0\u7684\u65b9\u5411\u4fe1\u606f\uff0c\u5982\u679cp1,p2\u5747\u5728\\( \\vec {p_3p_4}\\)\u00a0 \u7684\u540c\u4e00\u4fa7\uff0c\u90a3\u4e48\u4e24\u7ebf\u6bb5\u4e0d\u76f8\u4ea4\uff0c\u5982\u679c\u5206\u5217\u76f4\u7ebf\u4e24\u4fa7\uff0c\u90a3\u4e48\u7ee7\u7eed\u5224\u65adp3,p4\u4e0e\\( \\vec {p_1p_2}\\)\u7684\u65b9\u4f4d\u4fe1\u606f\uff0c\u5982\u679c\u4e5f\u662f\u5206\u5217\u4e24\u4fa7\uff0c\u90a3\u4e48\u80fd\u591f\u8ba4\u5b9a\u4e24\u4e2a\u7ebf\u6bb5\u5b58\u5728\u4ea4\u70b9\u3002<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ for each endpoint, compute which side of the other segment it lies\r\n\/\/ if both endpoints lie on the same side of the other segment,\r\n\/\/ the segments do not intersect\r\nint Pq1=CGAlgorithms::orientationIndex(p1,p2,p3);\r\nint Pq2=CGAlgorithms::orientationIndex(p1,p2,p4);\r\nif ((Pq1&gt;0 &amp;&amp; Pq2&gt;0) || (Pq1&lt;0 &amp;&amp; Pq2&lt;0)) \r\n{\r\n  return NO_INTERSECTION;\r\n}\r\n\/\/p1,p2 \u4e0e p3p4\u7684\u65b9\u4f4d\u5173\u7cfb\u5224\u65ad\u7565\u3002<\/pre>\n<p>3\uff09\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u505a\u4e00\u4e2a\u5224\u65ad\uff0c\u5373p1p2,p3p4\u662f\u5426\u662f\u5171\u7ebf\u7684\u3002\u5982\u679cp1,p2\u00a0\u90fd\u5728\\( \\vec {p_3p_4}\\)\u00a0\u4e0a\u5e76\u4e14\u00a0p3,p4\u90fd\u5728 \\( \\vec {p_1p_2}\\)\u4e0a\uff0c\u90a3\u4e48\u53ef\u4ee5\u5224\u65ad\u4e24\u7ebf\u6bb5\u5171\u7ebf\u3002\u90a3\u4e48\u7ebf\u6bb5\u6709\u4e24\u4e2a\u4ea4\u70b9\u3002<\/p>\n<pre class=\"lang:c++ decode:true \">bool collinear=Pq1==0 &amp;&amp; Pq2==0 &amp;&amp; Qp1==0 &amp;&amp; Qp2==0;<\/pre>\n<p>4\uff09\u5982\u679c\u4e0d\u5171\u7ebf\uff0c\u90a3\u4e48\u4e24\u76f4\u7ebf\u7684\u5b58\u5728\u4e14\u4ec5\u5b58\u5728\u4e00\u4e2a\u4ea4\u70b9\u3002geos\u00a0\u901a\u8fc7\u00a0<span class=\"lang:c++ decode:true crayon-inline \">intersection(const Coordinate&amp; p1,const Coordinate&amp; p2, const Coordinate&amp; q1, const Coordinate&amp; q2, Coordinate &amp;intPt) const<\/span>\u00a0\u65b9\u6cd5\u5b9e\u73b0\u4e24\u76f4\u7ebf\u6c42\u4ea4\u3002<\/p>\n<p>\u56e0\u4e3a\u5728\u5b9e\u9645\u8fd0\u7b97\u4e2d\uff0cp1,p2,p3,p4\u7684\u503c\u901a\u5e38\u6709\u975e\u5e38\u5927\u7684offset\uff0c\u6bd4\u5982\u9ad8\u65af\u5750\u6807\uff0c\u5728\u8ba1\u7b97\u4e2d\u5f88\u6709\u53ef\u80fd\u9020\u6210\u6ea2\u51fa\u548c\u7cbe\u5ea6\u95ee\u9898\uff0c\u56e0\u6b64\u5728\u8fd0\u7b97\u65f6\u4f1a\u51cf\u6389\u4e00\u4e2a\u4e0e\u4e0a\u8ff0\u70b9\u76f8\u8fd1\u7684\u503c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u201cnormalize\u201d\u3002geos\u7684\u5b9e\u73b0\u662f\u51cf\u6389\u4e0a\u8ff0\u70b9\u7684\u5e73\u5747\u503c\uff1a<\/p>\n<pre class=\"lang:c++ decode:true \">\tdouble intMidX = (intMinX + intMaxX) \/ 2.0;\r\n\tdouble intMidY = (intMinY + intMaxY) \/ 2.0;<\/pre>\n<p>\u8fd9\u6837\u5c31\u4e0d\u592a\u597d\u4e86\u3002\u867d\u7136double\u7684\u503c\u57df\u975e\u5e38\u5927\uff0c\u4f46\u662f\u8fd9\u4ecd\u7136\u4e0d\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7f16\u7a0b\u5b9e\u8df5\u3002<\/p>\n<hr \/>\n<p>\u4e0b\u9762\u7ed9\u51fa\u6211\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u8003\u8651\u7ebf\u6bb5pq\u53ef\u4ee5\u7528\u4ee5\u4e0b\u65b9\u5f0f\u8868\u8fbe\uff1a$$l = p + \\vec {pq}t\u00a0 \u00a0 ;t\\in[0,1];$$<\/p>\n<p>\u7ebf\u6bb5\\( \\vec {p_1p_2} = p_1 + \\vec vt\\)\uff0c\u7ebf\u6bb5\u00a0\\( \\vec {p_3p_4} = p_3 + \\vec\u00a0wr\\),\u4e24\u7ebf\u6bb5\u7684\u4ea4\u70b9\u53ef\u4ee5\u7ec4\u6210\u65b9\u7a0b \\(p_1 + \\vec vt = p_3 + \\vec\u00a0wr\\) \u3002\u5373\u4ea4\u70b9\u6ee1\u8db3\u4e24\u4e2a\u7ebf\u6bb5\u7684\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>\u5982\u679c\u65b9\u7a0b\u6709\u89e3\uff0c\u5e76\u4e14 \\(t \\in [0,1]\u00a0 r\\in[0,1]\\)\u4ee3\u8868\u4e24\u7ebf\u6bb5\u6709\u552f\u4e00\u4ea4\u70b9\u3002<\/p>\n<p>\u65b9\u7a0b\u7684\u77e9\u9635\u5f62\u5f0f\u5982\u4e0b\uff1a$$\\begin{bmatrix}\\vec v &amp; -\\vec w\\end{bmatrix}\\begin{bmatrix}t \\\\ r \\end{bmatrix}=\\begin{bmatrix}\\vec {p_1p_3}\\end{bmatrix}$$<\/p>\n<p>1\uff09\u5982\u679c\u5de6\u4fa7\u77e9\u9635\u5947\u5f02\uff0c\u5373v,w\u5171\u7ebf\uff08\u7ebf\u6027\u76f8\u5173\uff09\uff0c\u90a3\u4e48\u65b9\u7a0b\\(Ax=b\\)\u65e0\u89e3\u6216\u8005\u5b58\u5728\u65e0\u7a77\u591a\u4e2a\u89e3\u3002\u8868\u73b0\u5728\u51e0\u4f55\u4e0a\u4e3a\u4e24\u7ebf\u6bb5\u6240\u5728\u76f4\u7ebf\u65e0\u4ea4\u70b9(2D\u4e0b\u8868\u73b0\u4e3a\u5e73\u884c)\u6216\u8005\u5b58\u5728\u65e0\u6570\u4e2a\u4ea4\u70b9\uff08\u5171\u7ebf\uff09\u3002\u4ece\u7ebf\u6027\u4ee3\u6570\u89d2\u5ea6\u4e0a\u8bb2\uff0c<\/p>\n<p>\u5f53\\(rank(A)=rank(A|b)\\)\u65f6\uff0c\u65b9\u7a0b\u5b58\u5728\u65e0\u7a77\u591a\u89e3\u3002<\/p>\n<p>\u5f53\\(rank(A) &lt; rank(A|b)\\)\u65f6\uff0c\u65b9\u7a0b\u65e0\u89e3\u3002<\/p>\n<p>2\uff09\u5982\u679c\u5de6\u4fa7\u77e9\u9635\u53ef\u9006\uff0c\u90a3\u4e48\u76f4\u63a5\u6c42\u89e3t,r\uff0c\u68c0\u67e5t,r\u662f\u5426\u5728[0,1]\u533a\u95f4\u5185\uff0c\u5e76\u4e14\u4efb\u610f\u5c06t,r\u5e26\u56de\uff0c\u53ef\u4ee5\u6c42\u7ebf\u6bb5\u4ea4\u70b9\u3002<\/p>\n<p>\u4e0b\u5217\u4ee3\u7801\u7ed9\u51fa\u7ebf\u6bb5\\(\\vec{p1p2},\\vec{p3p4}\\)\u4ea4\u70b9\u7684\u8ba1\u7b97\u4ee3\u7801(\u4e0d\u8ba1\u7b97\u4e24\u7ebf\u6bb5\u5171\u7ebf\u7684\u60c5\u51b5)\uff0c\u501f\u52a9\u7ebf\u6027\u4ee3\u6570\u7b97\u6cd5\u5e93<span class=\"lang:c++ decode:true  crayon-inline \">Eigen<\/span>\u00a0\u5b9e\u73b0:<\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;Eigen\/Dense&gt;\r\nbool Intersection::line_segment_intersection(const Coordinate &amp;p1,\r\n                                             const Coordinate &amp;p2,\r\n                                             const Coordinate &amp;p3,\r\n                                             const Coordinate &amp;p4,\r\n                                             Coordinate &amp;p)\r\n{\r\n    auto v = p2 - p1;\r\n    auto w = p4 - p3;\r\n    Eigen::Matrix2d A;\r\n    A&lt;&lt;v.x,-w.x,v.y,-w.y;\r\n\r\n    \/\/A is not inversible\r\n    if(0 == A.determinant())\r\n        return false;\r\n\r\n    Eigen::Vector2d b;\r\n    b &lt;&lt;p3.x-p1.x,p3.y-p1.y;\r\n    Eigen::Vector2d x = A.colPivHouseholderQr().solve(b);\r\n    double t = x(0);\r\n    double r = x(1);\r\n\r\n    \/\/if intersection is not on line segment\r\n    if(t&lt;0||t&gt;1||r&lt;0||r&gt;1)\r\n        return true;\r\n\r\n    p = p1 + v*t; \r\n    return true; \r\n}<\/pre>\n<hr \/>\n<p><strong>\u4e24\u79cd\u65b9\u6cd5\u7684\u5bf9\u6bd4\uff1a<\/strong><\/p>\n<p>1\uff09geos\u65b9\u6cd5\u7684\u4ea4\u70b9\u662f\u5426\u5b58\u5728\u548c\u6c42\u4ea4\u70b9\u7684\u903b\u8f91\u662f\u5206\u5f00\u7684\u3002\u6bcf\u6b21\u8ba1\u7b97\u70b9\u4e0e\u76f4\u7ebf\u7684\u65b9\u4f4d\u5173\u7cfb\u9700\u8981\u4e00\u6b21\u77e2\u91cf\u53c9\u79ef\u8ba1\u7b97\uff0c\u8fd9\u4e00\u6837\u7684\u64cd\u4f5c\u8981\u8fdb\u884c\u56db\u6b21(\u5b58\u5728\u4ea4\u70b9)\u3002<\/p>\n<p>\u6211\u7ed9\u51fa\u7684\u65b9\u6cd5\u53ea\u9700\u8981\u89e3\u4e00\u4e2a\u7ebf\u6027\u65b9\u7a0b\uff0c\u8fd0\u7528crammer\u6cd5\u5219\uff0c\u53ea\u9700\u8981\u89e3\u4e09\u4e2a\\(2\\times2\\)\u884c\u5217\u5f0f\u3002\u5e76\u4e14\u76f4\u63a5\u53ef\u901a\u8fc7\u89e3\u7684\u60c5\u51b5\u5224\u65ad\u00a0\u4e0d\u60f3\u4ea4\/\u5171\u7ebf\/\u6709\u4ea4\u70b9\u3002<\/p>\n<p>2\uff09\u6211\u7ed9\u51fa\u7684\u65b9\u6cd5\u540c\u6837\u9002\u5408\u9ad8\u7ef4\u7ebf\u6bb5\u7684\u4ea4\u70b9\u8ba1\u7b97\u3002geos\u56e0\u4e3a\u8981\u5224\u65ad\u70b9\u5728\u76f4\u7ebf\u7684\u54ea\u4e00\u4fa7\uff0c\u4f1a\u53d7\u9650\u4e8e\u4e8c\u7ef4\u8fd0\u7b97\u3002<\/p>\n<p>3\uff09\u6839\u636e\u65b9\u5411\u5411\u91cf\u7cfb\u6570\u503c\u57df\u7684\u4e0d\u540c\uff0c\u6211\u7ed9\u51fa\u7684\u65b9\u6cd5\u53ef\u4ee5\u8868\u793a \u7ebf\u6bb5\\(t\\in[0,1]\\) \u5c04\u7ebf\\(t\\in[0,\\infty]\\),\u76f4\u7ebf\\(t\\in[-\\infty,+\\infty]\\)\u7684\u662f\u5426\u76f8\u4ea4\u4e0e\u4ea4\u70b9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e24\u7ebf\u6bb5\uff08\u76f4\u7ebf\/\u5c04\u7ebf\uff09\u6c42\u4ea4\u70b9\u662f\u51e0\u4f55\u8ba1\u7b97\u4e2d\u7684\u4e00\u4e2a\u6700\u57fa\u672c\u7684\u7b97\u6cd5\u3002\u867d\u7136\u5b83\u5927\u90e8\u5206\u60c5\u51b5\u90fd\u4e0d\u662f &hellip; <a href=\"http:\/\/www.whudj.cn\/?p=283\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"_links":{"self":[{"href":"http:\/\/www.whudj.cn\/index.php?rest_route=\/wp\/v2\/posts\/283"}],"collection":[{"href":"http:\/\/www.whudj.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.whudj.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.whudj.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.whudj.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=283"}],"version-history":[{"count":16,"href":"http:\/\/www.whudj.cn\/index.php?rest_route=\/wp\/v2\/posts\/283\/revisions"}],"predecessor-version":[{"id":1218,"href":"http:\/\/www.whudj.cn\/index.php?rest_route=\/wp\/v2\/posts\/283\/revisions\/1218"}],"wp:attachment":[{"href":"http:\/\/www.whudj.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.whudj.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.whudj.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}