mirror of
https://github.com/jhasse/poly2tri.git
synced 2024-11-05 22:09:52 +01:00
added AddHole to api; added random distribution example
This commit is contained in:
parent
7cbc314b7b
commit
8348364612
11
README
11
README
@ -36,9 +36,16 @@ Windows command line:
|
||||
Running the Examples
|
||||
----------------------------------------------
|
||||
|
||||
p2t <filename> <center-x> <center-y> <zoom>
|
||||
Load data points from a file:
|
||||
p2t <filename> <center_x> <center_y> <zoom>
|
||||
|
||||
Random distribution of points inside a consrained box:
|
||||
p2t random <num_points> <box_radius> <zoom>
|
||||
|
||||
Examples:
|
||||
|
||||
./p2t dude.dat 300 500 2
|
||||
./p2t nazca_monkey.dat 0 0 9
|
||||
./p2t nazca_monkey.dat 0 0 9
|
||||
|
||||
./p2t random 10 100 5.0
|
||||
./p2t random 1000 20000 0.025
|
@ -43,6 +43,10 @@ void CDT::AddHole(std::vector<Point*> polyline)
|
||||
sweep_context_->AddHole(polyline);
|
||||
}
|
||||
|
||||
void CDT::AddPoint(Point* point) {
|
||||
sweep_context_->AddPoint(point);
|
||||
}
|
||||
|
||||
void CDT::Triangulate()
|
||||
{
|
||||
sweep_->Triangulate(*sweep_context_);
|
||||
|
@ -42,6 +42,8 @@ public:
|
||||
CDT(std::vector<Point*> polyline);
|
||||
/// Add a hole
|
||||
void AddHole(std::vector<Point*> polyline);
|
||||
/// Add a single point
|
||||
void AddPoint(Point* point);
|
||||
/// Triangulate points
|
||||
void Triangulate();
|
||||
/// Get Delaunay triangles
|
||||
|
@ -53,6 +53,10 @@ void SweepContext::AddHole(std::vector<Point*> polyline)
|
||||
}
|
||||
}
|
||||
|
||||
void SweepContext::AddPoint(Point* point) {
|
||||
points_.push_back(point);
|
||||
}
|
||||
|
||||
std::vector<Triangle*> SweepContext::GetTriangles()
|
||||
{
|
||||
return triangles_;
|
||||
|
@ -46,43 +46,48 @@ class AdvancingFront;
|
||||
class SweepContext {
|
||||
public:
|
||||
|
||||
// Constructor
|
||||
/// Constructor
|
||||
SweepContext(std::vector<Point*> polyline);
|
||||
// Destructor
|
||||
/// Destructor
|
||||
~SweepContext();
|
||||
|
||||
void set_head(Point* p1);
|
||||
|
||||
Point* head();
|
||||
|
||||
void set_tail(Point* p1);
|
||||
|
||||
Point* tail();
|
||||
|
||||
int point_count();
|
||||
|
||||
Node& LocateNode(Point& point);
|
||||
|
||||
void RemoveNode(Node* node);
|
||||
|
||||
void CreateAdvancingFront();
|
||||
|
||||
// Try to map a node to all sides of this triangle that don't have a neighbor
|
||||
/// Try to map a node to all sides of this triangle that don't have a neighbor
|
||||
void MapTriangleToNodes(Triangle& t);
|
||||
|
||||
void AddToMap(Triangle* triangle);
|
||||
|
||||
Point* GetPoint(const int& index);
|
||||
|
||||
Point* GetPoints();
|
||||
|
||||
void RemoveFromMap(Triangle* triangle);
|
||||
|
||||
void AddHole(std::vector<Point*> polyline);
|
||||
|
||||
void AddPoint(Point* point);
|
||||
|
||||
AdvancingFront* front();
|
||||
|
||||
void MeshClean(Triangle& triangle);
|
||||
|
||||
std::vector<Triangle*> GetTriangles();
|
||||
std::list<Triangle*> GetMap();
|
||||
|
||||
std::vector<Edge*> edge_list;
|
||||
|
||||
struct Basin {
|
||||
|
140
testbed/main.cc
140
testbed/main.cc
@ -49,6 +49,7 @@ void Draw(const double zoom);
|
||||
void DrawMap(const double zoom);
|
||||
void ConstrainedColor(bool constrain);
|
||||
double StringToDouble(const std::string& s);
|
||||
float RandomFloat(float min, float max);
|
||||
|
||||
/// Dude hole examples
|
||||
vector<Point*> CreateHeadHole();
|
||||
@ -72,86 +73,117 @@ vector< vector<Point*> > polylines;
|
||||
|
||||
/// Draw the entire triangle map?
|
||||
bool draw_map = false;
|
||||
/// Create a random distribution of points?
|
||||
bool random_distribution = false;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
|
||||
int num_points = 0, max, min;
|
||||
double zoom;
|
||||
|
||||
if (argc != 5) {
|
||||
cout << "Usage: p2t filename centerX centerY zoom" << endl;
|
||||
cout << "-== USAGE ==-" << endl;
|
||||
cout << "Load Data File: p2t filename center_x center_y zoom" << endl;
|
||||
cout << " Random Points: p2t random num_points width zoom" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
// initialize random seed:
|
||||
srand ( time(NULL) );
|
||||
|
||||
int a = 0;
|
||||
int b = 2000;
|
||||
|
||||
for(int i = 0; i < num_points; i++) {
|
||||
double x = rand() % (b - a - 1) + a + 1;
|
||||
double y = rand() % (b - a - 1) + a + 1;
|
||||
polyline[i] = Point(x, y);
|
||||
}
|
||||
*/
|
||||
|
||||
cx = atof(argv[2]);
|
||||
cy = atof(argv[3]);
|
||||
|
||||
// Parse and tokenize data file
|
||||
string line;
|
||||
ifstream myfile(argv[1]);
|
||||
vector<p2t::Point*> points;
|
||||
if (myfile.is_open()) {
|
||||
while (!myfile.eof()) {
|
||||
getline(myfile, line);
|
||||
if (line.size() == 0) {
|
||||
break;
|
||||
}
|
||||
istringstream iss(line);
|
||||
vector<string> tokens;
|
||||
copy(istream_iterator<string>(iss), istream_iterator<string>(),
|
||||
back_inserter<vector<string> >(tokens));
|
||||
double x = StringToDouble(tokens[0]);
|
||||
double y = StringToDouble(tokens[1]);
|
||||
points.push_back(new Point(x, y));
|
||||
}
|
||||
myfile.close();
|
||||
if(string(argv[1]) == "random") {
|
||||
num_points = atoi(argv[2]);
|
||||
random_distribution = true;
|
||||
max = atoi(argv[3]);
|
||||
min = -max;
|
||||
cx = cy = 0;
|
||||
zoom = atof(argv[4]);
|
||||
} else {
|
||||
cout << "File not opened" << endl;
|
||||
zoom = atof(argv[4]);
|
||||
cx = atof(argv[2]);
|
||||
cy = atof(argv[3]);
|
||||
}
|
||||
|
||||
vector<p2t::Point*> polyline;
|
||||
|
||||
if(random_distribution) {
|
||||
// Create a simple bounding box
|
||||
polyline.push_back(new Point(min,min));
|
||||
polyline.push_back(new Point(min,max));
|
||||
polyline.push_back(new Point(max,max));
|
||||
polyline.push_back(new Point(max,min));
|
||||
} else {
|
||||
// Load pointset from file
|
||||
|
||||
// Parse and tokenize data file
|
||||
string line;
|
||||
ifstream myfile(argv[1]);
|
||||
if (myfile.is_open()) {
|
||||
while (!myfile.eof()) {
|
||||
getline(myfile, line);
|
||||
if (line.size() == 0) {
|
||||
break;
|
||||
}
|
||||
istringstream iss(line);
|
||||
vector<string> tokens;
|
||||
copy(istream_iterator<string>(iss), istream_iterator<string>(),
|
||||
back_inserter<vector<string> >(tokens));
|
||||
double x = StringToDouble(tokens[0]);
|
||||
double y = StringToDouble(tokens[1]);
|
||||
polyline.push_back(new Point(x, y));
|
||||
num_points++;
|
||||
}
|
||||
myfile.close();
|
||||
} else {
|
||||
cout << "File not opened" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Number of points = " << points.size() << endl;
|
||||
polylines.push_back(points);
|
||||
cout << "Number of constrained edges = " << polyline.size() << endl;
|
||||
polylines.push_back(polyline);
|
||||
|
||||
Init();
|
||||
|
||||
///
|
||||
/// Perform triangulation
|
||||
///
|
||||
/*
|
||||
* Perform triangulation!
|
||||
*/
|
||||
|
||||
double init_time = glfwGetTime();
|
||||
|
||||
// Step 1 - Create CDT and add primary polyline
|
||||
CDT* cdt = new CDT(points);
|
||||
/*
|
||||
* STEP 1: Create CDT and add primary polyline
|
||||
* NOTE: polyline must be a simple polygon. The polyline's points
|
||||
* constitute the polygon's constrained edges!
|
||||
*/
|
||||
CDT* cdt = new CDT(polyline);
|
||||
|
||||
/*
|
||||
// Step 2 - Add holes if necessary
|
||||
* STEP 2: Add holes or Steiner points if necessary
|
||||
*/
|
||||
string s(argv[1]);
|
||||
if(s.find("dude.dat", 0) != string::npos) {
|
||||
// Add head hole
|
||||
vector<Point*> head_hole = CreateHeadHole();
|
||||
num_points += head_hole.size();
|
||||
cdt->AddHole(head_hole);
|
||||
// Add chest hole
|
||||
vector<Point*> chest_hole = CreateChestHole();
|
||||
num_points += chest_hole.size();
|
||||
cdt->AddHole(chest_hole);
|
||||
|
||||
polylines.push_back(head_hole);
|
||||
polylines.push_back(chest_hole);
|
||||
} else if (random_distribution) {
|
||||
max-=1;
|
||||
min+=1;
|
||||
srand (time(NULL));
|
||||
for(int i = 0; i < num_points; i++) {
|
||||
double x = RandomFloat(min, max);
|
||||
double y = RandomFloat(min, max);
|
||||
cdt->AddPoint(new Point(x, y));
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// Step 3 - Triangulate!
|
||||
/*
|
||||
* STEP 3: Triangulate!
|
||||
*/
|
||||
cdt->Triangulate();
|
||||
|
||||
double dt = glfwGetTime() - init_time;
|
||||
@ -159,10 +191,11 @@ int main(int argc, char* argv[])
|
||||
triangles = cdt->GetTriangles();
|
||||
map = cdt->GetMap();
|
||||
|
||||
cout << "Number of points = " << num_points << endl;
|
||||
cout << "Number of triangles = " << triangles.size() << endl;
|
||||
cout << "Elapsed time (secs) = " << dt << endl;
|
||||
cout << "Elapsed time (ms) = " << dt*1000.0 << endl;
|
||||
|
||||
MainLoop(atof(argv[4]));
|
||||
MainLoop(zoom);
|
||||
|
||||
ShutDown(0);
|
||||
return 0;
|
||||
@ -367,3 +400,8 @@ double StringToDouble(const std::string& s)
|
||||
return 0;
|
||||
return x;
|
||||
}
|
||||
|
||||
float RandomFloat(float min, float max) {
|
||||
float r = (float)rand() / (float)RAND_MAX;
|
||||
return min + r * (max - min);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user