Debian Codegraph @

tools/tungsten/third_party/kotlin-math/README.md

raw symbols SCIP
1 # kotlin-math
2 Set of Kotlin APIs to make graphics math easier to write. These APIs are mostly
3 modeled after GLSL (OpenGL Shading Language) to make porting code to and from
4 shaders easier.
5
6 The various types offered by this library are only meant to be _value types_.
7 Most APIs are therefore exposed as top-level functions and not as methods.
8 For instance:
9
10 ```kotlin
11 val v = Float3(1.0f, 3.0f, 4.0f)
12 val n = normalize(v)
13 ```
14
15 ## Building the project
16
17 Simply run the following command to generate `build/libs/kotlin-math.jar`:
18
19 ```bash
20 $ ./gradlew assemble
21 ```
22
23 ## Types
24
25 Vector types:
26 - Float2, vector of 2 floats
27 - Float3, vector of 3 floats
28 - Float4, vector of 4 floats
29 - Bool2, vector of 2 booleans
30 - Bool3, vector of 3 booleans
31 - Bool4, vector of 4 booleans
32
33 Matrix types:
34 - Mat3, 3x3 float matrix
35 - Mat4, 4x4 float matrix
36
37 ## Vector types
38
39 ### Accessing components
40
41 Each vector type exposes its component as properties:
42
43 ```kotlin
44 val x = myVector.x
45 val (x, y, z) = myVector
46 ```
47
48 A vector can also be treated as an array:
49
50 ```kotlin
51 val x = myVector[0]
52 val x = myVector[VectorComponents.X]
53 ```
54
55 The traditional mathematical form with 1-based indexing can be used:
56
57 ```kotlin
58 val x = myVector(1)
59 ```
60
61 ### Property aliases
62
63 To improve code readability, the vector types provide aliases for each property,
64 allowing you to choose the most appropriate names:
65
66 ```kotlin
67 val (x, y, z) = myPosition
68 val (r, g, b) = myColor
69 val (s, t) = myTextureCoordinates
70 ```
71
72 ### Swizzling
73
74 Vector types also provide different ways to swizzle their components, although
75 in a more limited way than in GLSL. The most obvious use for swizzling is to
76 extract sub-vectors:
77
78 ```kotlin
79 val position = Float3(…)
80 val position2d = position.xy // extract a Float2
81
82 val colorWithAlpha = Float4(…)
83 val rgbColor = colorWithAlpha.rgb // extract a Float3
84 ```
85
86 The get operators allows for more complex swizzling by enabling re-ordering and
87 duplication of the components:
88
89 ```kotlin
90 val colorWithAlpha = Float4(…)
91 val bgrColor = colorWithAlpha[
92 VectorComponents.B,
93 VectorComponents.G,
94 VectorComponents.R
95 ] // re-ordered 3 components sub-vector
96 ```
97
98 ### Comparing vector types
99
100 Vector comparisons follow GLSL rules:
101 - `==` returns true if all components are equal
102 - `!=` returns true if not all components are equal
103
104 In addition you can use component-wise relational operators that return a vector
105 of boolean with the result of each component-wise comparison:
106 - `lessThan`
107 - `lessThanEqual`
108 - `greaterThan`
109 - `greaterThanEqual`
110 - `equal`
111 - `notEqual`
112
113 Example:
114
115 ```kotlin
116 if (all(lessThan(v1, v2))) {
117 // …
118 }
119 ```
120
121 You can also use the following infix operators if you prefer the operator
122 syntax:
123 - `lt`
124 - `lte`
125 - `gt`
126 - `gte`
127 - `eq`
128 - `neq`
129
130 Example:
131
132 ```kotlin
133 if (any(v1 lte v2)) {
134 // …
135 }
136 ```
137
138 ## Matrix types
139
140 Matrices are represented as a set of column vectors. For instance, a `Mat4` can
141 be destructured into the right, up, forward and translation vectors:
142
143 ```kotlin
144 val (right, up, forward, translation) = myMat4
145 ```
146
147 Each vector can be accessed as a property or by its index:
148
149 ```kotlin
150 forward = myMat4.forward
151 forward = myMat4.z
152 forward = myMat4[2]
153 forward = myMat4[MatrixColumns.Z]
154 ```
155
156 Matrix types also offer APIs to access each element individually by specifying
157 the column then row:
158
159 ```kotlin
160 v = myMat4.z[1]
161 v = myMat4[2, 1]
162 v = myMat4[MatrixColumns.Z, 1]
163 ```
164
165 You can also use the invoke operator to access elements in row-major mode with
166 1-based indices to follow the traditional mathematical notation:
167
168 ```kotlin
169 v = myMat4(2, 3) // equivalent to myMat4[2, 1]
170 ```
171
172 ## Scalar APIs
173
174 The file `Scalar.kt` contains various helper methods to use common math operations
175 with floats. It is intended to be used in combination with Kotlin 1.2's new float
176 math methods.