From d857e3e9cdaf613b7b0e83de244095a5f1e2df59 Mon Sep 17 00:00:00 2001 From: whitequark Date: Sun, 10 May 2020 09:24:12 +0000 Subject: [PATCH] Various header cleanups. NFC. * Don't use a reserved identifier in include guards. * Use fabs() from instead of our own ffabs(). This shouldn't make any difference with modern toolchains. * Convert a few preprocessor macros to constexprs. --- src/platform/gui.h | 2 ++ src/resource.h | 5 +++-- src/solvespace.cpp | 8 ++++---- src/solvespace.h | 36 +++++++++++++----------------------- src/system.cpp | 12 ++++++------ 5 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/platform/gui.h b/src/platform/gui.h index 85a2abd11..05dbb7a56 100644 --- a/src/platform/gui.h +++ b/src/platform/gui.h @@ -7,6 +7,8 @@ #ifndef SOLVESPACE_GUI_H #define SOLVESPACE_GUI_H +class RgbaColor; + namespace Platform { //----------------------------------------------------------------------------- diff --git a/src/resource.h b/src/resource.h index bb0e1c7a9..18c1e5821 100644 --- a/src/resource.h +++ b/src/resource.h @@ -4,13 +4,14 @@ // Copyright 2016 whitequark //----------------------------------------------------------------------------- -#ifndef __RESOURCE_H -#define __RESOURCE_H +#ifndef SOLVESPACE_RESOURCE_H +#define SOLVESPACE_RESOURCE_H class Camera; class Point2d; class Pixmap; class Vector; +class RgbaColor; std::string LoadString(const std::string &name); std::string LoadStringFromGzip(const std::string &name); diff --git a/src/solvespace.cpp b/src/solvespace.cpp index a3a53caac..042194c5d 100644 --- a/src/solvespace.cpp +++ b/src/solvespace.cpp @@ -151,7 +151,7 @@ void SolveSpaceUI::Init() { } bool SolveSpaceUI::LoadAutosaveFor(const Platform::Path &filename) { - Platform::Path autosaveFile = filename.WithExtension(AUTOSAVE_EXT); + Platform::Path autosaveFile = filename.WithExtension(BACKUP_EXT); FILE *f = OpenFile(autosaveFile, "rb"); if(!f) @@ -474,7 +474,7 @@ bool SolveSpaceUI::GetFilenameAndSave(bool saveAs) { if(saveAs || saveFile.IsEmpty()) { Platform::FileDialogRef dialog = Platform::CreateSaveFileDialog(GW.window); - dialog->AddFilter(C_("file-type", "SolveSpace models"), { "slvs" }); + dialog->AddFilter(C_("file-type", "SolveSpace models"), { SKETCH_EXT }); dialog->ThawChoices(settings, "Sketch"); if(!newSaveFile.IsEmpty()) { dialog->SetFilename(newSaveFile); @@ -503,13 +503,13 @@ void SolveSpaceUI::Autosave() ScheduleAutosave(); if(!saveFile.IsEmpty() && unsaved) { - SaveToFile(saveFile.WithExtension(AUTOSAVE_EXT)); + SaveToFile(saveFile.WithExtension(BACKUP_EXT)); } } void SolveSpaceUI::RemoveAutosave() { - Platform::Path autosaveFile = saveFile.WithExtension(AUTOSAVE_EXT); + Platform::Path autosaveFile = saveFile.WithExtension(BACKUP_EXT); RemoveFile(autosaveFile); } diff --git a/src/solvespace.h b/src/solvespace.h index 7ca2e01b2..7b3e0020b 100644 --- a/src/solvespace.h +++ b/src/solvespace.h @@ -85,6 +85,7 @@ namespace SolveSpace { using std::min; using std::max; using std::swap; +using std::fabs; #if defined(__GNUC__) __attribute__((noreturn)) @@ -116,44 +117,31 @@ inline double WRAP_SYMMETRIC(double v, double n) { return v; } -// Why is this faster than the library function? -inline double ffabs(double v) { return (v > 0) ? v : (-v); } - #define CO(v) (v).x, (v).y, (v).z -#define ANGLE_COS_EPS (1e-6) -#define LENGTH_EPS (1e-6) -#define VERY_POSITIVE (1e10) -#define VERY_NEGATIVE (-1e10) +static constexpr double ANGLE_COS_EPS = 1e-6; +static constexpr double LENGTH_EPS = 1e-6; +static constexpr double VERY_POSITIVE = 1e10; +static constexpr double VERY_NEGATIVE = -1e10; inline double Random(double vmax) { return (vmax*rand()) / RAND_MAX; } +#include "platform/platform.h" +#include "platform/gui.h" +#include "resource.h" + class Expr; class ExprVector; class ExprQuaternion; class RgbaColor; enum class Command : uint32_t; -//================ -// From the platform-specific code. - -#include "platform/platform.h" -#include "platform/gui.h" - -const size_t MAX_RECENT = 8; - -#define AUTOSAVE_EXT "slvs~" - +// Temporary heap, defined in the platform-specific code. void *AllocTemporary(size_t n); void FreeAllTemporary(); -// End of platform-specific functions -//================ - -#include "resource.h" - enum class Unit : uint32_t { MM = 0, INCHES, @@ -670,7 +658,9 @@ class SolveSpaceUI { static void MenuFile(Command id); void Autosave(); void RemoveAutosave(); - static const size_t MAX_RECENT = 8; + static constexpr size_t MAX_RECENT = 8; + static constexpr const char *SKETCH_EXT = "slvs"; + static constexpr const char *BACKUP_EXT = "slvs~"; std::vector recentFiles; bool Load(const Platform::Path &filename); bool GetFilenameAndSave(bool saveAs); diff --git a/src/system.cpp b/src/system.cpp index 08752600c..9c64b3084 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -187,15 +187,15 @@ bool System::SolveLinearSystem(double X[], double A[][MAX_UNKNOWNS], // greater. First, find a pivot (between rows i and N-1). max = 0; for(ip = i; ip < n; ip++) { - if(ffabs(A[ip][i]) > max) { + if(fabs(A[ip][i]) > max) { imax = ip; - max = ffabs(A[ip][i]); + max = fabs(A[ip][i]); } } // Don't give up on a singular matrix unless it's really bad; the // assumption code is responsible for identifying that condition, // so we're not responsible for reporting that error. - if(ffabs(max) < 1e-20) continue; + if(fabs(max) < 1e-20) continue; // Swap row imax with row i for(jp = 0; jp < n; jp++) { @@ -217,7 +217,7 @@ bool System::SolveLinearSystem(double X[], double A[][MAX_UNKNOWNS], // We've put the matrix in upper triangular form, so at this point we // can solve by back-substitution. for(i = n - 1; i >= 0; i--) { - if(ffabs(A[i][i]) < 1e-20) continue; + if(fabs(A[i][i]) < 1e-20) continue; temp = B[i]; for(j = n - 1; j > i; j--) { @@ -309,7 +309,7 @@ bool System::NewtonSolve(int tag) { if(isnan(mat.B.num[i])) { return false; } - if(ffabs(mat.B.num[i]) > CONVERGE_TOLERANCE) { + if(fabs(mat.B.num[i]) > CONVERGE_TOLERANCE) { converged = false; break; } @@ -493,7 +493,7 @@ SolveResult System::Solve(Group *g, int *rank, int *dof, List *bad, SK.constraint.ClearTags(); // Not using range-for here because index is used in additional ways for(i = 0; i < eq.n; i++) { - if(ffabs(mat.B.num[i]) > CONVERGE_TOLERANCE || isnan(mat.B.num[i])) { + if(fabs(mat.B.num[i]) > CONVERGE_TOLERANCE || isnan(mat.B.num[i])) { // This constraint is unsatisfied. if(!mat.eq[i].isFromConstraint()) continue;