├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── bunny.xyz ├── downsample.cpp └── sample.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | *.smod 19 | 20 | # Compiled Static libraries 21 | *.lai 22 | *.la 23 | *.a 24 | *.lib 25 | 26 | # Executables 27 | *.exe 28 | *.out 29 | *.app 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Kutay Macit 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ downsample.cpp -o downsample 3 | 4 | 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # downsampling-point-cloud 2 | 3 | 4 | ## Usage 5 | 6 | ``` 7 | make; downsample 8 | ``` 9 | After running the program enter the leaf sizes for each axis. 10 | 11 | 12 | ## Sample Input-Output 13 | ![ScreenShot](https://raw.githubusercontent.com/ucanbizon/downsampling-point-cloud/master/sample.png) 14 | 15 | Tested with Ubuntu 16.04 LTS. 16 | -------------------------------------------------------------------------------- /bunny.xyz: -------------------------------------------------------------------------------- 1 | 0.0054216 0.11349 0.040749 2 | -0.0017447 0.11425 0.041273 3 | -0.010661 0.11338 0.040916 4 | 0.026422 0.11499 0.032623 5 | 0.024545 0.12284 0.024255 6 | 0.034137 0.11316 0.02507 7 | 0.02886 0.11773 0.027037 8 | 0.02675 0.12234 0.017605 9 | 0.03575 0.1123 0.019109 10 | 0.015982 0.12307 0.031279 11 | 0.0079813 0.12438 0.032798 12 | 0.018101 0.11674 0.035493 13 | 0.0086687 0.11758 0.037538 14 | 0.01808 0.12536 0.026132 15 | 0.0080861 0.12866 0.02619 16 | 0.02275 0.12146 0.029671 17 | -0.0018689 0.12456 0.033184 18 | -0.011168 0.12376 0.032519 19 | -0.0020063 0.11937 0.038104 20 | -0.01232 0.11816 0.037427 21 | -0.0016659 0.12879 0.026782 22 | -0.011971 0.12723 0.026219 23 | 0.016484 0.12828 0.01928 24 | 0.0070921 0.13103 0.018415 25 | 0.0014615 0.13134 0.017095 26 | -0.013821 0.12886 0.019265 27 | -0.01725 0.11202 0.040077 28 | -0.074556 0.13415 0.051046 29 | -0.065971 0.14396 0.04109 30 | -0.071925 0.14545 0.043266 31 | -0.06551 0.13624 0.042195 32 | -0.071112 0.13767 0.047518 33 | -0.079528 0.13416 0.051194 34 | -0.080421 0.14428 0.042793 35 | -0.082672 0.1378 0.046806 36 | -0.08813 0.13514 0.042222 37 | -0.066325 0.12347 0.050729 38 | -0.072399 0.12662 0.052364 39 | -0.066091 0.11973 0.050881 40 | -0.072012 0.11811 0.052295 41 | -0.062433 0.12627 0.043831 42 | -0.068326 0.12998 0.048875 43 | -0.063094 0.11811 0.044399 44 | -0.071301 0.11322 0.04841 45 | -0.080515 0.12741 0.052034 46 | -0.078179 0.1191 0.051116 47 | -0.085216 0.12609 0.049001 48 | -0.089538 0.12621 0.044589 49 | -0.082659 0.11661 0.04797 50 | -0.089536 0.11784 0.04457 51 | -0.0565 0.15248 0.030132 52 | -0.055517 0.15313 0.026915 53 | -0.03625 0.17198 0.00017688 54 | -0.03775 0.17198 0.00022189 55 | -0.03625 0.16935 0.00051958 56 | -0.033176 0.15711 0.0018682 57 | -0.051913 0.1545 0.011273 58 | -0.041707 0.16642 0.0030522 59 | -0.049468 0.16414 0.0041988 60 | -0.041892 0.15669 0.0054879 61 | -0.051224 0.15878 0.0080283 62 | -0.062417 0.15317 0.033161 63 | -0.07167 0.15319 0.033701 64 | -0.062543 0.15524 0.027405 65 | -0.07211 0.1555 0.027645 66 | -0.078663 0.15269 0.032268 67 | -0.081569 0.15374 0.026085 68 | -0.08725 0.1523 0.022135 69 | -0.05725 0.15568 0.010325 70 | -0.057888 0.1575 0.0073225 71 | -0.0885 0.15223 0.019215 72 | -0.056129 0.14616 0.03085 73 | -0.054705 0.13555 0.032127 74 | -0.054144 0.14714 0.026275 75 | -0.046625 0.13234 0.021909 76 | -0.05139 0.13694 0.025787 77 | -0.018278 0.12238 0.030773 78 | -0.021656 0.11643 0.035209 79 | -0.031921 0.11566 0.032851 80 | -0.021348 0.12421 0.024562 81 | -0.03241 0.12349 0.023293 82 | -0.024869 0.12094 0.028745 83 | -0.031747 0.12039 0.028229 84 | -0.052912 0.12686 0.034968 85 | -0.041672 0.11564 0.032998 86 | -0.052037 0.1168 0.034582 87 | -0.042495 0.12488 0.024082 88 | -0.047946 0.12736 0.028108 89 | -0.042421 0.12035 0.028633 90 | -0.047661 0.12024 0.028871 91 | -0.035964 0.1513 0.0005395 92 | -0.050598 0.1474 0.013881 93 | -0.046375 0.13293 0.018289 94 | -0.049125 0.13856 0.016269 95 | -0.042976 0.14915 0.0054003 96 | -0.047965 0.14659 0.0086783 97 | -0.022926 0.1263 0.018077 98 | -0.031583 0.1259 0.017804 99 | -0.041733 0.12796 0.01665 100 | -0.061482 0.14698 0.036168 101 | -0.071729 0.15026 0.038328 102 | -0.060526 0.1368 0.035999 103 | -0.082619 0.14823 0.035955 104 | -0.087824 0.14449 0.033779 105 | -0.089 0.13828 0.037774 106 | -0.085662 0.15095 0.028208 107 | -0.089601 0.14725 0.025869 108 | -0.090681 0.13748 0.02369 109 | -0.058722 0.12924 0.038992 110 | -0.060075 0.11512 0.037685 111 | -0.091812 0.12767 0.038703 112 | -0.091727 0.11657 0.039619 113 | -0.093164 0.12721 0.025211 114 | -0.093938 0.12067 0.024399 115 | -0.091583 0.14522 0.01986 116 | -0.090929 0.13667 0.019817 117 | -0.093094 0.11635 0.018959 118 | 0.024948 0.10286 0.041418 119 | 0.0336 0.092627 0.040463 120 | 0.02742 0.096386 0.043312 121 | 0.03392 0.086911 0.041034 122 | 0.028156 0.086837 0.045084 123 | 0.03381 0.078604 0.040854 124 | 0.028125 0.076874 0.045059 125 | 0.0145 0.093279 0.05088 126 | 0.0074817 0.09473 0.052315 127 | 0.017407 0.10535 0.043139 128 | 0.0079536 0.10633 0.042968 129 | 0.018511 0.097194 0.047253 130 | 0.0086436 0.099323 0.048079 131 | -0.0020197 0.095698 0.053906 132 | -0.011446 0.095169 0.053862 133 | -0.001875 0.10691 0.043455 134 | -0.011875 0.10688 0.043019 135 | -0.0017622 0.10071 0.046648 136 | -0.012498 0.10008 0.045916 137 | 0.016381 0.085894 0.051642 138 | 0.0081167 0.08691 0.055228 139 | 0.017644 0.076955 0.052372 140 | 0.008125 0.076853 0.055536 141 | 0.020575 0.088169 0.049006 142 | 0.022445 0.075721 0.049563 143 | -0.0017931 0.086849 0.056843 144 | -0.011943 0.086771 0.057009 145 | -0.0019567 0.076863 0.057803 146 | -0.011875 0.076964 0.057022 147 | 0.03325 0.067541 0.040033 148 | 0.028149 0.066829 0.042953 149 | 0.026761 0.057829 0.042588 150 | 0.023571 0.04746 0.040428 151 | 0.015832 0.067418 0.051639 152 | 0.0080431 0.066902 0.055006 153 | 0.013984 0.058886 0.050416 154 | 0.0080973 0.056888 0.05295 155 | 0.020566 0.065958 0.0483 156 | 0.018594 0.056539 0.047879 157 | 0.012875 0.052652 0.049689 158 | -0.0017852 0.066712 0.056503 159 | -0.011785 0.066885 0.055015 160 | -0.001875 0.056597 0.05441 161 | -0.01184 0.057054 0.052714 162 | -0.015688 0.052469 0.049615 163 | 0.0066154 0.04993 0.051259 164 | 0.018088 0.046655 0.043321 165 | 0.008841 0.045437 0.046623 166 | 0.017688 0.039719 0.043084 167 | 0.008125 0.039516 0.045374 168 | -0.0016111 0.049844 0.05172 169 | -0.01245 0.046773 0.050903 170 | -0.013851 0.039778 0.051036 171 | -0.0020294 0.044874 0.047587 172 | -0.011653 0.04686 0.048661 173 | -0.0018611 0.039606 0.047339 174 | -0.0091545 0.03958 0.049415 175 | 0.043661 0.094028 0.02252 176 | 0.034642 0.10473 0.031831 177 | 0.028343 0.1072 0.036339 178 | 0.036339 0.096552 0.034843 179 | 0.031733 0.099372 0.038505 180 | 0.036998 0.10668 0.026781 181 | 0.032875 0.11108 0.02959 182 | 0.040938 0.097132 0.026663 183 | 0.044153 0.086466 0.024241 184 | 0.05375 0.072221 0.020429 185 | 0.04516 0.076574 0.023594 186 | 0.038036 0.086663 0.035459 187 | 0.037861 0.076625 0.035658 188 | 0.042216 0.087237 0.028254 189 | 0.042355 0.076747 0.02858 190 | 0.043875 0.096228 0.015269 191 | 0.044375 0.096797 0.0086445 192 | 0.039545 0.1061 0.017655 193 | 0.042313 0.10009 0.017237 194 | 0.045406 0.087417 0.015604 195 | 0.055118 0.072639 0.017944 196 | 0.048722 0.07376 0.017434 197 | 0.045917 0.086298 0.0094211 198 | 0.019433 0.1096 0.039063 199 | 0.01097 0.11058 0.039648 200 | 0.046657 0.057153 0.031337 201 | 0.056079 0.066335 0.024122 202 | 0.048168 0.06701 0.026298 203 | 0.056055 0.057253 0.024902 204 | 0.051163 0.056662 0.029137 205 | 0.036914 0.067032 0.036122 206 | 0.033 0.06472 0.039903 207 | 0.038004 0.056507 0.033119 208 | 0.030629 0.054915 0.038484 209 | 0.041875 0.066383 0.028357 210 | 0.041434 0.06088 0.029632 211 | 0.044921 0.049904 0.031243 212 | 0.054635 0.050167 0.022044 213 | 0.04828 0.04737 0.025845 214 | 0.037973 0.048347 0.031456 215 | 0.028053 0.047061 0.035991 216 | 0.025595 0.040346 0.03415 217 | 0.038455 0.043509 0.028278 218 | 0.032031 0.043278 0.029253 219 | 0.036581 0.040335 0.025144 220 | 0.03019 0.039321 0.026847 221 | 0.059333 0.067891 0.017361 222 | 0.0465 0.071452 0.01971 223 | 0.059562 0.057747 0.01834 224 | 0.055636 0.049199 0.019173 225 | 0.0505 0.045064 0.019181 226 | 0.023 0.047803 0.039776 227 | 0.022389 0.03886 0.038795 228 | -0.019545 0.0939 0.052205 229 | -0.021462 0.10618 0.042059 230 | -0.031027 0.10395 0.041228 231 | -0.022521 0.097723 0.045194 232 | -0.031858 0.097026 0.043878 233 | -0.043262 0.10412 0.040891 234 | -0.052154 0.10404 0.040972 235 | -0.041875 0.096944 0.042424 236 | -0.051919 0.096967 0.043563 237 | -0.021489 0.086672 0.054767 238 | -0.027 0.083087 0.050284 239 | -0.02107 0.077249 0.054365 240 | -0.026011 0.089634 0.048981 241 | -0.031893 0.087035 0.044169 242 | -0.025625 0.074892 0.047102 243 | -0.03197 0.0769 0.042177 244 | -0.041824 0.086954 0.043295 245 | -0.051825 0.086844 0.044933 246 | -0.041918 0.076728 0.042564 247 | -0.051849 0.076877 0.042992 248 | -0.061339 0.10393 0.041164 249 | -0.072672 0.10976 0.044294 250 | -0.061784 0.096825 0.043327 251 | -0.070058 0.096203 0.041397 252 | -0.080439 0.11091 0.044343 253 | -0.061927 0.086724 0.04452 254 | -0.070344 0.087352 0.041908 255 | -0.06141 0.077489 0.042178 256 | -0.068579 0.080144 0.041024 257 | -0.019045 0.067732 0.052388 258 | -0.017742 0.058909 0.050809 259 | -0.023548 0.066382 0.045226 260 | -0.03399 0.067795 0.040929 261 | -0.02169 0.056549 0.045164 262 | -0.036111 0.060706 0.040407 263 | -0.041231 0.066951 0.041392 264 | -0.048588 0.070956 0.040357 265 | -0.0403 0.059465 0.040446 266 | -0.02192 0.044965 0.052258 267 | -0.029187 0.043585 0.051088 268 | -0.021919 0.039826 0.053521 269 | -0.030331 0.039749 0.052133 270 | -0.021998 0.049847 0.046725 271 | -0.031911 0.046848 0.045187 272 | -0.035276 0.039753 0.047529 273 | -0.042016 0.044823 0.041594 274 | -0.05194 0.044707 0.043498 275 | -0.041928 0.039327 0.043582 276 | -0.051857 0.039252 0.046212 277 | -0.059453 0.04424 0.042862 278 | -0.060765 0.039087 0.044363 279 | -0.024273 0.11038 0.039129 280 | -0.032379 0.10878 0.037952 281 | -0.041152 0.10853 0.037969 282 | -0.051698 0.10906 0.038258 283 | -0.062091 0.10877 0.038274 284 | -0.071655 0.10596 0.037516 285 | -0.074634 0.097746 0.038347 286 | -0.07912 0.10508 0.032308 287 | -0.080203 0.096758 0.033592 288 | -0.08378 0.10568 0.025985 289 | -0.087292 0.10314 0.020825 290 | -0.08521 0.097079 0.02781 291 | -0.088082 0.096456 0.022985 292 | -0.07516 0.08604 0.038816 293 | -0.064577 0.073455 0.03897 294 | -0.072279 0.076416 0.036413 295 | -0.076375 0.072563 0.02873 296 | -0.080031 0.087076 0.03429 297 | -0.078919 0.079371 0.032477 298 | -0.084834 0.086686 0.026974 299 | -0.087891 0.089233 0.022611 300 | -0.081048 0.077169 0.025829 301 | -0.086393 0.10784 0.018635 302 | -0.087672 0.10492 0.017264 303 | -0.089333 0.098483 0.01761 304 | -0.086375 0.083067 0.018607 305 | -0.089179 0.089186 0.018947 306 | -0.082879 0.076109 0.017794 307 | -0.0825 0.074674 0.0071175 308 | -0.026437 0.064141 0.039321 309 | -0.030035 0.06613 0.038942 310 | -0.026131 0.056531 0.038882 311 | -0.031664 0.056657 0.037742 312 | -0.045716 0.064541 0.039166 313 | -0.051959 0.066869 0.036733 314 | -0.042557 0.055545 0.039026 315 | -0.049406 0.056892 0.034344 316 | -0.0555 0.062391 0.029498 317 | -0.05375 0.058574 0.026313 318 | -0.03406 0.050137 0.038577 319 | -0.041741 0.04959 0.03929 320 | -0.050975 0.049435 0.036965 321 | -0.053 0.051065 0.029209 322 | -0.054145 0.054568 0.012257 323 | -0.055848 0.05417 0.0083272 324 | -0.054844 0.049295 0.011462 325 | -0.05615 0.050619 0.0092929 326 | -0.061451 0.068257 0.035376 327 | -0.069725 0.069958 0.032788 328 | -0.062823 0.063322 0.026886 329 | -0.071037 0.066787 0.025228 330 | -0.060857 0.060568 0.022643 331 | -0.067 0.061558 0.020109 332 | -0.0782 0.071279 0.021032 333 | -0.062116 0.045145 0.037802 334 | -0.065473 0.039513 0.037964 335 | -0.06725 0.03742 0.033413 336 | -0.072702 0.065008 0.018701 337 | -0.06145 0.059165 0.018731 338 | -0.0675 0.061479 0.019221 339 | -0.057411 0.054114 0.0038257 340 | -0.079222 0.070654 0.017735 341 | -0.062473 0.04468 0.01111 342 | -0.06725 0.042258 0.010414 343 | -0.066389 0.040515 0.01316 344 | -0.068359 0.038502 0.011958 345 | -0.061381 0.04748 0.007607 346 | -0.068559 0.043549 0.0081576 347 | -0.070929 0.03983 0.0085888 348 | -0.016625 0.18375 -0.019735 349 | -0.015198 0.17471 -0.018868 350 | -0.015944 0.16264 -0.0091037 351 | -0.015977 0.1607 -0.0088072 352 | -0.013251 0.16708 -0.015264 353 | -0.014292 0.16098 -0.011252 354 | -0.013986 0.184 -0.023739 355 | -0.011633 0.17699 -0.023349 356 | -0.0091029 0.16988 -0.021457 357 | -0.025562 0.18273 -0.0096247 358 | -0.02725 0.18254 -0.0094384 359 | -0.025736 0.17948 -0.0089653 360 | -0.031216 0.17589 -0.0051154 361 | -0.020399 0.1845 -0.014943 362 | -0.021339 0.17645 -0.014566 363 | -0.027125 0.17234 -0.010156 364 | -0.03939 0.1733 -0.0023575 365 | -0.022876 0.16406 -0.0078103 366 | -0.031597 0.16651 -0.0049292 367 | -0.0226 0.15912 -0.003799 368 | -0.030372 0.15767 -0.0012672 369 | -0.021158 0.16849 -0.012383 370 | -0.027 0.1712 -0.01022 371 | -0.041719 0.16813 -0.00074958 372 | -0.04825 0.16748 -0.00015191 373 | -0.03725 0.16147 -7.2628e-05 374 | -0.066429 0.15783 -0.0085673 375 | -0.071284 0.15839 -0.005998 376 | -0.065979 0.16288 -0.017792 377 | -0.071623 0.16384 -0.01576 378 | -0.066068 0.16051 -0.013567 379 | -0.073307 0.16049 -0.011832 380 | -0.077 0.16204 -0.019241 381 | -0.077179 0.15851 -0.01495 382 | -0.073691 0.17286 -0.037944 383 | -0.07755 0.17221 -0.039175 384 | -0.065921 0.16586 -0.025022 385 | -0.072095 0.16784 -0.024725 386 | -0.066 0.16808 -0.030916 387 | -0.073448 0.17051 -0.032045 388 | -0.07777 0.16434 -0.025938 389 | -0.077893 0.16039 -0.021299 390 | -0.078211 0.169 -0.034566 391 | -0.034667 0.15131 -0.00071029 392 | -0.066117 0.17353 -0.047453 393 | -0.071986 0.17612 -0.045384 394 | -0.06925 0.182 -0.055026 395 | -0.064992 0.17802 -0.054645 396 | -0.069935 0.17983 -0.051988 397 | -0.07793 0.17516 -0.0444 398 | -------------------------------------------------------------------------------- /downsample.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class point_and_box{ 11 | public: 12 | int idx; 13 | int box; 14 | point_and_box(int arg_idx){ idx = arg_idx; box = -1;} 15 | bool operator < (const point_and_box &rhs) const {return(box < rhs.box);} 16 | }; 17 | 18 | template class vec3{ 19 | public: 20 | T x, y ,z; 21 | 22 | vec3(){ } 23 | 24 | vec3(T arg_x, T arg_y, T arg_z){ 25 | x = arg_x; 26 | y = arg_y; 27 | z = arg_z; 28 | } 29 | 30 | vec3& operator = (const vec3 &rhs) { 31 | x = rhs.x; 32 | y = rhs.y; 33 | z = rhs.z; 34 | return *this; 35 | } 36 | 37 | vec3(const vec3 & rhs){ 38 | x = rhs.x; 39 | y = rhs.y; 40 | z = rhs.z; 41 | } 42 | 43 | vec3& operator+=(const vec3& rhs){ 44 | x += rhs.x; 45 | y += rhs.y; 46 | z += rhs.z; 47 | return *this; 48 | } 49 | }; 50 | 51 | void getMinMax(vector< vec3 > & inCloud, vec3 &minp, vec3 &maxp){ 52 | for(int i = 0; i < inCloud.size(); i++){ 53 | minp.x = std::min(minp.x, inCloud[i].x); 54 | minp.y = std::min(minp.y, inCloud[i].y); 55 | minp.z = std::min(minp.z, inCloud[i].z); 56 | 57 | maxp.x = std::max(maxp.x, inCloud[i].x); 58 | maxp.y = std::max(maxp.y, inCloud[i].y); 59 | maxp.z = std::max(maxp.z, inCloud[i].z); 60 | } 61 | } 62 | 63 | 64 | void filterPoints(vec3 & leafCount, vector< vec3 > & inCloud, vector< vec3 > & outCloud, vector< point_and_box > indices){ 65 | //Compute minimum and maximum point values 66 | vec3 minp(DBL_MAX, DBL_MAX, DBL_MAX), maxp(-DBL_MAX, -DBL_MAX, -DBL_MAX); 67 | getMinMax(inCloud, minp, maxp); 68 | 69 | //Compute bounding box sizes 70 | vec3 leafSize((maxp.x - minp.x)/leafCount.x, (maxp.y - minp.y)/leafCount.y, (maxp.z - minp.z)/leafCount.z); 71 | vec3 inv_leafSize(1.0/leafSize.x, 1.0/leafSize.y, 1.0/leafSize.z); 72 | 73 | //Compute the minimum and maximum bounding box values 74 | vec3 minb( static_cast ( floor(minp.x * inv_leafSize.x) ), 75 | static_cast ( floor(minp.y * inv_leafSize.y) ), 76 | static_cast ( floor(minp.z * inv_leafSize.z) ) ); 77 | 78 | vec3 maxb( static_cast ( floor(maxp.x * inv_leafSize.x) ), 79 | static_cast ( floor(maxp.y * inv_leafSize.y) ), 80 | static_cast ( floor(maxp.z * inv_leafSize.z) ) ); 81 | 82 | vec3 divb(maxb.x - minb.x + 1, maxb.y - minb.y + 1, maxb.z - minb.z + 1); 83 | vec3 divb_mul(1, divb.x, divb.x * divb.y); 84 | 85 | //Go over all points and insert them into the right leaf 86 | for(int i = 0; i < inCloud.size(); i++){ 87 | int ijk0 = static_cast ( floor(inCloud[i].x * inv_leafSize.x) - minb.x ); 88 | int ijk1 = static_cast ( floor(inCloud[i].y * inv_leafSize.y) - minb.y ); 89 | int ijk2 = static_cast ( floor(inCloud[i].z * inv_leafSize.z) - minb.z ); 90 | int idx = ijk0 * divb_mul.x + ijk1 * divb_mul.y + ijk2 * divb_mul.z; 91 | indices[i].box = idx; 92 | } 93 | sort(indices.begin(), indices.end(), less()); 94 | for(int cp = 0; cp < inCloud.size();){ 95 | vec3 centroid(inCloud[indices[cp].idx].x, inCloud[indices[cp].idx].y, inCloud[indices[cp].idx].z); 96 | int i = cp + 1; 97 | while(i < inCloud.size() && indices[cp].box == indices[i].box){ 98 | centroid += inCloud[indices[i].idx] 99 | ++i; 100 | } 101 | centroid.x /= static_cast(i - cp); 102 | centroid.y /= static_cast(i - cp); 103 | centroid.z /= static_cast(i - cp); 104 | 105 | outCloud.push_back(centroid); 106 | cp = i; 107 | } 108 | } 109 | 110 | int main(int argc, char* argv[]){ 111 | vec3 point(0.0,0.0,0.0); 112 | vector< vec3 > inCloud, outCloud; 113 | vector indices; 114 | ifstream infile(argv[1]); 115 | while(infile >> point.x >> point.y >> point.z){ 116 | indices.push_back( point_and_box( inCloud.size() ) ); 117 | inCloud.push_back( vec3(point) ); 118 | } 119 | infile.close(); 120 | vec3 leafCount; 121 | cout << "There are " << inCloud.size() << " points. Specify the" << endl; 122 | cout << "number of the leaves in X axis: "; 123 | cin >> leafCount.x; 124 | cout << "number of the leaves in Y axis: "; 125 | cin >> leafCount.y; 126 | cout << "number of the leaves in Z axis: "; 127 | cin >> leafCount.z; 128 | 129 | filterPoints(leafCount, inCloud, outCloud, indices); 130 | cout << "The number of points are downsampled to " << outCloud.size() <<"." << endl; 131 | ofstream outfile(argv[2]); 132 | for(int i = 0; i < outCloud.size(); i++) 133 | outfile << outCloud[i].x << " " << outCloud[i].y << " " << outCloud[i].z << endl; 134 | outfile.close(); 135 | } 136 | -------------------------------------------------------------------------------- /sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ucanbizon/downsampling-point-cloud/2a88fee1883f3e17f1ee9cc97d3e9c249a10b965/sample.png --------------------------------------------------------------------------------