@@ -228,9 +228,8 @@ synfig::find_closest_point(const ValueBase &bline, const Point &pos, Real radius
228228Real
229229synfig::std_to_hom (const ValueBase &bline, Real pos, bool index_loop, bool bline_loop)
230230{
231- Real loops = index_loop ? floor (pos) : 0.0 ;
231+ const Real loops = index_loop ? floor (pos) : 0.0 ;
232232 pos -= loops;
233- assert (approximate_greater_or_equal (pos, 0.0 ));
234233
235234 // trivial cases
236235 if (approximate_less_or_equal (pos, Real (0 )))
@@ -239,31 +238,31 @@ synfig::std_to_hom(const ValueBase &bline, Real pos, bool index_loop, bool bline
239238 return loops + 1 ;
240239
241240 const std::vector<BLinePoint> list (bline.get_list_of (BLinePoint ()));
242- size_t size = list.size ();
241+ const size_t size = list.size ();
243242 if (size == 0 )
244243 return loops;
245- size_t count = bline_loop? size : size - 1 ;
244+ const size_t count = bline_loop? size : size - 1 ;
246245 if (count < 1 )
247246 return loops + pos;
248247
249248 // Calculate the lengths and the total length
250249 std::vector<Real> lengths;
251- Real bline_total_length = bline_length (list, bline_loop, &lengths);
250+ const Real bline_total_length = bline_length (list, bline_loop, &lengths);
252251 // If the total length of the bline is zero return pos
253- if (approximate_equal (bline_total_length, 0.0 ))
252+ if (approximate_equal (bline_total_length, 0.0 ))
254253 return pos;
255- size_t from_vertex = size_t (pos*count);
254+ const size_t from_vertex = size_t (pos*count);
256255 // Calculate the partial length until the bezier that holds the current
257256 Real partial_length = 0 ;
258257 std::vector<Real>::const_iterator length_iter (lengths.begin ());
259- for (size_t i= 0 ; i < from_vertex; ++i, ++length_iter)
258+ for (size_t i = 0 ; i < from_vertex; ++i, ++length_iter)
260259 partial_length += *length_iter;
261260 // Calculate the remaining length of the position over current bezier
262261 // Setup the curve of the current bezier.
263- size_t next_vertex = (from_vertex + 1 ) % size;
264- const BLinePoint & blinepoint0 = list[from_vertex];
265- const BLinePoint & blinepoint1 = list[next_vertex];
266- hermite<Vector> curve (blinepoint0.get_vertex (), blinepoint1.get_vertex (),
262+ const size_t next_vertex = (from_vertex + 1 ) % size;
263+ const BLinePoint& blinepoint0 = list[from_vertex];
264+ const BLinePoint& blinepoint1 = list[next_vertex];
265+ const hermite<Vector> curve (blinepoint0.get_vertex (), blinepoint1.get_vertex (),
267266 blinepoint0.get_tangent2 (), blinepoint1.get_tangent1 ());
268267 // add the distance on the bezier we are on.
269268 partial_length += curve.find_distance (0.0 , pos*count - from_vertex);
@@ -274,7 +273,7 @@ synfig::std_to_hom(const ValueBase &bline, Real pos, bool index_loop, bool bline
274273Real
275274synfig::hom_to_std (const ValueBase &bline, Real pos, bool index_loop, bool bline_loop)
276275{
277- Real loops = index_loop ? floor (pos) : 0.0 ;
276+ const Real loops = index_loop ? floor (pos) : 0.0 ;
278277 pos -= loops;
279278 assert (approximate_greater_or_equal (pos, 0.0 ));
280279
@@ -285,59 +284,58 @@ synfig::hom_to_std(const ValueBase &bline, Real pos, bool index_loop, bool bline
285284 return loops + 1 ;
286285
287286 const std::vector<BLinePoint> list (bline.get_list_of (BLinePoint ()));
288- size_t size = list.size ();
287+ const size_t size = list.size ();
289288 if (size == 0 )
290289 return loops;
291- size_t count = bline_loop? size : size - 1 ;
290+ const size_t count = bline_loop? size : size - 1 ;
292291 if (count < 1 )
293292 return loops + pos;
294293
295294 // Calculate the lengths and the total length
296295 std::vector<Real> lengths;
297- Real bline_total_length= bline_length (bline, bline_loop,&lengths);
296+ const Real bline_total_length = bline_length (bline, bline_loop, &lengths);
298297 // Calculate the my partial length (the length where pos is)
299- Real target_length = pos * bline_total_length;
298+ const Real target_length = pos * bline_total_length;
300299 std::vector<Real>::const_iterator length_iter (lengths.begin ());
301300 // Find the previous bezier where we pos is placed and the sum
302301 // of lengths to it (cumulative_length)
303302 // also remember the bezier's length where we stop
304303 Real cumulative_length = 0 ;
305304 size_t from_vertex = 0 ;
306305 Real segment_length = 0 ;
307- while (target_length > cumulative_length && length_iter != lengths.end ())
308- {
306+ while (approximate_greater (target_length, cumulative_length) && length_iter != lengths.end ()) {
309307 segment_length = *length_iter;
310308 cumulative_length += segment_length;
311309
312310 ++length_iter;
313311 ++from_vertex;
314312 }
315- // correct the iters and partial length in case we passed over
316- if (cumulative_length > target_length)
317- {
313+ if (approximate_equal (target_length, cumulative_length)) {
314+ return loops + Real (from_vertex) / count;
315+ } else {
316+ // correct the iters and partial length in case we passed over
318317 --length_iter;
319318 cumulative_length -= *length_iter;
320319 --from_vertex;
321320 }
322321 // set up the curve
323- const BLinePoint & blinepoint0 = list[from_vertex];
324- const BLinePoint & blinepoint1 = list[(from_vertex+1 ) % size];
325- hermite<Vector> curve (blinepoint0.get_vertex (), blinepoint1.get_vertex (),
322+ const BLinePoint& blinepoint0 = list[from_vertex];
323+ const BLinePoint& blinepoint1 = list[(from_vertex+1 ) % size];
324+ const hermite<Vector> curve (blinepoint0.get_vertex (), blinepoint1.get_vertex (),
326325 blinepoint0.get_tangent2 (), blinepoint1.get_tangent1 ());
327326 // Find the solution to which is the standard position which matches the current
328327 // homogeneous position
329328 // Secant method: http://en.wikipedia.org/wiki/Secant_method
330329 Real sn (0.0 ); // the standard position on current bezier
331330 Real sn1 (0.0 ), sn2 (1.0 );
332- Real t0 ((target_length-cumulative_length)/segment_length); // the homogeneous position on the current bezier
331+ const Real t0 ((target_length-cumulative_length)/segment_length); // the homogeneous position on the current bezier
333332 int iterations=0 ;
334333 const int max_iterations=100 ;
335334 const Real max_error (0.00001 );
336335 Real error;
337336 Real fsn1 (t0-curve.find_distance (0.0 ,sn1)/segment_length);
338337 Real fsn2 (t0-curve.find_distance (0.0 ,sn2)/segment_length);
339- do
340- {
338+ do {
341339 sn=sn1-fsn1*((sn1-sn2)/(fsn1-fsn2));
342340 Real fsn=t0-curve.find_distance (0.0 , sn)/segment_length;
343341 sn2=sn1;
@@ -360,22 +358,22 @@ synfig::bline_length(const ValueBase &bline, bool bline_loop, std::vector<Real>
360358 const std::vector<BLinePoint> list (bline.get_list_of (BLinePoint ()));
361359 if (list.empty ())
362360 return 0 ;
363- size_t max_vertex_index ( list.size ()) ;
364- if (!bline_loop) max_vertex_index--;
365- if (max_vertex_index < 1 ) return Real ();
361+ const size_t max_vertex_index = bline_loop ? list. size () : list.size () - 1 ;
362+ if ( max_vertex_index < 1 )
363+ return Real ();
366364
367365 if (lengths)
368366 lengths->reserve (max_vertex_index);
369367
370368 // Calculate the lengths and the total length
371369 Real total_length = 0 ;
372- for (size_t i0 = 0 ; i0 < max_vertex_index; ++i0) {
373- size_t i1 = (i0 + 1 )% list.size ();
374- const BLinePoint & blinepoint0 = list[i0];
375- const BLinePoint & blinepoint1 = list[i1];
376- hermite<Vector> curve (blinepoint0.get_vertex (), blinepoint1.get_vertex (),
370+ for (size_t i0 = 0 ; i0 < max_vertex_index; ++i0) {
371+ const size_t i1 = (i0 + 1 ) % list.size ();
372+ const BLinePoint& blinepoint0 = list[i0];
373+ const BLinePoint& blinepoint1 = list[i1];
374+ const hermite<Vector> curve (blinepoint0.get_vertex (), blinepoint1.get_vertex (),
377375 blinepoint0.get_tangent2 (), blinepoint1.get_tangent1 ());
378- Real l= curve.length ();
376+ const Real l = curve.length ();
379377 if (lengths) lengths->push_back (l);
380378 total_length+=l;
381379 }
0 commit comments