Skip to content

Commit

Permalink
revert clipper to 32 bit float math
Browse files Browse the repository at this point in the history
doubles don't improve anything after the epsilon fix
  • Loading branch information
wootguy committed Oct 27, 2023
1 parent 76affdf commit 927c051
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 203 deletions.
49 changes: 23 additions & 26 deletions src/editor/Clipper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,11 @@ int Clipper::clipVertices(CMesh& mesh, BSPPLANE& clip) {
int positive = 0;
int negative = 0;

vec3d clipNorm = vec3d(clip.vNormal);
double dDist = clip.fDist;

for (int i = 0; i < mesh.verts.size(); i++) {
CVertex& vert = mesh.verts[i];

if (vert.visible) {
vert.distance = dotProduct(clipNorm, vert.pos) - dDist;
vert.distance = dotProduct(clip.vNormal, vert.pos) - clip.fDist;

if (vert.distance > EPSILON) {
positive++;
Expand Down Expand Up @@ -75,8 +72,8 @@ void Clipper::clipEdges(CMesh& mesh, BSPPLANE& clip) {
CVertex& v1 = mesh.verts[edge.verts[1]];

if (edge.visible) {
double d0 = v0.distance;
double d1 = v1.distance;
float d0 = v0.distance;
float d1 = v1.distance;

if (d0 <= 0 && d1 <= 0) {
// edge is culled, remove edge from faces sharing it
Expand All @@ -103,8 +100,8 @@ void Clipper::clipEdges(CMesh& mesh, BSPPLANE& clip) {

// the edge is split by the plane. Compute the point of intersection.

double t = d0 / (d0 - d1);
vec3d intersect = v0.pos*(1 - t) + v1.pos * t;
float t = d0 / (d0 - d1);
vec3 intersect = v0.pos*(1 - t) + v1.pos * t;
int idx = mesh.verts.size();
mesh.verts.push_back(intersect);

Expand All @@ -119,7 +116,7 @@ void Clipper::clipEdges(CMesh& mesh, BSPPLANE& clip) {
}

void Clipper::clipFaces(CMesh& mesh, BSPPLANE& clip) {
CFace closeFace({}, vec3d(clip.vNormal).invert());
CFace closeFace({}, vec3(clip.vNormal).invert());
int findex = mesh.faces.size();

for (int i = 0; i < mesh.faces.size(); i++) {
Expand Down Expand Up @@ -187,21 +184,21 @@ bool Clipper::getOpenPolyline(CMesh& mesh, CFace& face, int& start, int& final)
}

CMesh Clipper::createMaxSizeVolume() {
const vec3d min = vec3d(-MAX_COORD, -MAX_COORD, -MAX_COORD);
const vec3d max = vec3d(MAX_COORD, MAX_COORD, MAX_COORD);
const vec3 min = vec3(-MAX_COORD, -MAX_COORD, -MAX_COORD);
const vec3 max = vec3(MAX_COORD, MAX_COORD, MAX_COORD);

CMesh mesh;

{
mesh.verts.push_back(CVertex(vec3d(min.x, min.y, min.z))); // 0 front-left-bottom
mesh.verts.push_back(CVertex(vec3d(max.x, min.y, min.z))); // 1 front-right-bottom
mesh.verts.push_back(CVertex(vec3d(max.x, max.y, min.z))); // 2 back-right-bottom
mesh.verts.push_back(CVertex(vec3d(min.x, max.y, min.z))); // 3 back-left-bottom

mesh.verts.push_back(CVertex(vec3d(min.x, min.y, max.z))); // 4 front-left-top
mesh.verts.push_back(CVertex(vec3d(max.x, min.y, max.z))); // 5 front-right-top
mesh.verts.push_back(CVertex(vec3d(max.x, max.y, max.z))); // 6 back-right-top
mesh.verts.push_back(CVertex(vec3d(min.x, max.y, max.z))); // 7 back-left-top
mesh.verts.push_back(CVertex(vec3(min.x, min.y, min.z))); // 0 front-left-bottom
mesh.verts.push_back(CVertex(vec3(max.x, min.y, min.z))); // 1 front-right-bottom
mesh.verts.push_back(CVertex(vec3(max.x, max.y, min.z))); // 2 back-right-bottom
mesh.verts.push_back(CVertex(vec3(min.x, max.y, min.z))); // 3 back-left-bottom

mesh.verts.push_back(CVertex(vec3(min.x, min.y, max.z))); // 4 front-left-top
mesh.verts.push_back(CVertex(vec3(max.x, min.y, max.z))); // 5 front-right-top
mesh.verts.push_back(CVertex(vec3(max.x, max.y, max.z))); // 6 back-right-top
mesh.verts.push_back(CVertex(vec3(min.x, max.y, max.z))); // 7 back-left-top
}

{
Expand All @@ -223,12 +220,12 @@ CMesh Clipper::createMaxSizeVolume() {
}

{
mesh.faces.push_back(CFace({ 0, 1, 2, 3 }, vec3d( 0, -1, 0))); // 0 front
mesh.faces.push_back(CFace({ 4, 5, 6, 7 }, vec3d( 0, 1, 0))); // 1 back
mesh.faces.push_back(CFace({ 1, 5, 8, 9 }, vec3d(-1, 0, 0))); // 2 left
mesh.faces.push_back(CFace({ 3, 7, 10, 11 }, vec3d( 1, 0, 0))); // 3 right
mesh.faces.push_back(CFace({ 2, 6, 9, 11 }, vec3d( 0, 0, 1))); // 4 top
mesh.faces.push_back(CFace({ 0, 4, 8, 10 }, vec3d( 0, 0, -1))); // 5 bottom
mesh.faces.push_back(CFace({ 0, 1, 2, 3 }, vec3( 0, -1, 0))); // 0 front
mesh.faces.push_back(CFace({ 4, 5, 6, 7 }, vec3( 0, 1, 0))); // 1 back
mesh.faces.push_back(CFace({ 1, 5, 8, 9 }, vec3(-1, 0, 0))); // 2 left
mesh.faces.push_back(CFace({ 3, 7, 10, 11 }, vec3( 1, 0, 0))); // 3 right
mesh.faces.push_back(CFace({ 2, 6, 9, 11 }, vec3( 0, 0, 1))); // 4 top
mesh.faces.push_back(CFace({ 0, 4, 8, 10 }, vec3( 0, 0, -1))); // 5 bottom
}

return mesh;
Expand Down
10 changes: 5 additions & 5 deletions src/editor/Clipper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
// https://www.geometrictools.com/Documentation/ClipMesh.pdf

struct CVertex {
vec3d pos;
double distance = 0;
vec3 pos;
float distance = 0;
int occurs = 0;
bool visible = true;

CVertex(vec3d pos) : pos(pos) {}
CVertex(vec3 pos) : pos(pos) {}
};

struct CEdge {
Expand All @@ -37,9 +37,9 @@ struct CEdge {
struct CFace {
vector<int> edges;
bool visible = true;
vec3d normal;
vec3 normal;

CFace(vector<int> edges, vec3d normal) {
CFace(vector<int> edges, vec3 normal) {
this->edges = edges;
this->normal = normal;
}
Expand Down
138 changes: 0 additions & 138 deletions src/util/vectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,127 +3,6 @@
#include "mat4x4.h"
#include "util.h"

vec3d operator-(vec3d v1, vec3d v2)
{
v1.x -= v2.x;
v1.y -= v2.y;
v1.z -= v2.z;
return v1;
}

vec3d operator+(vec3d v1, vec3d v2)
{
v1.x += v2.x;
v1.y += v2.y;
v1.z += v2.z;
return v1;
}

vec3d operator*(vec3d v1, vec3d v2)
{
v1.x *= v2.x;
v1.y *= v2.y;
v1.z *= v2.z;
return v1;
}

vec3d operator/(vec3d v1, vec3d v2)
{
v1.x /= v2.x;
v1.y /= v2.y;
v1.z /= v2.z;
return v1;
}

vec3d operator-(vec3d v, double f)
{
v.x -= f;
v.y -= f;
v.z -= f;
return v;
}

vec3d operator+(vec3d v, double f)
{
v.x += f;
v.y += f;
v.z += f;
return v;
}

vec3d operator*(vec3d v, double f)
{
v.x *= f;
v.y *= f;
v.z *= f;
return v;
}

vec3d operator/(vec3d v, double f)
{
v.x /= f;
v.y /= f;
v.z /= f;
return v;
}

void vec3d::operator-=(vec3d v)
{
x -= v.x;
y -= v.y;
z -= v.z;
}

void vec3d::operator+=(vec3d v)
{
x += v.x;
y += v.y;
z += v.z;
}

void vec3d::operator*=(vec3d v)
{
x *= v.x;
y *= v.y;
z *= v.z;
}

void vec3d::operator/=(vec3d v)
{
x /= v.x;
y /= v.y;
z /= v.z;
}

void vec3d::operator-=(double f)
{
x -= f;
y -= f;
z -= f;
}

void vec3d::operator+=(double f)
{
x += f;
y += f;
z += f;
}

void vec3d::operator*=(double f)
{
x *= f;
y *= f;
z *= f;
}

void vec3d::operator/=(double f)
{
x /= f;
y /= f;
z /= f;
}


bool operator==( vec3 v1, vec3 v2 )
{
vec3 v = v1 - v2;
Expand Down Expand Up @@ -268,14 +147,6 @@ void vec3::operator/=( float f )
z /= f;
}

vec3d crossProduct(vec3d v1, vec3d v2)
{
float x = v1.y * v2.z - v2.y * v1.z;
float y = v2.x * v1.z - v1.x * v2.z;
float z = v1.x * v2.y - v1.y * v2.x;
return vec3d(x, y, z);
}

vec3 crossProduct( vec3 v1, vec3 v2 )
{
float x = v1.y*v2.z - v2.y*v1.z;
Expand All @@ -289,11 +160,6 @@ float crossProduct(vec2 v1, vec2 v2)
return (v1.x * v2.y) - (v1.y * v2.x);
}

double dotProduct(vec3d v1, vec3d v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}

float dotProduct( vec3 v1, vec3 v2 )
{
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
Expand Down Expand Up @@ -331,10 +197,6 @@ vec3 vec3::invert() {
return vec3(x != 0 ? -x : x, y != 0 ? -y : y, z != 0 ? -z : z);
}

vec3d vec3d::invert() {
return vec3d(x != 0 ? -x : x, y != 0 ? -y : y, z != 0 ? -z : z);
}

float vec3::length()
{
return sqrt( (x*x) + (y*y) + (z*z) );
Expand Down
35 changes: 1 addition & 34 deletions src/util/vectors.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,37 +99,4 @@ vec4 operator*(vec4 v, float f);
vec4 operator/(vec4 v, float f);

bool operator==(vec4 v1, vec4 v2);
bool operator!=(vec4 v1, vec4 v2);

struct vec3d
{
double x, y, z;
vec3d() : x(), y(), z() {}
vec3d(float x, float y, float z) : x(x), y(y), z(z) {}
vec3d(vec3 v) : x(v.x), y(v.y), z(v.z) {}
vec3 vec3f() { return vec3(x, y, z); }
vec3d invert();

void operator-=(vec3d v);
void operator+=(vec3d v);
void operator*=(vec3d v);
void operator/=(vec3d v);

void operator-=(double f);
void operator+=(double f);
void operator*=(double f);
void operator/=(double f);
};

vec3d operator-(vec3d v1, vec3d v2);
vec3d operator+(vec3d v1, vec3d v2);
vec3d operator*(vec3d v1, vec3d v2);
vec3d operator/(vec3d v1, vec3d v2);

vec3d operator+(vec3d v, double f);
vec3d operator-(vec3d v, double f);
vec3d operator*(vec3d v, double f);
vec3d operator/(vec3d v, double f);

double dotProduct(vec3d v1, vec3d v2);
vec3d crossProduct(vec3d v1, vec3d v2);
bool operator!=(vec4 v1, vec4 v2);

0 comments on commit 927c051

Please sign in to comment.