Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_102_PointCloud_Property

Attaching per-point attributes (here, colors) to a PointCloud using named properties.

Overview

Building on Tutorial_101_PointCloud, this console tutorial shows how to store extra data alongside each point. We synthesize the same 10 x 10 grid of points, give every point a random color, then read back both the coordinates and the colors and print them. No viewer is opened; the goal is to understand Easy3D's property system, which underpins almost all geometry processing in the library.

The technique

A property (a.k.a. a property map) is a parallel array: one value per mesh element, addressed by the element's handle. Rather than a struct-of-fields per point, Easy3D keeps each attribute in its own contiguous array, so adding or removing an attribute never disturbs the others and iteration stays cache-friendly. Properties are identified by a unique string name. By convention a prefix says which element type the property lives on: "v:" for vertices, "e:" for edges, "f:" for faces (the prefix is optional but recommended). The coordinates themselves are just a built-in vertex property named "v:point", so a position is accessed exactly like any other attribute. Creating a property whose name is already in use triggers a warning, which is why every property must be given a distinct name.

Easy3D API

Running it

Console-only. It first prints "point cloud has 100 points", then one line per point giving its index, xyz coordinates, and random color, e.g. "index: 0, xyz: ..., color: ...". The cloud is heap-allocated here and explicitly deleted at the end to release its memory.

1
27#include <easy3d/core/point_cloud.h>
28#include <easy3d/core/random.h>
29#include <easy3d/util/initializer.h>
30
75using namespace easy3d;
76
77int main(int argc, char **argv) {
78 // initialize Easy3D.
79 initialize();
80
81 // Seed the random number generator. A fixed seed (0) makes the "random" colors below
82 // reproducible, so the printed output is identical on every run.
83 srand(0);
84
85 // Create a point cloud.
86 auto cloud = new PointCloud;
87
88 // Add some points. Here we add 100 points on a 10*10 grid.
89 for (int i = -5; i < 5; ++i) {
90 for (int j = -5; j < 5; ++j)
91 cloud->add_vertex(
92 vec3(static_cast<float>(i), static_cast<float>(j), 0)); // z = 0: all points are on XY plane.
93 }
94 std::cout << "point cloud has " << cloud->n_vertices() << " points" << std::endl;
95
96 // In Easy3D, all actual 3D data and related information are stored as
97 // per-element (e.g., vertex, edge, and face) properties. A model can have
98 // multiple properties and the properties can be accessed by their names.
99 // So it is important to give each property a unique name. Easy3D will pop
100 // up a warning if you attempt to create a property with an already used name.
101
102 // Let's add a random color to each point. We give this property a name
103 // "v:color". Here "v:" is optional, and it just indicates that this property
104 // is defined on the vertices. You will later see properties can be defined
105 // one edges (use "e:") and faces (use "f:") for surface meshes.
106 auto colors = cloud->add_vertex_property<vec3>("v:color");
107 for (auto v : cloud->vertices()) // iterate over all vertices
108 colors[v] = random_color(); // assign a random color to point 'v'
109
110 // You can use the get_vertex_property() function to access the named properties.
111 // Below I show you how to access the xyz coordinates and color of each point.
112 // Here we simply print the coordinates and colors.
113
114 // The point coordinates are stored as a per-point property 'v:point'.
115 auto points = cloud->get_vertex_property<vec3>("v:point");
116 for (auto v : cloud->vertices())
117 std::cout << "index: " << v.idx() << ", xyz: " << points[v] << ", color: " << colors[v] << std::endl;
118
119 // delete the point cloud (i.e., release memory)
120 delete cloud;
121
122 return EXIT_SUCCESS;
123}
A data structure for point clouds.
Definition point_cloud.h:46
Vertex add_vertex(const vec3 &p)
Add a new vertex with position p.
Definition point_cloud.cpp:169
Definition collider.cpp:182
See also
PointCloud, vec3, Tutorial_101_PointCloud, Tutorial_106_SurfaceMesh_Property