Merge pull request #17555 from komakai:objc-fix-sift

pull/17586/head
Alexander Alekhin 4 years ago
commit 6fdddd53a1
  1. 2
      modules/core/misc/objc/common/CVObjcUtil.h
  2. 7
      modules/core/misc/objc/common/Mat.h
  3. 20
      modules/core/misc/objc/common/Mat.mm
  4. 9
      modules/imgproc/misc/objc/test/ImgprocTest.swift
  5. 56
      modules/objc/generator/gen_objc.py
  6. 2
      platforms/ios/build_framework.py

@ -15,8 +15,6 @@ typedef union { float f; int32_t i; } V32;
#ifdef __cplusplus
#import <vector>
#define MAKE_PTR(t) (*((cv::Ptr<t>*)self.nativePtr))
template <typename CV, typename OBJC> std::vector<CV> objc2cv(NSArray<OBJC*>* _Nonnull array, CV& (* _Nonnull converter)(OBJC* _Nonnull)) {
std::vector<CV> ret;
for (OBJC* obj in array) {

@ -26,17 +26,16 @@ NS_ASSUME_NONNULL_BEGIN
@interface Mat : NSObject
#ifdef __cplusplus
@property(readonly) cv::Mat* nativePtr;
@property(readonly) cv::Ptr<cv::Mat> nativePtr;
@property(readonly) cv::Mat& nativeRef;
#endif
#pragma mark - Constructors
- (instancetype)init;
- (void)dealloc;
#ifdef __cplusplus
- (instancetype)initWithNativeMat:(cv::Mat*)nativeMat;
+ (instancetype)fromNativePtr:(cv::Mat*)nativePtr;
- (instancetype)initWithNativeMat:(cv::Ptr<cv::Mat>)nativeMat;
+ (instancetype)fromNativePtr:(cv::Ptr<cv::Mat>)nativePtr;
+ (instancetype)fromNative:(cv::Mat&)nativeRef;
#endif
- (instancetype)initWithRows:(int)rows cols:(int)cols type:(int)type;

@ -39,39 +39,31 @@ static bool updateIdx(cv::Mat* mat, std::vector<int>& indices, int inc) {
}
- (cv::Mat&)nativeRef {
return *(cv::Mat*)_nativePtr;
return *_nativePtr;
}
- (instancetype)init {
self = [super init];
if (self) {
_nativePtr = new cv::Mat();
_nativePtr = cv::Ptr<cv::Mat>(new cv::Mat());
}
return self;
}
- (void)dealloc {
if (_nativePtr != NULL) {
_nativePtr->release();
delete _nativePtr;
}
_nsdata = NULL;
}
- (instancetype)initWithNativeMat:(cv::Mat*)nativePtr {
- (instancetype)initWithNativeMat:(cv::Ptr<cv::Mat>)nativePtr {
self = [super init];
if (self) {
_nativePtr = new cv::Mat(*nativePtr);
_nativePtr = nativePtr;
}
return self;
}
+ (instancetype)fromNativePtr:(cv::Mat*)nativePtr {
+ (instancetype)fromNativePtr:(cv::Ptr<cv::Mat>)nativePtr {
return [[Mat alloc] initWithNativeMat:nativePtr];
}
+ (instancetype)fromNative:(cv::Mat&)nativeRef {
return [[Mat alloc] initWithNativeMat:&nativeRef];
return [[Mat alloc] initWithNativeMat:cv::Ptr<cv::Mat>(new cv::Mat(nativeRef))];
}
- (instancetype)initWithRows:(int)rows cols:(int)cols type:(int)type {

@ -671,9 +671,12 @@ class ImgprocTest: OpenCVTestCase {
let points = [Point2f(x: 0, y: 0), Point2f(x: -1, y: 1), Point2f(x: 1, y: 1), Point2f(x: 1, y: -1), Point2f(x: -1, y: -1)]
let rrect = Imgproc.fitEllipse(points: points)
assertPoint2fEquals(Point2f(x: 0, y: 0), rrect.center, OpenCVTestCase.FEPS)
XCTAssertEqual(Float(2.828), rrect.size.width, accuracy: OpenCVTestCase.FEPS)
XCTAssertEqual(Float(2.828), rrect.size.height, accuracy: OpenCVTestCase.FEPS)
let FIT_ELLIPSE_CENTER_EPS:Float = 0.01
let FIT_ELLIPSE_SIZE_EPS:Float = 0.4
assertPoint2fEquals(Point2f(x: 0, y: 0), rrect.center, FIT_ELLIPSE_CENTER_EPS)
XCTAssertEqual(Float(2.828), rrect.size.width, accuracy: FIT_ELLIPSE_SIZE_EPS)
XCTAssertEqual(Float(2.828), rrect.size.height, accuracy: FIT_ELLIPSE_SIZE_EPS)
}
func testFitLine() throws {

@ -231,8 +231,12 @@ class ClassInfo(GeneralInfo):
self.objc_name = m[1:]
self.base = ''
self.is_base_class = True
self.native_ptr_name = "nativePtr"
if decl[1]:
self.base = re.sub(r"^.*:", "", decl[1].split(",")[0]).strip().replace(self.objc_name, "")
if self.base:
self.is_base_class = False
self.native_ptr_name = "nativePtr" + self.objc_name
def __repr__(self):
return Template("CLASS $namespace::$classpath.$name : $base").substitute(**self.__dict__)
@ -296,7 +300,6 @@ class ClassInfo(GeneralInfo):
if self.base:
self.objc_header_template = T_OBJC_CLASS_HEADER
self.objc_body_template = T_OBJC_CLASS_BODY
self.is_base_class = False
else:
self.base = "NSObject"
if self.name != Module:
@ -326,19 +329,18 @@ class ClassInfo(GeneralInfo):
nativePointerHandling = Template(
"""
#ifdef __cplusplus
@property(readonly)$cName* nativePtr;
@property(readonly)cv::Ptr<$cName> $native_ptr_name;
#endif
- (void)dealloc;
#ifdef __cplusplus
- (instancetype)initWithNativePtr:($cName*)nativePtr;
+ (instancetype)fromNative:($cName*)nativePtr;
- (instancetype)initWithNativePtr:(cv::Ptr<$cName>)nativePtr;
+ (instancetype)fromNative:(cv::Ptr<$cName>)nativePtr;
#endif
"""
).substitute(
cName = self.fullName(isCPP=True)
) if self.is_base_class else "",
cName = self.fullName(isCPP=True),
native_ptr_name = self.native_ptr_name
),
manualMethodDeclations = "",
methodDeclarations = self.method_declarations.getvalue(),
name = self.name,
@ -353,28 +355,24 @@ class ClassInfo(GeneralInfo):
module = M,
nativePointerHandling=Template(
"""
- (void)dealloc {
if (_nativePtr != NULL) {
delete _nativePtr;
}
}
- (instancetype)initWithNativePtr:($cName*)nativePtr {
self = [super init];
- (instancetype)initWithNativePtr:(cv::Ptr<$cName>)nativePtr {
self = [super $init_call];
if (self) {
_nativePtr = nativePtr;
_$native_ptr_name = nativePtr;
}
return self;
}
+ (instancetype)fromNative:($cName*)nativePtr {
+ (instancetype)fromNative:(cv::Ptr<$cName>)nativePtr {
return [[$objcName alloc] initWithNativePtr:nativePtr];
}
"""
).substitute(
cName=self.fullName(isCPP=True),
objcName=self.objc_name
) if self.is_base_class else "",
cName = self.fullName(isCPP=True),
objcName = self.objc_name,
native_ptr_name = self.native_ptr_name,
init_call = "init" if self.is_base_class else "initWithNativePtr:nativePtr"
),
manualMethodDeclations = "",
methodImplementations = self.method_implementations.getvalue(),
name = self.name,
@ -576,7 +574,7 @@ class ObjectiveCWrapperGenerator(object):
type_dict.setdefault(name, {}).update(
{ "objc_type" : classinfo.objc_name + "*",
"from_cpp" : "[" + classinfo.objc_name + " fromNative:%(n)s]",
"to_cpp" : "*(" + classinfo.namespace.replace(".", "::") + "::" + classinfo.objc_name + "*)(%(n)s.nativePtr)" }
"to_cpp" : "*(%(n)s." + classinfo.native_ptr_name + ")" }
)
# missing_consts { Module : { public : [[name, val],...], private : [[]...] } }
@ -596,7 +594,7 @@ class ObjectiveCWrapperGenerator(object):
type_dict.setdefault("Ptr_"+name, {}).update(
{ "objc_type" : classinfo.objc_name + "*",
"c_type" : name,
"to_cpp": "%(n)s.nativePtr",
"to_cpp": "%(n)s." + classinfo.native_ptr_name,
"from_cpp_ptr": "[" + name + " fromNativePtr:%(n)s]"}
)
logging.info('ok: class %s, name: %s, base: %s', classinfo, name, classinfo.base)
@ -934,19 +932,19 @@ class ObjectiveCWrapperGenerator(object):
elif ret_type.startswith("Ptr_"):
cpp_type = type_dict[ret_type]["c_type"]
namespace_prefix = self.get_namespace_prefix(cpp_type)
ret_val = namespace_prefix + cpp_type + "* retVal = "
ret_val = "cv::Ptr<" + namespace_prefix + cpp_type + "> retVal = "
ret = "return [" + type_dict[ret_type]["objc_type"][:-1] + " fromNative:retVal];"
elif ret_type == "void":
ret_val = ""
ret = ""
elif ret_type == "": # c-tor
constructor = True
ret_val = "return [self initWithNativePtr:new "
tail = "]"
ret_val = "return [self initWithNativePtr:cv::Ptr<" + fi.fullClass(isCPP=True) + ">(new "
tail = ")]"
ret = ""
elif self.isWrapped(ret_type): # wrapped class
namespace_prefix = self.get_namespace_prefix(ret_type)
ret_val = namespace_prefix + ret_type + "* retVal = new " + namespace_prefix + ret_type + "("
ret_val = "cv::Ptr<" + namespace_prefix + ret_type + "> retVal = new " + namespace_prefix + ret_type + "("
tail = ")"
ret_type_dict = type_dict[ret_type]
from_cpp = ret_type_dict["from_cpp_ptr"] if ret_type_dict.has_key("from_cpp_ptr") else ret_type_dict["from_cpp"]
@ -993,7 +991,7 @@ class ObjectiveCWrapperGenerator(object):
prologue = "\n " + "\n ".join(prologue) if prologue else "",
epilogue = "\n " + "\n ".join(epilogue) if epilogue else "",
static = "+" if static else "-",
obj_deref = ("MAKE_PTR(" + fi.fullClass(isCPP=True) + ")->" if not ci.is_base_class else "_nativePtr->") if not static and not constructor else "",
obj_deref = ("self." + ci.native_ptr_name + "->") if not static and not constructor else "",
cv_name = fi.cv_name if static else fi.fullClass(isCPP=True) if constructor else fi.name,
cv_args = ", ".join(cv_args),
tail = tail
@ -1068,7 +1066,7 @@ typedef NS_ENUM(int, {2}) {{
objc_type = type_data.get("objc_type", pi.ctype)
ci.addImports(pi.ctype, False)
ci.method_declarations.write("@property " + ("(readonly) " if not pi.rw else "") + objc_type + " " + pi.name + ";\n")
ptr_ref = "MAKE_PTR(" + ci.fullName(isCPP=True) + ")->" if not ci.is_base_class else "_nativePtr->"
ptr_ref = "self." + ci.native_ptr_name + "->" if not ci.is_base_class else "self.nativePtr->"
if type_data.has_key("v_type"):
vector_type = type_data["v_type"]
full_cpp_type = (self.get_namespace_prefix(vector_type) if (vector_type.find("::") == -1) else "") + vector_type

@ -389,7 +389,7 @@ if __name__ == "__main__":
parser.add_argument('--enable_nonfree', default=False, dest='enablenonfree', action='store_true', help='enable non-free modules (disabled by default)')
parser.add_argument('--debug', default=False, dest='debug', action='store_true', help='Build "Debug" binaries (disabled by default)')
parser.add_argument('--debug_info', default=False, dest='debug_info', action='store_true', help='Build with debug information (useful for Release mode: BUILD_WITH_DEBUG_INFO=ON)')
parser.add_argument('--framework_name', default='opencv2', dest='framework_name', action='store_true', help='Name of OpenCV framework (default: opencv2, will change to OpenCV in future version)')
parser.add_argument('--framework_name', default='opencv2', dest='framework_name', help='Name of OpenCV framework (default: opencv2, will change to OpenCV in future version)')
parser.add_argument('--legacy_build', default=False, dest='legacy_build', action='store_true', help='Build legacy opencv2 framework (default: False, equivalent to "--framework_name=opencv2 --without=objc")')
args = parser.parse_args()

Loading…
Cancel
Save