@@ -15,11 +15,10 @@ Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
1515
1616#include < iostream>
1717#include < string>
18- #include < iostream>
1918#include < sstream>
2019#include < limits>
2120
22- #include < nan .h>
21+ #include < napi .h>
2322#include < boost/polygon/polygon.hpp>
2423
2524#undef min
@@ -107,56 +106,42 @@ void convolve_two_polygon_sets(polygon_set& result, const polygon_set& a, const
107106
108107double inputscale;
109108
110- using v8::Local;
111- using v8::Array;
112- using v8::Isolate;
113- using v8::String;
114- using v8::Local;
115- using v8::Object;
116- using v8::Value;
117-
118- using namespace boost ::polygon;
119-
120- NAN_METHOD (calculateNFP) {
121- // std::stringstream buffer;
122- // std::streambuf * old = std::cout.rdbuf(buffer.rdbuf());
123- v8::Local<v8::Context> context = info.GetIsolate ()->GetCurrentContext ();
124-
125- Isolate* isolate = info.GetIsolate ();
126-
127- Local<Object> group = Local<Object>::Cast (info[0 ]);
128- Local<Array> A = Local<Array>::Cast (group->Get (isolate->GetCurrentContext (), String::NewFromUtf8 (isolate," A" ,v8::NewStringType::kNormal ).ToLocalChecked ()).ToLocalChecked ());
129- Local<Array> B = Local<Array>::Cast (group->Get (isolate->GetCurrentContext (), String::NewFromUtf8 (isolate," B" ,v8::NewStringType::kNormal ).ToLocalChecked ()).ToLocalChecked ());
109+ Napi::Value CalculateNFP (const Napi::CallbackInfo& info) {
110+ Napi::Env env = info.Env ();
111+
112+ Napi::Object group = info[0 ].As <Napi::Object>();
113+ Napi::Array A = group.Get (" A" ).As <Napi::Array>();
114+ Napi::Array B = group.Get (" B" ).As <Napi::Array>();
130115
131116 polygon_set a, b, c;
132117 std::vector<polygon> polys;
133118 std::vector<point> pts;
134119
135120 // get maximum bounds for scaling factor
136- unsigned int len = A-> Length ();
121+ unsigned int len = A. Length ();
137122 double Amaxx = 0 ;
138123 double Aminx = 0 ;
139124 double Amaxy = 0 ;
140125 double Aminy = 0 ;
141126 for (unsigned int i = 0 ; i < len; i++) {
142- Local< Object> obj = Local<Object>:: Cast (A-> Get (isolate-> GetCurrentContext (), i).ToLocalChecked () );
143- Amaxx = (std::max)(Amaxx, ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " x" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ());
144- Aminx = (std::min)(Aminx, ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " x" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ());
145- Amaxy = (std::max)(Amaxy, ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " y" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ());
146- Aminy = (std::min)(Aminy, ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " y" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ());
127+ Napi:: Object obj = A. Get (i).As <Napi::Object>( );
128+ Amaxx = (std::max)(Amaxx, obj. Get (" x" ). As <Napi::Number>(). DoubleValue ());
129+ Aminx = (std::min)(Aminx, obj. Get (" x" ). As <Napi::Number>(). DoubleValue ());
130+ Amaxy = (std::max)(Amaxy, obj. Get (" y" ). As <Napi::Number>(). DoubleValue ());
131+ Aminy = (std::min)(Aminy, obj. Get (" y" ). As <Napi::Number>(). DoubleValue ());
147132 }
148133
149- len = B-> Length ();
134+ len = B. Length ();
150135 double Bmaxx = 0 ;
151136 double Bminx = 0 ;
152137 double Bmaxy = 0 ;
153138 double Bminy = 0 ;
154139 for (unsigned int i = 0 ; i < len; i++) {
155- Local< Object> obj = Local<Object>:: Cast (B-> Get (isolate-> GetCurrentContext (), i).ToLocalChecked () );
156- Bmaxx = (std::max)(Bmaxx, ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " x" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ());
157- Bminx = (std::min)(Bminx, ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " x" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ());
158- Bmaxy = (std::max)(Bmaxy, ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " y" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ());
159- Bminy = (std::min)(Bminy, ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " y" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ());
140+ Napi:: Object obj = B. Get (i).As <Napi::Object>( );
141+ Bmaxx = (std::max)(Bmaxx, obj. Get (" x" ). As <Napi::Number>(). DoubleValue ());
142+ Bminx = (std::min)(Bminx, obj. Get (" x" ). As <Napi::Number>(). DoubleValue ());
143+ Bmaxy = (std::max)(Bmaxy, obj. Get (" y" ). As <Napi::Number>(). DoubleValue ());
144+ Bminy = (std::min)(Bminy, obj. Get (" y" ). As <Napi::Number>(). DoubleValue ());
160145 }
161146
162147 double Cmaxx = Amaxx + Bmaxx;
@@ -171,19 +156,19 @@ NAN_METHOD(calculateNFP) {
171156 int maxi = std::numeric_limits<int >::max ();
172157
173158 if (maxda < 1 ){
174- maxda = 1 ;
159+ maxda = 1 ;
175160 }
176161
177162 // why 0.1? dunno. it doesn't screw up with 0.1
178163 inputscale = (0 .1f * (double )(maxi)) / maxda;
179164
180165 // double scale = 1000;
181- len = A-> Length ();
166+ len = A. Length ();
182167
183168 for (unsigned int i = 0 ; i < len; i++) {
184- Local< Object> obj = Local<Object>:: Cast (A-> Get (isolate-> GetCurrentContext (), i).ToLocalChecked () );
185- int x = (int )(inputscale * ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " x" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ());
186- int y = (int )(inputscale * ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " y" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ());
169+ Napi:: Object obj = A. Get (i).As <Napi::Object>( );
170+ int x = (int )(inputscale * obj. Get (" x" ). As <Napi::Number>(). DoubleValue ());
171+ int y = (int )(inputscale * obj. Get (" y" ). As <Napi::Number>(). DoubleValue ());
187172
188173 pts.push_back (point (x, y));
189174 }
@@ -193,40 +178,42 @@ NAN_METHOD(calculateNFP) {
193178 a+=poly;
194179
195180 // subtract holes from a here...
196- Local<Array> holes = Local<Array>::Cast (A->Get (isolate->GetCurrentContext (), String::NewFromUtf8 (isolate," children" ,v8::NewStringType::kNormal ).ToLocalChecked ()).ToLocalChecked ());
197- len = holes->Length ();
198-
199- for (unsigned int i=0 ; i<len; i++){
200- Local<Array> hole = Local<Array>::Cast (holes->Get (isolate->GetCurrentContext (), i).ToLocalChecked ());
201- pts.clear ();
202- unsigned int hlen = hole->Length ();
203- for (unsigned int j=0 ; j<hlen; j++){
204- Local<Object> obj = Local<Object>::Cast (hole->Get (isolate->GetCurrentContext (), j).ToLocalChecked ());
205- int x = (int )(inputscale * (double )obj->Get (isolate->GetCurrentContext (), String::NewFromUtf8 (isolate," x" ,v8::NewStringType::kNormal ).ToLocalChecked ()).ToLocalChecked ()->NumberValue (context).FromJust ());
206- int y = (int )(inputscale * (double )obj->Get (isolate->GetCurrentContext (), String::NewFromUtf8 (isolate," y" ,v8::NewStringType::kNormal ).ToLocalChecked ()).ToLocalChecked ()->NumberValue (context).FromJust ());
207- pts.push_back (point (x, y));
181+ if (A.Has (" children" )) {
182+ Napi::Array holes = A.Get (" children" ).As <Napi::Array>();
183+ len = holes.Length ();
184+
185+ for (unsigned int i=0 ; i<len; i++){
186+ Napi::Array hole = holes.Get (i).As <Napi::Array>();
187+ pts.clear ();
188+ unsigned int hlen = hole.Length ();
189+ for (unsigned int j=0 ; j<hlen; j++){
190+ Napi::Object obj = hole.Get (j).As <Napi::Object>();
191+ int x = (int )(inputscale * obj.Get (" x" ).As <Napi::Number>().DoubleValue ());
192+ int y = (int )(inputscale * obj.Get (" y" ).As <Napi::Number>().DoubleValue ());
193+ pts.push_back (point (x, y));
194+ }
195+ boost::polygon::set_points (poly, pts.begin (), pts.end ());
196+ a -= poly;
208197 }
209- boost::polygon::set_points (poly, pts.begin (), pts.end ());
210- a -= poly;
211198 }
212199
213200 // and then load points B
214201 pts.clear ();
215- len = B-> Length ();
202+ len = B. Length ();
216203
217204 // javascript nfps are referenced with respect to the first point
218205 double xshift = 0 ;
219206 double yshift = 0 ;
220207
221208 for (unsigned int i = 0 ; i < len; i++) {
222- Local< Object> obj = Local<Object>:: Cast (B-> Get (isolate-> GetCurrentContext (), i).ToLocalChecked () );
223- int x = -(int )(inputscale * ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " x" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ());
224- int y = -(int )(inputscale * ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " y" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ());
209+ Napi:: Object obj = B. Get (i).As <Napi::Object>( );
210+ int x = -(int )(inputscale * obj. Get (" x" ). As <Napi::Number>(). DoubleValue ());
211+ int y = -(int )(inputscale * obj. Get (" y" ). As <Napi::Number>(). DoubleValue ());
225212 pts.push_back (point (x, y));
226213
227214 if (i==0 ){
228- xshift = ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " x" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ();
229- yshift = ( double ) obj-> Get (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " y" ,v8::NewStringType:: kNormal ). ToLocalChecked ()). ToLocalChecked ()-> NumberValue (context). FromJust ();
215+ xshift = obj. Get (" x" ). As <Napi::Number>(). DoubleValue ();
216+ yshift = obj. Get (" y" ). As <Napi::Number>(). DoubleValue ();
230217 }
231218 }
232219
@@ -238,47 +225,44 @@ NAN_METHOD(calculateNFP) {
238225 convolve_two_polygon_sets (c, a, b);
239226 c.get (polys);
240227
241- Local< Array> result_list = Array::New (isolate );
228+ Napi:: Array result_list = Napi:: Array::New (env );
242229
243230 for (unsigned int i = 0 ; i < polys.size (); ++i ){
244231
245- Local<Array> pointlist = Array::New (isolate);
246- int j = 0 ;
247-
248- for (polygon_traits<polygon>::iterator_type itr = polys[i].begin (); itr != polys[i].end (); ++itr) {
249- Local<Object> p = Object::New (isolate);
250- // std::cout << (double)(*itr).get(boost::polygon::HORIZONTAL) / inputscale << std::endl;
251- p->Set (isolate->GetCurrentContext (), String::NewFromUtf8 (isolate," x" ,v8::NewStringType::kNormal ).ToLocalChecked (), v8::Number::New (isolate, ((double )(*itr).get (boost::polygon::HORIZONTAL)) / inputscale + xshift));
252- p->Set (isolate->GetCurrentContext (), String::NewFromUtf8 (isolate," y" ,v8::NewStringType::kNormal ).ToLocalChecked (), v8::Number::New (isolate, ((double )(*itr).get (boost::polygon::VERTICAL)) / inputscale + yshift));
232+ Napi::Array pointlist = Napi::Array::New (env);
233+ int j = 0 ;
234+
235+ for (polygon_traits<polygon>::iterator_type itr = polys[i].begin (); itr != polys[i].end (); ++itr) {
236+ Napi::Object p = Napi::Object::New (env);
237+ p.Set (" x" , ((double )(*itr).get (boost::polygon::HORIZONTAL)) / inputscale + xshift);
238+ p.Set (" y" , ((double )(*itr).get (boost::polygon::VERTICAL)) / inputscale + yshift);
253239
254- pointlist-> Set (isolate-> GetCurrentContext (), j, p) ;
240+ pointlist[j] = p ;
255241 j++;
256242 }
257243
258244 // holes
259- Local< Array> children = Array::New (isolate );
245+ Napi:: Array children = Napi:: Array::New (env );
260246 int k = 0 ;
261247 for (polygon_with_holes_traits<polygon>::iterator_holes_type itrh = begin_holes (polys[i]); itrh != end_holes (polys[i]); ++itrh){
262- Local< Array> child = Array::New (isolate );
263- int z = 0 ;
264- for (polygon_traits<polygon>::iterator_type itr2 = (*itrh).begin (); itr2 != (*itrh).end (); ++itr2) {
265- Local< Object> c = Object::New (isolate );
266- c-> Set (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " x" ,v8::NewStringType:: kNormal ). ToLocalChecked (), v8::Number::New (isolate, (( double )(*itr2).get (boost::polygon::HORIZONTAL)) / inputscale + xshift) );
267- c-> Set (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " y" ,v8::NewStringType:: kNormal ). ToLocalChecked (), v8::Number::New (isolate, (( double )(*itr2).get (boost::polygon::VERTICAL)) / inputscale + yshift) );
268-
269- child-> Set (isolate-> GetCurrentContext (), z, c) ;
270- z++;
271- }
272- children-> Set (isolate-> GetCurrentContext (), k, child) ;
273- k++;
248+ Napi:: Array child = Napi:: Array::New (env );
249+ int z = 0 ;
250+ for (polygon_traits<polygon>::iterator_type itr2 = (*itrh).begin (); itr2 != (*itrh).end (); ++itr2) {
251+ Napi:: Object c = Napi:: Object::New (env );
252+ c. Set (" x" , (( double )(*itr2).get (boost::polygon::HORIZONTAL)) / inputscale + xshift);
253+ c. Set (" y" , (( double )(*itr2).get (boost::polygon::VERTICAL)) / inputscale + yshift);
254+
255+ child[z] = c ;
256+ z++;
257+ }
258+ children[k] = child;
259+ k++;
274260 }
275261
276- pointlist-> Set (isolate-> GetCurrentContext (), String::NewFromUtf8 (isolate, " children" ,v8::NewStringType:: kNormal ). ToLocalChecked () , children);
262+ pointlist. Set (" children" , children);
277263
278- result_list-> Set (isolate-> GetCurrentContext (), i, pointlist) ;
264+ result_list[i] = pointlist;
279265 }
280266
281- // std::string text = buffer.str();
282-
283- info.GetReturnValue ().Set (result_list);
267+ return result_list;
284268}
0 commit comments