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
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_GONE 10
#define MAX_POLYGONS 100
typedef struct _v3f
{
float x, y, z;
} v3f;
typedef struct _polygon
{
int poly;
int gone[MAX_GONE];
} polygon;
typedef struct _reindexed
{
int v;
float angle;
} indexed;
v3f QuadVertices[4] = // quad with incorrect vertices ordering (non manifold)
{
{ -1.0f, 0.0f, 0.0f },
{ 1.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f },
{ 0.0f, -1.0f, 0.0f }
};
v3f center;
v3f v1;
v3f v2;
indexed Indexed[4];
polygon Faces[MAX_POLYGONS];
void MakeQuadConnectivity()
{
Faces[0].poly = 4;
Faces[0].gone[0] = 0;
Faces[0].gone[1] = 1;
Faces[0].gone[2] = 2;
Faces[0].gone[3] = 3;
}
void PrintVertices( v3f *v, int num )
{
int i;
for ( i = 0 ; i < num ; i++ )
{
printf("v[%d] = { %.0f , %.0f , %.0f } \n",
i,
v[i].x,
v[i].y,
v[i].z
);
}
}
float Normalize( v3f *v )
{
float dist1, dist2 = 0.0f;
dist2 += v->x * v->x;
dist2 += v->y * v->y;
dist2 += v->z * v->z;
dist1 = 1.0f / sqrt( dist2 );
v->x *= dist1;
v->y *= dist1;
v->z *= dist1;
}
void ComputeAngles(v3f *v, int num, indexed *Idx)
{
int i;
float cosinus, signe, angle;
for ( i = 0 ; i < num ; i++)
{
center.x += v[i].x;
center.y += v[i].y;
center.z += v[i].z;
}
center.x /= num;
center.y /= num;
center.z /= num;
v1.x = v[0].x - center.x;
v1.y = v[0].y - center.y;
v1.z = v[0].z - center.z;
Normalize(&v1);
// printf("v1 = {*%.0f, %.0f, %.0f } \n", v1.x, v1.y, v1.z);
for ( i = 0; i < num ; i++)
{
v2.x = v[i].x - center.x;
v2.y = v[i].y - center.y;
v2.z = v[i].z - center.z;
Normalize(&v2);
cosinus = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
angle = acos(cosinus) * (180.0f / M_PI);
signe = v1.x * v2.y - v1.y * v2.x;
if ( signe < 0.0f )
{
angle += 180.0f;
}
Idx[i].v = i;
Idx[i].angle = angle;
// printf("Angle[%d] = %.0f \n", i, angle);
}
}
int Reindexation( const void *a, const void *b )
{
indexed *pa, *pb;
pa = (indexed *) a;
pb = (indexed *) b;
return pa->angle - pb->angle;
}
void PrintReindexed( v3f *v, int num, indexed *Idx, polygon *p )
{
int i;
qsort( Idx, num, sizeof(indexed), Reindexation);
if ( p ) p->poly = num;
for ( i = 0 ; i < num ; i++ )
{
printf("v[%d -> %d] = {%.0f, %.0f, %.0f } [angle=%.0f] \n",
Idx[i].v,
i,
v[ Idx[i].v ].x,
v[ Idx[i].v ].y,
v[ Idx[i].v ].z,
Idx[i].angle
);
if ( p ) p->gone[i] = Idx[i].v;
}
}
void PrintPolygon( polygon * p)
{
int i;
printf("\nPolygon[%d] = [ ", p->poly);
for ( i = 0 ; i < p->poly ; i++ )
{
printf("%d ", p->gone[i] );
}
printf(" ] \n");
}
int main( int argc , char **argv )
{
MakeQuadConnectivity();
printf("Avant reindexation \n");
PrintVertices(QuadVertices, 4);
printf("\nRéindexation ...\n\n");
ComputeAngles(QuadVertices, 4, Indexed);
PrintReindexed(QuadVertices, 4, Indexed, &Faces[0] );
PrintPolygon( &Faces[0] );
return 0;
} |
Partager