From 1c131c03fe8c5d5ab17193c9f6e7e79d81110d52 Mon Sep 17 00:00:00 2001 From: ultramn Date: Thu, 2 Mar 2017 19:55:33 -0800 Subject: added affinesolve --- dave/AffineSolve.c | 322 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 322 insertions(+) create mode 100644 dave/AffineSolve.c diff --git a/dave/AffineSolve.c b/dave/AffineSolve.c new file mode 100644 index 0000000..36587e6 --- /dev/null +++ b/dave/AffineSolve.c @@ -0,0 +1,322 @@ +// +// main.c +// AffineSolve +// +// Created by user on 3/2/17. +// Copyright © 2017 user. All rights reserved. +// + +#include +#include +#include +#include + +#define LH_ID 0 +#define NUM_HMD 32 + +float hmd_pos[NUM_HMD][3]; +void ReadHmdPoints() +{ + int i; + FILE *fin = fopen("HMD_points.csv","r"); + if (fin==NULL) { + printf("ERROR: could not open HMD_points.csv for reading\n"); + exit(1); + } + + for (i=0; i TOO_SMALL) { + float scaleRot = stepSizeRot / gradSizeRot; + for (j=0; j<3; j++) { + for (k=0; k<3; k++) { + T[j][k] += scaleRot * conj[j][k]; + } + } + stepSizeRot *= falloff; + } + + if (gradSizePos > TOO_SMALL) { + float scalePos = stepSizePos / gradSizePos; + for (j=0; j<3; j++) { + T[j][3] += scalePos * conj[j][3]; + } + stepSizePos *= falloff; + } + + // Constrain the gradient (such that scaling is one) + if (constrain) + { + // Measure the scales + float len[3] = {0.0, 0.0, 0.0}; + for (j=0; j<3; j++) { + double lenSq = 0.0; + for (k=0; k<3; k++) { lenSq += (double)T[j][k] * (double)T[j][k]; } + len[j] = sqrt(lenSq); + } + + // How far off is the scale? + float xzLen = 0.5 * (len[0] + len[2]); + if (xzLen > TOO_SMALL) { + float inv_xzLen = 1.0 / xzLen; + for (j=0; j<3; j++) { + T[3][j] *= inv_xzLen; + } + } + + // Rescale the thing + for (j=0; j<3; j++) + { + if (len[j] > TOO_SMALL) { + float inv_len = 1.0 / len[j]; + for (k=0; k<3; k++) { T[j][k] *= inv_len; } + } + } + } + } + +} + +int main() +{ + int i,j,k; + float Tcalc[4][4]; + float O[MAX_POINTS][4]; + float N[MAX_POINTS][3]; + float D[MAX_POINTS]; + int nPoints = 0; + + // Read the hmd points + ReadHmdPoints(); + + //------------------------- + // Read the lighthouse data + //------------------------- + FILE *fin = fopen("ptinfo.csv", "r"); + if (fin==NULL) { printf("ERROR: could not open ptinfo.csv for reading\n"); exit(1); } + while (!feof(fin)) + { + // Read the angle + int sen,lh,axis,count; + float angle, avglen, stddevang, stddevlen; + float max_outlier_length, max_outlier_angle; + int rt = fscanf( fin, "%d %d %d %d %f %f %f %f %f %f\n", + &sen, &lh, &axis, &count, + &angle, &avglen, &stddevang, &stddevlen, + &max_outlier_length, &max_outlier_angle); + if (rt != 10) { break; } + + if (lh == LH_ID && sen < NUM_HMD) { + // Set the offset + O[nPoints][0] = hmd_pos[sen][0]; + O[nPoints][1] = hmd_pos[sen][1]; + O[nPoints][2] = hmd_pos[sen][2]; + O[nPoints][3] = 1.0; + + // Calculate the plane equation + if (axis == 1) { // Horizontal + N[nPoints][0] = -cos(angle); + N[nPoints][1] = -sin(angle); + N[nPoints][2] = 0.0; + D[nPoints] = 0.0; + } else { // Vertical + N[nPoints][0] = 0.0; + N[nPoints][1] = -sin(angle); + N[nPoints][2] = cos(angle); + D[nPoints] = 0.0; + } + + printf("pt %d O %.3f %.3f %.3f %.3f N %.3f %.3f %.3f D %.3f\n", + nPoints, + O[nPoints][0], O[nPoints][1], O[nPoints][2], O[nPoints][3], + N[nPoints][0], N[nPoints][1], N[nPoints][2], + D[nPoints]); + + nPoints++; + } + } + fclose(fin); + + printf("nPoints %d\n", nPoints); + + // Run the calculation for Tcalc + int run; + //for (run=0; run<100; run++) { + + // Initialize Tcalc to the identity matrix + //memcpy(Tcalc, Torig, 4*4*sizeof(float)); + memset(Tcalc, 0, 4*4*sizeof(float)); + for (i=0; i<4; i++) { Tcalc[i][i] = 1.0f; } + + // Solve it! + AffineSolve( + Tcalc, // OUTPUT: transform + O, // INPUT: points, offsets + N, // INPUT: plane normals + D, // INPUT: plane offsets + nPoints, NITER, + STEP_SIZE_ROT, STEP_SIZE_POS, FALLOFF, + 1); + //} + + PRINT_MAT(Tcalc,4,4); + + + // insert code here... + printf("Hello, World!\n"); + return 0; +} -- cgit v1.2.3