Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math module #108

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
2628ee4
math: create math module configuration
kaylendog Mar 28, 2021
c531e50
math: Interpolation
kaylendog Mar 28, 2021
56bc2b2
max: include guava to fix missing Preconditions
kaylendog Mar 28, 2021
05a42aa
wip: initial implementation of vector class
kaylendog Mar 29, 2021
f495c46
add ordinal generator
kaylendog Apr 24, 2021
db633ee
add missing space
kaylendog Apr 24, 2021
d4714af
wip :sparkles: fixup vector definition
kaylendog Apr 24, 2021
f2ff3db
wip :sparkles: add vector tests
kaylendog Apr 24, 2021
d580249
wip :sparkles: add averages methods
kaylendog Apr 24, 2021
510b490
wip :sparkles: port over NumberUtil from common
kaylendog Apr 24, 2021
627b84a
wip :sparkles: port over MathUtil -> RandomUtil from common
kaylendog Apr 24, 2021
17183e6
tweaks to median calculation, consistent @Test annotation
kaylendog Apr 24, 2021
e01d721
wip :sparkles: matrix2 and matrix3 implementations, translation builders
kaylendog Apr 24, 2021
fb3e29b
wip :sparkles: add distribution methods
kaylendog Apr 24, 2021
5a20b83
fix floating point inaccuracy in pcc test
kaylendog Apr 24, 2021
a31b974
add Distribution.isAssociated
kaylendog Apr 24, 2021
404244e
add README
kaylendog Apr 24, 2021
d9c784b
tweaks to NumberUtil and RandomUtil
kaylendog Apr 24, 2021
98cc75f
fix vector unit tests
kaylendog Apr 24, 2021
c088345
add null safety to Averages
kaylendog Apr 24, 2021
a15af50
fix fundamental flaw in vector methods
kaylendog Apr 24, 2021
3e620ef
fix: dataset is empty? trying to divide by zero? PROCEED AS NORMAL :3
kaylendog Apr 24, 2021
5838c45
wip :sparkles: matrix2 implementation
kaylendog Apr 24, 2021
6ed6eaf
compensate for floating point inaccuracy in TransformationBuilder test
kaylendog Apr 24, 2021
99b37c9
wip :sparkles: add parametric class
kaylendog Apr 25, 2021
612c72b
fixes to randomutil
kaylendog Apr 25, 2021
78f4dd2
add random vector methods
kaylendog Apr 25, 2021
3df79f5
add center parameter to random vector methods
kaylendog Apr 25, 2021
77d0923
add RandomUtil.randomIntExclusive
kaylendog Apr 25, 2021
3f902ef
move RandomUtil.randomElement methods to randomIntExclusive
kaylendog Apr 25, 2021
90f3d9d
tweaks to javadoc
kaylendog Apr 25, 2021
8de4206
add NumberUtil.isNumeric comment
kaylendog Apr 25, 2021
a9da72f
remove dependency on stickyapi:common
kaylendog Apr 25, 2021
6779650
wip :sparkles: cubic bezier implementation
kaylendog Apr 25, 2021
93465d8
amend parameter in bezier evaluation
kaylendog Apr 25, 2021
46ab4a8
add LocationUtil to bukkit
kaylendog Apr 25, 2021
16bda31
deprecate old MathUtils and RandomUtils
kaylendog Apr 25, 2021
3f4aa73
fix RandomUtil call
kaylendog Apr 25, 2021
8e97b31
add license header to CubicBezier
kaylendog Apr 25, 2021
1446c64
bump gradle version
kaylendog Apr 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {

allprojects {
group = "com.dumbdogdiner"
version = "3.0.2"
version = "3.1.0"

// java plugin is applied in subprojects
apply plugin: "jacoco"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2020-2021 DumbDogDiner <dumbdogdiner.com>. All rights reserved.
* Licensed under the MIT license, see LICENSE for more information...
*/
package com.dumbdogdiner.stickyapi.bukkit.util;

import com.dumbdogdiner.stickyapi.math.vector.Vector3;
import org.bukkit.Location;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;

/**
* Utilities for converting {@link Vector3}s into Bukkit's {@link Location} object.
*/
public final class LocationUtil {
private LocationUtil() {}

/**
* Convert the target {@link Vector3} into a Bukkit {@link Location}.
* @param world The world this location should be contained in
* @param vector The target vector
* @return A new {@link Location} in the target world.
*/
public static @NotNull Location toLocation(@NotNull World world, @NotNull Vector3 vector) {
return new Location(world, vector.getX(), vector.getY(), vector.getZ());
}

/**
* Convert the target {@link Location} into a {@link Vector3}.
* @param location The target location
* @return A new {@link Location} in the target world.
*/
public static @NotNull Vector3 toVector3(@NotNull Location location) {
return new Vector3(location.getX(), location.getY(), location.getZ());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
package com.dumbdogdiner.stickyapi.bukkit.util;

import com.dumbdogdiner.stickyapi.common.util.MathUtil;
import com.dumbdogdiner.stickyapi.math.RandomUtil;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
Expand Down Expand Up @@ -101,7 +101,7 @@ public static Predicate<Player> inRange(Location center, double radius) {
*/
public static Player selectRandom(Predicate<Player> condition) {
List<Player> list = selectPlayers(condition);
return list.get(MathUtil.randomInt(list.size()));
return RandomUtil.randomElement(list);
}

/**
Expand Down
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {
dependencies {
// Depend on the config project
api project(":config") // api - transistively expose config dependency when implementing :common
api project(":math") // api - transistively expose api dependency when implementing :common
testImplementation project(":config").sourceSets.test.output

// LocaleProviderTest - add snakeyaml so YamlProvider (from the :config project) can work properly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import java.util.List;

import com.dumbdogdiner.stickyapi.common.util.Debugger;
import com.dumbdogdiner.stickyapi.common.util.NumberUtil;
import com.dumbdogdiner.stickyapi.common.util.TimeUtil;

import com.dumbdogdiner.stickyapi.math.NumberUtil;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -356,7 +356,7 @@ public Arguments requiredTimeString(@NotNull String name) {

private Arguments optionalIntImplementation(@NotNull String name, @NotNull Integer fallback) {
debug.print("Looking for optional integer " + name + "...");
if (unparsedArgs.size() > position && NumberUtil.isNumeric(unparsedArgs.get(position))) {
if (unparsedArgs.size() > position && NumberUtil.isInteger(unparsedArgs.get(position), false)) {
parsedArgs.put(name, unparsedArgs.get(position));
position++;
debug.print("Found int at position " + String.valueOf(position) + " - new args size = "
Expand Down Expand Up @@ -405,7 +405,7 @@ public Arguments optionalInt(@NotNull String name, @NotNull Integer fallback) {
public Arguments requiredInt(@NotNull String name) {
debug.print("Looking for optional required " + name + "...");

if (unparsedArgs.size() > position && NumberUtil.isNumeric(unparsedArgs.get(position))) {
if (unparsedArgs.size() > position && NumberUtil.isInteger(unparsedArgs.get(position), false)) {
parsedArgs.put(name, unparsedArgs.get(position));
position++;
debug.print("Found int at position " + String.valueOf(position) + " - new args size = "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import java.util.Random;

/**
* <p>
* Util class for commonly used math operations.
* </p>
* @deprecated in favor of the stickyapi:math module.
*/
@Deprecated(since = "3.1")
public class MathUtil {
private MathUtil() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
* <p>
* Provides extra functionality for Java Number classes.
* </p>
*/
* @deprecated in favor of the stickyapi:math module.
*/
@Deprecated(since = "3.1")
public final class NumberUtil {
private NumberUtil() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;
import java.util.UUID;

import com.dumbdogdiner.stickyapi.math.RandomUtil;
import com.google.common.base.Preconditions;

import net.md_5.bungee.api.ChatColor;
Expand Down Expand Up @@ -333,16 +334,16 @@ public static String randomObfuscatedString(int min, int max, int minRunBeforeSp
};
StringBuilder obfuscated = new StringBuilder();

int len = MathUtil.randomInt(min, max);
int len = RandomUtil.randomInt(min, max);
int charsSinceSpace = 0;
while (obfuscated.length() < len) {
if (minRunBeforeSpace > 0 && charsSinceSpace > minRunBeforeSpace &&
// Set a 5% probability of the character being a space
MathUtil.randomInt(1, 100) <= 5) {
RandomUtil.probability(0.05)) {
obfuscated.append(' ');
charsSinceSpace = 0;
} else {
obfuscated.append(MathUtil.randomElement(choices));
obfuscated.append(RandomUtil.randomElement(choices));
charsSinceSpace++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.Optional;

import com.dumbdogdiner.stickyapi.math.NumberUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -286,7 +287,7 @@ public static Timestamp toTimestamp(@NotNull String timePeriod) {
return null;

// If it's numeric, lets do some extra checks!
if (NumberUtil.isNumeric(timePeriod)) {
if (NumberUtil.isInteger(timePeriod, false)) {
// Return null if it's greater 12 characters long
if (timePeriod.length() > 12)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
*/
package com.dumbdogdiner.stickyapi.common.nbt;

import com.dumbdogdiner.stickyapi.math.RandomUtil;
import com.google.gson.JsonPrimitive;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;

import java.text.MessageFormat;

import static com.dumbdogdiner.stickyapi.common.util.MathUtil.randomDouble;
import static com.dumbdogdiner.stickyapi.common.util.MathUtil.randomInt;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.*;

Expand All @@ -23,8 +22,8 @@ class NbtNumberTagTest {

@BeforeEach
public void setUp(){
i = randomInt(-256, 4096);
f = (float) randomDouble(-256, 8192);
i = RandomUtil.randomInt(-256, 4096);
f = (float) RandomUtil.randomDouble(-256, 8192);
}

@RepeatedTest(REPEAT)
Expand Down
Empty file added config/README.md
Empty file.
3 changes: 3 additions & 0 deletions math/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# stickyapi:math

Provides a lightweight mathematics library for general purpose use.
4 changes: 4 additions & 0 deletions math/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies {
// TODO: Investigate why this isn't provided by common.
implementation "com.google.guava:guava:30.1.1-jre"
}
3 changes: 3 additions & 0 deletions math/lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This file is generated by the 'io.freefair.lombok' Gradle plugin
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2020-2021 DumbDogDiner <dumbdogdiner.com>. All rights reserved.
* Licensed under the MIT license, see LICENSE for more information...
*/
package com.dumbdogdiner.stickyapi.math;

import com.google.common.base.Preconditions;

/**
* Utility class for interpolating between values.
* TODO: Inaccuracy issues for longs.
*/
public final class Interpolation {
private Interpolation() {}

/**
* Clamp the value `t` between `min` and `max`.
* @param max The minimum value
* @param min The maximum value
* @param t The value to clamp
* @return The clamped value between `max` and `min`.
*/
public static double clamp(Number min, Number max, Number t) {
Preconditions.checkNotNull(min);
Preconditions.checkNotNull(max);
Preconditions.checkNotNull(t);
return Double.min(max.doubleValue(), Double.max(min.doubleValue(), t.doubleValue()));
}

/**
* Linearly interpolate between a and b with time parameter t.
* `t` is clamped between `0` and `1`.
* @param a The first value
* @param b The second value
* @param t The time parameter
* @return The linearly interpolated value between `a` and `b`.
*/
public static double lerp(Number a, Number b, Number t) {
return lerpUnclamped(a, b, clamp(0, 1, t));
}

/**s
* Linearly interpolate between a and b with time parameter t.
* `t` is not clamped between `0` and `1`.
* @param a The first value
* @param b The second value
* @param t The time parameter
* @return The linearly interpolated value.
*/
public static double lerpUnclamped(Number a, Number b, Number t) {
Preconditions.checkNotNull(a);
Preconditions.checkNotNull(b);
Preconditions.checkNotNull(t);
return b.doubleValue() * t.doubleValue() + (1 - t.doubleValue()) * a.doubleValue();
}

/**
* Sinusoidaly interpolate between <code>a</code> and <code>b</code> with time parameter <code>t</code>.
* @param a The first value
* @param b The second value
* @param t The time parameter
* @return The sinusoidaly interpolated value.
*/
public static double sinusoidal(Number a, Number b, Number t) {
// calculate sinusoidal param
double param = Math.pow(Math.sin(t.doubleValue() * Math.PI / 2), 2);
return lerp(a, b, param);
}
}
Loading