Skip to content
Browse files

Re-adds check for arguments as array, returns results wrapped in arra…

…y (to account for the future possibility of MultiArray input.
  • Loading branch information...
1 parent 2a2418f commit e993ff04ee1fa0d9759367ebc7d3e6ead58412aa Sean Ouimet committed Aug 15, 2012
Showing with 47 additions and 32 deletions.
  1. +47 −32 src/init.cc
View
79 src/init.cc
@@ -12,44 +12,59 @@ using namespace ClipperLib;
v8::Handle<Value> Method(const Arguments& args) {
v8::HandleScope scope;
- // if (args[0]->IsArray()) {
- v8::Local<v8::Array> points = v8::Array::Cast(*args[0]);
-
- Polygons polyshape(1);
- Polygon polyout;
-
- // Populate the subject polygon
- for (int i = 0, limiti = points->Length(); i < limiti; i += 2) {
- // pair = v8::Array::New(2);
- // pair->set(0, points[0]);
- v8::Local<v8::Value> pairA = points->Get(i);
- v8::Local<v8::Value> pairB = points->Get(i+1);
- polyshape[0].push_back(
- IntPoint(
- pairA->NumberValue(),
- pairB->NumberValue()
- )
- );
- }
+ if (args[0]->IsArray()) {
+ try {
+ v8::Local<v8::Array> points = v8::Array::Cast(*args[0]);
- ClipperLib::OffsetPolygons(polyshape, polyshape, args[1]->NumberValue(), jtMiter, 3);
+ ClipperLib::Polygons polyshape(1), polydone;
- if (polyshape.size() > 0) {
- Handle<Array> outPoints = Array::New(); //polyshape[0].size() * 2
- v8::Local<v8::Number> total = v8::Number::New(polyshape[0].size());
+ // Populate the subject polygon
+ for (int i = 0, limiti = points->Length(); i < limiti; i += 2) {
+ // pair = v8::Array::New(2);
+ // pair->set(0, points[0]);
+ v8::Local<v8::Value> pairA = points->Get(i);
+ v8::Local<v8::Value> pairB = points->Get(i+1);
+ polyshape[0].push_back(
+ IntPoint(
+ pairA->NumberValue(),
+ pairB->NumberValue()
+ )
+ );
+ }
- for (Polygon::size_type k = 0; k < polyshape[0].size(); ++k) {
- IntPoint ip = polyshape[0][k];
- v8::Local<v8::Number> x = v8::Number::New(ip.X);
- v8::Local<v8::Number> y = v8::Number::New(ip.Y);
- outPoints->Set(outPoints->Length(), x);
- outPoints->Set(outPoints->Length(), y);
+ bool wasReversed = false;
+ if (!ClipperLib::Orientation(polyshape[0])) {
+ ClipperLib::ReversePolygons(polyshape[0]);
+ wasReversed = true;
}
- return scope.Close(outPoints);
- }
+ double miterLimit = 3.0;
- // }
+ ClipperLib::OffsetPolygons(polyshape, polydone, args[1]->NumberValue(), jtMiter, miterLimit);
+
+ if (!wasReversed && ClipperLib::Orientation(polydone[0])) {
+ ClipperLib::ReversePolygons(polydone);
+ }
+
+ if (polydone.size() > 0) {
+ Handle<Array> outPolygons = Array::New();
+ for (Polygon::size_type g = 0; g < polydone.size(); ++g) {
+ Handle<Array> outPoints = Array::New();
+ for (Polygon::size_type k = 0; k < polydone[g].size(); ++k) {
+ IntPoint ip = polydone[g][k];
+ v8::Local<v8::Number> x = v8::Number::New(ip.X);
+ v8::Local<v8::Number> y = v8::Number::New(ip.Y);
+ outPoints->Set(outPoints->Length(), x);
+ outPoints->Set(outPoints->Length(), y);
+ }
+ outPolygons->Set(g, outPoints);
+ }
+ return scope.Close(outPolygons);
+ }
+ } catch (...) {
+ return scope.Close(v8::Boolean::New(false));
+ }
+ }
return scope.Close(v8::Undefined());
}

0 comments on commit e993ff0

Please sign in to comment.
Something went wrong with that request. Please try again.