From 6d4676fc1d15f301aafcb5ff189ba09452f86443 Mon Sep 17 00:00:00 2001
From: Sergi Hernandez Juan <shernand@iri.upc.edu>
Date: Wed, 25 Oct 2023 11:21:09 +0200
Subject: [PATCH] Stop the spline generation if the number of points gets too
 big (generation diverges for some reason)

---
 src/g2_spline.cpp | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/g2_spline.cpp b/src/g2_spline.cpp
index f3cdbd9..2cd609b 100644
--- a/src/g2_spline.cpp
+++ b/src/g2_spline.cpp
@@ -587,14 +587,16 @@ void CG2Spline::generate(double resolution,unsigned int iterations)
   double n1_c_start,n1_2_k_s_start,n1_s_start,n1_2_k_c_start;
   double n2_c_end,n2_2_k_s_end,n2_s_end,n2_2_k_c_end;
   double u,pow_u,new_length,last_length;
-  unsigned int i;
+  unsigned int i,max_num_points;
 
   this->resolution=resolution;
   last_length=0.0;
   new_length=sqrt(pow(this->end.x-this->start.x,2)+pow(this->end.y-this->start.y,2));
+  max_num_points=10.0*(new_length/resolution);
+  this->num_points=0;
   if(new_length>this->resolution)
   {
-    while(iterations>0 && fabs(new_length-last_length)>this->resolution)
+    while(iterations>0 && fabs(new_length-last_length)>this->resolution && this->num_points<max_num_points)
     {
       last_length=new_length;
       this->num_points=ceil(last_length/resolution);
@@ -680,14 +682,16 @@ void CG2Spline::generate(double resolution,double length,unsigned int iterations
   double n1_c_start,n1_2_k_s_start,n1_s_start,n1_2_k_c_start;
   double n2_c_end,n2_2_k_s_end,n2_s_end,n2_2_k_c_end;
   double u,pow_u,new_length,last_length;
-  unsigned int i;
+  unsigned int i,max_num_points;
 
   this->resolution=resolution;
   last_length=0.0;
   new_length=length;
+  max_num_points=10.0*(length/resolution);
+  this->num_points=0;
   if(new_length>this->resolution)
   {
-    while(iterations>0 && fabs(new_length-last_length)>this->resolution)
+    while(iterations>0 && fabs(new_length-last_length)>this->resolution && this->num_points<max_num_points)
     {
       last_length=new_length;
       this->num_points=ceil(last_length/resolution);
-- 
GitLab