Parcourir la source

Introduce Vector3

customisations
alemart il y a 10 mois
Parent
révision
45815671c4
3 fichiers modifiés avec 213 ajouts et 0 suppressions
  1. 61
    0
      docs/api/vector3.md
  2. 1
    0
      mkdocs.yml
  3. 151
    0
      src/geometry/vector3.ts

+ 61
- 0
docs/api/vector3.md Voir le fichier

@@ -0,0 +1,61 @@
1
+# Vector3
2
+
3
+A vector in 3D space.
4
+
5
+*Since:* 0.4.0
6
+
7
+## Properties
8
+
9
+### x
10
+
11
+`vector.x: number`
12
+
13
+The x coordinate of the vector.
14
+
15
+### y
16
+
17
+`vector.y: number`
18
+
19
+The y coordinate of the vector.
20
+
21
+### z
22
+
23
+`vector.z: number`
24
+
25
+The z coordinate of the vector.
26
+
27
+## Methods
28
+
29
+### length
30
+
31
+`vector.length(): number`
32
+
33
+Compute the length of the vector: `sqrt(x^2 + y^2 + z^2)`.
34
+
35
+**Returns**
36
+
37
+The length of the vector.
38
+
39
+### equals
40
+
41
+`vector.equals(v: Vector3): boolean`
42
+
43
+Check if `this` and `v` have the same coordinates.
44
+
45
+**Arguments**
46
+
47
+* `v: Vector3`. A vector.
48
+
49
+**Returns**
50
+
51
+`true` if `this` and `v` have the same coordinates.
52
+
53
+### toString
54
+
55
+`vector.toString(): string`
56
+
57
+Generate a string representation of the vector.
58
+
59
+**Returns**
60
+
61
+A string representation of the vector.

+ 1
- 0
mkdocs.yml Voir le fichier

@@ -96,6 +96,7 @@ nav:
96 96
       - 'PerspectiveView': 'api/perspective-view.md'
97 97
       - 'RigidTransform': 'api/rigid-transform.md'
98 98
       - 'Quaternion': 'api/quaternion.md'
99
+      - 'Vector3': 'api/vector3.md'
99 100
     - 'Visualization':
100 101
       - 'Viewport': 'api/viewport.md'
101 102
       - 'HUD': 'api/hud.md'

+ 151
- 0
src/geometry/vector3.ts Voir le fichier

@@ -0,0 +1,151 @@
1
+/*
2
+ * encantar.js
3
+ * GPU-accelerated Augmented Reality for the web
4
+ * Copyright (C) 2022-2024 Alexandre Martins <alemartf(at)gmail.com>
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Lesser General Public License as published
8
+ * by the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
+ *
19
+ * vector3.ts
20
+ * 3D vectors
21
+ */
22
+
23
+/** Small number */
24
+const EPSILON = 1e-6;
25
+
26
+// public / non-internal methods do not change the contents of the quaternion
27
+
28
+
29
+
30
+/**
31
+ * A vector in 3D space
32
+ */
33
+export class Vector3
34
+{
35
+    /** x coordinate */
36
+    public x: number;
37
+
38
+    /** y coordinate */
39
+    public y: number;
40
+
41
+    /** z coordinate */
42
+    public z: number;
43
+
44
+
45
+
46
+    /**
47
+     * Constructor
48
+     */
49
+    constructor(x: number = 0, y: number = 0, z: number = 0)
50
+    {
51
+        this.x = +x;
52
+        this.y = +y;
53
+        this.z = +z;
54
+    }
55
+
56
+    /**
57
+     * Instantiate a zero vector
58
+     * @returns a new zero vector
59
+     */
60
+    static Zero(): Vector3
61
+    {
62
+        return new Vector3(0, 0, 0);
63
+    }
64
+
65
+    /**
66
+     * The length of this vector
67
+     * @returns sqrt(x^2 + y^2 + z^2)
68
+     */
69
+    length(): number
70
+    {
71
+        const x = this.x;
72
+        const y = this.y;
73
+        const z = this.z;
74
+
75
+        return Math.sqrt(x*x + y*y + z*z);
76
+    }
77
+
78
+    /**
79
+     * Check if this and v have the same coordinates
80
+     * @param v a vector
81
+     * @returns true if this and v have the same coordinates
82
+     */
83
+    equals(v: Vector3): boolean
84
+    {
85
+        return this.x === v.x && this.y === v.y && this.z === v.z;
86
+    }
87
+
88
+    /**
89
+     * Convert to string
90
+     * @returns a string
91
+     */
92
+    toString(): string
93
+    {
94
+        const x = this.x.toFixed(5);
95
+        const y = this.y.toFixed(5);
96
+        const z = this.z.toFixed(5);
97
+
98
+        return `Vector3(${x},${y},${z})`;
99
+    }
100
+
101
+    /**
102
+     * Normalize this vector
103
+     * @returns this vector, normalized
104
+     * @internal
105
+     */
106
+    _normalize(): Vector3
107
+    {
108
+        const length = this.length();
109
+
110
+        if(length < EPSILON) // zero?
111
+            return this;
112
+
113
+        this.x /= length;
114
+        this.y /= length;
115
+        this.z /= length;
116
+
117
+        return this;
118
+    }
119
+
120
+    /**
121
+     * Copy v to this
122
+     * @param v a vector
123
+     * @returns this vector
124
+     * @internal
125
+     */
126
+    _copyFrom(v: Vector3): Vector3
127
+    {
128
+        this.x = v.x;
129
+        this.y = v.y;
130
+        this.z = v.z;
131
+
132
+        return this;
133
+    }
134
+
135
+    /**
136
+     * Set the coordinates of this vector
137
+     * @param x x-coordinate
138
+     * @param y y-coordinate
139
+     * @param z z-coordinate
140
+     * @returns this vector
141
+     * @internal
142
+     */
143
+    _set(x: number, y: number, z: number): Vector3
144
+    {
145
+        this.x = +x;
146
+        this.y = +y;
147
+        this.z = +z;
148
+
149
+        return this;
150
+    }
151
+}

Chargement…
Annuler
Enregistrer