{"id":4797,"date":"2013-09-06T09:00:20","date_gmt":"2013-09-06T13:00:20","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=4797"},"modified":"2013-12-05T13:58:38","modified_gmt":"2013-12-05T18:58:38","slug":"inpolyhedron","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2013\/09\/06\/inpolyhedron\/","title":{"rendered":"INPOLYHEDRON"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/37856-inpolyhedron-are-points-inside-a-triangulated-volume-\">inpolyhedron<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/43596\">Sven<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Polygons, Polyhedra and Convexity<a name=\"1\"><\/a><\/h3>\r\n   <p>It's often useful to know if a point is inside of a polygon (two dimensions) and sometimes if a point is inside of a polyhedron\r\n      (three dimensions).  For two dimensions, MATLAB ships with <a href=\"\">inpolygon<\/a>, a nice function to handle this.  However, this same operation in three dimensions becomes more complicated.  It is simplified\r\n      if the object is convex.  In this case, the vertices can be represented as a set of constraints and we can apply these constraints\r\n      to the points to test whether they are inside or not. <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/33036\">Matt J<\/a> does this in his <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/30892-analyze-n-dimensional-polyhedra-in-terms-of-vertices-or--in-equalities\">vert2lcon<\/a> function (The runner-up, who will also get some MathWorks swag, for this week's post!).\r\n   <\/p>\r\n   <p>But what about when the polydehron is not necessarily convex?  Now we no longer have this luxury and have to resort to more\r\n      complex algorithms.\r\n   <\/p>\r\n   <p>Fortunately, Sven has done the work for us!<\/p>\r\n   <p>Let's use <a title=\"https:\/\/blogs.mathworks.com\/community\/2013\/06\/20\/paul-prints-the-l-shaped-membrane\/ (link no longer works)\">Paul's three dimensional L-Shaped Membrane<\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Load shellVertices, shellfaces from Paul's Blog:<\/span>\r\nload <span style=\"color: #A020F0\">PaulsMembrane.mat<\/span>\r\n\r\n<span style=\"color: #228B22\">% Draw the membrane:<\/span>\r\npatch(<span style=\"color: #A020F0\">'Vertices'<\/span>,shellVertices,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'Faces'<\/span>,shellFaces,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'FaceColor'<\/span>,<span style=\"color: #A020F0\">'r'<\/span>);\r\naxis <span style=\"color: #A020F0\">tight<\/span>\r\nview(-51,24)<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/guest\/main\/membrane.png\"> <\/p>\r\n   <p>Add 10000 random points to it<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">pts = rand([10000,3]); <span style=\"color: #228B22\">%Generate a random 10000pts<\/span>\r\nhold <span style=\"color: #A020F0\">on<\/span>;\r\nplot3(pts(:,1),pts(:,2),pts(:,3),<span style=\"color: #A020F0\">'b*'<\/span>); <span style=\"color: #228B22\">%add points to plot<\/span><\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/guest\/main\/membranepts.png\"> <\/p>\r\n   <p>Find the points in the polyhedron<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">in = inpolyhedron(shellFaces,shellVertices,pts,<span style=\"color: #A020F0\">'FlipNormals'<\/span>,true);\r\ndelete(hLine); <span style=\"color: #228B22\">%clean up<\/span>\r\nplot3(pts(in,1),pts(in,2),pts(in,3),<span style=\"color: #A020F0\">'b*'<\/span>); <span style=\"color: #228B22\">%add points to plot<\/span>\r\nset(hPatch,<span style=\"color: #A020F0\">'FaceAlpha'<\/span>,0.3,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'EdgeColor'<\/span>,<span style=\"color: #A020F0\">'none'<\/span>); <span style=\"color: #228B22\">%make the points inside visible<\/span><\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/guest\/main\/final.png\"> <\/p>\r\n   <p>Woohoo!<\/p>\r\n   <h3>Comments<a name=\"6\"><\/a><\/h3>\r\n   <p>In addition to everything you saw here, there is also excellent help and many options built into inpolyhedron.  Give it a\r\n      try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=4797#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/37856-inpolyhedron-are-points-inside-a-triangulated-volume-#comments\">comment<\/a> for Sven.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_8e4056a62840488f91bd5de7c0751711() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='8e4056a62840488f91bd5de7c0751711 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 8e4056a62840488f91bd5de7c0751711';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Sean de Wolski';\r\n        copyright = 'Copyright 2013 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_8e4056a62840488f91bd5de7c0751711()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2013a<br><\/p>\r\n<\/div>\r\n<!--\r\n8e4056a62840488f91bd5de7c0751711 ##### SOURCE BEGIN #####\r\n%% INPOLYHEDRON\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495 Sean>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/37856-inpolyhedron-are-points-inside-a-triangulated-volume-\r\n% inpolyhedron> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/43596 Sven>.\r\n% \r\n\r\n%% Polygons, Polyhedra and Convexity\r\n%\r\n% It's often useful to know if a point is inside of a polygon (two\r\n% dimensions) and sometimes if a point is inside of a polyhedron (three\r\n% dimensions).  For two dimensions, MATLAB ships with\r\n% <\r\n% inpolygon>, a nice function to handle this.  However, this same operation\r\n% in three dimensions becomes more complicated.  It is simplified if the\r\n% object is convex.  In this case, the vertices can be represented as a set\r\n% of constraints and we can apply these constraints to the points to test\r\n% whether they are inside or not.\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/33036 Matt\r\n% J> does this in his\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/30892-analyze-n-dimensional-polyhedra-in-terms-of-vertices-or--in-equalities\r\n% vert2lcon> function (The runner-up, who will also get some MathWorks\r\n% swag, for this week's post!).\r\n% \r\n% But what about when the polydehron is not necessarily convex?  Now we\r\n% no longer have this luxury and have to resort to more complex algorithms.\r\n%\r\n% Fortunately, Sven has done the work for us!\r\n%\r\n% Let's use\r\n% <https:\/\/blogs.mathworks.com\/community\/2013\/06\/20\/paul-prints-the-l-shaped-membrane\/ Paul's three dimensional L-Shaped Membrane>.\r\n%\r\n\r\n% Load shellVertices, shellfaces from Paul's Blog:\r\nload PaulsMembrane.mat\r\n\r\n% Draw the membrane:\r\npatch('Vertices',shellVertices,...\r\n    'Faces',shellFaces,...\r\n    'FaceColor','r');\r\naxis tight\r\nview(-51,24)\r\n\r\n%% \r\n% <<membrane.png>>\r\n%\r\n% Add 10000 random points to it\r\n%  \r\n\r\npts = rand([10000,3]); %Generate a random 10000pts\r\nhold on;\r\nplot3(pts(:,1),pts(:,2),pts(:,3),'b*'); %add points to plot\r\n\r\n%%  \r\n% <<membranepts.png>>\r\n%\r\n% Find the points in the polyhedron\r\n\r\n%%\r\nin = inpolyhedron(shellFaces,shellVertices,pts,'FlipNormals',true);\r\ndelete(hLine); %clean up\r\nplot3(pts(in,1),pts(in,2),pts(in,3),'b*'); %add points to plot\r\nset(hPatch,'FaceAlpha',0.3,...\r\n    'EdgeColor','none'); %make the points inside visible\r\n\r\n%%\r\n% <<final.png>>\r\n%\r\n% Woohoo!\r\n \r\n \r\n\r\n%% Comments\r\n% \r\n% In addition to everything you saw here, there is also excellent help and\r\n% many options built into inpolyhedron.  Give it a try and let us know what\r\n% you think <https:\/\/blogs.mathworks.com\/pick\/?p=4797#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/37856-inpolyhedron-are-points-inside-a-triangulated-volume-#comments comment> for Sven.\r\n% \r\n##### SOURCE END ##### 8e4056a62840488f91bd5de7c0751711\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/guest\/main\/membrane.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is inpolyhedron by Sven.\r\n      \r\n   \r\n   Polygons, Polyhedra and Convexity\r\n   It's often useful to know if a point is inside of a polygon (two dimensions)... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2013\/09\/06\/inpolyhedron\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4797"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=4797"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4797\/revisions"}],"predecessor-version":[{"id":4948,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4797\/revisions\/4948"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=4797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=4797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=4797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}