mirror of
https://github.com/jhasse/poly2tri.git
synced 2024-11-26 07:16:11 +01:00
Merge pull request #46 from pierre-dejoue/testbed-autozoom
QoL: Add auto-zoom feature to the testbed app
This commit is contained in:
commit
54af704954
@ -74,14 +74,19 @@ Load data points from a file:
|
|||||||
```
|
```
|
||||||
build/testbed/p2t <filename> <center_x> <center_y> <zoom>
|
build/testbed/p2t <filename> <center_x> <center_y> <zoom>
|
||||||
```
|
```
|
||||||
|
Load data points from a file and automatically fit the geometry to the window:
|
||||||
|
```
|
||||||
|
build/testbed/p2t <filename>
|
||||||
|
```
|
||||||
Random distribution of points inside a constrained box:
|
Random distribution of points inside a constrained box:
|
||||||
```
|
```
|
||||||
build/testbed/p2t random <num_points> <box_radius> <zoom>
|
build/testbed/p2t random <num_points> <box_radius> <zoom>
|
||||||
```
|
```
|
||||||
Examples:
|
Examples:
|
||||||
```
|
```
|
||||||
build/testbed/p2t testbed/data/dude.dat 300 500 2
|
build/testbed/p2t testbed/data/dude.dat 350 500 3
|
||||||
build/testbed/p2t testbed/data/nazca_monkey.dat 0 0 9
|
|
||||||
|
build/testbed/p2t testbed/data/nazca_monkey.dat
|
||||||
|
|
||||||
build/testbed/p2t random 10 100 5.0
|
build/testbed/p2t random 10 100 5.0
|
||||||
build/testbed/p2t random 1000 20000 0.025
|
build/testbed/p2t random 1000 20000 0.025
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Poly2Tri Copyright (c) 2009-2018, Poly2Tri Contributors
|
* Poly2Tri Copyright (c) 2009-2022, Poly2Tri Contributors
|
||||||
* https://github.com/jhasse/poly2tri
|
* https://github.com/jhasse/poly2tri
|
||||||
*
|
*
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
@ -40,10 +40,12 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include <limits>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -51,11 +53,12 @@ using namespace p2t;
|
|||||||
|
|
||||||
bool ParseFile(string filename, vector<Point*>& out_polyline, vector<vector<Point*>>& out_holes,
|
bool ParseFile(string filename, vector<Point*>& out_polyline, vector<vector<Point*>>& out_holes,
|
||||||
vector<Point*>& out_steiner);
|
vector<Point*>& out_steiner);
|
||||||
|
std::pair<Point, Point> BoundingBox(const std::vector<Point*>& polyline);
|
||||||
void GenerateRandomPointDistribution(size_t num_points, double min, double max,
|
void GenerateRandomPointDistribution(size_t num_points, double min, double max,
|
||||||
vector<Point*>& out_polyline,
|
vector<Point*>& out_polyline,
|
||||||
vector<vector<Point*>>& out_holes,
|
vector<vector<Point*>>& out_holes,
|
||||||
vector<Point*>& out_steiner);
|
vector<Point*>& out_steiner);
|
||||||
void Init();
|
void Init(int window_width, int window_height);
|
||||||
void ShutDown(int return_code);
|
void ShutDown(int return_code);
|
||||||
void MainLoop(const double zoom);
|
void MainLoop(const double zoom);
|
||||||
void Draw(const double zoom);
|
void Draw(const double zoom);
|
||||||
@ -69,6 +72,13 @@ double rotate_y = 0.0,
|
|||||||
rotate_z = 0.0;
|
rotate_z = 0.0;
|
||||||
const double rotations_per_tick = 0.2;
|
const double rotations_per_tick = 0.2;
|
||||||
|
|
||||||
|
/// Default window size
|
||||||
|
constexpr int default_window_width = 800;
|
||||||
|
constexpr int default_window_height = 600;
|
||||||
|
|
||||||
|
/// Autozoom border (percentage)
|
||||||
|
const double autozoom_border = 0.05;
|
||||||
|
|
||||||
/// Screen center x
|
/// Screen center x
|
||||||
double cx = 0.0;
|
double cx = 0.0;
|
||||||
/// Screen center y
|
/// Screen center y
|
||||||
@ -105,14 +115,21 @@ int main(int argc, char* argv[])
|
|||||||
double max, min;
|
double max, min;
|
||||||
double zoom;
|
double zoom;
|
||||||
|
|
||||||
if (argc != 5) {
|
if (argc != 2 && argc != 5) {
|
||||||
cout << "-== USAGE ==-" << endl;
|
cout << "-== USAGE ==-" << endl;
|
||||||
cout << "Load Data File: p2t filename center_x center_y zoom" << endl;
|
cout << "Load Data File: p2t <filename> <center_x> <center_y> <zoom>" << endl;
|
||||||
cout << "Example: ./build/p2t testbed/data/dude.dat 500 500 1" << endl;
|
cout << " Example: build/testbed/p2t testbed/data/dude.dat 350 500 3" << endl;
|
||||||
|
cout << "Load Data File with Auto-Zoom: p2t <filename>" << endl;
|
||||||
|
cout << " Example: build/testbed/p2t testbed/data/nazca_monkey.dat" << endl;
|
||||||
|
cout << "Generate Random Polygon: p2t random <num_points> <box_radius> <zoom>" << endl;
|
||||||
|
cout << " Example: build/testbed/p2t random 100 1 500" << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string(argv[1]) == "random") {
|
// If true, adjust the zoom settings to fit the input geometry to the window
|
||||||
|
const bool autozoom = (argc == 2);
|
||||||
|
|
||||||
|
if (!autozoom && string(argv[1]) == "random") {
|
||||||
num_points = atoi(argv[2]);
|
num_points = atoi(argv[2]);
|
||||||
random_distribution = true;
|
random_distribution = true;
|
||||||
char* pEnd;
|
char* pEnd;
|
||||||
@ -122,9 +139,11 @@ int main(int argc, char* argv[])
|
|||||||
zoom = atof(argv[4]);
|
zoom = atof(argv[4]);
|
||||||
} else {
|
} else {
|
||||||
filename = string(argv[1]);
|
filename = string(argv[1]);
|
||||||
cx = atof(argv[2]);
|
if (!autozoom) {
|
||||||
cy = atof(argv[3]);
|
cx = atof(argv[2]);
|
||||||
zoom = atof(argv[4]);
|
cy = atof(argv[3]);
|
||||||
|
zoom = atof(argv[4]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (random_distribution) {
|
if (random_distribution) {
|
||||||
@ -136,7 +155,21 @@ int main(int argc, char* argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Init();
|
if (autozoom) {
|
||||||
|
assert(0.0 <= autozoom_border && autozoom_border < 1.0);
|
||||||
|
const auto bbox = BoundingBox(polyline);
|
||||||
|
Point center = bbox.first + bbox.second;
|
||||||
|
center *= 0.5;
|
||||||
|
cx = center.x;
|
||||||
|
cy = center.y;
|
||||||
|
Point sides = bbox.second - bbox.first;
|
||||||
|
zoom = 2.0 * (1.0 - autozoom_border) * std::min((double)default_window_width / sides.x, (double)default_window_height / sides.y);
|
||||||
|
std::cout << "center_x = " << cx << std::endl;
|
||||||
|
std::cout << "center_y = " << cy << std::endl;
|
||||||
|
std::cout << "zoom = " << zoom << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Init(default_window_width, default_window_height);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Perform triangulation!
|
* Perform triangulation!
|
||||||
@ -182,6 +215,7 @@ int main(int argc, char* argv[])
|
|||||||
cout << "Total number of points = " << (polyline.size() + points_in_holes + steiner.size())
|
cout << "Total number of points = " << (polyline.size() + points_in_holes + steiner.size())
|
||||||
<< endl;
|
<< endl;
|
||||||
cout << "Number of triangles = " << triangles.size() << endl;
|
cout << "Number of triangles = " << triangles.size() << endl;
|
||||||
|
cout << "Is Delaunay = " << (IsDelaunay(triangles) ? "true" : "false") << endl;
|
||||||
cout << "Elapsed time (ms) = " << dt * 1000.0 << endl;
|
cout << "Elapsed time (ms) = " << dt * 1000.0 << endl;
|
||||||
|
|
||||||
MainLoop(zoom);
|
MainLoop(zoom);
|
||||||
@ -262,6 +296,21 @@ bool ParseFile(string filename, vector<Point*>& out_polyline, vector<vector<Poin
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::pair<Point, Point> BoundingBox(const std::vector<Point*>& polyline)
|
||||||
|
{
|
||||||
|
assert(polyline.size() > 0);
|
||||||
|
using Scalar = decltype(p2t::Point::x);
|
||||||
|
Point min(std::numeric_limits<Scalar>::max(), std::numeric_limits<Scalar>::max());
|
||||||
|
Point max(std::numeric_limits<Scalar>::min(), std::numeric_limits<Scalar>::min());
|
||||||
|
for (const Point* point : polyline) {
|
||||||
|
min.x = std::min(min.x, point->x);
|
||||||
|
min.y = std::min(min.y, point->y);
|
||||||
|
max.x = std::max(max.x, point->x);
|
||||||
|
max.y = std::max(max.y, point->y);
|
||||||
|
}
|
||||||
|
return std::make_pair(min, max);
|
||||||
|
}
|
||||||
|
|
||||||
void GenerateRandomPointDistribution(size_t num_points, double min, double max,
|
void GenerateRandomPointDistribution(size_t num_points, double min, double max,
|
||||||
vector<Point*>& out_polyline,
|
vector<Point*>& out_polyline,
|
||||||
vector<vector<Point*>>& out_holes, vector<Point*>& out_steiner)
|
vector<vector<Point*>>& out_holes, vector<Point*>& out_steiner)
|
||||||
@ -280,14 +329,11 @@ void GenerateRandomPointDistribution(size_t num_points, double min, double max,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Init()
|
void Init(int window_width, int window_height)
|
||||||
{
|
{
|
||||||
const int window_width = 800,
|
|
||||||
window_height = 600;
|
|
||||||
|
|
||||||
if (glfwInit() != GL_TRUE)
|
if (glfwInit() != GL_TRUE)
|
||||||
ShutDown(1);
|
ShutDown(1);
|
||||||
// 800 x 600, 16 bit color, no depth, alpha or stencil buffers, windowed
|
// width x height, 16 bit color, no depth, alpha or stencil buffers, windowed
|
||||||
window = glfwCreateWindow(window_width, window_height, "Poly2Tri - C++", NULL, NULL);
|
window = glfwCreateWindow(window_width, window_height, "Poly2Tri - C++", NULL, NULL);
|
||||||
if (!window)
|
if (!window)
|
||||||
ShutDown(1);
|
ShutDown(1);
|
||||||
@ -373,7 +419,7 @@ void Draw(const double zoom)
|
|||||||
// reset zoom
|
// reset zoom
|
||||||
Point center = Point(cx, cy);
|
Point center = Point(cx, cy);
|
||||||
|
|
||||||
ResetZoom(zoom, center.x, center.y, 800, 600);
|
ResetZoom(zoom, center.x, center.y, (double)default_window_width, (double)default_window_height);
|
||||||
|
|
||||||
for (int i = 0; i < triangles.size(); i++) {
|
for (int i = 0; i < triangles.size(); i++) {
|
||||||
Triangle& t = *triangles[i];
|
Triangle& t = *triangles[i];
|
||||||
@ -414,7 +460,7 @@ void DrawMap(const double zoom)
|
|||||||
// reset zoom
|
// reset zoom
|
||||||
Point center = Point(cx, cy);
|
Point center = Point(cx, cy);
|
||||||
|
|
||||||
ResetZoom(zoom, center.x, center.y, 800, 600);
|
ResetZoom(zoom, center.x, center.y, (double)default_window_width, (double)default_window_height);
|
||||||
|
|
||||||
list<Triangle*>::iterator it;
|
list<Triangle*>::iterator it;
|
||||||
for (it = map.begin(); it != map.end(); it++) {
|
for (it = map.begin(); it != map.end(); it++) {
|
||||||
@ -454,8 +500,6 @@ void ConstrainedColor(bool constrain)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
double StringToDouble(const std::string& s)
|
double StringToDouble(const std::string& s)
|
||||||
{
|
{
|
||||||
std::istringstream i(s);
|
std::istringstream i(s);
|
||||||
|
Loading…
Reference in New Issue
Block a user