|
| 1 | +// |
| 2 | +// FOCV_ObjectCreation.cpp |
| 3 | +// Pods |
| 4 | +// |
| 5 | +// Created by Łukasz Kurant on 06/08/2024. |
| 6 | +// |
| 7 | + |
| 8 | +#include "FOCV_Object.hpp" |
| 9 | +#include "FOCV_Storage.hpp" |
| 10 | +#include <opencv2/opencv.hpp> |
| 11 | + |
| 12 | +constexpr uint64_t hashString(const char* str, size_t length) { |
| 13 | + uint64_t hash = 14695981039346656037ull; |
| 14 | + const uint64_t fnv_prime = 1099511628211ull; |
| 15 | + |
| 16 | + for (size_t i = 0; i < length; ++i) { |
| 17 | + hash ^= static_cast<uint64_t>(str[i]); |
| 18 | + hash *= fnv_prime; |
| 19 | + } |
| 20 | + |
| 21 | + return hash; |
| 22 | +} |
| 23 | + |
| 24 | +jsi::String FOCV_Object::create(jsi::Runtime& runtime, const jsi::Value* arguments) { |
| 25 | + std::any object; |
| 26 | + std::string objectType = arguments[0].asString(runtime).utf8(runtime); |
| 27 | + |
| 28 | + switch(hashString(objectType.c_str(), objectType.size())) { |
| 29 | + case hashString("mat", 3): { |
| 30 | + int rows = arguments[1].asNumber(); |
| 31 | + int cols = arguments[2].asNumber(); |
| 32 | + int type = arguments[3].asNumber(); |
| 33 | + object = cv::Mat(rows, cols, type); |
| 34 | + } break; |
| 35 | + case hashString("mat_array", 9): { |
| 36 | + object = std::vector<cv::Mat>(); |
| 37 | + } break; |
| 38 | + case hashString("rect", 4): { |
| 39 | + int x = arguments[1].asNumber(); |
| 40 | + int y = arguments[2].asNumber(); |
| 41 | + int width = arguments[3].asNumber(); |
| 42 | + int height = arguments[4].asNumber(); |
| 43 | + object = cv::Rect(x, y, width, height); |
| 44 | + } break; |
| 45 | + case hashString("rect_array", 10): { |
| 46 | + object = std::vector<cv::Rect>(); |
| 47 | + } break; |
| 48 | + case hashString("point", 5): { |
| 49 | + int x = arguments[1].asNumber(); |
| 50 | + int y = arguments[2].asNumber(); |
| 51 | + object = cv::Point(x, y); |
| 52 | + } break; |
| 53 | + case hashString("point_array", 11): { |
| 54 | + object = std::vector<cv::Point>(); |
| 55 | + } break; |
| 56 | + case hashString("size", 4): { |
| 57 | + int width = arguments[1].asNumber(); |
| 58 | + int height = arguments[2].asNumber(); |
| 59 | + object = cv::Size(width, height); |
| 60 | + } break; |
| 61 | + case hashString("vec3b", 4): { |
| 62 | + int a = arguments[1].asNumber(); |
| 63 | + int b = arguments[2].asNumber(); |
| 64 | + int c = arguments[3].asNumber(); |
| 65 | + object = cv::Vec3b(a, b, c); |
| 66 | + } break; |
| 67 | + } |
| 68 | + |
| 69 | + std::string id = FOCV_Storage::save(object); |
| 70 | + return jsi::String::createFromUtf8(runtime, id); |
| 71 | +} |
| 72 | + |
| 73 | +jsi::Object FOCV_Object::convertToJSI(jsi::Runtime& runtime, const jsi::Value* arguments) { |
| 74 | + |
| 75 | + jsi::Object value(runtime); |
| 76 | + std::string objectType = arguments[0].asString(runtime).utf8(runtime); |
| 77 | + std::string id = arguments[1].asString(runtime).utf8(runtime); |
| 78 | + |
| 79 | + switch(hashString(objectType.c_str(), objectType.size())) { |
| 80 | + case hashString("mat", 3): { |
| 81 | + cv::Mat mat = FOCV_Storage::get<cv::Mat>(id); |
| 82 | + |
| 83 | + value.setProperty(runtime, "size", jsi::Value(mat.size)); |
| 84 | + value.setProperty(runtime, "cols", jsi::Value(mat.cols)); |
| 85 | + value.setProperty(runtime, "rows", jsi::Value(mat.rows)); |
| 86 | + |
| 87 | + } break; |
| 88 | + case hashString("mat_array", 9): { |
| 89 | + std::vector<cv::Mat> mats = FOCV_Storage::get<std::vector<cv::Mat>>(id); |
| 90 | + |
| 91 | + auto array = jsi::Array(runtime, mats.size()); |
| 92 | + |
| 93 | + for (int i = 0; i < mats.size(); i++) { |
| 94 | + jsi::Object item(runtime); |
| 95 | + |
| 96 | + item.setProperty(runtime, "size", jsi::Value(mats.at(i).size)); |
| 97 | + item.setProperty(runtime, "cols", jsi::Value(mats.at(i).cols)); |
| 98 | + item.setProperty(runtime, "rows", jsi::Value(mats.at(i).rows)); |
| 99 | + array.setValueAtIndex(runtime, i, item); |
| 100 | + } |
| 101 | + |
| 102 | + value.setProperty(runtime, "array", array); |
| 103 | + } break; |
| 104 | + case hashString("rect", 4): { |
| 105 | + cv::Rect rect = FOCV_Storage::get<cv::Rect>(id); |
| 106 | + |
| 107 | + value.setProperty(runtime, "x", jsi::Value(rect.x)); |
| 108 | + value.setProperty(runtime, "y", jsi::Value(rect.y)); |
| 109 | + value.setProperty(runtime, "width", jsi::Value(rect.width)); |
| 110 | + value.setProperty(runtime, "height", jsi::Value(rect.height)); |
| 111 | + } break; |
| 112 | + case hashString("rect_array", 10): { |
| 113 | + std::vector<cv::Rect> rects = FOCV_Storage::get<std::vector<cv::Rect>>(id); |
| 114 | + |
| 115 | + auto array = jsi::Array(runtime, rects.size()); |
| 116 | + |
| 117 | + for (int i = 0; i < rects.size(); i++) { |
| 118 | + jsi::Object item(runtime); |
| 119 | + |
| 120 | + item.setProperty(runtime, "x", jsi::Value(rects.at(i).x)); |
| 121 | + item.setProperty(runtime, "y", jsi::Value(rects.at(i).y)); |
| 122 | + item.setProperty(runtime, "width", jsi::Value(rects.at(i).width)); |
| 123 | + item.setProperty(runtime, "height", jsi::Value(rects.at(i).height)); |
| 124 | + array.setValueAtIndex(runtime, i, item); |
| 125 | + } |
| 126 | + |
| 127 | + value.setProperty(runtime, "array", array); |
| 128 | + } break; |
| 129 | + case hashString("point", 5): { |
| 130 | + cv::Point point = FOCV_Storage::get<cv::Point>(id); |
| 131 | + |
| 132 | + value.setProperty(runtime, "x", jsi::Value(point.x)); |
| 133 | + value.setProperty(runtime, "y", jsi::Value(point.y)); |
| 134 | + } break; |
| 135 | + case hashString("point_array", 11): { |
| 136 | + std::vector<cv::Point> points = FOCV_Storage::get<std::vector<cv::Point>>(id); |
| 137 | + |
| 138 | + auto array = jsi::Array(runtime, points.size()); |
| 139 | + |
| 140 | + for (int i = 0; i < points.size(); i++) { |
| 141 | + jsi::Object item(runtime); |
| 142 | + |
| 143 | + item.setProperty(runtime, "x", jsi::Value(points.at(i).x)); |
| 144 | + item.setProperty(runtime, "y", jsi::Value(points.at(i).y)); |
| 145 | + array.setValueAtIndex(runtime, i, item); |
| 146 | + } |
| 147 | + |
| 148 | + value.setProperty(runtime, "array", array); |
| 149 | + } break; |
| 150 | + case hashString("size", 4): { |
| 151 | + cv::Size size = FOCV_Storage::get<cv::Size>(id); |
| 152 | + |
| 153 | + value.setProperty(runtime, "width", jsi::Value(size.width)); |
| 154 | + value.setProperty(runtime, "height", jsi::Value(size.height)); |
| 155 | + } break; |
| 156 | + case hashString("vec3b", 4): { |
| 157 | + cv::Vec3b vec = FOCV_Storage::get<cv::Vec3b>(id); |
| 158 | + |
| 159 | + value.setProperty(runtime, "a", jsi::Value(vec.val[0])); |
| 160 | + value.setProperty(runtime, "b", jsi::Value(vec.val[1])); |
| 161 | + value.setProperty(runtime, "c", jsi::Value(vec.val[2])); |
| 162 | + } break; |
| 163 | + } |
| 164 | + |
| 165 | + jsi::Object object(runtime); |
| 166 | + object.setProperty(runtime, "value", value); |
| 167 | + |
| 168 | + return object; |
| 169 | +} |
0 commit comments