-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStruct2.h
More file actions
242 lines (207 loc) · 6.03 KB
/
Struct2.h
File metadata and controls
242 lines (207 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#ifndef STRUCT2_H
#define STRUCT2_H
#include <math.h> // Math Library Header File
#include <FL/gl.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct{
public :
float x, y, z;
} POLYGON_VERTEX;
class POLYGON_LIST
{
public :
POLYGON_VERTEX *point_list;
int point_number;
int statut; //statut 0 = open statut 1 =close
POLYGON_LIST()
{
this->point_number = 0;
this->statut = 0;
}
int POLYGON_POINT_INSIDE ( POLYGON_VERTEX P)
{
int cn = 0; // the crossing number counter
// loop through all edges of the polygon
for (int i=0; i<point_number -1; i++) { // edge from V[i] to V[i+1]
if (((point_list[i].y <= P.y) && (point_list[i+1].y > P.y)) // an upward crossing
|| ((point_list[i].y > P.y) && (point_list[i+1].y <= P.y))) { // a downward crossing
// compute the actual edge-ray intersect x-coordinate
float vt = (float)(P.y - point_list[i].y) / (point_list[i+1].y - point_list[i].y);
if (P.x < point_list[i].x + vt * (point_list[i+1].x - point_list[i].x)) // P.x < intersect
++cn; // a valid crossing of y=P.y right of P.x
}
}
//last edge
if (((point_list[0].y <= P.y) && (point_list[point_number -1].y > P.y)) // an upward crossing
|| ((point_list[0].y > P.y) && (point_list[point_number -1].y <= P.y))) { // a downward crossing
// compute the actual edge-ray intersect x-coordinate
float vt = (float)(P.y - point_list[0].y) / (point_list[point_number -1].y - point_list[0].y);
if (P.x < point_list[0].x + vt * (point_list[point_number -1].x - point_list[0].x)) // P.x < intersect
++cn; // a valid crossing of y=P.y right of P.x
}
return (cn&1); // 0 if even (out), and 1 if odd (in)
}
void Polygon_close()
{
if (Polygon_valid())
{
this->statut = 1;
}
else
{
Polygon_init();
//error message
}
}
void Polygon_init()
{
if (this->point_number >0)
{free (this->point_list);}
this->point_number = 0;
this->statut = 0;
}
int Poly_cross (float x1, float y1, float x2, float y2, float u1, float u2, float v1, float v2)
{
float b1;
float b2;
float xi,yi;
float a1,a2;
if ( (x2-x1 ==0)||(u2-u1 ==0))
{return 0;}// Cases were one edge is vertical are avoided...(A and B exceptions)
b1 = (y2-y1)/(x2-x1); //(A)
b2 = (v2-v1)/(u2-u1); // (B)
if ( b2-b1 ==0)
{return 0;}// Cases were two edges are parallel are avoided. (C exception)
a1 = y1-b1*x1;
a2 = v1-b2*u1;
xi = - (a1-a2)/(b1-b2); //(C)
yi = a1+b1*xi ;
if (
((x1-xi)*(xi-x2)>=0)
&&( (u1-xi)*(xi-u2)>=0)
&&( (y1-yi)*(yi-y2)>=0)
&& ((v1-yi)*(yi-v2)>=0)
)
{return 1;}
else {return 0;}
}
int Polygon_valid () // Allocate Memory For Each Object
{
int x1,x2,y1,y2,u1,u2,v1,v2;
// And Defines points
int valid = 1;
if (this->point_number < 3)
{ return 0;}
else
{//return 1;
if (this->point_number ==3)
{
x1 = (int)point_list[0].x;
x2 = (int)point_list[1].x;
y1 = (int)point_list[0].y;
y2 = (int)point_list[1].y;
u1 = (int)point_list[1].x;
u2 = (int)point_list[2].x;
v1 = (int)point_list[1].y;
v2 = (int)point_list[2].y;
//test if paralleles
}
else
{ int i;
for ( i=0; i<point_number -1; i++)
{
x1 = (int)point_list[i].x;
x2 = (int)point_list[i+1].x;
y1 = (int)point_list[i].y;
y2 = (int)point_list[i+1].y;
for (int j = i+2; j<point_number-1 ; j++)
{
fprintf(stderr, "Segment %d - %d\n", i+1, j+1);
u1 = (int)point_list[j].x;
u2 = (int)point_list[j+1].x;
v1 = (int)point_list[j].y;
v2 = (int)point_list[j+1].y;
if (Poly_cross ((float)x1,(float)y1,(float)x2,(float)y2,(float)u1,(float)u2,(float)v1,(float)v2))
{valid =0;}
}
}
//Last edge
x1 = (int)point_list[point_number -1].x;
x2 = (int)point_list[0].x;
y1 = (int)point_list[point_number -1].y;
y2 = (int)point_list[0].y;
for (i = 1; i<point_number -2; i++)
{
u1 = (int)point_list[i].x;
u2 = (int)point_list[i+1].x;
v1 = (int)point_list[i].y;
v2 = (int)point_list[i+1].y;
fprintf(stderr, "Segment %d - %d\n", point_number, i+1);
if (Poly_cross ((float)x1,(float)y1,(float)x2,(float)y2,(float)u1,(float)u2,(float)v1,(float)v2))
{valid =0;}
}
}
}
return valid;
}
void Polygon_add (int x, int y)
{
if (this->statut ==0)
{
this->point_number ++;
if (this->point_number == 1)
{
Polygon_allocate (1);
}
else
{
Polygon_reallocate (this->point_number);
}
this->point_list[this->point_number -1].x = (float)x;
this->point_list[this->point_number -1].y = (float)y;
}
}
void Polygon_add (float x, float y, float z)
{
if (this->statut ==0)
{
this->point_number ++;
if (this->point_number == 1)
{
Polygon_allocate (1);
}
else
{
Polygon_reallocate (this->point_number);
}
this->point_list[this->point_number -1].x = x;
this->point_list[this->point_number -1].y = y;
this->point_list[this->point_number -1].z = z;
}
}
void Polygon_allocate (int n) // Allocate Memory For Each Object
{ // And Defines points
//return 1;
this->point_list = (POLYGON_VERTEX*)malloc(sizeof(POLYGON_VERTEX)*n);
}
void Polygon_reallocate(int n)
{
this->point_list = (POLYGON_VERTEX*)realloc(this->point_list, sizeof(POLYGON_VERTEX)*n);
}
};
class VERTEX2
{
public :
float x, y, z; // X, Y & Z Point coordinate
float nx, ny, nz; // Mean Normal Vector
int *triangles; // Triangle list to which the point belongs
int nbtriangles; //Number of triangles in this list
float area_percent; //Total percentage towards mean configuration
float area_total;
GLfloat color[4],color2[4];
float vx,vy,vz,vn,vx1,vy1,vz1,vx2,vy2,vz2; // Vector normal to normal vector and proportional to landmark distance from corresponding landmark to mean form.
} ; // Called VERTEX
typedef GLfloat Mat[4];
typedef Mat glMatrix[4];
#endif