Skip to content

Commit ddddbc1

Browse files
Ink Open Sourcecopybara-github
authored andcommitted
Add some of the remaining Parallelogram methods
PiperOrigin-RevId: 682434311
1 parent e1b3a4a commit ddddbc1

File tree

4 files changed

+231
-0
lines changed

4 files changed

+231
-0
lines changed

ink/geometry/internal/jni/BUILD.bazel

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ cc_library(
2828
":mesh_jni",
2929
":modeled_shape_jni",
3030
":mutable_envelope_jni",
31+
":parallelogram_jni",
3132
":parallelogram_jni_helper",
3233
":rect_jni",
34+
":rect_jni_helper",
3335
":triangle_jni",
3436
":vec_jni",
3537
":vec_jni_helper",
@@ -201,6 +203,27 @@ cc_library(
201203
alwayslink = 1,
202204
)
203205

206+
cc_library(
207+
name = "rect_jni_helper",
208+
srcs = ["rect_jni_helper.cc"],
209+
hdrs = ["rect_jni_helper.h"],
210+
# Temporarily limited visibility while new APIs are being developed.
211+
visibility = ["//visibility:private"],
212+
deps = [
213+
":vec_jni_helper",
214+
"//ink/geometry:rect",
215+
"//ink/jni/internal:jni_defines",
216+
"@com_google_absl//absl/log",
217+
"@com_google_absl//absl/log:absl_check",
218+
] + select({
219+
"@platforms//os:android": [],
220+
"//conditions:default": [
221+
"@rules_jni//jni",
222+
],
223+
}),
224+
alwayslink = 1,
225+
)
226+
204227
cc_library(
205228
name = "affine_transform_jni",
206229
srcs = ["affine_transform_jni.cc"],
@@ -219,6 +242,27 @@ cc_library(
219242
alwayslink = 1,
220243
)
221244

245+
cc_library(
246+
name = "parallelogram_jni",
247+
srcs = ["parallelogram_jni.cc"],
248+
deps = [
249+
":rect_jni_helper",
250+
":vec_jni_helper",
251+
"//ink/geometry:angle",
252+
"//ink/geometry:envelope",
253+
"//ink/geometry:quad",
254+
"//ink/geometry:vec",
255+
"//ink/jni/internal:jni_defines",
256+
"@com_google_absl//absl/log",
257+
] + select({
258+
"@platforms//os:android": [],
259+
"//conditions:default": [
260+
"@rules_jni//jni",
261+
],
262+
}),
263+
alwayslink = 1,
264+
)
265+
222266
cc_library(
223267
name = "parallelogram_jni_helper",
224268
srcs = ["parallelogram_jni_helper.cc"],
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <jni.h>
16+
17+
#include <utility>
18+
19+
#include "ink/geometry/angle.h"
20+
#include "ink/geometry/envelope.h"
21+
#include "ink/geometry/internal/jni/rect_jni_helper.h"
22+
#include "ink/geometry/internal/jni/vec_jni_helper.h"
23+
#include "ink/geometry/quad.h"
24+
#include "ink/geometry/vec.h"
25+
#include "ink/jni/internal/jni_defines.h"
26+
27+
namespace {
28+
29+
using ::ink::Angle;
30+
using ::ink::Envelope;
31+
using ::ink::Quad;
32+
using ::ink::Vec;
33+
34+
} // namespace
35+
36+
extern "C" {
37+
38+
JNI_METHOD(geometry, ParallelogramNative, jobject, nativeCreateBoundingBox)
39+
(JNIEnv* env, jclass clazz, jfloat center_x, jfloat center_y, jfloat width,
40+
jfloat height, jfloat rotation, jfloat shear_factor,
41+
jclass immutable_box_class, jclass immutable_vec_class) {
42+
Quad quad = Quad::FromCenterDimensionsRotationAndShear(
43+
{center_x, center_y}, width, height, Angle::Radians(rotation),
44+
shear_factor);
45+
46+
return ink::CreateJImmutableBoxFromRect(env, Envelope(quad).AsRect().value(),
47+
immutable_box_class,
48+
immutable_vec_class);
49+
}
50+
51+
JNI_METHOD(geometry, ParallelogramNative, void, nativePopulateBoundingBox)
52+
(JNIEnv* env, jclass clazz, jfloat center_x, jfloat center_y, jfloat width,
53+
jfloat height, jfloat rotation, jfloat shear_factor, jobject mutable_box) {
54+
Quad quad = Quad::FromCenterDimensionsRotationAndShear(
55+
{center_x, center_y}, width, height, Angle::Radians(rotation),
56+
shear_factor);
57+
58+
ink::FillJMutableBoxFromRect(env, mutable_box,
59+
Envelope(quad).AsRect().value());
60+
}
61+
62+
JNI_METHOD(geometry, ParallelogramNative, jobjectArray, nativeCreateSemiAxes)
63+
(JNIEnv* env, jclass clazz, jfloat center_x, jfloat center_y, jfloat width,
64+
jfloat height, jfloat rotation, jfloat shear_factor,
65+
jclass immutable_vec_class) {
66+
Quad quad = Quad::FromCenterDimensionsRotationAndShear(
67+
{center_x, center_y}, width, height, Angle::Radians(rotation),
68+
shear_factor);
69+
std::pair<Vec, Vec> axes = quad.SemiAxes();
70+
jobjectArray vector_array =
71+
env->NewObjectArray(2, immutable_vec_class, nullptr);
72+
env->SetObjectArrayElement(
73+
vector_array, 0,
74+
ink::CreateJImmutableVecFromVec(env, axes.first, immutable_vec_class));
75+
env->SetObjectArrayElement(
76+
vector_array, 1,
77+
ink::CreateJImmutableVecFromVec(env, axes.second, immutable_vec_class));
78+
return vector_array;
79+
}
80+
81+
JNI_METHOD(geometry, ParallelogramNative, void, nativePopulateSemiAxes)
82+
(JNIEnv* env, jclass clazz, jfloat center_x, jfloat center_y, jfloat width,
83+
jfloat height, jfloat rotation, jfloat shear_factor, jobject out_axis1,
84+
jobject out_axis2) {
85+
Quad quad = Quad::FromCenterDimensionsRotationAndShear(
86+
{center_x, center_y}, width, height, Angle::Radians(rotation),
87+
shear_factor);
88+
std::pair<Vec, Vec> axes = quad.SemiAxes();
89+
ink::FillJMutableVecFromVec(env, out_axis1, axes.first);
90+
ink::FillJMutableVecFromVec(env, out_axis2, axes.second);
91+
}
92+
93+
} // extern "C"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
// Copyright 2024 Google LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#include "ink/geometry/internal/jni/rect_jni_helper.h"
17+
18+
#include <jni.h>
19+
20+
#include "absl/log/absl_check.h"
21+
#include "ink/geometry/internal/jni/vec_jni_helper.h"
22+
#include "ink/geometry/rect.h"
23+
#include "ink/jni/internal/jni_defines.h"
24+
25+
namespace ink {
26+
27+
jobject CreateJImmutableBoxFromRect(JNIEnv* env, Rect rect,
28+
jclass immutable_box_class,
29+
jclass immutable_vec_class) {
30+
ABSL_CHECK(immutable_box_class);
31+
jmethodID from_two_points_method = env->GetStaticMethodID(
32+
immutable_box_class, "fromTwoPoints",
33+
"(L" INK_PACKAGE "/geometry/Vec;L" INK_PACKAGE
34+
"/geometry/Vec;)L" INK_PACKAGE "/geometry/ImmutableBox;");
35+
ABSL_CHECK(from_two_points_method);
36+
return env->CallStaticObjectMethod(
37+
immutable_box_class, from_two_points_method,
38+
ink::CreateJImmutableVecFromPoint(env, {rect.XMin(), rect.YMin()},
39+
immutable_vec_class),
40+
ink::CreateJImmutableVecFromPoint(env, {rect.XMax(), rect.YMax()},
41+
immutable_vec_class));
42+
}
43+
44+
void FillJMutableBoxFromRect(JNIEnv* env, jobject mutable_box, Rect rect) {
45+
jclass mutable_box_class = env->GetObjectClass(mutable_box);
46+
47+
jmethodID set_x_bound_method =
48+
env->GetMethodID(mutable_box_class, "setXBounds",
49+
"(FF)L" INK_PACKAGE "/geometry/MutableBox;");
50+
ABSL_CHECK(set_x_bound_method);
51+
env->CallObjectMethod(mutable_box, set_x_bound_method, rect.XMin(),
52+
rect.XMax());
53+
54+
jmethodID set_y_bound_method =
55+
env->GetMethodID(mutable_box_class, "setYBounds",
56+
"(FF)L" INK_PACKAGE "/geometry/MutableBox;");
57+
ABSL_CHECK(set_y_bound_method);
58+
env->CallObjectMethod(mutable_box, set_y_bound_method, rect.YMin(),
59+
rect.YMax());
60+
}
61+
62+
} // namespace ink
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef INK_GEOMETRY_INTERNAL_JNI_RECT_JNI_HELPER_H_
16+
#define INK_GEOMETRY_INTERNAL_JNI_RECT_JNI_HELPER_H_
17+
18+
#include <jni.h>
19+
20+
#include "ink/geometry/rect.h"
21+
22+
namespace ink {
23+
24+
jobject CreateJImmutableBoxFromRect(JNIEnv* env, Rect rect,
25+
jclass immutable_box_class,
26+
jclass immutable_vec_class);
27+
28+
void FillJMutableBoxFromRect(JNIEnv* env, jobject mutable_box, Rect rect);
29+
30+
} // namespace ink
31+
32+
#endif // INK_GEOMETRY_INTERNAL_JNI_RECT_JNI_HELPER_H_

0 commit comments

Comments
 (0)