• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
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
16import { float32 } from "@koalaui/compat"
17
18export class Point3 {
19    x: float32
20    y: float32
21    z: float32
22    constructor(x: float32, y: float32, z: float32) {
23        this.x = x
24        this.y = y
25        this.z = z
26    }
27
28    subtract(value: Point3): Point3 {
29        return new Point3(
30            this.x - value.x,
31            this.y - value.y,
32            this.z - value.z
33        )
34    }
35
36    cross(value: Point3): Point3 {
37        return new Point3(
38            this.y * value.z - this.z * value.y,
39            this.z * value.x - this.x * value.z,
40            this.x * value.y - this.y * value.x
41        )
42    }
43
44    normalize(): Point3 {
45        const mag = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z) as float32
46        const tolerance = (1.0 / (1 << 12))
47        if (mag < tolerance) {
48            // This semicolon after return this is a workaround for ArkTS bug
49            return this;
50        }
51        return new Point3(this.x / mag, this.y / mag, this.z /  mag)
52    }
53}