Cheers
Nifal Adam




Posted 18 June 2012 - 02:28 AM
Posted 18 June 2012 - 05:57 AM
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define COLS 3 //Defining a constant for number of columns
int main(int argc, char **argv) {
FILE *fp = fopen(argv[1], "r"); //Opening file to read
char string1[20];
int loopx = 0, loopy = 0, loopn = 0;
double tmp;
int i = 0;
static double (*vectarr)[COLS]; //Dynamic memory allocation
static double (*normarr)[COLS];
static double (*centerarr)[COLS];
normarr = malloc(COLS * sizeof(double));
if (normarr == NULL) {
puts("Failure to allocate memory");
exit(0);
}
vectarr = malloc(3 * COLS * sizeof(double));
if (vectarr == NULL) {
puts("Failure to allocate memory");
exit(0);
}
centerarr = malloc(COLS * sizeof(double));
if (centerarr == NULL) {
puts("Failure to allocate memory");
exit(0);
}
if (fp == NULL) {
printf("Cannot open file.\n");
exit(1);
}
while (fscanf(fp, "%s", string1) != EOF) { //The infinite loop
if (isdigit(string1[0]) != 0 || isdigit(string1[1]) != 0) {
//Saving the values into the dynamic array
if (loopn == 3) {
vectarr[3 * i + loopx][loopy] = atof(string1);
loopy++;
if (loopy == 3) {
loopx++;
loopy = 0;
}
}
if (loopn != 3) {
normarr[i][loopn] = atof(string1);
loopn++;
}
if (loopn == 3 && loopx == 3) { //On to next triangle
for (loopy = 0; loopy < 3; loopy++) {
tmp = 0;
for (loopx = 0; loopx < 3; loopx++)
tmp = tmp + vectarr[3 * i + loopx][loopy];
tmp = tmp / 3;
centerarr[i][loopy] = tmp;
}
loopx = 0;
loopy = 0;
loopn = 0;
i++;
double (*tmp1)[COLS] = realloc(normarr,
(i + 1) * COLS * sizeof(double)); //reallocating memory
if (tmp1 != NULL)
normarr = tmp1;
else {
fprintf(stderr, "Can't reallocate memory\n");
}
double (*tmp2)[COLS] = realloc(vectarr,
3 * (i + 1) * COLS * sizeof(double));
if (tmp2 != NULL)
vectarr = tmp2;
else {
fprintf(stderr, "Can't reallocate memory\n");
}
double (*tmp3)[COLS] = realloc(centerarr,
(i + 1) * COLS * sizeof(double)); //reallocating memory
if (tmp3 != NULL)
centerarr = tmp3;
else {
fprintf(stderr, "Can't reallocate memory\n");
}
}
}
}
printf("Done"); //Indicating that the program is done.
if (fp != NULL) //Closing the file
fclose(fp);
return 0;
}
Posted 18 June 2012 - 06:17 AM
#define COLS 3 //Defining a constant for number of columns
int main(int argc, char **argv) {
static double (*normarr)[COLS];
normarr = malloc(COLS * sizeof(double));
double normarr[COLS][COLS];
This post has been edited by jimblumberg: 18 June 2012 - 06:17 AM
Posted 18 June 2012 - 06:22 AM
double (*foo())[COLS] {
double (*vectarr)[COLS] = malloc( 10 * sizeof(*vectarr) );
return vectarr;
}
typedef double (*atype)[COLS];
atype bar() {
double (*vectarr)[COLS] = malloc( 10 * sizeof(*vectarr) );
// could also do
// atype vectarr = malloc( 10 * sizeof(*vectarr) );
return vectarr;
}
typedef struct {
double (*vectarr)[COLS]; //Dynamic memory allocation
double (*normarr)[COLS];
double (*centerarr)[COLS];
} stl_st;
stl_st readSTLFile ( const char *filename ) {
stl_st result = { NULL, NULL, NULL };
// do stuff
return result;
}
Posted 18 June 2012 - 06:44 AM
typedef struct {
double **data;
int rows, int cols;
} Matrix;
typedef struct {
Matrix vect, norm, center;
} Data;
void createMatrix(Matrix *, int rows, int cols);
void destroyMatrix(Matrix *);
void createData(Data *, int N, int cols);
void destroyData(Data *);
Posted 20 June 2012 - 04:26 AM
//Count the no of facets
#ifndef ExtRowFunc_C
#define ExtRowFunc_C
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void ExtRowFunc(char* file_name, int* no_facets) {
FILE *fp = fopen(file_name, "r"); //Opening file to read
if (fp == NULL) {
printf("Cannot open file.\n");
exit(1);
}
*no_facets = 0;
char string[20];
while (fscanf(fp, "%s", string) != EOF) {
if (!strcmp(string, "endfacet"))
(*no_facets)++;
}
}
#endif
#ifndef StlParse_C
#define StlParse_C
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void StlParse(char* file_name, int no_facets, double* vectarr, double* normarr) {
char string[20];
int i = 0;
int loopx = 0, loopy = 0, loopn = 0;
FILE *fp = fopen(file_name, "r"); //Opening file to read
if (fp == NULL) {
printf("Cannot open file.\n");
exit(1);
}
while (fscanf(fp, "%s", string) != EOF) {
if (isdigit(string[0]) != 0 || isdigit(string[1]) != 0) {
if (loopn == 3) {
vectarr[9 * i + 3 * loopx + loopy] = atof(string);
loopy++;
if (loopy == 3) {
loopx++;
loopy = 0;
}
}
if (loopn != 3) {
normarr[3 * i + loopn] = atof(string);
loopn++;
}
if (loopn == 3 && loopx == 3) { //On to next triangle
loopx = 0;
loopy = 0;
loopn = 0;
i++;
}
} //isdigit
} //while
}
#endif
|
|
Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).
|
