/* Mathematiques de l'Infographie               */
/* Test des classes CH3D, Pos3D et Dir3D        */
/*                                              */
/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Mars 2021                                    */

#include <stdio.h>

#include "Pos3D.h"
#include "Dir3D.h"

//////////////////////////////////////////////////

#include <math.h>

/* Fonction de test de la planarite             */
/* d'une facette à quatre sommets               */

int testPlanarite(Pos3D *p1,Pos3D *p2,Pos3D *p3,Pos3D *p4) {
  const float EPSILON = 0.000001F;
  Dir3D v1(p1,p2);
  Dir3D v2(p1,p3);
  Dir3D v3(p1,p4);
  float res = (v1^v2)*v3;
  res = (res > 0 ? res: -res);
  return res <= EPSILON;
}

int main(void) {

  { Pos3D *p0 = new Pos3D();
    p0->print();
    printf("\n");
    delete(p0); }

  { Pos3D p1(2.0F,3.0F,4.0F);
    p1.print();
    printf("\n"); }

  { Pos3D *p2 = new Pos3D(-1.1F,-0.2F,-2.3F);
    p2->print();
    printf("\n");
    delete(p2); }

  printf("\n");

  { Dir3D *d0 = new Dir3D();
    d0->print();
    printf("\n");
    delete(d0); }

  { Dir3D d1(1.1F,2.3F,-2.1F);
    d1.print();
    printf("\n"); }

  { Pos3D p1(2.0F,3.0F,4.0F);
    Pos3D *p2 = new Pos3D(-1.1F,-0.2F,-2.3F);
    Dir3D *d2 = new Dir3D(p1,*p2);
    d2->print();
    printf("\n");
    delete(p2);
    delete(d2); }

  printf("\n");

  { Dir3D d1(1.1F,2.3F,-2.1F);
    Pos3D p1(2.0F,3.0F,4.0F);
    Pos3D *p2 = new Pos3D(-1.1F,-0.2F,-2.3F);
    Dir3D *d2 = new Dir3D(&p1,p2);
    printf("%f\n",d1.norme());
    printf("%f\n",d2->norme());
    d1 = d1.normalize();
    *d2 = d2->normalize();
    d1.print();
    printf("  %25.20f\n",d1.norme());
    d2->print();
    printf("  %25.20f\n",d2->norme());
    delete(p2);
    delete(d2); }

  printf("\n");

  { Dir3D d1(1.1F,2.3F,-2.1F);
    Pos3D p1(2.0F,3.0F,4.0F);
    Pos3D *p2 = new Pos3D(-1.1F,-0.2F,-2.3F);
    Dir3D *d2 = new Dir3D(p1,p2);
    printf("%f\n",d1**d2);
    printf("%f\n",*d2*d1);
    delete(d2);
    delete(p2); }

  printf("\n");

  { Dir3D *pv1 = new Dir3D(1.1F,2.3F,-2.1F);
    Dir3D *pv2 = new Dir3D(-1.1F,-0.2F,-2.3F);
    Dir3D pv3 = *pv1^*pv2;
    pv3.print();
    printf("\n");
    *pv1 = *pv1^*pv2;
    pv1->print();
    printf("\n");
    delete(pv1);
    pv1 = new Dir3D(1.1F,2.3F,-2.1F);
    *pv2 = *pv1^*pv2;
    pv2->print();
    printf("\n");
    delete(pv1);
    delete(pv2); }

  printf("\n");

  { Pos3D p1(2.0,  3.0, 4.0);
    Pos3D p2(6.0,  1.0, 1.0);
    Pos3D p3(3.0,  7.0, 5.0);
    Pos3D p4(7.0,-13.0,-5.0);
    Pos3D p5(7.0,-13.0,-6.0);
    printf("P1 : ");
    p1.print();
    printf("\n");
    printf("P2 : ");
    p2.print();
    printf("\n");
    printf("P3 : ");
    p3.print();
    printf("\n");
    printf("P4 : ");
    p4.print();
    printf("\n");
    printf("P5 : ");
    p5.print();
    printf("\n");
    if ( testPlanarite(&p1,&p2,&p3,&p4) )
      printf("Facette (P1,P2,P3,P4) planaire\n");
      else
      printf("Facette (P1,P2,P3,P4) non planaire\n");
    if ( testPlanarite(&p1,&p2,&p3,&p5) )
      printf("Facette (P1,P2,P3,P5) planaire\n");
      else
      printf("Facette (P1,P2,P3,P5) non planaire\n"); }

  getchar();

  return(0);
}

//////////////////////////////////////////////////
