void Stroke::Resample(float iSampling) { //cerr << "old size :" << strokeVerticesSize() << endl; if (iSampling == 0) return; if (iSampling >= _sampling) return; _sampling = iSampling; // Resample... //real curvilinearLength = 0.0f; vertex_container newVertices; real t = 0.0f; const real limit = 0.99; StrokeVertex *newVertex = NULL; StrokeInternal::StrokeVertexIterator it = strokeVerticesBegin(); StrokeInternal::StrokeVertexIterator next = it; ++next; StrokeInternal::StrokeVertexIterator itend = strokeVerticesEnd(); while ((it != itend) && (next != itend)) { newVertices.push_back(&(*it)); Vec2r a((it)->getPoint()); Vec2r b((next)->getPoint()); Vec2r vec_tmp(b - a); real norm_var = vec_tmp.norm(); if (norm_var <= _sampling) { //curvilinearLength += norm_var; ++it; ++next; continue; } //curvilinearLength += _sampling; t = _sampling / norm_var; while (t < limit) { newVertex = new StrokeVertex(&(*it), &(*next), t); //newVertex->setCurvilinearAbscissa(curvilinearLength); newVertices.push_back(newVertex); t = t + _sampling / norm_var; } ++it; ++next; } // add last: if ((it != itend) && (next == itend)/* && (t == 0.f)*/) newVertices.push_back(&(*it)); _Vertices.clear(); _Vertices = newVertices; newVertices.clear(); if (_rep) { delete _rep; _rep = new StrokeRep(this); } }
int Stroke::Resample(int iNPoints) { int NPointsToAdd = iNPoints - strokeVerticesSize(); if (NPointsToAdd <= 0) return 0; StrokeInternal::StrokeVertexIterator it = strokeVerticesBegin(); StrokeInternal::StrokeVertexIterator next = it; ++next; StrokeInternal::StrokeVertexIterator itend = strokeVerticesEnd(); vertex_container newVertices; real t = 0.0f; StrokeVertex *newVertex = NULL; vector<StrokeSegment> strokeSegments; int N = 0; float meanlength = 0; int nsegments = 0; while ((it != itend) && (next != itend)) { Vec2r a((it)->getPoint()); Vec2r b((next)->getPoint()); Vec2r vec_tmp(b - a); real norm_var = vec_tmp.norm(); int numberOfPointsToAdd = (int)floor(NPointsToAdd * norm_var / _Length); float csampling = norm_var / (float)(numberOfPointsToAdd + 1); strokeSegments.push_back(StrokeSegment(it, next, norm_var, numberOfPointsToAdd, csampling)); N += numberOfPointsToAdd; meanlength += norm_var; ++nsegments; ++it; ++next; } meanlength /= (float)nsegments; // if we don't have enough points let's resample finer some segments bool checkEveryone = false; bool resampled; while (N < NPointsToAdd) { resampled = false; for (vector<StrokeSegment>::iterator s = strokeSegments.begin(), send = strokeSegments.end(); s != send; ++s) { if (s->_sampling == 0.0f) continue; if (s->_resampled == false) { if ((!checkEveryone) && (s->_length < meanlength)) continue; // resample s->_n = s->_n + 1; s->_sampling = s->_length / (float)(s->_n + 1); s->_resampled = resampled = true; N++; if (N == NPointsToAdd) break; } } if (checkEveryone && !resampled) break; checkEveryone = true; } if (N < NPointsToAdd) { // fatal error, likely because _Length is inconsistent with the stroke length computed with the // vertices return -1; } // actually resample: for (vector<StrokeSegment>::iterator s = strokeSegments.begin(), send = strokeSegments.end(); s != send; ++s) { newVertices.push_back(&(*(s->_begin))); if (s->_sampling < _sampling) _sampling = s->_sampling; t = s->_sampling / s->_length; for (int i = 0; i < s->_n; ++i) { newVertex = new StrokeVertex(&(*(s->_begin)), &(*(s->_end)), t); newVertices.push_back(newVertex); t += s->_sampling / s->_length; } it = s->_begin; next = s->_end; } // add last: ++it; ++next; if ((it != itend) && (next == itend) /* && (t == 0.0f)*/) newVertices.push_back(&(*it)); int newsize = newVertices.size(); if (newsize != iNPoints) cerr << "Warning: incorrect points number" << endl; _Vertices.clear(); _Vertices = newVertices; newVertices.clear(); return 0; }