[go: up one dir, main page]

Menu

[r990]: / mia2 / vistaio / mesh.cc  Maximize  Restore  History

Download this file

855 lines (671 with data), 23.7 kB

  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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
/* -*- mona-c++ -*-
* Copyright (c) Leipzig, Madrid 2004 - 2008
* Max-Planck-Institute for Human Cognitive and Brain Science
* Max-Planck-Institute for Evolutionary Anthropology
* BIT, ETSI Telecomunicacion, UPM
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
// $Id: vista_meshio.cc 896 2006-03-27 10:01:12Z write1 $
/*! \brief plugin function to handle vista- format mesh IO
\file vista_meshio.cc
\author Gert Wollny <wollny@cbs.mpg.de>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <vistaio/vista4mia.hh>
#include <mia/mesh/triangularMesh.hh>
namespace vista_mesh_io {
using namespace mona;
using namespace std;
static char const * const format = "vista";
class TVistaMeshIO: public CTriangleMeshIO {
public:
TVistaMeshIO();
virtual CTriangleMesh* load(string const & filename,bool, CProgressCallback *cb)const;
virtual bool save(const CTriangleMesh& data, string const & filename,bool, CProgressCallback *cb)const;
const string short_descr()const;
};
extern "C" CPluginBase *get_plugin_interface()
{
vista::prepare_vista_logging();
return new TVistaMeshIO;
}
// implementation part
TVistaMeshIO::TVistaMeshIO():
CTriangleMeshIO(format)
{
add_property(io_plugin_property_history);
}
const string TVistaMeshIO::short_descr()const
{
return string("Vista/Simbio triangle mesh input/output support");
}
template <class T>
struct TVNodeType: public VNodeBaseRec {
T type;
T3DVector<T> vertex;
};
template <class T>
struct TVNNodeType: public TVNodeType<T> {
T3DVector<T> normal;
};
template <class T>
struct TVSNodeType: public TVNodeType<T> {
T scale;
};
template <class T>
struct TVCNodeType: public TVNodeType<T> {
T3DVector<T> color;
};
template <class T>
struct TVNCNodeType: public TVNNodeType<T> {
T3DVector<T> color;
};
template <class T>
struct TVCSNodeType: public TVCNodeType<T> {
T scale;
};
template <class T>
struct TVNSNodeType: public TVNNodeType<T> {
T scale;
};
template <class T>
struct TVNCSNodeType: public TVNCNodeType<T> {
T scale;
};
struct TTriangelType {
VNodeBaseRec base;
int type;
T3DVector<unsigned int> t;
};
static CTriangleMesh::trianglefield_type *read_triangles(VGraph graph, size_t v_size)
{
cvdebug() << "vista_meshio::read_triangles()\n";
long node_size = graph->nfields;
if ( node_size != 4 ) {
cverr() << "only triangles supported\n";
return false;
}
if (VNodeRepn(graph) != VLongRepn) {
cverr() <<"only long indices supported\n";
return false;
}
TTriangelType *node = (TTriangelType*)VGraphFirstNode(graph);
CTriangleMesh::trianglefield_type *triangles = new CTriangleMesh::trianglefield_type(VGraphNNodes(graph));
CTriangleMesh::triangle_iterator t = triangles->begin();
CTriangleMesh::triangle_iterator e = triangles->end();
while (node) {
if (t == e) {
triangles->release();
return NULL;
}
node->t.x--;
node->t.y--;
node->t.z--;
if (node->t.x < v_size &&
node->t.y < v_size &&
node->t.z < v_size) {
// set triangle values
*t++ = node->t;
}else{
cverr() <<"adressing vertexes " << node->t << "but have only"<<v_size<< "vertices\n";
cverr() <<"Bougus mesh: Triangle vertex out of bounds - bailing out\n";
return false;
}
node = (TTriangelType*)VGraphNextNode(graph);
}
return triangles;
}
static CTriangleMesh *read_vgraph(VGraph vgraph, VGraph tgraph)
{
TVNodeType<float> *node = (TVNodeType<float> *)VGraphFirstNode(vgraph);
CTriangleMesh::vertexfield_type *vertices = new CTriangleMesh::vertexfield_type(VGraphNNodes(vgraph));
CTriangleMesh::vertex_iterator v = vertices->begin();
CTriangleMesh::vertex_iterator e = vertices->end();
while (node) {
if (v == e)
return false;
*v++ = node->vertex;
node = (TVNodeType<float> *)VGraphNextNode(vgraph);
}
CTriangleMesh::trianglefield_type *triangles = read_triangles(tgraph, vertices->size());
if (!triangles) {
vertices->release();
return NULL;
}
return new CTriangleMesh(triangles, vertices, NULL, NULL, NULL);
}
static CTriangleMesh *read_vsgraph(VGraph vgraph, VGraph tgraph)
{
TVSNodeType<float> *node =
(TVSNodeType<float> *)VGraphFirstNode(vgraph);
CTriangleMesh::vertexfield_type *vertices =
new CTriangleMesh::vertexfield_type(VGraphNNodes(vgraph));
CTriangleMesh::scalefield_type *scales=
new CTriangleMesh::scalefield_type(VGraphNNodes(vgraph));
CTriangleMesh::vertex_iterator v = vertices->begin();
CTriangleMesh::vertex_iterator e = vertices->end();
CTriangleMesh::scale_iterator s = scales->begin();
while (node && v != e) {
*v++ = node->vertex;
*s++ = node->scale;
node = (TVSNodeType<float> *)VGraphNextNode(vgraph);
}
CTriangleMesh::trianglefield_type *triangles = read_triangles(tgraph, vertices->size());
if (!triangles) {
vertices->release();
scales->release();
return NULL;
}
return new CTriangleMesh(triangles, vertices, NULL, NULL, scales);
}
static CTriangleMesh *read_vcgraph(VGraph vgraph, VGraph tgraph)
{
CTriangleMesh::vertexfield_type *vertices =
new CTriangleMesh::vertexfield_type(VGraphNNodes(vgraph));
CTriangleMesh::colorfield_type *color =
new CTriangleMesh::colorfield_type(VGraphNNodes(vgraph));
TVCNodeType<float> *node =
(TVCNodeType<float> *)VGraphFirstNode(vgraph);
CTriangleMesh::vertex_iterator v = vertices->begin();
CTriangleMesh::vertex_iterator e = vertices->end();
CTriangleMesh::color_iterator c = color->begin();
while (node && v != e) {
*v++ = node->vertex;
*c++ = node->color;
node = (TVCNodeType<float> *)VGraphNextNode(vgraph);
}
CTriangleMesh::trianglefield_type *triangles = read_triangles(tgraph, vertices->size());
if (!triangles) {
vertices->release();
color->release();
return NULL;
}
return new CTriangleMesh(triangles, vertices, NULL, color, NULL);
}
static CTriangleMesh *read_vcsgraph(VGraph vgraph, VGraph tgraph)
{
CTriangleMesh::vertexfield_type *vertices =
new CTriangleMesh::vertexfield_type(VGraphNNodes(vgraph));
CTriangleMesh::colorfield_type *color =
new CTriangleMesh::colorfield_type(VGraphNNodes(vgraph));
CTriangleMesh::scalefield_type *scales=
new CTriangleMesh::scalefield_type(VGraphNNodes(vgraph));
TVCSNodeType<float> *node =
(TVCSNodeType<float> *)VGraphFirstNode(vgraph);
CTriangleMesh::vertex_iterator v = vertices->begin();
CTriangleMesh::vertex_iterator e = vertices->end();
CTriangleMesh::color_iterator c = color->begin();
CTriangleMesh::scale_iterator s = scales->begin();
while (node && v != e) {
*v++ = node->vertex;
*c++ = node->color;
*s++ = node->scale;
node = (TVCSNodeType<float> *)VGraphNextNode(vgraph);
}
CTriangleMesh::trianglefield_type *triangles = read_triangles(tgraph, vertices->size());
if (!triangles) {
vertices->release();
color->release();
scales->release();
return NULL;
}
return new CTriangleMesh(triangles, vertices, NULL, color, scales);
}
static CTriangleMesh *read_vngraph(VGraph vgraph, VGraph tgraph)
{
TVNNodeType<float> *node =
(TVNNodeType<float> *)VGraphFirstNode(vgraph);
CTriangleMesh::vertexfield_type *vertices =
new CTriangleMesh::vertexfield_type(VGraphNNodes(vgraph));
CTriangleMesh::normalfield_type *normals =
new CTriangleMesh::normalfield_type(VGraphNNodes(vgraph));
CTriangleMesh::vertex_iterator v = vertices->begin();
CTriangleMesh::vertex_iterator e = vertices->end();
CTriangleMesh::normal_iterator n = normals->begin();
while (node && v != e) {
*v++ = node->vertex;
*n++ = node->normal;
node = (TVNNodeType<float> *)VGraphNextNode(vgraph);
}
CTriangleMesh::trianglefield_type *triangles = read_triangles(tgraph, vertices->size());
if (!triangles) {
vertices->release();
normals->release();
return NULL;
}
return new CTriangleMesh(triangles, vertices, normals, NULL, NULL);
}
static CTriangleMesh *read_vnsgraph(VGraph vgraph, VGraph tgraph)
{
TVNSNodeType<float> *node =
(TVNSNodeType<float> *)VGraphFirstNode(vgraph);
CTriangleMesh::vertexfield_type *vertices =
new CTriangleMesh::vertexfield_type(VGraphNNodes(vgraph));
CTriangleMesh::normalfield_type *normals =
new CTriangleMesh::normalfield_type(VGraphNNodes(vgraph));
CTriangleMesh::scalefield_type *scales=
new CTriangleMesh::scalefield_type(VGraphNNodes(vgraph));
CTriangleMesh::vertex_iterator v = vertices->begin();
CTriangleMesh::vertex_iterator e = vertices->end();
CTriangleMesh::normal_iterator n = normals->begin();
CTriangleMesh::scale_iterator s = scales->begin();
while (node && v != e) {
*n++ = node->normal;
*v++ = node->vertex;
*s++ = node->scale;
node = (TVNSNodeType<float> *)VGraphNextNode(vgraph);
}
CTriangleMesh::trianglefield_type *triangles = read_triangles(tgraph, vertices->size());
if (!triangles) {
vertices->release();
normals->release();
scales->release();
return NULL;
}
return new CTriangleMesh(triangles, vertices, NULL, NULL, scales);
}
static CTriangleMesh *read_vncgraph(VGraph vgraph, VGraph tgraph)
{
TVNCNodeType<float> *node =
(TVNCNodeType<float> *)VGraphFirstNode(vgraph);
CTriangleMesh::vertexfield_type *vertices =
new CTriangleMesh::vertexfield_type(VGraphNNodes(vgraph));
CTriangleMesh::normalfield_type *normals =
new CTriangleMesh::normalfield_type(VGraphNNodes(vgraph));
CTriangleMesh::colorfield_type *color =
new CTriangleMesh::colorfield_type(VGraphNNodes(vgraph));
CTriangleMesh::vertex_iterator v = vertices->begin();
CTriangleMesh::vertex_iterator e = vertices->end();
CTriangleMesh::normal_iterator n = normals->begin();
CTriangleMesh::color_iterator c = color->begin();
while (node && v != e) {
*n++ = node->normal;
*v++ = node->vertex;
*c++ = node->color;
node = (TVNCNodeType<float> *)VGraphNextNode(vgraph);
}
CTriangleMesh::trianglefield_type *triangles = read_triangles(tgraph, vertices->size());
if (!triangles) {
vertices->release();
return NULL;
}
return new CTriangleMesh(triangles, vertices, normals, color, NULL);
}
static CTriangleMesh *read_vncsgraph(VGraph vgraph, VGraph tgraph)
{
TVNCSNodeType<float> *node =
(TVNCSNodeType<float> *)VGraphFirstNode(vgraph);
CTriangleMesh::vertexfield_type *vertices =
new CTriangleMesh::vertexfield_type(VGraphNNodes(vgraph));
CTriangleMesh::normalfield_type *normals =
new CTriangleMesh::normalfield_type(VGraphNNodes(vgraph));
CTriangleMesh::colorfield_type *color =
new CTriangleMesh::colorfield_type(VGraphNNodes(vgraph));
CTriangleMesh::scalefield_type *scales=
new CTriangleMesh::scalefield_type(VGraphNNodes(vgraph));
CTriangleMesh::vertex_iterator v = vertices->begin();
CTriangleMesh::vertex_iterator e = vertices->end();
CTriangleMesh::normal_iterator n = normals->begin();
CTriangleMesh::color_iterator c = color->begin();
CTriangleMesh::scale_iterator s = scales->begin();
while (node && v != e) {
*n++ = node->normal;
*v++ = node->vertex;
*c++ = node->color;
*s++ = node->scale;
node = (TVNCSNodeType<float> *)VGraphNextNode(vgraph);
}
CTriangleMesh::trianglefield_type *triangles = read_triangles(tgraph, vertices->size());
if (!triangles) {
vertices->release();
normals->release();
scales->release();
color->release();
return NULL;
}
return new CTriangleMesh(triangles, vertices, normals, color, scales);
}
static VGraph create_vgraph(const CTriangleMesh& mesh)
{
VGraph result = VCreateGraph(mesh.vertices_size(), 4, VFloatRepn, 0);
CTriangleMesh::const_vertex_iterator vi = mesh.vertices_begin();
CTriangleMesh::const_vertex_iterator ve = mesh.vertices_end();
TVNodeType<float> node;
node.type = 1;
int pos = 1;
while (vi != ve) {
node.vertex = *vi++;
VGraphAddNodeAt(result,(VNodestruct*)&node,pos++);
}
return result;
}
static VGraph create_vngraph(const CTriangleMesh& mesh)
{
VGraph result = VCreateGraph(mesh.vertices_size(), 7, VFloatRepn, 0);
CTriangleMesh::const_vertex_iterator vi = mesh.vertices_begin();
CTriangleMesh::const_vertex_iterator ve = mesh.vertices_end();
CTriangleMesh::const_normal_iterator ni = mesh.normals_begin();
TVNNodeType<float> node;
node.type = 2;
int pos = 1;
while (vi != ve) {
node.vertex = *vi++;
node.normal = *ni++;
VGraphAddNodeAt(result,(VNodestruct*)&node,pos++);
}
return result;
}
static VGraph create_vsgraph(const CTriangleMesh& mesh)
{
VGraph result = VCreateGraph(mesh.vertices_size(), 11, VFloatRepn, 0);
CTriangleMesh::const_vertex_iterator vi = mesh.vertices_begin();
CTriangleMesh::const_vertex_iterator ve = mesh.vertices_end();
CTriangleMesh::const_scale_iterator si = mesh.scale_begin();
TVSNodeType<float> node;
node.type = 4;
int pos = 1;
while (vi != ve) {
node.vertex = *vi++;
node.scale = *si++;
VGraphAddNodeAt(result,(VNodestruct*)&node,pos++);
}
return result;
}
static VGraph create_vnsgraph(const CTriangleMesh& mesh)
{
VGraph result = VCreateGraph(mesh.vertices_size(), 8, VFloatRepn, 0);
CTriangleMesh::const_vertex_iterator vi = mesh.vertices_begin();
CTriangleMesh::const_vertex_iterator ve = mesh.vertices_end();
CTriangleMesh::const_normal_iterator ni = mesh.normals_begin();
CTriangleMesh::const_scale_iterator si = mesh.scale_begin();
int pos = 1;
TVNSNodeType<float> node;
node.type = 5;
while (vi != ve) {
node.vertex = *vi++;
node.normal = *ni++;
node.scale = *si++;
VGraphAddNodeAt(result,(VNodestruct*)&node,pos++);
}
return result;
}
static VGraph create_vcgraph(const CTriangleMesh& mesh)
{
VGraph result = VCreateGraph(mesh.vertices_size(), 7, VFloatRepn, 0);
CTriangleMesh::const_vertex_iterator vi = mesh.vertices_begin();
CTriangleMesh::const_vertex_iterator ve = mesh.vertices_end();
CTriangleMesh::const_color_iterator ci = mesh.color_begin();
int pos = 1;
TVNCNodeType<float> node;
node.type = 7;
while (vi != ve) {
node.vertex = *vi++;
node.color = *ci++;
VGraphAddNodeAt(result,(VNodestruct*)&node,pos++);
}
return result;
}
static VGraph create_vncgraph(const CTriangleMesh& mesh)
{
VGraph result = VCreateGraph(mesh.vertices_size(), 10, VFloatRepn, 0);
CTriangleMesh::const_vertex_iterator vi = mesh.vertices_begin();
CTriangleMesh::const_vertex_iterator ve = mesh.vertices_end();
CTriangleMesh::const_normal_iterator ni = mesh.normals_begin();
CTriangleMesh::const_color_iterator ci = mesh.color_begin();
int pos = 1;
TVNCNodeType<float> node;
node.type = 3;
while (vi != ve) {
node.vertex = *vi++;
node.normal = *ni++;
node.color = *ci++;
VGraphAddNodeAt(result,(VNodestruct*)&node,pos++);
}
return result;
}
static VGraph create_vcsgraph(const CTriangleMesh& mesh)
{
VGraph result = VCreateGraph(mesh.vertices_size(), 8, VFloatRepn, 0);
CTriangleMesh::const_vertex_iterator vi = mesh.vertices_begin();
CTriangleMesh::const_vertex_iterator ve = mesh.vertices_end();
CTriangleMesh::const_color_iterator ci = mesh.color_begin();
CTriangleMesh::const_scale_iterator si = mesh.scale_begin();
TVCSNodeType<float> node;
node.type = 8;
int pos = 1;
while (vi != ve) {
node.vertex = *vi++;
node.color = *ci++;
node.scale = *si++;
VGraphAddNodeAt(result,(VNodestruct*)&node,pos++);
}
return result;
}
static VGraph create_vncsgraph(const CTriangleMesh& mesh)
{
VGraph result = VCreateGraph(mesh.vertices_size(), 11, VFloatRepn, 0);
CTriangleMesh::const_vertex_iterator vi = mesh.vertices_begin();
CTriangleMesh::const_vertex_iterator ve = mesh.vertices_end();
CTriangleMesh::const_normal_iterator ni = mesh.normals_begin();
CTriangleMesh::const_color_iterator ci = mesh.color_begin();
CTriangleMesh::const_scale_iterator si = mesh.scale_begin();
TVNCSNodeType<float> node;
node.type = 6;
int pos = 1;
while (vi != ve) {
node.vertex = *vi++;
node.normal = *ni++;
node.color = *ci++;
node.scale = *si++;
VGraphAddNodeAt(result,(VNodestruct*)&node,pos++);
}
return result;
}
typedef VGraph (*vertexgraph_create)(const CTriangleMesh& mesh);
typedef CTriangleMesh * (*vertexgraph_read)(VGraph graph, VGraph graph);
typedef struct {
int simbio_type;
const char *type_name;
vertexgraph_create create;
vertexgraph_read read;
int data_avail;
} vertexio_t;
vertexio_t vertexio_lookup[] = {
{ 1, "Vertex", create_vgraph, read_vgraph ,
CTriangleMesh::ed_vertex },
{ 2, "VertexNormal", create_vngraph, read_vngraph ,
CTriangleMesh::ed_vertex | CTriangleMesh::ed_normal},
{ 4, "VertexScalar", create_vsgraph, read_vsgraph ,
CTriangleMesh::ed_vertex | CTriangleMesh::ed_scale},
{ 5, "VertexNormalScalar", create_vnsgraph, read_vnsgraph ,
CTriangleMesh::ed_vertex | CTriangleMesh::ed_normal| CTriangleMesh::ed_scale},
{ 7, "VertexColor", create_vcgraph, read_vcgraph ,
CTriangleMesh::ed_vertex | CTriangleMesh::ed_color},
{ 8, "VertexColorScalar", create_vcsgraph, read_vcsgraph ,
CTriangleMesh::ed_vertex | CTriangleMesh::ed_color| CTriangleMesh::ed_scale},
{ 3, "VertexNormalColor", create_vncgraph, read_vncgraph ,
CTriangleMesh::ed_vertex | CTriangleMesh::ed_normal | CTriangleMesh::ed_color},
{ 6, "VertexNormalColorScalar", create_vncsgraph, read_vncsgraph,
CTriangleMesh::ed_vertex | CTriangleMesh::ed_normal
| CTriangleMesh::ed_color | CTriangleMesh::ed_scale},
{ 0, NULL, NULL, NULL, 0}
};
typedef struct {
VGraph graph;
vertexgraph_read read;
} vertex_reader_t;
static bool lookup_vertex_graph(VAttrList list, VAttrListPosn *pos, vertex_reader_t *result)
{
vertexio_t *lookup = vertexio_lookup;
while (lookup->simbio_type != 0) {
if (VLookupAttr(list,lookup->type_name,pos)) {
VGetAttrValue(pos, NULL, VGraphRepn, &result->graph);
if (result->graph) {
result->read = lookup->read;
cvdebug() <<"found "<< lookup->type_name << "\n";
VNextAttr(pos);
return true;
}
}
++lookup;
}
cvwarn() <<"I didn't find a known graph attribute, will try generic graph\n";
lookup = vertexio_lookup;
VFirstAttr(list, pos);
while (VAttrExists(pos)) {
if (VGetAttrRepn(pos) == VGraphRepn){
cvdebug() <<"Got a graph\n";
VGetAttrValue(pos, NULL, VGraphRepn, &result->graph);
TVNodeType<float> *node = (TVNodeType<float> *)VGraphFirstNode(result->graph);
while (lookup->simbio_type && lookup->simbio_type != node->type)
++lookup;
cvdebug() <<"found "<< lookup->type_name << "\n";
result->read = lookup->read;
VNextAttr(pos);
break;
}
VNextAttr(pos);
}
return lookup->simbio_type != 0;
}
static VGraph lookup_triangle_graph(VAttrList list, VAttrListPosn *pos)
{
VAttrListPosn save_pos = *pos;
VGraph result = NULL;
if (VLookupAttr(list,"Triangles",pos)) {
VGetAttrValue(pos, NULL, VGraphRepn, &result);
VNextAttr(pos);
}else{
*pos = save_pos;
cvdebug() <<"Didn't find an attribute called 'triangles', will try generic graph\n";
while (VAttrExists(pos)) {
if (VGetAttrRepn(pos) == VGraphRepn){
cvdebug() <<"Got a graph\n";
VGetAttrValue(pos, NULL, VGraphRepn, &result);
VNextAttr(pos);
break;
}
VNextAttr(pos);
}
}
return result;
}
static CTriangleMesh *do_load_mesh(VAttrList list)
{
VAttrListPosn pos;
vertex_reader_t vertex_source;
VGraph triangle_graph = NULL;
if ( ! lookup_vertex_graph(list,&pos,&vertex_source) ) {
cvwarn() <<"Didn't find a graph type attribute\n";
return NULL;
}
// pos is now behind the vertex - graph
if ((triangle_graph = lookup_triangle_graph(list,&pos) ) == NULL) {
cvwarn() <<"Unable to find triangles\n";
return NULL;
}
int v_size = VGraphNNodes(vertex_source.graph);
int num_faces = VGraphNNodes(triangle_graph);
if (VNodeRepn(vertex_source.graph) != VFloatRepn) {
cvwarn() <<"Sorry, only float vertex coordinates supported\n";
return NULL;
}
cvdebug() << "Create mesh with " << v_size << " vertices and " << num_faces << " faces.\n";
return vertex_source.read(vertex_source.graph, triangle_graph);
}
CTriangleMesh *TVistaMeshIO::load(string const & filename, bool from_stdin, CProgressCallback *cb) const
{
CTriangleMesh *result = NULL;
CInputFile file(filename,from_stdin);
if (!file)
return NULL;
VAttrList list= VReadFile(file,0);
if (list) {
cvdebug() << "Got a valid vista file\n";
VAttrList nested_list;
VAttrListPosn pos;
if (VLookupAttr(list,"mesh",&pos)) {
VGetAttrValue(&pos,NULL,VAttrListRepn,&nested_list);
}else{
cvdebug() << "Let's put meshes in a nested list!\n";
nested_list = list;
}
result = do_load_mesh(nested_list);
VDestroyAttrList(list);
}
return result;
}
static VGraph create_triangle_graph_and_link(const CTriangleMesh& mesh, VGraph vertex_graph)
{
VGraph result = VCreateGraph(mesh.triangle_size(), 4, VLongRepn, 0);
VSetAttr(VGraphAttrList(result), "primitive_interp", NULL, VStringRepn, "surface");
VSetAttr(VGraphAttrList(result), "implicit_links", NULL, VStringRepn, "true");
VSetAttr(VGraphAttrList(result), "component_interp", NULL, VStringRepn, "primitive");
CTriangleMesh::const_triangle_iterator ti = mesh.triangles_begin();
CTriangleMesh::const_triangle_iterator te = mesh.triangles_end();
TTriangelType node;
node.type = 3;
int pos = 1;
while (ti != te) {
node.t = *ti++;
++node.t.x;
++node.t.y;
++node.t.z;
VGraphAddNodeAt(result,(VNodestruct*)&node,pos++);
VGraphLinkNodesBi(vertex_graph, node.t.x, node.t.y );
VGraphLinkNodesBi(vertex_graph, node.t.y, node.t.z );
VGraphLinkNodesBi(vertex_graph, node.t.z, node.t.x );
}
return result;
}
bool TVistaMeshIO::save(const CTriangleMesh& mesh, string const & filename, bool to_stdout, CProgressCallback *cb)const
{
bool result = false;
COutputFile outf(filename,to_stdout);
if (!outf)
return false;
VGraph vertex_graph;
VGraph triangle_graph;
int type = mesh.get_available_data();
cvdebug() << "available: " << type << "\n";
VAttrList list = VCreateAttrList();
vertexio_t *lookup = vertexio_lookup;
while (lookup->data_avail && lookup->data_avail != type)
++lookup;
cvdebug() << "will save " << lookup->type_name << "\n";
if (!lookup->data_avail) {
cverr() << "Fatal: unknown data in mesh\n";
return false;
}
vertex_graph = lookup->create(mesh);
if (!vertex_graph) {
cverr() << "Fatal: unable to create vertex graph\n";
return false;
}
VSetAttr(VGraphAttrList(vertex_graph), "component_interp", NULL, VStringRepn, "vertex");
VSetAttr(list, lookup->type_name, NULL, VGraphRepn, vertex_graph);
triangle_graph = create_triangle_graph_and_link(mesh, vertex_graph);
if (!triangle_graph)
VDestroyAttrList(list);
VSetAttr(list, "Triangles", NULL, VGraphRepn, triangle_graph);
result = (VWriteFile(outf, list) == TRUE);
VDestroyAttrList(list);
return result;
}
} // end namespace