diff --git a/.checkstyle b/.checkstyle new file mode 100644 index 0000000..34ed486 --- /dev/null +++ b/.checkstyle @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..d43dd0c --- /dev/null +++ b/.classpath @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d075be7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +/bin/ +/Operator/ +/DrawerProperties/ +*.pdfd +*.dbc +SchedulerConfig.txt +scenicView.properties +ScenariumConfig.txt +*.class +*~ +*.bck +build.number +/extern/ +/out/ +/.settings/ +/junit/ +/target/ diff --git a/.project b/.project new file mode 100644 index 0000000..9f45611 --- /dev/null +++ b/.project @@ -0,0 +1,24 @@ + + + atriasoft-etk + + + atriasoft-etk + + + + org.eclipse.jdt.core.javabuilder + + + + + net.sf.eclipsecs.core.CheckstyleBuilder + + + + + + org.eclipse.jdt.core.javanature + net.sf.eclipsecs.core.CheckstyleNature + + diff --git a/CheckStyle.xml b/CheckStyle.xml new file mode 100755 index 0000000..d68aedd --- /dev/null +++ b/CheckStyle.xml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/CleanUp.xml b/CleanUp.xml new file mode 100644 index 0000000..9df98d2 --- /dev/null +++ b/CleanUp.xml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Formatter.xml b/Formatter.xml new file mode 100644 index 0000000..79dcbb8 --- /dev/null +++ b/Formatter.xml @@ -0,0 +1,366 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a612ad9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,373 @@ +Mozilla Public License Version 2.0 +================================== + +1. Definitions +-------------- + +1.1. "Contributor" + means each individual or legal entity that creates, contributes to + the creation of, or owns Covered Software. + +1.2. "Contributor Version" + means the combination of the Contributions of others (if any) used + by a Contributor and that particular Contributor's Contribution. + +1.3. "Contribution" + means Covered Software of a particular Contributor. + +1.4. "Covered Software" + means Source Code Form to which the initial Contributor has attached + the notice in Exhibit A, the Executable Form of such Source Code + Form, and Modifications of such Source Code Form, in each case + including portions thereof. + +1.5. "Incompatible With Secondary Licenses" + means + + (a) that the initial Contributor has attached the notice described + in Exhibit B to the Covered Software; or + + (b) that the Covered Software was made available under the terms of + version 1.1 or earlier of the License, but not also under the + terms of a Secondary License. + +1.6. "Executable Form" + means any form of the work other than Source Code Form. + +1.7. "Larger Work" + means a work that combines Covered Software with other material, in + a separate file or files, that is not Covered Software. + +1.8. "License" + means this document. + +1.9. "Licensable" + means having the right to grant, to the maximum extent possible, + whether at the time of the initial grant or subsequently, any and + all of the rights conveyed by this License. + +1.10. "Modifications" + means any of the following: + + (a) any file in Source Code Form that results from an addition to, + deletion from, or modification of the contents of Covered + Software; or + + (b) any new file in Source Code Form that contains any Covered + Software. + +1.11. "Patent Claims" of a Contributor + means any patent claim(s), including without limitation, method, + process, and apparatus claims, in any patent Licensable by such + Contributor that would be infringed, but for the grant of the + License, by the making, using, selling, offering for sale, having + made, import, or transfer of either its Contributions or its + Contributor Version. + +1.12. "Secondary License" + means either the GNU General Public License, Version 2.0, the GNU + Lesser General Public License, Version 2.1, the GNU Affero General + Public License, Version 3.0, or any later versions of those + licenses. + +1.13. "Source Code Form" + means the form of the work preferred for making modifications. + +1.14. "You" (or "Your") + means an individual or a legal entity exercising rights under this + License. For legal entities, "You" includes any entity that + controls, is controlled by, or is under common control with You. For + purposes of this definition, "control" means (a) the power, direct + or indirect, to cause the direction or management of such entity, + whether by contract or otherwise, or (b) ownership of more than + fifty percent (50%) of the outstanding shares or beneficial + ownership of such entity. + +2. License Grants and Conditions +-------------------------------- + +2.1. Grants + +Each Contributor hereby grants You a world-wide, royalty-free, +non-exclusive license: + +(a) under intellectual property rights (other than patent or trademark) + Licensable by such Contributor to use, reproduce, make available, + modify, display, perform, distribute, and otherwise exploit its + Contributions, either on an unmodified basis, with Modifications, or + as part of a Larger Work; and + +(b) under Patent Claims of such Contributor to make, use, sell, offer + for sale, have made, import, and otherwise transfer either its + Contributions or its Contributor Version. + +2.2. Effective Date + +The licenses granted in Section 2.1 with respect to any Contribution +become effective for each Contribution on the date the Contributor first +distributes such Contribution. + +2.3. Limitations on Grant Scope + +The licenses granted in this Section 2 are the only rights granted under +this License. No additional rights or licenses will be implied from the +distribution or licensing of Covered Software under this License. +Notwithstanding Section 2.1(b) above, no patent license is granted by a +Contributor: + +(a) for any code that a Contributor has removed from Covered Software; + or + +(b) for infringements caused by: (i) Your and any other third party's + modifications of Covered Software, or (ii) the combination of its + Contributions with other software (except as part of its Contributor + Version); or + +(c) under Patent Claims infringed by Covered Software in the absence of + its Contributions. + +This License does not grant any rights in the trademarks, service marks, +or logos of any Contributor (except as may be necessary to comply with +the notice requirements in Section 3.4). + +2.4. Subsequent Licenses + +No Contributor makes additional grants as a result of Your choice to +distribute the Covered Software under a subsequent version of this +License (see Section 10.2) or under the terms of a Secondary License (if +permitted under the terms of Section 3.3). + +2.5. Representation + +Each Contributor represents that the Contributor believes its +Contributions are its original creation(s) or it has sufficient rights +to grant the rights to its Contributions conveyed by this License. + +2.6. Fair Use + +This License is not intended to limit any rights You have under +applicable copyright doctrines of fair use, fair dealing, or other +equivalents. + +2.7. Conditions + +Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted +in Section 2.1. + +3. Responsibilities +------------------- + +3.1. Distribution of Source Form + +All distribution of Covered Software in Source Code Form, including any +Modifications that You create or to which You contribute, must be under +the terms of this License. You must inform recipients that the Source +Code Form of the Covered Software is governed by the terms of this +License, and how they can obtain a copy of this License. You may not +attempt to alter or restrict the recipients' rights in the Source Code +Form. + +3.2. Distribution of Executable Form + +If You distribute Covered Software in Executable Form then: + +(a) such Covered Software must also be made available in Source Code + Form, as described in Section 3.1, and You must inform recipients of + the Executable Form how they can obtain a copy of such Source Code + Form by reasonable means in a timely manner, at a charge no more + than the cost of distribution to the recipient; and + +(b) You may distribute such Executable Form under the terms of this + License, or sublicense it under different terms, provided that the + license for the Executable Form does not attempt to limit or alter + the recipients' rights in the Source Code Form under this License. + +3.3. Distribution of a Larger Work + +You may create and distribute a Larger Work under terms of Your choice, +provided that You also comply with the requirements of this License for +the Covered Software. If the Larger Work is a combination of Covered +Software with a work governed by one or more Secondary Licenses, and the +Covered Software is not Incompatible With Secondary Licenses, this +License permits You to additionally distribute such Covered Software +under the terms of such Secondary License(s), so that the recipient of +the Larger Work may, at their option, further distribute the Covered +Software under the terms of either this License or such Secondary +License(s). + +3.4. Notices + +You may not remove or alter the substance of any license notices +(including copyright notices, patent notices, disclaimers of warranty, +or limitations of liability) contained within the Source Code Form of +the Covered Software, except that You may alter any license notices to +the extent required to remedy known factual inaccuracies. + +3.5. Application of Additional Terms + +You may choose to offer, and to charge a fee for, warranty, support, +indemnity or liability obligations to one or more recipients of Covered +Software. However, You may do so only on Your own behalf, and not on +behalf of any Contributor. You must make it absolutely clear that any +such warranty, support, indemnity, or liability obligation is offered by +You alone, and You hereby agree to indemnify every Contributor for any +liability incurred by such Contributor as a result of warranty, support, +indemnity or liability terms You offer. You may include additional +disclaimers of warranty and limitations of liability specific to any +jurisdiction. + +4. Inability to Comply Due to Statute or Regulation +--------------------------------------------------- + +If it is impossible for You to comply with any of the terms of this +License with respect to some or all of the Covered Software due to +statute, judicial order, or regulation then You must: (a) comply with +the terms of this License to the maximum extent possible; and (b) +describe the limitations and the code they affect. Such description must +be placed in a text file included with all distributions of the Covered +Software under this License. Except to the extent prohibited by statute +or regulation, such description must be sufficiently detailed for a +recipient of ordinary skill to be able to understand it. + +5. Termination +-------------- + +5.1. The rights granted under this License will terminate automatically +if You fail to comply with any of its terms. However, if You become +compliant, then the rights granted under this License from a particular +Contributor are reinstated (a) provisionally, unless and until such +Contributor explicitly and finally terminates Your grants, and (b) on an +ongoing basis, if such Contributor fails to notify You of the +non-compliance by some reasonable means prior to 60 days after You have +come back into compliance. Moreover, Your grants from a particular +Contributor are reinstated on an ongoing basis if such Contributor +notifies You of the non-compliance by some reasonable means, this is the +first time You have received notice of non-compliance with this License +from such Contributor, and You become compliant prior to 30 days after +Your receipt of the notice. + +5.2. If You initiate litigation against any entity by asserting a patent +infringement claim (excluding declaratory judgment actions, +counter-claims, and cross-claims) alleging that a Contributor Version +directly or indirectly infringes any patent, then the rights granted to +You by any and all Contributors for the Covered Software under Section +2.1 of this License shall terminate. + +5.3. In the event of termination under Sections 5.1 or 5.2 above, all +end user license agreements (excluding distributors and resellers) which +have been validly granted by You or Your distributors under this License +prior to termination shall survive termination. + +************************************************************************ +* * +* 6. Disclaimer of Warranty * +* ------------------------- * +* * +* Covered Software is provided under this License on an "as is" * +* basis, without warranty of any kind, either expressed, implied, or * +* statutory, including, without limitation, warranties that the * +* Covered Software is free of defects, merchantable, fit for a * +* particular purpose or non-infringing. The entire risk as to the * +* quality and performance of the Covered Software is with You. * +* Should any Covered Software prove defective in any respect, You * +* (not any Contributor) assume the cost of any necessary servicing, * +* repair, or correction. This disclaimer of warranty constitutes an * +* essential part of this License. No use of any Covered Software is * +* authorized under this License except under this disclaimer. * +* * +************************************************************************ + +************************************************************************ +* * +* 7. Limitation of Liability * +* -------------------------- * +* * +* Under no circumstances and under no legal theory, whether tort * +* (including negligence), contract, or otherwise, shall any * +* Contributor, or anyone who distributes Covered Software as * +* permitted above, be liable to You for any direct, indirect, * +* special, incidental, or consequential damages of any character * +* including, without limitation, damages for lost profits, loss of * +* goodwill, work stoppage, computer failure or malfunction, or any * +* and all other commercial damages or losses, even if such party * +* shall have been informed of the possibility of such damages. This * +* limitation of liability shall not apply to liability for death or * +* personal injury resulting from such party's negligence to the * +* extent applicable law prohibits such limitation. Some * +* jurisdictions do not allow the exclusion or limitation of * +* incidental or consequential damages, so this exclusion and * +* limitation may not apply to You. * +* * +************************************************************************ + +8. Litigation +------------- + +Any litigation relating to this License may be brought only in the +courts of a jurisdiction where the defendant maintains its principal +place of business and such litigation shall be governed by laws of that +jurisdiction, without reference to its conflict-of-law provisions. +Nothing in this Section shall prevent a party's ability to bring +cross-claims or counter-claims. + +9. Miscellaneous +---------------- + +This License represents the complete agreement concerning the subject +matter hereof. If any provision of this License is held to be +unenforceable, such provision shall be reformed only to the extent +necessary to make it enforceable. Any law or regulation which provides +that the language of a contract shall be construed against the drafter +shall not be used to construe this License against a Contributor. + +10. Versions of the License +--------------------------- + +10.1. New Versions + +Mozilla Foundation is the license steward. Except as provided in Section +10.3, no one other than the license steward has the right to modify or +publish new versions of this License. Each version will be given a +distinguishing version number. + +10.2. Effect of New Versions + +You may distribute the Covered Software under the terms of the version +of the License under which You originally received the Covered Software, +or under the terms of any subsequent version published by the license +steward. + +10.3. Modified Versions + +If you create software not governed by this License, and you want to +create a new license for such software, you may create and use a +modified version of this License if you rename the license and remove +any references to the name of the license steward (except to note that +such modified license differs from this License). + +10.4. Distributing Source Code Form that is Incompatible With Secondary +Licenses + +If You choose to distribute Source Code Form that is Incompatible With +Secondary Licenses under the terms of this version of the License, the +notice described in Exhibit B of this License must be attached. + +Exhibit A - Source Code Form License Notice +------------------------------------------- + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + +If it is not possible or desirable to put the notice in a particular +file, then You may include the notice in a location (such as a LICENSE +file in a relevant directory) where a recipient would be likely to look +for such a notice. + +You may add additional accurate notices of copyright ownership. + +Exhibit B - "Incompatible With Secondary Licenses" Notice +--------------------------------------------------------- + + This Source Code Form is "Incompatible With Secondary Licenses", as + defined by the Mozilla Public License, v. 2.0. diff --git a/src/module-info.java b/src/module-info.java new file mode 100644 index 0000000..fd1ce90 --- /dev/null +++ b/src/module-info.java @@ -0,0 +1,9 @@ +/** Basic module interface. + * + * @author Edouard DUPIN */ + +open module org.atriasoft.etk { + exports org.atriasoft.etk; + exports org.atriasoft.etk.math; + requires transitive io.scenarium.logger; +} diff --git a/src/org/atriasoft/etk/Color.java b/src/org/atriasoft/etk/Color.java new file mode 100644 index 0000000..7f51443 --- /dev/null +++ b/src/org/atriasoft/etk/Color.java @@ -0,0 +1,25 @@ +package org.atriasoft.etk; + +public class Color { + @Override + public String toString() { + return "Color [r=" + r + ", g=" + g + ", b=" + b + ", a=" + a + "]"; + } + public float r; + public float g; + public float b; + public float a; + public Color(float r, float g, float b, float a) { + this.r = r; + this.g = g; + this.b = b; + this.a = a; + } + public Color(float r, float g, float b) { + super(); + this.r = r; + this.g = g; + this.b = b; + this.a = 1.0f; + } +} diff --git a/src/org/atriasoft/etk/ThreadAbstract.java b/src/org/atriasoft/etk/ThreadAbstract.java new file mode 100644 index 0000000..adba966 --- /dev/null +++ b/src/org/atriasoft/etk/ThreadAbstract.java @@ -0,0 +1,61 @@ +package org.atriasoft.etk; + +public abstract class ThreadAbstract { + // thread section: + private boolean threadStopRequested = false; + private Thread threadInstance = null; + private final String threadName; + public ThreadAbstract(String name) { + this.threadName = name; + } + + public void threadStart() { + System.out.println("INFO: Start the thread : " + this.threadName); + if (threadInstance != null) { + threadStop(); + } + threadStopRequested = false; + threadInstance = new Thread() { + public void run() { + threadRun(); + } + }; + threadInstance.setName(threadName); + threadInstance.start(); + } + + private void threadRun() { + System.out.println("INFO: Thread Start: " + threadName); + birth(); + while (threadStopRequested == false) { + try { + runPeriodic(); + } catch (Exception eee) { + eee.printStackTrace(); + } + } + death(); + System.out.println("INFO: Thread Stop: " + threadName); + } + protected abstract void birth(); + protected abstract void runPeriodic(); + protected abstract void death(); + + public void threadStop() { + if (threadStopRequested == true) { + return; + } + threadStopRequested = true; + if (threadInstance == null) { + return; + } + threadInstance.interrupt(); + try { + threadInstance.join(); + } catch (InterruptedException eee) { + // nothing to do + } + threadInstance = null; + } + +} diff --git a/src/org/atriasoft/etk/Uri.java b/src/org/atriasoft/etk/Uri.java new file mode 100644 index 0000000..4f6a12e --- /dev/null +++ b/src/org/atriasoft/etk/Uri.java @@ -0,0 +1,39 @@ +package org.atriasoft.etk; + +import java.util.HashMap; +import java.util.Map; + +public class Uri { + private static Map genericMap = new HashMap(); + + public static void setGroup(String groupName, String basePath) { + genericMap.put(groupName.toUpperCase(), basePath); + } + private final String value; + + public Uri(String value) { + this.value = value; + } + + public Uri(String group, String path) { + this.value = group.toUpperCase() + ":" + path; + } + + public String getValue() { + return value; + } + + public String getPath() { + String[] ret = value.split(":",2); + return genericMap.get(ret[0]) + "/" + ret[1]; + } + public String get() { + return getPath(); + } + + @Override + public String toString() { + return "Uri [value=" + value + "]"; + } + +} diff --git a/src/org/atriasoft/etk/internal/Log.java b/src/org/atriasoft/etk/internal/Log.java new file mode 100644 index 0000000..123a89a --- /dev/null +++ b/src/org/atriasoft/etk/internal/Log.java @@ -0,0 +1,60 @@ +package org.atriasoft.etk.internal; + +import io.scenarium.logger.LogLevel; +import io.scenarium.logger.Logger; + +public class Log { + private static final String LIB_NAME = "etk"; + private static final String LIB_NAME_DRAW = Logger.getDrawableName(LIB_NAME); + private static final boolean PRINT_CRITICAL = Logger.getNeedPrint(LIB_NAME, LogLevel.CRITICAL); + private static final boolean PRINT_ERROR = Logger.getNeedPrint(LIB_NAME, LogLevel.ERROR); + private static final boolean PRINT_WARNING = Logger.getNeedPrint(LIB_NAME, LogLevel.WARNING); + private static final boolean PRINT_INFO = Logger.getNeedPrint(LIB_NAME, LogLevel.INFO); + private static final boolean PRINT_DEBUG = Logger.getNeedPrint(LIB_NAME, LogLevel.DEBUG); + private static final boolean PRINT_VERBOSE = Logger.getNeedPrint(LIB_NAME, LogLevel.VERBOSE); + private static final boolean PRINT_TODO = Logger.getNeedPrint(LIB_NAME, LogLevel.TODO); + private static final boolean PRINT_PRINT = Logger.getNeedPrint(LIB_NAME, LogLevel.PRINT); + + private Log() {} + + public static void print(String data) { + if (PRINT_PRINT) + Logger.print(LIB_NAME_DRAW, data); + } + + public static void todo(String data) { + if (PRINT_TODO) + Logger.todo(LIB_NAME_DRAW, data); + } + + public static void critical(String data) { + if (PRINT_CRITICAL) + Logger.critical(LIB_NAME_DRAW, data); + } + + public static void error(String data) { + if (PRINT_ERROR) + Logger.error(LIB_NAME_DRAW, data); + } + + public static void warning(String data) { + if (PRINT_WARNING) + Logger.warning(LIB_NAME_DRAW, data); + } + + public static void info(String data) { + if (PRINT_INFO) + Logger.info(LIB_NAME_DRAW, data); + } + + public static void debug(String data) { + if (PRINT_DEBUG) + Logger.debug(LIB_NAME_DRAW, data); + } + + public static void verbose(String data) { + if (PRINT_VERBOSE) + Logger.verbose(LIB_NAME_DRAW, data); + } + +} diff --git a/src/org/atriasoft/etk/math/Matrix3f.java b/src/org/atriasoft/etk/math/Matrix3f.java new file mode 100644 index 0000000..4491614 --- /dev/null +++ b/src/org/atriasoft/etk/math/Matrix3f.java @@ -0,0 +1,435 @@ +package org.atriasoft.etk.math; + + +public class Matrix3f { + public float[] mat = new float[3*3]; //!< matrix data + /** + * @brief Constructor that load zero matrix + */ + public Matrix3f(){ + this.mat[0] = 0.0f; + this.mat[1] = 0.0f; + this.mat[2] = 0.0f; + this.mat[3] = 0.0f; + this.mat[4] = 0.0f; + this.mat[5] = 0.0f; + this.mat[6] = 0.0f; + this.mat[7] = 0.0f; + this.mat[8] = 0.0f; + } + /** + * @brief Configuration constructorwith single value. + * @param value single value + */ + public Matrix3f(float value){ + this.mat[0] = value; + this.mat[1] = value; + this.mat[2] = value; + this.mat[3] = value; + this.mat[4] = value; + this.mat[5] = value; + this.mat[6] = value; + this.mat[7] = value; + this.mat[8] = value; + } + /** + * @brief Configuration constructor. + * @param a1 element 0x0 + * @param a2 element 0x1 + * @param a3 element 0x2 + * @param b1 element 1x0 + * @param b2 element 1x1 + * @param b3 element 1x2 + * @param c1 element 2x0 + * @param c2 element 2x1 + * @param c3 element 2x2 + */ + public Matrix3f(float a1, float a2, float a3, + float b1, float b2, float b3, + float c1, float c2, float c3) { + this.mat[0] = a1; this.mat[1] = a2; this.mat[2] = a3; + this.mat[3] = b1; this.mat[4] = b2; this.mat[5] = b3; + this.mat[6] = c1; this.mat[7] = c2; this.mat[8] = c3; + } + + /** + * @brief Copy constructor. + * @param obj Matrix object to copy + */ + public Matrix3f(Matrix3f obj) { + this.mat[0] = obj.mat[0]; + this.mat[1] = obj.mat[1]; + this.mat[2] = obj.mat[2]; + this.mat[3] = obj.mat[3]; + this.mat[4] = obj.mat[4]; + this.mat[5] = obj.mat[5]; + this.mat[6] = obj.mat[6]; + this.mat[7] = obj.mat[7]; + this.mat[8] = obj.mat[8]; + } + /** + * @brief Set Value on the matrix + * @param a1 element 0x0 + * @param a2 element 0x1 + * @param a3 element 0x2 + * @param b1 element 1x0 + * @param b2 element 1x1 + * @param b3 element 1x2 + * @param c1 element 2x0 + * @param c2 element 2x1 + * @param c3 element 2x2 + */ + public void set(float a1, float a2, float a3, + float b1, float b2, float b3, + float c1, float c2, float c3) { + this.mat[0] = a1; this.mat[1] = a2; this.mat[2] = a3; + this.mat[3] = b1; this.mat[4] = b2; this.mat[5] = b3; + this.mat[6] = c1; this.mat[7] = c2; this.mat[8] = c3; + } + /** + * @brief Load Zero matrix + */ + public void setZero() { + this.mat[0] = 0.0f; + this.mat[1] = 0.0f; + this.mat[2] = 0.0f; + this.mat[3] = 0.0f; + this.mat[4] = 0.0f; + this.mat[5] = 0.0f; + this.mat[6] = 0.0f; + this.mat[7] = 0.0f; + this.mat[8] = 0.0f; + } + + /** + * @brief get the colom id values + * @param iii Id of the colomn + * @return Vector 3D vith the values + */ + Vector3f getColumn(int iii) { + if (iii == 0) { + return new Vector3f(this.mat[0], this.mat[3], this.mat[6]); + } else if (iii == 1) { + return new Vector3f(this.mat[1], this.mat[4], this.mat[7]); + } + return new Vector3f(this.mat[2], this.mat[5], this.mat[8]); + } + /** + * @brief get the row id values + * @param iii Id of the row + * @return Vector 3D vith the values + */ + public Vector3f getRow(int iii) { + if (iii == 0) { + return new Vector3f(this.mat[0], this.mat[1], this.mat[2]); + } else if (iii == 1) { + return new Vector3f(this.mat[3], this.mat[4], this.mat[5]); + } + return new Vector3f(this.mat[6], this.mat[7], this.mat[8]); + } + public float get(int iii) { + return this.mat[iii]; + } + /** + * @brief get a transpose matrix of this one. + * @return the transpose matrix + */ + public Matrix3f getTranspose() { + return new Matrix3f(this.mat[0], this.mat[3], this.mat[6], + this.mat[1], this.mat[4], this.mat[7], + this.mat[2], this.mat[5], this.mat[8]); +} + + /** + * @brief Transpose the current matrix. + */ + public void transpose() { + float tmp = this.mat[1]; + this.mat[1] = this.mat[3]; + this.mat[3] = tmp; + tmp = this.mat[2]; + this.mat[2] = this.mat[6]; + this.mat[6] = tmp; + tmp = this.mat[5]; + this.mat[5] = this.mat[7]; + this.mat[7] = tmp; + } + /** + * @brief Computes the determinant of the matrix. + * @return The determinent Value. + */ + public float determinant() { + return this.mat[0] * (this.mat[4] * this.mat[8]-this.mat[7] * this.mat[5]) + - this.mat[1] * (this.mat[3] * this.mat[8]-this.mat[6] * this.mat[5]) + + this.mat[2] * (this.mat[3] * this.mat[7]-this.mat[6] * this.mat[4]); + } + /** + * @brief Calculate the trace of the matrix + * @return value of addition of all element in the diagonal + */ + public float getTrace() { + return (this.mat[0] + this.mat[4] + this.mat[8]); + } + /** + * @brief Inverts the matrix. + * @note The determinant must be != 0, otherwithe the matrix can't be inverted. + * @return The inverted matrix. + */ + public Matrix3f inverseNew() { + Matrix3f tmp = new Matrix3f(this); + tmp.inverse(); + return tmp; + } + + /** + * @brief Inverts the current matrix. + * @note The determinant must be != 0, otherwithe the matrix can't be inverted. + */ + public Matrix3f inverse(){ + float det = determinant(); + //assert(Math.abs(det) > MACHINEEPSILON); + float invDet = 1.0f / det; + this.set( (this.mat[4] * this.mat[8]-this.mat[7] * this.mat[5]), + -(this.mat[1] * this.mat[8]-this.mat[7] * this.mat[2]), + (this.mat[1] * this.mat[5]-this.mat[2] * this.mat[4]), + -(this.mat[3] * this.mat[8]-this.mat[6] * this.mat[5]), + (this.mat[0] * this.mat[8]-this.mat[6] * this.mat[2]), + -(this.mat[0] * this.mat[5]-this.mat[3] * this.mat[2]), + (this.mat[3] * this.mat[7]-this.mat[6] * this.mat[4]), + -(this.mat[0] * this.mat[7]-this.mat[6] * this.mat[1]), + (this.mat[0] * this.mat[4]-this.mat[1] * this.mat[3])); + this.multiply(invDet); + return this; + } + /** + * @brief get the matrix with the absolute value + * @return matix in absolute + */ + public Matrix3f getAbsolute() { + return new Matrix3f(Math.abs(this.mat[0]), Math.abs(this.mat[1]), Math.abs(this.mat[2]), + Math.abs(this.mat[3]), Math.abs(this.mat[4]), Math.abs(this.mat[5]), + Math.abs(this.mat[6]), Math.abs(this.mat[7]), Math.abs(this.mat[8])); +} + /** + * @brief absolutise the matrix + */ + public void absolute(){ + this.mat[0] = Math.abs(this.mat[0]); + this.mat[1] = Math.abs(this.mat[1]); + this.mat[2] = Math.abs(this.mat[2]); + this.mat[3] = Math.abs(this.mat[3]); + this.mat[4] = Math.abs(this.mat[4]); + this.mat[5] = Math.abs(this.mat[5]); + this.mat[6] = Math.abs(this.mat[6]); + this.mat[7] = Math.abs(this.mat[7]); + this.mat[8] = Math.abs(this.mat[8]); + } + /** + * @brief Load Identity matrix + */ + public void setIdentity(){ + this.mat[0] = 1.0f; this.mat[1] = 0.0f; this.mat[2] = 0.0f; + this.mat[3] = 0.0f; this.mat[4] = 1.0f; this.mat[5] = 0.0f; + this.mat[6] = 0.0f; this.mat[7] = 0.0f; this.mat[8] = 1.0f; + } + /** + * @brief create a Identity matrix + * @return created new matrix + */ + public static Matrix3f identity() { + return new Matrix3f(1, 0, 0, 0, 1, 0, 0, 0, 1); + } + /** + * @brief create a ZERO matrix + * @return created new matrix + */ + public static Matrix3f zero() { + return new Matrix3f(0, 0, 0, 0, 0, 0, 0, 0, 0); + } + /** + * @brief create a skew-symmetric matrix using a given vector that can be used to compute cross product with another vector using matrix multiplication + * @param vector Vector to comute + * @return Matrix to compute + */ + public static Matrix3f computeSkewSymmetricMatrixForCrossProduct(Vector3f vector) { + return new Matrix3f( 0.0f , -vector.z, vector.y, + vector.z, 0.0f , -vector.x, + -vector.y, vector.x, 0.0f); + } + /** + * @brief Operator= Asign the current object with an other object + * @param obj Reference on the external object + * @return Local reference of the vector asigned + */ + public Matrix3f set(Matrix3f obj ){ + for(int iii=0; iii<3*3 ; ++iii) { + this.mat[iii] = obj.mat[iii]; + } + return this; + } + /** + * @brief Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are identical + * @return false The Objects are NOT identical + */ + boolean isEqual(Matrix3f obj) { + for(int iii=0; iii<3*3 ; ++iii) { + if(this.mat[iii] != obj.mat[iii]) { + return false; + } + } + return true; + } + /** + * @brief In-Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are NOT identical + * @return false The Objects are identical + */ + public boolean isDifferent(Matrix3f obj) { + for(int iii=0; iii<3*3 ; ++iii) { + if(this.mat[iii] != obj.mat[iii]) { + return true; + } + } + return false; + } + /** + * @brief Operator+= Addition an other matrix with this one + * @param obj Reference on the external object + * @return Local reference of the vector additionned + */ + public Matrix3f add(Matrix3f obj){ + for(int iii=0; iii<3*3 ; ++iii) { + this.mat[iii] += obj.mat[iii]; + } + return this; + } + /** + * @brief Operator+ Addition an other matrix with this one + * @param obj Reference on the external object + * @return New vector containing the value + */ + public Matrix3f addNew(Matrix3f obj) { + Matrix3f tmp = new Matrix3f(this); + tmp.add(obj); + return tmp; + } + /** + * @brief Operator-= Decrement an other matrix with this one + * @param obj Reference on the external object + * @return Local reference of the vector decremented + */ + public Matrix3f less(Matrix3f obj) { + for(int iii=0; iii<3*3 ; ++iii) { + this.mat[iii] -= obj.mat[iii]; + } + return this; + } + /** + * @brief Operator- Decrement an other matrix with this one + * @param obj Reference on the external object + * @return New vector containing the value + */ + public Matrix3f lessNew(Matrix3f obj) { + Matrix3f tmp = new Matrix3f(this); + tmp.less(obj); + return tmp; + } + /** + * @brief Operator*= Multiplication an other matrix with this one + * @param obj Reference on the external object + * @return Local reference of the vector multiplicated + */ + public Matrix3f multiply(Matrix3f obj) { + float a1 = this.mat[0]*obj.mat[0] + this.mat[1]*obj.mat[3] + this.mat[2]*obj.mat[6]; + float b1 = this.mat[3]*obj.mat[0] + this.mat[4]*obj.mat[3] + this.mat[5]*obj.mat[6]; + float c1 = this.mat[6]*obj.mat[0] + this.mat[7]*obj.mat[3] + this.mat[8]*obj.mat[6]; + float a2 = this.mat[0]*obj.mat[1] + this.mat[1]*obj.mat[4] + this.mat[2]*obj.mat[7]; + float b2 = this.mat[3]*obj.mat[1] + this.mat[4]*obj.mat[4] + this.mat[5]*obj.mat[7]; + float c2 = this.mat[6]*obj.mat[1] + this.mat[7]*obj.mat[4] + this.mat[8]*obj.mat[7]; + this.mat[2] = this.mat[0]*obj.mat[2] + this.mat[1]*obj.mat[5] + this.mat[2]*obj.mat[8]; + this.mat[5] = this.mat[3]*obj.mat[2] + this.mat[4]*obj.mat[5] + this.mat[5]*obj.mat[8]; + this.mat[8] = this.mat[6]*obj.mat[2] + this.mat[7]*obj.mat[5] + this.mat[8]*obj.mat[8]; + this.mat[0] = a1; + this.mat[3] = b1; + this.mat[6] = c1; + this.mat[1] = a2; + this.mat[4] = b2; + this.mat[7] = c2; + return this; + } + /** + * @brief Operator* Multiplication an other matrix with this one + * @param obj Reference on the external object + * @return New vector containing the value + */ + public Matrix3f multiplyNew(Matrix3f obj) { + Matrix3f tmp = new Matrix3f(this); + tmp.multiply(obj); + return tmp; + } + /** + * @brief Operator*= Multiplication a value + * @param value value to multiply all the matrix + * @return Local reference of the vector multiplicated + */ + public Matrix3f multiply(float value){ + this.mat[0] *= value; + this.mat[1] *= value; + this.mat[2] *= value; + this.mat[3] *= value; + this.mat[4] *= value; + this.mat[5] *= value; + this.mat[6] *= value; + this.mat[7] *= value; + this.mat[8] *= value; + return this; + } + /** + * @brief Operator*= Multiplication a value + * @param value value to multiply all the matrix + * @return Local reference of the vector multiplicated + */ + public Matrix3f multiplyNew(float value) { + Matrix3f tmp = new Matrix3f(this); + tmp.multiply(value); + return tmp; + } + /** + * @brief Operator* apply matrix on a vector + * @param point Point value to apply the matrix + * @return New vector containing the value + */ + public Vector3f multiply(Vector3f point) { + return new Vector3f(point.x * this.mat[0] + point.y * this.mat[1] + point.z * this.mat[2], + point.x * this.mat[3] + point.y * this.mat[4] + point.z * this.mat[5], + point.x * this.mat[6] + point.y * this.mat[7] + point.z * this.mat[8]); + } + /** + * @brief Create a matrix 3D with a simple rotation + * @param normal vector aroud witch apply the rotation + * @param angleRad Radian angle to set at the matrix + * @return New matrix of the transformation requested + */ + public static Matrix3f createMatrixRotate(Vector3f normal, float angleRad) { + Matrix3f tmp = new Matrix3f(); + float cosVal = (float)Math.cos(angleRad); + float sinVal = (float)Math.sin(angleRad); + float invVal = 1.0f-cosVal; + // set rotation : + tmp.mat[0] = normal.x * normal.x * invVal + cosVal; + tmp.mat[1] = normal.x * normal.y * invVal - normal.z * sinVal; + tmp.mat[2] = normal.x * normal.z * invVal + normal.y * sinVal; + + tmp.mat[3] = normal.y * normal.x * invVal + normal.z * sinVal; + tmp.mat[4] = normal.y * normal.y * invVal + cosVal; + tmp.mat[5] = normal.y * normal.z * invVal - normal.x * sinVal; + + tmp.mat[6] = normal.z * normal.x * invVal - normal.y * sinVal; + tmp.mat[7] = normal.z * normal.y * invVal + normal.x * sinVal; + tmp.mat[8] = normal.z * normal.z * invVal + cosVal; + return tmp; + } +} diff --git a/src/org/atriasoft/etk/math/Matrix4f.java b/src/org/atriasoft/etk/math/Matrix4f.java new file mode 100644 index 0000000..88f4866 --- /dev/null +++ b/src/org/atriasoft/etk/math/Matrix4f.java @@ -0,0 +1,538 @@ +package org.atriasoft.etk.math; + +public class Matrix4f { + public float[] mat = new float[4*4]; //!< matrix data + /** + * @brief configure identity of the matrix + */ + public void setIdentity() { + for(int iii=0; iii<4*4 ; iii++) { + this.mat[iii] = 0; + } + this.mat[0] = 1.0f; + this.mat[5] = 1.0f; + this.mat[10] = 1.0f; + this.mat[15] = 1.0f; + } + public static Matrix4f identity() { + Matrix4f tmp = new Matrix4f(); + tmp.setIdentity(); + return tmp; + } + /** + * @brief Constructor that load identity + */ + public Matrix4f() { + setIdentity(); + } + /** + * @brief Configuration constructor. + * @param a1 1st colomn, 1 line value + * @param b1 2nd colomn, 1 line value + * @param c1 3rd colomn, 1 line value + * @param d1 4th colomn, 1 line value + * @param a2 1st colomn, 2 line value + * @param b2 2nd colomn, 2 line value + * @param c2 3rd colomn, 2 line value + * @param d2 4th colomn, 2 line value + * @param a3 1st colomn, 3 line value + * @param b3 2nd colomn, 3 line value + * @param c3 3rd colomn, 3 line value + * @param d3 4th colomn, 3 line value + * @param a4 1st colomn, 4 line value + * @param b4 2nd colomn, 4 line value + * @param c4 3rd colomn, 4 line value + * @param d4 4th colomn, 4 line value + */ + public Matrix4f(float a1, float b1, float c1, float d1, + float a2, float b2, float c2, float d2, + float a3, float b3, float c3, float d3, + float a4, float b4, float c4, float d4) { + this.mat[0] = a1; + this.mat[1] = b1; + this.mat[2] = c1; + this.mat[3] = d1; + this.mat[4] = a2; + this.mat[5] = b2; + this.mat[6] = c2; + this.mat[7] = d2; + this.mat[8] = a3; + this.mat[9] = b3; + this.mat[10] = c3; + this.mat[11] = d3; + this.mat[12] = a4; + this.mat[13] = b4; + this.mat[14] = c4; + this.mat[15] = d4; + } + /** + * @brief Configuration constructor. + * @param values vector of values + */ + public Matrix4f(float[] values) { + if (values == null) { + setIdentity(); + return; + } + for(int iii=0; iii<16 ; ++iii) { + this.mat[iii] = values[iii]; + } + } + public Matrix4f(Matrix3f matrix) { + this.mat[0] = matrix.mat[0]; + this.mat[1] = matrix.mat[1]; + this.mat[2] = matrix.mat[2]; + this.mat[3] = 0; + this.mat[4] = matrix.mat[3]; + this.mat[5] = matrix.mat[4]; + this.mat[6] = matrix.mat[5]; + this.mat[7] = 0; + this.mat[8] = matrix.mat[6]; + this.mat[9] = matrix.mat[7]; + this.mat[10] = matrix.mat[8]; + this.mat[11] = 0; + this.mat[12] = 0; + this.mat[13] = 0; + this.mat[14] = 0; + this.mat[15] = 1; +// this.mat[0] = matrix.mat[0]; +// this.mat[1] = matrix.mat[3]; +// this.mat[2] = matrix.mat[6]; +// this.mat[3] = 0; +// this.mat[4] = matrix.mat[1]; +// this.mat[5] = matrix.mat[4]; +// this.mat[6] = matrix.mat[7]; +// this.mat[7] = 0; +// this.mat[8] = matrix.mat[2]; +// this.mat[9] = matrix.mat[5]; +// this.mat[10] = matrix.mat[8]; +// this.mat[11] = 0; +// this.mat[12] = 0; +// this.mat[13] = 0; +// this.mat[14] = 0; +// this.mat[15] = 1; + } + /** + * @brief Operator= Asign the current object with an other object + * @param obj Reference on the external object + */ + public void set(Matrix4f obj) { + for(int iii=0; iii<16 ; iii++) { + this.mat[iii] = obj.mat[iii]; + } + } + /** + * @brief Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are identical + * @return false The Objects are NOT identical + */ + public boolean isEqual(Matrix4f obj) { + for(int iii=0; iii<4*4 ; ++iii) { + if(this.mat[iii] != obj.mat[iii]) { + return false; + } + } + return true; + } + /** + * @brief In-Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are NOT identical + * @return false The Objects are identical + */ + public boolean isDifferent(Matrix4f obj) { + for(int iii=0; iii<4*4 ; ++iii) { + if(this.mat[iii] != obj.mat[iii]) { + return true; + } + } + return false; + } + /** + * @brief Operator+= Addition an other matrix with this one + * @param obj Reference on the external object + */ + public void add(Matrix4f obj) { + for(int iii=0; iii<4*4 ; ++iii) { + this.mat[iii] += obj.mat[iii]; + } + } + /** + * @brief Operator-= Decrement an other matrix with this one + * @param obj Reference on the external object + */ + public void decrement(Matrix4f obj) { + for(int iii=0; iii<4*4 ; ++iii) { + this.mat[iii] -= obj.mat[iii]; + } + } + /** + * @brief Operator*= Multiplication an other matrix with this one + * @param obj Reference on the external object + */ + public Matrix4f multiply(Matrix4f obj) { + // output Matrix + float[] matrixOut = new float[16]; + for(int xxx=0; xxx<4 ; xxx++) { + for(int yyy=0; yyy<4 ; yyy++) { + float value = 0; + for(int kkk=0; kkk<4 ; kkk++) { + value += this.mat[yyy*4+kkk] * obj.mat[kkk*4+xxx]; + } + matrixOut[yyy*4+xxx] = value; + } + } + // set it at the output + for(int iii=0; iii<4*4 ; iii++) { + this.mat[iii] = matrixOut[iii]; + } + return this; + } + public Matrix4f multiplyNew(Matrix4f obj) { + return this.clone().multiply(obj); + } + /** + * @brief Operator* apply matrix on a vector + * @param point Point value to apply the matrix + * @return New vector containing the value + */ + public Vector3f multiply(Vector3f point) { + return new Vector3f( this.mat[0]*point.x + this.mat[1]*point.y + this.mat[2]*point.z + this.mat[3], + this.mat[4]*point.x + this.mat[5]*point.y + this.mat[6]*point.z + this.mat[7], + this.mat[8]*point.x + this.mat[9]*point.y + this.mat[10]*point.z + this.mat[11] ); + } + /** + * @brief Transpose the current matix (usefull for OpenGL display) + */ + public Matrix4f transpose() { + float tmpVal = this.mat[1]; + this.mat[1] = this.mat[4]; + this.mat[4] = tmpVal; + + tmpVal = this.mat[2]; + this.mat[2] = this.mat[8]; + this.mat[8] = tmpVal; + + tmpVal = this.mat[6]; + this.mat[6] = this.mat[9]; + this.mat[9] = tmpVal; + + tmpVal = this.mat[3]; + this.mat[3] = this.mat[12]; + this.mat[12] = tmpVal; + + tmpVal = this.mat[7]; + this.mat[7] = this.mat[13]; + this.mat[13] = tmpVal; + + tmpVal = this.mat[11]; + this.mat[11] = this.mat[14]; + this.mat[14] = tmpVal; + return this; + } + public Matrix4f transposeNew() { + return this.clone().transpose(); + } + /** + * @brief Scale the current Matrix. + * @param vect Scale vector to apply. + */ + public Matrix4f scale(Vector3f vect) { + return scale(vect.x, vect.y, vect.z); + } + public Matrix4f scaleNew(Vector3f vect) { + return this.clone().scale(vect.x, vect.y, vect.z); + } + /** + * @brief Scale the current Matrix. + * @param sx Scale X value to apply. + * @param sy Scale Y value to apply. + * @param sz Scale Z value to apply. + */ + public Matrix4f scale(float sx, float sy, float sz) { + this.mat[0] *= sx; this.mat[1] *= sy; this.mat[2] *= sz; + this.mat[4] *= sx; this.mat[5] *= sy; this.mat[6] *= sz; + this.mat[8] *= sx; this.mat[9] *= sy; this.mat[10] *= sz; + return this; + } + public Matrix4f scaleNew(float sx, float sy, float sz) { + return this.clone().scale(sx, sy, sz); + } + /** + * @brief Scale the current Matrix in all direction with 1 value. + * @param scale Scale XYZ value to apply. + */ + public Matrix4f scale(float scale) { + return scale(scale, scale, scale); + } + public Matrix4f scaleNew(float scale) { + return this.clone().scale(scale, scale, scale); + } + /** + * @brief Makes a rotation matrix about an arbitrary axis. + * @param vect vector to apply the angle. + * @param angleRad angle to apply. + */ + public Matrix4f rotate(Vector3f vect, float angleRad) { + Matrix4f tmpMat = createMatrixRotate(vect, angleRad); + return this.multiply(tmpMat); + } + public Matrix4f rotateNew(Vector3f vect, float angleRad) { + Matrix4f tmpMat = createMatrixRotate(vect, angleRad); + return this.multiplyNew(tmpMat); + } + /** + * @brief Makes a translation of the matrix + * @param vect Translation to apply. + */ + public Matrix4f translate(Vector3f vect) { + Matrix4f tmpMat = createMatrixTranslate(vect); + return this.multiply(tmpMat); + } + public Matrix4f translateNew(Vector3f vect) { + Matrix4f tmpMat = createMatrixTranslate(vect); + return this.multiplyNew(tmpMat); + } + /** + * @brief Computes a cofactor. Used for matrix inversion. + * @param row Id of raw. + * @param col Id of colomn. + * @return the coFactorValue. + */ + public float coFactor(int row, int col) { + return ( ( this.mat[((row+1)&3)*4 + ((col+1)&3)] * this.mat[((row+2)&3)*4 + ((col+2)&3)] * this.mat[((row+3)&3)*4 + ((col+3)&3)] + + this.mat[((row+1)&3)*4 + ((col+2)&3)] * this.mat[((row+2)&3)*4 + ((col+3)&3)] * this.mat[((row+3)&3)*4 + ((col+1)&3)] + + this.mat[((row+1)&3)*4 + ((col+3)&3)] * this.mat[((row+2)&3)*4 + ((col+1)&3)] * this.mat[((row+3)&3)*4 + ((col+2)&3)] ) + - ( this.mat[((row+3)&3)*4 + ((col+1)&3)] * this.mat[((row+2)&3)*4 + ((col+2)&3)] * this.mat[((row+1)&3)*4 + ((col+3)&3)] + + this.mat[((row+3)&3)*4 + ((col+2)&3)] * this.mat[((row+2)&3)*4 + ((col+3)&3)] * this.mat[((row+1)&3)*4 + ((col+1)&3)] + + this.mat[((row+3)&3)*4 + ((col+3)&3)] * this.mat[((row+2)&3)*4 + ((col+1)&3)] * this.mat[((row+1)&3)*4 + ((col+2)&3)] ) + ) * (((row + col) & 1)== 1? -1.0f : +1.0f); +} + /** + * @brief Computes the determinant of the matrix. + * @return The determinent Value. + */ + public float determinant() { + return this.mat[0] * coFactor(0, 0) + + this.mat[1] * coFactor(0, 1) + + this.mat[2] * coFactor(0, 2) + + this.mat[3] * coFactor(0, 3); + } + + /** + * @brief Inverts the matrix. + * @note The determinant must be != 0, otherwithe the matrix can't be inverted. + * @return The inverted matrix. + */ + public Matrix4f invertNew() { + float det = determinant(); + if(Math.abs(det) < (1.0e-7f)) { + // The matrix is not invertible! Singular case! + return clone(); + } + Matrix4f temp = new Matrix4f(); + float iDet = 1.0f / det; + temp.mat[0] = coFactor(0,0) * iDet; + temp.mat[1] = coFactor(0,1) * iDet; + temp.mat[2] = coFactor(0,2) * iDet; + temp.mat[3] = coFactor(0,3) * iDet; + temp.mat[4] = coFactor(1,0) * iDet; + temp.mat[5] = coFactor(1,1) * iDet; + temp.mat[6] = coFactor(1,2) * iDet; + temp.mat[7] = coFactor(1,3) * iDet; + temp.mat[8] = coFactor(2,0) * iDet; + temp.mat[9] = coFactor(2,1) * iDet; + temp.mat[10] = coFactor(2,2) * iDet; + temp.mat[11] = coFactor(2,3) * iDet; + temp.mat[12] = coFactor(3,0) * iDet; + temp.mat[13] = coFactor(3,1) * iDet; + temp.mat[14] = coFactor(3,2) * iDet; + temp.mat[15] = coFactor(3,3) * iDet; + return temp; + } + + /** + * @brief Operator= Asign the current object with an other object + * @param obj Reference on the external object + */ + public Matrix4f clone() { + Matrix4f out = new Matrix4f(); + for(int iii=0; iii<16 ; iii++) { + out.mat[iii] = this.mat[iii]; + } + return out; + } + + /** + * @brief Create projection matrix with the box parameter (camera view in -z axis) + * @param xmin X minimum size of the frustum + * @param xmax X maximum size of the frustum + * @param ymin Y minimum size of the frustum + * @param ymax Y maximum size of the frustum + * @param zNear Z minimum size of the frustum + * @param zFar Z maximum size of the frustum + * @return New matrix of the transformation requested + */ + public static Matrix4f createMatrixFrustum(float xmin, float xmax, float ymin, float ymax, float zNear, float zFar) { + Matrix4f tmp = new Matrix4f(); + for(int iii=0; iii<4*4 ; iii++) { + tmp.mat[iii] = 0; + } + // 0 1 2 3 + // 4 5 6 7 + // 8 9 10 11 + // 12 13 14 15 + tmp.mat[0] = (2.0f * zNear) / (xmax - xmin); + tmp.mat[5] = (2.0f * zNear) / (ymax - ymin); + tmp.mat[10] = -(zFar + zNear) / (zFar - zNear); + tmp.mat[2] = (xmax + xmin) / (xmax - xmin); + tmp.mat[6] = (ymax + ymin) / (ymax - ymin); + tmp.mat[14] = -1.0f; + tmp.mat[11] = -(2.0f * zFar * zNear) / (zFar - zNear); + return tmp; + } + + /** + * @brief Create projection matrix with human repensentation view (camera view in -z axis) + * @param foxy Focal in radian of the camera + * @param aspect aspect ratio of the camera + * @param zNear Z near size of the camera + * @param zFar Z far size of the camera + * @return New matrix of the transformation requested + */ + public static Matrix4f createMatrixPerspective(float foxy, float aspect, float zNear, float zFar) { + //TKDEBUG("drax perspective: foxy=" << foxy << "->" << aspect << " " << zNear << "->" << zFar); + float xmax = zNear * (float)Math.tan(foxy/2.0); + float xmin = -xmax; + + float ymin = xmin / aspect; + float ymax = xmax / aspect; + //TKDEBUG("drax perspective: " << xmin << "->" << xmax << " & " << ymin << "->" << ymax << " " << zNear << "->" << zFar); + return createMatrixFrustum(xmin, xmax, ymin, ymax, zNear, zFar); + } + /** + * @brief Create orthogonal projection matrix with the box parameter (camera view in -z axis) + * @param left left size of the camera + * @param right Right size of the camera + * @param bottom Buttom size of the camera + * @param top Top Size of the camera + * @param nearVal Z near size of the camera + * @param farVal Z far size of the camera + * @return New matrix of the transformation requested + */ + public static Matrix4f createMatrixOrtho(float left, float right, float bottom, float top, float nearVal, float farVal) { + Matrix4f tmp = new Matrix4f(); + for(int iii=0; iii<4*4 ; iii++) { + tmp.mat[iii] = 0; + } + tmp.mat[0] = 2.0f / (right - left); + tmp.mat[5] = 2.0f / (top - bottom); + tmp.mat[10] = -2.0f / (farVal - nearVal); + tmp.mat[3] = -1*(right + left) / (right - left); + tmp.mat[7] = -1*(top + bottom) / (top - bottom); + tmp.mat[11] = -1*(farVal + nearVal) / (farVal - nearVal); + tmp.mat[15] = 1; + return tmp; + } + /** + * @brief Create a matrix 3D with a simple translation + * @param translate 3 dimention translation + * @return New matrix of the transformation requested + */ + public static Matrix4f createMatrixTranslate(Vector3f translate) { + Matrix4f tmp = new Matrix4f(); + // set translation : + tmp.mat[3] = translate.x; + tmp.mat[7] = translate.y; + tmp.mat[11] = translate.z; + return tmp; + } + /** + * @brief Create a matrix 3D with a simple scale + * @param scale 3 dimention scale + * @return New matrix of the transformation requested + */ + public static Matrix4f createMatrixScale(Vector3f scale){ + Matrix4f tmp = new Matrix4f(); + tmp.scale(scale); + return tmp; + } + + /** + * @brief Create a matrix 3D with a simple rotation + * @param normal vector aroud witch apply the rotation + * @param angleRad Radian angle to set at the matrix + * @return New matrix of the transformation requested + */ + public static Matrix4f createMatrixRotate(Vector3f normal, float angleRad) { + Matrix4f tmp = new Matrix4f(); + float cosVal = (float)Math.cos(angleRad); + float sinVal = (float)Math.sin(angleRad); + float invVal = 1.0f-cosVal; + // set rotation : + tmp.mat[0] = normal.x * normal.x * invVal + cosVal; + tmp.mat[1] = normal.x * normal.y * invVal - normal.z * sinVal; + tmp.mat[2] = normal.x * normal.z * invVal + normal.y * sinVal; + + tmp.mat[4] = normal.y * normal.x * invVal + normal.z * sinVal; + tmp.mat[5] = normal.y * normal.y * invVal + cosVal; + tmp.mat[6] = normal.y * normal.z * invVal - normal.x * sinVal; + + tmp.mat[8] = normal.z * normal.x * invVal - normal.y * sinVal; + tmp.mat[9] = normal.z * normal.y * invVal + normal.x * sinVal; + tmp.mat[10] = normal.z * normal.z * invVal + cosVal; + return tmp; + } + //! @notindoc + public static Matrix4f createMatrixRotate2(Vector3f vect) { + return createMatrixLookAt(vect, new Vector3f(0,0,0), new Vector3f(0,1,0)); + } + + /** + * @brief Create projection matrix with camera property (camera view in -z axis) + * @param eye Optical center of the camera + * @param target Point of where the camera is showing + * @param up Up vector of the camera + * @return New matrix of the transformation requested + */ + public static Matrix4f createMatrixLookAt(Vector3f eye, + Vector3f target, + Vector3f up) { + Matrix4f tmp = new Matrix4f(); + + Vector3f forward = eye; + forward.less(target); + forward.safeNormalize(); + Vector3f xaxis = target.cross(up.normalizeNew()); + xaxis.safeNormalize(); + Vector3f up2 = xaxis.cross(forward); + xaxis.safeNormalize(); + + tmp.mat[0] = xaxis.x; + tmp.mat[1] = up2.x; + tmp.mat[2] = forward.x; + tmp.mat[3] = eye.x; + + tmp.mat[4] = xaxis.y; + tmp.mat[5] = up2.y; + tmp.mat[6] = forward.y; + tmp.mat[7] = eye.y; + + tmp.mat[8] = xaxis.z; + tmp.mat[9] = up2.z; + tmp.mat[10] = forward.z; + tmp.mat[11] = eye.z; + + tmp.mat[12] = 0.0f; + tmp.mat[13] = 0.0f; + tmp.mat[14] = 0.0f; + tmp.mat[15] = 1.0f; + return tmp; + } + + public float[] getTable() { + return this.mat; + } + +} diff --git a/src/org/atriasoft/etk/math/Quaternion.java b/src/org/atriasoft/etk/math/Quaternion.java new file mode 100644 index 0000000..76a8328 --- /dev/null +++ b/src/org/atriasoft/etk/math/Quaternion.java @@ -0,0 +1,593 @@ +package org.atriasoft.etk.math; + +public class Quaternion { + public float x; + public float y; + public float z; + public float w; + + /** @brief No initialization constructor (faster ...) */ + public Quaternion() { + this.x = 0.0f; + this.y = 0.0f; + this.z = 0.0f; + this.w = 0.0f; + } + + /* void checkValues() { if ( isinf(this.x) == true || isnan(this.x) == true || isinf(this.y) == true || isnan(this.y) == true || isinf(this.z) == true || isnan(this.z) == true || isinf(this.w) == + * true || isnan(this.w) == true) { TKCRITICAL(" set transform: (" << this.x << "," << this.y << "," << this.z << "," << this.w << ")"); } } */ + /** + * @brief Constructor from scalars. + * @param xxx X value + * @param yyy Y value + * @param zzz Z value + * @param www W value */ + public Quaternion(float xxx, float yyy, float zzz, float www) { + this.x = xxx; + this.y = yyy; + this.z = zzz; + this.w = www; + } + + /** + * @brief Constructor with the component w and a vector 3D. + * @param www W value + * @param vec 3D vector value + */ + public Quaternion(float www, Vector3f vec) { + this.x = vec.x; + this.y = vec.y; + this.z = vec.z; + this.w = www; + } + + /** @brief Constructor with Euler angles (in radians) to a quaternion + * @param eulerAngles list of all euleu angle */ + public Quaternion(Vector3f eulerAngles) { + setEulerAngles(eulerAngles); + } + + /** @brief Create a unit quaternion from a rotation matrix + * @param matrix generic matrix */ + public Quaternion(Matrix3f matrix) { + float trace = matrix.getTrace(); + if (trace < 0.0f) { + if (matrix.mat[4] > matrix.mat[0]) { + if (matrix.mat[8] > matrix.mat[4]) { + float rrr = (float) Math.sqrt(matrix.mat[8] - matrix.mat[0] - matrix.mat[4] + 1.0f); + float sss = 0.5f / rrr; + this.x = (matrix.mat[6] + matrix.mat[2]) * sss; + this.y = (matrix.mat[5] + matrix.mat[7]) * sss; + this.z = 0.5f * rrr; + this.w = (matrix.mat[3] - matrix.mat[1]) * sss; + } else { + float rrr = (float) Math.sqrt(matrix.mat[4] - matrix.mat[8] - matrix.mat[0] + 1.0f); + float sss = 0.5f / rrr; + this.x = (matrix.mat[1] + matrix.mat[3]) * sss; + this.y = 0.5f * rrr; + this.z = (matrix.mat[5] + matrix.mat[7]) * sss; + this.w = (matrix.mat[2] - matrix.mat[6]) * sss; + } + } else if (matrix.mat[8] > matrix.mat[0]) { + float rrr = (float) Math.sqrt(matrix.mat[8] - matrix.mat[0] - matrix.mat[4] + 1.0f); + float sss = 0.5f / rrr; + this.x = (matrix.mat[6] + matrix.mat[2]) * sss; + this.y = (matrix.mat[5] + matrix.mat[7]) * sss; + this.z = 0.5f * rrr; + this.w = (matrix.mat[3] - matrix.mat[1]) * sss; + } else { + float rrr = (float) Math.sqrt(matrix.mat[0] - matrix.mat[4] - matrix.mat[8] + 1.0f); + float sss = 0.5f / rrr; + this.x = 0.5f * rrr; + this.y = (matrix.mat[1] + matrix.mat[3]) * sss; + this.z = (matrix.mat[6] - matrix.mat[2]) * sss; + this.w = (matrix.mat[7] - matrix.mat[5]) * sss; + } + } else { + float rrr = (float) Math.sqrt(trace + 1.0f); + float sss = 0.5f / rrr; + this.x = (matrix.mat[7] - matrix.mat[5]) * sss; + this.y = (matrix.mat[2] - matrix.mat[6]) * sss; + this.z = (matrix.mat[3] - matrix.mat[1]) * sss; + this.w = 0.5f * rrr; + } + } + + public Quaternion(Quaternion obj) { + this.x = obj.x; + this.y = obj.y; + this.z = obj.z; + this.w = obj.w; + } + + /** @brief Add a vector to this one. + * @param obj The vector to add to this one + * @return Local reference of the vector */ + public Quaternion add(Quaternion obj) { + this.x += obj.x; + this.y += obj.y; + this.z += obj.z; + this.w += obj.w; + return this; + } + + /** @brief Add a vector to this one. + * @param obj The vector to add to this one + * @return New vector containing the value */ + public Quaternion addNew(Quaternion obj) { + return new Quaternion(this.x + obj.x, this.y + obj.y, this.z + obj.z, this.w + obj.w); + } + + /** @brief Subtract a vector from this one + * @param obj The vector to subtract + * @return Local reference of the vector */ + public Quaternion less(Quaternion obj) { + this.x -= obj.x; + this.y -= obj.y; + this.z -= obj.z; + this.w -= obj.w; + return this; + } + + /** @brief Subtract a vector from this one + * @param obj The vector to subtract + * @return New quaternion containing the value */ + public Quaternion lessNew(Quaternion obj) { + return new Quaternion(this.x - obj.x, this.y - obj.y, this.z - obj.z, this.w - obj.w); + } + + /** @brief Scale the quaternion + * @param val Scale factor + * @return Local reference of the quaternion */ + public Quaternion multiply(float val) { + this.x *= val; + this.y *= val; + this.z *= val; + this.w *= val; + return this; + } + + /** @brief Scale the quaternion + * @param val Scale factor + * @return New quaternion containing the value */ + public Quaternion multiplyNew(float val) { + return new Quaternion(this.x * val, this.y * val, this.z * val, this.w * val); + } + + /** @brief Inversely scale the quaternion + * @param val Scale factor to divide by. + * @return Local reference of the quaternion */ + public Quaternion devide(float val) { + if (val != 0) { + this.x /= val; + this.y /= val; + this.z /= val; + this.w /= val; + } + return this; + } + + /** @brief Inversely scale the quaternion + * @param val Scale factor to divide by. + * @return New quaternion containing the value */ + public Quaternion devideNew(float val) { + if (val != 0) { + return new Quaternion(this.x / val, this.y / val, this.z / val, this.w / val); + } + return new Quaternion(this); + } + + /** @brief Return the dot product + * @param obj The other quaternion in the dot product + * @return Dot result value */ + public float dot(Quaternion obj) { + return this.x * obj.x + this.y * obj.y + this.z * obj.z + this.w * obj.w; + } + + /** @brief Return the squared length of the quaternion. + * @return Squared length value. */ + public float length2() { + return dot(this); + } + + /** @brief Return the length of the quaternion + * @return Length value */ + public float length() { + return (float) Math.sqrt(length2()); + } + + /** @brief Normalize this quaternion x^2 + y^2 + z^2 + w^2 = 1 + * @return Local reference of the quaternion normalized */ + public Quaternion normalize() { + float invLength = 1.0f / length(); + this.x *= invLength; + this.y *= invLength; + this.z *= invLength; + this.w *= invLength; + return this; + } + + /** @brief Return a normalized version of this quaternion + * @return New quaternion containing the value */ + public Quaternion normalizeNew() { + Quaternion tmp = new Quaternion(this); + tmp.normalize(); + return tmp; + } + + /** @brief Normalize this quaternion x^2 + y^2 + z^2 + w^2 = 1 + * @return Local reference of the quaternion normalized */ + public Quaternion safeNormalize() { + float lengthTmp = length(); + if (lengthTmp == 0.0f) { + this.x = 0.0f; + this.y = 0.0f; + this.z = 0.0f; + this.w = 1.0f; + } + float invLength = 1.0f / lengthTmp; + this.x *= invLength; + this.y *= invLength; + this.z *= invLength; + this.w *= invLength; + return this; + } + + /** @brief Return a normalized version of this quaternion + * @return New quaternion containing the value */ + public Quaternion safeNormalizeNew() { + Quaternion tmp = new Quaternion(this); + tmp.safeNormalize(); + return tmp; + } + + /** @brief Set the absolute values of each element */ + public Quaternion absolute() { + this.x = Math.abs(this.x); + this.y = Math.abs(this.y); + this.z = Math.abs(this.z); + this.w = Math.abs(this.w); + return this; + } + + /** @brief Return a quaternion will the absolute values of each element + * @return New quaternion with the absolute value */ + public Quaternion absoluteNew() { + return new Quaternion(Math.abs(this.x), Math.abs(this.y), Math.abs(this.z), Math.abs(this.w)); + } + + /** @brief Get X value + * @return the x value */ + public float getX() { + return this.x; + } + + /** @brief Get Y value + * @return the y value */ + public float getY() { + return this.y; + } + + /** @brief Get Z value + * @return the z value */ + public float getZ() { + return this.z; + } + + /** @brief Get W value + * @return the w value */ + public float getW() { + return this.w; + } + + /** @brief Set the x value + * @param x New value */ + public Quaternion setX(float x) { + this.x = x; + return this; + }; + + /** @brief Set the y value + * @param y New value */ + public Quaternion setY(float y) { + this.y = y; + return this; + }; + + /** @brief Set the z value + * @param z New value */ + public Quaternion setZ(float z) { + this.z = z; + return this; + }; + + /** @brief Set the w value + * @param w New value */ + public Quaternion setW(float w) { + this.w = w; + return this; + } + + /** @brief Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are identical + * @return false The Objects are NOT identical */ + public boolean isEqual(Quaternion obj) { + return ((this.w == obj.w) && (this.z == obj.z) && (this.y == obj.y) && (this.x == obj.x)); + } + + /** @brief In-Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are NOT identical + * @return false The Objects are identical */ + public boolean isDifferent(Quaternion obj) { + return ((this.w != obj.w) || (this.z != obj.z) || (this.y != obj.y) || (this.x != obj.x)); + } + + /** @brief Multiply this quaternion by the other. + * @param obj The other quaternion + * @return Local reference of the quaternion */ + public Quaternion multiply(Quaternion obj) { + Vector3f base = getVectorV(); + Vector3f crossValue = base.cross(obj.getVectorV()); + this.x = this.w * obj.x + obj.w * this.x + crossValue.x; + this.y = this.w * obj.y + obj.w * this.y + crossValue.y; + this.z = this.w * obj.z + obj.w * this.z + crossValue.z; + this.w = this.w * obj.w - base.dot(obj.getVectorV()); + safeNormalize(); + return this; + } + + /** @brief Multiply this quaternion by the other. + * @param obj The other quaternion + * @return New quaternion containing the value */ + public Quaternion multiplyNew(Quaternion obj) { + Quaternion tmp = new Quaternion(this); + tmp.multiply(obj); + return tmp; + } + + /** @brief Operator* with a vector. This methods rotates a point given the rotation of a quaternion + * @param point Point to move + * @return Point with the updated position */ + public Vector3f multiply(Vector3f point) { + Vector3f qvec = getVectorV(); + Vector3f uv = qvec.cross(point); + Vector3f uuv = qvec.cross(uv); + uv.multiply(2.0f * this.w); + uuv.multiply(2.0f); + return uv.add(point).add(uuv); + } + + public Vector3f multiply(float xxx, float yyy, float zzz) { + Vector3f point = new Vector3f(xxx, yyy, zzz); + Vector3f qvec = getVectorV(); + Vector3f uv = qvec.cross(point); + Vector3f uuv = qvec.cross(uv); + uv.multiply(2.0f * this.w); + uuv.multiply(2.0f); + return uv.add(point).add(uuv); + } + + /** @brief Set each element to the max of the current values and the values of another Vector + * @param obj The other Vector to compare with */ + public void setMax(Quaternion obj) { + this.x = Math.max(this.x, obj.x); + this.y = Math.max(this.y, obj.y); + this.z = Math.max(this.z, obj.z); + this.w = Math.max(this.w, obj.w); + } + + /** @brief Set each element to the min of the current values and the values of another Vector + * @param obj The other Vector to compare with */ + public void setMin(Quaternion obj) { + this.x = Math.min(this.x, obj.x); + this.y = Math.min(this.y, obj.y); + this.z = Math.min(this.z, obj.z); + this.w = Math.min(this.w, obj.w); + } + + /** @brief Set Value on the quaternion + * @param xxx X value. + * @param yyy Y value. + * @param zzz Z value. + * @param www W value. */ + public void setValue(float xxx, float yyy, float zzz, float www) { + this.x = xxx; + this.y = yyy; + this.z = zzz; + this.w = www; + } + + /** @brief Set 0 value on all the quaternion */ + public void setZero() { + setValue(0, 0, 0, 0); + } + + /** @brief get a 0 value on all a quaternion + * @return a (float)Math. quaternion */ + public static Quaternion zero() { + return new Quaternion(0, 0, 0, 0); + } + + /** @brief Check if the quaternion is equal to (0,0,0,0) + * @return true The value is equal to (0,0,0,0) + * @return false The value is NOT equal to (0,0,0,0) */ + public boolean isZero() { + return this.x == 0 && this.y == 0 && this.z == 0 && this.w == 0; + } + + /** @brief Set identity value at the quaternion */ + public void setIdentity() { + setValue(0, 0, 0, 1); + } + + /** @brief get an identity quaternion + * @return an identity quaternion */ + public static Quaternion identity() { + return new Quaternion(0, 0, 0, 1); + } + + /** @brief get x, y, z in a Vector3f */ + public Vector3f getVectorV() { + return new Vector3f(this.x, this.y, this.z); + } + + /** @brief Inverse the quaternion */ + public void inverse() { + float invLengthSquare = 1.0f / length2(); + this.x *= -invLengthSquare; + this.y *= -invLengthSquare; + this.z *= -invLengthSquare; + this.w *= invLengthSquare; + } + + /** @brief Return the inverse of the quaternion + * @return inverted quaternion */ + public Quaternion inverseNew() { + Quaternion tmp = new Quaternion(this); + tmp.inverse(); + return tmp; + } + + /** @brief Return the unit quaternion + * @return Quaternion unitarised */ + public Quaternion getUnit() { + return normalizeNew(); + } + + /** @brief Conjugate the quaternion */ + public void conjugate() { + this.x *= -1.0f; + this.y *= -1.0f; + this.z *= -1.0f; + } + + /** @brief Return the conjugate of the quaternion + * @return Conjugate quaternion */ + public Quaternion conjugateNew() { + Quaternion tmp = new Quaternion(this); + tmp.conjugate(); + return tmp; + } + + /** @brief Compute the rotation angle (in radians) and the rotation axis + * @param angle Angle of the quaternion + * @param axis Axis of the quaternion */ + public void getAngleAxis(float angle, Vector3f axis) { + Quaternion quaternion = getUnit(); + angle = (float) Math.acos(quaternion.w) * 2.0f; + Vector3f rotationAxis = new Vector3f(quaternion.x, quaternion.y, quaternion.z); + rotationAxis = rotationAxis.normalizeNew(); + axis.setValue(rotationAxis.x, rotationAxis.y, rotationAxis.z); + } + + /** @brief Get the orientation matrix corresponding to this quaternion + * @return the 3x3 transformation matrix */ + public Matrix3f getMatrix() { + float nQ = this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w; + float sss = 0.0f; + if (nQ > 0.0f) { + sss = 2.0f / nQ; + } + float xs = this.x * sss; + float ys = this.y * sss; + float zs = this.z * sss; + float wxs = this.w * xs; + float wys = this.w * ys; + float wzs = this.w * zs; + float xxs = this.x * xs; + float xys = this.x * ys; + float xzs = this.x * zs; + float yys = this.y * ys; + float yzs = this.y * zs; + float zzs = this.z * zs; + return new Matrix3f(1.0f - yys - zzs, xys - wzs, xzs + wys, xys + wzs, 1.0f - xxs - zzs, yzs - wxs, xzs - wys, yzs + wxs, 1.0f - xxs - yys); + } + + public Matrix4f getMatrix4() { + + float nQ = this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w; + float sss = 0.0f; + if (nQ > 0.0f) { + sss = 2.0f / nQ; + } + float xs = this.x * sss; + float ys = this.y * sss; + float zs = this.z * sss; + float wxs = this.w * xs; + float wys = this.w * ys; + float wzs = this.w * zs; + float xxs = this.x * xs; + float xys = this.x * ys; + float xzs = this.x * zs; + float yys = this.y * ys; + float yzs = this.y * zs; + float zzs = this.z * zs; + return new Matrix4f(1.0f - yys - zzs, xys - wzs, xzs + wys, 0, xys + wzs, 1.0f - xxs - zzs, yzs - wxs, 0, xzs - wys, yzs + wxs, 1.0f - xxs - yys, 0, 0, 0, 0, 1); + } + + /** @brief Compute the spherical linear interpolation between two quaternions. + * @param obj1 First quaternion + * @param obj2 Second quaternion + * @param ttt linar coefficient interpolation to be such that [0..1] */ + public static Quaternion slerp(Quaternion obj1, Quaternion obj2, float ttt) { + // TKASSERT(ttt >= 0.0f ttt <= 1.0f, "wrong intermolation"); + float invert = 1.0f; + float cosineTheta = obj1.dot(obj2); + if (cosineTheta < 0.0f) { + cosineTheta = -cosineTheta; + invert = -1.0f; + } + if (1 - cosineTheta < 0.00001f) { + return obj1.multiplyNew(1.0f - ttt).add(obj2.multiplyNew(ttt * invert)); + } + float theta = (float) Math.acos(cosineTheta); + float sineTheta = (float) Math.sin(theta); + float coeff1 = (float) Math.sin((1.0f - ttt) * theta) / sineTheta; + float coeff2 = (float) Math.sin(ttt * theta) / sineTheta * invert; + return obj1.multiplyNew(coeff1).add(obj2.multiplyNew(coeff2)); + } + + /** @brief Configure the quaternion with euler angles. + * @param angles Eular angle of the quaternion. */ + public void setEulerAngles(Vector3f angles) { + float angle = angles.x * 0.5f; + float sinX = (float) Math.sin(angle); + float cosX = (float) Math.cos(angle); + angle = angles.y * 0.5f; + float sinY = (float) Math.sin(angle); + float cosY = (float) Math.cos(angle); + angle = angles.z * 0.5f; + float sinZ = (float) Math.sin(angle); + float cosZ = (float) Math.cos(angle); + float cosYcosZ = cosY * cosZ; + float sinYcosZ = sinY * cosZ; + float cosYsinZ = cosY * sinZ; + float sinYsinZ = sinY * sinZ; + this.x = sinX * cosYcosZ - cosX * sinYsinZ; + this.y = cosX * sinYcosZ + sinX * cosYsinZ; + this.z = cosX * cosYsinZ - sinX * sinYcosZ; + this.w = cosX * cosYcosZ + sinX * sinYsinZ; + normalize(); + } + + /** @brief Clone the current Quaternion. + * @return New Quaternion containing the value */ + @Override + public Quaternion clone() { + return new Quaternion(this); + } + + @Override + public String toString() { + return "Quaternion(" + this.x + "," + this.y + "," + this.z + "," + this.w + ")"; + } + + // a * diff = b + public static Quaternion diff(Quaternion a, Quaternion b) { + // Log.info("diff " + a + " " + b); + Quaternion inv = a.inverseNew(); + return inv.multiply(b); + } +} diff --git a/src/org/atriasoft/etk/math/Transform3D.java b/src/org/atriasoft/etk/math/Transform3D.java new file mode 100644 index 0000000..1983bce --- /dev/null +++ b/src/org/atriasoft/etk/math/Transform3D.java @@ -0,0 +1,149 @@ +package org.atriasoft.etk.math; + +public class Transform3D { + protected Vector3f position; //! Position + public Vector3f getPosition() { + return position; + } + public void setPosition(Vector3f position) { + this.position = position; + } + public Quaternion getOrientation() { + return orientation; + } + public void setOrientation(Quaternion orientation) { + this.orientation = orientation; + } + protected Quaternion orientation; //!< Orientation + public Transform3D() { + this.position = Vector3f.zero(); + this.orientation = Quaternion.identity(); + } + public Transform3D(Vector3f position) { + this.position = position.clone(); + this.orientation = Quaternion.identity(); + } + public Transform3D(Vector3f position, Matrix3f orientation) { + this.position = position.clone(); + this.orientation = new Quaternion(orientation); + } + public Transform3D(Vector3f position, Quaternion orientation) { + this.position = position.clone(); + this.orientation = orientation.clone(); + } + public Transform3D(Transform3D transform3d) { + this.position = transform3d.position.clone(); + this.orientation = transform3d.orientation.clone(); + } + /** + * @brief Get the identity of the transformation + */ + public static Transform3D identity() { + return new Transform3D(Vector3f.zero(), Quaternion.identity()); + } + /// Set the Transform3D to the identity transform + public void setIdentity() { + this.position = Vector3f.zero(); + this.orientation = Quaternion.identity(); + } + /// Set the transform from an OpenGL transform matrix + public void setFromOpenGL(float[] matrix) { + Matrix3f tmpMatrix = new Matrix3f(matrix[0], matrix[4], matrix[8], + matrix[1], matrix[5], matrix[9], + matrix[2], matrix[6], matrix[10]); + this.orientation = new Quaternion(tmpMatrix); + this.position.setValue(matrix[12], matrix[13], matrix[14]); + } + /// Get the OpenGL matrix of the transform + public Matrix4f getOpenGLMatrix() { + Matrix4f out = new Matrix4f(); + Matrix3f tmpMatrix = this.orientation.getMatrix(); + // version transposer... +// out.mat[0] = tmpMatrix.mat[0]; +// out.mat[1] = tmpMatrix.mat[3]; +// out.mat[2] = tmpMatrix.mat[6]; +// out.mat[3] = 0.0f; +// out.mat[4] = tmpMatrix.mat[1]; +// out.mat[5] = tmpMatrix.mat[4]; +// out.mat[6] = tmpMatrix.mat[7]; +// out.mat[7] = 0.0f; +// out.mat[8] = tmpMatrix.mat[2]; +// out.mat[9] = tmpMatrix.mat[5]; +// out.mat[10] = tmpMatrix.mat[8]; +// out.mat[11] = 0.0f; +// out.mat[12] = this.position.x; +// out.mat[13] = this.position.y; +// out.mat[14] = this.position.z; +// out.mat[15] = 1.0f; + out.mat[0] = tmpMatrix.mat[0]; + out.mat[1] = tmpMatrix.mat[1]; + out.mat[2] = tmpMatrix.mat[2]; + out.mat[3] = this.position.x; + out.mat[4] = tmpMatrix.mat[3]; + out.mat[5] = tmpMatrix.mat[4]; + out.mat[6] = tmpMatrix.mat[5]; + out.mat[7] = this.position.y; + out.mat[8] = tmpMatrix.mat[6]; + out.mat[9] = tmpMatrix.mat[7]; + out.mat[10] = tmpMatrix.mat[8]; + out.mat[11] = this.position.z; + out.mat[12] = 0.0f; + out.mat[13] = 0.0f; + out.mat[14] = 0.0f; + out.mat[15] = 1.0f; + return out; + } + /// Return the inverse of the transform + public Transform3D inverseNew() { + Quaternion invQuaternion = this.orientation.inverseNew(); + Matrix3f invMatrix = invQuaternion.getMatrix(); + return new Transform3D(invMatrix.multiply(this.position.multiplyNew(-1)), invQuaternion); + } + /// Return an interpolated transform + public Transform3D interpolateTransforms(Transform3D old, + Transform3D newOne, + float interpolationFactor) { + Vector3f interPosition = old.position.multiplyNew(1.0f - interpolationFactor) + .add(newOne.position.multiplyNew(interpolationFactor)); + Quaternion interOrientation = Quaternion.slerp(old.orientation, + newOne.orientation, interpolationFactor); + return new Transform3D(interPosition, interOrientation); + } + /// Return the transformed vector + public Vector3f multiply(Vector3f vector) { + return this.orientation.getMatrix().multiply(vector).add(this.position); + } + /// Operator of multiplication of a transform with another one + public Transform3D multiplyNew(Transform3D transform2) { + return new Transform3D(this.orientation.getMatrix().multiply(transform2.position).add(this.position), + this.orientation.multiplyNew(transform2.orientation)); + } + /// Return true if the two transforms are equal + public boolean isEqual(Transform3D transform2) { + return this.position.isEqual(transform2.position) && this.orientation.isEqual(transform2.orientation); + } + /// Return true if the two transforms are different + public boolean isDifferent(Transform3D transform2) { + return this.position.isDifferent(transform2.position) || this.orientation.isDifferent(transform2.orientation); + } + /// Assignment operator + public Transform3D set(Transform3D transform) { + this.position = transform.position.clone(); + this.orientation = transform.orientation.clone(); + return this; + } + /** + * @brief Clone the current Transform3D. + * @return New Transform3D containing the value + */ + public Transform3D clone() { + return new Transform3D(this); + } + @Override + public String toString() { + return "Transform3D(" + this.position + " & " + this.orientation + ")"; + } + public void applyRotation(Quaternion rotation) { + this.orientation = this.orientation.multiply(rotation); + } +} diff --git a/src/org/atriasoft/etk/math/Vector2f.java b/src/org/atriasoft/etk/math/Vector2f.java new file mode 100644 index 0000000..bd8d3fb --- /dev/null +++ b/src/org/atriasoft/etk/math/Vector2f.java @@ -0,0 +1,365 @@ +package org.atriasoft.etk.math; + +public class Vector2f { + public float x = 0; + public float y = 0; + /* **************************************************** + * Constructor + *****************************************************/ + public Vector2f() { + this.x = 0; + this.y = 0; + } + public static Vector2f zero() { + return new Vector2f(0,0); + } + /** + * @brief Constructor from scalars + * @param xxx X value + * @param yyy Y value + */ + public Vector2f(float xxx, float yyy) { + this.x = xxx; + this.y = yyy; + } + /** + * @brief Constructor with external vector + * @param obj The vector to add to this one + */ + public Vector2f(Vector2f obj) { + this.x = obj.x; + this.y = obj.y; + } + /** + * @brief Operator= Asign the current object with an other object + * @param obj Reference on the external object + */ + public void set(Vector2f obj) { + this.x = obj.x; + this.y = obj.y; + } + /** + * @brief Operator= Asign the current object with a value + * @param val Value to assign on the object + */ + public void set(float val) { + this.x = val; + this.y = val; + } + /** + * @brief Operator= Asign the current object with a value + * @param xxx X value + * @param yyy Y value + */ + public void set(float xxx, float yyy) { + this.x = xxx; + this.y = yyy; + } + /** + * @brief Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are identical + * @return false The Objects are NOT identical + */ + public boolean isEqual(Vector2f obj) { + return ( obj.x == this.x + && obj.y == this.y); + } + /** + * @brief In-Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are NOT identical + * @return false The Objects are identical + */ + public boolean isDifferent(Vector2f obj) { + return ( obj.x != this.x + || obj.y != this.y); + } + public boolean isLowerOrEqual(Vector2f obj) { + return ( this.x <= obj.x + && this.y <= obj.y); + } + public boolean isLower(Vector2f obj) { + return ( this.x < obj.x + && this.y < obj.y); + } + public boolean isGreaterOrEqual(Vector2f obj) { + return ( this.x >= obj.x + && this.y >= obj.y); + } + public boolean isGreater(Vector2f obj) { + return ( this.x > obj.x + && this.y > obj.y); + } + /** + * @brief Operator+= Addition an other vertor with this one + * @param obj Reference on the external object + */ + void add(Vector2f obj) { + this.x += obj.x; + this.y += obj.y; + } + /** + * @brief Operator+= Addition an other vertor with this one + * @param val Value to addition at x/y + */ + public void add(float val) { + this.x += val; + this.y += val; + } + /** + * @brief Operator-= Decrement an other vertor with this one + * @param obj Reference on the external object + */ + public void less(Vector2f obj) { + this.x -= obj.x; + this.y -= obj.y; + } + /** + * @brief Operator-= Decrement an other vertor with this one + * @param val Value to addition at x/y + */ + public void less(float val) { + this.x -= val; + this.y -= val; + } + /** + * @brief Operator*= Multiplication an other vertor with this one + * @param obj Reference on the external object + */ + public void multiply(Vector2f obj) { + this.x *= obj.x; + this.y *= obj.y; + } + /** + * @brief Operator*= Multiplication an other vertor with this one + * @param val Value to addition at x/y + */ + public void multiply(float val) { + this.x *= val; + this.y *= val; + } + /** + * @brief Operator/ Dividing an other vertor with this one + * @param obj Reference on the external object + */ + public void devide(Vector2f obj) { + this.x /= obj.x; + this.y /= obj.y; + } + /** + * @brief Operator/ Dividing an other vertor with this one + * @param val Value to addition at x/y + */ + public void devide(float val) { + this.x /= val; + this.y /= val; + } + /** + * @brief Incrementation of this vector (+1 of 2 elements) + */ + public void increment() { + this.x++; + this.y++; + } + /** + * @brief Decrementation of this vector (-1 of 2 elements) + */ + public void decrement() { + this.x--; + this.y--; + } + /** + * @brief Return the cross product / determinant + * @param obj The other vector in the cross product + * @return cross product value + */ + public float cross(Vector2f obj) { + return this.x * obj.y + - this.y * obj.x; + } + /** + * @brief Return the dot product + * @param obj The other vector in the dot product + * @return Dot product value + */ + public float dot(Vector2f obj) { + return this.x * obj.x + + this.y * obj.y; + } + /** + * @brief Get the length of the vector squared + * @return Squared length value. + */ + public float length2() { + return dot(this); + } + /** + * @brief Get the length of the vector + * @return Length value + */ + public float length() { + return (float) Math.sqrt(length2()); + } + /** + * @brief Return the distance squared between the ends of this and another vector + * This is symantically treating the vector like a point + * @param obj The other vector to compare distance + * @return the square distance of the 2 points + */ + public float distance2(Vector2f obj) { + float deltaX = obj.x - this.x; + float deltaY = obj.y - this.y; + return deltaX*deltaX + deltaY*deltaY; + } + /** + * @brief Return the distance between the ends of this and another vector + * This is symantically treating the vector like a point + * @param obj The other vector to compare distance + * @return the distance of the 2 points + */ + public float distance(Vector2f obj) { + return (float)Math.sqrt(this.distance2(obj)); + } + /** + * @brief Normalize this vector x^2 + y^2 = 1 + */ + public void normalize() { + this.devide(length()); + } + /** + * @brief Normalize this vector x^2 + y^2 = 1 (check if not deviding by 0, if it is the case ==> return (1,0)) + * @return Local reference of the vector normalized + */ + public void safeNormalize() { + float tmp = length(); + if (tmp != 0) { + this.devide(length()); + return; + } + setValue(1,0); + return; + } + /** + * @brief Return a normalized version of this vector + * @return New vector containing the value + */ + public Vector2f normalized() { + Vector2f tmp = this.clone(); + tmp.normalize(); + return tmp; + } + /** + * @brief Return a vector will the absolute values of each element + * @return New vector containing the value + */ + public Vector2f absolute() { + return new Vector2f( Math.abs(this.x), + Math.abs(this.y)); + } + /** + * @brief Return the axis with the smallest value + * @return values are 0,1 for x or y + */ + public int minAxis() { + return this.x < this.y ? 0 : 1; + } + /** + * @brief Return the axis with the largest value + * @return values are 0,1 for x or y + */ + public int maxAxis() { + return this.x < this.y ? 1 : 0; + } + /** + * @brief Return the axis with the smallest ABSOLUTE value + * @return values 0,1 for x, or z + */ + public int furthestAxis() { + return absolute().minAxis(); + } + /** + * @brief Return the axis with the largest ABSOLUTE value + * @return values 0,1 for x or y + */ + public int closestAxis() { + return absolute().maxAxis(); + } + /** + * @brief Set the x value + * @param xxx New value + */ + public void setX(float xxx) { + this.x = xxx; + }; + /** + * @brief Set the y value + * @param yyy New value + */ + public void setY(float yyy) { + this.y = yyy; + }; + /** + * @brief Get X value + * @return the x value + */ + public float getX() { + return this.x; + } + /** + * @brief Get Y value + * @return the y value + */ + public float getY() { + return this.y; + } + /** + * @brief Set each element to the max of the current values and the values of another vector + * @param other The other vector to compare with + */ + public void setMax(Vector2f other) { + this.x = Math.max(this.x, other.x); + this.y = Math.max(this.y, other.y); + } + /** + * @brief Set each element to the min of the current values and the values of another vector + * @param other The other vector to compare with + */ + public void setMin(Vector2f other) { + this.x = Math.min(this.x, other.x); + this.y = Math.min(this.y, other.y); + } + /** + * @brief Set Value on the vector + * @param xxx X value. + * @param yyy Y value. + */ + public void setValue(float xxx, float yyy) { + this.x = xxx; + this.y = yyy; + } + /** + * @brief Set 0 value on all the vector + */ + public void setZero() { + this.x = 0; + this.y = 0; + } + /** + * @brief Check if the vector is equal to (0,0) + * @return true The value is equal to (0,0) + * @return false The value is NOT equal to (0,0) + */ + public boolean isZero() { + return this.x == 0 + && this.y == 0; + } + + public Vector2f clone() { + return new Vector2f(this); + } + @Override + public String toString() { + return "Vector2f(" + this.x + "," + this.y + ")"; + } +} diff --git a/src/org/atriasoft/etk/math/Vector2i.java b/src/org/atriasoft/etk/math/Vector2i.java new file mode 100644 index 0000000..35e8a10 --- /dev/null +++ b/src/org/atriasoft/etk/math/Vector2i.java @@ -0,0 +1,362 @@ +package org.atriasoft.etk.math; + +public class Vector2i { + public int x = 0; + public int y = 0; + /* **************************************************** + * Constructor + *****************************************************/ + public Vector2i() { + this.x = 0; + this.y = 0; + } + /** + * @brief Constructor from scalars + * @param xxx X value + * @param yyy Y value + */ + public Vector2i(int xxx, int yyy) { + this.x = xxx; + this.y = yyy; + } + /** + * @brief Constructor with external vector + * @param obj The vector to add to this one + */ + public Vector2i(Vector2i obj) { + this.x = obj.x; + this.y = obj.y; + } + /** + * @brief Operator= Asign the current object with an other object + * @param obj Reference on the external object + */ + public void set(Vector2i obj) { + this.x = obj.x; + this.y = obj.y; + } + /** + * @brief Operator= Asign the current object with a value + * @param val Value to assign on the object + */ + public void set(int val) { + this.x = val; + this.y = val; + } + /** + * @brief Operator= Asign the current object with a value + * @param xxx X value + * @param yyy Y value + */ + public void set(int xxx, int yyy) { + this.x = xxx; + this.y = yyy; + } + /** + * @brief Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are identical + * @return false The Objects are NOT identical + */ + public boolean isEqual(Vector2i obj) { + return ( obj.x == this.x + && obj.y == this.y); + } + /** + * @brief In-Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are NOT identical + * @return false The Objects are identical + */ + public boolean isDifferent(Vector2i obj) { + return ( obj.x != this.x + || obj.y != this.y); + } + public boolean isLowerOrEqual(Vector2i obj) { + return ( this.x <= obj.x + && this.y <= obj.y); + } + public boolean isLower(Vector2i obj) { + return ( this.x < obj.x + && this.y < obj.y); + } + public boolean isGreaterOrEqual(Vector2i obj) { + return ( this.x >= obj.x + && this.y >= obj.y); + } + public boolean isGreater(Vector2i obj) { + return ( this.x > obj.x + && this.y > obj.y); + } + /** + * @brief Operator+= Addition an other vertor with this one + * @param obj Reference on the external object + */ + void add(Vector2i obj) { + this.x += obj.x; + this.y += obj.y; + } + /** + * @brief Operator+= Addition an other vertor with this one + * @param val Value to addition at x/y + */ + public void add(int val) { + this.x += val; + this.y += val; + } + /** + * @brief Operator-= Decrement an other vertor with this one + * @param obj Reference on the external object + */ + public void less(Vector2i obj) { + this.x -= obj.x; + this.y -= obj.y; + } + /** + * @brief Operator-= Decrement an other vertor with this one + * @param val Value to addition at x/y + */ + public void less(int val) { + this.x -= val; + this.y -= val; + } + /** + * @brief Operator*= Multiplication an other vertor with this one + * @param obj Reference on the external object + */ + public void multiply(Vector2i obj) { + this.x *= obj.x; + this.y *= obj.y; + } + /** + * @brief Operator*= Multiplication an other vertor with this one + * @param val Value to addition at x/y + */ + public void multiply(int val) { + this.x *= val; + this.y *= val; + } + /** + * @brief Operator/ Dividing an other vertor with this one + * @param obj Reference on the external object + */ + public void devide(Vector2i obj) { + this.x /= obj.x; + this.y /= obj.y; + } + /** + * @brief Operator/ Dividing an other vertor with this one + * @param val Value to addition at x/y + */ + public void devide(int val) { + this.x /= val; + this.y /= val; + } + /** + * @brief Incrementation of this vector (+1 of 2 elements) + */ + public void increment() { + this.x++; + this.y++; + } + /** + * @brief Decrementation of this vector (-1 of 2 elements) + */ + public void decrement() { + this.x--; + this.y--; + } + /** + * @brief Return the cross product / determinant + * @param obj The other vector in the cross product + * @return cross product value + */ + public int cross(Vector2i obj) { + return this.x * obj.y + - this.y * obj.x; + } + /** + * @brief Return the dot product + * @param obj The other vector in the dot product + * @return Dot product value + */ + public int dot(Vector2i obj) { + return this.x * obj.x + + this.y * obj.y; + } + /** + * @brief Get the length of the vector squared + * @return Squared length value. + */ + public int length2() { + return dot(this); + } + /** + * @brief Get the length of the vector + * @return Length value + */ + public int length() { + return (int) Math.sqrt(length2()); + } + /** + * @brief Return the distance squared between the ends of this and another vector + * This is symantically treating the vector like a point + * @param obj The other vector to compare distance + * @return the square distance of the 2 points + */ + public int distance2(Vector2i obj) { + int deltaX = obj.x - this.x; + int deltaY = obj.y - this.y; + return deltaX*deltaX + deltaY*deltaY; + } + /** + * @brief Return the distance between the ends of this and another vector + * This is symantically treating the vector like a point + * @param obj The other vector to compare distance + * @return the distance of the 2 points + */ + public int distance(Vector2i obj) { + return (int)Math.sqrt(this.distance2(obj)); + } + /** + * @brief Normalize this vector x^2 + y^2 = 1 + */ + public void normalize() { + this.devide(length()); + } + /** + * @brief Normalize this vector x^2 + y^2 = 1 (check if not deviding by 0, if it is the case ==> return (1,0)) + * @return Local reference of the vector normalized + */ + public void safeNormalize() { + int tmp = length(); + if (tmp != 0) { + this.devide(length()); + return; + } + setValue(1,0); + return; + } + /** + * @brief Return a normalized version of this vector + * @return New vector containing the value + */ + public Vector2i normalized() { + Vector2i tmp = this.clone(); + tmp.normalize(); + return tmp; + } + /** + * @brief Return a vector will the absolute values of each element + * @return New vector containing the value + */ + public Vector2i absolute() { + return new Vector2i( Math.abs(this.x), + Math.abs(this.y)); + } + /** + * @brief Return the axis with the smallest value + * @return values are 0,1 for x or y + */ + public int minAxis() { + return this.x < this.y ? 0 : 1; + } + /** + * @brief Return the axis with the largest value + * @return values are 0,1 for x or y + */ + public int maxAxis() { + return this.x < this.y ? 1 : 0; + } + /** + * @brief Return the axis with the smallest ABSOLUTE value + * @return values 0,1 for x, or z + */ + public int furthestAxis() { + return absolute().minAxis(); + } + /** + * @brief Return the axis with the largest ABSOLUTE value + * @return values 0,1 for x or y + */ + public int closestAxis() { + return absolute().maxAxis(); + } + /** + * @brief Set the x value + * @param xxx New value + */ + public void setX(int xxx) { + this.x = xxx; + }; + /** + * @brief Set the y value + * @param yyy New value + */ + public void setY(int yyy) { + this.y = yyy; + }; + /** + * @brief Get X value + * @return the x value + */ + public int getX() { + return this.x; + } + /** + * @brief Get Y value + * @return the y value + */ + public int getY() { + return this.y; + } + /** + * @brief Set each element to the max of the current values and the values of another vector + * @param other The other vector to compare with + */ + public void setMax(Vector2i other) { + this.x = Math.max(this.x, other.x); + this.y = Math.max(this.y, other.y); + } + /** + * @brief Set each element to the min of the current values and the values of another vector + * @param other The other vector to compare with + */ + public void setMin(Vector2i other) { + this.x = Math.min(this.x, other.x); + this.y = Math.min(this.y, other.y); + } + /** + * @brief Set Value on the vector + * @param xxx X value. + * @param yyy Y value. + */ + public void setValue(int xxx, int yyy) { + this.x = xxx; + this.y = yyy; + } + /** + * @brief Set 0 value on all the vector + */ + public void setZero() { + this.x = 0; + this.y = 0; + } + /** + * @brief Check if the vector is equal to (0,0) + * @return true The value is equal to (0,0) + * @return false The value is NOT equal to (0,0) + */ + public boolean isZero() { + return this.x == 0 + && this.y == 0; + } + + public Vector2i clone() { + return new Vector2i(this); + } + @Override + public String toString() { + return "Vector2i(" + this.x + "," + this.y + ")"; + } +} diff --git a/src/org/atriasoft/etk/math/Vector3f.java b/src/org/atriasoft/etk/math/Vector3f.java new file mode 100644 index 0000000..b15a43c --- /dev/null +++ b/src/org/atriasoft/etk/math/Vector3f.java @@ -0,0 +1,547 @@ +package org.atriasoft.etk.math; + +public class Vector3f { + public float x; + public float y; + public float z; + /** + * @brief Default contructor + */ + public Vector3f() { + this.x = 0; + this.y = 0; + this.z = 0; + } + /** + * @brief Constructor from scalars + * @param xxx X value + * @param yyy Y value + * @param zzz Z value + */ + public Vector3f(float xxx, float yyy, float zzz) { + this.x = xxx; + this.y = yyy; + this.z = zzz; + } + /** + * @brief Constructor from scalars + * @param value unique value for X,Y and Z value + */ + public Vector3f(float value) { + this.x = value; + this.y = value; + this.z = value; + } + /** + * @brief Constructor from other vector (copy) + * @param obj The vector to add to this one + */ + public Vector3f(Vector3f obj) { + this.x += obj.x; + this.y += obj.y; + this.z += obj.z; + } + /** + * @brief Add a vector to this one + * @param obj The vector to add to this one + */ + public Vector3f add(Vector3f obj) { + this.x += obj.x; + this.y += obj.y; + this.z += obj.z; + return this; + } + /** + * @brief Add a vector to this one + * @param obj The vector to add to this one + */ + public Vector3f addNew(Vector3f obj) { + return new Vector3f(this.x + obj.x, + this.y + obj.y, + this.z + obj.z); + } + /** + * @brief Subtract a vector from this one + * @param obj The vector to subtract + */ + public Vector3f less(Vector3f obj) { + this.x -= obj.x; + this.y -= obj.y; + this.z -= obj.z; + return this; + } + /** + * @brief Subtract a vector from this one + * @param obj The vector to subtract + * @return A new vector with the data + */ + public Vector3f lessNew(Vector3f obj) { + return new Vector3f(this.x - obj.x, this.y - obj.y, this.z - obj.z); + } + /** + * @brief Scale the vector + * @param val Scale factor + */ + public Vector3f multiply(float val) { + this.x *= val; + this.y *= val; + this.z *= val; + return this; + } + /** + * @brief Scale the vector + * @param val Scale factor + * @return A new vector with the data + */ + public Vector3f multiplyNew(float val) { + return new Vector3f(this.x * val, this.y * val, this.z * val); + } + /** + * @brief Inversely scale the vector + * @param val Scale factor to divide by + */ + public void devide(float val) { + if (val != 0.0f) { + float tmpVal = 1.0f / val; + this.x *= tmpVal; + this.y *= tmpVal; + this.z *= tmpVal; + } + // TODO maybe throw ... + } + /** + * @brief Return the dot product + * @param obj The other vector in the dot product + * @return Dot product value + */ + public float dot(Vector3f obj) { + return this.x * obj.x + + this.y * obj.y + + this.z * obj.z; + } + /** + * @brief Get the length of the vector squared + * @return Squared length value. + */ + public float length2() { + return dot(this); + } + /** + * @brief Get the length of the vector + * @return Length value + */ + public float length() { + return (float) Math.sqrt(length2()); + } + /** + * @brief Return the distance squared between the ends of this and another vector + * This is symantically treating the vector like a point + * @param obj The other vector to compare distance + * @return the square distance of the 2 points + */ + public float distance2(Vector3f obj) { + float deltaX = obj.x - this.x; + float deltaY = obj.y - this.y; + float deltaZ = obj.z - this.z; + return deltaX*deltaX + deltaY*deltaY + deltaZ*deltaZ; + } + /** + * @brief Return the distance between the ends of this and another vector + * This is symantically treating the vector like a point + * @param obj The other vector to compare distance + * @return the distance of the 2 points + */ + public float distance(Vector3f obj) { + return (float)Math.sqrt(this.distance2(obj)); + } + /** + * @brief Normalize this vector x^2 + y^2 + z^2 = 1 (check if not deviding by 0, if it is the case ==> return (1,0,0)) + * @return the current vector + */ + public Vector3f safeNormalize() { + float length = length(); + if (length != 0.0f) { + this.devide(length); + return this; + } + this.setValue(1,0,0); + return this; + } + /** + * @brief Normalize this vector x^2 + y^2 + z^2 = 1 + * @return the current vector + */ + public Vector3f normalize() { + this.devide(this.length()); + return this; + } + /** + * @brief Return a normalized version of this vector + * @return New vector containing the value + */ + public Vector3f normalizeNew() { + Vector3f out = new Vector3f(this); + out.normalize(); + return out; + } + /** + * @brief Return a normalized version of this vector (check if not deviding by 0, if it is the case ==> return (1,0,0)) + * @return New vector containing the value + */ + public Vector3f safeNormalizeNew() { + Vector3f out = new Vector3f(this); + out.safeNormalize(); + return out; + } + /** + * @brief Return a rotated version of this vector + * @param wAxis The axis to rotate about + * @param angle The angle to rotate by + * @return New vector containing the value + */ + public Vector3f rotateNew( Vector3f wAxis, float angle ) { + Vector3f out = wAxis.clone(); + out.multiply( wAxis.dot( this ) ); + Vector3f x = this.clone(); + x.less(out); + Vector3f y = wAxis.cross( this ); + x.multiply((float)Math.cos(angle)); + y.multiply((float)Math.sin(angle)); + out.add(x); + out.add(y); + return out; + } + /** + * @brief Calculate the angle between this and another vector + * @param obj The other vector + * @return Angle in radian + */ + public float angle(Vector3f obj) { + float s = (float) Math.sqrt(length2() * obj.length2()); + if (0!=s) { + return (float) Math.acos(this.dot(obj) / s); + } + return 0; + } + /** + * @brief Return a vector will the absolute values of each element + * @return the curent reference + */ + public Vector3f absolute() { + this.x = Math.abs(this.x); + this.y = Math.abs(this.y); + this.z = Math.abs(this.z); + return this; + } + /** + * @brief Return a vector will the absolute values of each element + * @return New vector containing the value + */ + public Vector3f absoluteNew() { + return new Vector3f( Math.abs(this.x), + Math.abs(this.y), + Math.abs(this.z)); + } + /** + * @brief Return the cross product between this and another vector + * @param obj The other vector + * @return Vector with the result of the cross product + */ + public Vector3f cross(Vector3f obj) { + return new Vector3f(this.y * obj.z - this.z * obj.y, + this.z * obj.x - this.x * obj.z, + this.x * obj.y - this.y * obj.x); + } + /** + * @brief Return the triple product between this and another vector and another + * @param obj1 The other vector 1 + * @param obj2 The other vector 2 + * @return Value with the result of the triple product + */ + public float triple(Vector3f obj1, Vector3f obj2) { + return this.x * (obj1.y * obj2.z - obj1.z * obj2.y) + + this.y * (obj1.z * obj2.x - obj1.x * obj2.z) + + this.z * (obj1.x * obj2.y - obj1.y * obj2.x); + } + /** + * @brief Return the axis with the smallest value + * @return values 0,1,2 for x, y, or z + */ + public int minAxis() { + if (this.x < this.y) { + return this.x < this.z ? 0 : 2; + } + return this.y < this.z ? 1 : 2; + } + /** + * @brief Return the axis with the largest value + * @return values 0,1,2 for x, y, or z + */ + public int maxAxis() { + if (this.x < this.y) { + return this.y < this.z ? 2 : 1; + } + return this.x < this.z ? 2 : 0; + } + /** + * @brief Return the axis with the smallest ABSOLUTE value + * @return values 0,1,2 for x, y, or z + */ + public int furthestAxis() { + return absoluteNew().minAxis(); + } + /** + * @brief Return the axis with the largest ABSOLUTE value + * @return values 0,1,2 for x, y, or z + */ + public int closestAxis() { + return absoluteNew().maxAxis(); + } + /** + * @brief Interpolate the vector with a ration between 2 others + * @param obj0 First vector + * @param obj1 Second vector + * @param ratio Ratio between obj0 and obj1 + */ + public void setInterpolate3(Vector3f obj0, Vector3f obj1, float ratio) { + float inverse = 1.0f - ratio; + this.x = inverse * obj0.x + ratio * obj1.x; + this.y = inverse * obj0.y + ratio * obj1.y; + this.z = inverse * obj0.z + ratio * obj1.z; + // this.co[3] = s * v0[3] + rt * v1[3]; + } + /** + * @brief Return the linear interpolation between this and another vector + * @param obj The other vector + * @param ratio The ratio of this to obj (ratio = 0 => return copy of this, ratio=1 => return other) + * @return New vector containing the value + */ + public Vector3f lerp(Vector3f obj, float ratio) { + return new Vector3f(this.x + (obj.x - this.x) * ratio, + this.y + (obj.y - this.y) * ratio, + this.z + (obj.z - this.z) * ratio); + } + /** + * @brief Elementwise multiply this vector by the other + * @param obj The other vector + * @return the current reference + */ + public Vector3f multiply(Vector3f obj) { + this.x *= obj.x; + this.y *= obj.y; + this.z *= obj.z; + return this; + } + /** + * @brief Elementwise multiply this vector by the other + * @param obj The other vector + */ + public Vector3f multiplyNew(Vector3f obj) { + return new Vector3f(this.x * obj.x, this.y * obj.y, this.z * obj.z); + } + /** + * @brief Set the x value + * @param x New value + */ + public void setX(float x) { + this.x = x; + } + /** + * @brief Set the y value + * @param y New value + */ + public void setY(float y) { + this.y = y; + } + /** + * @brief Set the z value + * @param z New value + */ + public void setZ(float z) { + this.z = z; + } + /** + * @brief Get X value + * @return the x value + */ + public float getX() { + return this.x; + } + /** + * @brief Get Y value + * @return the y value + */ + public float getY() { + return this.y; + } + /** + * @brief Get Z value + * @return the z value + */ + public float getZ() { + return this.z; + } + /** + * @brief Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are identical + * @return false The Objects are NOT identical + */ + public boolean isEqual(Vector3f obj) { + return ( (this.z == obj.z) + && (this.y == obj.y) + && (this.x == obj.x)); + } + /** + * @brief In-Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are NOT identical + * @return false The Objects are identical + */ + public boolean isDifferent(Vector3f obj) { + return ( (this.z != obj.z) + || (this.y != obj.y) + || (this.x != obj.x)); + } + /** + * @brief Set each element to the max of the current values and the values of another Vector3f + * @param obj The other Vector3f to compare with + */ + public void setMax(Vector3f obj) { + this.x = Math.max(this.x, obj.x); + this.y = Math.max(this.y, obj.y); + this.z = Math.max(this.z, obj.z); + } + /** + * @brief Set each element to the min of the current values and the values of another Vector3f + * @param obj The other Vector3f to compare with + */ + public void setMin(Vector3f obj) { + this.x = Math.min(this.x, obj.x); + this.y = Math.min(this.y, obj.y); + this.z = Math.min(this.z, obj.z); + } + /** + * @brief Get the minimum value of the vector (x, y, z) + * @return The min value + */ + public float getMin() { + return Math.min(Math.min(this.x, this.y), this.z); + } + /** + * @brief Get the maximum value of the vector (x, y, z) + * @return The max value + */ + public float getMax() { + return Math.max(Math.max(this.x, this.y), this.z); + } + /** + * @brief Set Value on the vector + * @param xxx X value. + * @param yyy Y value. + * @param zzz Z value. + */ + public void setValue(float xxx, float yyy, float zzz) { + this.x = xxx; + this.y = yyy; + this.z = zzz; + } + /** + * @brief Create a skew matrix of the object + * @param obj0 Vector matric first line + * @param obj1 Vector matric second line + * @param obj2 Vector matric third line + */ + public void getSkewSymmetricMatrix(Vector3f obj0,Vector3f obj1,Vector3f obj2) { + obj0.setValue(0 ,-z ,y); + obj1.setValue(z ,0 ,-x); + obj2.setValue(-y ,x ,0); + } + /** + * @brief Set 0 value on all the vector + */ + public void setZero() { + setValue(0,0,0); + } + /** + * @brief Check if the vector is equal to (0,0,0) + * @return true The value is equal to (0,0,0) + * @return false The value is NOT equal to (0,0,0) + */ + public boolean isZero() { + return this.x == 0 + && this.y == 0 + && this.z == 0; + } + /** + * @brief Get the Axis id with the minimum value + * @return Axis ID 0,1,2 + */ + public int getMinAxis() { + return (this.x < this.y ? (this.x < this.z ? 0 : 2) : (this.y < this.z ? 1 : 2)); + } + /** + * @brief Get the Axis id with the maximum value + * @return Axis ID 0,1,2 + */ + public int getMaxAxis() { + return (this.x < this.y ? (this.y < this.z ? 2 : 1) : (this.x < this.z ? 2 : 0)); + } + /** + * @breif Get the orthogonal vector of the current vector + * @return The ortho vector + */ + public Vector3f getOrthoVector() { + Vector3f vectorAbs = new Vector3f(Math.abs(this.x), Math.abs(this.y), Math.abs(this.z)); + int minElement = vectorAbs.getMinAxis(); + if (minElement == 0) { + float devider = 1.0f / (float)Math.sqrt(this.y*this.y + this.z*this.z); + return new Vector3f(0.0f, -this.z*devider, this.y*devider); + } else if (minElement == 1) { + float devider = 1.0f / (float)Math.sqrt(this.x*this.x + this.z*this.z); + return new Vector3f(-this.z*devider, 0.0f, this.x*devider); + } + float devider = 1.0f / (float)Math.sqrt(this.x*this.x + this.y*this.y); + return new Vector3f(-this.y*devider, this.x*devider, 0.0f); + } + /** + * @brief Clone the current vector. + * @return New vector containing the value + */ + public Vector3f clone() { + return new Vector3f(this); + } + public static Vector3f zero() { + return new Vector3f(0,0,0); + } + @Override + public String toString() { + return "Vector3f(" + this.x + "," + this.y + "," + this.z + ")"; + } + + + + + + /** + * @brief Get the length square between the 2 vectors + * @param start First vector + * @param stop second vector + * @return Length value + */ + public static float length2(Vector3f start, Vector3f stop) { + float x = stop.x - start.x; + float y = stop.y - start.y; + float z = stop.z - start.z; + return x * x + y * y + z * z; + } + /** + * @brief Get the length between the 2 vectors + * @param start First vector + * @param stop second vector + * @return Length value + */ + public float length(Vector3f start, Vector3f stop) { + return (float) Math.sqrt(length2(start, stop)); + } +} diff --git a/src/org/atriasoft/etk/math/Vector3i.java b/src/org/atriasoft/etk/math/Vector3i.java new file mode 100644 index 0000000..bfb9b00 --- /dev/null +++ b/src/org/atriasoft/etk/math/Vector3i.java @@ -0,0 +1,476 @@ +package org.atriasoft.etk.math; + +public class Vector3i { + public int x = 0; + public int y = 0; + public int z = 0; + /** + * @brief Default contructor + */ + public Vector3i() { + } + /** + * @brief Constructor from scalars + * @param xxx X value + * @param yyy Y value + * @param zzz Z value + */ + public Vector3i(int xxx, int yyy, int zzz) { + this.x = xxx; + this.y = yyy; + this.z = zzz; + } + /** + * @brief Constructor from scalars + * @param value unique value for X,Y and Z value + */ + public Vector3i(int value) { + this.x = value; + this.y = value; + this.z = value; + } + /** + * @brief Constructor from other vector (copy) + * @param obj The vector to add to this one + */ + public Vector3i(Vector3i obj) { + this.x += obj.x; + this.y += obj.y; + this.z += obj.z; + } + /** + * @brief Add a vector to this one + * @param obj The vector to add to this one + */ + public Vector3i add(Vector3i obj) { + this.x += obj.x; + this.y += obj.y; + this.z += obj.z; + return this; + } + /** + * @brief Add a vector to this one + * @param obj The vector to add to this one + */ + public Vector3i addNew(Vector3i obj) { + return new Vector3i(this.x + obj.x, + this.y + obj.y, + this.z + obj.z); + } + /** + * @brief Subtract a vector from this one + * @param obj The vector to subtract + */ + public Vector3i less(Vector3i obj) { + this.x -= obj.x; + this.y -= obj.y; + this.z -= obj.z; + return this; + } + /** + * @brief Scale the vector + * @param val Scale factor + */ + public Vector3i multiply(int val) { + this.x *= val; + this.y *= val; + this.z *= val; + return this; + } + /** + * @brief Scale the vector + * @param val Scale factor + */ + public Vector3i multiplyNew(int val) { + return new Vector3i(this.x * val, this.y * val, this.z * val); + } + /** + * @brief Inversely scale the vector + * @param val Scale factor to divide by + */ + public void devide(int val) { + if (val != 0.0f) { + this.x /= val; + this.y /= val; + this.z /= val; + } + // TODO maybe throw ... + } + /** + * @brief Return the dot product + * @param obj The other vector in the dot product + * @return Dot product value + */ + public int dot(Vector3i obj) { + return this.x * obj.x + + this.y * obj.y + + this.z * obj.z; + } + /** + * @brief Get the length of the vector squared + * @return Squared length value. + */ + public int length2() { + return dot(this); + } + /** + * @brief Get the length of the vector + * @return Length value + */ + public int length() { + return (int) Math.sqrt(length2()); + } + /** + * @brief Return the distance squared between the ends of this and another vector + * This is symantically treating the vector like a point + * @param obj The other vector to compare distance + * @return the square distance of the 2 points + */ + public int distance2(Vector3i obj) { + int deltaX = obj.x - this.x; + int deltaY = obj.y - this.y; + int deltaZ = obj.z - this.z; + return deltaX*deltaX + deltaY*deltaY + deltaZ*deltaZ; + } + /** + * @brief Return the distance between the ends of this and another vector + * This is symantically treating the vector like a point + * @param obj The other vector to compare distance + * @return the distance of the 2 points + */ + public int distance(Vector3i obj) { + return (int)Math.sqrt(this.distance2(obj)); + } + /** + * @brief Normalize this vector x^2 + y^2 + z^2 = 1 (check if not deviding by 0, if it is the case ==> return (1,0,0)) + */ + public void safeNormalize() { + int length = length(); + if (length != 0.0f) { + this.devide(length); + } + this.setValue(1,0,0); + } + /** + * @brief Normalize this vector x^2 + y^2 + z^2 = 1 + */ + public void normalize() { + this.devide(this.length()); + } + /** + * @brief Return a normalized version of this vector + * @return New vector containing the value + */ + public Vector3i normalizeNew() { + Vector3i out = new Vector3i(this); + out.normalize(); + return out; + } + /** + * @brief Return a normalized version of this vector (check if not deviding by 0, if it is the case ==> return (1,0,0)) + * @return New vector containing the value + */ + public Vector3i safeNormalizeNew() { + Vector3i out = new Vector3i(this); + out.safeNormalize(); + return out; + } + /** + * @brief Return a rotated version of this vector + * @param wAxis The axis to rotate about + * @param angle The angle to rotate by + * @return New vector containing the value + */ + public Vector3i rotateNew( Vector3i wAxis, int angle ) { + Vector3i out = wAxis.clone(); + out.multiply( wAxis.dot( this ) ); + Vector3i x = this.clone(); + x.less(out); + Vector3i y = wAxis.cross( this ); + x.multiply((int)Math.cos(angle)); + y.multiply((int)Math.sin(angle)); + out.add(x); + out.add(y); + return out; + } + /** + * @brief Calculate the angle between this and another vector + * @param obj The other vector + * @return Angle in radian + */ + public int angle(Vector3i obj) { + int s = (int) Math.sqrt(length2() * obj.length2()); + if (0!=s) { + return (int) Math.acos(this.dot(obj) / s); + } + return 0; + } + /** + * @brief Return a vector will the absolute values of each element + * @return the curent reference + */ + public Vector3i absolute() { + this.x = Math.abs(this.x); + this.y = Math.abs(this.y); + this.z = Math.abs(this.z); + return this; + } + /** + * @brief Return a vector will the absolute values of each element + * @return New vector containing the value + */ + public Vector3i absoluteNew() { + return new Vector3i( Math.abs(this.x), + Math.abs(this.y), + Math.abs(this.z)); + } + /** + * @brief Return the cross product between this and another vector + * @param obj The other vector + * @return Vector with the result of the cross product + */ + public Vector3i cross(Vector3i obj) { + return new Vector3i(this.y * obj.z - this.z * obj.y, + this.z * obj.x - this.x * obj.z, + this.x * obj.y - this.y * obj.x); + } + /** + * @brief Return the triple product between this and another vector and another + * @param obj1 The other vector 1 + * @param obj2 The other vector 2 + * @return Value with the result of the triple product + */ + public int triple(Vector3i obj1, Vector3i obj2) { + return this.x * (obj1.y * obj2.z - obj1.z * obj2.y) + + this.y * (obj1.z * obj2.x - obj1.x * obj2.z) + + this.z * (obj1.x * obj2.y - obj1.y * obj2.x); + } + /** + * @brief Return the axis with the smallest value + * @return values 0,1,2 for x, y, or z + */ + public int minAxis() { + if (this.x < this.y) { + return this.x < this.z ? 0 : 2; + } + return this.y < this.z ? 1 : 2; + } + /** + * @brief Return the axis with the largest value + * @return values 0,1,2 for x, y, or z + */ + public int maxAxis() { + if (this.x < this.y) { + return this.y < this.z ? 2 : 1; + } + return this.x < this.z ? 2 : 0; + } + /** + * @brief Return the axis with the smallest ABSOLUTE value + * @return values 0,1,2 for x, y, or z + */ + public int furthestAxis() { + return absoluteNew().minAxis(); + } + /** + * @brief Return the axis with the largest ABSOLUTE value + * @return values 0,1,2 for x, y, or z + */ + public int closestAxis() { + return absoluteNew().maxAxis(); + } + /** + * @brief Return the linear interpolation between this and another vector + * @param obj The other vector + * @param ratio The ratio of this to obj (ratio = 0 => return copy of this, ratio=1 => return other) + * @return New vector containing the value + */ + public Vector3i lerp(Vector3i obj, int ratio) { + return new Vector3i(this.x + (obj.x - this.x) * ratio, + this.y + (obj.y - this.y) * ratio, + this.z + (obj.z - this.z) * ratio); + } + /** + * @brief Elementwise multiply this vector by the other + * @param obj The other vector + * @return the current reference + */ + public Vector3i multiply(Vector3i obj) { + this.x *= obj.x; + this.y *= obj.y; + this.z *= obj.z; + return this; + } + /** + * @brief Elementwise multiply this vector by the other + * @param obj The other vector + */ + public Vector3i multiplyNew(Vector3i obj) { + this.x *= obj.x; + this.y *= obj.y; + this.z *= obj.z; + return this; + } + /** + * @brief Set the x value + * @param x New value + */ + public void setX(int x) { + this.x = x; + } + /** + * @brief Set the y value + * @param y New value + */ + public void setY(int y) { + this.y = y; + } + /** + * @brief Set the z value + * @param z New value + */ + public void setZ(int z) { + this.z = z; + } + /** + * @brief Get X value + * @return the x value + */ + public int getX() { + return this.x; + } + /** + * @brief Get Y value + * @return the y value + */ + public int getY() { + return this.y; + } + /** + * @brief Get Z value + * @return the z value + */ + public int getZ() { + return this.z; + } + /** + * @brief Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are identical + * @return false The Objects are NOT identical + */ + public boolean isEqual(Vector3i obj) { + return ( (this.z == obj.z) + && (this.y == obj.y) + && (this.x == obj.x)); + } + /** + * @brief In-Equality compare operator with an other object. + * @param obj Reference on the comparing object + * @return true The Objects are NOT identical + * @return false The Objects are identical + */ + public boolean isDifferent(Vector3i obj) { + return ( (this.z != obj.z) + || (this.y != obj.y) + || (this.x != obj.x)); + } + /** + * @brief Set each element to the max of the current values and the values of another Vector3f + * @param obj The other Vector3f to compare with + */ + public void setMax(Vector3i obj) { + this.x = Math.max(this.x, obj.x); + this.y = Math.max(this.y, obj.y); + this.z = Math.max(this.z, obj.z); + } + /** + * @brief Set each element to the min of the current values and the values of another Vector3f + * @param obj The other Vector3f to compare with + */ + public void setMin(Vector3i obj) { + this.x = Math.min(this.x, obj.x); + this.y = Math.min(this.y, obj.y); + this.z = Math.min(this.z, obj.z); + } + /** + * @brief Get the minimum value of the vector (x, y, z) + * @return The min value + */ + public int getMin() { + return Math.min(Math.min(this.x, this.y), this.z); + } + /** + * @brief Get the maximum value of the vector (x, y, z) + * @return The max value + */ + public int getMax() { + return Math.max(Math.max(this.x, this.y), this.z); + } + /** + * @brief Set Value on the vector + * @param xxx X value. + * @param yyy Y value. + * @param zzz Z value. + */ + public void setValue(int xxx, int yyy, int zzz) { + this.x = xxx; + this.y = yyy; + this.z = zzz; + } + /** + * @brief Create a skew matrix of the object + * @param obj0 Vector matric first line + * @param obj1 Vector matric second line + * @param obj2 Vector matric third line + */ + public void getSkewSymmetricMatrix(Vector3i obj0,Vector3i obj1,Vector3i obj2) { + obj0.setValue(0 ,-z ,y); + obj1.setValue(z ,0 ,-x); + obj2.setValue(-y ,x ,0); + } + /** + * @brief Set 0 value on all the vector + */ + public void setZero() { + setValue(0,0,0); + } + /** + * @brief Check if the vector is equal to (0,0,0) + * @return true The value is equal to (0,0,0) + * @return false The value is NOT equal to (0,0,0) + */ + public boolean isZero() { + return this.x == 0 + && this.y == 0 + && this.z == 0; + } + /** + * @brief Get the Axis id with the minimum value + * @return Axis ID 0,1,2 + */ + public int getMinAxis() { + return (this.x < this.y ? (this.x < this.z ? 0 : 2) : (this.y < this.z ? 1 : 2)); + } + /** + * @brief Get the Axis id with the maximum value + * @return Axis ID 0,1,2 + */ + public int getMaxAxis() { + return (this.x < this.y ? (this.y < this.z ? 2 : 1) : (this.x < this.z ? 2 : 0)); + } + /** + * @brief Clone the current vector. + * @return New vector containing the value + */ + public Vector3i clone() { + return new Vector3i(this); + } + public static Vector3i zero() { + return new Vector3i(0,0,0); + } + @Override + public String toString() { + return "Vector3i(" + this.x + "," + this.y + "," + this.z + ")"; + } +} diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/test/src/test/atriasoft/etk/.keep b/test/src/test/atriasoft/etk/.keep new file mode 100644 index 0000000..e69de29 diff --git a/test/src/test/atriasoft/etk/Log.java b/test/src/test/atriasoft/etk/Log.java new file mode 100644 index 0000000..5c3b329 --- /dev/null +++ b/test/src/test/atriasoft/etk/Log.java @@ -0,0 +1,59 @@ +package test.atriasoft.etk; + +import io.scenarium.logger.LogLevel; +import io.scenarium.logger.Logger; + +public class Log { + private static final String LIB_NAME = "etk-test"; + private static final String LIB_NAME_DRAW = Logger.getDrawableName(LIB_NAME); + private static final boolean PRINT_CRITICAL = Logger.getNeedPrint(LIB_NAME, LogLevel.CRITICAL); + private static final boolean PRINT_ERROR = Logger.getNeedPrint(LIB_NAME, LogLevel.ERROR); + private static final boolean PRINT_WARNING = Logger.getNeedPrint(LIB_NAME, LogLevel.WARNING); + private static final boolean PRINT_INFO = Logger.getNeedPrint(LIB_NAME, LogLevel.INFO); + private static final boolean PRINT_DEBUG = Logger.getNeedPrint(LIB_NAME, LogLevel.DEBUG); + private static final boolean PRINT_VERBOSE = Logger.getNeedPrint(LIB_NAME, LogLevel.VERBOSE); + private static final boolean PRINT_TODO = Logger.getNeedPrint(LIB_NAME, LogLevel.TODO); + private static final boolean PRINT_PRINT = Logger.getNeedPrint(LIB_NAME, LogLevel.PRINT); + + private Log() {} + + public static void print(String data) { + if (PRINT_PRINT) + Logger.print(LIB_NAME_DRAW, data); + } + + public static void critical(String data) { + if (PRINT_CRITICAL) + Logger.critical(LIB_NAME_DRAW, data); + } + + public static void error(String data) { + if (PRINT_ERROR) + Logger.error(LIB_NAME_DRAW, data); + } + + public static void warning(String data) { + if (PRINT_WARNING) + Logger.warning(LIB_NAME_DRAW, data); + } + + public static void info(String data) { + if (PRINT_INFO) + Logger.info(LIB_NAME_DRAW, data); + } + + public static void debug(String data) { + if (PRINT_DEBUG) + Logger.debug(LIB_NAME_DRAW, data); + } + + public static void verbose(String data) { + if (PRINT_VERBOSE) + Logger.verbose(LIB_NAME_DRAW, data); + } + + public static void todo(String data) { + if (PRINT_TODO) + Logger.todo(LIB_NAME_DRAW, data); + } +} diff --git a/test/src/test/atriasoft/etk/Log2.java b/test/src/test/atriasoft/etk/Log2.java new file mode 100644 index 0000000..6b57782 --- /dev/null +++ b/test/src/test/atriasoft/etk/Log2.java @@ -0,0 +1,18 @@ +package test.atriasoft.etk; + +import io.scenarium.logger.LogLevel; +import io.scenarium.logger.Logger; + +public class Log2 { + private static final String LIB_NAME = "etk-test-2"; + private static final String LIB_NAME_DRAW = Logger.getDrawableName(LIB_NAME); + private static final boolean PRINT_DEBUG = Logger.getNeedPrint(LIB_NAME, LogLevel.DEBUG); + + private Log2() {} + + public static void debug(String data) { + if (PRINT_DEBUG) + Logger.debug(LIB_NAME_DRAW, data); + } + +} diff --git a/test/src/test/atriasoft/etk/TestBasicLog.java b/test/src/test/atriasoft/etk/TestBasicLog.java new file mode 100644 index 0000000..6106c2c --- /dev/null +++ b/test/src/test/atriasoft/etk/TestBasicLog.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + * + * Contributors: + * Edouard DUPIN - initial API and implementation + ******************************************************************************/ +package test.atriasoft.etk; + +import java.util.ArrayList; +import java.util.List; + +import io.scenarium.logger.Logger; + +import org.junit.Test; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +//import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +@TestMethodOrder(OrderAnnotation.class) +public class TestBasicLog { + + @Test + @Order(1) + public void aaFirstInitialisation() { + List args = new ArrayList<>(); + args.add("--log-level=999"); + args.add("--log-level=1"); + args.add("--log-no-color"); + args.add("--log-color"); + args.add("--log-lib=sc-log-test+6"); + args.add("--log-lib=sc-log-test/6"); + args.add("--log-lib=sc-log-test:6"); + args.add("--log-lib=sc-log-test:verbose"); + args.add("--log-lib=sc-log-test2+3"); + args.add("--log-lib=sc-log-test"); + args.add("--log-with-stupid-parameter=sdkfjsqdlkf"); + args.add("--help"); + Logger.init(args); + } + + @Test + @Order(2) + public void bbSecondInitialisation() { + List args = new ArrayList<>(); + Logger.init(args); + } + + @Test + @Order(3) + public void ccBasicLogCall() { + Log.print("Simple print"); + Log.todo("Simple todo"); + Log.error("Simple error"); + Log.warning("Simple warning"); + Log.info("Simple info"); + Log.debug("Simple debug"); + Log.verbose("Simple verbose"); + } + + // TODO REFACTO REMOVE this and set it in the Test of the logger. + public static String getAAAAAAA(int dfsdf) { + int hhh = 0; + for (int kkk = 0; kkk < dfsdf; kkk++) + for (int iii = 0; iii < 10000; iii++) + for (int jjj = 0; jjj < 100000; jjj++) + for (int lll = 0; lll < 100000; lll++) + hhh++; + return "kkk" + hhh; + } + + public static void testLog() { + Log.print("test direct [START]"); + // test de 10 secondes contre 0.0?? second quand le niveau n'est pas assez grand ... + long timeStart = System.currentTimeMillis(); + for (int iii = 0; iii < 100000000; iii++) + Log2.debug("test direct"); + long timeStop = System.currentTimeMillis(); + Log.print("test direct [END] : " + timeStart + " to " + timeStop + " ==> delta=" + (timeStop - timeStart)); + Log.print("test concat [START]"); + // C'est très long dans les 2 cas ... + timeStart = System.currentTimeMillis(); + for (int iii = 0; iii < 6; iii++) + Log2.debug("test concat: non fonctionnel, il applelle le get a chaque log ... " + getAAAAAAA(iii)); + timeStop = System.currentTimeMillis(); + Log.print("test concat [END] : " + timeStart + " to " + timeStop + " ==> delta=" + (timeStop - timeStart)); + } + + @Test + @Order(4) + public void ddTestSimpleLog() { + testLog(); + } + + @Test + @Order(4) + public void eeUsage() { + Logger.usage(); + } + +} diff --git a/version.txt b/version.txt new file mode 100644 index 0000000..6c6aa7c --- /dev/null +++ b/version.txt @@ -0,0 +1 @@ +0.1.0 \ No newline at end of file