Skip to content

Commit

Permalink
Various header cleanups. NFC.
Browse files Browse the repository at this point in the history
  * Don't use a reserved identifier in include guards.
  * Use fabs() from <cmath> instead of our own ffabs().
    This shouldn't make any difference with modern toolchains.
  * Convert a few preprocessor macros to constexprs.
  • Loading branch information
whitequark committed May 10, 2020
1 parent e00d486 commit d857e3e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 35 deletions.
2 changes: 2 additions & 0 deletions src/platform/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#ifndef SOLVESPACE_GUI_H
#define SOLVESPACE_GUI_H

class RgbaColor;

namespace Platform {

//-----------------------------------------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/solvespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
36 changes: 13 additions & 23 deletions src/solvespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ namespace SolveSpace {
using std::min;
using std::max;
using std::swap;
using std::fabs;

#if defined(__GNUC__)
__attribute__((noreturn))
Expand Down Expand Up @@ -116,44 +117,31 @@ inline double WRAP_SYMMETRIC(double v, double n) {
return v;
}

// Why is this faster than the library function?

This comment has been minimized.

Copy link
@ruevs

ruevs May 10, 2020

Member

So we presume that what whatever compiler Jonathan was using back in 2009 had a slow library, and that "modern" std::fabs is faster? Probably a safe presumption 2ca5334 .

This comment has been minimized.

Copy link
@whitequark

whitequark May 10, 2020

Author Contributor

Yup.

This comment has been minimized.

Copy link
@ruevs

ruevs May 10, 2020

Member

OK :-)

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,
Expand Down Expand Up @@ -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<Platform::Path> recentFiles;
bool Load(const Platform::Path &filename);
bool GetFilenameAndSave(bool saveAs);
Expand Down
12 changes: 6 additions & 6 deletions src/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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--) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -493,7 +493,7 @@ SolveResult System::Solve(Group *g, int *rank, int *dof, List<hConstraint> *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;

Expand Down

0 comments on commit d857e3e

Please sign in to comment.