質問編集履歴

1

ソースコード

2017/11/14 07:36

投稿

windowsaa
windowsaa

スコア16

test CHANGED
File without changes
test CHANGED
@@ -10,802 +10,4 @@
10
10
 
11
11
 
12
12
 
13
- [sorce code]
14
-
15
- ```C++
16
-
17
- #include<iostream>
18
-
19
- #include<gl/glut.h>
20
-
21
- #include"stlreader.h"
22
-
23
-
24
-
25
- #define STL_FILE "C:\Users\owner\Desktop\nsx_concept_LOW.stl"
26
-
27
-
28
-
29
- void display(void);
30
-
31
- void reshape(int w, int h);
32
-
33
- void draw();
34
-
35
- void init();
36
-
37
- void DRAW_XYZ();
38
-
39
-
40
-
41
- Triangle *triangles;
42
-
43
- int num;
44
-
45
- STLreader stlreader;
46
-
47
- //unsigned char key = NULL;
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
- int main(int argc, char *argv[])
56
-
57
- {
13
+ [
58
-
59
- glutInitWindowPosition(128, 128);
60
-
61
- glutInitWindowSize(800, 800);
62
-
63
- glutInit(&argc, argv);
64
-
65
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
66
-
67
- glutCreateWindow("argv[0]");
68
-
69
- init();
70
-
71
- glutDisplayFunc(display);
72
-
73
- glutReshapeFunc(reshape);
74
-
75
- // glutKeyboardFunc(keyboard);
76
-
77
-
78
-
79
- glutMainLoop();
80
-
81
-
82
-
83
- if (triangles)
84
-
85
- delete[] triangles;
86
-
87
-
88
-
89
- return 0;
90
-
91
- }
92
-
93
-
94
-
95
-
96
-
97
- void display(void)
98
-
99
- {
100
-
101
-
102
-
103
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
104
-
105
-
106
-
107
- glLoadIdentity();
108
-
109
- gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //視点の設定
110
-
111
-
112
-
113
- draw(); //描画
114
-
115
-
116
-
117
- glutSwapBuffers(); //ウィンドウに出力
118
-
119
-
120
-
121
- }
122
-
123
-
124
-
125
-
126
-
127
- void reshape(int w, int h)
128
-
129
- {
130
-
131
- glViewport(0, 0, w, h); //ビューポートの設定
132
-
133
-
134
-
135
- glMatrixMode(GL_PROJECTION);
136
-
137
- glLoadIdentity();
138
-
139
- gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0); //視野の設定
140
-
141
- glMatrixMode(GL_MODELVIEW);
142
-
143
- }
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
- void draw()
152
-
153
- {
154
-
155
- if (triangles)
156
-
157
- delete[] triangles;
158
-
159
-
160
-
161
- if (!(num = STLreader::countTriangles(string(STL_FILE))))
162
-
163
-
164
-
165
- return;
166
-
167
-
168
-
169
- triangles = new Triangle[num];
170
-
171
-
172
-
173
- stlreader.readASCIISTLFile(STL_FILE, triangles);
174
-
175
-
176
-
177
- stlreader.readBinarySTLFile(STL_FILE, triangles);
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
- glClear(GL_COLOR_BUFFER_BIT);
186
-
187
-
188
-
189
- DRAW_XYZ();
190
-
191
-
192
-
193
- glBegin(GL_TRIANGLES);
194
-
195
-
196
-
197
- glColor3f(0.0, 0.0, 0.0);
198
-
199
-
200
-
201
- for (int i = 0; i < num; i++)
202
-
203
- {
204
-
205
-
206
-
207
- glNormal3dv(triangles[i].nornVec);
208
-
209
- glVertex3dv(triangles[i].pnt1);
210
-
211
- glVertex3dv(triangles[i].pnt2);
212
-
213
- glVertex3dv(triangles[i].pnt3);
214
-
215
-
216
-
217
- }
218
-
219
-
220
-
221
-
222
-
223
- glEnd();
224
-
225
- glFlush();
226
-
227
-
228
-
229
- }
230
-
231
-
232
-
233
-
234
-
235
- void init()
236
-
237
- {
238
-
239
-
240
-
241
- glClearColor(1.0, 1.0, 1.0, 1.0);
242
-
243
- }
244
-
245
-
246
-
247
- void DRAW_XYZ()
248
-
249
- {
250
-
251
- glBegin(GL_LINES);
252
-
253
-
254
-
255
- glColor3f(0, 1, 0);//x
256
-
257
- glVertex2f(-100, 0);
258
-
259
- glVertex2f(100, 0);
260
-
261
-
262
-
263
- glColor3f(1, 0, 0);//y
264
-
265
- glVertex2f(0, 0);
266
-
267
- glVertex2f(0, 100);
268
-
269
-
270
-
271
- glColor3f(0, 0, 1);//z
272
-
273
- glVertex3f(0, 0, -100);
274
-
275
- glVertex3f(0, 0, 100);
276
-
277
- glEnd();
278
-
279
-
280
-
281
- }
282
-
283
- ```
284
-
285
- C++
286
-
287
- ヘッダーファイルです
288
-
289
- ```C++
290
-
291
- #include <iostream>
292
-
293
- #include <cmath>
294
-
295
- #include <string>
296
-
297
- #include <fstream>
298
-
299
-
300
-
301
- using namespace std;
302
-
303
-
304
-
305
- struct vert
306
-
307
- {
308
-
309
- double pnt1[3];
310
-
311
- double pnt2[3];
312
-
313
- double pnt3[3];
314
-
315
- };
316
-
317
-
318
-
319
- struct vert4
320
-
321
- {
322
-
323
- double pnt1[4];//x,y,z,w
324
-
325
- double pnt2[4];
326
-
327
- double pnt3[4];
328
-
329
- };
330
-
331
-
332
-
333
- struct Triangle {
334
-
335
- double nornVec[3];
336
-
337
- double pnt1[3];
338
-
339
- double pnt2[3];
340
-
341
- double pnt3[3];
342
-
343
- unsigned int index;
344
-
345
- };
346
-
347
-
348
-
349
-
350
-
351
- class STLreader
352
-
353
- {
354
-
355
-
356
-
357
- unsigned int num_tmp_points;//一時的な変数
358
-
359
- unsigned int num_triangles;//三角形の数
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
- public:
368
-
369
- static int countTriangles(string filename)
370
-
371
- {
372
-
373
- //STLファイルのオープン
374
-
375
- ifstream ifs;
376
-
377
- char line[80];
378
-
379
- ifs.open(filename);
380
-
381
- if (ifs.fail())
382
-
383
- return false;
384
-
385
- string str;
386
-
387
- int count = 0;
388
-
389
- int a;
390
-
391
- getline(ifs, str);
392
-
393
- //int h = str.find("solid");
394
-
395
- if (str.find("solid")==-1){
396
-
397
- ifs.close();
398
-
399
- ifs.open(filename, ios::binary);
400
-
401
- if (ifs.fail())
402
-
403
- return false;
404
-
405
- ifs.read(line, 80);
406
-
407
- ifs.read(line, 4);
408
-
409
- return *(int *)line;
410
-
411
- }
412
-
413
- while (getline(ifs, str)){
414
-
415
- a = str.find("normal");
416
-
417
- if (a > 0)
418
-
419
- count++;
420
-
421
- }
422
-
423
- ifs.close();
424
-
425
- return count;
426
-
427
- };
428
-
429
-
430
-
431
- bool readASCIISTLFile(string filename, Triangle *values)
432
-
433
- {
434
-
435
-
436
-
437
- //STLファイルのオープン
438
-
439
- ifstream ifs(filename);
440
-
441
- if (ifs.fail())
442
-
443
- return false;
444
-
445
-
446
-
447
- //vector<mesh> Stldata;
448
-
449
- string str;
450
-
451
- int index = 0, j;
452
-
453
- double x = 0, y = 0, z = 0;
454
-
455
- //tmp_point *tmp_pnt = new tmp_point;
456
-
457
-
458
-
459
- ////STLファイルのオープン
460
-
461
- //in = fopen(filename.c_str(), "r");
462
-
463
- //if (in == NULL)
464
-
465
- // return false;
466
-
467
- getline(ifs, str);
468
-
469
- if (0 < str.find("solid"))
470
-
471
- return false;
472
-
473
- //ファイルから座標値の読み込み
474
-
475
- printf("Tring text STL file ...");
476
-
477
- num_tmp_points = 0;
478
-
479
- num_triangles = 0;
480
-
481
-
482
-
483
-
484
-
485
- while (getline(ifs, str)){
486
-
487
-
488
-
489
-
490
-
491
- j = str.find("vertex");
492
-
493
- if (j<0)
494
-
495
- continue;
496
-
497
- //連続する3頂点の読み込みポリゴンを登録
498
-
499
- //tmp_pnt = (tmp_point *)malloc(sizeof(tmp_point));
500
-
501
- for (int c = 2; c >= 0; c--)
502
-
503
- {
504
-
505
- values[num_triangles].pnt1[c] = atof(str.substr(str.rfind(" ")).c_str());
506
-
507
- str.erase(str.rfind(" "));
508
-
509
- num_tmp_points++;
510
-
511
- }
512
-
513
- //point_array[num_tmp_points-1] = tmp_pnt;
514
-
515
-
516
-
517
- //tmp_pnt = (tmp_point *)malloc(sizeof(tmp_point));
518
-
519
- getline(ifs, str);
520
-
521
-
522
-
523
- for (int c = 2; c >= 0; c--)
524
-
525
- {
526
-
527
- values[num_triangles].pnt2[c] = atof(str.substr(str.rfind(" ")).c_str());
528
-
529
- str.erase(str.rfind(" "));
530
-
531
- //tmp_pnt->index = num_tmp_points;
532
-
533
- num_tmp_points++;
534
-
535
- }
536
-
537
- //point_array[num_tmp_points-1] = tmp_pnt;
538
-
539
-
540
-
541
- //tmp_pnt = (tmp_point *)malloc(sizeof(tmp_point));
542
-
543
- getline(ifs, str);
544
-
545
-
546
-
547
- for (int c = 2; c >= 0; c--)
548
-
549
- {
550
-
551
- values[num_triangles].pnt3[c] = atof(str.substr(str.rfind(" ")).c_str());
552
-
553
- str.erase(str.rfind(" "));
554
-
555
- //tmp_pnt->index = num_tmp_points;
556
-
557
- num_tmp_points++;
558
-
559
- }
560
-
561
-
562
-
563
- values[num_triangles].nornVec[0] = getNormalVert(values[num_triangles])[0];
564
-
565
- values[num_triangles].nornVec[1] = getNormalVert(values[num_triangles])[1];
566
-
567
- values[num_triangles].nornVec[2] = getNormalVert(values[num_triangles])[2];
568
-
569
-
570
-
571
- values[num_triangles].index = num_triangles;
572
-
573
- num_triangles++;
574
-
575
-
576
-
577
- }
578
-
579
- ifs.close();
580
-
581
- //fclose(in);
582
-
583
- if (num_triangles > 0)
584
-
585
- printf("Done.\n");
586
-
587
- else
588
-
589
- printf("Failed\n");
590
-
591
- return(num_triangles > 0);
592
-
593
-
594
-
595
- }
596
-
597
-
598
-
599
- bool readBinarySTLFile(string STL_file, Triangle *values)
600
-
601
- {
602
-
603
- char line[81];
604
-
605
- float *coord;
606
-
607
- int num;
608
-
609
- FILE *in;
610
-
611
- errno_t error;
612
-
613
-
614
-
615
- ifstream ifs(STL_file);
616
-
617
- if (ifs.fail())
618
-
619
- return false;
620
-
621
-
622
-
623
-
624
-
625
- if ((error = fopen_s(&in,STL_file.c_str(), "rb")) != 0)
626
-
627
- {
628
-
629
- // エラー処理
630
-
631
- //ofs << "Can't open file " << endl;
632
-
633
- fclose(in);
634
-
635
- return false;
636
-
637
- }
638
-
639
-
640
-
641
- //最初の84ビット分を読み飛ばす
642
-
643
- if (fread(line, 1, 80, in) != 80)
644
-
645
- return false;
646
-
647
-
648
-
649
- printf("Tring binary STL file ...");
650
-
651
- fread(line, 1, 4, in);
652
-
653
- num = *(int*)line;
654
-
655
-
656
-
657
- //ofs << "number of triangles: " << num<< endl;
658
-
659
- int j = 0;
660
-
661
- //ofs << "solid model" << endl;
662
-
663
- while (fread(line, 1, 50, in) == 50)
664
-
665
- {
666
-
667
- coord = (float *)line;
668
-
669
- //values[j].nornVec[0] = coord[0];
670
-
671
- //values[j].nornVec[1] = coord[1];
672
-
673
- //values[j].nornVec[2] = coord[2];
674
-
675
-
676
-
677
- values[j].pnt1[0] = coord[3];
678
-
679
- values[j].pnt1[1] = coord[4];
680
-
681
- values[j].pnt1[2] = coord[5];
682
-
683
-
684
-
685
- values[j].pnt2[0] = coord[6];
686
-
687
- values[j].pnt2[1] = coord[7];
688
-
689
- values[j].pnt2[2] = coord[8];
690
-
691
-
692
-
693
- values[j].pnt3[0] = coord[9];
694
-
695
- values[j].pnt3[1] = coord[10];
696
-
697
- values[j].pnt3[2] = coord[11];
698
-
699
-
700
-
701
- values[j].nornVec[0] = getNormalVert(values[j])[0];
702
-
703
- values[j].nornVec[1] = getNormalVert(values[j])[1];
704
-
705
- values[j].nornVec[2] = getNormalVert(values[j])[2];
706
-
707
- values[j].index = j;
708
-
709
- j++;
710
-
711
-
712
-
713
- }
714
-
715
-
716
-
717
- //ofs << "endsolid model" << endl;
718
-
719
- fclose(in);
720
-
721
- //ofs.close();
722
-
723
- if (num > 0)
724
-
725
- printf("Done.\n");
726
-
727
- else
728
-
729
- printf("Failed\n");
730
-
731
- return(num > 0);
732
-
733
-
734
-
735
- }
736
-
737
-
738
-
739
- //vec0とvec1の内積の計算
740
-
741
- double dot(double vec0[], double vec1[])
742
-
743
- {
744
-
745
- return (vec0[0] * vec1[0] + vec0[1] * vec1[1] + vec0[2] * vec1[2]);
746
-
747
- }
748
-
749
-
750
-
751
- void cross(double vec0[], double vec1[], double vec2[])
752
-
753
- {
754
-
755
- vec2[0] = vec0[1] * vec1[2] - vec0[2] * vec1[1];
756
-
757
- vec2[1] = vec0[2] * vec1[0] - vec0[0] * vec1[2];
758
-
759
- vec2[2] = vec0[0] * vec1[1] - vec0[1] * vec1[0];
760
-
761
- }
762
-
763
-
764
-
765
- void normVec(double vec[])
766
-
767
- {
768
-
769
- double norm;
770
-
771
- norm = sqrt((vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]));
772
-
773
- vec[0] /= norm; vec[1] /= norm; vec[2] /= norm;
774
-
775
- }
776
-
777
-
778
-
779
- double* getNormalVert(Triangle vertex)
780
-
781
- {
782
-
783
- double normalVec[3] = {};
784
-
785
- double vec1[3] = { vertex.pnt2[0] - vertex.pnt1[0],
786
-
787
- vertex.pnt2[1] - vertex.pnt1[1],
788
-
789
- vertex.pnt2[2] - vertex.pnt1[2] };
790
-
791
- double vec2[3] = { vertex.pnt3[0] - vertex.pnt2[0],
792
-
793
- vertex.pnt3[1] - vertex.pnt2[1],
794
-
795
- vertex.pnt3[2] - vertex.pnt2[2] };
796
-
797
- cross(vec1, vec2, normalVec);
798
-
799
- normVec(normalVec);//正規化
800
-
801
- return normalVec;
802
-
803
- }
804
-
805
-
806
-
807
-
808
-
809
- };
810
-
811
- ```