// // import { Vec3, v3, Quat, Segment } from "./index"
// import { createProgram } from './render/gl';
// // var v = new Vec3();
// // v.add(v3(10, 1230, 12));
// // document.write(v.toString());

// // var quat = new Quat();

// // document.write(quat.x + "")
// // var segment = new Segment(v3(10, 20, 0), v3(100, 100, 0));
// // const distance = v.distanceSegment(segment);
// // document.write(JSON.stringify(distance))
// const vs = `
// uniform mat4 u,worldViewProjection;
// uniform vec3 u,lightWorldPos;
// uniform mat4 u,world;
// uniform mat4 u,viewInverse;
// uniform mat4 u,worldInverseTranspose;

// attribute vec4 position;
// attribute vec3 normal;
// attribute vec2 texcoord;

// varying vec4 v,position;
// varying vec2 v,texCoord;
// varying vec3 v,normal;
// varying vec3 v,surfaceToLight;
// varying vec3 v,surfaceToView;

// void main() {
//     v,texCoord = texcoord;
//     v,position = u,worldViewProjection * position;
//     v,normal = (u,worldInverseTranspose * vec4(normal, 0)).xyz;
//     v,surfaceToLight = u,lightWorldPos - (u,world * position).xyz;
//     v,surfaceToView = (u,viewInverse[3] - (u,world * position)).xyz;
//     gl,Position = v,position;
// }
// `
// const fs = `
// varying vec4 v,position;
// varying vec2 v,texCoord;
// varying vec3 v,normal;
// varying vec3 v,surfaceToLight;
// varying vec3 v,surfaceToView;

// uniform vec4 u,lightColor;
// uniform vec4 u,ambient;
// uniform sampler2D u,diffuse;
// uniform vec4 u,specular;
// uniform float u,shininess;
// uniform float u,specularFactor;

// vec4 lit(float l, float h, float m) {
//     return vec4(1.0,
//         max(l, 0.0),
//         (l > 0.0) ? pow(max(0.0, h), m) : 0.0,
//         1.0);
// }

// void main() {
//     vec4 diffuseColor = texture2D(u,diffuse, v,texCoord);
//     vec3 a,normal = normalize(v,normal);
//     vec3 surfaceToLight = normalize(v,surfaceToLight);
//     vec3 surfaceToView = normalize(v,surfaceToView);
//     vec3 halfVector = normalize(surfaceToLight + surfaceToView);
//     vec4 litR = lit(dot(a,normal, surfaceToLight),
//         dot(a,normal, halfVector), u,shininess);
//     vec4 outColor = vec4((
//         u,lightColor * (diffuseColor * litR.y + diffuseColor * u,ambient +
//             u,specular * litR.z * u,specularFactor)).rgb,
//         diffuseColor.a);
//     gl,FragColor = outColor;
// }
// `

// const canvas = document.createElement('canvas');
// const renderer: WebGL2RenderingContext | WebGLRenderingContext = canvas.getContext("webgl2") || canvas.getContext("webgl")!;

// const program = createProgram(renderer, { vertexShader: vs, fragmentShader: fs })

// const arrays = {
//     position: [1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1],
//     normal: [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1],
//     texcoord: [1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
//     indices: [0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23],
// };

// function render(time: number) {
//     requestAnimationFrame(render);
// }
// requestAnimationFrame(render)
import * as cga from "./index"
import { Vec3, v3 } from './math/Vec3';
import { GLView } from './glview';
import { recognitionCCW } from './alg/recognition';

var glv = new GLView({ container: document.body })

var ps = [6.31, 0.85, 6.52, 1.61, 6.63, 2.39, 6.65, 2.98, 6.66, 3.17, 6.59, 3.96, 6.43, 4.73, 6.17, 5.48, 5.83, 6.19, 5.41, 6.85, 4.92, 7.47, 4.35, 8.01, 3.73, 8.49, 3.05, 8.89, 2.33, 9.21, 1.57, 9.44, 0.8, 9.58, 0.01, 9.63, - 0.78, 9.58, - 1.55, 9.44, - 2.3, 9.21, - 3.03, 8.89, - 3.7, 8.49, - 4.33, 8.01, - 4.9, 7.47, - 5.39, 6.85, - 5.81, 6.19, - 6.15, 5.48, - 6.4, 4.73, - 6.57, 3.96, - 6.64, 3.17, - 6.63, 2.98, - 6.61, 2.39, - 6.5, 1.61, - 6.29, 0.87, - 6.8, 0.7, - 6.96, 1.71, - 6.98, 1.79, - 7.06, 2.66, - 7.06, 2.98, - 7.05, 3.51, - 6.93, 4.34, - 6.71, 5.16, - 6.41, 5.94, - 6.01, 6.68, - 5.53, 7.36, - 4.96, 7.98, - 4.34, 8.53, -3.65, 9, - 2.91, 9.4, - 2.12, 9.69, - 1.31, 9.89, - 0.47, 10, 0.37, 10.01, 1.21, 9.91, 2.02, 9.72, 2.81, 9.44, 3.55, 9.06, 4.25, 8.6, 4.88, 8.05, 5.46, 7.43, 5.95, 6.76, 6.37, 6.03, 6.68, 5.25, 6.9, 4.44, 7.04, 3.61, 7.06, 2.98, 7.07, 2.77, 7, 1.9, 6.96, 1.74, 6.79, 0.69]
var ps1 = [6.74, -0.89, 7.16, 1.74, 7.2, 1.9, 7.27, 2.77, 7.26, 2.98, 7.24, 3.63, 7.1, 4.48, 6.87, 5.31, 6.55, 6.12, 6.12, 6.87, 5.62, 7.56, 5.02, 8.19, 4.37, 8.76, 3.65, 9.23, 2.89, 9.62, 2.08, 9.91, 1.24, 10.11, 0.38, 10.21, -0.48, 10.2, -1.35, 10.09, -2.18, 9.88, -2.99, 9.58, -3.75, 9.17, -4.46, 8.69, -5.1, 8.12, -5.69, 7.48, -6.18, 6.78, -6.59, 6.02, -6.9, 5.22, -7.13, 4.38, -7.25, 3.52, -7.26, 2.98, -7.26, 2.66, -7.18, 1.79, -7.16, 1.71, -6.74, -0.87, -6.54, -0.87, -6.8, 0.7, -6.96, 1.71, -6.98, 1.79, -7.06, 2.66, -7.06, 2.98, -7.05, 3.51, -6.93, 4.34, -6.71, 5.16, -6.41, 5.94, -6.01, 6.68, -5.53, 7.36, -4.96, 7.98, -4.34, 8.53, -3.65, 9, -2.91, 9.4, -2.12, 9.69, -1.31, 9.89, -0.47, 10, 0.37, 10.01, 1.21, 9.91, 2.02, 9.72, 2.81, 9.44, 3.55, 9.06, 4.25, 8.6, 4.88, 8.05, 5.46, 7.43, 5.95, 6.76, 6.37, 6.03, 6.68, 5.25, 6.9, 4.44, 7.04, 3.61, 7.06, 2.98, 7.07, 2.77, 7, 1.9, 6.96, 1.74, 6.79, 0.69, 6.54, -0.89
]
var bound: Vec3[] = []
for (let i = 0; i < ps.length; i += 2) {
    bound.push(new Vec3(ps[i], ps[i + 1], 0));
}
var bound1: Vec3[] = []
for (let i = 0; i < ps1.length; i += 2) {
    bound1.push(new Vec3(ps1[i], ps1[i + 1], 0));
}
console.log(recognitionCCW(bound));
console.log(recognitionCCW(bound1));


const mesh = cga.extrudeToMesh(bound, [v3(), v3(0, 0, 100)], { isClosed: true, sealStart: true, sealEnd: true })
glv.add(mesh);
const mesh1 = cga.extrudeToMesh(bound1, [v3(), v3(0, 0, 100)], { isClosed: true, sealStart: true, sealEnd: true })
mesh1.position.x = 15;
glv.add(mesh1);

glv.run();
function randomV3() {
    return cga.v3(
        Math.random() * 100 - 50,
        Math.random() * 100,
        Math.random() * 100 - 50
    );
}
var ray = new cga.Ray(randomV3(), randomV3().normalize());
var segment = new cga.Segment(randomV3(), randomV3());
var result = ray.distanceSegment(segment);
