{"id":10976,"date":"2019-10-25T09:00:11","date_gmt":"2019-10-25T13:00:11","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=10976"},"modified":"2020-03-23T09:21:46","modified_gmt":"2020-03-23T13:21:46","slug":"minimal-bounding-box","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2019\/10\/25\/minimal-bounding-box\/","title":{"rendered":"Minimal Bounding Box"},"content":{"rendered":"<div xmlns:mwsh=\"http:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n   <introduction><\/p>\n<p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>&#8216;s pick this week is <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/18264\">Minimal Bounding Box<\/a> by <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/232523\">Johannes Korsawe<\/a>.\n      <\/p>\n<p>   <\/introduction><\/p>\n<p>I was recently asked by a customer &#8220;How can I replicate the flatness measurements from a CMM machine?&#8221;  A <a href=\"https:\/\/en.wikipedia.org\/wiki\/Coordinate-measuring_machine\">coordinate measuring machine<\/a> measures geometric properties of an object.\n   <\/p>\n<p>A quick internet search, took me to <a href=\"https:\/\/insights.faro.com\/buildit-software\/how-to-evaluate-flatness-in-gd-t\">this site<\/a> which introduced two methods.  The customer wanted the &#8220;Minimum Zone&#8221; method, sometimes also referred to as parallel planes.  This essentially boils down to &#8220;fit a minimum bounding box and calculate the distance between the top and bottom of the box&#8221;.\n   <\/p>\n<p>I knew it must be possible to to formulate this as an optimization routine whether something linear that <tt>linprog<\/tt> could handle or nonlinear with linear constraints enforcing for <tt>fmincon<\/tt>.  A quick search of the File Exchange yielded Johannes&#8217; file which does exactly what was needed. It also provides a plotting routine to help validate and understand the output.\n   <\/p>\n<p>Here&#8217;s an example of what worked, using random numbers in place of the CMM measurements.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">npts = 10; <span style=\"color: #228B22\">% Number of points from CMM machine.<\/span>\r\nx = randi(100, npts, 1)+randn(npts, 1);\r\ny = randi(100, npts, 1)+randn(npts, 1);\r\nz = rand(npts, 1);\r\n\r\n[~, cornerpts] = minboundbox(x, y, z);\r\n\r\nscatter3(x, y, z);\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplotminbox(cornerpts, <span style=\"color: #A020F0\">'r'<\/span>);<\/pre>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainMinBoundingBox\/mainMinBoundingBox_01.png\"> <\/p>\n<p>To calculate flatness, I just need to calculate the distance between a top and bottom point.  I wasn&#8217;t sure if the point order was deterministic so it&#8217;s easier to just calculate the closest point on the opposite plane from a chosen one.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bottompt = cornerpts(1,:);\r\n[~, topidx] = min(hypot(cornerpts(2:end,1)-bottompt(1), cornerpts(2:end,2)-bottompt(2)));\r\ntoppt = cornerpts(topidx+1,:);\r\n\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nscatter3(bottompt(1), bottompt(2), bottompt(3), <span style=\"color: #A020F0\">'b*'<\/span>)\r\nscatter3(toppt(1), toppt(2), toppt(3), <span style=\"color: #A020F0\">'g*'<\/span>)<\/pre>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainMinBoundingBox\/mainMinBoundingBox_02.png\"> <\/p>\n<p>Computing the distance can be done with a couple calls to <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/hypot.html\"><tt>hypot<\/tt><\/a>.\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">planesep = hypot(hypot(toppt(1)-bottompt(1), toppt(2)-bottompt(2)), toppt(3)-bottompt(3));\r\ndisp(<span style=\"color: #A020F0\">\"Plane separation is: \"<\/span> + planesep)<\/pre>\n<pre style=\"font-style:oblique\">Plane separation is: 0.55053\r\n<\/pre>\n<h3>Comments<a name=\"4\"><\/a><\/h3>\n<p>Give it a try and let us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=10976#respond\">here<\/a> or leave a <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/18264#comments\">comment<\/a> for Johannes.\n   <\/p>\n<p><script language=\"JavaScript\">\n<!--\n\n    function grabCode_1696e3d50844498fa68023ada2ce54e9() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='1696e3d50844498fa68023ada2ce54e9 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1696e3d50844498fa68023ada2ce54e9';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Sean de Wolski';\n        copyright = 'Copyright 2019 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<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\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }   \n      \n-->\n<\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><a href=\"javascript:grabCode_1696e3d50844498fa68023ada2ce54e9()\"><span style=\"font-size: x-small;        font-style: italic;\">Get<br \/>\n            the MATLAB code<br \/>\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>      Published with MATLAB&reg; R2019b<\/p>\n<\/div>\n<p><!--\n1696e3d50844498fa68023ada2ce54e9 ##### SOURCE BEGIN #####\n%% Minimal Bounding Box\n%\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/18264 Minimal Bounding Box> by\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/232523 Johannes Korsawe>.\n% \n\n%% \n%\n% I was recently asked by a customer \"How can I replicate the flatness\n% measurements from a CMM machine?\"  A\n% <https:\/\/en.wikipedia.org\/wiki\/Coordinate-measuring_machine coordinate\n% measuring machine> measures geometric properties of an object.\n%\n% A quick internet search, took me to\n% <https:\/\/insights.faro.com\/buildit-software\/how-to-evaluate-flatness-in-gd-t\n% this site> which introduced two methods.  The customer wanted the\n% \"Minimum Zone\" method, sometimes also referred to as parallel planes.\n% This essentially boils down to \"fit a minimum bounding box and calculate\n% the distance between the top and bottom of the box\".\n%\n% I knew it must be possible to to formulate this as an optimization\n% routine whether something linear that |linprog| could handle or nonlinear\n% with linear constraints enforcing for |fmincon|.  A quick search of the\n% File Exchange yielded Johannes' file which does exactly what was needed.\n% It also provides a plotting routine to help validate and understand the\n% output.\n%\n% Here's an example of what worked, using random numbers in place of the\n% CMM measurements.\n\nnpts = 10; % Number of points from CMM machine.\nx = randi(100, npts, 1)+randn(npts, 1);\ny = randi(100, npts, 1)+randn(npts, 1);\nz = rand(npts, 1);\n\n[~, cornerpts] = minboundbox(x, y, z);\n\nscatter3(x, y, z);\nhold on\nplotminbox(cornerpts, 'r');\n\n%% \n% To calculate flatness, I just need to calculate the distance between a\n% top and bottom point.  I wasn't sure if the point order was deterministic\n% so it's easier to just calculate the closest point on the opposite plane.\n\nbottompt = cornerpts(1,:);\n[~, topidx] = min(hypot(cornerpts(2:end,1)-bottompt(1), cornerpts(2:end,2)-bottompt(2)));\ntoppt = cornerpts(topidx+1,:);\n\nhold on\nscatter3(bottompt(1), bottompt(2), bottompt(3), 'b*')\nscatter3(toppt(1), toppt(2), toppt(3), 'g*')\n\n%%\n% Computing the distance can be done with a couple calls to <https:\/\/www.mathworks.com\/help\/matlab\/ref\/hypot.html |hypot|>.\nplanesep = hypot(hypot(toppt(1)-bottompt(1), toppt(2)-bottompt(2)), toppt(3)-bottompt(3));\ndisp(\"Plane separation is: \" + planesep)\n\n%% Comments\n% \n% Give it a try and let us know what you think\n% <http:\/\/blogs.mathworks.com\/pick\/?p=10976#respond here> or leave a\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/18264#comments\n% comment> for Johannes.\n%\n \n\n##### SOURCE END ##### 1696e3d50844498fa68023ada2ce54e9\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/pick\/files\/FaceTracking-1.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div>\n<p>Sean&#8216;s pick this week is Minimal Bounding Box by Johannes Korsawe.<\/p>\n<p>I was recently asked by a customer &#8220;How can I replicate the flatness measurements from a CMM&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2019\/10\/25\/minimal-bounding-box\/\">read more >><\/a><\/p>\n","protected":false},"author":87,"featured_media":11381,"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\/10976"}],"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=10976"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10976\/revisions"}],"predecessor-version":[{"id":11379,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10976\/revisions\/11379"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media\/11381"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=10976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=10976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=10976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}