partial implementation of complex out args in Java wrappers

This commit is contained in:
Andrey Pavlenko 2011-07-13 14:23:47 +00:00
parent 066039fd15
commit 801079e2ba
7 changed files with 432 additions and 364 deletions

View File

@ -14,8 +14,8 @@ type_dict = {
"env" : { "j_type" : "", "jn_type" : "", "jni_type" : "JNIEnv*"}, "env" : { "j_type" : "", "jn_type" : "", "jni_type" : "JNIEnv*"},
"cls" : { "j_type" : "", "jn_type" : "", "jni_type" : "jclass"}, "cls" : { "j_type" : "", "jn_type" : "", "jni_type" : "jclass"},
"bool" : { "j_type" : "boolean", "jn_type" : "boolean", "jni_type" : "jboolean", "suffix" : "Z" }, "bool" : { "j_type" : "boolean", "jn_type" : "boolean", "jni_type" : "jboolean", "suffix" : "Z" },
"int" : { "j_type" : "int", "jn_type" : "int", "jni_type" : "int", "suffix" : "I" }, "int" : { "j_type" : "int", "jn_type" : "int", "jni_type" : "jint", "suffix" : "I" },
"long" : { "j_type" : "int", "jn_type" : "int", "jni_type" : "int", "suffix" : "I" }, "long" : { "j_type" : "int", "jn_type" : "int", "jni_type" : "jint", "suffix" : "I" },
"float" : { "j_type" : "float", "jn_type" : "float", "jni_type" : "jfloat", "suffix" : "F" }, "float" : { "j_type" : "float", "jn_type" : "float", "jni_type" : "jfloat", "suffix" : "F" },
"double" : { "j_type" : "double", "jn_type" : "double", "jni_type" : "jdouble", "suffix" : "D" }, "double" : { "j_type" : "double", "jn_type" : "double", "jni_type" : "jdouble", "suffix" : "D" },
"size_t" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" }, "size_t" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" },
@ -26,37 +26,37 @@ type_dict = {
"jni_type" : "jlong", #"jni_name" : "*%(n)s", "jni_type" : "jlong", #"jni_name" : "*%(n)s",
"suffix" : "J" }, "suffix" : "J" },
"Point" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y")), "Point" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y")),
"jni_var" : "cv::Point %(n)s((int)%(n)s_x, (int)%(n)s_y)", "jni_var" : "Point %(n)s((int)%(n)s_x, (int)%(n)s_y)",
"suffix" : "DD"}, "suffix" : "DD"},
"Point2f" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y")), "Point2f" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y")),
"jni_var" : "cv::Point2f %(n)s((float)%(n)s_x, (float)%(n)s_y)", "jni_var" : "Point2f %(n)s((float)%(n)s_x, (float)%(n)s_y)",
"suffix" : "DD"}, "suffix" : "DD"},
"Point2d" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y")), "Point2d" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y")),
"jni_var" : "cv::Point2d %(n)s(%(n)s_x, %(n)s_y)", "jni_var" : "Point2d %(n)s(%(n)s_x, %(n)s_y)",
"suffix" : "DD"}, "suffix" : "DD"},
"Point3i" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y"), ("double", ".z")), "Point3i" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y"), ("double", ".z")),
"jni_var" : "cv::Point3i %(n)s((int)%(n)s_x, (int)%(n)s_y, (int)%(n)s_z)", "jni_var" : "Point3i %(n)s((int)%(n)s_x, (int)%(n)s_y, (int)%(n)s_z)",
"suffix" : "DDD"}, "suffix" : "DDD"},
"Point3f" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y"), ("double", ".z")), "Point3f" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y"), ("double", ".z")),
"jni_var" : "cv::Point3f %(n)s((float)%(n)s_x, (float)%(n)s_y, (float)%(n)s_z)", "jni_var" : "Point3f %(n)s((float)%(n)s_x, (float)%(n)s_y, (float)%(n)s_z)",
"suffix" : "DDD"}, "suffix" : "DDD"},
"Point3d" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y"), ("double", ".z")), "Point3d" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y"), ("double", ".z")),
"jni_var" : "cv::Point3d %(n)s(%(n)s_x, %(n)s_y, %(n)s_z)", "jni_var" : "Point3d %(n)s(%(n)s_x, %(n)s_y, %(n)s_z)",
"suffix" : "DDD"}, "suffix" : "DDD"},
"Rect" : { "j_type" : "Rect", "jn_args" : (("int", ".x"), ("int", ".y"), ("int", ".width"), ("int", ".height")), "Rect" : { "j_type" : "Rect", "jn_args" : (("int", ".x"), ("int", ".y"), ("int", ".width"), ("int", ".height")),
"jni_var" : "cv::Rect %(n)s(%(n)s_x, %(n)s_y, %(n)s_width, %(n)s_height)", "jni_var" : "Rect %(n)s(%(n)s_x, %(n)s_y, %(n)s_width, %(n)s_height)",
"suffix" : "IIII"}, "suffix" : "IIII"},
"Size" : { "j_type" : "Size", "jn_args" : (("double", ".width"), ("double", ".height")), "Size" : { "j_type" : "Size", "jn_args" : (("double", ".width"), ("double", ".height")),
"jni_var" : "cv::Size %(n)s((int)%(n)s_width, (int)%(n)s_height)", "jni_var" : "Size %(n)s((int)%(n)s_width, (int)%(n)s_height)",
"suffix" : "DD"}, "suffix" : "DD"},
"Size2f" : { "j_type" : "Size", "jn_args" : (("double", ".width"), ("double", ".height")), "Size2f" : { "j_type" : "Size", "jn_args" : (("double", ".width"), ("double", ".height")),
"jni_var" : "cv::Size2f %(n)s((float)%(n)s_width, (float)%(n)s_height)", "jni_var" : "Size2f %(n)s((float)%(n)s_width, (float)%(n)s_height)",
"suffix" : "DD"}, "suffix" : "DD"},
"RotatedRect": { "j_type" : "RotatedRect", "jn_args" : (("double", ".center.x"), ("double", ".center.y"), ("double", ".size.width"), ("double", ".size.height"), ("double", ".angle")), "RotatedRect": { "j_type" : "RotatedRect", "jn_args" : (("double", ".center.x"), ("double", ".center.y"), ("double", ".size.width"), ("double", ".size.height"), ("double", ".angle")),
"jni_var" : "cv::RotatedRect %(n)s(cv::Point2f(%(n)s_center_x, %(n)s_center_y), cv::Size2f(%(n)s_size_width, %(n)s_size_height), %(n)s_angle)", "jni_var" : "RotatedRect %(n)s(cv::Point2f(%(n)s_center_x, %(n)s_center_y), cv::Size2f(%(n)s_size_width, %(n)s_size_height), %(n)s_angle)",
"suffix" : "DDDDD"}, "suffix" : "DDDDD"},
"Scalar" : { "j_type" : "Scalar", "jn_args" : (("double", ".v0"), ("double", ".v1"), ("double", ".v2"), ("double", ".v3")), "Scalar" : { "j_type" : "Scalar", "jn_args" : (("double", ".v0"), ("double", ".v1"), ("double", ".v2"), ("double", ".v3")),
"jni_var" : "cv::Scalar %(n)s(%(n)s_v0, %(n)s_v1, %(n)s_v2, %(n)s_v3)", "jni_var" : "Scalar %(n)s(%(n)s_v0, %(n)s_v1, %(n)s_v2, %(n)s_v3)",
"suffix" : "DDDD"}, "suffix" : "DDDD"},
"Range" : { "j_type" : "Range", "jn_args" : (("int", ".start"), ("int", ".end")), "Range" : { "j_type" : "Range", "jn_args" : (("int", ".start"), ("int", ".end")),
"jni_var" : "cv::Range %(n)s(%(n)s_start, %(n)s_end)", "jni_var" : "cv::Range %(n)s(%(n)s_start, %(n)s_end)",
@ -189,8 +189,9 @@ class JavaWrapperGenerator(object):
print "Duplicated class: " + classinfo.name print "Duplicated class: " + classinfo.name
sys.exit(-1) sys.exit(-1)
type_dict[classinfo.name] = \ type_dict[classinfo.name] = \
{ "j_type" : classinfo.name, "jn_args" : (("__int64", ".nativeObj"),), { "j_type" : classinfo.name,
"jni_name" : "(*("+classinfo.name+"*)%(n)s_nativeObj)", "jn_type" : "long", "jn_args" : (("__int64", ".nativeObj"),),
"jni_name" : "(*("+classinfo.name+"*)%(n)s_nativeObj)", "jni_type" : "jlong",
"suffix" : "J" } "suffix" : "J" }
@ -404,8 +405,7 @@ class JavaWrapperGenerator(object):
# java comment # java comment
self.java_code.write( "\n%s// C++: %s\n" % (indent, c_decl) ) self.java_code.write( "\n%s// C++: %s\n" % (indent, c_decl) )
# check if we 'know' all the types # check if we 'know' all the types
type_info = type_dict.get(fi.ctype) if fi.ctype not in type_dict: # unsupported ret type
if not (type_info and type_info.get("jn_type")): # unsupported ret type
msg = "// Return type '%s' is not supported, skipping the function\n\n" % fi.ctype msg = "// Return type '%s' is not supported, skipping the function\n\n" % fi.ctype
self.skipped_func_list.append(c_decl + "\n" + msg) self.skipped_func_list.append(c_decl + "\n" + msg)
self.java_code.write( indent + msg ) self.java_code.write( indent + msg )
@ -414,18 +414,18 @@ class JavaWrapperGenerator(object):
return return
for a in fi.args: for a in fi.args:
if a.ctype not in type_dict: if a.ctype not in type_dict:
msg = "// Unknown type '%s', skipping the function\n\n" % a.ctype msg = "// Unknown type '%s' (%s), skipping the function\n\n" % (a.ctype, a.out or "I")
self.skipped_func_list.append(c_decl + "\n" + msg) self.skipped_func_list.append(c_decl + "\n" + msg)
self.java_code.write( indent + msg ) self.java_code.write( indent + msg )
#self.cpp_code.write( msg ) #self.cpp_code.write( msg )
print "SKIP:", c_decl, "\n\tdue to ARG type", a.ctype, a.out print "SKIP:", c_decl, "\n\tdue to ARG type", a.ctype, "/" + (a.out or "I")
return return
if a.ctype != "Mat" and "jn_args" in type_dict[a.ctype] and a.out: # complex out args not yet supported if a.ctype != "Mat" and "jn_args" in type_dict[a.ctype] and a.out: # complex out args not yet supported
msg = "// Unsupported type '%s&', skipping the function\n\n" % a.ctype msg = "// Unsupported OUT type '%s' (%s), skipping the function\n\n" % (a.ctype, a.out or "I")
self.skipped_func_list.append(c_decl + "\n" + msg) self.skipped_func_list.append(c_decl + "\n" + msg)
self.java_code.write( indent + msg ) self.java_code.write( indent + msg )
#self.cpp_code.write( msg ) #self.cpp_code.write( msg )
print "SKIP:", c_decl, "\n\tdue to OUT ARG of type", a.ctype, a.out print "SKIP:", c_decl, "\n\tdue to OUT ARG of type", a.ctype, "/" + (a.out or "I")
return return
self.ported_func_counter += 1 self.ported_func_counter += 1
@ -470,7 +470,7 @@ class JavaWrapperGenerator(object):
jn_code.write( Template(\ jn_code.write( Template(\
"${indent}private static native $jn_type $jn_name($jn_args);\n").substitute(\ "${indent}private static native $jn_type $jn_name($jn_args);\n").substitute(\
indent = indent, \ indent = indent, \
jn_type = type_dict[fi.ctype]["jn_type"], \ jn_type = type_dict[fi.ctype].get("jn_type", "double[]"), \
jn_name = fi.jn_name, \ jn_name = fi.jn_name, \
jn_args = ", ".join(["%s %s" % (type_dict[a.ctype]["jn_type"], a.name.replace(".","_")) for a in jn_args]) jn_args = ", ".join(["%s %s" % (type_dict[a.ctype]["jn_type"], a.name.replace(".","_")) for a in jn_args])
) ); ) );
@ -495,8 +495,10 @@ class JavaWrapperGenerator(object):
elif fi.ctype == "": # c-tor elif fi.ctype == "": # c-tor
impl_code = "nativeObj = $jn_name($jn_args_call);" impl_code = "nativeObj = $jn_name($jn_args_call);"
elif fi.ctype in self.classes: # wrapped class elif fi.ctype in self.classes: # wrapped class
impl_code = " return new %s( $jn_name($jn_args_call) ); " % \ impl_code = "return new %s( $jn_name($jn_args_call) );" % \
self.classes[fi.ctype].jname self.classes[fi.ctype].jname
elif "jn_type" not in type_dict[fi.ctype]:
impl_code = "return new "+type_dict[fi.ctype]["j_type"]+"( $jn_name($jn_args_call) );"
static = "static" static = "static"
if fi.classname: if fi.classname:
@ -518,25 +520,35 @@ class JavaWrapperGenerator(object):
) ) ) )
# cpp part: # cpp part:
# jni_func(..) { return cv_func(..); } # jni_func(..) { _retval_ = cv_func(..); return _retval_; }
ret = "return " ret = "return _retval_;"
ext = ""
default = "return 0;" default = "return 0;"
if fi.ctype == "void": if fi.ctype == "void":
ret = "" ret = "return;"
default = "" default = "return;"
elif not fi.ctype: # c-tor
ret = "return (jlong) _retval_;"
elif fi.ctype == "string": elif fi.ctype == "string":
ret = "return env->NewStringUTF" ret = "return env->NewStringUTF(_retval_.c_str());"
ext = ".c_str()"
default = 'return env->NewStringUTF("");' default = 'return env->NewStringUTF("");'
elif fi.ctype in self.classes: # wrapped class: elif fi.ctype in self.classes: # wrapped class:
ret = "return (jlong) new " + self.classes[fi.ctype].jname ret = "return (jlong) new %s(_retval_);" % fi.ctype
elif "jni_type" not in type_dict[fi.ctype]: # jdoubleArray
ret = "double _tmp_[6]; " + \
"/* "+fi.ctype+"_to_double6(_retval_, _tmp_); */" + \
"jdoubleArray _da_ = env->NewDoubleArray(6); " + \
"env->SetDoubleArrayRegion(_da_, 0, 6, _tmp_); " + \
"return _da_;"
cvname = "cv::" + fi.name cvname = "cv::" + fi.name
j2cvargs = [] j2cvargs = []
retval = fi.ctype + " _retval_ = "
if fi.ctype == "void":
retval = ""
if fi.classname: if fi.classname:
if not fi.ctype: # c-tor if not fi.ctype: # c-tor
cvname = "(jlong) new " + fi.classname retval = fi.classname + "* _retval_ = "
cvname = "new " + fi.classname
elif fi.static: elif fi.static:
cvname = "%s::%s" % (fi.classname, fi.name) cvname = "%s::%s" % (fi.classname, fi.name)
else: else:
@ -551,7 +563,7 @@ class JavaWrapperGenerator(object):
if "jni_var" in type_dict[a.ctype]: # complex type if "jni_var" in type_dict[a.ctype]: # complex type
j2cvargs.append(type_dict[a.ctype]["jni_var"] % {"n" : a.name} + ";") j2cvargs.append(type_dict[a.ctype]["jni_var"] % {"n" : a.name} + ";")
rtype = type_dict[fi.ctype]["jni_type"] rtype = type_dict[fi.ctype].get("jni_type", "jdoubleArray")
self.cpp_code.write ( Template( \ self.cpp_code.write ( Template( \
""" """
@ -563,7 +575,8 @@ JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_$fname
LOGD("$module::$fname()"); LOGD("$module::$fname()");
#endif // DEBUG #endif // DEBUG
$j2cv $j2cv
$ret( $cvname( $cvargs )$ext ); $retval$cvname( $cvargs );
$ret
} catch(cv::Exception e) { } catch(cv::Exception e) {
#ifdef DEBUG #ifdef DEBUG
LOGD("$module::$fname() catched cv::Exception: %s", e.what()); LOGD("$module::$fname() catched cv::Exception: %s", e.what());
@ -592,8 +605,8 @@ JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_$fname
ret = ret, \ ret = ret, \
cvname = cvname, \ cvname = cvname, \
cvargs = ", ".join([a for a in cvargs]), \ cvargs = ", ".join([a for a in cvargs]), \
ext = ext, \ default = default, \
default = default retval = retval, \
) ) ) )
# processing args with default values # processing args with default values

View File

@ -3,53 +3,61 @@ package org.opencv;
//javadoc:Point_ //javadoc:Point_
public class Point { public class Point {
public double x, y; public double x, y;
public Point(double x, double y) { public Point(double x, double y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
public Point() { public Point() {
this(0, 0); this(0, 0);
} }
public Point clone() { public Point(double[] vals) {
return new Point(x, y); this();
} if(vals!=null) {
x = vals.length>0 ? vals[0] : 0;
y = vals.length>1 ? vals[1] : 0;
}
}
public double dot(Point p) { public Point clone() {
return x * p.x + y * p.y; return new Point(x, y);
} }
@Override public double dot(Point p) {
public int hashCode() { return x * p.x + y * p.y;
final int prime = 31; }
int result = 1;
long temp;
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override @Override
public boolean equals(Object obj) { public int hashCode() {
if (this == obj) return true; final int prime = 31;
if ( ! (obj instanceof Point) ) return false; int result = 1;
Point it = (Point) obj; long temp;
return x == it.x && y == it.y; temp = Double.doubleToLongBits(x);
} result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
public boolean inside(Rect r) { @Override
return r.contains(this); public boolean equals(Object obj) {
} if (this == obj) return true;
if ( ! (obj instanceof Point) ) return false;
Point it = (Point) obj;
return x == it.x && y == it.y;
}
public boolean inside(Rect r) {
return r.contains(this);
}
@Override @Override
public String toString() { public String toString() {
if (this == null) return "null"; if (this == null) return "null";
return "{" + x + ", " + y + "}"; return "{" + x + ", " + y + "}";
} }
} }

View File

@ -3,55 +3,64 @@ package org.opencv;
//javadoc:Point3_ //javadoc:Point3_
public class Point3 { public class Point3 {
public double x, y, z; public double x, y, z;
public Point3(double x, double y, double z) { public Point3(double x, double y, double z) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.z = z; this.z = z;
} }
public Point3() { public Point3() {
this(0, 0, 0); this(0, 0, 0);
} }
public Point3(Point p) { public Point3(Point p) {
x = p.x; x = p.x;
y = p.y; y = p.y;
z = 0; z = 0;
} }
public Point3 clone() { public Point3(double[] vals) {
return new Point3(x, y, z); this();
} if(vals!=null) {
x = vals.length>0 ? vals[0] : 0;
y = vals.length>1 ? vals[1] : 0;
z = vals.length>2 ? vals[2] : 0;
}
}
public double dot(Point3 p) { public Point3 clone() {
return x * p.x + y * p.y + z * p.z; return new Point3(x, y, z);
} }
public Point3 cross(Point3 p) { public double dot(Point3 p) {
return new Point3(y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x); return x * p.x + y * p.y + z * p.z;
} }
@Override public Point3 cross(Point3 p) {
public int hashCode() { return new Point3(y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x);
final int prime = 31; }
int result = 1;
long temp;
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(z);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override @Override
public boolean equals(Object obj) { public int hashCode() {
if (this == obj) return true; final int prime = 31;
if (!(obj instanceof Point3)) return false; int result = 1;
Point3 it = (Point3) obj; long temp;
return x == it.x && y == it.y && z == it.z; temp = Double.doubleToLongBits(x);
} result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(z);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Point3)) return false;
Point3 it = (Point3) obj;
return x == it.x && y == it.y && z == it.z;
}
} }

View File

@ -3,81 +3,91 @@ package org.opencv;
//javadoc:Rect_ //javadoc:Rect_
public class Rect { public class Rect {
public int x, y, width, height; public int x, y, width, height;
public Rect(int x, int y, int width, int height) { public Rect(int x, int y, int width, int height) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.width = width; this.width = width;
this.height = height; this.height = height;
} }
public Rect() { public Rect() {
this(0, 0, 0, 0); this(0, 0, 0, 0);
} }
public Rect(Point p1, Point p2) { public Rect(Point p1, Point p2) {
x = (int) (p1.x < p2.x ? p1.x : p2.x); x = (int) (p1.x < p2.x ? p1.x : p2.x);
y = (int) (p1.y < p2.y ? p1.y : p2.y); y = (int) (p1.y < p2.y ? p1.y : p2.y);
width = (int) (p1.x > p2.x ? p1.x : p2.x) - x; width = (int) (p1.x > p2.x ? p1.x : p2.x) - x;
height = (int) (p1.y > p2.y ? p1.y : p2.y) - y; height = (int) (p1.y > p2.y ? p1.y : p2.y) - y;
} }
public Rect(Point p, Size s) { public Rect(Point p, Size s) {
this((int)p.x, (int)p.y, (int)s.width, (int)s.height); this((int)p.x, (int)p.y, (int)s.width, (int)s.height);
} }
public Rect clone() { public Rect(double[] vals) {
return new Rect(x, y, width, height); this();
} if(vals!=null) {
x = vals.length>0 ? (int)vals[0] : 0;
y = vals.length>1 ? (int)vals[1] : 0;
width = vals.length>2 ? (int)vals[2] : 0;
height = vals.length>3 ? (int)vals[3] : 0;
}
}
public Point tl() { public Rect clone() {
return new Point(x, y); return new Rect(x, y, width, height);
} }
public Point br() { public Point tl() {
return new Point(x + width, y + height); return new Point(x, y);
} }
public Size size() { public Point br() {
return new Size(width, height); return new Point(x + width, y + height);
} }
public double area() { public Size size() {
return width * height; return new Size(width, height);
} }
public boolean contains(Point p) { public double area() {
return x <= p.x && p.x < x + width && y <= p.y && p.y < y + height; return width * height;
} }
@Override public boolean contains(Point p) {
public int hashCode() { return x <= p.x && p.x < x + width && y <= p.y && p.y < y + height;
final int prime = 31; }
int result = 1;
long temp;
temp = Double.doubleToLongBits(height);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(width);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override @Override
public boolean equals(Object obj) { public int hashCode() {
if (this == obj) return true; final int prime = 31;
if (!(obj instanceof Rect)) return false; int result = 1;
Rect it = (Rect) obj; long temp;
return x == it.x && y == it.y && width == it.width && height == it.height; temp = Double.doubleToLongBits(height);
} result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(width);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override @Override
public String toString() { public boolean equals(Object obj) {
if (this == null) return "null"; if (this == obj) return true;
return "{" + x + ", " + y + ", " + width + "x" + height+"}"; if (!(obj instanceof Rect)) return false;
} Rect it = (Rect) obj;
return x == it.x && y == it.y && width == it.width && height == it.height;
}
@Override
public String toString() {
if (this == null) return "null";
return "{" + x + ", " + y + ", " + width + "x" + height+"}";
}
} }

View File

@ -3,85 +3,96 @@ package org.opencv;
//javadoc:RotatedRect_ //javadoc:RotatedRect_
public class RotatedRect { public class RotatedRect {
public Point center; public Point center;
public Size size; public Size size;
public double angle; public double angle;
public RotatedRect() { public RotatedRect() {
this.angle=0; this.angle=0;
} }
public RotatedRect(Point c, Size s, double a) { public RotatedRect(Point c, Size s, double a) {
this.center = c.clone(); this.center = c.clone();
this.size = s.clone(); this.size = s.clone();
this.angle = a; this.angle = a;
} }
public void points(Point pt[]) public RotatedRect(double[] vals) {
{ this();
double _angle = angle*Math.PI/180.0; if(vals!=null) {
double b = (double)Math.cos(_angle)*0.5f; center.x = vals.length>0 ? (int)vals[0] : 0;
double a = (double)Math.sin(_angle)*0.5f; center.x = vals.length>1 ? (int)vals[1] : 0;
size.width = vals.length>2 ? (int)vals[2] : 0;
size.height = vals.length>3 ? (int)vals[3] : 0;
angle = vals.length>4 ? (int)vals[4] : 0;
}
}
pt[0] = new Point( public void points(Point pt[])
center.x - a*size.height - b*size.width, {
center.y + b*size.height - a*size.width); double _angle = angle*Math.PI/180.0;
double b = (double)Math.cos(_angle)*0.5f;
double a = (double)Math.sin(_angle)*0.5f;
pt[1] = new Point( pt[0] = new Point(
center.x + a*size.height - b*size.width, center.x - a*size.height - b*size.width,
center.y - b*size.height - a*size.width); center.y + b*size.height - a*size.width);
pt[2] = new Point( pt[1] = new Point(
2*center.x - pt[0].x, center.x + a*size.height - b*size.width,
2*center.y - pt[0].y); center.y - b*size.height - a*size.width);
pt[3] = new Point( pt[2] = new Point(
2*center.x - pt[1].x, 2*center.x - pt[0].x,
2*center.y - pt[1].y); 2*center.y - pt[0].y);
}
public Rect boundingRect() pt[3] = new Point(
{ 2*center.x - pt[1].x,
Point pt[] = new Point[4]; 2*center.y - pt[1].y);
points(pt); }
Rect r=new Rect((int)Math.floor(Math.min(Math.min(Math.min(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
(int)Math.floor(Math.min(Math.min(Math.min(pt[0].y, pt[1].y), pt[2].y), pt[3].y)), public Rect boundingRect()
(int)Math.ceil(Math.max(Math.max(Math.max(pt[0].x, pt[1].x), pt[2].x), pt[3].x)), {
(int)Math.ceil(Math.max(Math.max(Math.max(pt[0].y, pt[1].y), pt[2].y), pt[3].y))); Point pt[] = new Point[4];
r.width -= r.x - 1; points(pt);
r.height -= r.y - 1; Rect r=new Rect((int)Math.floor(Math.min(Math.min(Math.min(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
return r; (int)Math.floor(Math.min(Math.min(Math.min(pt[0].y, pt[1].y), pt[2].y), pt[3].y)),
} (int)Math.ceil(Math.max(Math.max(Math.max(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
(int)Math.ceil(Math.max(Math.max(Math.max(pt[0].y, pt[1].y), pt[2].y), pt[3].y)));
r.width -= r.x - 1;
r.height -= r.y - 1;
return r;
}
public RotatedRect clone() { public RotatedRect clone() {
return new RotatedRect(center, size, angle); return new RotatedRect(center, size, angle);
} }
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
long temp; long temp;
temp = Double.doubleToLongBits(center.x); temp = Double.doubleToLongBits(center.x);
result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(center.y); temp = Double.doubleToLongBits(center.y);
result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(size.width); temp = Double.doubleToLongBits(size.width);
result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(size.height); temp = Double.doubleToLongBits(size.height);
result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(angle); temp = Double.doubleToLongBits(angle);
result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + (int) (temp ^ (temp >>> 32));
return result; return result;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) return true; if (this == obj) return true;
if (!(obj instanceof RotatedRect)) return false; if (!(obj instanceof RotatedRect)) return false;
RotatedRect it = (RotatedRect) obj; RotatedRect it = (RotatedRect) obj;
return center.equals(it.center) && size.equals(it.size) && angle == it.angle; return center.equals(it.center) && size.equals(it.size) && angle == it.angle;
} }
} }

View File

@ -3,72 +3,81 @@ package org.opencv;
//javadoc:Scalar_ //javadoc:Scalar_
public class Scalar { public class Scalar {
public double v0, v1, v2, v3; public double v0, v1, v2, v3;
public Scalar(double v0, double v1, double v2, double v3) { public Scalar(double v0, double v1, double v2, double v3) {
this.v0 = v0; this.v0 = v0;
this.v1 = v1; this.v1 = v1;
this.v2 = v2; this.v2 = v2;
this.v3 = v3; this.v3 = v3;
} }
public Scalar(double v0, double v1, double v2) { public Scalar(double v0, double v1, double v2) {
this(v0, v1, v2, 0); this(v0, v1, v2, 0);
} }
public Scalar(double v0, double v1) { public Scalar(double v0, double v1) {
this(v0, v1, 0, 0); this(v0, v1, 0, 0);
} }
public Scalar(double v0) { public Scalar(double v0) {
this(v0, 0, 0, 0); this(v0, 0, 0, 0);
} }
public static Scalar all(double v) { public Scalar(double[] vals) {
return new Scalar(v, v, v, v); if(vals!=null) {
} v0 = vals.length>0 ? (int)vals[0] : 0;
v1 = vals.length>1 ? (int)vals[1] : 0;
v2 = vals.length>2 ? (int)vals[2] : 0;
v3 = vals.length>3 ? (int)vals[3] : 0;
}
}
public Scalar clone() { public static Scalar all(double v) {
return new Scalar(v0, v1, v2, v3); return new Scalar(v, v, v, v);
} }
public Scalar mul(Scalar it, double scale) { public Scalar clone() {
return new Scalar( v0 * it.v0 * scale, v1 * it.v1 * scale, return new Scalar(v0, v1, v2, v3);
v2 * it.v2 * scale, v3 * it.v3 * scale ); }
}
public Scalar mul(Scalar it) { public Scalar mul(Scalar it, double scale) {
return mul(it, 1); return new Scalar( v0 * it.v0 * scale, v1 * it.v1 * scale,
} v2 * it.v2 * scale, v3 * it.v3 * scale );
public Scalar conj() { }
return new Scalar(v0, -v1, -v2, -v3);
}
public boolean isReal() { public Scalar mul(Scalar it) {
return v1 == 0 && v2 == 0 && v3 == 0; return mul(it, 1);
} }
public Scalar conj() {
return new Scalar(v0, -v1, -v2, -v3);
}
@Override public boolean isReal() {
public int hashCode() { return v1 == 0 && v2 == 0 && v3 == 0;
final int prime = 31; }
int result = 1;
long temp;
temp = Double.doubleToLongBits(v0);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(v1);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(v2);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(v3);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override @Override
public boolean equals(Object obj) { public int hashCode() {
if (this == obj) return true; final int prime = 31;
if (!(obj instanceof Scalar)) return false; int result = 1;
Scalar it = (Scalar) obj; long temp;
return v0 == it.v0 && v1 == it.v1 && v2 == it.v2 && v3 == it.v3; temp = Double.doubleToLongBits(v0);
} result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(v1);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(v2);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(v3);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Scalar)) return false;
Scalar it = (Scalar) obj;
return v0 == it.v0 && v1 == it.v1 && v2 == it.v2 && v3 == it.v3;
}
} }

View File

@ -3,48 +3,56 @@ package org.opencv;
//javadoc:Size_ //javadoc:Size_
public class Size { public class Size {
public double width, height; public double width, height;
public Size(double width, double height) { public Size(double width, double height) {
this.width = width; this.width = width;
this.height = height; this.height = height;
} }
public Size() { public Size() {
this(0, 0); this(0, 0);
} }
public Size(Point p) { public Size(Point p) {
width = (double) p.x; width = (double) p.x;
height = (double) p.y; height = (double) p.y;
} }
public double area() { public Size(double[] vals) {
return width * height; this();
} if(vals!=null) {
width = vals.length>0 ? vals[0] : 0;
height = vals.length>1 ? vals[1] : 0;
}
}
public Size clone() { public double area() {
return new Size(width, height); return width * height;
} }
@Override public Size clone() {
public int hashCode() { return new Size(width, height);
final int prime = 31; }
int result = 1;
long temp;
temp = Double.doubleToLongBits(height);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(width);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override @Override
public boolean equals(Object obj) { public int hashCode() {
if (this == obj) return true; final int prime = 31;
if (!(obj instanceof Size)) return false; int result = 1;
Size it = (Size) obj; long temp;
return width == it.width && height == it.height; temp = Double.doubleToLongBits(height);
} result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(width);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Size)) return false;
Size it = (Size) obj;
return width == it.width && height == it.height;
}
} }