Welcome to mirror list, hosted at ThFree Co, Russian Federation.

shared.cpp « admesh « src - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e9f075498295cb85078460962692064bdbc3652b (plain)
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
/*  ADMesh -- process triangulated solid meshes
 *  Copyright (C) 1995, 1996  Anthony D. Martin <amartin@engr.csulb.edu>
 *  Copyright (C) 2013, 2014  several contributors, see AUTHORS
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *  Questions, comments, suggestions, etc to
 *           https://github.com/admesh/admesh/issues
 */

#include <stdlib.h>
#include <string.h>

#include <vector>

#include <boost/nowide/cstdio.hpp>

#include "stl.h"

void stl_invalidate_shared_vertices(stl_file *stl)
{
  	stl->v_indices.clear();
  	stl->v_shared.clear();
}

void stl_generate_shared_vertices(stl_file *stl)
{
	// 3 indices to vertex per face
	stl->v_indices.assign(stl->stats.number_of_facets, v_indices_struct());
	// Shared vertices (3D coordinates)
	stl->v_shared.clear();
	stl->v_shared.reserve(stl->stats.number_of_facets / 2);

	// A degenerate mesh may contain loops: Traversing a fan will end up in an endless loop
	// while never reaching the starting face. To avoid these endless loops, traversed faces at each fan traversal
	// are marked with a unique fan_traversal_stamp.
	unsigned int			  fan_traversal_stamp = 0;
	std::vector<unsigned int> fan_traversal_facet_visited(stl->stats.number_of_facets, 0);

	for (uint32_t facet_idx = 0; facet_idx < stl->stats.number_of_facets; ++ facet_idx) {
		for (int j = 0; j < 3; ++ j) {
			if (stl->v_indices[facet_idx].vertex[j] != -1)
				// Shared vertex was already assigned.
				continue;
			// Create a new shared vertex.
			stl->v_shared.emplace_back(stl->facet_start[facet_idx].vertex[j]);
			// Traverse the fan around the j-th vertex of the i-th face, assign the newly created shared vertex index to all the neighboring triangles in the triangle fan.
			int  facet_in_fan_idx 	= facet_idx;
			bool edge_direction 	= false;
			bool traversal_reversed = false;
			int  vnot      			= (j + 2) % 3;
			// Increase the 
			++ fan_traversal_stamp;
			for (;;) {
				// Next edge on facet_in_fan_idx to be traversed. The edge is indexed by its starting vertex index.
				int next_edge    = 0;
				// Vertex index in facet_in_fan_idx, which is being pivoted around, and which is being assigned a new shared vertex.
				int pivot_vertex = 0;
				if (vnot > 2) {
					// The edge of facet_in_fan_idx opposite to vnot is equally oriented, therefore
					// the neighboring facet is flipped.
			  		if (! edge_direction) {
			    		pivot_vertex = (vnot + 2) % 3;
			    		next_edge    = pivot_vertex;			    		
			  		} else {
			    		pivot_vertex = (vnot + 1) % 3;
			    		next_edge    = vnot % 3;
			  		}
			  		edge_direction = ! edge_direction;
				} else {
					// The neighboring facet is correctly oriented.
			  		if (! edge_direction) {
			    		pivot_vertex = (vnot + 1) % 3;
			    		next_edge    = vnot;
			  		} else {
			    		pivot_vertex = (vnot + 2) % 3;
			    		next_edge    = pivot_vertex;
			  		}
				}
				stl->v_indices[facet_in_fan_idx].vertex[pivot_vertex] = stl->v_shared.size() - 1;
				fan_traversal_facet_visited[facet_in_fan_idx] = fan_traversal_stamp;

				// next_edge is an index of the starting vertex of the edge, not an index of the opposite vertex to the edge!
				int next_facet = stl->neighbors_start[facet_in_fan_idx].neighbor[next_edge];
				if (next_facet == -1) {
					// No neighbor going in the current direction.
					if (traversal_reversed) {
						// Went to one limit, then turned back and reached the other limit. Quit the fan traversal.
					    break;
					} else {
						// Reached the first limit. Now try to reverse and traverse up to the other limit.
					    edge_direction        = true;
					    vnot 	         	  = (j + 1) % 3;
					    traversal_reversed    = true;
				    	facet_in_fan_idx      = facet_idx;
					}
				} else if (next_facet == facet_idx) {
					// Traversed a closed fan all around.
//					assert(! traversal_reversed);
					break;
				} else if (next_facet >= (int)stl->stats.number_of_facets) {
					// The mesh is not valid!
					// assert(false);
					break;
				} else if (fan_traversal_facet_visited[next_facet] == fan_traversal_stamp) {
					// Traversed a closed fan all around, but did not reach the starting face.
					// This indicates an invalid geometry (non-manifold).
					//assert(false);
					break;
				} else {
					// Continue traversal.
					// next_edge is an index of the starting vertex of the edge, not an index of the opposite vertex to the edge!
					vnot = stl->neighbors_start[facet_in_fan_idx].which_vertex_not[next_edge];
					facet_in_fan_idx = next_facet;
				}
			}
		}
	}
}

bool stl_write_off(stl_file *stl, const char *file)
{
	/* Open the file */
	FILE *fp = boost::nowide::fopen(file, "w");
	if (fp == nullptr) {
		char *error_msg = (char*)malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */
		sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing", file);
		perror(error_msg);
		free(error_msg);
		return false;
	}

	fprintf(fp, "OFF\n");
	fprintf(fp, "%d %d 0\n", stl->v_shared.size(), stl->stats.number_of_facets);
	for (int i = 0; i < stl->v_shared.size(); ++ i)
		fprintf(fp, "\t%f %f %f\n", stl->v_shared[i](0), stl->v_shared[i](1), stl->v_shared[i](2));
	for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i)
		fprintf(fp, "\t3 %d %d %d\n", stl->v_indices[i].vertex[0], stl->v_indices[i].vertex[1], stl->v_indices[i].vertex[2]);
	fclose(fp);
	return true;
}

bool stl_write_vrml(stl_file *stl, const char *file)
{
	/* Open the file */
  	FILE *fp = boost::nowide::fopen(file, "w");
	if (fp == nullptr) {
  		char *error_msg = (char*)malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */
		sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing", file);
		perror(error_msg);
		free(error_msg);
		return false;
	}

	fprintf(fp, "#VRML V1.0 ascii\n\n");
	fprintf(fp, "Separator {\n");
	fprintf(fp, "\tDEF STLShape ShapeHints {\n");
	fprintf(fp, "\t\tvertexOrdering COUNTERCLOCKWISE\n");
	fprintf(fp, "\t\tfaceType CONVEX\n");
	fprintf(fp, "\t\tshapeType SOLID\n");
	fprintf(fp, "\t\tcreaseAngle 0.0\n");
	fprintf(fp, "\t}\n");
	fprintf(fp, "\tDEF STLModel Separator {\n");
	fprintf(fp, "\t\tDEF STLColor Material {\n");
	fprintf(fp, "\t\t\temissiveColor 0.700000 0.700000 0.000000\n");
	fprintf(fp, "\t\t}\n");
	fprintf(fp, "\t\tDEF STLVertices Coordinate3 {\n");
	fprintf(fp, "\t\t\tpoint [\n");

	int i = 0;
	for (; i + 1 < stl->v_shared.size(); ++ i)
		fprintf(fp, "\t\t\t\t%f %f %f,\n", stl->v_shared[i](0), stl->v_shared[i](1), stl->v_shared[i](2));
	fprintf(fp, "\t\t\t\t%f %f %f]\n", stl->v_shared[i](0), stl->v_shared[i](1), stl->v_shared[i](2));
	fprintf(fp, "\t\t}\n");
	fprintf(fp, "\t\tDEF STLTriangles IndexedFaceSet {\n");
	fprintf(fp, "\t\t\tcoordIndex [\n");

	for (int i = 0; i + 1 < (int)stl->stats.number_of_facets; ++ i)
		fprintf(fp, "\t\t\t\t%d, %d, %d, -1,\n", stl->v_indices[i].vertex[0], stl->v_indices[i].vertex[1], stl->v_indices[i].vertex[2]);
	fprintf(fp, "\t\t\t\t%d, %d, %d, -1]\n", stl->v_indices[i].vertex[0], stl->v_indices[i].vertex[1], stl->v_indices[i].vertex[2]);
	fprintf(fp, "\t\t}\n");
	fprintf(fp, "\t}\n");
	fprintf(fp, "}\n");
	fclose(fp);
	return true;
}

bool stl_write_obj(stl_file *stl, const char *file)
{

  	FILE *fp = boost::nowide::fopen(file, "w");
  	if (fp == nullptr) {
    	char* error_msg = (char*)malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */
    	sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing", file);
    	perror(error_msg);
    	free(error_msg);
    	return false;
  	}

	for (size_t i = 0; i < stl->v_shared.size(); ++ i)
    	fprintf(fp, "v %f %f %f\n", stl->v_shared[i](0), stl->v_shared[i](1), stl->v_shared[i](2));
  	for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i)
    	fprintf(fp, "f %d %d %d\n", stl->v_indices[i].vertex[0]+1, stl->v_indices[i].vertex[1]+1, stl->v_indices[i].vertex[2]+1);
  	fclose(fp);
  	return true;
}