@@ -171,9 +171,16 @@ class CPointsMap :
171171 m_z[index] = z;
172172 }
173173
174- /* * The virtual method for \a insertPoint() *without* calling
175- * mark_as_modified() */
176- virtual void insertPointFast (float x, float y, float z = 0 ) = 0;
174+ /* * Low-level method for \a insertPoint() *without* calling mark_as_modified().
175+ * Note that for derived classes having more per-point fields, you must ensure adding those fields
176+ * after this to keep the length of all vectors consistent.
177+ */
178+ void insertPointFast (float x, float y, float z)
179+ {
180+ m_x.push_back (x);
181+ m_y.push_back (y);
182+ m_z.push_back (z);
183+ }
177184
178185 /* * Get all the data fields for one point as a vector: depending on the
179186 * implementation class this can be [X Y Z] or [X Y Z R G B], etc...
@@ -200,9 +207,10 @@ class CPointsMap :
200207 this ->clear ();
201208 this ->reserve (N);
202209 this ->registerPointFieldsFrom (obj);
210+ const auto ctx = this ->prepareForInsertPointsFrom (obj);
203211 for (size_t i = 0 ; i < N; i++)
204212 {
205- insertPointFrom (obj, i);
213+ insertPointFrom (obj, i, ctx );
206214 }
207215 }
208216
@@ -798,28 +806,56 @@ class CPointsMap :
798806 return allAdded;
799807 }
800808
809+ /* * Insert context for insertPointFrom().
810+ * \sa prepareForInsertPointsFrom()
811+ */
812+ struct InsertCtx
813+ {
814+ // For XYZ
815+ const mrpt::aligned_std_vector<float >* xs_src = nullptr ;
816+ const mrpt::aligned_std_vector<float >* ys_src = nullptr ;
817+ const mrpt::aligned_std_vector<float >* zs_src = nullptr ;
818+
819+ // Optional field mappings (only fields present in both maps)
820+ struct FloatFieldMapping
821+ {
822+ const mrpt::aligned_std_vector<float >* src_buf = nullptr ;
823+ mrpt::aligned_std_vector<float >* dst_buf = nullptr ;
824+ };
825+ std::vector<FloatFieldMapping> float_fields;
826+
827+ struct UInt16FieldMapping
828+ {
829+ const mrpt::aligned_std_vector<uint16_t >* src_buf = nullptr ;
830+ mrpt::aligned_std_vector<uint16_t >* dst_buf = nullptr ;
831+ };
832+ std::vector<UInt16FieldMapping> uint16_fields;
833+ };
834+
835+ /* * Prepare efficient data structures for repeated insertion from another point map with
836+ * insertPointFrom() */
837+ InsertCtx prepareForInsertPointsFrom (const CPointsMap& source) const ;
838+
801839 /* * Generic method to copy *all* applicable point properties from
802840 * one map to another, e.g. timestamp, intensity, etc.
803841 * \note Before calling this in a loop, make sure of calling registerPointFieldsFrom()
804842 */
805- void insertPointFrom (const mrpt::maps:: CPointsMap& source, size_t sourcePointIndex)
843+ void insertPointFrom (const CPointsMap& source, size_t sourcePointIndex, const InsertCtx& ctx )
806844 {
807- const auto i = sourcePointIndex; // shortcut
808- // mandatory fields: XYZ
809- const auto & xs = source.getPointsBufferRef_x ();
810- const auto & ys = source.getPointsBufferRef_y ();
811- const auto & zs = source.getPointsBufferRef_z ();
812- insertPointFast (xs[i], ys[i], zs[i]);
813-
814- // Optional fields: only if they already exist on both maps:
815- for (const auto & f : this ->getPointFieldNames_float_except_xyz ())
845+ const size_t i = sourcePointIndex;
846+ insertPointFast ((*ctx.xs_src )[i], (*ctx.ys_src )[i], (*ctx.zs_src )[i]);
847+
848+ // Optional fields
849+ for (auto & f : ctx.float_fields )
816850 {
817- insertPointField_float (f, source. getPointField_float (i, f) );
851+ f. dst_buf -> push_back ((*f. src_buf )[i] );
818852 }
819- for (const auto & f : this ->getPointFieldNames_uint16 ())
853+
854+ for (auto & f : ctx.uint16_fields )
820855 {
821- insertPointField_uint16 (f, source. getPointField_uint16 (i, f) );
856+ f. dst_buf -> push_back ((*f. src_buf )[i] );
822857 }
858+
823859 mark_as_modified ();
824860 }
825861
@@ -1171,7 +1207,9 @@ class CPointsMap :
11711207 clear ();
11721208 reserve (N);
11731209 for (size_t i = 0 ; i < N; ++i)
1210+ {
11741211 this ->insertPointFast (cloud.points [i].x , cloud.points [i].y , cloud.points [i].z );
1212+ }
11751213 }
11761214
11771215 /* * @} */
0 commit comments