20
20
// Please report all bugs and problems to <gmsh@geuz.org>.
23
24
#include "MVertex.h"
28
#if defined(HAVE_GMSH_EMBEDDED)
29
# include "GmshEmbedded.h"
25
34
int MVertex::_globalNum = 0;
26
35
double MVertexLessThanLexicographic::tolerance = 1.e-6;
37
bool MVertexLessThanLexicographic::operator()(const MVertex *v1, const MVertex *v2) const
39
if(v1->x() - v2->x() > tolerance) return true;
40
if(v1->x() - v2->x() < -tolerance) return false;
41
if(v1->y() - v2->y() > tolerance) return true;
42
if(v1->y() - v2->y() < -tolerance) return false;
43
if(v1->z() - v2->z() > tolerance) return true;
28
47
void MVertex::writeMSH(FILE *fp, bool binary, double scalingFactor)
30
if(_num < 0) return; // negative vertices are never saved
49
if(_index < 0) return; // negative index vertices are never saved
33
fprintf(fp, "%d %.16g %.16g %.16g\n", _num,
52
fprintf(fp, "%d %.16g %.16g %.16g\n", _index,
39
fwrite(&_num, sizeof(int), 1, fp);
58
fwrite(&_index, sizeof(int), 1, fp);
40
59
double data[3] = {x() * scalingFactor, y() * scalingFactor, z() * scalingFactor};
41
60
fwrite(data, sizeof(double), 3, fp);
45
64
void MVertex::writeMSH(FILE *fp, double version, bool binary, int num,
46
int elementary, int physical)
65
int elementary, int physical)
49
68
fprintf(fp, "%d 15", num);
51
70
fprintf(fp, " %d %d 1", physical, elementary);
53
72
fprintf(fp, " 2 %d %d", physical, elementary);
54
fprintf(fp, " %d\n", _num);
73
fprintf(fp, " %d\n", _index);
57
76
int tags[4] = {num, physical, elementary, 0};
58
77
fwrite(tags, sizeof(int), 4, fp);
59
int verts[1] = {_num};
78
int verts[1] = {_index};
60
79
fwrite(verts, sizeof(int), 1, fp);
64
83
void MVertex::writeVRML(FILE *fp, double scalingFactor)
66
if(_num < 0) return; // negative vertices are never saved
85
if(_index < 0) return; // negative index vertices are never saved
68
87
fprintf(fp, "%.16g %.16g %.16g,\n",
69
x() * scalingFactor, y() * scalingFactor, z() * scalingFactor);
88
x() * scalingFactor, y() * scalingFactor, z() * scalingFactor);
72
91
void MVertex::writeUNV(FILE *fp, double scalingFactor)
74
if(_num < 0) return; // negative vertices are never saved
93
if(_index < 0) return; // negative index vertices are never saved
77
96
int displacement_coord_sys = 1;
79
fprintf(fp, "%10d%10d%10d%10d\n", _num, coord_sys, displacement_coord_sys, color);
98
fprintf(fp, "%10d%10d%10d%10d\n", _index, coord_sys, displacement_coord_sys, color);
80
99
// hack to print the numbers with "D+XX" exponents
82
101
sprintf(tmp, "%25.16E%25.16E%25.16E\n", x() * scalingFactor,
83
y() * scalingFactor, z() * scalingFactor);
102
y() * scalingFactor, z() * scalingFactor);
84
103
for(unsigned int i = 0; i < strlen(tmp); i++) if(tmp[i] == 'E') tmp[i] = 'D';
88
107
void MVertex::writeMESH(FILE *fp, double scalingFactor)
90
if(_num < 0) return; // negative vertices are never saved
109
if(_index < 0) return; // negative index vertices are never saved
92
111
fprintf(fp, " %20.14G %20.14G %20.14G %d\n",
93
x() * scalingFactor, y() * scalingFactor, z() * scalingFactor, 0);
112
x() * scalingFactor, y() * scalingFactor, z() * scalingFactor, 0);
96
static void double_to_char8(double val, char *str){
115
static void double_to_char8(double val, char *str)
98
118
sprintf(str, "%.2E", val);
99
119
else if(val >= 1.e-3)
106
126
sprintf(str, "%f", val);
108
128
sprintf(str, "%.1E", val);
131
// Windows uses 3 digits in the exponent (which apparently does not
132
// conform with ANSI C): remove the extra 0
133
if(strlen(str) == 9 && (str[4] == 'E' || str[5] == 'E')){
112
142
void MVertex::writeBDF(FILE *fp, int format, double scalingFactor)
114
if(_num < 0) return; // negative vertices are never saved
144
if(_index < 0) return; // negative index vertices are never saved
116
146
char xs[17], ys[17], zs[17];
117
147
double x1 = x() * scalingFactor;
121
151
// free field format (max 8 char per field, comma separated, 10 per line)
122
152
double_to_char8(x1, xs); double_to_char8(y1, ys); double_to_char8(z1, zs);
123
fprintf(fp, "GRID,%d,%d,%s,%s,%s\n", _num, 0, xs, ys, zs);
153
fprintf(fp, "GRID,%d,%d,%s,%s,%s\n", _index, 0, xs, ys, zs);
125
155
else if(format == 1){
126
156
// small field format (8 char par field, 10 per line)
127
157
double_to_char8(x1, xs); double_to_char8(y1, ys); double_to_char8(z1, zs);
128
fprintf(fp, "GRID %-8d%-8d%-8s%-8s%-8s\n", _num, 0, xs, ys, zs);
158
fprintf(fp, "GRID %-8d%-8d%-8s%-8s%-8s\n", _index, 0, xs, ys, zs);
131
161
// large field format (8 char first/last field, 16 char middle, 6 per line)
132
fprintf(fp, "GRID* %-16d%-16d%-16.9G%-16.9G*N%-6d\n", _num, 0, x1, y1, _num);
133
fprintf(fp, "*N%-6d%-16.9G\n", _num, z1);
162
fprintf(fp, "GRID* %-16d%-16d%-16.9G%-16.9G*N%-6d\n", _index, 0, x1, y1, _index);
163
fprintf(fp, "*N%-6d%-16.9G\n", _index, z1);
167
std::set<MVertex*, MVertexLessThanLexicographic>::iterator
168
MVertex::linearSearch(std::set<MVertex*, MVertexLessThanLexicographic> &pos)
170
double tol = MVertexLessThanLexicographic::tolerance;
171
for(std::set<MVertex*, MVertexLessThanLexicographic>::iterator it = pos.begin();
172
it != pos.end(); ++it){
173
if(distance(*it) < tol) return it;
178
void parametricCoordinates(const MVertex *ver, const GFace *gf, double &u, double &v)
180
GEntity *ge = ver->onWhat();
182
ver->getParameter(0, u);
183
ver->getParameter(1, v);
185
else if(ge->dim() == 1){
187
ver->getParameter(0, t);
188
GEdge *ged = dynamic_cast<GEdge*>(ge);
189
SPoint2 p = ged->reparamOnFace((GFace*)gf, t, 1);
194
GVertex *gver = dynamic_cast<GVertex*>(ge);
195
SPoint2 p = gver->reparamOnFace((GFace*)gf, 1);