303: Plane extraction

This tutorial demonstrates how to extract basic geometric primitives (planes, cylinders, cones, spheres, and toruses) from 3D point clouds using the Easy3D library.

In this example, we will:

  • Load a 3D point cloud from a file.

  • Configure the PrimitivesRansac class for primitive extraction.

  • Extract planes as geometric primitives.

  • Visualize the extracted planar segments.

Loaded point cloud with 100000 points.
Number of planes extracted: 52
Plane 0:
  Index: 0
  Position: (-0.004067, -0.267421, -0.221142)
  Normal: (0.093712, 0.797201, 0.596396)
  Vertices (20 out of 13748 shown): [5761, 5777, 5790, 5754, 5756, 5806, 5747, 5751, 5786, 5744, 13060, 13051, 13052, 13057, 30584, 30597, 30579, 30639, 30590, 30636]
Plane 1:
  Index: 1
  Position: (0.350143, 0.047361, -0.165126)
  Normal: (0.915110, 0.171135, -0.365084)
  Vertices (20 out of 12232 shown): [74455, 22111, 25732, 25731, 25735, 74680, 25748, 74673, 74668, 25761, 25763, 25728, 74684, 74674, 74699, 74702, 74704, 53126, 74686, 74688]
Plane 2:
  Index: 2
  Position: (0.200181, -0.250927, 0.215577)
  Normal: (0.399862, -0.581834, 0.708223)
  Vertices (20 out of 9013 shown): [47536, 47683, 47681, 47677, 47666, 47660, 47658, 47655, 47647, 47643, 14158, 47679, 14150, 47641, 47649, 47645, 47638, 14132, 47669, 2748]
Plane 3:
  Index: 3
  Position: (0.059659, 0.210875, 0.199141)
  Normal: (0.090679, 0.792480, 0.603119)
  Vertices (20 out of 8867 shown): [9545, 9543, 9535, 9529, 9524, 9519, 9509, 9501, 9492, 9468, 9465, 9437, 62750, 47535, 62907, 62802, 62857, 109, 14139, 62795]
Plane 4:
  Index: 4
  Position: (-0.283744, -0.108790, 0.123758)
  Normal: (0.912489, 0.165476, -0.374142)
  Vertices (20 out of 8966 shown): [54875, 79703, 171, 28900, 28851, 79761, 28838, 28832, 28845, 28843, 28846, 28841, 93651, 977, 79858, 99873, 988, 79846, 93609, 29337]
Plane 5:
  Index: 5
  Position: (-0.147552, 0.100702, -0.375682)
  Normal: (0.402555, -0.585083, 0.704008)
  Vertices (20 out of 5920 shown): [65501, 65561, 17541, 17528, 17542, 17510, 17602, 12199, 12056, 12106, 12220, 428, 52095, 17563, 65548, 65492, 65474, 17612, 17544, 17565]
Plane 6:
  Index: 6
  Position: (-0.084087, 0.363997, -0.138143)
  Normal: (0.404578, -0.580406, 0.706714)
  Vertices (20 out of 3446 shown): [29095, 147, 79916, 79922, 79909, 79920, 29089, 29070, 29074, 79924, 79918, 79905, 79907, 29085, 79911, 29093, 29091, 29086, 190, 16761]
Plane 7:
  Index: 7
  Position: (-0.063579, 0.337112, -0.275055)
  Normal: (0.093818, 0.818134, 0.567323)
  Vertices (20 out of 2146 shown): [7356, 7312, 7358, 7310, 7363, 16738, 18251, 7345, 7331, 7317, 65006, 7315, 7334, 7347, 7329, 7338, 7325, 7333, 396, 7354]
Plane 8:
  Index: 8
  Position: (-0.364697, -0.156527, 0.304755)
  Normal: (0.910446, 0.184272, -0.370313)
  Vertices (20 out of 1131 shown): [90781, 90789, 91753, 91644, 90758, 91674, 91663, 90730, 90840, 90831, 90822, 90797, 90794, 90786, 90857, 91704, 90745, 90747, 90783, 90735]
Plane 9:
  Index: 9
  Position: (-0.462292, 0.099770, -0.016113)
  Normal: (0.602943, -0.528789, 0.597363)
  Vertices (20 out of 1542 shown): [74569, 56435, 10635, 10770, 74603, 56307, 10704, 56457, 10655, 10775, 10603, 10660, 10646, 10609, 10751, 10760, 10682, 10667, 10612, 10611]
Plane 10:
  Index: 10
  Position: (-0.401492, 0.012815, 0.090468)
  Normal: (0.205694, -0.598489, 0.774275)
  Vertices (20 out of 1486 shown): [26229, 26157, 26189, 26194, 26060, 26092, 95100, 26110, 26163, 26171, 26241, 26246, 26083, 26208, 26199, 26090, 26131, 86564, 26139, 26057]
Plane 11:
  Index: 11
  Position: (-0.327262, -0.095660, 0.224496)
  Normal: (0.583184, -0.533718, 0.612406)
  Vertices (20 out of 1484 shown): [39352, 74869, 75003, 74999, 74880, 74996, 74992, 74981, 75027, 74990, 74986, 75028, 74917, 75023, 74988, 74879, 75000, 74924, 74913, 25987]
Plane 12:
  Index: 12
  Position: (0.261728, -0.220430, 0.301507)
  Normal: (0.972566, 0.062124, -0.224177)
  Vertices (20 out of 1341 shown): [32580, 32547, 32544, 28087, 78957, 32538, 32593, 28083, 30459, 78961, 28090, 78863, 28075, 28089, 78933, 78907, 78970, 78969, 78876, 78866]
Plane 13:
  Index: 13
  Position: (-0.266424, -0.182396, 0.331008)
  Normal: (0.205344, -0.594935, 0.777101)
  Vertices (20 out of 1462 shown): [11574, 11642, 11589, 11639, 11508, 11587, 11613, 11572, 11531, 11604, 11552, 11640, 11620, 11529, 11598, 11686, 11526, 11514, 11591, 11592]
Plane 14:
  Index: 14
  Position: (0.128948, -0.248593, 0.354581)
  Normal: (0.820512, 0.271190, -0.503206)
  Vertices (20 out of 1327 shown): [50410, 50334, 27285, 77531, 77537, 77521, 27277, 77525, 27279, 77523, 27274, 50456, 50434, 50376, 50412, 50338, 50445, 50415, 50443, 50385]
Plane 15:
  Index: 15
  Position: (0.332804, 0.180604, 0.318677)
  Normal: (0.410973, -0.335302, 0.847746)
  Vertices (20 out of 695 shown): [14314, 14306, 14305, 14351, 61072, 61111, 14398, 14360, 14373, 14215, 14249, 14217, 14244, 14271, 14218, 14284, 14268, 14369, 14330, 14344]
Plane 16:
  Index: 16
  Position: (-0.267034, 0.212684, -0.320896)
  Normal: (0.973556, 0.049180, -0.223093)
  Vertices (20 out of 1135 shown): [32845, 32830, 37824, 32817, 83759, 41833, 41771, 41733, 33100, 33154, 33189, 32822, 32848, 32841, 825, 41765, 41772, 41831, 94536, 94612]
Plane 17:
  Index: 17
  Position: (-0.501853, 0.039342, 0.065385)
  Normal: (0.910350, 0.184601, -0.370386)
  Vertices (20 out of 1087 shown): [65917, 65966, 65947, 65975, 65907, 65933, 17921, 17857, 65930, 65924, 65888, 66021, 65953, 17836, 17933, 17876, 65885, 17904, 17840, 66014]
Plane 18:
  Index: 18
  Position: (-0.319055, -0.252465, 0.108431)
  Normal: (-0.090629, -0.796401, -0.597939)
  Vertices (20 out of 561 shown): [80506, 80512, 80516, 29358, 29374, 29378, 29369, 29373, 10333, 10425, 10479, 10415, 10500, 29381, 10516, 10386, 10446, 10452, 10362, 10572]
Plane 19:
  Index: 19
  Position: (-0.214359, 0.270158, -0.410128)
  Normal: (0.398801, -0.596749, 0.696310)
  Vertices (20 out of 1094 shown): [64075, 64105, 64135, 64102, 64064, 938, 64119, 64124, 64115, 64095, 64086, 64106, 32332, 14164, 14166, 32326, 14169, 14162, 64996, 32329]
Plane 20:
  Index: 20
  Position: (0.376264, 0.245953, 0.228238)
  Normal: (0.904358, 0.374517, -0.204628)
  Vertices (20 out of 655 shown): [71966, 71818, 71753, 23904, 71930, 71974, 71789, 23635, 52198, 23822, 23819, 23800, 175, 71957, 71884, 71867, 71863, 71816, 71928, 71922]
Plane 21:
  Index: 21
  Position: (0.222616, -0.275157, 0.375863)
  Normal: (0.404236, -0.574104, 0.712038)
  Vertices (20 out of 1167 shown): [8153, 8471, 8183, 54573, 8174, 8189, 8187, 8181, 8190, 8179, 8192, 54388, 54375, 8166, 54372, 54363, 8168, 8185, 54359, 54399]
Plane 22:
  Index: 22
  Position: (-0.110009, 0.225808, -0.406225)
  Normal: (0.847174, 0.267992, -0.458777)
  Vertices (20 out of 923 shown): [84555, 33244, 33242, 33239, 33226, 84548, 84566, 33221, 33247, 33241, 33237, 33235, 33229, 1021, 17473, 33251, 33230, 33232, 42102, 832]
Plane 23:
  Index: 23
  Position: (0.122841, 0.488314, -0.052864)
  Normal: (-0.364497, 0.770827, -0.522463)
  Vertices (20 out of 698 shown): [53766, 413, 59539, 59498, 59542, 59571, 13335, 59557, 59523, 59479, 59540, 59458, 59550, 59544, 59554, 59508, 59515, 59530, 59591, 59502]
Plane 24:
  Index: 24
  Position: (0.029590, 0.117863, 0.440758)
  Normal: (0.410733, -0.335455, 0.847802)
  Vertices (20 out of 681 shown): [21080, 21014, 15639, 63400, 15620, 15616, 70648, 63337, 63384, 15627, 63370, 63403, 63343, 63309, 15648, 15624, 15642, 15641, 15673, 15676]
Plane 25:
  Index: 25
  Position: (-0.179036, 0.427971, 0.069256)
  Normal: (-0.365230, 0.770901, -0.521842)
  Vertices (20 out of 669 shown): [87975, 88002, 88010, 87966, 38178, 88058, 38159, 87995, 88096, 88065, 88060, 88013, 87988, 87978, 88015, 88092, 87973, 88078, 88046, 87955]
Plane 26:
  Index: 26
  Position: (-0.199703, 0.108992, -0.442033)
  Normal: (0.088037, 0.782750, 0.616078)
  Vertices (20 out of 688 shown): [565, 388, 84235, 84246, 31915, 82927, 31882, 82933, 82903, 31900, 31886, 31913, 31911, 31929, 31878, 31934, 31884, 31898, 31923, 31939]
Plane 27:
  Index: 27
  Position: (0.076024, 0.183865, 0.351883)
  Normal: (0.902728, 0.377030, -0.207199)
  Vertices (20 out of 655 shown): [70579, 70651, 70633, 70540, 70546, 70531, 70665, 70548, 70576, 70521, 70600, 70575, 70537, 70644, 70595, 70692, 70498, 70604, 70691, 70500]
Plane 28:
  Index: 28
  Position: (0.233300, 0.453140, -0.024539)
  Normal: (0.904830, 0.373990, -0.203504)
  Vertices (20 out of 668 shown): [55194, 55215, 55137, 55128, 55237, 55261, 55273, 55286, 55289, 55267, 55082, 55199, 55085, 55188, 55230, 9793, 55208, 55132, 55235, 55184]
Plane 29:
  Index: 29
  Position: (0.267958, 0.275963, 0.199098)
  Normal: (-0.362656, 0.770716, -0.523906)
  Vertices (20 out of 667 shown): [52141, 515, 200, 52330, 52200, 52148, 52233, 52283, 52264, 52261, 52134, 52161, 52268, 52243, 52272, 52203, 52111, 52315, 52194, 52146]
Plane 30:
  Index: 30
  Position: (0.078124, 0.423546, 0.036942)
  Normal: (0.857116, -0.030940, -0.514194)
  Vertices (20 out of 670 shown): [60755, 60886, 13276, 13279, 13320, 60862, 60695, 60767, 13300, 60699, 60919, 60850, 60914, 60893, 60920, 60808, 60827, 60913, 60864, 60712]
Plane 31:
  Index: 31
  Position: (-0.114043, 0.333057, 0.185486)
  Normal: (0.406446, -0.346258, 0.845522)
  Vertices (20 out of 669 shown): [37398, 89381, 89308, 89327, 89397, 89512, 89262, 89282, 89434, 89432, 89314, 89311, 89369, 89423, 89344, 89506, 89284, 89353, 89323, 89339]
Plane 32:
  Index: 32
  Position: (-0.079115, 0.153673, 0.414256)
  Normal: (0.859234, -0.031443, -0.510615)
  Vertices (20 out of 661 shown): [37128, 37143, 37047, 37013, 37017, 37114, 37063, 37162, 749, 37118, 37094, 37124, 20844, 20870, 735, 37134, 37121, 37206, 37131, 88630]
Plane 33:
  Index: 33
  Position: (0.222256, 0.211199, 0.290183)
  Normal: (0.857315, -0.030936, -0.513861)
  Vertices (20 out of 654 shown): [24380, 31556, 24303, 72074, 72088, 31547, 31544, 24341, 24255, 24210, 24368, 24342, 24231, 24392, 24272, 24260, 24286, 24257, 24331, 24229]
Plane 34:
  Index: 34
  Position: (-0.224326, 0.365431, 0.156955)
  Normal: (0.859255, -0.031059, -0.510603)
  Vertices (20 out of 657 shown): [742, 65091, 65077, 65093, 475, 16918, 65074, 65059, 65080, 29761, 29764, 65070, 65063, 65210, 65162, 65169, 65082, 65084, 65191, 21467]
Plane 35:
  Index: 35
  Position: (-0.036551, 0.216304, 0.322247)
  Normal: (-0.362944, 0.770584, -0.523900)
  Vertices (20 out of 663 shown): [63975, 63982, 63862, 63981, 63954, 63949, 22790, 63867, 63977, 63968, 63890, 63989, 63979, 63869, 63916, 63930, 63876, 63924, 63882, 63897]
Plane 36:
  Index: 36
  Position: (0.190099, 0.392895, 0.063947)
  Normal: (0.406270, -0.346703, 0.845424)
  Vertices (20 out of 679 shown): [55680, 53563, 55712, 53468, 53590, 53687, 53570, 7663, 53639, 53637, 53478, 53501, 53673, 53664, 53566, 53592, 53671, 53472, 53688, 53618]
Plane 37:
  Index: 37
  Position: (-0.413400, 0.211070, 0.149681)
  Normal: (0.045758, 0.786745, 0.615580)
  Vertices (20 out of 552 shown): [35223, 35233, 35056, 86756, 86587, 86559, 35352, 86615, 35372, 86628, 35320, 86744, 35142, 86695, 86704, 86641, 86625, 86740, 86631, 86761]
Plane 38:
  Index: 38
  Position: (-0.072166, 0.397985, 0.095758)
  Normal: (0.902709, 0.377337, -0.206720)
  Vertices (20 out of 652 shown): [6868, 6783, 6847, 52755, 6797, 52895, 52899, 52817, 52765, 52741, 52689, 90386, 52844, 52861, 52776, 52864, 52761, 52682, 52742, 52749]
Plane 39:
  Index: 39
  Position: (-0.006956, -0.141946, 0.491580)
  Normal: (0.904592, 0.207077, -0.372601)
  Vertices (20 out of 569 shown): [26423, 26434, 26437, 26439, 26481, 75729, 26447, 26417, 26489, 26419, 26454, 26428, 26491, 26479, 26458, 26440, 26485, 26432, 26451, 26452]
Plane 40:
  Index: 40
  Position: (-0.310483, -0.290960, 0.158460)
  Normal: (0.090911, 0.796065, 0.598344)
  Vertices (20 out of 539 shown): [81451, 81454, 76988, 30309, 30311, 76995, 76993, 76909, 76970, 76900, 76972, 76897, 76987, 76892, 76979, 76932, 76895, 77004, 76975, 76899]
Plane 41:
  Index: 41
  Position: (0.004541, 0.236967, 0.434992)
  Normal: (0.086133, 0.790254, 0.606696)
  Vertices (20 out of 377 shown): [68985, 69017, 15645, 15691, 22737, 22714, 22711, 22802, 22743, 22745, 22746, 22792, 22784, 22779, 22774, 22758, 70727, 70707, 70748, 22756]
Plane 42:
  Index: 42
  Position: (0.226314, 0.501722, 0.058387)
  Normal: (-0.086930, -0.790238, -0.606603)
  Vertices (20 out of 367 shown): [21498, 21500, 21630, 71781, 21632, 21526, 52343, 21545, 21550, 21603, 21616, 21625, 21600, 21537, 21568, 21581, 21585, 21605, 21518, 70047]
Plane 43:
  Index: 43
  Position: (0.048284, 0.318216, -0.380371)
  Normal: (0.129623, 0.727435, 0.673823)
  Vertices (20 out of 771 shown): [59728, 59660, 59710, 59731, 85521, 85455, 85501, 85513, 85485, 85499, 34470, 85460, 59658, 59654, 59692, 59643, 59694, 59727, 59662, 59691]
Plane 44:
  Index: 44
  Position: (-0.272770, 0.013624, 0.391542)
  Normal: (0.045698, 0.786804, 0.615508)
  Vertices (20 out of 540 shown): [39204, 39328, 39140, 39136, 39241, 39054, 39079, 39218, 39126, 39348, 39119, 39283, 23149, 39230, 39174, 39334, 39161, 39027, 39117, 39209]
Plane 45:
  Index: 45
  Position: (0.211009, -0.075999, 0.441015)
  Normal: (0.107662, 0.763062, 0.637295)
  Vertices (20 out of 489 shown): [59800, 59807, 218, 59809, 15842, 15839, 26821, 26804, 15834, 8158, 8156, 63851, 15846, 15836, 63854, 63845, 63856, 63843, 15848, 15844]
Plane 46:
  Index: 46
  Position: (0.176138, -0.386643, 0.212095)
  Normal: (0.090081, 0.796582, 0.597782)
  Vertices (20 out of 495 shown): [31390, 31431, 63545, 31438, 31339, 31406, 31432, 31419, 32047, 622, 82390, 76607, 32032, 83050, 32043, 82448, 82452, 32036, 31395, 32041]
Plane 47:
  Index: 47
  Position: (-0.028644, -0.163849, 0.568931)
  Normal: (0.386254, -0.570931, 0.724463)
  Vertices (20 out of 453 shown): [120, 29826, 29839, 29848, 1089, 5524, 99196, 26493, 45039, 45035, 81057, 45030, 45042, 99198, 29847, 81059, 81037, 45033, 45028, 45036]
Plane 48:
  Index: 48
  Position: (0.025201, 0.374388, -0.394916)
  Normal: (0.394916, -0.585351, 0.708100)
  Vertices (20 out of 420 shown): [83679, 95030, 8014, 8003, 8004, 8006, 8009, 8011, 8012, 95081, 997, 95025, 95022, 42172, 95049, 42158, 42199, 95021, 95019, 95063]
Plane 49:
  Index: 49
  Position: (0.164277, 0.508346, 0.058630)
  Normal: (0.087236, 0.790482, 0.606241)
  Vertices (20 out of 350 shown): [9816, 55721, 55652, 55657, 55699, 59855, 55690, 55692, 55730, 55719, 55711, 55649, 55697, 55666, 55659, 55661, 9777, 55300, 9819, 9835]
Plane 50:
  Index: 50
  Position: (-0.065637, -0.234315, 0.454815)
  Normal: (-0.198177, 0.978731, -0.053025)
  Vertices (20 out of 385 shown): [99620, 99669, 99679, 99546, 99617, 99625, 99673, 26863, 99611, 99675, 99607, 99604, 99602, 99682, 99643, 26843, 99639, 99635, 89545, 89597]
Plane 51:
  Index: 51
  Position: (-0.140331, 0.448244, 0.180509)
  Normal: (0.086045, 0.791502, 0.605079)
  Vertices (20 out of 357 shown): [52814, 38214, 90405, 90419, 90425, 38145, 90492, 38184, 38207, 38188, 90467, 88076, 427, 38216, 38223, 38227, 88000, 38143, 90412, 90423]
Plane extraction result saved to file extracted_planes.bvg

0

# sphinx_gallery_thumbnail_path = '_static/sphx_glr_tutorial_303_plane_extraction_thumb.png'

# -------------------------------------------------------------------------------
# Adding Easy3D Python Bindings to the System Path
# -------------------------------------------------------------------------------
# This is required if the bindings are not installed via `pip` but are located in
# a local build directory. For building and installing Python bindings of Easy3D,
# please refer to: https://github.com/LiangliangNan/Easy3D/blob/main/README.md
# -------------------------------------------------------------------------------
import sys
sys.path.append("../../cmake-build-release/lib/python")  # Update this path to point to your Easy3D build directory.

# -----------------------------------------------------------------------------
# Importing the Easy3D Library
# -----------------------------------------------------------------------------
# Import the Easy3D library, which provides tools for 3D data processing,
# primitive extraction, and visualization.

import easy3d

# -----------------------------------------------------------------------------
# Initializing Easy3D
# -----------------------------------------------------------------------------
# Before using any Easy3D functionality, we must initialize the library.
# The 'True' parameter enables detailed logging, useful for debugging and
# understanding the library's internal processes.

easy3d.initialize(True)

# -----------------------------------------------------------------------------
# Loading a Point Cloud
# -----------------------------------------------------------------------------
# The Easy3D library supports various point cloud formats (e.g., .bin, .xyz).
# Here, we load a sample point cloud file provided by Easy3D. Replace the path
# with your own dataset as needed.

pointcloud_file = easy3d.resource_directory() + "/data/polyhedron.bin"  # Update if necessary
point_cloud = easy3d.PointCloudIO.load(pointcloud_file)  # Load the point cloud
print(f"Loaded point cloud with {point_cloud.n_vertices()} points.")  # Print point count

# -----------------------------------------------------------------------------
# Creating a PrimitivesRansac Instance
# -----------------------------------------------------------------------------
# The PrimitivesRansac class is responsible for detecting geometric primitives
# (e.g., planes, cylinders) in a point cloud using RANSAC (Random Sample Consensus).
# We configure this instance to extract planes in this example.

ransac = easy3d.PrimitivesRansac()  # Create a PrimitivesRansac instance

# -----------------------------------------------------------------------------
# Configuring Primitive Types for Extraction
# -----------------------------------------------------------------------------
# Specify the types of primitives to extract. In this example, we add planes.
# Other available types include cylinders, cones, spheres, and toruses.

ransac.add_primitive_type(easy3d.PrimitivesRansac.PLANE)  # Add plane extraction

# -----------------------------------------------------------------------------
# Detecting Primitives in the Point Cloud
# -----------------------------------------------------------------------------
# The detect() method applies the RANSAC algorithm to extract primitives.
# Parameters:
# - point_cloud: The input point cloud.
# - min_support: Minimum number of points required for a primitive.
# - dist_threshold: Distance threshold relative to the bounding box's max dimension.
# - bitmap_resolution: Bitmap resolution relative to the bounding box width.
# - normal_threshold: Cosine of the maximum normal deviation.
# - overlook_probability: Probability of overlooking a primitive.
ransac.detect(point_cloud,
              min_support=200,
              dist_threshold=0.005,
              bitmap_resolution=0.02,
              normal_threshold=0.8,
              overlook_probability=0.001,
              )

# -----------------------------------------------------------------------------
# Retrieving Extracted Planes
# -----------------------------------------------------------------------------
# Extracted primitives are retrieved using getter methods such as `get_planes`.
# Each plane object contains attributes like:
# - `primitive_index`: Unique index for the plane.
# - `position`: A point on the plane.
# - `normal`: The normal vector of the plane.
# - `vertices`: Indices of points belonging to the plane.
# Note: The extracted primitives are also stored as properties in the point cloud self:
#   - "v:primitive_type"  (one of PLANE, SPHERE, CYLINDER, CONE, TORUS, and UNKNOWN)
#   - "v:primitive_index" (-1, 0, 1, 2...). -1 meaning a vertex does not belong to any
#     primitive (thus its primitive_type must be UNKNOWN).
planes = ransac.get_planes()  # Retrieve detected planes
print(f"Number of planes extracted: {len(planes)}")  # Print the number of detected planes

for i, plane in enumerate(planes):
    print(f"Plane {i}:")
    print(f"  Index: {plane.primitive_index}")
    print(f"  Position: {plane.position}")
    print(f"  Normal: {plane.normal}")

    # Print up to 20 vertex indices (for demonstration purposes)
    vertex_count = len(plane.vertices)
    print(f"  Vertices ({min(vertex_count, 20)} out of {vertex_count} shown): {plane.vertices[:20]}")  # e.g., [5788, 5801, 5777, 5796, ... ]

# -----------------------------------------------------------------------------
# Optional: Detecting Cylinders
# -----------------------------------------------------------------------------
# To detect other primitives (e.g., cylinders), configure the RANSAC instance
# and retrieve the results. Uncomment the following code to detect cylinders.

# ransac.add_primitive_type(easy3d.PrimitivesRansac.CYLINDER)
# ransac.detect(point_cloud, 200, 0.005, 0.02, 0.8, 0.001)
# cylinders = ransac.get_cylinders()
# print(f"Number of cylinders extracted: {len(cylinders)}")
# for i, cylinder in enumerate(cylinders):
#     print(f"Cylinder {i}:")
#     print(f"  Index: {cylinder.primitive_index}")
#     print(f"  Position: {cylinder.position}")
#     print(f"  Direction: {cylinder.direction}")
#     print(f"  Radius: {cylinder.radius}")
#     # Print up to 20 vertex indices (for demonstration purposes)
#     vertex_count = len(cylinder.vertices)
#     print(f"  Vertices ({min(vertex_count, 20)} out of {vertex_count} shown): {cylinder.vertices[:20]}")  # e.g., [5788, 5801, 5777, 5796, ... ]

# -----------------------------------------------------------------------------
# Saving the extraction result into a file
# -----------------------------------------------------------------------------

# After detection, the point cloud model already carries the result in the "v:primitive_type"
# and "v:primitive_index" properties, which can be saved into a file in the 'bvg'
# (Binary Vertex Group) format. The ASCII 'vg' format also works but is slower.
# See here for more information about these file formats:
#       https://github.com/LiangliangNan/PolyFit/blob/main/data/ReadMe-data.md
# Alternatively, the extracted primitives can also be saved into the 'ply' format,
# where the type and index of a primitive are recorded by the "v:primitive_type"
# and "v:primitive_index" properties, respectively.
result_file_name = "extracted_planes.bvg"   # You can try ".vg" or ".ply"
if easy3d.PointCloudIO.save(result_file_name, point_cloud):
    print(f'Plane extraction result saved to file {result_file_name}')
else:
    print(f'Failed to save plane extraction result to file {result_file_name}')

# -----------------------------------------------------------------------------
# Visualizing the extracted planar segments
# -----------------------------------------------------------------------------

viewer = easy3d.Viewer("Easy3D Viewer - Plane extraction")  # Create a viewer
viewer.set_usage("") # Optional. Just to hide the lengthy manual in console window
viewer.add_model(point_cloud)  # Add the point cloud to the viewer

# The extracted primitives are also stored in int type vertex property "v:primitive_index".
# We can color the point cloud by this property.
point_cloud.renderer().color_by_segmentation("v:primitive_index")

# Run the viewer
viewer.run()

Total running time of the script: (0 minutes 5.372 seconds)

Gallery generated by Sphinx-Gallery