85 Commits

Author SHA1 Message Date
Colby Klein
8282157954 new tests for project/unproject 2022-04-30 15:38:13 -07:00
Colby Klein
7d22d13bca (breaking) simplify mat4.project and unproject
the separate view and projection just weren't really useful, might as
well pass in your own full transform instead.

this also fixes the z range, which should not have been manipulated.
this simplifies usage to check something as on screen to just 1 > z > 0

might break the test, but tested working locally
2022-04-30 14:10:51 -07:00
Colby Klein
62e5e16b46 mat4.mul can now concatenate a table of inputs 2022-04-30 12:07:54 -07:00
Colby Klein
0bff69f3ba add vec3.normalize_len, fix some doc typos 2022-04-21 12:10:04 -07:00
Colby Klein
919900fea2 add utils.sign 2022-04-21 11:53:00 -07:00
Colby Klein
9f57a1a939 remove unused a parameter from mat4.look_at 2022-04-21 11:22:55 -07:00
Colby Klein
13cd41770f fix formatting 2022-04-21 11:22:18 -07:00
shakesoda
4350c25d0c
Merge pull request #72 from idbrii/color
Overhaul color: fix bugs, change to 0-1
2022-04-21 10:47:54 -07:00
shakesoda
9b4b6e7fa7
Merge pull request #76 from xiejiangzhi/xjz
Add Vec3.angle_to
2022-04-21 10:46:01 -07:00
shakesoda
ab0132a6ac
Merge pull request #66 from aki-cat/master
Implement scale to mat4.from_transform method
2022-04-20 15:56:31 -07:00
jiangzhi.xie
6bc7ec7718 Add testing code for vec3.angle_to 2022-04-20 20:05:52 +08:00
xiejiangzhi
02d7604b17 Add vec3.angle_to 2022-04-20 19:13:08 +08:00
David Briscoe
7d99a08134 [Breaking] color: Convert hue [0,359] -> [0,1]
imgui [uses 0-1 for everything internally](
https://github.com/ocornut/imgui/blob/master/imgui.cpp#L2045).

[Both Unity](https://docs.unity3d.com/ScriptReference/Color.RGBToHSV.html)
[and Godot](https://docs.godotengine.org/en/stable/classes/class_color.html#class-color-property-h)
use H, S, and V in the range 0.0 to 1.0

Since we're switching colours out of 0,255 we should also switch hue out
of 0,359. Now all of our r,g,b,a,h,s,v values are all in 0,1.
2022-04-01 21:55:40 -07:00
David Briscoe
0c12f17805 color: Linear alpha in linear<->gamma conversions
Maintain linear alpha when converting to gamma.

Update to latest wikipedia links.

Add simple test that does the round trip conversion.
2022-04-01 21:55:40 -07:00
David Briscoe
41b1f0b5b9 color: Add more tests 2022-04-01 21:47:56 -07:00
David Briscoe
343f320e2f [Breaking] color: Convert 0-255 -> 0-1 range
Our hsv logic assumes 0-1 (cs.rit.edu says "r,g,b values are from 0 to
1") and it makes more sense to provide __mul for multiplying in 0-1
since the result will stay in that range. Additionally, love switched to
0-1 color since 11.0.

Included an as_255() function to unpack the color in 0-255 for some
amount of backwards compatibility. Doesn't make sense to provide any
kind of color object for it since most of our functions won't work
correctly.

Add tests for hue(), saturation(), value() that fail (even with /255
removed) for the old code, but pass on the new 0-1 range because the hsv
logic outputs colors in 0-1. Test comparisons use reduced precision
because my input data has limited precision.
2022-04-01 21:47:56 -07:00
David Briscoe
df92f0c5cd [Breaking] color: Make expected range match docs
lighten/darken: documented range is 0-255, so we don't need to multiply.
Makes more sense to work with colors in the same range that we're
incrementing by, but this changes behaviour.

saturation/value: we use a percent, so it's 0-1. It's clamped so that's
obviously expected.

Add tests to validate this new behaviour.
2022-04-01 21:47:09 -07:00
David Briscoe
509594aec2 color: Use color_mt and normal indexing
Fix #34.

Set color_mt on new colors so add, subtract, multiply, is_color work.

We don't use ffi for color (32cf0e8), so remove cdata check for 0-based
offset indexing.

Add tests for creating colours and using operators.
2022-04-01 19:55:55 -07:00
David Briscoe
29954aa60e vec: Add nan checking functions
Add:
* vec2.has_nan + test
* vec3.has_nan + test
* quat.has_nan
* mat4.has_nan
* utils.is_nan + test
2022-03-26 23:33:13 -07:00
aki-cat
d7c257387d Implement scale to mat4.from_transform method 2021-12-10 00:31:57 -03:00
David Briscoe
4dc773865e Reference private.round instead of util.round
Fix "Error: src/lib/cpml/modules/_private_utils.lua:8: attempt to index
    global 'utils' (a nil value)"

Looks like when round was moved to _private_utils, this didn't get
replaced. There is no utils defined and utils.round just points to
private.round.

Add a test. This never triggered test failures because test don't pass
precision.

Test
A love2d program with main.lua:
    local Vec2 = require "cpml.modules.vec2"
    local v = Vec2(1.234, 3.5326)
    print(v:round(0.1))
2021-11-24 09:55:57 -08:00
David Briscoe
39aee9a421 Fix angle_to to produce signed angle
angle_to was producing the angle from +x to the difference between a,b
which is unexpected. Instead, it should produce the smallest absolute
angle between the two vectors and be signed to indicate the direction of
rotation.

By using the old angle_to implementation and modifying equal() to print
out the failures, you can see the numbers that it was producing before
didn't make much sense:

    right:angle_to(down) = 45.0
    right:angle_to(left) = 0.0
    right:angle_to(up)   = -45.0
    down:angle_to(right) = -135.0
    down:angle_to(left)  = -45.0
    down:angle_to(up)    = -90.0
    left:angle_to(down)  = 135.0
    left:angle_to(up)    = -135.0
    up:angle_to(right)   = 135.0
    up:angle_to(down)    = 90.0
    up:angle_to(left)    = 45.0

Now it produces numbers you'd expect:

    right:angle_to(down) = -90.0
    right:angle_to(left) = 180.0
    right:angle_to(up)   = 90.0
    down:angle_to(right) = 90.0
    down:angle_to(left)  = -90.0
    down:angle_to(up)    = 180.0
    left:angle_to(down)  = 90.0
    left:angle_to(up)    = -90.0
    up:angle_to(right)   = -90.0
    up:angle_to(down)    = 180.0
    up:angle_to(left)    = 90.0

See also https://stackoverflow.com/questions/21483999/using-atan2-to-find-angle-between-two-vectors

Also added tests for angle_between.
2021-06-15 11:56:04 -07:00
mcc
ddb80f48e6 Compatibility note for 1.0 release and PR#48 test 2020-05-03 13:36:26 -04:00
mcclure
1000f1d6e5
Merge pull request #48 from mcclure/mat4-reverse
Consistency: mat4 multiply in wrong order
2020-05-03 13:13:11 -04:00
mcclure
61affeb669
Merge pull request #56 from mcclure/identity-quat
Fix for issue #55 (quat():to_angle_axis() returns gibberish)
2020-05-03 12:54:47 -04:00
mcc
336f416f29 Manual merge PR#53 2020-05-03 12:52:17 -04:00
mcc
f7497b9bf7 Manual merge PR#52 2020-05-03 12:50:42 -04:00
mcc
6e1cf4be56 Manual merge PR#51 2020-05-03 12:46:30 -04:00
mcclure
d4f7cd1280
Merge pull request #50 from mcclure/master
:round(precision) methods for vec2, vec3, bound2, bound3
2020-05-03 12:37:58 -04:00
mcclure
5601094c04
Merge pull request #49 from mcclure/vec4_cols
Column version of to_vec4s (to_vec4s_cols)
2020-05-03 12:37:22 -04:00
mcc
9cd858a0ef Add tests to identity-quat patch and also take an axis not a full value for the identity fallback 2020-04-25 18:32:48 -04:00
mcclure
86f0056397
Merge pull request #47 from mcclure/master-test-failures
Fix failing tests
2019-11-30 17:37:03 -05:00
mcc
185b19d766 vec2.to_vec3(z) creates a vec3 with the given z (or 0) 2019-11-30 11:31:22 -05:00
mcc
7452cc0c6c Simple methods for flipping a vector on exactly 1 axis 2019-11-30 11:25:00 -05:00
mcc
e3f817bf7e extend(vec) and extend_bound(bound) for bounds
expands bounds to cover new points
2019-11-30 01:24:48 -05:00
mcc
8e65db07ce :round(precision) methods for vec2, vec3, bound2, bound3
Uses utils.lua, which requires moving utils.round to a new _private_utils.lua
2019-11-30 00:03:55 -05:00
mcc
13cc666232 Column version of to_vec4s (to_vec4s_cols) for issue #32 2019-11-29 22:24:47 -05:00
mcc
d51a930e4a Consistency (issue #33): mat4 multiply in wrong order
This is a breaking change.
2019-11-29 22:12:22 -05:00
mcc
d845a479ac Fix typo in bound3 test which was breaking under LuaJIT 2019-11-29 21:08:04 -05:00
mcc
7667ea9a3d Fix typo in bound2 test which was breaking under LuaJIT 2019-11-29 17:55:03 -05:00
mcc
7fa1785469 Fix failing tests; this involves changing 2 bad tests and 1 bad behavior:
- vec2 to_polar/from_cartesian tests were testing for equality rather than using an epsilon.
- bound2.contains had two tests that were plain wrong.
- While I'm fixing the bounds test, bound2.contains and bound3.contains probably ought to test their own min and max values for inclusion.
- The implementation of mat4.look_at appears to be wrong. The final column was being set to 0,0,0,1 which comparing against other implementations does not seem to be correct. My replacement code is modeled on the method used in mat4x4_look_at() in linmath.h in GLFW, which says it's a reimplementation on the method from gluLookAt(). With this change the test passes as originally written.
2019-11-29 17:16:20 -05:00
mcc
b80543c242 Make vec2(vec2(1,2)) and vec3(vec3(1,2,3)) consistent between lua and luajit 2019-11-29 14:44:34 -05:00
Colby Klein
130fe2aca0
Merge pull request #30 from mcclure/aabb
Axis-aligned bounding box classes for 2 and 3 dimensions
2018-02-22 04:09:36 -08:00
mcc
7c18a65695 Bound2, bound3: Add tests and fix several major bugs so that those tests pass. 2018-01-27 00:46:56 -05:00
mcc
a41af4702b Quaternions: Add tests for component-wise to_angle_axis and from_angle_axis 2018-01-26 21:05:03 -05:00
bjorn
2ac5321bdf mat4:reflect; 2018-01-05 22:08:11 -08:00
Colby Klein
0ff68c69fd fix lerp for vec3, update tests for broken lerp case 2017-05-19 09:49:07 -07:00
Colby Klein
65e3676af5 swap lerp arg order, fix tests 2017-03-29 07:42:40 -07:00
Colby Klein
0b0bea16ef add some lerp/decay tests 2017-03-29 05:32:31 -07:00
Colby Klein
9776dbbf39 update quat test to reflect that pow() needs unit quats 2016-12-16 00:46:43 -08:00