Page MenuHomec4science

ObjectLoader.c
No OneTemporary

File Metadata

Created
Wed, May 22, 19:19

ObjectLoader.c

/*********************************************************************************
* Copyright (c) 2017 *
* Ahmed Eleliemy <ahmed.eleliemy@unibas.ch> *
* University of Basel, Switzerland *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the license (GNU LGPL) which comes with this package. *
*********************************************************************************/
#include "ObjectLoader.h"
#include <string.h>
#include <ctype.h>
#include "SpinDataStructures.h"
char ** strsplit(char* a_str, const char a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
/* Count how many elements will be extracted. */
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller knows where the list of returned strings ends. */
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
*(result + idx) = 0;
}
return result;
}
MeshObject* loadofffile(char * path)
{
MeshObject * strMeshObject;
int i=0;
double dNVertice, dNFace = 0;
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
const char delim =' ' ;
char **info ;
double x, y, z = 0;
strMeshObject = malloc(sizeof(MeshObject));
fp = fopen(path, "r");
//off format
read = getline(&line, &len, fp);
//free(line);
#ifdef MDEBUG
printf("Master Read the first line\n");
#endif
// read number of vertices and faces
read = getline(&line, &len, fp);
info=strsplit(line, delim);
dNVertice = atof(info[0]);
dNFace = atof(info[1]);
strMeshObject->iFaceCount = dNFace;
strMeshObject->iPointCount = dNVertice;
//free(line);
free(info);
#ifdef MDEBUG
printf("Master Read the second line\n");
printf("Master reading data file..\n Number of points %d and Number of face %d\n",(int)dNVertice,(int)dNFace);
#endif
strMeshObject->dptrXPoints = malloc(sizeof(double) * dNVertice);
strMeshObject->dptrYPoints = malloc(sizeof(double) * dNVertice);
strMeshObject->dptrZPoints = malloc(sizeof(double) * dNVertice);
#ifdef MDEBUG
printf("Master points allocated \n");
#endif
for (i = 0; i < dNVertice; i++)
{
read = getline(&line, &len, fp);
info = strsplit(line,delim);
x = atof(info[0]);
y = atof(info[1]);
z = atof(info[2]);
//printf("Master %f %f %f \n",x,y,z);
strMeshObject->dptrXPoints[i] = x;
strMeshObject->dptrYPoints[i] = y;
strMeshObject->dptrZPoints[i] = z;
//free(line);
}
strMeshObject->dptrXNPoints = malloc(sizeof(double) * dNVertice*3);
strMeshObject->dptrYNPoints = malloc(sizeof(double) * dNVertice*3);
strMeshObject->dptrZNPoints = malloc(sizeof(double) * dNVertice*3);
strMeshObject->facesPerPoint= malloc(sizeof(int) * dNVertice);
memset(strMeshObject->facesPerPoint,0,sizeof(int) * dNVertice);
for (i = 0; i < dNFace; i++)
{
double Ux = 0, Uy = 0, Uz = 0;
double Vx = 0, Vy = 0, Vz = 0;
read = getline(&line, &len, fp);
info = strsplit(line,delim);
int index0 = atoi(info[1]);
int index1 = atoi(info[2]);
int index2 = atoi(info[3]);
double P0X =strMeshObject->dptrXPoints[index0];
double P0Y =strMeshObject->dptrYPoints[index0];
double P0Z =strMeshObject->dptrZPoints[index0];
double P1X =strMeshObject->dptrXPoints[index1];
double P1Y =strMeshObject->dptrYPoints[index1];
double P1Z =strMeshObject->dptrZPoints[index1];
double P2X =strMeshObject->dptrXPoints[index2];
double P2Y =strMeshObject->dptrYPoints[index2];
double P2Z =strMeshObject->dptrZPoints[index2];
double UX= P1X- P0X;
double UY= P1Y- P0Y;
double UZ= P1Z- P0Z;
double VX= P2X- P0X;
double VY= P2Y- P0Y;
double VZ= P2Z- P0Z;
strMeshObject->dptrXNPoints[index0]+= UY*VZ - UZ*VY;
strMeshObject->dptrXNPoints[index1]+= UY*VZ - UZ*VY;
strMeshObject->dptrXNPoints[index2]+= UY*VZ - UZ*VY;
strMeshObject->dptrYNPoints[index0]+= UZ*VX - UX*VZ;
strMeshObject->dptrYNPoints[index1]+= UZ*VX - UX*VZ;
strMeshObject->dptrYNPoints[index2]+= UZ*VX - UX*VZ;
strMeshObject->dptrZNPoints[index0]+= UX*VY - UY*VX;
strMeshObject->dptrZNPoints[index1]+= UX*VY - UY*VX;
strMeshObject->dptrZNPoints[index2]+= UX*VY - UY*VX;
strMeshObject->facesPerPoint[index0]+=1;
strMeshObject->facesPerPoint[index1]+=1;
strMeshObject->facesPerPoint[index2]+=1;
//free(line);
}
fclose(fp);
free(info);
free(line);
#ifdef MDEBUG
printf("Master: feeded data to the mesh object and freed the pointers..\n");
#endif
return strMeshObject;
}

Event Timeline