accessors for x and y point

This commit is contained in:
Peter Braden 2012-01-16 17:46:14 -08:00
parent 388dbcec23
commit 908ed1edf5
7 changed files with 102 additions and 22 deletions

View File

@ -1,4 +1,6 @@
#!/bin/bash
node-waf configure && node-waf build && echo '-- Compiled OK --
' && node smoketest
' && node test/smoke.js && echo '-- Tests Done, running smoke --
' && node smoketest.js

View File

@ -1,14 +1,5 @@
var opencv = require('./lib/opencv')
, assert = require('assert')
assert.ok(!!opencv)
assert.ok(!!opencv.Point)
assert.ok(!!new opencv.Point(1, 2))
assert.equal(new opencv.Point(1, 2).x, 1)
assert.equal(new opencv.Point(1, 2).y, 2)
assert.equal(Math.round(new opencv.Point(1.1, 2).x * 100), 110)
assert.equal(Math.round(new opencv.Point(1.2, 2.75).y *100), 275)
console.log(opencv.version)
console.log(new opencv.Image("./examples/mona.jpg").width, "!!")

View File

@ -4,5 +4,9 @@ void
OpenCV::Init(Handle<Object> target) {
HandleScope scope;
target->Set(String::NewSymbol("version"), String::New("1"));
// Version string.
char out [21];
int n = sprintf(out, "%i.%i", CV_MAJOR_VERSION, CV_MINOR_VERSION);
target->Set(String::NewSymbol("version"), String::New(out, n));
}

View File

@ -12,7 +12,6 @@
using namespace v8;
using namespace node;
class OpenCV: public node::ObjectWrap{
public:
static void Init(Handle<Object> target);

View File

@ -15,22 +15,21 @@ Point::Init(Handle<Object> target) {
// Prototype
Local<ObjectTemplate> proto = constructor->PrototypeTemplate();
proto->SetAccessor(String::NewSymbol("x"), GetX);
proto->SetAccessor(String::NewSymbol("y"), GetY);
proto->SetAccessor(String::NewSymbol("x"), GetX, RaiseImmutable);
proto->SetAccessor(String::NewSymbol("y"), GetY, RaiseImmutable);
NODE_SET_PROTOTYPE_METHOD(constructor, "dot", Dot);
/*proto->SetAccessor(String::NewSymbol("source"), GetSource, SetSource);
proto->SetAccessor(String::NewSymbol("complete"), GetComplete);
proto->SetAccessor(String::NewSymbol("width"), GetWidth);
proto->SetAccessor(String::NewSymbol("height"), GetHeight);
proto->SetAccessor(String::NewSymbol("onload"), GetOnload, SetOnload);
proto->SetAccessor(String::NewSymbol("onerror"), GetOnerror, SetOnerror);*/
target->Set(String::NewSymbol("Point"), constructor->GetFunction());
};
Handle<Value>
Point::New(const Arguments &args) {
HandleScope scope;
if (args.This()->InternalFieldCount() == 0)
return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Cannot Instantiate without new")));
double x = 0, y = 0;
if (args[0]->IsNumber()) x = args[0]->NumberValue();
if (args[1]->IsNumber()) y = args[1]->NumberValue();
@ -53,6 +52,21 @@ Point::GetY(Local<String> prop, const AccessorInfo &info) {
return scope.Close(Number::New(pt->point.y));
}
void
Point::RaiseImmutable(Local<String> property, Local<Value> value, const AccessorInfo& info) {
v8::ThrowException(v8::Exception::TypeError(v8::String::New("Point is immutable")));
}
Handle<Value>
Point::Dot(const v8::Arguments& args){
HandleScope scope;
Point *p1 = ObjectWrap::Unwrap<Point>(args.This());
Point *p2 = ObjectWrap::Unwrap<Point>(args[0]->ToObject());
// Since V 2.3 Native Dot no longer supported
return scope.Close(Number::New(p1->point.x * p2->point.x + p1->point.y * p2->point.y));
}
Point::Point(double x, double y): ObjectWrap() {
point = cvPoint2D32f(x, y);

View File

@ -12,4 +12,7 @@ class Point: public node::ObjectWrap {
static Handle<Value> GetX(Local<String> prop, const AccessorInfo &info);
static Handle<Value> GetY(Local<String> prop, const AccessorInfo &info);
static void RaiseImmutable(Local<String> property, Local<Value> value, const AccessorInfo& info);
static Handle<Value> Dot(const v8::Arguments&);
};

67
test/smoke.js Normal file
View File

@ -0,0 +1,67 @@
var vows = require('vows')
, assert = require('assert')
vows.describe('Smoke Tests OpenCV').addBatch({
"Importing": {
topic: require('../lib/opencv')
, "returns obj": function(topic){
assert.ok(!!topic)
}
, '.version' : function(topic){
assert.ok(!!topic.version)
}
, '.Point imports': function(topic){
assert.ok(!!topic.Point)
}
, '.Image imports' : function(topic){
assert.ok(!!topic.Image)
}
}
, "Point" : {
topic : require('../lib/opencv')
, 'constructor' : function(cv){
assert.ok(!!new cv.Point(1, 2))
assert.throws(function () { cv.Point(1, 2)}, TypeError); // cannot call without new
}
, 'accessors' : function(cv){
assert.equal(new cv.Point(1, 2).x, 1)
assert.equal(new cv.Point(1, 2).y, 2)
assert.equal(Math.round(new cv.Point(1.1, 2).x * 100), 110)
assert.equal(Math.round(new cv.Point(1.2, 2.75).y *100), 275)
assert.throws(function () {new cv.Point(1.1, 2).x = 5}, Error); // Points are immutable
assert.throws(function () {new cv.Point(1.1, 2).y = 5}, Error); // Points are immutable
}
, '.dot': function(cv){
var p1 = new cv.Point(3, 6)
, p2 = new cv.Point(5, 7)
assert.ok(p1.dot);
assert.ok(p1.dot(p2));
}
, '.inside' : function(){}
}
, "Image" : {
}
}).run();