├── .github └── workflows │ └── format.yml ├── .gitignore ├── LICENSE ├── README.md ├── buildscene.m ├── data └── kinect_single.mat ├── documentation └── MS paper1.pdf ├── fcngetpose.m ├── fcnvoxels.m ├── functions ├── MarchingCubes.m ├── fcngetvispose.m ├── fcngetvispose3.m ├── fcnplotcam.m ├── fcnregsmall.m ├── fcnregsmall2.m ├── fcnxform.m └── rayintersection │ ├── amanatidesWooAlgorithm.m │ ├── rayBoxIntersection.m │ └── test.m ├── preview.jpg ├── results.jpg └── results.mp4 /.github/workflows/format.yml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Ultralytics Actions https://github.com/ultralytics/actions 4 | # This workflow formats code and documentation in PRs to Ultralytics standards 5 | 6 | name: Ultralytics Actions 7 | 8 | on: 9 | issues: 10 | types: [opened] 11 | pull_request: 12 | branches: [main] 13 | types: [opened, closed, synchronize, review_requested] 14 | 15 | permissions: 16 | contents: write # Modify code in PRs 17 | pull-requests: write # Add comments and labels to PRs 18 | issues: write # Add comments and labels to issues 19 | 20 | jobs: 21 | actions: 22 | runs-on: ubuntu-latest 23 | steps: 24 | - name: Run Ultralytics Actions 25 | uses: ultralytics/actions@main 26 | with: 27 | token: ${{ secrets._GITHUB_TOKEN || secrets.GITHUB_TOKEN }} # Auto-generated token 28 | labels: true # Auto-label issues/PRs using AI 29 | python: true # Format Python with Ruff and docformatter 30 | prettier: true # Format YAML, JSON, Markdown, CSS 31 | spelling: true # Check spelling with codespell 32 | links: false # Check broken links with Lychee 33 | summary: true # Generate AI-powered PR summaries 34 | openai_api_key: ${{ secrets.OPENAI_API_KEY }} # Powers PR summaries, labels and comments 35 | brave_api_key: ${{ secrets.BRAVE_API_KEY }} # Used for broken link resolution 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # https://github.com/github/gitignore/blob/master/Global/macOS.gitignore ----------------------------------------------- 2 | 3 | # General 4 | .DS_Store 5 | .AppleDouble 6 | .LSOverride 7 | 8 | # Icon must end with two \r 9 | Icon 10 | Icon? 11 | 12 | # Thumbnails 13 | ._* 14 | 15 | # Files that might appear in the root of a volume 16 | .DocumentRevisions-V100 17 | .fseventsd 18 | .Spotlight-V100 19 | .TemporaryItems 20 | .Trashes 21 | .VolumeIcon.icns 22 | .com.apple.timemachine.donotpresent 23 | 24 | # Directories potentially created on remote AFP share 25 | .AppleDB 26 | .AppleDesktop 27 | Network Trash Folder 28 | Temporary Items 29 | .apdisk 30 | 31 | 32 | # https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore 33 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 34 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 35 | 36 | # User-specific stuff: 37 | .idea/* 38 | .idea/**/workspace.xml 39 | .idea/**/tasks.xml 40 | .idea/dictionaries 41 | .html # Bokeh Plots 42 | .pg # TensorFlow Frozen Graphs 43 | .avi # videos 44 | 45 | # Sensitive or high-churn files: 46 | .idea/**/dataSources/ 47 | .idea/**/dataSources.ids 48 | .idea/**/dataSources.local.xml 49 | .idea/**/sqlDataSources.xml 50 | .idea/**/dynamic.xml 51 | .idea/**/uiDesigner.xml 52 | 53 | # Gradle: 54 | .idea/**/gradle.xml 55 | .idea/**/libraries 56 | 57 | # CMake 58 | cmake-build-debug/ 59 | cmake-build-release/ 60 | 61 | # Mongo Explorer plugin: 62 | .idea/**/mongoSettings.xml 63 | 64 | ## File-based project format: 65 | *.iws 66 | 67 | ## Plugin-specific files: 68 | 69 | # IntelliJ 70 | out/ 71 | 72 | # mpeltonen/sbt-idea plugin 73 | .idea_modules/ 74 | 75 | # JIRA plugin 76 | atlassian-ide-plugin.xml 77 | 78 | # Cursive Clojure plugin 79 | .idea/replstate.xml 80 | 81 | # Crashlytics plugin (for Android Studio and IntelliJ) 82 | com_crashlytics_export_strings.xml 83 | crashlytics.properties 84 | crashlytics-build.properties 85 | fabric.properties 86 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Ultralytics logo 2 | 3 | # Introduction 🌟 4 | 5 | [![Ultralytics Actions](https://github.com/ultralytics/kinect/actions/workflows/format.yml/badge.svg)](https://github.com/ultralytics/kinect/actions/workflows/format.yml) 6 | [![Ultralytics Discord](https://img.shields.io/discord/1089800235347353640?logo=discord&logoColor=white&label=Discord&color=blue)](https://discord.com/invite/ultralytics) 7 | [![Ultralytics Forums](https://img.shields.io/discourse/users?server=https%3A%2F%2Fcommunity.ultralytics.com&logo=discourse&label=Forums&color=blue)](https://community.ultralytics.com/) 8 | [![Ultralytics Reddit](https://img.shields.io/reddit/subreddit-subscribers/ultralytics?style=flat&logo=reddit&logoColor=white&label=Reddit&color=blue)](https://reddit.com/r/ultralytics) 9 | 10 | Welcome to the [Ultralytics Kinect](https://github.com/ultralytics/kinect) repository! This project showcases advanced **3D scene reconstruction** algorithms utilizing data captured by the [Microsoft Kinect sensor](https://developer.microsoft.com/en-us/windows/kinect/), a pioneering [depth-imaging](https://www.ultralytics.com/glossary/computer-vision-cv) device. Explore our implementation of cutting-edge techniques in [computer vision](https://www.ultralytics.com/glossary/computer-vision-cv) and see the results in action through example videos on the [Ultralytics YouTube channel](https://www.youtube.com/ultralytics). 11 | 12 | [![Kinect Video Preview](https://github.com/ultralytics/kinect/blob/main/preview.jpg)](https://youtu.be/dqK5DkgTGyk "Click to Watch the 3D Reconstruction Demo!") 13 | 14 | # Prerequisites 🛠️ 15 | 16 | Before starting, ensure you have [MATLAB](https://www.mathworks.com/products/matlab.html) version 2018a or newer installed. You'll also need our repository of common MATLAB functions. Clone it using the following command: 17 | 18 | ```shell 19 | git clone https://github.com/ultralytics/functions-matlab 20 | ``` 21 | 22 | After cloning, add the repository to your MATLAB path. Replace `/path/to/` with the actual directory where you cloned the `functions-matlab` repository: 23 | 24 | ```matlab 25 | addpath(genpath('/path/to/functions-matlab')) % Add the cloned repo to your MATLAB path 26 | ``` 27 | 28 | Additionally, confirm that the following MATLAB toolboxes are installed: 29 | 30 | - [Statistics and Machine Learning Toolbox](https://www.mathworks.com/products/statistics.html) 31 | - [Signal Processing Toolbox](https://www.mathworks.com/products/signal.html) 32 | 33 | For more details on setting up development environments, check out the general [Ultralytics documentation](https://docs.ultralytics.com/). 34 | 35 | # How to Run 🏃 36 | 37 | To launch the 3D scene reconstruction process, simply run the `buildscene` command within your MATLAB environment: 38 | 39 | ```matlab 40 | buildscene % Start the 3D reconstruction process 41 | ``` 42 | 43 | This script initiates the reconstruction using the provided Kinect data and the implemented algorithms. For a general overview of running Ultralytics projects, see our [Quickstart Guide](https://docs.ultralytics.com/quickstart/). 44 | 45 | # Contribute 🤝 46 | 47 | Your contributions can significantly enhance this project! We welcome collaboration from the open-source community. To get started, please review our [Contributing Guide](https://docs.ultralytics.com/help/contributing/) and consider sharing your feedback through our [Survey](https://www.ultralytics.com/survey?utm_source=github&utm_medium=social&utm_campaign=Survey). Your insights help drive improvements and innovation at [Ultralytics](https://www.ultralytics.com/). A big thank you to all our contributors! 48 | 49 | [![Ultralytics open-source contributors](https://raw.githubusercontent.com/ultralytics/assets/main/im/image-contributors.png)](https://github.com/ultralytics/ultralytics/graphs/contributors) 50 | 51 | # Licensing Options ⚖️ 52 | 53 | This software is available under the **AGPL-3.0 License**, an [OSI-approved](https://opensource.org/license/agpl-v3) open-source license emphasizing transparency and collaboration. This license is ideal for students, researchers, and enthusiasts looking to experiment and share their work. You can find the full license details in the [LICENSE](LICENSE) file. 54 | 55 | For commercial use cases requiring integration into proprietary products and services, Ultralytics offers an **Enterprise License**. This license bypasses the open-source requirements of AGPL-3.0, providing flexibility for commercial deployment. Learn more about our commercial licensing options at [Ultralytics Licensing](https://www.ultralytics.com/license). 56 | 57 | # Stay Connected! 🌐 58 | 59 | Encountered a bug or have a feature request? Please submit it via [GitHub Issues](https://github.com/ultralytics/kinect/issues). We also invite you to join our vibrant [Discord](https://discord.com/invite/ultralytics) community for discussions, support, and collaboration with fellow developers and the Ultralytics team. Stay updated on the latest in AI and computer vision through the [Ultralytics Blog](https://www.ultralytics.com/blog). 60 | 61 |
62 |
63 | Ultralytics GitHub 64 | space 65 | Ultralytics LinkedIn 66 | space 67 | Ultralytics Twitter 68 | space 69 | Ultralytics YouTube 70 | space 71 | Ultralytics TikTok 72 | space 73 | Ultralytics BiliBili 74 | space 75 | Ultralytics Discord 76 |
77 | -------------------------------------------------------------------------------- /buildscene.m: -------------------------------------------------------------------------------- 1 | % Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | clc; clear; close; 4 | vidFlag=true; 5 | 6 | %Add MATLAB common functions to MATLAB path! 7 | x = mfilename('fullpath'); path1=x(1:find(x==filesep,1,'last')); 8 | addpath([path1 'functions']) 9 | addpath([path1 'matfiles']) 10 | load kinect_single.mat 11 | 12 | sf=1; %starting frame 13 | ef=size(xyz,4); 14 | xyz = xyz(:,:,:,sf:ef); 15 | rgb = rgb(:,:,:,sf:ef); 16 | 17 | ni = size(xyz,4); 18 | xyzv = reshape(xyz,[480*640 3 ni]); clear xyz 19 | %rgbv = double(reshape(rgb,[480*640 3 ni]))/255; 20 | 21 | [h0, hf]=fig(2,3); 22 | set(hf,'Units','pixels','position',[0 0 1280 720]); 23 | set(h0,'nextplot','replacechildren'); set(h0(3),'cameraviewangle',8,'clim',[0.8 4],'position',[.38 .05 .6 .9]) 24 | lim3 = [0 3 -2 2 -2 1]; xlims=[0 0]; ylims=[0 0]; zlims=[0 0]; 25 | set(h0([1 4]),'cameraviewangle',5.5) 26 | C=[0 0 1; 1 0 0; 0 -1 0]; %Takashi 2 NED 27 | set(h0(2),'position',[.38 .1 .08 .08],'cameraviewangle',4) 28 | set(h0(5),'position',[.38 .42 .1 .1]) 29 | set(h0(6),'position',[.39 .82 .1 .1]) 30 | axis(h0([2 5]),'off') 31 | %delete(h0(6)) 32 | 33 | h1=[]; h4a=[]; h2a=[]; h1a=[]; h1b=[]; xc=zeros(60E6,3,'single'); 34 | nc=1; 35 | %v = zeros(256,256,256); 36 | xhat = [0 0 0, 0 0 0];% xyz rpy 37 | if vidFlag 38 | vidObj = VideoWriter(fcnincrementfname('SLAM_rgb'),'MPEG-4'); 39 | open(vidObj); 40 | end 41 | for i=1:ni 42 | fprintf('frame %.0f\n',i) 43 | xyzi = xyzv(:,:,i)*C'; 44 | rv = fcnrange(xyzi); v1=find(rv>0); x1=xyzi(v1,:); n1=numel(v1); 45 | r=reshape(double(rv),480,640); r(r==0)=nan; 46 | 47 | %221 48 | h=h0(1); 49 | delete(h1a(ishandle(h1a))); h1a=image(rgb(:,:,:,i),'Parent',h); title(h,sprintf('frame %.0f, t=%.2fs',i,i/30),'units','normalized','position',[.5 .9]); hold(h,'on'); if i==1; set(h,'ydir','reverse'); axis(h,'off','equal'); end 50 | 51 | %224 52 | if i==1 53 | h=h0(4); h224=pcolor(h,r); shading(h,'flat'); hc=colorbar('peer',h,'location','north'); set(h,'clim',[0.8 4]); set(h,'ydir','reverse'); fcncolorbar(1,'m',hc); axis(h,'equal','off'); set(hc,'xcolor',[.7 .7 .7],'ycolor',[.7 .7 .7]); box(h,'on') 54 | else 55 | set(h224,'CData',r); 56 | end 57 | 58 | %223 59 | h=h0(3); 60 | if i>1 61 | I1 = im2double(rgb2gray(rgb(:,:,:,i-1))); 62 | %xhat = fcngetvispose3(xc(1:nc,:),x1,xhat,i,rgb,xyzi,xyzim1,h0); 63 | %[~,xhat]=fcnregsmall2(h0,xhat,rgb,I1,i); 64 | [xhat,I1,features1,vp1] = fcngetvispose(h0,xhat,rgb,I1,features1,vp1,i); 65 | xhat = fcngetpose(xc(1:nc,:),x1,xhat,i,rgb,xyzi,h0); 66 | else 67 | I1 = im2double(rgb2gray(rgb(:,:,:,i))); 68 | cornerPoints1 = detectHarrisFeatures(I1);%,'MaximumCornerCount', 500, 'CornerThreshold', 0.00001, 'NeighborhoodSize', [21 21]); 69 | [features1, vp1] = extractFeatures(I1, cornerPoints1, 'BlockSize', 21); %valid points 70 | end 71 | x1_w=fcnxform(x1,xhat); xc(nc:nc+n1-1,:)=x1_w; nc=nc+n1-1; 72 | 73 | delete(h4a); %set(h4a,'color',[.8 .8 .8],'linewidth',.5) 74 | h4a = fcnplotcam(h,xhat,rgb(:,:,:,i)); hold(h,'on'); 75 | v2 = int32(rand(50000,1)*nc+1); xc1=xc(v2,:); 76 | scatter3(h,xc1(:,1),xc1(:,2),xc1(:,3),1,fcnrange(xc1,xhat(1:3))); 77 | 78 | if i==1 79 | hold(h,'on'); axis(h,'equal','vis3d'); box(h,'on'); view(h,-60,89); set(h,'clim',[0.8 4]); set(h,'ydir','reverse','zdir','reverse','ylimmode','manual','zlimmode','manual','xlimmode','manual'); 80 | xlabel(h,'x (m)'); ylabel(h,'y (m)'); zlabel(h,'z (m)'); axis(h,'off') 81 | end 82 | view(h,-60+i*.5,40) 83 | 84 | x=fcnxform([0 0 0; .1 0 0],xhat); plot3(h,x(:,1),x(:,2),x(:,3),'r-'); 85 | y=fcnxform([0 0 0; 0 .1 0],xhat); plot3(h,y(:,1),y(:,2),y(:,3),'g-'); 86 | z=fcnxform([0 0 0; 0 0 .1],xhat); plot3(h,z(:,1),z(:,2),z(:,3),'b-'); 87 | 88 | %get new lims 89 | xlims=fcnminmax([x1_w(:,1); xlims']); ylims=fcnminmax([x1_w(:,2); ylims']); zlims=fcnminmax([x1_w(:,3); zlims']); axis(h0(3),[xlims ylims zlims]) 90 | if i==1; fcnfontsize(18); end 91 | 92 | xyzim1=xyzi; 93 | drawnow; if vidFlag; writeVideo(vidObj,getframe(hf)); end 94 | end 95 | xc=xc(1:nc,:); 96 | v2 = randi(nc,min(1E6,nc),1); xc1=xc(v2,:); scatter3(h,xc1(:,1),xc1(:,2),xc1(:,3),0.01,fcnrange(xc1,xhat(1:3))); 97 | 98 | %MESHLAB OUTPUT ----------------------------------------------------------- 99 | % fid=fopen('xc.asc','wt'); 100 | % fprintf(fid,'%3.4f, %3.4f, %3.4f \n',xc(1:10E6,:)') 101 | % fclose(fid) 102 | 103 | %VOXELIZE ----------------------------------------------------------------- 104 | %v = fcnvoxels(xc1); 105 | 106 | delete(h0(h0~=h0(3))) 107 | h=h0(3); 108 | pos1 = get(h,'pos'); 109 | pos2 = [0 0 1 1]; 110 | v = 1-logspace(0,-2,30); 111 | view0=get(h,'view'); 112 | for i=1:numel(v) 113 | set(h,'pos',pos1*(1-v(i))+pos2*v(i)) 114 | view(h,view0(1),view0(2)+v(i)*50); 115 | drawnow; if vidFlag; writeVideo(vidObj,getframe(hf)); end 116 | fprintf('Camera transition frame %.0f/%.0f\n',i,numel(v)) 117 | end 118 | 119 | v = 1-logspace(0,-2,150); 120 | view0=get(h,'view'); 121 | va = get(h,'cameraviewangle'); 122 | for i = 1:numel(v) 123 | view(h,view0(1)+v(i)*450,view0(2)+v(i)*-75) 124 | set(h,'cameraviewangle',va-va*.5*v(i)); 125 | drawnow; if vidFlag; writeVideo(vidObj,getframe(hf)); end 126 | fprintf('Camera transition frame %.0f/%.0f\n',i,numel(v)) 127 | end 128 | if vidFlag; close(vidObj); end 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /data/kinect_single.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ultralytics/kinect/686f383369b225a246861129774f0973aeeff134/data/kinect_single.mat -------------------------------------------------------------------------------- /documentation/MS paper1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ultralytics/kinect/686f383369b225a246861129774f0973aeeff134/documentation/MS paper1.pdf -------------------------------------------------------------------------------- /fcngetpose.m: -------------------------------------------------------------------------------- 1 | % Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | function xhat = fcngetpose(xc,x1,xhat,i,rgb,xyzi,h) 4 | options = optimset('TolX',1E-9,'TolFun',1E-9,'MaxIter',5000,'Display','off','MaxFunEvals',5000,'LargeScale', 'off','GradObj','on'); 5 | if i==1 6 | return 7 | end 8 | 9 | %v1=ceil(rand(1E4,1)*size(xc,1)); 10 | %delete(get(h(6),'children')); plot3(h(6),xc(v1,1),xc(v1,2),xc(v1,3),'.','markersize',5,'color',[.7 .7 1]); hold(h(5),'on'); hold(h(6),'on') %cumulative 11 | 12 | %camvec = fcnRPY2DCM_B2W(xhat(4:6))*[1; 0; 0]; 13 | % theta = fcnangle([xc(:,1)-xhat(1), xc(:,2)-xhat(2), xc(:,3)-xhat(3)], camvec'); %fov=53deg horizontal 43deg vertical 14 | % xc = xc(theta<(45*pi/180),:); 15 | 16 | %v1=ceil(rand(1E4,1)*size(xc,1)); 17 | %plot3(h(6),xc(v1,1),xc(v1,2),xc(v1,3),'.','markersize',5,'color',[0 0 1]) 18 | %plot3(h(6),[0 camvec(1)]+xhat(1),[0 camvec(2)]+xhat(2),[0 camvec(3)]+xhat(3),'-k','linewidth',3) 19 | 20 | 21 | x = zeros(1,6); 22 | fx = zeros(1,1); 23 | for j=1 24 | [x(j,:), fx(j)] = fcnoptimize(xc,x1,xhat,.75,2E6,options,i); 25 | end 26 | [fx1,k]=min(fx); 27 | 28 | xhat = x(k,:); 29 | %fprintf('dispersion=%.3f\n',std(fx)/mean(fx)) 30 | end 31 | 32 | 33 | function [xhat,fval] = fcnoptimize(xc,x1,xhat,fraction,nxcp,options,i) 34 | tic 35 | nxc=size(xc,1); n1=size(x1,1); nxcp=min(nxc,nxcp); %max historical point to select from past 36 | if i>5 37 | x = linspace(0,1,100); 38 | ci = fcnrandcdf(cumsum(exp(-x*6)),x,round(9000/fraction),'linear'); ci=nxc-round(ci*nxcp)-1; 39 | %[y,x]=hist(ci,30); bar(h(5),x,y,1,'b'); axis(h(5),'on','normal','tight') 40 | else 41 | ci=uint32(rand(round(9000/fraction),1)*(nxcp-1)+1); 42 | end 43 | xc1=double(xc(ci,:)); 44 | ci=uint32(rand(round(3000/fraction),1)*(n1-1)+1); x11=x1(ci,:); 45 | 46 | ns = createns(xc1,'NSMethod','kdtree'); 47 | [xhat,fval]=fminunc(@(x) fcncost(xc1,double(x11),x,ns,fraction), double(xhat), options); fprintf('%9.3f%9.3f%9.3f%9.3f%9.3f%9.3f (%.2fs icp, fx=%.3f)\n',xhat,toc,fval) 48 | end 49 | 50 | function [fx, gx] = fcncost(vc0,v10,x,ns,fraction) 51 | dx = 1E-5; 52 | n1=size(v10,1); 53 | v1=v10*fcnRPY2DCM_W2B(x(4:6)); v1=[v1(:,1)+x(1), v1(:,2)+x(2), v1(:,3)+x(3)]; 54 | 55 | [i, r] = knnsearch(ns,v1,'k',1); 56 | [r, j] = sort(r); %min ranges sorted 57 | a = 1:round(fraction*n1); j=j(a); 58 | fx = sum(r(a).^2); 59 | vc = vc0(i(j),:); 60 | v1 = v1(j,:); 61 | 62 | v10=v10(j,:); 63 | v{1} = [v1(:,1)+dx, v1(:,2), v1(:,3)]; 64 | v{2} = [v1(:,1), v1(:,2)+dx, v1(:,3)]; 65 | v{3} = [v1(:,1), v1(:,2), v1(:,3)+dx]; 66 | vt = v10*fcnRPY2DCM_W2B(x(4:6)+[dx 0 0]); vt=[vt(:,1)+x(1), vt(:,2)+x(2), vt(:,3)+x(3)]; v{4}=vt; 67 | vt = v10*fcnRPY2DCM_W2B(x(4:6)+[0 dx 0]); vt=[vt(:,1)+x(1), vt(:,2)+x(2), vt(:,3)+x(3)]; v{5}=vt; 68 | vt = v10*fcnRPY2DCM_W2B(x(4:6)+[0 0 dx]); vt=[vt(:,1)+x(1), vt(:,2)+x(2), vt(:,3)+x(3)]; v{6}=vt; 69 | 70 | fg=zeros(6,1); 71 | for i=1:6 72 | rs=sum((v{i}-vc).^2,2); 73 | fg(i) = sum(rs); 74 | end 75 | gx = (fg-fx)./dx; 76 | end 77 | 78 | -------------------------------------------------------------------------------- /fcnvoxels.m: -------------------------------------------------------------------------------- 1 | % Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | %function v=fcnvoxels(x1) 4 | nv=400; 5 | nr=size(x1,1); 6 | 7 | xmin = min(x1,[],1); 8 | xmax = max(x1,[],1); 9 | 10 | x=linspace(xmin(1),xmax(1),nv); 11 | y=linspace(xmin(2),xmax(2),nv); 12 | z=linspace(xmin(3),xmax(3),nv); 13 | [xm,ym,zm] = ndgrid(x,y,z); 14 | 15 | xi=zeros(size(x1),'uint16'); 16 | xi(:,1) = fcnindex1(x,x1(:,1)); 17 | xi(:,2) = fcnindex1(y,x1(:,2)); 18 | xi(:,3) = fcnindex1(z,x1(:,3)); 19 | 20 | isovalue = 1; 21 | v = accumarray(xi, 1, [nv nv nv]); 22 | 23 | 24 | figure 25 | h=gca; 26 | 27 | n = isosurface(xm,ym,zm,v,isovalue); 28 | p1 = patch(n);%,'AmbientStrength',1); 29 | set(p1,'FaceColor',[1 1 1]);%,'EdgeColor','none','FaceAlpha',1) 30 | reducepatch(p1, .8) 31 | isonormals(x,y,z,v,p1) %WTF does this actually do? 32 | lighting phong 33 | material shiny 34 | 35 | %mrp = nv + rand*nv; 36 | %light('Position',[0 0 0]); light('Position',[0 0 mrp-1]); light('Position',[0 0 mrp+1]); light('Position',[-5 0 mrp]); light('Position',[0 -5 mrp]); light('Position',[5 0 mrp]); light('Position',[0 5 mrp]); 37 | 38 | p2 = patch(isocaps(xm,ym,zm,v,isovalue)); 39 | set(p2,'EdgeColor','none','FaceColor','interp','SpecularColorReflectance',0,'SpecularStrength',.3); 40 | 41 | 42 | hold(h,'on'); axis(h,'equal','vis3d'); box(h,'on'); view(h,-60,89); set(h,'clim',[0.8 4]); set(h,'ydir','reverse','zdir','reverse','ylimmode','manual','zlimmode','manual','xlimmode','manual'); 43 | xlabel(h,'x (m)'); ylabel(h,'y (m)'); zlabel(h,'z (m)'); -------------------------------------------------------------------------------- /functions/MarchingCubes.m: -------------------------------------------------------------------------------- 1 | % Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | function [F,V,col] = MarchingCubes(x,y,z,c,iso,colors) 4 | 5 | % [F,V] = MarchingCubes(X,Y,Z,C,ISO) 6 | % [F,V,COL] = MarchingCubes(X,Y,Z,C,ISO,COLORS) 7 | % 8 | % Use marching cubes algorithm to compute a triangulated mesh of the 9 | % isosurface within the 3D matrix of scalar values C at isosurface value 10 | % ISO. The 3D matrices (X,Y,Z) specify the points at which the data C 11 | % is given. Output arguments F and V are the face list and vertex list 12 | % of the resulting triangulated mesh. The orientation of the triangles is 13 | % chosen such that the normals point from the higher values to the lower 14 | % values. Optional arguments COLORS and COLS can be used to produce 15 | % interpolated mesh face colors. For usage, see Matlab's isosurface.m. 16 | % To avoid Out of Memory errors when matrix C is large, convert matrices 17 | % X,Y,Z and C from doubles (Matlab default) to singles (32-bit floats). 18 | % 19 | % Adapted for Matlab by Peter Hammer in 2011 based on an 20 | % Octave function written by Martin Helm in 2009 21 | % http://www.mhelm.de/octave/m/marching_cube.m 22 | % 23 | % Revised 30 September, 2011 to add code by Oliver Woodford for removing 24 | % duplicate vertices. 25 | 26 | PlotFlag = 1; % 1=plot isosurface, 0=do not plot 27 | calc_cols = false; 28 | lindex = 4; 29 | 30 | [edgeTable, triTable] = GetTables(); 31 | 32 | if ((nargin ~= 5 && nargin ~= 6) || (nargout ~= 2 && nargout ~= 3)) 33 | error('wrong number of input and/or output arguments'); 34 | end 35 | 36 | if (ndims(x) ~= 3 || ndims(y) ~= 3 || ndims(z) ~= 3 || ndims(c) ~= 3) 37 | error('x, y, z, c must be matrices of dim 3'); 38 | end 39 | 40 | if (any(size(x) ~= size(y)) || any(size(y) ~= size(z)) || any(size(z) ~= size(c))) 41 | error('x, y, z, c must be the same size'); 42 | end 43 | 44 | if (any(size(x) < [2 2 2])) 45 | error('grid size must be at least 2x2x2'); 46 | end 47 | 48 | if (~isscalar(iso)) 49 | error('iso needs to be scalar value'); 50 | end 51 | 52 | if ( nargin == 6 ) 53 | if (size(colors) ~= size(c)) 54 | error( 'color must be matrix of same size as c'); 55 | end 56 | calc_cols = true; 57 | lindex = 5; 58 | end 59 | 60 | n = size(c) - 1; 61 | 62 | 63 | % for each cube, assign which edges are intersected by the isosurface 64 | 65 | cc = zeros(n(1),n(2),n(3),'uint16'); % 3d array of 8-bit vertex codes 66 | 67 | vertex_idx = {1:n(1), 1:n(2), 1:n(3); ... 68 | 2:n(1)+1, 1:n(2), 1:n(3); ... 69 | 2:n(1)+1, 2:n(2)+1, 1:n(3); ... 70 | 1:n(1), 2:n(2)+1, 1:n(3); ... 71 | 1:n(1), 1:n(2), 2:n(3)+1; ... 72 | 2:n(1)+1, 1:n(2), 2:n(3)+1; ... 73 | 2:n(1)+1, 2:n(2)+1, 2:n(3)+1; ... 74 | 1:n(1), 2:n(2)+1, 2:n(3)+1 }; 75 | 76 | for ii=1:8 % loop thru vertices of all cubes 77 | idx = c(vertex_idx{ii, :}) > iso; % which cubes have vtx ii > iso 78 | cc(idx) = bitset(cc(idx), ii); % for those cubes, turn bit ii on 79 | end 80 | 81 | cedge = edgeTable(cc+1); % intersected edges for each cube 82 | id = find(cedge); % voxels which are intersected (indx into cedge) 83 | if isempty(id) % all voxels are above or below iso 84 | F = []; 85 | V = []; 86 | col = []; 87 | return 88 | end 89 | 90 | 91 | % calculate the list of intersection points 92 | xyz_off = [1, 1, 1; 2, 1, 1; 2, 2, 1; 1, 2, 1; 1, 1, 2; 2, 1, 2; 2, 2, 2; 1, 2, 2]; 93 | edges = [1 2; 2 3; 3 4; 4 1; 5 6; 6 7; 7 8; 8 5; 1 5; 2 6; 3 7; 4 8]; 94 | offset = sub2ind(size(c), xyz_off(:, 1), xyz_off(:, 2), xyz_off(:, 3)) -1; 95 | pp = zeros(length(id), lindex, 12); 96 | ccedge = [cedge(id), id]; 97 | ix_offset=0; 98 | for jj=1:12 99 | id__ = logical(bitget(ccedge(:, 1), jj)); % used for logical indexing 100 | id_ = ccedge(id__, 2); 101 | [ix iy iz] = ind2sub(size(cc), id_); 102 | id_c = sub2ind(size(c), ix, iy, iz); 103 | id1 = id_c + offset(edges(jj, 1)); 104 | id2 = id_c + offset(edges(jj, 2)); 105 | if ( calc_cols ) 106 | pp(id__, 1:5, jj) = [InterpolateVertices(iso, x(id1), y(id1), z(id1), ... 107 | x(id2), y(id2), z(id2), c(id1), c(id2), colors(id1), colors(id2)), ... 108 | (1:size(id_, 1))' + ix_offset ]; 109 | else 110 | pp(id__, 1:4, jj) = [InterpolateVertices(iso, x(id1), y(id1), z(id1), ... 111 | x(id2), y(id2), z(id2), c(id1), c(id2)), ... 112 | (1:size(id_, 1))' + ix_offset ]; 113 | end 114 | ix_offset = ix_offset + size(id_, 1); 115 | end 116 | 117 | % calculate the triangulation from the point list 118 | F = []; 119 | tri = triTable(cc(id)+1, :); 120 | for jj=1:3:15 121 | id_ = find(tri(:, jj)>0); 122 | V = [id_, lindex*ones(size(id_, 1), 1),tri(id_, jj:jj+2) ]; 123 | if ( ~ isempty(V) ) 124 | p1 = sub2ind(size(pp), V(:,1), V(:,2), V(:,3)); 125 | p2 = sub2ind(size(pp), V(:,1), V(:,2), V(:,4)); 126 | p3 = sub2ind(size(pp), V(:,1), V(:,2), V(:,5)); 127 | F = [F; pp(p1), pp(p2), pp(p3)]; 128 | end 129 | end 130 | 131 | V = []; 132 | col = []; 133 | for jj = 1:12 134 | idp = pp(:, lindex, jj) > 0; 135 | if any(idp) 136 | V(pp(idp, lindex, jj), 1:3) = pp(idp, 1:3, jj); 137 | if (calc_cols) 138 | col(pp(idp, lindex, jj),1) = pp(idp, 4, jj); 139 | end 140 | end 141 | end 142 | 143 | % Remove duplicate vertices (by Oliver Woodford) 144 | [V I] = sortrows(V); 145 | M = [true; any(diff(V), 2)]; 146 | V = V(M,:); 147 | I(I) = cumsum(M); 148 | F = I(F); 149 | 150 | if PlotFlag 151 | figure('color',[1 1 1]) 152 | patch('vertices',V,'faces',F,'edgecolor','none',... 153 | 'facecolor',[1 0 0],'facelighting','phong') 154 | light 155 | axis equal off 156 | end 157 | 158 | % ============================================================ 159 | % ================== SUBFUNCTIONS =========================== 160 | % ============================================================ 161 | 162 | function p = InterpolateVertices(isolevel,p1x,p1y,p1z,p2x,p2y,p2z,valp1,valp2,col1,col2) 163 | 164 | if nargin == 9 165 | p = zeros(length(p1x), 3); 166 | elseif nargin == 11 167 | p = zeros(length(p1x), 4); 168 | else 169 | error('Wrong number of arguments'); 170 | end 171 | mu = zeros(length(p1x), 1); 172 | id = abs(valp1-valp2) < (10*eps) .* (abs(valp1) + abs(valp2)); 173 | if ( any(id) ) 174 | p(id, 1:3) = [ p1x(id), p1y(id), p1z(id) ]; 175 | if ( nargin == 11 ) 176 | p(id, 4) = col1(id); 177 | end 178 | end 179 | nid = ~id; 180 | if any(nid) 181 | mu(nid) = (isolevel - valp1(nid)) ./ (valp2(nid) - valp1(nid)); 182 | p(nid, 1:3) = [p1x(nid) + mu(nid) .* (p2x(nid) - p1x(nid)), ... 183 | p1y(nid) + mu(nid) .* (p2y(nid) - p1y(nid)), ... 184 | p1z(nid) + mu(nid) .* (p2z(nid) - p1z(nid))]; 185 | if nargin == 11 186 | p(nid, 4) = col1(nid) + mu(nid) .* (col2(nid) - col1(nid)); 187 | end 188 | end 189 | 190 | 191 | function [edgeTable, triTable] = GetTables() 192 | 193 | edgeTable = [ 194 | 0, 265, 515, 778, 1030, 1295, 1541, 1804, ... 195 | 2060, 2309, 2575, 2822, 3082, 3331, 3593, 3840, ... 196 | 400, 153, 915, 666, 1430, 1183, 1941, 1692, ... 197 | 2460, 2197, 2975, 2710, 3482, 3219, 3993, 3728, ... 198 | 560, 825, 51, 314, 1590, 1855, 1077, 1340, ... 199 | 2620, 2869, 2111, 2358, 3642, 3891, 3129, 3376, ... 200 | 928, 681, 419, 170, 1958, 1711, 1445, 1196, ... 201 | 2988, 2725, 2479, 2214, 4010, 3747, 3497, 3232, ... 202 | 1120, 1385, 1635, 1898, 102, 367, 613, 876, ... 203 | 3180, 3429, 3695, 3942, 2154, 2403, 2665, 2912, ... 204 | 1520, 1273, 2035, 1786, 502, 255, 1013, 764, ... 205 | 3580, 3317, 4095, 3830, 2554, 2291, 3065, 2800, ... 206 | 1616, 1881, 1107, 1370, 598, 863, 85, 348, ... 207 | 3676, 3925, 3167, 3414, 2650, 2899, 2137, 2384, ... 208 | 1984, 1737, 1475, 1226, 966, 719, 453, 204, ... 209 | 4044, 3781, 3535, 3270, 3018, 2755, 2505, 2240, ... 210 | 2240, 2505, 2755, 3018, 3270, 3535, 3781, 4044, ... 211 | 204, 453, 719, 966, 1226, 1475, 1737, 1984, ... 212 | 2384, 2137, 2899, 2650, 3414, 3167, 3925, 3676, ... 213 | 348, 85, 863, 598, 1370, 1107, 1881, 1616, ... 214 | 2800, 3065, 2291, 2554, 3830, 4095, 3317, 3580, ... 215 | 764, 1013, 255, 502, 1786, 2035, 1273, 1520, ... 216 | 2912, 2665, 2403, 2154, 3942, 3695, 3429, 3180, ... 217 | 876, 613, 367, 102, 1898, 1635, 1385, 1120, ... 218 | 3232, 3497, 3747, 4010, 2214, 2479, 2725, 2988, ... 219 | 1196, 1445, 1711, 1958, 170, 419, 681, 928, ... 220 | 3376, 3129, 3891, 3642, 2358, 2111, 2869, 2620, ... 221 | 1340, 1077, 1855, 1590, 314, 51, 825, 560, ... 222 | 3728, 3993, 3219, 3482, 2710, 2975, 2197, 2460, ... 223 | 1692, 1941, 1183, 1430, 666, 915, 153, 400, ... 224 | 3840, 3593, 3331, 3082, 2822, 2575, 2309, 2060, ... 225 | 1804, 1541, 1295, 1030, 778, 515, 265, 0]; 226 | 227 | triTable =[ 228 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 229 | 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 230 | 0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 231 | 1, 8, 3, 9, 8, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 232 | 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 233 | 0, 8, 3, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 234 | 9, 2, 10, 0, 2, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 235 | 2, 8, 3, 2, 10, 8, 10, 9, 8, -1, -1, -1, -1, -1, -1, -1; 236 | 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 237 | 0, 11, 2, 8, 11, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 238 | 1, 9, 0, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 239 | 1, 11, 2, 1, 9, 11, 9, 8, 11, -1, -1, -1, -1, -1, -1, -1; 240 | 3, 10, 1, 11, 10, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 241 | 0, 10, 1, 0, 8, 10, 8, 11, 10, -1, -1, -1, -1, -1, -1, -1; 242 | 3, 9, 0, 3, 11, 9, 11, 10, 9, -1, -1, -1, -1, -1, -1, -1; 243 | 9, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 244 | 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 245 | 4, 3, 0, 7, 3, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 246 | 0, 1, 9, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 247 | 4, 1, 9, 4, 7, 1, 7, 3, 1, -1, -1, -1, -1, -1, -1, -1; 248 | 1, 2, 10, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 249 | 3, 4, 7, 3, 0, 4, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1; 250 | 9, 2, 10, 9, 0, 2, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1; 251 | 2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, -1, -1, -1, -1; 252 | 8, 4, 7, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 253 | 11, 4, 7, 11, 2, 4, 2, 0, 4, -1, -1, -1, -1, -1, -1, -1; 254 | 9, 0, 1, 8, 4, 7, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1; 255 | 4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, -1, -1, -1, -1; 256 | 3, 10, 1, 3, 11, 10, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1; 257 | 1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, -1, -1, -1, -1; 258 | 4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, -1, -1, -1, -1; 259 | 4, 7, 11, 4, 11, 9, 9, 11, 10, -1, -1, -1, -1, -1, -1, -1; 260 | 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 261 | 9, 5, 4, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 262 | 0, 5, 4, 1, 5, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 263 | 8, 5, 4, 8, 3, 5, 3, 1, 5, -1, -1, -1, -1, -1, -1, -1; 264 | 1, 2, 10, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 265 | 3, 0, 8, 1, 2, 10, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1; 266 | 5, 2, 10, 5, 4, 2, 4, 0, 2, -1, -1, -1, -1, -1, -1, -1; 267 | 2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, -1, -1, -1, -1; 268 | 9, 5, 4, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 269 | 0, 11, 2, 0, 8, 11, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1; 270 | 0, 5, 4, 0, 1, 5, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1; 271 | 2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, -1, -1, -1, -1; 272 | 10, 3, 11, 10, 1, 3, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1; 273 | 4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, -1, -1, -1, -1; 274 | 5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, -1, -1, -1, -1; 275 | 5, 4, 8, 5, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1; 276 | 9, 7, 8, 5, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 277 | 9, 3, 0, 9, 5, 3, 5, 7, 3, -1, -1, -1, -1, -1, -1, -1; 278 | 0, 7, 8, 0, 1, 7, 1, 5, 7, -1, -1, -1, -1, -1, -1, -1; 279 | 1, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 280 | 9, 7, 8, 9, 5, 7, 10, 1, 2, -1, -1, -1, -1, -1, -1, -1; 281 | 10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, -1, -1, -1, -1; 282 | 8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, -1, -1, -1, -1; 283 | 2, 10, 5, 2, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1; 284 | 7, 9, 5, 7, 8, 9, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1; 285 | 9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, -1, -1, -1, -1; 286 | 2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, -1, -1, -1, -1; 287 | 11, 2, 1, 11, 1, 7, 7, 1, 5, -1, -1, -1, -1, -1, -1, -1; 288 | 9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, -1, -1, -1, -1; 289 | 5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0, -1; 290 | 11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0, -1; 291 | 11, 10, 5, 7, 11, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 292 | 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 293 | 0, 8, 3, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 294 | 9, 0, 1, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 295 | 1, 8, 3, 1, 9, 8, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1; 296 | 1, 6, 5, 2, 6, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 297 | 1, 6, 5, 1, 2, 6, 3, 0, 8, -1, -1, -1, -1, -1, -1, -1; 298 | 9, 6, 5, 9, 0, 6, 0, 2, 6, -1, -1, -1, -1, -1, -1, -1; 299 | 5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, -1, -1, -1, -1; 300 | 2, 3, 11, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 301 | 11, 0, 8, 11, 2, 0, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1; 302 | 0, 1, 9, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1; 303 | 5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, -1, -1, -1, -1; 304 | 6, 3, 11, 6, 5, 3, 5, 1, 3, -1, -1, -1, -1, -1, -1, -1; 305 | 0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, -1, -1, -1, -1; 306 | 3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, -1, -1, -1, -1; 307 | 6, 5, 9, 6, 9, 11, 11, 9, 8, -1, -1, -1, -1, -1, -1, -1; 308 | 5, 10, 6, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 309 | 4, 3, 0, 4, 7, 3, 6, 5, 10, -1, -1, -1, -1, -1, -1, -1; 310 | 1, 9, 0, 5, 10, 6, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1; 311 | 10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, -1, -1, -1, -1; 312 | 6, 1, 2, 6, 5, 1, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1; 313 | 1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, -1, -1, -1, -1; 314 | 8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, -1, -1, -1, -1; 315 | 7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9, -1; 316 | 3, 11, 2, 7, 8, 4, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1; 317 | 5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, -1, -1, -1, -1; 318 | 0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1; 319 | 9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6, -1; 320 | 8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, -1, -1, -1, -1; 321 | 5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11, -1; 322 | 0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7, -1; 323 | 6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, -1, -1, -1, -1; 324 | 10, 4, 9, 6, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 325 | 4, 10, 6, 4, 9, 10, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1; 326 | 10, 0, 1, 10, 6, 0, 6, 4, 0, -1, -1, -1, -1, -1, -1, -1; 327 | 8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, -1, -1, -1, -1; 328 | 1, 4, 9, 1, 2, 4, 2, 6, 4, -1, -1, -1, -1, -1, -1, -1; 329 | 3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, -1, -1, -1, -1; 330 | 0, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 331 | 8, 3, 2, 8, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1; 332 | 10, 4, 9, 10, 6, 4, 11, 2, 3, -1, -1, -1, -1, -1, -1, -1; 333 | 0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, -1, -1, -1, -1; 334 | 3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, -1, -1, -1, -1; 335 | 6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1, -1; 336 | 9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, -1, -1, -1, -1; 337 | 8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1, -1; 338 | 3, 11, 6, 3, 6, 0, 0, 6, 4, -1, -1, -1, -1, -1, -1, -1; 339 | 6, 4, 8, 11, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 340 | 7, 10, 6, 7, 8, 10, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1; 341 | 0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, -1, -1, -1, -1; 342 | 10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, -1, -1, -1, -1; 343 | 10, 6, 7, 10, 7, 1, 1, 7, 3, -1, -1, -1, -1, -1, -1, -1; 344 | 1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, -1, -1, -1, -1; 345 | 2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9, -1; 346 | 7, 8, 0, 7, 0, 6, 6, 0, 2, -1, -1, -1, -1, -1, -1, -1; 347 | 7, 3, 2, 6, 7, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 348 | 2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, -1, -1, -1, -1; 349 | 2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7, -1; 350 | 1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11, -1; 351 | 11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, -1, -1, -1, -1; 352 | 8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6, -1; 353 | 0, 9, 1, 11, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 354 | 7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, -1, -1, -1, -1; 355 | 7, 11, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 356 | 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 357 | 3, 0, 8, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 358 | 0, 1, 9, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 359 | 8, 1, 9, 8, 3, 1, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1; 360 | 10, 1, 2, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 361 | 1, 2, 10, 3, 0, 8, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1; 362 | 2, 9, 0, 2, 10, 9, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1; 363 | 6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, -1, -1, -1, -1; 364 | 7, 2, 3, 6, 2, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 365 | 7, 0, 8, 7, 6, 0, 6, 2, 0, -1, -1, -1, -1, -1, -1, -1; 366 | 2, 7, 6, 2, 3, 7, 0, 1, 9, -1, -1, -1, -1, -1, -1, -1; 367 | 1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, -1, -1, -1, -1; 368 | 10, 7, 6, 10, 1, 7, 1, 3, 7, -1, -1, -1, -1, -1, -1, -1; 369 | 10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, -1, -1, -1, -1; 370 | 0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, -1, -1, -1, -1; 371 | 7, 6, 10, 7, 10, 8, 8, 10, 9, -1, -1, -1, -1, -1, -1, -1; 372 | 6, 8, 4, 11, 8, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 373 | 3, 6, 11, 3, 0, 6, 0, 4, 6, -1, -1, -1, -1, -1, -1, -1; 374 | 8, 6, 11, 8, 4, 6, 9, 0, 1, -1, -1, -1, -1, -1, -1, -1; 375 | 9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, -1, -1, -1, -1; 376 | 6, 8, 4, 6, 11, 8, 2, 10, 1, -1, -1, -1, -1, -1, -1, -1; 377 | 1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, -1, -1, -1, -1; 378 | 4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, -1, -1, -1, -1; 379 | 10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3, -1; 380 | 8, 2, 3, 8, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1; 381 | 0, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 382 | 1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, -1, -1, -1, -1; 383 | 1, 9, 4, 1, 4, 2, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1; 384 | 8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, -1, -1, -1, -1; 385 | 10, 1, 0, 10, 0, 6, 6, 0, 4, -1, -1, -1, -1, -1, -1, -1; 386 | 4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3, -1; 387 | 10, 9, 4, 6, 10, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 388 | 4, 9, 5, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 389 | 0, 8, 3, 4, 9, 5, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1; 390 | 5, 0, 1, 5, 4, 0, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1; 391 | 11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, -1, -1, -1, -1; 392 | 9, 5, 4, 10, 1, 2, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1; 393 | 6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, -1, -1, -1, -1; 394 | 7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, -1, -1, -1, -1; 395 | 3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6, -1; 396 | 7, 2, 3, 7, 6, 2, 5, 4, 9, -1, -1, -1, -1, -1, -1, -1; 397 | 9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, -1, -1, -1, -1; 398 | 3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, -1, -1, -1, -1; 399 | 6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8, -1; 400 | 9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, -1, -1, -1, -1; 401 | 1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4, -1; 402 | 4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10, -1; 403 | 7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, -1, -1, -1, -1; 404 | 6, 9, 5, 6, 11, 9, 11, 8, 9, -1, -1, -1, -1, -1, -1, -1; 405 | 3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, -1, -1, -1, -1; 406 | 0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, -1, -1, -1, -1; 407 | 6, 11, 3, 6, 3, 5, 5, 3, 1, -1, -1, -1, -1, -1, -1, -1; 408 | 1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, -1, -1, -1, -1; 409 | 0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10, -1; 410 | 11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5, -1; 411 | 6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, -1, -1, -1, -1; 412 | 5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, -1, -1, -1, -1; 413 | 9, 5, 6, 9, 6, 0, 0, 6, 2, -1, -1, -1, -1, -1, -1, -1; 414 | 1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8, -1; 415 | 1, 5, 6, 2, 1, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 416 | 1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6, -1; 417 | 10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, -1, -1, -1, -1; 418 | 0, 3, 8, 5, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 419 | 10, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 420 | 11, 5, 10, 7, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 421 | 11, 5, 10, 11, 7, 5, 8, 3, 0, -1, -1, -1, -1, -1, -1, -1; 422 | 5, 11, 7, 5, 10, 11, 1, 9, 0, -1, -1, -1, -1, -1, -1, -1; 423 | 10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, -1, -1, -1, -1; 424 | 11, 1, 2, 11, 7, 1, 7, 5, 1, -1, -1, -1, -1, -1, -1, -1; 425 | 0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, -1, -1, -1, -1; 426 | 9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, -1, -1, -1, -1; 427 | 7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2, -1; 428 | 2, 5, 10, 2, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1; 429 | 8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, -1, -1, -1, -1; 430 | 9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, -1, -1, -1, -1; 431 | 9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2, -1; 432 | 1, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 433 | 0, 8, 7, 0, 7, 1, 1, 7, 5, -1, -1, -1, -1, -1, -1, -1; 434 | 9, 0, 3, 9, 3, 5, 5, 3, 7, -1, -1, -1, -1, -1, -1, -1; 435 | 9, 8, 7, 5, 9, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 436 | 5, 8, 4, 5, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1; 437 | 5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, -1, -1, -1, -1; 438 | 0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, -1, -1, -1, -1; 439 | 10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4, -1; 440 | 2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, -1, -1, -1, -1; 441 | 0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11, -1; 442 | 0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5, -1; 443 | 9, 4, 5, 2, 11, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 444 | 2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, -1, -1, -1, -1; 445 | 5, 10, 2, 5, 2, 4, 4, 2, 0, -1, -1, -1, -1, -1, -1, -1; 446 | 3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9, -1; 447 | 5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, -1, -1, -1, -1; 448 | 8, 4, 5, 8, 5, 3, 3, 5, 1, -1, -1, -1, -1, -1, -1, -1; 449 | 0, 4, 5, 1, 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 450 | 8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, -1, -1, -1, -1; 451 | 9, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 452 | 4, 11, 7, 4, 9, 11, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1; 453 | 0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, -1, -1, -1, -1; 454 | 1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, -1, -1, -1, -1; 455 | 3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4, -1; 456 | 4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, -1, -1, -1, -1; 457 | 9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3, -1; 458 | 11, 7, 4, 11, 4, 2, 2, 4, 0, -1, -1, -1, -1, -1, -1, -1; 459 | 11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, -1, -1, -1, -1; 460 | 2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, -1, -1, -1, -1; 461 | 9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7, -1; 462 | 3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10, -1; 463 | 1, 10, 2, 8, 7, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 464 | 4, 9, 1, 4, 1, 7, 7, 1, 3, -1, -1, -1, -1, -1, -1, -1; 465 | 4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, -1, -1, -1, -1; 466 | 4, 0, 3, 7, 4, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 467 | 4, 8, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 468 | 9, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 469 | 3, 0, 9, 3, 9, 11, 11, 9, 10, -1, -1, -1, -1, -1, -1, -1; 470 | 0, 1, 10, 0, 10, 8, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1; 471 | 3, 1, 10, 11, 3, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 472 | 1, 2, 11, 1, 11, 9, 9, 11, 8, -1, -1, -1, -1, -1, -1, -1; 473 | 3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, -1, -1, -1, -1; 474 | 0, 2, 11, 8, 0, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 475 | 3, 2, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 476 | 2, 3, 8, 2, 8, 10, 10, 8, 9, -1, -1, -1, -1, -1, -1, -1; 477 | 9, 10, 2, 0, 9, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 478 | 2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, -1, -1, -1, -1; 479 | 1, 10, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 480 | 1, 3, 8, 9, 1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 481 | 0, 9, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 482 | 0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1; 483 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 ] + 1; -------------------------------------------------------------------------------- /functions/fcngetvispose.m: -------------------------------------------------------------------------------- 1 | % Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | function [xhat,I2,features2,vp2] = fcngetvispose(h,xhat,rgb,I1,features1,vp1,i) 4 | options = optimset('TolX',1E-9,'TolFun',1E-9,'MaxIter',50,'Display','off','MaxFunEvals',5000,'LargeScale', 'off','GradObj','on'); tic 5 | if i==1 6 | return 7 | end 8 | rpy0 = fcnregsmall2(h,xhat,rgb,I1,i); 9 | %rpy0 = [0 0 0]; 10 | 11 | %I1 = im2double(rgb2gray(rgb(:,:,:,i-1))); 12 | I2 = im2double(rgb2gray(rgb(:,:,:,i))); 13 | %cornerPoints1 = detectHarrisFeatures(I1); 14 | cornerPoints2 = detectHarrisFeatures(I2); 15 | %[features1, vp1] = extractFeatures(I1, cornerPoints1, 'BlockSize', 21); %valid points 16 | [features2, vp2] = extractFeatures(I2, cornerPoints2, 'BlockSize', 21); 17 | [indexPairs, match_metric] = matchFeatures(features1, features2, 'MatchThreshold', 20); 18 | mp1 = vp1(indexPairs(:, 1), :); %matched points 1 19 | mp2 = vp2(indexPairs(:, 2), :); %matched points 2 20 | %cvexShowImagePair(I1, I2, 'Corners in left image', 'Corners in right image', 'SingleColor', cornerPoints1, cornerPoints2); 21 | 22 | mp1 = mp1.Location; 23 | mp2 = mp2.Location; 24 | 25 | r=sqrt(sum((mp1-mp2).^2,2)); 26 | j=r<100; 27 | mp1=mp1(j,:); mp2=mp2(j,:); 28 | for ii=1:6 29 | r=sqrt(sum((mp1-mp2).^2,2)); mu=mean(r); sigma=std(r); 30 | j=r<(mu+2*sigma) & r>(mu-2*sigma); 31 | mp1=mp1(j,:); mp2=mp2(j,:); 32 | end 33 | %cvexShowMatches(I1, I2, mp1, mp2, 'Matched points in I1', 'Matched points in I2'); 34 | 35 | if isempty(mp1) 36 | fprintf('WARNING: Not enough tie points to register.\n') 37 | xhat = [xhat(1:3), fcnW2BDCM2RPY( (fcnRPY2DCM_B2W(xhat(4:6))*fcnRPY2DCM_W2B(rpy0))' )]; 38 | return 39 | end 40 | 41 | % [y,i]=sort(rs); 42 | % inliers = i(1:round(numel(rs)*.5)); 43 | ip1 = mp1; 44 | ip2 = mp2; 45 | if size(mp1,1)>10 46 | try %#ok 47 | [fMatrix, inliers] = estimateFundamentalMatrix(mp1, mp2, 'Method', 'RANSAC', 'NumTrials', 1000, 'DistanceThreshold', 0.01, 'Confidence', 99.999); 48 | ip1 = mp1(inliers, :); 49 | ip2 = mp2(inliers, :); 50 | end 51 | end 52 | 53 | if isempty(ip1) 54 | fprintf('WARNING: Not enough tie points to register.\n') 55 | xhat = [xhat(1:3), fcnW2BDCM2RPY( (fcnRPY2DCM_B2W(xhat(4:6))*fcnRPY2DCM_W2B(rpy0))' )]; 56 | return 57 | end 58 | 59 | %cvexShowMatches(I1, I2, ip1, ip2, 'Inlier points in I1', 'Inlier points in I2'); 60 | plot(h(1),[ip1(:,1) ip2(:,1)]',[ip1(:,2) ip2(:,2)]','y-',ip1(:,1),ip1(:,2),'ro',ip2(:,1),ip2(:,2),'g+') 61 | %[t1, t2] = estimateUncalibratedRectification(fMatrix, ip1, ip2, size(I2)); 62 | %cvexShowStereoImages('Rectified image 1', 'Rectified image 2', I1, I2, ip1, ip2, fMatrix, t1, t2); 63 | %cvexShowMatches(I1, I2, ip1, ip2, 'Inlier points in I1', 'Inlier points in I2', 'RC', t1, t2); 64 | 65 | %I3 = reshape(fcnrange(xyzim1),480,640); I3=I3./max3(I3); 66 | %I4 = reshape(fcnrange(xyzi),480,640); I4=I4./max3(I4); 67 | %cvexShowMatches(I3, I4, ip1, ip2, 'Inlier points in I1', 'Inlier points in I2', 'RC', t1, t2); 68 | %cvexShowImagePair(I3, I4, 'Corners in left image', 'Corners in right image', 'SingleColor', ip1, ip2); 69 | 70 | ip1=double(ip1); ip2=double(ip2); 71 | a=[ip1(:,1)-641/2 481/2-ip1(:,2)]; b=[ip2(:,1)-641/2 481/2-ip2(:,2)]; 72 | %figure; plot(a(:,1),a(:,2),'ro',b(:,1),b(:,2),'g+',[a(:,1)'; b(:,1)'],[a(:,2)'; b(:,2)'],'y'); axis([-640/2 640/2 -480/2 480/2]) 73 | 74 | p2r = 0.00155898; %pixels to radians, ((57/640+43/480)/2)*pi/180 75 | asc=[ones(size(a,1),1), a(:,[2 1])*p2r]; bsc=[ones(size(b,1),1), b(:,[2 1])*p2r]; 76 | acc=fcnSC2CC(asc); bcc=fcnSC2CC(bsc); 77 | 78 | rpy=fminunc(@(rpy) fcnanglecost(acc,bcc,rpy), rpy0, options); 79 | fprintf('%9.3f%9.3f%9.3f rpy (%.2fs RANSAC corners)\n',rpy,toc) 80 | if any(abs(rpy)>.6) 81 | fprintf('WARNING: Visual registration failed, position jump detected (roll=%.0f, pitch=%.0f, yaw=%.0f)!\n',rpy*57.3) 82 | rpy = rpy0; 83 | end 84 | 85 | xhat = [xhat(1:3), fcnW2BDCM2RPY( (fcnRPY2DCM_B2W(xhat(4:6))*fcnRPY2DCM_W2B(rpy0))' )]; 86 | fprintf('%9.3f%9.3f%9.3f%9.3f%9.3f%9.3f (%.2fs RANSAC corners)\n',xhat,toc) 87 | end 88 | 89 | 90 | function [fx, gx] = fcnanglecost(a,b,x) 91 | dx = 1E-5; 92 | a1 = fcnxform(a,[0 0 0 x]); 93 | theta = fcnangle(a1,b); 94 | fx = sum(theta.^2); 95 | 96 | fg=zeros(3,1); 97 | for i=1:3 98 | x1=x; x1(i)=x1(i)+dx; 99 | a1 = fcnxform(a,[0 0 0 x1]); 100 | theta = fcnangle(a1,b); 101 | fg(i) = sum(theta.^2); 102 | end 103 | gx = (fg-fx)./dx; 104 | end 105 | 106 | 107 | -------------------------------------------------------------------------------- /functions/fcngetvispose3.m: -------------------------------------------------------------------------------- 1 | % Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | function xhat = fcngetvispose(xc,x1,xhat,i,rgb,xyzi,xyzim1,h) 4 | options = optimset('TolX',1E-9,'TolFun',1E-9,'MaxIter',50,'Display','off','MaxFunEvals',5000,'LargeScale', 'off','GradObj','on'); tic 5 | if i==1 6 | return 7 | end 8 | 9 | I1 = im2double(rgb2gray(rgb(:,:,:,i-1))); 10 | I2 = im2double(rgb2gray(rgb(:,:,:,i))); 11 | hcornerdet = vision.CornerDetector('MaximumCornerCount', 500, 'CornerThreshold', 0.00001, 'NeighborhoodSize', [21 21]); 12 | cornerPoints1 = step(hcornerdet, I1); 13 | cornerPoints2 = step(hcornerdet, I2); 14 | [features1, validPoints1] = extractFeatures(I1, cornerPoints1, 'BlockSize', 21); 15 | [features2, validPoints2] = extractFeatures(I2, cornerPoints2, 'BlockSize', 21); 16 | [indexPairs, match_metric] = matchFeatures(features1, features2, 'Metric', 'normxcorr', 'MatchThreshold', 20); 17 | mp1 = validPoints1(indexPairs(:, 1), :); %matched points 1 18 | mp2 = validPoints2(indexPairs(:, 2), :); %matched points 2 19 | %cvexShowImagePair(I1, I2, 'Corners in left image', 'Corners in right image', 'SingleColor', cornerPoints1, cornerPoints2); 20 | 21 | r=sqrt(sum((double(mp1)-double(mp2)).^2,2)); 22 | j=r<100; 23 | mp1=mp1(j,:); mp2=mp2(j,:); 24 | for i=1:6 25 | r=sqrt(sum((double(mp1)-double(mp2)).^2,2)); mu=mean(r); sigma=std(r); 26 | j=r<(mu+2*sigma) & r>(mu-2*sigma); 27 | mp1=mp1(j,:); mp2=mp2(j,:); 28 | %cvexShowMatches(I1, I2, mp1, mp2, 'Matched points in I1', 'Matched points in I2'); 29 | end 30 | 31 | % [y,i]=sort(rs); 32 | % inliers = i(1:round(numel(rs)*.5)); 33 | ip1 = mp1; 34 | ip2 = mp2; 35 | if size(mp1,1)>10 36 | try %#ok 37 | [fMatrix, inliers] = estimateFundamentalMatrix(mp1, mp2, 'Method', 'RANSAC', 'NumTrials', 1000, 'DistanceThreshold', 0.01, 'Confidence', 99.999); 38 | ip1 = mp1(inliers, :); 39 | ip2 = mp2(inliers, :); 40 | end 41 | end 42 | 43 | %cvexShowMatches(I1, I2, ip1, ip2, 'Inlier points in I1', 'Inlier points in I2'); 44 | plot(h(1),[ip1(:,1) ip2(:,1)]',[ip1(:,2) ip2(:,2)]','y-',ip1(:,1),ip1(:,2),'ro',ip2(:,1),ip2(:,2),'g+') 45 | %[t1, t2] = estimateUncalibratedRectification(fMatrix, ip1, ip2, size(I2)); 46 | %cvexShowStereoImages('Rectified image 1', 'Rectified image 2', I1, I2, ip1, ip2, fMatrix, t1, t2); 47 | %cvexShowMatches(I1, I2, ip1, ip2, 'Inlier points in I1', 'Inlier points in I2', 'RC', t1, t2); 48 | 49 | %I3 = reshape(fcnrange(xyzim1),480,640); I3=I3./max3(I3); 50 | %I4 = reshape(fcnrange(xyzi),480,640); I4=I4./max3(I4); 51 | %cvexShowMatches(I3, I4, ip1, ip2, 'Inlier points in I1', 'Inlier points in I2', 'RC', t1, t2); 52 | %cvexShowImagePair(I3, I4, 'Corners in left image', 'Corners in right image', 'SingleColor', ip1, ip2); 53 | 54 | 55 | %3D OPTIMIZATION 56 | ind=sub2ind([480 640],ip1(:,2),ip1(:,1)); a=xyzim1(ind,:); 57 | ind=sub2ind([480 640],ip2(:,2),ip2(:,1)); b=xyzi(ind,:); 58 | i=find(fcnrange(a,b)<10 & sum(a.*b,2)>0); a=a(i,:); b=b(i,:); 59 | %delete(get(h(5),'children')); 60 | %figure; plot3(a(:,1),a(:,2),b(:,3),'ro',b(:,1),b(:,2),b(:,3),'g+',[a(:,1)'; b(:,1)'],[a(:,2)'; b(:,2)'],[a(:,3)'; b(:,3)'],'y') 61 | 62 | if numel(i)<4 63 | fprintf('Visual registration failed, only %.0f points available!\n',numel(i)) 64 | return 65 | end 66 | a_w=fcnxform(a,xhat); 67 | xhatout=fminunc(@(x) fcncost(a_w,b,x), double(xhat), options); 68 | %x = fcnxform(x1,xhat); %plot3(h(6),x(:,1),x(:,2),x(:,3),'.g','markersize',5); 69 | 70 | r=fcnrange(xhat(1:3),xhatout(1:3)); 71 | droll = abs(xhat(4)-xhatout(4)); 72 | dpitch = abs(xhat(5)-xhatout(5)); 73 | dyaw = abs(xhat(6)-xhatout(6)); 74 | 75 | if r>.5 || droll>.5 || dpitch>.5 || dyaw>.5 76 | fprintf('Visual registration failed, position jump detected (%.1fm, roll=%.0f, pitch=%.0f, yaw=%.0f)!\n',r,droll*57.3,dpitch*57.3,dyaw*57.3) 77 | return 78 | end 79 | xhat = xhatout; 80 | fprintf('%9.3f%9.3f%9.3f%9.3f%9.3f%9.3f (%.2fs icp)\n',xhat,toc) 81 | end 82 | 83 | 84 | function [fx, gx] = fcncost(a,b,x) 85 | dx = 1E-5; 86 | b0=b; b=fcnxform(b,x); 87 | [~,rs] = rangec(a,b); 88 | fx = sum(rs); 89 | 90 | fg=zeros(6,1); 91 | for i=1:6 92 | x1=x; x1(i)=x1(i)+dx; 93 | b = fcnxform(b0,x1); 94 | rs=sum((b-a).^2,2); 95 | fg(i) = sum(rs); 96 | end 97 | gx = (fg-fx)./dx; 98 | end 99 | 100 | -------------------------------------------------------------------------------- /functions/fcnplotcam.m: -------------------------------------------------------------------------------- 1 | % Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | function [h] = fcnplotcam(ha,xhat,C) 4 | r2d = 180/pi; 5 | d2r = pi/180; 6 | 7 | r = .5; 8 | 9 | ov = ones(1,10); 10 | 11 | el = [ov*-21.5, linspace(-21.5, 21.5, 10), ov*21.5, linspace(21.5, -21.5, 10)]*d2r; 12 | az = [linspace(-28.5, 28.5, 10), ov*28.5, linspace(28.5, -28.5, 10), ov*-28.5]*d2r; 13 | 14 | x = [fcnSC2CC([ones(40,1)*r el' az']); [0 0 0]; fcnSC2CC([r 21.5*d2r -28.5*d2r]); [0 0 0]; fcnSC2CC([r 21.5*d2r 28.5*d2r]); [0 0 0]; fcnSC2CC([r -21.5*d2r 28.5*d2r])]; 15 | x=fcnxform(x,xhat); 16 | h=plot3(ha,x(:,1),x(:,2),x(:,3),'-','color',[0 0 0]); hold(ha,'on') 17 | 18 | 19 | %plot image 20 | [X,Y] = ndgrid(linspace(-28.5, 28.5, 640), linspace(21.5, -21.5, 480)); 21 | 22 | x = fcnSC2CCd([ones(640*480,1)*r, reshape(Y,[640*480 1]), reshape(X,[640*480 1])]); 23 | x=fcnxform(x,xhat); 24 | X=reshape(x(:,1),[640 480]); Y=reshape(x(:,2),[640 480]); Z=reshape(x(:,3),[640 480]); 25 | 26 | 27 | %h = [h, surf(ha,X',Y',Z',C,'edgecolor','none')]; 28 | 29 | h = [h, surf(ha,X',Y',Z',C,'edgecolor','none','facecolor','texture','cdata',C)]; 30 | 31 | end 32 | 33 | 34 | -------------------------------------------------------------------------------- /functions/fcnregsmall.m: -------------------------------------------------------------------------------- 1 | % Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | function rpy = fcnregsmall(hin,xhat,rgb,I1,i) 4 | options = optimset('TolX',1E-9,'TolFun',1E-9,'MaxIter',50,'Display','off','MaxFunEvals',5000,'LargeScale', 'off','GradObj','on'); tic 5 | if i==1 6 | return 7 | end 8 | p2r = 0.00155898; %pixels to radians, ((57/640+43/480)/2)*pi/180 9 | scale = 1/4; 10 | 11 | pf = 1; %plotflag 12 | 13 | I2 = im2double(rgb2gray(rgb(:,:,:,i))); 14 | I1s = imresize(I1, [480 640]*scale,'bilinear'); 15 | I2s = imresize(I2, [480 640]*scale,'bilinear'); 16 | a = I1s(30:end-29,30:end-29); 17 | if pf; h=fig(2,3); axes(h(1)); imshow(I1s); axes(h(2)); imshow(I2s); axes(h(3)); imshow(a); axis([-30 130 -30 90]); end 18 | imagesc(rgb(:,:,:,i-1),'Parent',hin(6)); set(hin(6),'ydir','reverse','cameraviewangle',4); axis(hin(6),'off','equal') 19 | 20 | xv = -29:1:29; 21 | fx = zeros(size(xv)); 22 | for j=1:59 23 | b = I2s(30+xv(j):end-29+xv(j),30:end-29); if pf; axes(h(4)); imshow(b); drawnow; end 24 | di = a-b; 25 | fx(j) = mean(mean(di.^2)); 26 | end 27 | [~, ix]=min(fx); ix=30-ix; pitch=ix*p2r/scale; 28 | plot(hin(2),fx); title(hin(2),sprintf('pitch=%.0f',pitch*57.3)) 29 | 30 | for j=1:59 31 | b = I2s(30:end-29,30+xv(j):end-29+xv(j)); if pf; axes(h(5)); imshow(b); drawnow; end 32 | di = a-b; 33 | fx(j) = mean(mean(di.^2)); 34 | end 35 | [~, ix]=min(fx); ix=ix-30; yaw=ix*p2r/scale; 36 | plot(hin(5),fx); title(hin(5),sprintf('yaw=%.0f',yaw*57.3)) 37 | 38 | rpy = [0 pitch yaw]; 39 | fprintf('%9.3f%9.3f%9.3f rpy (%.2fs small registration)\n',rpy,toc) 40 | end 41 | 42 | 43 | -------------------------------------------------------------------------------- /functions/fcnregsmall2.m: -------------------------------------------------------------------------------- 1 | % Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | function [rpy,xhat] = fcnregsmall2(hin,xhat,rgb,I1,i) 4 | options = optimset('TolX',1E-9,'TolFun',1E-9,'MaxIter',50,'Display','off','MaxFunEvals',5000,'LargeScale', 'off','GradObj','on'); tic 5 | if i==1 6 | return 7 | end 8 | p2r = 0.00155898; %pixels to radians, ((57/640+43/480)/2)*pi/180 9 | scale = 1/4; 10 | 11 | pf = 0; %plotflag 12 | 13 | I2 = im2double(rgb2gray(rgb(:,:,:,i))); 14 | I1s = imresize(I1, [480 640]*scale,'bilinear'); 15 | I2s = imresize(I2, [480 640]*scale,'bilinear'); 16 | a = I1s(30:end-29,30:end-29); 17 | if pf; h=fig(2,3); axes(h(1)); imshow(I1s); axes(h(2)); imshow(I2s); axes(h(3)); imshow(a); axis([-30 130 -30 90]); end 18 | imagesc(rgb(:,:,:,i-1),'Parent',hin(6)); set(hin(6),'ydir','reverse','cameraviewangle',3); title(hin(6),'previous frame','units','normalized','position',[.5 1]); axis(hin([2 5 6]),'off','equal') 19 | 20 | xv = -29:1:29; nxv = numel(xv); 21 | fx = zeros(nxv, nxv); 22 | for j=1:59 %up-down 23 | I2sj=I2s(30+xv(j):end-29+xv(j),:); 24 | for k=1:59 %left-right 25 | b = I2sj(:,30+xv(k):end-29+xv(k)); %if pf; axes(h(4)); imshow(b); drawnow; end 26 | di = a-b; 27 | fx(j,k) = sum(sum(di.^2)); 28 | end 29 | end 30 | [~,i] = min3(fx); 31 | rpy = [0 [-i(1)+30, i(2)-30]*p2r/scale]; 32 | pcolor(hin(2),fliplr(fx)); shading(hin(2),'flat'); title(hin(2),sprintf('pitch=%.0f, yaw=%.0f',rpy(2:3)*57.3)) 33 | 34 | 35 | if nargout==2 36 | xhat = [xhat(1:3), fcnW2BDCM2RPY( (fcnRPY2DCM_B2W(xhat(4:6))*fcnRPY2DCM_W2B(rpy))' )]; 37 | end 38 | 39 | % %DEBLUR PSF 40 | % PSF = fspecial('gaussian',20,20); 41 | % INITPSF = ones(size(PSF)); 42 | % WT = zeros(size(I2s)); 43 | % WT(20:end-19,20:end-19) = 1; 44 | % [J P] = deconvblind(I2s,INITPSF,20,.1,WT); 45 | % figure; imagesc(PSF) 46 | % figure; imshow(J) 47 | % figure; imshow(I2s) 48 | 49 | fprintf('%9.3f%9.3f%9.3f rpy (%.2fs small registration)\n',rpy,toc) 50 | end 51 | 52 | 53 | -------------------------------------------------------------------------------- /functions/fcnxform.m: -------------------------------------------------------------------------------- 1 | % Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | function [x2, C] = fcnxform(x,xhat) 4 | 5 | C = fcnRPY2DCM_W2B(xhat(4:6)); 6 | 7 | x1 = x*C; 8 | x2 = [x1(:,1)+xhat(1), x1(:,2)+xhat(2), x1(:,3)+xhat(3)]; %translation -------------------------------------------------------------------------------- /functions/rayintersection/amanatidesWooAlgorithm.m: -------------------------------------------------------------------------------- 1 | % Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | function amanatidesWooAlgorithm(origin, direction, grid3D, verbose) 4 | % A fast and simple voxel traversal algorithm through a 3D space partition (grid) 5 | % proposed by J. Amanatides and A. Woo (1987). 6 | % 7 | % Input: 8 | % origin. 9 | % direction. 10 | % grid3D: grid dimensions (nx, ny, nz, minBound, maxBound). 11 | % Author: 12 | % Jesús P. Mena-Chalco. 13 | 14 | if (verbose) 15 | figure; 16 | hold on; 17 | text(origin(1), origin(2), origin(3), 'origin'); 18 | plot3(origin(1), origin(2), origin(3), 'k.', 'MarkerSize', 15); 19 | quiver3(origin(1), origin(2), origin(3), direction(1), direction(2), direction(3), 30); 20 | 21 | vmin = grid3D.minBound'; 22 | vmax = grid3D.maxBound'; 23 | BoxVertices = [vmax(1) vmin(2) vmin(3); vmax(1) vmax(2) vmin(3); vmin(1) vmax(2) vmin(3); vmin(1) vmax(2) vmax(3); vmin(1) vmin(2) vmax(3); vmax(1) vmin(2) vmax(3); vmin; vmax ]; 24 | BoxFaces = [1 2 3 7; 1 2 8 6; 1 6 5 7; 7 5 4 3; 2 8 4 3; 8 6 5 4]; 25 | h = patch('Vertices',BoxVertices,'Faces',BoxFaces,'FaceColor','yellow'); 26 | set(h, 'FaceAlpha', 0.1); 27 | 28 | view(60,30); 29 | axis tight vis3d; 30 | xlabel('x'); 31 | ylabel('y'); 32 | zlabel('z'); 33 | grid on; 34 | end; 35 | 36 | [flag, tmin] = rayBoxIntersection(origin, direction, grid3D.minBound, grid3D.maxBound); 37 | 38 | if (flag==0) 39 | disp('\n The ray does not intersect the grid'); 40 | else 41 | if (tmin<0) 42 | tmin = 0; 43 | end; 44 | 45 | start = origin + tmin*direction; 46 | boxSize = grid3D.maxBound-grid3D.minBound; 47 | 48 | if (verbose) 49 | plot3(start(1), start(2), start(3), 'r.', 'MarkerSize', 15); 50 | end; 51 | 52 | x = floor( ((start(1)-grid3D.minBound(1))/boxSize(1))*grid3D.nx )+1; 53 | y = floor( ((start(2)-grid3D.minBound(2))/boxSize(2))*grid3D.ny )+1; 54 | z = floor( ((start(3)-grid3D.minBound(3))/boxSize(3))*grid3D.nz )+1; 55 | 56 | if (x==(grid3D.nx+1)); x=x-1; end; 57 | if (y==(grid3D.ny+1)); y=y-1; end; 58 | if (z==(grid3D.nz+1)); z=z-1; end; 59 | 60 | if (direction(1)>=0) 61 | tVoxelX = (x)/grid3D.nx; 62 | stepX = 1; 63 | else 64 | tVoxelX = (x-1)/grid3D.nx; 65 | stepX = -1; 66 | end; 67 | 68 | if (direction(2)>=0) 69 | tVoxelY = (y)/grid3D.ny; 70 | stepY = 1; 71 | else 72 | tVoxelY = (y-1)/grid3D.ny; 73 | stepY = -1; 74 | end; 75 | 76 | if (direction(3)>=0) 77 | tVoxelZ = (z)/grid3D.nz; 78 | stepZ = 1; 79 | else 80 | tVoxelZ = (z-1)/grid3D.nz; 81 | stepZ = -1; 82 | end; 83 | 84 | voxelMaxX = grid3D.minBound(1) + tVoxelX*boxSize(1); 85 | voxelMaxY = grid3D.minBound(2) + tVoxelY*boxSize(2); 86 | voxelMaxZ = grid3D.minBound(3) + tVoxelZ*boxSize(3); 87 | 88 | tMaxX = tmin + (voxelMaxX-start(1))/direction(1); 89 | tMaxY = tmin + (voxelMaxY-start(2))/direction(2); 90 | tMaxZ = tmin + (voxelMaxZ-start(3))/direction(3); 91 | 92 | voxelSizeX = boxSize(1)/grid3D.nx; 93 | voxelSizeY = boxSize(2)/grid3D.ny; 94 | voxelSizeZ = boxSize(3)/grid3D.nz; 95 | 96 | tDeltaX = voxelSizeX/abs(direction(1)); 97 | tDeltaY = voxelSizeY/abs(direction(2)); 98 | tDeltaZ = voxelSizeZ/abs(direction(3)); 99 | 100 | while ( (x<=grid3D.nx)&&(x>=1) && (y<=grid3D.ny)&&(y>=1) && (z<=grid3D.nz)&&(z>=1) ) 101 | 102 | if (verbose) 103 | fprintf('\nIntersection: voxel = [%d %d %d]', [x y z]); 104 | 105 | t1 = [(x-1)/grid3D.nx, (y-1)/grid3D.ny, (z-1)/grid3D.nz ]'; 106 | t2 = [ (x)/grid3D.nx, (y)/grid3D.ny, (z)/grid3D.nz ]'; 107 | 108 | vmin = (grid3D.minBound + t1.*boxSize)'; 109 | vmax = (grid3D.minBound + t2.*boxSize)'; 110 | 111 | smallBoxVertices = [vmax(1) vmin(2) vmin(3); vmax(1) vmax(2) vmin(3); vmin(1) vmax(2) vmin(3); vmin(1) vmax(2) vmax(3); vmin(1) vmin(2) vmax(3); vmax(1) vmin(2) vmax(3); vmin; vmax ]; 112 | smallBoxFaces = [1 2 3 7; 1 2 8 6; 1 6 5 7; 7 5 4 3; 2 8 4 3; 8 6 5 4]; 113 | 114 | h = patch('Vertices', smallBoxVertices, 'Faces', smallBoxFaces, 'FaceColor', 'blue', 'EdgeColor', 'white'); 115 | set(h,'FaceAlpha',0.2); 116 | end; 117 | 118 | % ---------------------------------------------------------- % 119 | % check if voxel [x,y,z] contains any intersection with the ray 120 | % 121 | % if ( intersection ) 122 | % break; 123 | % end; 124 | % ---------------------------------------------------------- % 125 | 126 | if (tMaxX < tMaxY) 127 | if (tMaxX < tMaxZ) 128 | x = x + stepX; 129 | tMaxX = tMaxX + tDeltaX; 130 | else 131 | z = z + stepZ; 132 | tMaxZ = tMaxZ + tDeltaZ; 133 | end; 134 | else 135 | if (tMaxY < tMaxZ) 136 | y = y + stepY; 137 | tMaxY = tMaxY + tDeltaY; 138 | else 139 | z = z + stepZ; 140 | tMaxZ = tMaxZ + tDeltaZ; 141 | end; 142 | end; 143 | end; 144 | end; 145 | end 146 | -------------------------------------------------------------------------------- /functions/rayintersection/rayBoxIntersection.m: -------------------------------------------------------------------------------- 1 | % Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | function [flag ,tmin] = rayBoxIntersection(origin, direction, vmin, vmax) 4 | % Ray/box intersection using the Smits' algorithm 5 | % 6 | % Input: 7 | % origin. 8 | % direction. 9 | % box = (vmin,vmax) 10 | % Output: 11 | % flag: (0) Reject, (1) Intersect. 12 | % tmin: distance from the ray origin. 13 | % Author: 14 | % Jesus Mena 15 | 16 | if (direction(1) >= 0) 17 | tmin = (vmin(1) - origin(1)) / direction(1); 18 | tmax = (vmax(1) - origin(1)) / direction(1); 19 | else 20 | tmin = (vmax(1) - origin(1)) / direction(1); 21 | tmax = (vmin(1) - origin(1)) / direction(1); 22 | end 23 | 24 | if (direction(2) >= 0) 25 | tymin = (vmin(2) - origin(2)) / direction(2); 26 | tymax = (vmax(2) - origin(2)) / direction(2); 27 | else 28 | tymin = (vmax(2) - origin(2)) / direction(2); 29 | tymax = (vmin(2) - origin(2)) / direction(2); 30 | end 31 | 32 | if ( (tmin > tymax) || (tymin > tmax) ) 33 | flag = 0; 34 | tmin = -1; 35 | return; 36 | end 37 | 38 | if (tymin > tmin) 39 | tmin = tymin; 40 | end 41 | 42 | if (tymax < tmax) 43 | tmax = tymax; 44 | end 45 | 46 | if (direction(3) >= 0) 47 | tzmin = (vmin(3) - origin(3)) / direction(3); 48 | tzmax = (vmax(3) - origin(3)) / direction(3); 49 | else 50 | tzmin = (vmax(3) - origin(3)) / direction(3); 51 | tzmax = (vmin(3) - origin(3)) / direction(3); 52 | end 53 | 54 | 55 | if ((tmin > tzmax) || (tzmin > tmax)) 56 | flag = 0; 57 | tmin = -1; 58 | return; 59 | end 60 | 61 | if (tzmin > tmin) 62 | tmin = tzmin; 63 | end 64 | 65 | if (tzmax < tmax) 66 | tmax = tzmax; 67 | end 68 | 69 | % if( (tmin < t1) && (tmax > t0) ) 70 | flag = 1; 71 | % else 72 | % flag = 0; 73 | % tmin = -1; 74 | % end; 75 | end 76 | -------------------------------------------------------------------------------- /functions/rayintersection/test.m: -------------------------------------------------------------------------------- 1 | % Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | % A fast and simple voxel traversal algorithm through a 3D space partition (grid) 4 | % proposed by J. Amanatides and A. Woo (1987). 5 | 6 | % % Test Nro. 1 7 | origin = [15, 15, 15]'; 8 | direction = [-0.3, -0.5, -0.7]'; 9 | 10 | % % Test Nro. 2 11 | %origin = [-8.5, -4.5, -9.5]'; 12 | %direction = [0.5, 0.5, 0.7]'; 13 | 14 | % Grid: dimensions 15 | grid3D.nx = 300; 16 | grid3D.ny = 300; 17 | grid3D.nz = 300; 18 | grid3D.minBound = [-10, -10, -20]'; 19 | grid3D.maxBound = [ 10, 10, 20]'; 20 | 21 | verbose = 0; 22 | amanatidesWooAlgorithm(origin, direction, grid3D, verbose); 23 | -------------------------------------------------------------------------------- /preview.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ultralytics/kinect/686f383369b225a246861129774f0973aeeff134/preview.jpg -------------------------------------------------------------------------------- /results.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ultralytics/kinect/686f383369b225a246861129774f0973aeeff134/results.jpg -------------------------------------------------------------------------------- /results.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ultralytics/kinect/686f383369b225a246861129774f0973aeeff134/results.mp4 --------------------------------------------------------------------------------