-
Notifications
You must be signed in to change notification settings - Fork 100
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
Use struct instead of Delaunator class #7
Comments
I think this makes sense. I didn't yet get rid of class-based API because it's harder to make it as fast in JS (passing around lots of arguments has an overhead), but I might switch in future. I also got rid of some properties in recent commits — now the result of the triangulation is three arrays, |
I wonder which interface will be better? Option 1: very simple and difficult to make a mistake but not very modern
Option 2: bit more modern (maybe too modern?)
Option 3? / cc @flippmoke |
I might suggest the following: // structure internal to library
// used for storing all our data that is relative to each index
struct TriangulatePoint {
std::size_t triangle;
std::size_t halfedge;
std::size_t hull;
}
// Point structure defined by user but templated out here
struct MyPoint {
double x;
double y;
// other data could be here
}
// Our vector of point data
std::vector<MyPoint> coords = {....};
// Users will have to define a custom get_x(), get_y() function here
// as some users might have slightly different format for point data
// this is a template that is defined within our library and we provide a
// predefined delaunator::Point if the user wants to use it, but otherwise
// can define their own.
double delaunator::get_x(MyPoint const& pt) {
return pt.x
}
double delaunator::get_y(MyPoint const& pt) {
return pt.y
}
// User can now call triangulate
std::vector<TriangulatePoint> out = delaunator::triangulate(coords); |
struct DelaunatorResult {
std::size_t triangles;
std::size_t halfedges;
} |
@flippmoke do I need to do something special to avoid copying of vectors in this case? like implementing move constructor in
|
@delfrrr FYI, I ported Delaunator to Rust as a learning project. Made some refactoring there to make the code simpler and clearer, and exposed a struct-based API. The C++ version will probably benefit from the same structure. Check it out: https://github.com/mourner/delaunator-rs |
Sorry, I have been on vacation.
std::vector<DelaunatorResult> out;
out = delaunator::triangulate(coords);//vectors will be copied 😱? This is not a style I would ever suggest that a user of said library ever use, but if you did here it would result in the use of the std vector operator=. This has a move operator included and assuming that |
@flippmoke np; I did a mistake which led to confusion; my question will be relevant for this code
how to avoid copy in this scenario, given that RVO is not available (or RVO will be always available given the other constrains as |
@delfrrr I am not sure why RVO would not be available with your example assuming that |
@flippmoke anything else?
The text was updated successfully, but these errors were encountered: