P
pereges
I hae compiled and executed codes of vector.c, reader.c , test.c
seperately and they can be execute in proper manner and give correct
outputs but when i tried to integrate them all into a single project
using pelles C it is giving two many errors.
/* vector.h */
#ifndef vector_h
#define vector_h
#include "common.h"
typedef struct vector_struct
{
double x, y, z;
} vector;
double vector_squared_length(vector* a) ;
double vector_length(vector *a);
vector *vector_negate(vector *a);
double vector_dot(vector *a, vector *b);
double vector_distance(vector *a, vector *b);
vector *vector_normalize( vector *a);
vector *vector_scale(vector *a, double newlength);
vector *vector_add(vector *a, vector *b, vector *c);
vector *vector_sub(vector *a, vector *b, vector *c);
vector *vector_cross(vector *a, vector *b, vector *c);
#endif
++++++++++++++++++++++++++++++++++++++
/* reader.h */
#ifndef reader_h
#define reader_h
#include "common.h"
typedef vector vertex;
typedef struct triangle_struct
{
int v0, v1, v2;
}triangle;
typedef struct object_struct
{
int nvert,ntri;
vertex *vert;
triangle *tri;
}object;
int read(object **);
#endif
+++++++++++++++++++++++++++++++++++++
/* common.h */
#ifndef common_h
#define common_h
#ifndef reader_h
#include "reader.h"
#endif
#ifndef vector_h
#include "vector.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void foo();
#endif
+++++++++++++++++++++++++++++++++++++++++
/* vector.c */
#include "common.h"
double vector_squared_length(vector* a)
{
return((a->x) * (a->x) + (a->y) * (a->y) + (a->z) * (a->z));
}
double vector_length(vector *a)
{
return (sqrt(vector_squared_length(a)));
}
vector *vector_negate(vector *a)
{
a->x = -a->x;
a->y = -a->y;
a->z = -a->z;
return(a);
}
double vector_dot(vector *a, vector *b)
{
return((a->x)*(b->x) + (a->y)*(b->y) + (a->z) *(b->z));
}
double vector_distance(vector *a, vector *b)
{
vector dv;
vector_sub(a, b, &dv);
return vector_length(&dv);
}
vector *vector_normalize( vector *a)
{
double len = vector_length(a);
if( len!= 0.0 )
{
a->x /= len;
a->y /= len;
a->z /= len;
}
return(a);
}
vector *vector_scale( vector *a, double newlength )
{
double length = vector_length(a);
if( length != 0.0 )
{
a->x *= newlength/length ;
a->y *= newlength/length ;
a->z *= newlength/length ;
}
return(a);
}
vector *vector_add( vector *a, vector *b, vector *c)
{
c->x = a->x + b->x ;
c->y = a->y + b->y ;
c->z = a->z + b->z;
return(c);
}
vector *vector_sub( vector *a, vector *b, vector *c)
{
c->x = a->x - b->x;
c->y = a->y - b->y;
c->z = a->z - b->z;
return(c);
}
vector *vector_cross( vector *a, vector *b, vector *c )
{
c->x = (a->y * b->z) - (a->z * b->y) ;
c->y = (a->z * b->x) - (a->x * b->z) ;
c->z = (a->x * b->y) - (a->y * b->x) ;
return(c);
}
++++++++++++++++++++++++++++++++++++++++++++++
/* common.c */
#include "common.h"
void foo(void)
{
}
+++++++++++++++++++++++++++++++++++++++++++++++
#include "common.h"
int reader(object **obj)
{
enum
{
FIRST_LINE,
SECOND_LINE,
THIRD_LINE,
FOURTH_LINE,
VERTICES,
GAP,
TRIANGLES,
LAST_LINE
} state;
int vertex_count = 0;
int triangle_count = 0;
FILE *zeus_file;
char data_line[120];
state = FIRST_LINE;
zeus_file = fopen("sphere.zeus", "r");
if(zeus_file == NULL)
{
perror(" File open error!");
return(EXIT_FAILURE);
}
while (state != LAST_LINE)
{
if (fgets(data_line, 120, zeus_file) == NULL)
{
perror("zeus file read failed");
return -1;
}
switch (state)
{
case (FIRST_LINE):
/* do nothing */
state = SECOND_LINE;
break;
case (SECOND_LINE):
/* do nothing */
state = THIRD_LINE;
break;
case (THIRD_LINE):
sscanf(data_line, "%d %*d %d", &((*obj)->nvert),
&((*obj)->ntri));
(*obj)->vert = calloc(sizeof(vertex),
(*obj)->nvert);
(*obj)->tri = calloc(sizeof(triangle),
(*obj)->ntri);
if( (*obj)->vert == NULL || (*obj)->tri == NULL)
{
perror("Memory cannot be allocated!");
return -1;
}
state = FOURTH_LINE;
break;
case (FOURTH_LINE):
/* do nothing */
state = VERTICES;
break;
case (VERTICES):
sscanf(data_line, "%lf %lf %lf",&((*obj)-
if (vertex_count >= ((*obj)->nvert))
state = GAP;
break;
case (GAP):
state = TRIANGLES;
break;
case (TRIANGLES):
sscanf(data_line, "%d %d %d",&((*obj)-
if (triangle_count >= (*obj)->ntri )
state = LAST_LINE;
break;
}
}
return
0;
}
++++++++++++++++++++++++++++++++++++++++++++++
/* test.c */
#include "common.h"
void test_vector(void)
{
vector *a;
vector *b;
a = malloc(sizeof(vector));
b = malloc(sizeof(vector));
a->x = 1; a->y = 3; a-> z= 9;
b->x = 2; b->y = 5; b->z = 7;
printf("Vector dot is %f \n",vector_dot(a,b));
}
void test_reader(void)
{
int i;
object o, *obj;
obj = &o;
i = reader(&obj);
if(i == EXIT_FAILURE)
printf("error");
else
{
printf("Printing the vertices and the triangles \n\n");
}
}
int main(void)
{
test_vector();
test_reader();
return 0;
}
seperately and they can be execute in proper manner and give correct
outputs but when i tried to integrate them all into a single project
using pelles C it is giving two many errors.
/* vector.h */
#ifndef vector_h
#define vector_h
#include "common.h"
typedef struct vector_struct
{
double x, y, z;
} vector;
double vector_squared_length(vector* a) ;
double vector_length(vector *a);
vector *vector_negate(vector *a);
double vector_dot(vector *a, vector *b);
double vector_distance(vector *a, vector *b);
vector *vector_normalize( vector *a);
vector *vector_scale(vector *a, double newlength);
vector *vector_add(vector *a, vector *b, vector *c);
vector *vector_sub(vector *a, vector *b, vector *c);
vector *vector_cross(vector *a, vector *b, vector *c);
#endif
++++++++++++++++++++++++++++++++++++++
/* reader.h */
#ifndef reader_h
#define reader_h
#include "common.h"
typedef vector vertex;
typedef struct triangle_struct
{
int v0, v1, v2;
}triangle;
typedef struct object_struct
{
int nvert,ntri;
vertex *vert;
triangle *tri;
}object;
int read(object **);
#endif
+++++++++++++++++++++++++++++++++++++
/* common.h */
#ifndef common_h
#define common_h
#ifndef reader_h
#include "reader.h"
#endif
#ifndef vector_h
#include "vector.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void foo();
#endif
+++++++++++++++++++++++++++++++++++++++++
/* vector.c */
#include "common.h"
double vector_squared_length(vector* a)
{
return((a->x) * (a->x) + (a->y) * (a->y) + (a->z) * (a->z));
}
double vector_length(vector *a)
{
return (sqrt(vector_squared_length(a)));
}
vector *vector_negate(vector *a)
{
a->x = -a->x;
a->y = -a->y;
a->z = -a->z;
return(a);
}
double vector_dot(vector *a, vector *b)
{
return((a->x)*(b->x) + (a->y)*(b->y) + (a->z) *(b->z));
}
double vector_distance(vector *a, vector *b)
{
vector dv;
vector_sub(a, b, &dv);
return vector_length(&dv);
}
vector *vector_normalize( vector *a)
{
double len = vector_length(a);
if( len!= 0.0 )
{
a->x /= len;
a->y /= len;
a->z /= len;
}
return(a);
}
vector *vector_scale( vector *a, double newlength )
{
double length = vector_length(a);
if( length != 0.0 )
{
a->x *= newlength/length ;
a->y *= newlength/length ;
a->z *= newlength/length ;
}
return(a);
}
vector *vector_add( vector *a, vector *b, vector *c)
{
c->x = a->x + b->x ;
c->y = a->y + b->y ;
c->z = a->z + b->z;
return(c);
}
vector *vector_sub( vector *a, vector *b, vector *c)
{
c->x = a->x - b->x;
c->y = a->y - b->y;
c->z = a->z - b->z;
return(c);
}
vector *vector_cross( vector *a, vector *b, vector *c )
{
c->x = (a->y * b->z) - (a->z * b->y) ;
c->y = (a->z * b->x) - (a->x * b->z) ;
c->z = (a->x * b->y) - (a->y * b->x) ;
return(c);
}
++++++++++++++++++++++++++++++++++++++++++++++
/* common.c */
#include "common.h"
void foo(void)
{
}
+++++++++++++++++++++++++++++++++++++++++++++++
#include "common.h"
int reader(object **obj)
{
enum
{
FIRST_LINE,
SECOND_LINE,
THIRD_LINE,
FOURTH_LINE,
VERTICES,
GAP,
TRIANGLES,
LAST_LINE
} state;
int vertex_count = 0;
int triangle_count = 0;
FILE *zeus_file;
char data_line[120];
state = FIRST_LINE;
zeus_file = fopen("sphere.zeus", "r");
if(zeus_file == NULL)
{
perror(" File open error!");
return(EXIT_FAILURE);
}
while (state != LAST_LINE)
{
if (fgets(data_line, 120, zeus_file) == NULL)
{
perror("zeus file read failed");
return -1;
}
switch (state)
{
case (FIRST_LINE):
/* do nothing */
state = SECOND_LINE;
break;
case (SECOND_LINE):
/* do nothing */
state = THIRD_LINE;
break;
case (THIRD_LINE):
sscanf(data_line, "%d %*d %d", &((*obj)->nvert),
&((*obj)->ntri));
(*obj)->vert = calloc(sizeof(vertex),
(*obj)->nvert);
(*obj)->tri = calloc(sizeof(triangle),
(*obj)->ntri);
if( (*obj)->vert == NULL || (*obj)->tri == NULL)
{
perror("Memory cannot be allocated!");
return -1;
}
state = FOURTH_LINE;
break;
case (FOURTH_LINE):
/* do nothing */
state = VERTICES;
break;
case (VERTICES):
sscanf(data_line, "%lf %lf %lf",&((*obj)-
vertex_count += 1;vert[vertex_count].x),&((*obj)->vert[vertex_count].y),&((*obj)-
vert[vertex_count].z));
if (vertex_count >= ((*obj)->nvert))
state = GAP;
break;
case (GAP):
state = TRIANGLES;
break;
case (TRIANGLES):
sscanf(data_line, "%d %d %d",&((*obj)-
triangle_count += 1;tri[triangle_count].v0),&((*obj)->tri[triangle_count].v1),&((*obj)-
tri[triangle_count].v2));
if (triangle_count >= (*obj)->ntri )
state = LAST_LINE;
break;
}
}
return
0;
}
++++++++++++++++++++++++++++++++++++++++++++++
/* test.c */
#include "common.h"
void test_vector(void)
{
vector *a;
vector *b;
a = malloc(sizeof(vector));
b = malloc(sizeof(vector));
a->x = 1; a->y = 3; a-> z= 9;
b->x = 2; b->y = 5; b->z = 7;
printf("Vector dot is %f \n",vector_dot(a,b));
}
void test_reader(void)
{
int i;
object o, *obj;
obj = &o;
i = reader(&obj);
if(i == EXIT_FAILURE)
printf("error");
else
{
printf("Printing the vertices and the triangles \n\n");
for(i=0; i said:vert.y, obj->vert.z);
for(i=0; i said:tri.v1,obj->tri.v2);
}
}
int main(void)
{
test_vector();
test_reader();
return 0;
}