Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New cork options "-first" and "-second" to produce A U (A^B) and B U (A^B) #32

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ PLATFORM := $(shell uname)
# | Programs |
# +----------+
CPP11_FLAGS := -std=c++11
CC := clang
CXX := clang++
CC := gcc
CXX := g++
ifeq ($(PLATFORM),Darwin) # on mac
CPP11_FLAGS := $(CPP11_FLAGS) -stdlib=libc++ -Wno-c++11-extensions
endif
Expand Down Expand Up @@ -79,6 +79,12 @@ ifeq ($(PLATFORM),Darwin)
LINKD := $(LINK) -Wl,-no_pie
endif

ARCH = $(shell uname -m)

ifeq ($(findstring x86_64,$(ARCH)), x86_64)
CCFLAGS += -m64
CXXFLAGS+= -m64
endif

# ***********************
# * SOURCE DECLARATIONS *
Expand Down
56 changes: 56 additions & 0 deletions src/cork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ void computeUnion(

cmIn0.boolUnion(cmIn1);

if(out->remesh)
cmIn0.remesh();

corkMesh2CorkTriMesh(&cmIn0, out);
}

Expand All @@ -218,6 +221,9 @@ void computeDifference(

cmIn0.boolDiff(cmIn1);

if(out->remesh)
cmIn0.remesh();

corkMesh2CorkTriMesh(&cmIn0, out);
}

Expand All @@ -230,6 +236,9 @@ void computeIntersection(

cmIn0.boolIsct(cmIn1);

if(out->remesh)
cmIn0.remesh();

corkMesh2CorkTriMesh(&cmIn0, out);
}

Expand All @@ -242,6 +251,9 @@ void computeSymmetricDifference(

cmIn0.boolXor(cmIn1);

if(out->remesh)
cmIn0.remesh();

corkMesh2CorkTriMesh(&cmIn0, out);
}

Expand All @@ -255,6 +267,50 @@ void resolveIntersections(
cmIn0.disjointUnion(cmIn1);
cmIn0.resolveIntersections();

if(out->remesh){
cmIn0.remesh();
}
corkMesh2CorkTriMesh(&cmIn0, out);
}

void computeFirst(
CorkTriMesh in0, CorkTriMesh in1, CorkTriMesh *out
) {
CorkMesh cmIn0, cmIn1;
corkTriMesh2CorkMesh(in0, &cmIn0);
corkTriMesh2CorkMesh(in1, &cmIn1);

cmIn0.boolFirst(cmIn1);

if(out->remesh)
cmIn0.remesh();

corkMesh2CorkTriMesh(&cmIn0, out);
}

void computeSecond(
CorkTriMesh in0, CorkTriMesh in1, CorkTriMesh *out
) {
CorkMesh cmIn0, cmIn1;
corkTriMesh2CorkMesh(in0, &cmIn0);
corkTriMesh2CorkMesh(in1, &cmIn1);

cmIn1.boolFirst(cmIn0);

if(out->remesh)
cmIn1.remesh();

corkMesh2CorkTriMesh(&cmIn1, out);
}


void remeshTriangles(
CorkTriMesh in, CorkTriMesh *out
) {
CorkMesh cmIn;
corkTriMesh2CorkMesh(in, &cmIn);

cmIn.remesh();

corkMesh2CorkTriMesh(&cmIn, out);
}
7 changes: 7 additions & 0 deletions src/cork.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct CorkTriMesh
uint n_vertices;
uint *triangles;
float *vertices;
uint remesh;
};

void freeCorkTriMesh(CorkTriMesh *mesh);
Expand Down Expand Up @@ -72,3 +73,9 @@ void computeSymmetricDifference(
// such that the two surfaces are now connected.
void resolveIntersections(CorkTriMesh in0, CorkTriMesh in1, CorkTriMesh *out);

// result = A cut by B
void computeFirst(CorkTriMesh in0, CorkTriMesh in1, CorkTriMesh *out);
// result = B cut by A
void computeSecond(CorkTriMesh in0, CorkTriMesh in1, CorkTriMesh *out);
// result = remesh(A)
void remeshTriangles(CorkTriMesh in, CorkTriMesh *out);
1 change: 1 addition & 0 deletions src/file_formats/off.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ int writeOFF(string filename, FileMesh *data)
int numfaces = data->triangles.size();
out << numvertices << ' ' << numfaces << ' ' << 0 << endl;

out.precision(18);
// vertex data
for(const auto &v : data->vertices) {
const Vec3d &p = v.pos;
Expand Down
5 changes: 4 additions & 1 deletion src/isct/triangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -3295,8 +3295,11 @@ struct behavior *b;
int increment;
int meshnumber;
#endif /* not TRILIBRARY */
int i, j, k;
int i, j;
#ifndef CDT_ONLY
int k;
char workstring[FILENAMESIZE];
#endif

b->poly = b->refine = b->quality = 0;
b->vararea = b->fixedarea = b->usertest = 0;
Expand Down
48 changes: 45 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ using std::ostream;

#include "cork.h"

int doremesh=0;

void file2corktrimesh(
const Files::FileMesh &in, CorkTriMesh *out
Expand Down Expand Up @@ -165,6 +166,7 @@ void CmdList::printHelp(ostream &out)
out <<
"Welcome to Cork. Usage:" << endl <<
" > cork [-command arg0 arg1 ... argn]*" << endl <<
" if --command is used, a remesh is applied to the output" << endl <<
"for example," << endl <<
" > cork -union box0.off box1.off result.off" << endl <<
"Options:" << endl;
Expand All @@ -184,6 +186,10 @@ void CmdList::runCommands(std::vector<string>::iterator &arg_it,
exit(1);
}
arg_cmd = arg_cmd.substr(1);
if(arg_cmd[0] == '-') {
doremesh=1;
arg_cmd = arg_cmd.substr(1);
}
arg_it++;

bool found = true;
Expand Down Expand Up @@ -217,6 +223,8 @@ genericBinaryOp(
CorkTriMesh in0;
CorkTriMesh in1;
CorkTriMesh out;

out.remesh=doremesh;

if(args == end) { cerr << "too few args" << endl; exit(1); }
loadMesh(*args, &in0);
Expand Down Expand Up @@ -244,7 +252,10 @@ genericBinaryOp(

int main(int argc, char *argv[])
{
initRand(); // that's useful
if(argc>=6){
initRand(atoi(argv[5])); //use the 4th argument to set the seed
}else
initRand();

if(argc < 2) {
cout << "Please type 'cork -help' for instructions" << endl;
Expand Down Expand Up @@ -305,8 +316,39 @@ int main(int argc, char *argv[])
" and output the connected mesh with those\n"
" intersections made explicit and connected",
genericBinaryOp(resolveIntersections));


cmds.regCmd("first",
"-first in0 in1 out Compute the first mesh in0 cut by the second mesh in1,\n"
" and output the result",
genericBinaryOp(computeFirst));
cmds.regCmd("second",
"-second in0 in1 out Compute the second mesh in1 cut by the first mesh in0,\n"
" and output the result",
genericBinaryOp(computeSecond));

// add cmds
cmds.regCmd("remesh",
"-remesh in out Remesh the input mesh to remove poorly shaped triangles",
[](std::vector<string>::iterator &args,
const std::vector<string>::iterator &end) {
CorkTriMesh in;
CorkTriMesh out;

if(args == end) { cerr << "too few args" << endl; exit(1); }
loadMesh(*args, &in);
args++;

remeshTriangles(in, &out);

if(args == end) { cerr << "too few args" << endl; exit(1); }
saveMesh(*args, out);
args++;

freeCorkTriMesh(&out);

delete[] in.vertices;
delete[] in.triangles;
});

cmds.runCommands(arg_it, args.end());

return 0;
Expand Down
28 changes: 14 additions & 14 deletions src/mesh/mesh.bool.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,17 +356,17 @@ void Mesh<VertData,TriData>::boolXor(Mesh &rhs)
});
}















template<class VertData, class TriData>
void Mesh<VertData,TriData>::boolFirst(Mesh &rhs)
{
BoolProblem bprob(this);
bprob.doSetup(rhs);
bprob.doDeleteAndFlip([](byte data) -> typename BoolProblem::TriCode {
if(data == 1) // part of op 1 OUTSIDE op 0
return BoolProblem::DELETE_TRI;
else // otherwise
return BoolProblem::KEEP_TRI;
});
}
1 change: 1 addition & 0 deletions src/mesh/mesh.decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ class Mesh
void boolDiff(Mesh &rhs);
void boolIsct(Mesh &rhs);
void boolXor(Mesh &rhs);
void boolFirst(Mesh &rhs);

private: // Internal Formats
struct Tri {
Expand Down
11 changes: 11 additions & 0 deletions src/util/prelude.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
// +-------------------------------------------------------------------------
#pragma once

#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sys/time.h>

#ifndef uint
typedef unsigned int uint;
Expand All @@ -39,6 +41,10 @@ typedef unsigned int uint;
typedef unsigned char byte;
#endif

#ifndef M_PI
#define M_PI (3.14159265358979323846)
#endif

// ***********
// * Logging

Expand Down Expand Up @@ -150,6 +156,11 @@ inline void initRand() {
srand(uint(time(0)));
}

inline void initRand(uint seed) {
// currently none! Should seed using clock
srand(seed);
}

inline double drand(double min, double max) {
const double invMAX = 1.0/double(RAND_MAX);
double rand0to1 = double(std::rand())*invMAX;
Expand Down