<commit_before>/*
 * (C) Copyright 2016-2018 JOML

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.

 */
package org.joml.sampling;

//#ifdef __HAS_NIO__
import java.nio.FloatBuffer;
//#endif
import java.util.ArrayList;

import org.joml.Random;
import org.joml.Vector2f;
import org.joml.Vector3f;

/**
 * Creates samples using the "Best Candidate" algorithm.
 * 
 * @author <NAME>
 */
public class BestCandidateSampling {

    private static final class IntHolder {
        int value;
    }

    /**
     * Generates Best Candidate samples on a unit sphere.
     * <p>
     * References:
     * <ul>
     * <li><a href="https://www.microsoft.com/en-us/research/wp-content/uploads/2005/09/tr-2005-123.pdf">Indexing the Sphere with the Hierarchical Triangular Mesh</a>
     * <li><a href="http://math.stackexchange.com/questions/1244512/point-in-a-spherical-triangle-test">Point in a spherical triangle test</a>
     * </ul>
     * 
     * @author <NAME>
     */
    public static class Sphere {
        /**
         * Implementation of a Hierarchical Triangular Mesh structure to index the sample points on the unit sphere for accelerating 1-nearest neighbor searches.
         * 
         * @author <NAME>
         */
        private static final class Node {
            private static final int MAX_OBJECTS_PER_NODE = 32;

            private float v0x, v0y, v0z;
            private float v1x, v1y, v1z;
            private float v2x, v2y, v2z;
            private float cx, cy, cz;
            private float arc;

            private ArrayList objects;
            private Node[] children;

            Node() {
                this.children = new Node[8];
                float s = 1f;
                this.arc = 2.0f * (float) Math.PI;
                /*
                 * See: https://www.microsoft.com/en-us/research/wp-content/uploads/2005/09/tr-2005-123.pdf
                 */
                this.children[0] = new Node(-s, 0, 0, 0, 0, s, 0, s, 0);
                this.children[1] = new Node(0, 0, s, s, 0, 0, 0, s, 0);
                this.children[2] = new Node(s, 0, 0, 0, 0, -s, 0, s, 0);
                this.children[3] = new Node(0, 0, -s, -s, 0, 0, 0, s, 0);
                this.children[4] = new Node(-s, 0, 0, 0, -s, 0, 0, 0, s);
                this.children[5] = new Node(0, 0, s, 0, -s, 0, s, 0, 0);
                this.children[6] = new Node(s, 0, 0, 0, -s, 0, 0, 0, -s);
                this.children[7] = new Node(0, 0, -s, 0, -s, 0, -s, 0, 0);
            }

            private Node(float x0, float y0, float z0, float x1, float y1, float z1, float x2, float y2, float z2) {
                this.v0x = x0;
                this.v0y = y0;
                this.v0z = z0;
                this.v1x = x1;
                this.v1y = y1;
                this.v1z = z1;
                this.v2x = x2;
                this.v2y = y2;
                this.v2z = z2;
                cx = (v0x + v1x + v2x) / 3.0f;
                cy = (v0y + v1y + v2y) / 3.0f;
                cz = (v0z + v1z + v2z) / 3.0f;
                float invCLen = 1.0f / (float) Math.sqrt(cx * cx + cy * cy + cz * cz);
                cx *= invCLen;
                cy *= invCLen;
                cz *= invCLen;
                // Compute maximum radius around triangle centroid
                float arc1 = greatCircleDist(cx, cy, cz, v0x, v0y, v0z);
                float arc2 = greatCircleDist(cx, cy, cz, v1x, v1y, v1z);
                float arc3 = greatCircleDist(cx, cy, cz, v2x, v2y, v2z);
                float dist = Math.max(Math.max(arc1, arc2), arc3);
                /*
                 * Convert radius into diameter, but also take into account the linear
                 * arccos approximation we use.
                 * This value was obtained empirically!
                 */
                dist *= 1.7f;
                this.arc = dist;
            }

            private void split() {
                float w0x = v1x + v2x;
                float w0y = v1y + v2y;
                float w0z = v1z + v2z;
                float len0 = 1.0f / (float) Math.sqrt(w0x * w0x + w0y * w0y + w0z * w0z);
                w0x *= len0;
                w0y *= len0;
                w0z *= len0;
                float w1x = v0x + v2x;
                float w1y = v0y + v2y;
                float w1z = v0z + v2z;
                float len1 = 1.0f / (float) Math.sqrt(w1x * w1x + w1y * w1y + w1z * w1z);
                w1x *= len1;
                w1y *= len1;
                w1z *= len1;
                float w2x = v0x + v1x;
                float w2y = v0y + v1y;
                float w2z = v0z + v1z;
                float len2 = 1.0f / (float) Math.sqrt(w2x * w2x + w2y * w2y + w2z * w2z);
                w2x *= len2;
                w2y *= len2;
                w2z *= len2;
                children = new Node[4];
                /*
                 * See: https://www.microsoft.com/en-us/research/wp-content/uploads/2005/09/tr-2005-123.pdf
                 */
                children[0] = new Node(v0x, v0y, v0z, w2x, w2y, w2z, w1x, w1y, w1z);
                children[1] = new Node(v1x, v1y, v1z, w0x, w0y, w0z, w2x, w2y, w2z);
                children[2] = new Node(v2x, v2y, v2z, w1x, w1y, w1z, w0x, w0y, w0z);
                children[3] = new Node(w0x, w0y, w0z, w1x, w1y, w1z, w2x, w2y, w2z);
            }

            private void insertIntoChild(Vector3f o) {
                for (int i = 0; i < children.length; i++) {
                    Node c = children[i];
                    /*
                     * Idea: Test whether ray from origin towards point cuts triangle
                     * 
                     * See: http://math.stackexchange.com/questions/1244512/point-in-a-spherical-triangle-test
                     */
                    if (isPointOnSphericalTriangle(o.x, o.y, o.z, c.v0x, c.v0y, c.v0z, c.v1x, c.v1y, c.v1z, c.v2x, c.v2y, c.v2z, 1E-6f)) {
                        c.insert(o);
                        return;
                    }
                }
            }<commit_msg>Fix URL that became invalid<commit_after>/*
 * (C) Copyright 2016-2018 JOML

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.

 */
package org.joml.sampling;

//#ifdef __HAS_NIO__
import java.nio.FloatBuffer;
//#endif
import java.util.ArrayList;

import org.joml.Random;
import org.joml.Vector2f;
import org.joml.Vector3f;

/**
 * Creates samples using the "Best Candidate" algorithm.
 * 
 * @author <NAME>
 */
public class BestCandidateSampling {

    private static final class IntHolder {
        int value;
    }

    /**
     * Generates Best Candidate samples on a unit sphere.
     * <p>
     * References:
     * <ul>
     * <li><a href="https://arxiv.org/ftp/cs/papers/0701/0701164.pdf">Indexing the Sphere with the Hierarchical Triangular Mesh</a>
     * <li><a href="http://math.stackexchange.com/questions/1244512/point-in-a-spherical-triangle-test">Point in a spherical triangle test</a>
     * </ul>
     * 
     * @author <NAME>
     */
    public static class Sphere {
        /**
         * Implementation of a Hierarchical Triangular Mesh structure to index the sample points on the unit sphere for accelerating 1-nearest neighbor searches.
         * 
         * @author <NAME>
         */
        private static final class Node {
            private static final int MAX_OBJECTS_PER_NODE = 32;

            private float v0x, v0y, v0z;
            private float v1x, v1y, v1z;
            private float v2x, v2y, v2z;
            private float cx, cy, cz;
            private float arc;

            private ArrayList objects;
            private Node[] children;

            Node() {
                this.children = new Node[8];
                float s = 1f;
                this.arc = 2.0f * (float) Math.PI;
                /*
                 * See: https://arxiv.org/ftp/cs/papers/0701/0701164.pdf
                 */
                this.children[0] = new Node(-s, 0, 0, 0, 0, s, 0, s, 0);
                this.children[1] = new Node(0, 0, s, s, 0, 0, 0, s, 0);
                this.children[2] = new Node(s, 0, 0, 0, 0, -s, 0, s, 0);
                this.children[3] = new Node(0, 0, -s, -s, 0, 0, 0, s, 0);
                this.children[4] = new Node(-s, 0, 0, 0, -s, 0, 0, 0, s);
                this.children[5] = new Node(0, 0, s, 0, -s, 0, s, 0, 0);
                this.children[6] = new Node(s, 0, 0, 0, -s, 0, 0, 0, -s);
                this.children[7] = new Node(0, 0, -s, 0, -s, 0, -s, 0, 0);
            }

            private Node(float x0, float y0, float z0, float x1, float y1, float z1, float x2, float y2, float z2) {
                this.v0x = x0;
                this.v0y = y0;
                this.v0z = z0;
                this.v1x = x1;
                this.v1y = y1;
                this.v1z = z1;
                this.v2x = x2;
                this.v2y = y2;
                this.v2z = z2;
                cx = (v0x + v1x + v2x) / 3.0f;
                cy = (v0y + v1y + v2y) / 3.0f;
                cz = (v0z + v1z + v2z) / 3.0f;
                float invCLen = 1.0f / (float) Math.sqrt(cx * cx + cy * cy + cz * cz);
                cx *= invCLen;
                cy *= invCLen;
                cz *= invCLen;
                // Compute maximum radius around triangle centroid
                float arc1 = greatCircleDist(cx, cy, cz, v0x, v0y, v0z);
                float arc2 = greatCircleDist(cx, cy, cz, v1x, v1y, v1z);
                float arc3 = greatCircleDist(cx, cy, cz, v2x, v2y, v2z);
                float dist = Math.max(Math.max(arc1, arc2), arc3);
                /*
                 * Convert radius into diameter, but also take into account the linear
                 * arccos approximation we use.
                 * This value was obtained empirically!
                 */
                dist *= 1.7f;
                this.arc = dist;
            }

            private void split() {
                float w0x = v1x + v2x;
                float w0y = v1y + v2y;
                float w0z = v1z + v2z;
                float len0 = 1.0f / (float) Math.sqrt(w0x * w0x + w0y * w0y + w0z * w0z);
                w0x *= len0;
                w0y *= len0;
                w0z *= len0;
                float w1x = v0x + v2x;
                float w1y = v0y + v2y;
                float w1z = v0z + v2z;
                float len1 = 1.0f / (float) Math.sqrt(w1x * w1x + w1y * w1y + w1z * w1z);
                w1x *= len1;
                w1y *= len1;
                w1z *= len1;
                float w2x = v0x + v1x;
                float w2y = v0y + v1y;
                float w2z = v0z + v1z;
                float len2 = 1.0f / (float) Math.sqrt(w2x * w2x + w2y * w2y + w2z * w2z);
                w2x *= len2;
                w2y *= len2;
                w2z *= len2;
                children = new Node[4];
                /*
                 * See: https://arxiv.org/ftp/cs/papers/0701/0701164.pdf
                 */
                children[0] = new Node(v0x, v0y, v0z, w2x, w2y, w2z, w1x, w1y, w1z);
                children[1] = new Node(v1x, v1y, v1z, w0x, w0y, w0z, w2x, w2y, w2z);
                children[2] = new Node(v2x, v2y, v2z, w1x, w1y, w1z, w0x, w0y, w0z);
                children[3] = new Node(w0x, w0y, w0z, w1x, w1y, w1z, w2x, w2y, w2z);
            }

            private void insertIntoChild(Vector3f o) {
                for (int i = 0; i < children.length; i++) {
                    Node c = children[i];
                    /*
                     * Idea: Test whether ray from origin towards point cuts triangle
                     * 
                     * See: http://math.stackexchange.com/questions/1244512/point-in-a-spherical-triangle-test
                     */
                    if (isPointOnSphericalTriangle(o.x, o.y, o.z, c.v0x, c.v0y, c.v0z, c.v1x, c.v1y, c.v1z, c.v2x, c.v2y, c.v2z, 1E-6f)) {
                        c.insert(o);
                        return;
                    }
                }
            }
{'fraction_non_alphanumeric': 0.10924610860053081, 'fraction_numerical': 0.045764292375008966, 'mean_word_length': 1.4958825635517365, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 27, 'https://': 6, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5556448', 'n_tokens_mistral': 4952, 'n_tokens_neox': 4496, 'n_words': 1412}