-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate_2d_sheet.c
245 lines (196 loc) · 7.04 KB
/
create_2d_sheet.c
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
243
244
245
#include <volume_io/internal_volume_io.h>
#include <bicpl.h>
#include <special_geometry.h>
private void create_2d_coordinates(
polygons_struct *polygons );
int main(
int argc,
char *argv[] )
{
STRING src_filename, dest_filename;
int n_objects;
File_formats format;
object_struct **object_list;
polygons_struct *polygons;
initialize_argument_processing( argc, argv );
if( !get_string_argument( NULL, &src_filename ) ||
!get_string_argument( NULL, &dest_filename ) )
{
print_error( "Usage: %s input.obj output.obj\n", argv[0] );
return( 1 );
}
if( input_graphics_file( src_filename, &format, &n_objects,
&object_list ) != OK || n_objects != 1 ||
get_object_type(object_list[0]) != POLYGONS )
return( 1 );
polygons = get_polygons_ptr( object_list[0] );
create_2d_coordinates( polygons );
(void) output_graphics_file( dest_filename, format, 1, object_list );
return( 0 );
}
private int create_path_between_nodes(
int n_points,
Point points[],
int n_neighbours[],
int *neighbours[],
int start_point,
int end_point,
float vertical[],
int *path_ptr[] )
{
int n_path, *path, current, best_neigh, n, neigh;
float dist, best_dist;
n_path = 0;
path = NULL;
ADD_ELEMENT_TO_ARRAY( path, n_path, end_point, DEFAULT_CHUNK_SIZE );
current = end_point;
do
{
best_dist = 0.0f;
for_less( neigh, 0, n_neighbours[current] )
{
n = neighbours[current][neigh];
dist = (float) distance_between_points( &points[current],
&points[n] ) +
vertical[n];
if( neigh == 0 || dist < best_dist )
{
best_dist = dist;
best_neigh = n;
}
}
current = best_neigh;
ADD_ELEMENT_TO_ARRAY( path, n_path, current, DEFAULT_CHUNK_SIZE );
}
while( current != start_point );
*path_ptr = path;
return( n_path );
}
private void compute_point_position(
Real x1,
Real dist02,
Real dist12,
Point *point )
{
Real x, y, rem;
x = (x1 * x1 + dist02 * dist02 - dist12 * dist12) / (2.0 * x1);
rem = dist02 * dist02 - x * x;
if( rem <= 0.0 )
y = 0.0;
else
y = sqrt( dist02 * dist02 - x * x );
fill_Point( *point, x, y, 0.0 );
}
private void create_2d_coordinates(
polygons_struct *polygons )
{
int point, *n_neighbours, **neighbours, origin, opposite;
int *path, *vertices_in_path, n, path_index, p0, p1, p2;
int current_point, n_points, start_point, vertex;
int path_size;
Real axis_length, dist1, dist2;
float *distances_from_origin, *distances_from_opposite;
Smallest_int *interior_flags;
progress_struct progress;
QUEUE_STRUCT( int ) queue;
check_polygons_neighbours_computed( polygons );
create_polygon_point_neighbours( polygons, TRUE, &n_neighbours,
&neighbours, &interior_flags, NULL );
n_points = polygons->n_points;
for_less( origin, 0, n_points )
{
if( !interior_flags[origin] )
break;
}
if( origin >= n_points )
{
print_error( "Surface is not an open sheet.\n" );
return;
}
print( "Getting distances from origin.\n" );
ALLOC( distances_from_origin, n_points );
(void) compute_distances_from_point( polygons, n_neighbours, neighbours,
&polygons->points[origin],
-1, -1.0, FALSE,
distances_from_origin, NULL );
opposite = -1;
for_less( point, 0, n_points )
{
if( !interior_flags[point] &&
(opposite < 0 ||
distances_from_origin[point] > distances_from_origin[opposite]) )
{
opposite = point;
}
}
print( "Getting distances from opposite.\n" );
ALLOC( distances_from_opposite, n_points );
(void) compute_distances_from_point( polygons, n_neighbours, neighbours,
&polygons->points[opposite],
-1, -1.0, FALSE,
distances_from_opposite, NULL );
path_size = create_path_between_nodes( polygons->n_points, polygons->points,
n_neighbours, neighbours, origin,
opposite, distances_from_origin, &path );
ALLOC( vertices_in_path, n_points );
for_less( point, 0, n_points )
vertices_in_path[point] = FALSE;
for_less( path_index, 0, path_size )
vertices_in_path[path[path_index]] = TRUE;
for_less( path_index, 1, path_size-1 )
{
p0 = path[path_index-1];
p1 = path[path_index];
p2 = path[path_index+1];
for_less( vertex, 0, n_neighbours[p1] )
{
if( neighbours[p1][vertex] == p0 )
break;
}
vertex = (vertex + 1) % n_neighbours[p1];
start_point = neighbours[p1][vertex];
if( start_point != p2 && !vertices_in_path[start_point] )
break;
}
if( path_index >= path_size-1 )
{
print_error( "Cannot find path\n" );
return;
}
INITIALIZE_QUEUE( queue );
vertices_in_path[start_point] = TRUE;
INSERT_IN_QUEUE( queue, start_point );
while( !IS_QUEUE_EMPTY(queue) )
{
REMOVE_FROM_QUEUE( queue, current_point );
for_less( n, 0, n_neighbours[current_point] )
{
if( !vertices_in_path[neighbours[current_point][n]] )
{
vertices_in_path[neighbours[current_point][n]] = TRUE;
INSERT_IN_QUEUE( queue, neighbours[current_point][n] );
}
}
}
DELETE_QUEUE( queue );
initialize_progress_report( &progress, FALSE, polygons->n_points,
"Computing Coords" );
axis_length = (Real) distances_from_origin[opposite];
for_less( point, 0, polygons->n_points )
{
dist1 = (Real) distances_from_origin[point];
dist2 = (Real) distances_from_opposite[point];
compute_point_position( axis_length, dist1, dist2,
&polygons->points[point] );
if( !vertices_in_path[point] )
Point_y(polygons->points[point]) =-Point_y(polygons->points[point]);
update_progress_report( &progress, point+1 );
}
terminate_progress_report( &progress );
delete_polygon_point_neighbours( polygons, n_neighbours, neighbours,
NULL, NULL );
FREE( vertices_in_path );
FREE( path );
FREE( distances_from_origin );
FREE( distances_from_opposite );
}