package org.joml.test;

import java.util.Random;

import junit.framework.TestCase;

import org.joml.Matrix4f;
import org.joml.TrapezoidOrthoCrop;
import org.joml.Vector3f;

/**
 * Tests for the {@link TrapezoidOrthoCrop} class.
 * 
 * @author Kai Burjack
 */
public class TrapezoidOrthoCropTest extends TestCase {

    public static void testRandomNdcWithinBounds() {
        Matrix4f camViewProj = new Matrix4f().perspective((float) Math.toRadians(90.0), 1.0f, 0.1f, 10.0f).lookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
        Matrix4f invCamViewProj = new Matrix4f();
        camViewProj.invert(invCamViewProj);
        Matrix4f lightView = new Matrix4f().lookAt(0, 5, 0, 0, 0, 0, 0, 0, -1);
        Matrix4f crop = new TrapezoidOrthoCrop().compute(camViewProj, lightView, 3.0f, new Matrix4f());
        Vector3f corner = new Vector3f();
        Random rnd = new Random();
        float minZ = Float.MAX_VALUE, maxZ = -Float.MAX_VALUE;
        for (int i = 0; i < 5000; i++) {
            float x = rnd.nextFloat() * 2.0f - 1.0f;
            float y = rnd.nextFloat() * 2.0f - 1.0f;
            float z = rnd.nextFloat() * 2.0f - 1.0f;
            corner.set(x, y, z);
            invCamViewProj.transformProject(corner);
            lightView.transformProject(corner);
            crop.transformProject(corner);
            float d = 1E5f;
            boolean withinBounds =
                    corner.x >= -1.0f - d && corner.x <= +1.0f + d &&
                    corner.y >= -1.0f - d && corner.y <= +1.0f + d &&
                    corner.z >= -1.0f - d && corner.z <= +1.0f + d;
            assertTrue(withinBounds);
            minZ = minZ < corner.z ? minZ : corner.z;
            maxZ = maxZ > corner.z ? maxZ : corner.z;
        }
    }

}

@@ -862,71 +862,8 @@
 ();%0A
-        float minZ = Float.MAX_VALUE, maxZ = -Float.MAX_VALUE;%0A
     
@@ -895,24 +895,24 @@
 000; i++) %7B%0A
+
             
@@ -1259,16 +1259,17 @@
 t d = 1E
+-
 5f;%0A    
@@ -1507,16 +1507,16 @@
 0f + d;%0A
+
         
@@ -1549,116 +1549,8 @@
 s);%0A
-            minZ = minZ %3C corner.z ? minZ : corner.z;%0A            maxZ = maxZ %3E corner.z ? maxZ : corner.z;%0A
     

Fix test