-
Notifications
You must be signed in to change notification settings - Fork 0
/
Triangles.h
745 lines (659 loc) · 24 KB
/
Triangles.h
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/**
Copyright 2020 Andrey Kudryavtsev ([email protected])
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appears in all copies and that both the
copyright notice and this permission notice appear in supporting
documentation, and that the same name not be used in advertising or
publicity pertaining to distribution of the software without specific,
written prior permission.
We make no representations about the suitability of this software for any
purpose. It is provided "as is" without any expressed or implied warranty.
*/
#pragma once
#include "Types.h"
#include "Vector.h"
#include <assert.h>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;
// output all nodes and indices to console in DEBUG
//#define DEBUG_ALL
// Convert string to uppercase
std::string upCase(std::string str);
// Trim string from left by deleting all chars
std::string trimLeft(std::string str, std::string chars);
// Trim string from right by deleting all chars
std::string trimRight(std::string str, std::string chars);
// Trim string from left and right by deleting chars
std::string trim(std::string str, std::string chars);
// Parse string by divider with from/to positions as output
int parseWords(std::string str, char divider, int pos1[], int pos2[], int maxcount);
/**
Class TTriangles to load/save STL files
---------------------------------------
https://en.wikipedia.org/wiki/STL_(file_format)
Simple templated class to keep a surface as a set of 3D triangles represented by node coordinates <I>coords</I>
and connectivity array <I>corners</I> (3 coordinate indices for every triangle (face)). So, the
three coordinates for an i-th triangle can be extracted as
coord0 = coords[corners[i * 3]];
coord1 = coords[corners[i * 3 + 1]];
coord2 = coords[corners[i * 3 + 2]];
Triangles can be loaded and saved from/into STL files. No checks about node numeration :
it is supposed that face normal is defined by counter-clockwise numeration of nodes when
looking from the normal sharp end (see faceNormal()).
When loading, duplicate nodes are excluded with a tolerance and face corners renumbered.
The tolerance specified in loadSTL() is important, it must be large enough - it is
used in exclusion of duplicate coordinates. If tolerance too large, degenerated triangles
may appear; if too small - duplicate nodes may not be all excluded.
STL files in the binary form may contain multiple parts by simply glueing multiple STL files
together; there is a provision in the code for that; not well tested though. Binary STL
files contain floats in only 4-byte format.
In text STL files the code selects proper number of digits in text representation for
templated float and double classes.
Using fopen() on binary files is a little bit old, and VS compiler wants #define _CRT_SECURE_NO_WARNINGS
mentioned in defines.h. But text input uses fstream.
*/
template <class T> class TTriangles {
public:
// node coordinates
std::vector<TVector<T> > coords;
// corner indices into vectors, 3 per face (triangle)
std::vector<LINT> corners;
// constructor(s)
TTriangles() = default;
// destructor
~TTriangles() = default;
// number of triangles (faces)
size_t numFaces() const;
// load tris from STL file, exclude duplicate nodes with
// tolerance and renumber corners
bool loadSTL(const std::string filename, std::string &partname, bool &binary, T tolerance);
// save as STL file
bool saveSTL(const std::string filename, const std::string partname, bool binary) const;
// save triangles into OBJ file
bool saveOBJ(const std::string filename, const std::string partname) const;
private:
// add flat triangle of three corners
// like that from STL file; call coords.reserve()
// before adding.
// NB to avoid checks (much faster), set tolerance to zero
bool addTri(TVector<T> v0, TVector<T> v1, TVector<T> v2, T tolerance);
// build connectivity array (with unique corners) by exclusion of
// duplicate coords and renumbering node indices (corners)
bool buildConnectivityArray(T tolerance);
// get tri normal
TVector<T> faceNormal(size_t faceNo) const;
// save as STL file
bool saveSTL(FILE *fp, std::string partname, bool binary) const;
// save triangles into OBJ file
bool saveOBJ(FILE *fp, const std::string partname) const;
};
template<class T> size_t TTriangles<T>::numFaces() const
{
assert(corners.size() % 3 == 0);
return corners.size() / 3;
}
template<class T> bool TTriangles<T>::addTri(TVector<T> v0, TVector<T> v1, TVector<T> v2, T tolerance)
{
// avoid checks
if (tolerance > T(0.0))
{
TVector<T> v01 = v1 - v0;
TVector<T> v12 = v2 - v1;
TVector<T> v20 = v0 - v2;
// check degeneration
T len0 = !v01;
T len1 = !v12;
T len2 = !v20;
if (len0 <= tolerance)
return false;
if (len1 <= tolerance)
return false;
if (len2 <= tolerance)
return false;
}
// connectivity array (with coord duplicates)
corners.push_back(coords.size());
corners.push_back(coords.size() + 1);
corners.push_back(coords.size() + 2);
// coordinates contain all three nodes
coords.push_back(v0);
coords.push_back(v1);
coords.push_back(v2);
return true;
}
template<class T> bool TTriangles<T>::saveSTL(FILE *fp, std::string partname, bool binary) const
{
if (binary)
{
size_t error = 0;
char header[80] = {0};
// remove "solid" if any
std::string name = upCase(partname);
if (name.substr(0,5) == "SOLID")
{
// spoil first character by replacing by space
partname[0] = ' ';
}
// limit name length by 80 characters
if (partname.length() > 80)
partname.erase(80,std::string::npos);
// move string to header
assert(partname.length() <= 80);
memmove(header,partname.c_str(),partname.length());
// save header
error = fwrite(header,sizeof(header),1,fp);
if (error == 0) return false;
// save number of triangles
int numtriangles = static_cast<int>(numFaces());
assert(sizeof(numtriangles) == 4);
error = fwrite(&numtriangles,sizeof(numtriangles),1,fp);
if (error == 0) return false;
// save triangles (vectors must be 4-byte floats)
float v[3] = {0};
for (size_t i = 0; i < numFaces(); i++)
{
size_t i3 = i * 3;
// save normal
TVector<T> normal = faceNormal(i);
v[0] = static_cast<float>(normal.X);
v[1] = static_cast<float>(normal.Y);
v[2] = static_cast<float>(normal.Z);
error = fwrite(v,sizeof(v),1,fp);
if (error == 0) return false;
// save three triangle nodes
TVector<T> co[3];
co[0] = coords[corners[i3]];
co[1] = coords[corners[i3 + 1]];
co[2] = coords[corners[i3 + 2]];
for (int j = 0; j < 3; j++)
{
v[0] = static_cast<float>(co[j].X);
v[1] = static_cast<float>(co[j].Y);
v[2] = static_cast<float>(co[j].Z);
error = fwrite(v,sizeof(v),1,fp);
if (error == 0) return false;
}
// save attribute
short int attr = 0;
assert(sizeof(attr) == 2);
error = fwrite(&attr,sizeof(attr),1,fp);
if (error == 0) return false;
}
return true;
} else
{
int error = 0;
error = fprintf(fp,"solid %s\n",partname.c_str());
if (error < 0) return false;
//assert(coords.size() % 3 == 0);
// format string depends only on current float type
int numdigits = std::numeric_limits<T>::digits10;
std::string digitsstr = to_string(numdigits);
std::string formatstr = std::string("vertex ") + std::string("%.") + digitsstr + "e %." + digitsstr + "e %." + digitsstr + "e\n";
std::string normalstr = std::string("facet normal ") + std::string("%.") + digitsstr + "e %." + digitsstr + "e %." + digitsstr + "e\n";
for (size_t i = 0; i < numFaces(); i++)
{
size_t i3 = i * 3;
TVector<T> normal = faceNormal(i);
error = fprintf(fp,normalstr.c_str(),normal.X,normal.Y,normal.Z);
if (error < 0) return false;
error = fprintf(fp,"outer loop\n");
if (error < 0) return false;
// save three triangle nodes
TVector<T> co[3];
co[0] = coords[corners[i3]];
co[1] = coords[corners[i3 + 1]];
co[2] = coords[corners[i3 + 2]];
error = fprintf(fp,formatstr.c_str(),co[0].X,co[0].Y,co[0].Z);
if (error < 0) return false;
error = fprintf(fp,formatstr.c_str(),co[1].X,co[1].Y,co[1].Z);
if (error < 0) return false;
error = fprintf(fp,formatstr.c_str(),co[2].X,co[2].Y,co[2].Z);
if (error < 0) return false;
error = fprintf(fp,"endloop\n");
if (error < 0) return false;
error = fprintf(fp,"endfacet\n");
if (error < 0) return false;
}
error = fprintf(fp,"endsolid %s\n",partname.c_str());
if (error < 0) return false;
return true;
}
}
template<class T> bool TTriangles<T>::saveSTL(const std::string filename, const std::string partname,
bool binary) const
{
FILE *fp = fopen(filename.c_str(),"wb");
bool res = saveSTL(fp,partname,binary);
fclose(fp);
return res;
}
template<class T> bool TTriangles<T>::saveOBJ(FILE *fp, const std::string partname) const
{
// we save only corners
assert(coords.size() > 0);
int error = 0;
// header
error = fprintf(fp,"# %s\n",partname.c_str());
if (error < 0) return false;
// save coordinates
error = fprintf(fp,"\n# coordinates\n\n");
if (error < 0) return false;
// format string depends only on current float type
int numdigits = std::numeric_limits<T>::digits10;
std::string digitsstr = to_string(numdigits);
std::string formatstr = std::string("v ") + std::string("%.") + digitsstr + "e %." + digitsstr + "e %." + digitsstr + "e\n";
for (size_t i = 0; i < coords.size(); i++)
{
error = fprintf(fp,formatstr.c_str(),coords[i].X,coords[i].Y,coords[i].Z);
if (error < 0) return false;
}
// save coordinates
error = fprintf(fp,"\n# faces\n\n");
if (error < 0) return false;
for (size_t i = 0; i < numFaces(); i++)
{
size_t i3 = i * 3;
size_t i0 = corners[i3];
size_t i1 = corners[i3 + 1];
size_t i2 = corners[i3 + 2];
error = fprintf(fp,"f %zd %zd %zd\n",i0 + 1,i1 + 1,i2 + 1);
if (error < 0) return false;
}
return true;
}
template<class T> bool TTriangles<T>::saveOBJ(const std::string filename, const std::string partname) const
{
FILE *fp = fopen(filename.c_str(),"wb");
bool res = saveOBJ(fp,partname);
fclose(fp);
return res;
}
template<class T> bool TTriangles<T>::loadSTL(const std::string filename, std::string &partname,
bool &binary, T tolerance)
{
FILE *fp = fopen(filename.c_str(),"rb");
if (fp == NULL) return false;
// result
bool OK = false;
// read first bytes to find out if the file is binary or text
binary = false;
char header[200];
size_t bytesread = fread(header,1,sizeof(header),fp);
OK = (bytesread > 0);
if (!OK)
{
fclose(fp);
return false;
}
// text file will contain "solid" and a number of LFs in 200 bytes
// truncate header and use std::string to find "solid"
header[199] = 0;
auto sheader = std::string(header);
// "solid" starting from zero byte
bool solidfound = (sheader.find("solid",0,5) == 0) ||
(sheader.find("SOLID",0,5) == 0);
// count number of LFs
int LFcount = 0;
for (int i = 0; i < bytesread; i++)
{
if (header[i] == 10) LFcount++;
}
// 100% the file is ASCII
binary = !(solidfound && LFcount > 0);
// file is binary just seek its beginning
if (binary)
{
partname = std::string(header);
// go to start of file
OK = (fseek(fp,0,SEEK_SET) == 0);
// read non-standard file as many files glued together
while (!feof(fp))
{
// all int-s are 32-bit in STL file
assert(sizeof(int) == 4);
// skip header
OK = OK && (fseek(fp,80,SEEK_CUR) == 0);
// read number of triangles
int numtriangles = 0;
OK = OK && (fread(&numtriangles,1,sizeof(numtriangles),fp) == sizeof(numtriangles));
if (!OK)
{
fclose(fp);
// OK, if some triangles already loaded
bool ok = buildConnectivityArray(tolerance);
return ok;
}
OK = (numtriangles > 0);
if (!OK)
{
fclose(fp);
// OK, if some triangles already loaded
bool ok = buildConnectivityArray(tolerance);
return ok;
}
// read all triangles
assert(sizeof(float) == 4);
float xyz[3];
for (int i = 0; i < numtriangles; i++)
{
//TVector<T> normal;
OK = (fread(xyz,1,sizeof(xyz),fp) == sizeof(xyz));
if (!OK)
{
fclose(fp);
return false;
}
//normal.X = static_cast<T>(xyz[0]);
//normal.Y = static_cast<T>(xyz[1]);
//normal.Z = static_cast<T>(xyz[2]);
//normal = +normal;
// three vertices
TVector<T> v[3];
for (int j = 0; j < 3; j++)
{
OK = (fread(xyz,1,sizeof(xyz),fp) == sizeof(xyz));
if (!OK)
{
fclose(fp);
return false;
}
v[j].X = static_cast<T>(xyz[0]);
v[j].Y = static_cast<T>(xyz[1]);
v[j].Z = static_cast<T>(xyz[2]);
}
addTri(v[0],v[1],v[2],tolerance);
// 2 bytes of unused data
short int temp = 0;
OK = (fread(&temp,1,2,fp) == 2);
if (!OK)
{
fclose(fp);
return false;
}
}
}
fclose(fp);
// remove duplicate nodes and renumber corners
bool ok = buildConnectivityArray(tolerance);
return ok;
} else
// ASCII
{
// close binary file and open as text
fclose(fp);
std::ifstream file;
std::ios_base::openmode oMode(std::ios::in|std::ios::binary);
file.open(filename.c_str(), oMode);
if (!file.is_open())
{
return false;
}
// line by line...
// loop theoretically may contain more than 3 nodes but
// it does not seem possible as binary version does not allow
// it at all
int nodecount = 0;
TVector<T> normal;
TVector<T> v[3];
while (!file.eof())
{
std::string line;
std::getline(file,line);
// last line might well be without ending (CR)/LF
if (file.fail()) {
break;
}
// trim line
line = trim(line," \n\r\t");
// skip empty
if (line.length() == 0)
continue;
// original line
std::string original = line;
// uppercase
line = upCase(line);
// lines starting with SOLID
if (line.substr(0,5) == "SOLID")
{
if (original.length() > 6)
{
// store part name
partname = original.substr(6);
partname = trim(partname," \n\r\t");
}
continue;
}
// lines starting with ENDSOLID
if (line.substr(0,8) == "ENDSOLID")
{
continue;
}
if (line == "ENDFACET")
{
} else if (line.substr(0,5) == "FACET")
{
// normal for this facet, this line is expected to be like
// "facet normal -0.7074 0.0 0.7074"
int pos1[100],pos2[100];
int numwords = parseWords(line,' ',pos1,pos2,100);
// skip this line
if (numwords != 5)
{
normal = TVector<T>();
// error, issue warning, do not return
continue;
}
// do not parse normal
} else if (line == "OUTER LOOP")
{
nodecount = 0;
} else if (line == "ENDLOOP")
{
if (nodecount >= 3)
{
// add triangle
addTri(v[0],v[1],v[2],tolerance);
} else
{
// Wrong count of nodes
// error, issue warning, do not return
}
// "vertex -1.0 0.5 0.33"
} else if (line.substr(0,6) == "VERTEX")
{
int pos1[100],pos2[100];
int numwords = parseWords(line,' ',pos1,pos2,100);
// "vertex" line contains less than 3 coordinates, take only
// first available coordinates, others left zeroes
if (nodecount < 3)
{
if (numwords < 4)
{
if (numwords == 2)
{
v[nodecount].X = static_cast<T>(atof(line.substr(pos1[1],(pos2[1] - pos1[1] + 1)).c_str()));
v[nodecount].Y = T(0.0);
v[nodecount].Z = T(0.0);
} if (numwords == 3)
{
v[nodecount].X = static_cast<T>(atof(line.substr(pos1[1],(pos2[1] - pos1[1] + 1)).c_str()));
v[nodecount].Y = static_cast<T>(atof(line.substr(pos1[2],(pos2[2] - pos1[2] + 1)).c_str()));
v[nodecount].Z = T(0.0);
}
// error, issue warning, do not return
} else
{
v[nodecount].X = static_cast<T>(atof(line.substr(pos1[1],(pos2[1] - pos1[1] + 1)).c_str()));
v[nodecount].Y = static_cast<T>(atof(line.substr(pos1[2],(pos2[2] - pos1[2] + 1)).c_str()));
v[nodecount].Z = static_cast<T>(atof(line.substr(pos1[3],(pos2[3] - pos1[3] + 1)).c_str()));
}
nodecount++;
}
} else
// unidentified line
{
// error, issue warning, do not return
}
} // while loop on all lines
file.close();
// remove duplicate nodes and renumber corners
bool ok = buildConnectivityArray(tolerance);
return ok;
} // ASCII
return false;
}
// tolerance for comparison routines
static double gtolerance2 = TOLERANCE(float) * TOLERANCE(float);
// comparator for sort()
template <class T> bool comparePoint(TVector<T> p1, TVector<T> p2)
{
if (p1.X < p2.X)
{
return true;
} else if (p1.X > p2.X)
{
return false;
} else
{
if (p1.Y < p2.Y)
{
return true;
} else if (p1.Y > p2.Y)
{
return false;
} else
{
if (p1.Z < p2.Z)
{
return true;
} else if (p1.Z > p2.Z)
{
return false;
} else
{
return false;
}
}
}
}
// comparator for equality
template <class T> bool equalPoint(TVector<T> p1, TVector<T> p2)
{
T dist2 = (p1 - p2).length2();
return (dist2 < gtolerance2);
}
// exclude duplicates from 3D array
template <class T> static bool removeDupNodes(std::vector<TVector<T> > &vectors,
std::vector<LINT> &replacement, T tolerance)
{
// list is empty
if (vectors.size() < 1) return true;
// just one vector
if (vectors.size() == 1)
{
replacement.push_back(0);
return true;
}
// set tolerance for comparison
gtolerance2 = static_cast<double>(tolerance * tolerance);
// put index number in W
for (size_t i = 0; i < vectors.size(); i++)
{
vectors[i].W = static_cast<T>(i);
}
// replacement after sort
std::vector<LINT> replacement0(vectors.size(),-1);
// erase
std::sort(vectors.begin(), vectors.end(), comparePoint<T>);
for (size_t i = 0; i < vectors.size(); i++)
{
size_t index = ROUND(vectors[i].W);
replacement0[i] = index;
}
// all replacements must be filled up
#ifdef _DEBUG
for (auto r : replacement0)
{
assert(r >= 0);
}
#endif
// make replacement
size_t count = 0;
vectors[0].W = static_cast<T>(count);
for (auto v = vectors.begin() + 1; v != vectors.end(); v++)
{
auto vprev = v - 1;
if (!equalPoint<T>(*vprev,*v))
{
count++;
}
v->W = static_cast<T>(count);
}
replacement.clear();
replacement.resize(vectors.size(),-1);
for (size_t i = 0; i < vectors.size(); i++)
{
size_t index = ROUND(vectors[i].W);
replacement[replacement0[i]] = index;
}
// all replacements must be filled up
#ifdef _DEBUG
for (auto r : replacement)
{
assert(r >= 0);
}
#endif
// exclude
auto unique_end = std::unique(vectors.begin(), vectors.end(), equalPoint<T>);
vectors.erase(unique_end, vectors.end());
return true;
}
template <class T> bool TTriangles<T>::buildConnectivityArray(T tolerance)
{
if (numFaces() == 0)
return false;
assert(coords.size() > 0);
assert(corners.size() > 0);
//assert(tolerance >= 0.00000001);
// make two passes
for (int j = 0; j < 2; j++)
{
// exclude duplicate nodes
std::vector<LINT> replacement;
if (!removeDupNodes(coords,replacement,tolerance))
return false;
// renumber all indices
for (size_t i = 0; i < corners.size(); i++)
{
corners[i] = replacement[corners[i]];
}
}
#ifdef DEBUG_ALL
#ifdef _DEBUG
printf("\nNODES\n");
for (size_t i = 0; i < coords.size(); i++)
{
printf("%lu %f %f %f\n",i,coords[i].X,coords[i].Y,coords[i].Z);
}
printf("\nCORNERS\n");
for (size_t i = 0; i < corners.size(); i += 3)
{
printf("%lu %zd %zd %zd\n",i,corners[i],corners[i + 1],corners[i + 2]);
}
#endif
#endif
return true;
}
template <class T> TVector<T> TTriangles<T>::faceNormal(size_t faceNo) const
{
LINT i0 = corners[faceNo * 3];
LINT i1 = corners[faceNo * 3 + 1];
LINT i2 = corners[faceNo * 3 + 2];
TVector<T> n = +((coords[i1] - coords[i0]) ^ (coords[i2] - coords[i1]));
return n;
}