Skip to content

Commit

Permalink
Port AffTransformation2 to N-API
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzm committed Aug 9, 2019
1 parent 70baf00 commit 695e216
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 118 deletions.
2 changes: 1 addition & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'sources': [
'src/cgal.cc',
# 'src/AffTransformation2.cc',
'src/AffTransformation2.cc',
# 'src/Arrangement2.cc',
# 'src/Arrangement2Face.cc',
# 'src/Arrangement2Halfedge.cc',
Expand Down
14 changes: 7 additions & 7 deletions spec/Point2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ describe("CGAL.Point2", function() {
expect(p.x()).toEqual(q.x());
});

// it("should be transformable", function() {
// var p = new CGAL.Point2([1,2]);
// var q = p.transform([1, 0, 3, 0, 1, 3]);
// expect(q instanceof CGAL.Point2).toBeTruthy();
// expect(q.x()).toEqual(4);
// expect(q.y()).toEqual(5);
// });
it("should be transformable", function() {
var p = new CGAL.Point2([1,2]);
var q = p.transform([1, 0, 3, 0, 1, 3]);
expect(q instanceof CGAL.Point2).toBeTruthy();
expect(q.x()).toEqual(4);
expect(q.y()).toEqual(5);
});
});
12 changes: 6 additions & 6 deletions spec/Polygon2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ describe("CGAL.Polygon2", function() {
expect(p3.area()).toBeCloseTo(-1.0, 4);
});

// it ("should support transformation", function() {
// var p1 = new CGAL.Polygon2([[0, 0], [1, 0], [1, 1], [0, 1]]);
// var p2;
// expect(function() {p2 = CGAL.Polygon2.transform([1, 0, 3, 0, 1, 3], p1);}).not.toThrow();
// expect(p2.toPOD(false)[0]).toEqual([3,3]);
// });
it ("should support transformation", function() {
var p1 = new CGAL.Polygon2([[0, 0], [1, 0], [1, 1], [0, 1]]);
var p2;
expect(function() {p2 = CGAL.Polygon2.transform([1, 0, 3, 0, 1, 3], p1);}).not.toThrow();
expect(p2.toPOD(false)[0]).toEqual([3,3]);
});
});
91 changes: 39 additions & 52 deletions src/AffTransformation2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,60 @@
#include "Direction2.h"
#include "cgal_args.h"

using namespace v8;
using namespace node;
using namespace std;


AffTransformation2::AffTransformation2(Napi::CallbackInfo const& info)
: CGALWrapper(info)
{
}


const char *AffTransformation2::Name = "AffTransformation2";


void AffTransformation2::RegisterMethods(v8::Isolate *isolate)
void AffTransformation2::AddProperties(std::vector<PropertyDescriptor>& properties)
{
}


bool AffTransformation2::ParseArg(Isolate *isolate, Local<Value> arg, Aff_transformation_2 &receiver)
bool AffTransformation2::ParseArg(Napi::Env env, Napi::Value arg, Aff_transformation_2& receiver)
{
HandleScope scope(isolate);
Local<Context> context = isolate->GetCurrentContext();

if (sConstructorTemplate.Get(isolate)->HasInstance(arg)) {
receiver = ExtractWrapped(Local<Object>::Cast(arg));
if (arg.IsObject() && arg.As<Napi::Object>().InstanceOf(sConstructor.Value())) {
receiver = Unwrap(arg.As<Napi::Object>())->mWrapped;
return true;
}

if (arg->IsArray()) {
Local<Array> inits = Local<Array>::Cast(arg);

if (inits->Length() >= 6 && inits->Length() <= 7) {
if (arg.IsArray()) {
Napi::Array inits = arg.As<Napi::Array>();

if (inits.Length() >= 6 && inits.Length() <= 7) {
K::RT m00, m01, m02, m10, m11, m12, hw(1);
if (::ParseArg(isolate, inits->Get(context, 0).ToLocalChecked(), m00) &&
::ParseArg(isolate, inits->Get(context, 1).ToLocalChecked(), m01) &&
::ParseArg(isolate, inits->Get(context, 2).ToLocalChecked(), m02) &&
::ParseArg(isolate, inits->Get(context, 3).ToLocalChecked(), m10) &&
::ParseArg(isolate, inits->Get(context, 4).ToLocalChecked(), m11) &&
::ParseArg(isolate, inits->Get(context, 5).ToLocalChecked(), m12))
if (::ParseNumberArg(env, inits[0u], m00) &&
::ParseNumberArg(env, inits[1], m01) &&
::ParseNumberArg(env, inits[2], m02) &&
::ParseNumberArg(env, inits[3], m10) &&
::ParseNumberArg(env, inits[4], m11) &&
::ParseNumberArg(env, inits[5], m12))
{
if ((inits->Length() == 7) && !::ParseArg(isolate, inits->Get(context, 6).ToLocalChecked(), hw))
if ((inits.Length() == 7) && !::ParseNumberArg(env, inits[6], hw))
return false;

receiver = Aff_transformation_2(m00, m01, m02, m10, m11, m12, hw);
return true;
}

}

if (inits->Length() >= 2 && inits->Length() <= 3) {

if (inits.Length() >= 2 && inits.Length() <= 3) {
Direction_2 dir;
K::RT num, den(1);
if (Direction2::ParseArg(isolate, inits->Get(context, 0).ToLocalChecked(), dir) &&
::ParseArg(isolate, inits->Get(context, 1).ToLocalChecked(), num))
if (Direction2::ParseArg(env, inits[0u], dir) &&
::ParseNumberArg(env, inits[1], num))
{
if ((inits->Length() == 3) && !::ParseArg(isolate, inits->Get(context, 2).ToLocalChecked(), den))
if ((inits.Length() == 3) && !::ParseNumberArg(env, inits[2], den))
return false;

receiver = Aff_transformation_2(Rotation(), dir, num, den);
return true;
}

}

}
Expand All @@ -69,35 +64,27 @@ bool AffTransformation2::ParseArg(Isolate *isolate, Local<Value> arg, Aff_transf
}


Local<Value> AffTransformation2::ToPOD(Isolate *isolate, const Aff_transformation_2 &aff, bool precise)
Napi::Value AffTransformation2::ToPOD(Napi::Env env, Aff_transformation_2 const& aff, bool precise)
{
EscapableHandleScope scope(isolate);
Local<Context> context = isolate->GetCurrentContext();
Local<Array> array = Array::New(isolate, 7);

try {
for(int i=0; i<7; ++i) {
int r = (i == 6) ? 2 : i/3;
int c = (i == 6) ? 2 : i%3;
K::RT a = aff.hm(r, c);
if (precise) {
ostringstream str;
Napi::Array array = Napi::Array::New(env, 7);

for(int i=0; i<7; ++i) {
int r = (i == 6) ? 2 : i/3;
int c = (i == 6) ? 2 : i%3;
K::RT a = aff.hm(r, c);
if (precise) {
ostringstream str;
#ifdef CGAL_USE_EPECK
str << a.exact();
str << a.exact();
#else
str << a;
str << a;
#endif
array->Set(context, i, String::NewFromUtf8(isolate, str.str().c_str(), NewStringType::kNormal).ToLocalChecked());
} else {
array->Set(context, i, Number::New(isolate, CGAL::to_double(a)));
}
array.Set(i, str.str());
} else {
array.Set(i, CGAL::to_double(a));
}
}

catch (const exception &e) {
isolate->ThrowException(String::NewFromUtf8(isolate, e.what(), NewStringType::kNormal).ToLocalChecked());
}

return scope.Escape(array);
return array;
}

19 changes: 11 additions & 8 deletions src/AffTransformation2.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@

#include "CGALWrapper.h"
#include "cgal_types.h"
#include "v8.h"
#include "napi.h"

#include <vector>

class AffTransformation2 : public CGALWrapper<AffTransformation2, Aff_transformation_2>
{
public:

AffTransformation2(Napi::CallbackInfo const& info);

// The name to be used for our JS class.
static const char *Name;

// Add our function templates to the package exports, and return string to be used to name
// the class and constructor in JS. Called indirectly at module load time via the module
// Add our property descriptors (instance and static methods and values) to the list that will
// be used to define our JS class. Called indirectly at module load time via the module
// init function.
static void RegisterMethods(v8::Isolate *isolate);
static void AddProperties(std::vector<PropertyDescriptor>& properties);

// Attempt to parse a v8 argument into the CGAL object referred to by receiver. Returns true
// Attempt to parse a JS argument into the CGAL object referred to by receiver. Returns true
// if parse was successful, false otherwise.
static bool ParseArg(v8::Isolate *isolate, v8::Local<v8::Value> arg, Aff_transformation_2 &receiver);
static bool ParseArg(Napi::Env env, Napi::Value arg, Aff_transformation_2& receiver);

// Convert a CGAL object of the wrapped class to a POD v8 object. If precise is set to false,
// Convert a CGAL object of the wrapped class to a POD JS object. If precise is set to false,
// will attempt to render in terms of doubles for coordinates, and may lose precision.
static v8::Local<v8::Value> ToPOD(v8::Isolate *isolate, const Aff_transformation_2 &aff, bool precise=true);
static Napi::Value ToPOD(Napi::Env env, Aff_transformation_2 const& aff, bool precise=true);

private:

Expand Down
26 changes: 13 additions & 13 deletions src/Point2.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Point2.h"
// #include "AffTransformation2.h"
#include "AffTransformation2.h"
#include "cgal_args.h"
#include "iomanip"

Expand All @@ -20,8 +20,8 @@ void Point2::AddProperties(vector<PropertyDescriptor>& properties)
properties.insert(properties.end(), {
InstanceMethod("isEqual", &Point2::IsEqual),
InstanceMethod("x", &Point2::X),
InstanceMethod("y", &Point2::Y)
// InstanceMethod("transform", &Point2::Transform)
InstanceMethod("y", &Point2::Y),
InstanceMethod("transform", &Point2::Transform)
});
}

Expand Down Expand Up @@ -98,13 +98,13 @@ Napi::Value Point2::Y(Napi::CallbackInfo const& info)
}


// Napi::Value Point2::Transform(Napi::CallbackInfo const& info)
// {
// Napi::Env env = info.Env();
// ARGS_ASSERT(isolate, info.Length() == 1);
// Aff_transformation_2 afft;
// if (AffTransformation2::ParseArg(env, info[0], afft)) {
// return mWrapped.transform(afft);
// }
// ARGS_ASSERT(env, false);
// }
Napi::Value Point2::Transform(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
ARGS_ASSERT(isolate, info.Length() == 1);
Aff_transformation_2 aff;
if (AffTransformation2::ParseArg(env, info[0], aff)) {
return Point2::New(env, mWrapped.transform(aff));
}
ARGS_ASSERT(env, false);
}
2 changes: 1 addition & 1 deletion src/Point2.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Point2 : public CGALWrapper<Point2, Point_2>
Napi::Value IsEqual(Napi::CallbackInfo const& info);
Napi::Value X(Napi::CallbackInfo const& info);
Napi::Value Y(Napi::CallbackInfo const& info);
// Napi::Value Transform(Napi::CallbackInfo const& info);
Napi::Value Transform(Napi::CallbackInfo const& info);

};

Expand Down
48 changes: 21 additions & 27 deletions src/Polygon2.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Polygon2.h"
#include "Point2.h"
// #include "AffTransformation2.h"
#include "AffTransformation2.h"
#include "cgal_args.h"

using namespace std;
Expand All @@ -25,8 +25,8 @@ void Polygon2::AddProperties(std::vector<PropertyDescriptor>& properties)
InstanceMethod("orientedSide", &Polygon2::OrientedSide),
InstanceMethod("boundedSide", &Polygon2::BoundedSide),
InstanceMethod("area", &Polygon2::Area),
InstanceMethod("coords", &Polygon2::Coords)
// NODE_SET_METHOD(constructorTemplate, "transform", Transform);
InstanceMethod("coords", &Polygon2::Coords),
StaticMethod("transform", &Polygon2::Transform)
});
}

Expand Down Expand Up @@ -103,30 +103,24 @@ Napi::Value Polygon2::Area(Napi::CallbackInfo const& info)
}


// void Polygon2::Transform(const FunctionCallbackInfo<Value> &info)
// {
// Isolate *isolate = info.GetIsolate();
// HandleScope scope(isolate);
// try {
// ARGS_ASSERT(isolate, info.Length() == 2);

// Aff_transformation_2 afft;
// if (!AffTransformation2::ParseArg(isolate, info[0], afft)) {
// ARGS_ASSERT(isolate, false);
// }

// Polygon_2 poly;
// if (!ParseArg(isolate, info[1], poly)) {
// ARGS_ASSERT(isolate, false);
// }

// Polygon_2 xpoly = CGAL::transform(afft, poly);
// info.GetReturnValue().Set(New(isolate, xpoly));
// }
// catch (const exception &e) {
// isolate->ThrowException(String::NewFromUtf8(isolate, e.what(), NewStringType::kNormal).ToLocalChecked());
// }
// }
Napi::Value Polygon2::Transform(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();

ARGS_ASSERT(env, info.Length() == 2);

Aff_transformation_2 afft;
if (!AffTransformation2::ParseArg(env, info[0u], afft)) {
ARGS_ASSERT(env, false);
}

Polygon_2 poly;
if (!ParseArg(env, info[1], poly)) {
ARGS_ASSERT(env, false);
}

return Polygon2::New(env, CGAL::transform(afft, poly));
}


Napi::Value Polygon2::Coords(Napi::CallbackInfo const& info)
Expand Down
2 changes: 1 addition & 1 deletion src/Polygon2.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Polygon2 : public CGALWrapper<Polygon2, Polygon_2>
Napi::Value OrientedSide(Napi::CallbackInfo const& info);
Napi::Value BoundedSide(Napi::CallbackInfo const& info);
Napi::Value Area(Napi::CallbackInfo const& info);
// Napi::Value Transform(Napi::CallbackInfo const& info);
static Napi::Value Transform(Napi::CallbackInfo const& info);
Napi::Value Coords(Napi::CallbackInfo const& info);

};
Expand Down
4 changes: 2 additions & 2 deletions src/cgal.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "cgal.h"
#include "cgal_types.h"
// #include "AffTransformation2.h"
#include "AffTransformation2.h"
// #include "Arrangement2.h"
#include "BBox2.h"
// #include "Curve2.h"
Expand Down Expand Up @@ -48,7 +48,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
exports.Set("ARR_TOP_BOUNDARY", (int)CGAL::ARR_TOP_BOUNDARY);
exports.Set("ARR_INTERIOR", (int)CGAL::ARR_INTERIOR);

// AffTransformation2::Init(env, exports);
AffTransformation2::Init(env, exports);
// Arrangement2::Init(env, exports);
BBox2::Init(env, exports);
// Curve2::Init(env, exports);
Expand Down

0 comments on commit 695e216

Please sign in to comment.