Skip to content

Commit 9b9212f

Browse files
committed
Matrix transformations
1 parent c1bbe9f commit 9b9212f

File tree

5 files changed

+423
-7
lines changed

5 files changed

+423
-7
lines changed

build.sbt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@ lazy val root = (project in file("."))
77
name := "VecMatLib"
88
)
99

10-
libraryDependencies ++= Seq(
11-
"org.scalatest" %% "scalatest" % "3.2.14" % Test
12-
)
10+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.14" % Test

src/main/scala/vecmatlib/matrix/Mat3d.scala

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package vecmatlib.matrix
22

3-
import vecmatlib.vector.Vec3d
3+
import vecmatlib.vector.{Vec2d, Vec3d}
44

55
/**
66
* 3x3 double matrix.
@@ -155,4 +155,110 @@ object Mat3d {
155155
val Identity: Mat3d = Mat3d(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
156156
/** Shorthand for the zero matrix */
157157
val Zero: Mat3d = Mat3d(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
158+
159+
/**
160+
* Returns a 3x3 translation matrix with the given translation.
161+
*
162+
* @param x X component of the translation
163+
* @param y Y component of the translation
164+
* @return A 3x3 translation matrix
165+
*/
166+
def translation(x: Double, y: Double): Mat3d = Mat3d(
167+
1.0f, 0.0f, x,
168+
0.0f, 1.0f, y,
169+
0.0f, 0.0f, 1.0f
170+
)
171+
172+
/**
173+
* Returns a 3x3 translation matrix with the given translation.
174+
*
175+
* @param v The translation vector
176+
* @return A 3x3 translation matrix
177+
*/
178+
def translation(v: Vec2d): Mat3d = translation(v.x, v.y)
179+
180+
/**
181+
* Returns a 3x3 rotation matrix with the given rotation on the x axis.
182+
*
183+
* @param x Rotation angle in radians
184+
* @return A 3x3 rotation matrix
185+
*/
186+
def rotationX(x: Double): Mat3d = Mat3d(
187+
1.0f, 0.0f, 0.0f,
188+
0.0f, math.cos(-x), -math.sin(-x),
189+
0.0f, math.sin(-x), math.cos(-x)
190+
)
191+
192+
/**
193+
* Returns a 3x3 rotation matrix with the given rotation on the y axis.
194+
*
195+
* @param y Rotation angle in radians
196+
* @return A 3x3 rotation matrix
197+
*/
198+
def rotationY(y: Double): Mat3d = Mat3d(
199+
math.cos(-y), 0.0f, math.sin(-y),
200+
0.0f, 1.0f, 0.0f,
201+
-math.sin(-y), 0.0f, math.cos(-y)
202+
)
203+
204+
/**
205+
* Returns a 3x3 rotation matrix with the given rotation on the z axis.
206+
*
207+
* @param z Rotation angle in radians
208+
* @return A 3x3 rotation matrix
209+
*/
210+
def rotationZ(z: Double): Mat3d = Mat3d(
211+
math.cos(-z), -math.sin(-z), 0.0f,
212+
math.sin(-z), math.cos(-z), 0.0f,
213+
0.0f, 0.0f, 1.0f
214+
)
215+
216+
/**
217+
* Returns a 3x3 rotation matrix with the given rotation.
218+
*
219+
* @param x Rotation angle in radians on the x axis
220+
* @param y Rotation angle in radians on the y axis
221+
* @param z Rotation angle in radians on the z axis
222+
* @return A 3x3 rotation matrix
223+
*/
224+
def rotation(x: Double, y: Double, z: Double): Mat3d = rotationX(x) * rotationY(y) * rotationZ(z)
225+
226+
/**
227+
* Returns a 3x3 scaling matrix with the given scale.
228+
*
229+
* @param x Scale on the x axis
230+
* @param y Scale on the y axis
231+
* @param z Scale on the z axis
232+
* @return A 3x3 scaling matrix
233+
*/
234+
def scaling(x: Double, y: Double, z: Double): Mat3d = Mat3d(
235+
x, 0.0f, 0.0f,
236+
0.0f, y, 0.0f,
237+
0.0f, 0.0f, z
238+
)
239+
240+
/**
241+
* Returns a 3x3 scaling matrix with the given scale.
242+
*
243+
* @param x Scale on the x axis
244+
* @param y Scale on the y axis
245+
* @return A 3x3 scaling matrix
246+
*/
247+
def scaling(x: Double, y: Double): Mat3d = scaling(x, y, 1.0f)
248+
249+
/**
250+
* Returns a 3x3 scaling matrix with the given scale.
251+
*
252+
* @param v The scale vector
253+
* @return A 3x3 scaling matrix
254+
*/
255+
def scaling(v: Vec3d): Mat3d = scaling(v.x, v.y, v.z)
256+
257+
/**
258+
* Returns a 3x3 scaling matrix with the given scale.
259+
*
260+
* @param v The scale vector
261+
* @return A 3x3 scaling matrix
262+
*/
263+
def scaling(v: Vec2d): Mat3d = scaling(v.x, v.y, 1.0f)
158264
}

src/main/scala/vecmatlib/matrix/Mat3f.scala

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package vecmatlib.matrix
22

3-
import vecmatlib.vector.{Vec3d, Vec3f}
3+
import vecmatlib.vector.{Vec2f, Vec3d, Vec3f}
44

55
/**
66
* 3x3 float matrix.
@@ -172,4 +172,110 @@ object Mat3f {
172172
val Identity: Mat3f = Mat3f(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f)
173173
/** Shorthand for the zero matrix */
174174
val Zero: Mat3f = Mat3f(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)
175+
176+
/**
177+
* Returns a 3x3 translation matrix with the given translation.
178+
*
179+
* @param x X component of the translation
180+
* @param y Y component of the translation
181+
* @return A 3x3 translation matrix
182+
*/
183+
def translation(x: Float, y: Float): Mat3f = Mat3f(
184+
1.0f, 0.0f, x,
185+
0.0f, 1.0f, y,
186+
0.0f, 0.0f, 1.0f
187+
)
188+
189+
/**
190+
* Returns a 3x3 translation matrix with the given translation.
191+
*
192+
* @param v The translation vector
193+
* @return A 3x3 translation matrix
194+
*/
195+
def translation(v: Vec2f): Mat3f = translation(v.x, v.y)
196+
197+
/**
198+
* Returns a 3x3 rotation matrix with the given rotation on the x axis.
199+
*
200+
* @param x Rotation angle in radians
201+
* @return A 3x3 rotation matrix
202+
*/
203+
def rotationX(x: Float): Mat3f = Mat3f(
204+
1.0f, 0.0f, 0.0f,
205+
0.0f, math.cos(-x).toFloat, -math.sin(-x).toFloat,
206+
0.0f, math.sin(-x).toFloat, math.cos(-x).toFloat
207+
)
208+
209+
/**
210+
* Returns a 3x3 rotation matrix with the given rotation on the y axis.
211+
*
212+
* @param y Rotation angle in radians
213+
* @return A 3x3 rotation matrix
214+
*/
215+
def rotationY(y: Float): Mat3f = Mat3f(
216+
math.cos(-y).toFloat, 0.0f, math.sin(-y).toFloat,
217+
0.0f, 1.0f, 0.0f,
218+
-math.sin(-y).toFloat, 0.0f, math.cos(-y).toFloat
219+
)
220+
221+
/**
222+
* Returns a 3x3 rotation matrix with the given rotation on the z axis.
223+
*
224+
* @param z Rotation angle in radians
225+
* @return A 3x3 rotation matrix
226+
*/
227+
def rotationZ(z: Float): Mat3f = Mat3f(
228+
math.cos(-z).toFloat, -math.sin(-z).toFloat, 0.0f,
229+
math.sin(-z).toFloat, math.cos(-z).toFloat, 0.0f,
230+
0.0f, 0.0f, 1.0f
231+
)
232+
233+
/**
234+
* Returns a 3x3 rotation matrix with the given rotation.
235+
*
236+
* @param x Rotation angle in radians on the x axis
237+
* @param y Rotation angle in radians on the y axis
238+
* @param z Rotation angle in radians on the z axis
239+
* @return A 3x3 rotation matrix
240+
*/
241+
def rotation(x: Float, y: Float, z: Float): Mat3f = rotationX(x) * rotationY(y) * rotationZ(z)
242+
243+
/**
244+
* Returns a 3x3 scaling matrix with the given scale.
245+
*
246+
* @param x Scale on the x axis
247+
* @param y Scale on the y axis
248+
* @param z Scale on the z axis
249+
* @return A 3x3 scaling matrix
250+
*/
251+
def scaling(x: Float, y: Float, z: Float): Mat3f = Mat3f(
252+
x, 0.0f, 0.0f,
253+
0.0f, y, 0.0f,
254+
0.0f, 0.0f, z
255+
)
256+
257+
/**
258+
* Returns a 3x3 scaling matrix with the given scale.
259+
*
260+
* @param x Scale on the x axis
261+
* @param y Scale on the y axis
262+
* @return A 3x3 scaling matrix
263+
*/
264+
def scaling(x: Float, y: Float): Mat3f = scaling(x, y, 1.0f)
265+
266+
/**
267+
* Returns a 3x3 scaling matrix with the given scale.
268+
*
269+
* @param v The scale vector
270+
* @return A 3x3 scaling matrix
271+
*/
272+
def scaling(v: Vec3f): Mat3f = scaling(v.x, v.y, v.z)
273+
274+
/**
275+
* Returns a 3x3 scaling matrix with the given scale.
276+
*
277+
* @param v The scale vector
278+
* @return A 3x3 scaling matrix
279+
*/
280+
def scaling(v: Vec2f): Mat3f = scaling(v.x, v.y, 1.0f)
175281
}

src/main/scala/vecmatlib/matrix/Mat4d.scala

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package vecmatlib.matrix
22

3-
import vecmatlib.vector.Vec4d
3+
import vecmatlib.vector.{Vec3d, Vec4d}
44

55
/**
66
* 4x4 double matrix.
@@ -184,4 +184,107 @@ object Mat4d {
184184
val Identity: Mat4d = Mat4d(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0)
185185
/** Shorthand for the zero matrix */
186186
val Zero: Mat4d = Mat4d(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
187+
188+
/**
189+
* Returns a 4x4 translation matrix with the given translation.
190+
*
191+
* @param x X component of the translation
192+
* @param y Y component of the translation
193+
* @param z Z component of the translation
194+
* @return A 4x4 translation matrix
195+
*/
196+
def translation(x: Double, y: Double, z: Double): Mat4d = Mat4d(
197+
1.0, 0.0, 0.0, x,
198+
0.0, 1.0, 0.0, y,
199+
0.0, 0.0, 1.0, z,
200+
0.0, 0.0, 0.0, 1.0
201+
)
202+
203+
/**
204+
* Returns a 4x4 translation matrix with the given translation.
205+
*
206+
* @param v The translation vector
207+
* @return A 4x4 translation matrix
208+
*/
209+
def translation(v: Vec3d): Mat4d = translation(v.x, v.y, v.z)
210+
211+
/**
212+
* Returns a 4x4 rotation matrix with the given rotation on the x axis.
213+
*
214+
* @param x Rotation angle in radians
215+
* @return A 4x4 rotation matrix
216+
*/
217+
def rotationX(x: Double): Mat4d = Mat4d(
218+
1.0, 0.0, 0.0, 0.0,
219+
0.0, math.cos(-x), -math.sin(-x), 0.0,
220+
0.0, math.sin(-x), math.cos(-x), 0.0,
221+
0.0, 0.0, 0.0, 1.0
222+
)
223+
224+
/**
225+
* Returns a 4x4 rotation matrix with the given rotation on the y axis.
226+
*
227+
* @param y Rotation angle in radians
228+
* @return A 4x4 rotation matrix
229+
*/
230+
def rotationY(y: Double): Mat4d = Mat4d(
231+
math.cos(-y), 0.0, math.sin(-y), 0.0,
232+
0.0, 1.0, 0.0, 0.0,
233+
-math.sin(-y), 0.0, math.cos(-y), 0.0,
234+
0.0, 0.0, 0.0, 1.0
235+
)
236+
237+
/**
238+
* Returns a 4x4 rotation matrix with the given rotation on the z axis.
239+
*
240+
* @param z Rotation angle in radians
241+
* @return A 4x4 rotation matrix
242+
*/
243+
def rotationZ(z: Double): Mat4d = Mat4d(
244+
math.cos(-z), -math.sin(-z), 0.0, 0.0,
245+
math.sin(-z), math.cos(-z), 0.0, 0.0,
246+
0.0, 0.0, 1.0, 0.0,
247+
0.0, 0.0, 0.0, 1.0
248+
)
249+
250+
/**
251+
* Returns a 4x4 rotation matrix with the given rotation.
252+
*
253+
* @param x Rotation angle in radians on the x axis
254+
* @param y Rotation angle in radians on the y axis
255+
* @param z Rotation angle in radians on the z axis
256+
* @return A 4x4 rotation matrix
257+
*/
258+
def rotation(x: Double, y: Double, z: Double): Mat4d = rotationX(x) * rotationY(y) * rotationZ(z)
259+
260+
/**
261+
* Returns a 4x4 rotation matrix with the given rotation.
262+
*
263+
* @param v Vector representing the rotation in radians
264+
* @return A 4x4 rotation matrix
265+
*/
266+
def rotation(v: Vec3d): Mat4d = rotation(v.x, v.y, v.z)
267+
268+
/**
269+
* Returns a 4x4 scaling matrix with the given scale.
270+
*
271+
* @param x Scale on the x axis
272+
* @param y Scale on the y axis
273+
* @param z Scale on the z axis
274+
* @return A 4x4 scaling matrix
275+
*/
276+
def scaling(x: Double, y: Double, z: Double): Mat4d = Mat4d(
277+
x, 0.0, 0.0, 0.0,
278+
0.0, y, 0.0, 0.0,
279+
0.0, 0.0, z, 0.0,
280+
0.0, 0.0, 0.0, 1.0
281+
)
282+
283+
/**
284+
* Returns a 4x4 scaling matrix with the given scale.
285+
*
286+
* @param v The scale vector
287+
* @return A 4x4 scaling matrix
288+
*/
289+
def scaling(v: Vec3d): Mat4d = scaling(v.x, v.y, v.z)
187290
}

0 commit comments

Comments
 (0)