From 9f1583af6008770b6bd7da80c2fb6ef23308a9a1 Mon Sep 17 00:00:00 2001 From: Nathan Glenn Date: Thu, 19 Sep 2024 23:34:09 -0500 Subject: [PATCH] Fix windows build failure by avoiding VLA's I guess the compiler on Windows is more strict about disallowing variable-length arrays, while GCC and Clang have have extensions to the C++ standard that allow them. Use a `std::vector` instead to avoid the errors on Windows. Should fix the following: rhs_functions_math.cpp(2134): error C2131: expression did not evaluate to a constant rhs_functions_math.cpp(2134): note: failure was caused by a read of a variable outside its lifetime rhs_functions_math.cpp(2134): note: see usage of 'count' rhs_functions_math.cpp(2161): error C3863: array type 'Point [count]' is not assignable --- .../rhs_functions_math.cpp | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/Core/SoarKernel/src/soar_representation/rhs_functions_math.cpp b/Core/SoarKernel/src/soar_representation/rhs_functions_math.cpp index 0d9e1bba5f..90260d74a9 100644 --- a/Core/SoarKernel/src/soar_representation/rhs_functions_math.cpp +++ b/Core/SoarKernel/src/soar_representation/rhs_functions_math.cpp @@ -24,6 +24,7 @@ #include #include +#include /* -------------------------------------------------------------------- @@ -1340,10 +1341,10 @@ int64_t compute_x_point(int64_t current_x, int64_t heading, int64_t speed, int64 { // Convert heading from degrees to radians double heading_rad = heading * PI / 180.0; - + // Calculate distance traveled double distance = speed * elapsed_time; - + // Calculate new x positions return current_x + (int)(round(distance * sin(heading_rad))); //return current_y + (int)(distance * cos(heading_rad)); @@ -1359,10 +1360,10 @@ int64_t compute_y_point(int64_t current_y, int64_t heading, int64_t speed, int64 { // Convert heading from degrees to radians double heading_rad = heading * PI / 180.0; - + // Calculate distance traveled double distance = speed * elapsed_time; - + // Calculate new y positions //return current_x + (int)(distance * sin(heading_rad)); return current_y + (int)(round(distance * cos(heading_rad))); @@ -2013,22 +2014,22 @@ typedef struct { char * id; } Point; -double calculate_angle(Point start, Point end) +double calculate_angle(Point start, Point end) { // Calculate angle in radians, with 0 pointing east double angle_rad = atan2(end.y - start.y, end.x - start.x); - + // Convert to degrees double angle_deg = angle_rad * 180 / PI; - + // Adjust to compass system (0 = North, clockwise) angle_deg = 90 - angle_deg; if (angle_deg < 0) angle_deg += 360; - + return angle_deg; } -double angle_difference(double angle1, double angle2) +double angle_difference(double angle1, double angle2) { double diff = fabs(angle1 - angle2); return (diff > 180) ? 360 - diff : diff; @@ -2040,21 +2041,21 @@ double angle_difference(double angle1, double angle2) Takes 3 args: point{x,y,id}, heading(int compass degrees),array of points and returns string id of nearest intercept -------------------------------------------------------------------- */ -char * compute_closest_intercept(Point starting, int heading, Point* points, int num_points) +char * compute_closest_intercept(Point starting, int heading, const std::vector& points) { double min_angle_diff = DBL_MAX; char nonvalid[] = "no_valid_point"; Point nearest_point = {1000, 1000, nonvalid}; // Initialize with invalid point - for (int i = 0; i < num_points; i++) + for (const auto& point : points) { - double angle = calculate_angle(starting, points[i]); + double angle = calculate_angle(starting, point); double angle_diff = angle_difference(heading, angle); - if (angle_diff < min_angle_diff) + if (angle_diff < min_angle_diff) { min_angle_diff = angle_diff; - nearest_point = points[i]; + nearest_point = point; } } @@ -2123,7 +2124,7 @@ Symbol* compute_closest_intercept_rhs_function_code(agent* thisAgent, cons* args thisAgent->outputManager->printa_sf(thisAgent, "Error: non-symbol (%y) passed to compute-closest-intercept function for points\n",points_arg); return NIL; } - + count = 0; slot* pointslot = points_arg->id->slots; //slots for points @@ -2131,7 +2132,7 @@ Symbol* compute_closest_intercept_rhs_function_code(agent* thisAgent, cons* args for (wme* curwmeA = pointslot->wmes; curwmeA != 0; curwmeA = curwmeA->next) { count = count + 1; } - Point points[count]; + std::vector points(count); count = 0; //point attributes Symbol * x_symbol = thisAgent->symbolManager->make_str_constant("x"); @@ -2156,15 +2157,15 @@ Symbol* compute_closest_intercept_rhs_function_code(agent* thisAgent, cons* args int64_t point_x = (x_value->symbol_type == INT_CONSTANT_SYMBOL_TYPE) ? x_value->ic->value : static_cast(x_value->fc->value); int64_t point_y = (y_value->symbol_type == INT_CONSTANT_SYMBOL_TYPE) ? y_value->ic->value : static_cast(y_value->fc->value); char * id = id_value->to_string(false, false, NIL, 0); - - //add to point array + + // add to point array points[count] = {point_x,point_y,id}; count = count + 1; } char starting_name[] = "starting"; Point starting = {current_x, current_y, starting_name}; - return thisAgent->symbolManager->make_str_constant(compute_closest_intercept(starting, heading, points, count)); + return thisAgent->symbolManager->make_str_constant(compute_closest_intercept(starting, heading, points)); } /* @@ -2549,7 +2550,7 @@ void init_built_in_rhs_math_functions(agent* thisAgent) add_rhs_function(thisAgent, thisAgent->symbolManager->make_str_constant("predict-x"), predict_x_position_rhs_function_code, 4, true, false, 0, true); add_rhs_function(thisAgent, thisAgent->symbolManager->make_str_constant("predict-y"), predict_y_position_rhs_function_code, 4, true, false, 0, true); add_rhs_function(thisAgent, thisAgent->symbolManager->make_str_constant("compute-closest-intercept"), compute_closest_intercept_rhs_function_code, 4, true, false, 0, true); - + /* RHS special purpose functions for Michigan Dice app*/ add_rhs_function(thisAgent, thisAgent->symbolManager->make_str_constant("compute-dice-probability"), dice_prob_rhs_function_code, 4, true, false, 0, true);