An example of using std::vector to optimize the animation of point clouds

Source: Internet
Author: User

1. Preparation

Use Std::vector to know the points:

(1) Memory contiguous container, sort of like an array

(2) Insert and delete elements are slower than std::list-because data migration

(3) Adding elements may cause memory allocations and data migrations.

2. Questions

ANYCAD::API::P Ointcloudnode uses floatlist and std::vector<float> to store some column points [x0, y0, z0, x1, y1, Z1, ...]:

void Setpoints (const floatlist &buffer)

If you want to display n points, you need 3n of length:


In order to simulate the motion trajectory of an object in space, which is a series of points, how can the dynamic drawing be realized efficiently?



3. The programme

The most basic approach:

Variables defined:

Pointcloudnode m_pointcoud;std::_vector<float> m_points;

Each time you call Push_back add point:

void Onaddpoint (x, Y, z) {     m_points.push (x);     M_points.push (y);     M_points.push (z);     M_pointcloud.setpoints (m_points);         Render ();}

"Optimization 1": To reduce the number of points displayed to cause memory problems and efficiency problems, you can limit how many points to display

int max_point3_count = Max_point_count * 3;

void OnAddPointV1 (x, Y, z) {   if (m_points.size () > Max_point3_count)   {        m_points.erase (M_points.begin () );        M_points.erase (M_points.begin ());        M_points.erase (M_points.begin ());    }     M_points.push (x);     M_points.push (y);     M_points.push (z);     M_pointcloud.setpoints (m_points);         Render ();}

What is the problem introduced by onAddPointV1?


Optimization 2: Avoid allocating memory every time the vector is allocated, specifying the initial memory size of the vector

M_points.reserve (Max_point3_count);

Optimization 3: Avoid data migration due to deletion of head element engine

The new points are placed at the end of the team or the team head, and the results are the same for the display. So you can overwrite the "expired" point.

int m_totalcount = 0;

void OnAddPointV3 (x, Y, z) {      ++m_totalcount;      if (M_totalcount <= max_point_count)      {               m_points.push_back (x);               M_points.push_back (y);               M_points.push_back (z);      }      else      {            int pos = (m_totalcount% 100-1) * 3;           M_points[pos] = x;           M_points[pos+1] = y;           M_POINTS[POS+2] = Z;      }       M_pointcloud.setpoints (m_points);            Render ();}

4 Summary

Slightly





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

An example of using std::vector to optimize the animation of point clouds

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.