/**********************************************************************
* EINFUEHRUNG IN DIE COMPUTERGRAFIK                      BEISPIEL 1.2 *
*                                                                     *
* Kalchmair Franz-Helmut                                     Gruppe 4 *
*                                                                     *
* LinAlg                                                              *
**********************************************************************/

#include <stdio.h>

typedef float vec3[3];
typedef float matrix33[3][3];

void readVector(vec3 v)
{
	printf("Vector coordinates: \n");
	printf("x: "); scanf("%f",&v[0]);
	printf("y: "); scanf("%f",&v[1]);
	printf("z: "); scanf("%f",&v[2]);
	//printf("in: %.3f\n",v[2]);
}

void printVector(vec3 v)
{
	printf("Vector: (%.3f, %.3f, %.3f)\n\n",v[0], v[1], v[2]);
}

void readMatrix(matrix33 m)
{
	
  const MAX = 3;
  int row,col;

  printf("Matrix(3x3) coordinates:\n");
  
  for (row=0; row<MAX; row++) {
    printf("row %d: \n",(row+1)); 
    for (col=0; col<MAX; col++) {
      printf("col %d: ",(col+1));
      fscanf(stdin, "%f",&m[row][col]);
    }
  }
}

void ScalarProd() 
{
  vec3 v1,v2;
  float sp=0;

  readVector(v1);
  readVector(v2);
  
  sp = v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];

  printf("Result of Sp: %.3f\n", sp);
}

void DotProd() 
{
  vec3 v1,v2,v;

  readVector(v1);
  readVector(v2);

  v[0] = v1[1]*v2[2]-v1[2]*v2[1];
  v[1] = -v1[0]*v2[2]+v1[2]*v2[0];
  v[2] = v1[0]*v2[1]-v1[1]*v2[0];

  printf("\nResult of Dotp:\n");
  printVector(v);
}

void MatrixVectorMult () 
{
	matrix33 m;
  vec3 v,vRes;

  readMatrix(m);
  readVector(v);

  vRes[0] = m[0][0]*v[0]+m[0][1]*v[1]+m[0][2]*v[2];
  vRes[1] = m[1][0]*v[0]+m[1][1]*v[1]+m[1][2]*v[2];
  vRes[2] = m[2][0]*v[0]+m[2][1]*v[1]+m[2][2]*v[2];

  printf("\nResult of Mvp:\n");
  printVector(vRes);
}

void main(int argc, char **argv)
{
	int start = 0;

	do {

		printf("\nWelcome to linalg\n");
		printf("\t1) Matrix/Vector multiplication (matrix33 * vec3)\n");
		printf("\t2) Dotproduct (vec3 x vec3)\n");
		printf("\t3) Scalarproduct (vec3 * vec3)\n");
		printf("\t4) Exit\n");
		scanf("%d",&start);
    printf("\n");
		switch (start) {
			case 1:
				MatrixVectorMult();
				break;
			case 2:
				DotProd();
				break;
			case 3:
				ScalarProd();
				break;
			case 4:
				break;
			default:
				printf("Falsche Taste ...\n\n");
		}

	} while (start != 4);
}
