~ubuntu-branches/ubuntu/intrepid/blender/intrepid-updates

« back to all changes in this revision

Viewing changes to intern/bsp/intern/BSP_CSGMesh.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-08-08 02:45:40 UTC
  • mfrom: (12.1.14 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080808024540-kkjp7ekfivzhuw3l
Tags: 2.46+dfsg-4
* Fix python syntax warning in import_dxf.py, which led to nasty output
  in installation/upgrade logs during byte-compilation, using a patch
  provided by the script author (Closes: #492280):
   - debian/patches/45_fix_python_syntax_warning

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
 
 * $Id: BSP_CSGMesh.cpp,v 1.8 2005/10/28 20:23:18 intrr Exp $
3
 
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 
2
 * $Id: BSP_CSGMesh.cpp 14444 2008-04-16 22:40:48Z hos $
 
3
 * ***** BEGIN GPL LICENSE BLOCK *****
4
4
 *
5
5
 * This program is free software; you can redistribute it and/or
6
6
 * modify it under the terms of the GNU General Public License
7
7
 * as published by the Free Software Foundation; either version 2
8
 
 * of the License, or (at your option) any later version. The Blender
9
 
 * Foundation also sells licenses for use in proprietary software under
10
 
 * the Blender License.  See http://www.blender.org/BL/ for information
11
 
 * about this.
 
8
 * of the License, or (at your option) any later version.
12
9
 *
13
10
 * This program is distributed in the hope that it will be useful,
14
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
23
 *
27
24
 * Contributor(s): none yet.
28
25
 *
29
 
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 
26
 * ***** END GPL LICENSE BLOCK *****
30
27
 */
31
28
 
32
29
 
33
 
#ifdef HAVE_CONFIG_H
34
 
#include <config.h>
35
 
#endif
36
 
 
37
30
#include "BSP_CSGMesh.h"
38
31
#include "MT_assert.h"
39
32
#include "CTR_TaggedSetOps.h"
40
 
#include "BSP_MeshFragment.h"
41
33
#include "MT_Plane3.h"
42
34
#include "BSP_CSGException.h"
43
35
 
54
46
        m_verts     = NULL;
55
47
        m_faces     = NULL;
56
48
        m_edges     = NULL;
57
 
        m_fv_data   = NULL;
58
 
        m_face_data = NULL;
59
49
}
60
50
 
61
51
        BSP_CSGMesh *
78
68
 
79
69
        if (m_edges != NULL) {
80
70
                mesh->m_edges = new vector<BSP_MEdge>(*m_edges);
81
 
                if (mesh->m_edges == NULL) return NULL;
 
71
                if (mesh->m_edges == NULL) {
 
72
                        delete mesh;
 
73
                        return NULL;
 
74
                }
82
75
        }
83
76
        if (m_verts != NULL) {
84
77
                mesh->m_verts = new vector<BSP_MVertex>(*m_verts);
85
 
                if (mesh->m_verts == NULL) return NULL;
 
78
                if (mesh->m_verts == NULL) {
 
79
                        if (m_edges != NULL) free(mesh->m_edges);
 
80
                        delete mesh;
 
81
                        return NULL;
 
82
                }
86
83
        }
87
84
        if (m_faces != NULL) {
88
85
                mesh->m_faces = new vector<BSP_MFace>(*m_faces);
89
 
                if (mesh->m_faces == NULL) return NULL;
90
 
        }
91
 
        if (m_fv_data != NULL) {
92
 
                mesh->m_fv_data = new BSP_CSGUserData(*m_fv_data);
93
 
                if (mesh->m_fv_data == NULL) return NULL;
94
 
        }
95
 
        if (m_face_data != NULL) {
96
 
                mesh->m_face_data = new BSP_CSGUserData(*m_face_data);
97
 
                if (mesh->m_face_data == NULL) return NULL;
 
86
                if (mesh->m_faces == NULL) {
 
87
                        delete mesh;
 
88
                        return NULL;
 
89
                }
98
90
        }
99
91
 
100
92
        return mesh;
133
125
        return true;
134
126
}
135
127
 
136
 
        void
137
 
BSP_CSGMesh::
138
 
SetFaceVertexData(
139
 
        BSP_CSGUserData *fv_data
140
 
){
141
 
        m_fv_data = fv_data;
142
 
}
143
 
 
144
 
        void
145
 
BSP_CSGMesh::
146
 
SetFaceData(
147
 
        BSP_CSGUserData *f_data
148
 
) {
149
 
        m_face_data = f_data;
150
 
}
151
 
 
152
 
 
153
128
                void
154
129
BSP_CSGMesh::
155
130
AddPolygon(
176
151
        face.m_plane = face_plane;
177
152
};
178
153
 
179
 
        void
180
 
BSP_CSGMesh::
181
 
AddPolygon(
182
 
        const int * verts,
183
 
        const int * fv_indices,
184
 
        int num_verts
185
 
){
186
 
        // This creates a new polygon on the end of the face list.
187
 
        AddPolygon(verts,num_verts);
188
 
 
189
 
        BSP_MFace & face = m_faces->back();
190
 
        // now we just fill in the fv indices
191
 
 
192
 
        if (fv_indices) {
193
 
                insert_iterator<vector<BSP_UserFVInd> > insert_point(face.m_fv_data,face.m_fv_data.end());
194
 
                copy(fv_indices,fv_indices + num_verts,insert_point);
195
 
        } else {
196
 
                face.m_fv_data.insert(face.m_fv_data.end(),num_verts,BSP_UserFVInd::Empty());
197
 
        }
198
 
}
199
 
 
200
 
 
201
 
        void
202
 
BSP_CSGMesh::
203
 
AddSubTriangle(
204
 
        const BSP_MFace &iface,
205
 
        const int * index_info
206
 
){
207
 
        // This creates a new polygon on the end of the face list.
208
 
 
209
 
        m_faces->push_back(BSP_MFace());                        
210
 
        BSP_MFace & face = m_faces->back();
211
 
 
212
 
        face.m_verts.push_back(iface.m_verts[index_info[0]]);
213
 
        face.m_verts.push_back(iface.m_verts[index_info[1]]);
214
 
        face.m_verts.push_back(iface.m_verts[index_info[2]]);
215
 
 
216
 
        face.m_fv_data.push_back(iface.m_fv_data[index_info[0]]);
217
 
        face.m_fv_data.push_back(iface.m_fv_data[index_info[1]]);
218
 
        face.m_fv_data.push_back(iface.m_fv_data[index_info[2]]);
219
 
 
220
 
        face.m_plane = iface.m_plane;
221
 
}
222
 
 
223
 
        
224
154
// assumes that the face already has a plane equation
225
155
        void
226
156
BSP_CSGMesh::
408
338
        return *m_edges;
409
339
}
410
340
 
411
 
        BSP_CSGUserData &
412
 
BSP_CSGMesh::
413
 
FaceVertexData(
414
 
) const {
415
 
        return *m_fv_data;
416
 
}
417
 
 
418
 
        BSP_CSGUserData &
419
 
BSP_CSGMesh::
420
 
FaceData(
421
 
) const {
422
 
        return *m_face_data;
423
 
}
424
 
 
425
 
 
426
341
BSP_CSGMesh::
427
342
~BSP_CSGMesh(
428
343
){
429
344
        if ( m_verts != NULL ) delete m_verts;
430
345
        if ( m_faces != NULL ) delete m_faces;
431
346
        if ( m_edges != NULL ) delete m_edges;
432
 
        if ( m_fv_data != NULL ) delete m_fv_data;
433
 
        if ( m_face_data != NULL ) delete m_face_data;
434
347
}
435
348
 
436
349
// local geometry queries.
588
501
        }
589
502
}
590
503
 
591
 
        void
592
 
BSP_CSGMesh::
593
 
InsertVertexIntoFace(
594
 
        BSP_MFace & face,
595
 
        const BSP_VertexInd & v1,
596
 
        const BSP_VertexInd & v2,
597
 
        const BSP_VertexInd & vi,
598
 
        CSG_InterpolateUserFaceVertexDataFunc fv_split_func,
599
 
        MT_Scalar epsilon
600
 
){
601
 
        // We assume that the face vertex data indices
602
 
        // are consistent with the vertex inidex data.
603
 
 
604
 
        // look for v1
605
 
        vector<BSP_VertexInd>::iterator result = 
606
 
                find(face.m_verts.begin(),face.m_verts.end(),v1);
607
 
        
608
 
        MT_assert(result != face.m_verts.end());
609
 
        
610
 
        BSP_CSGUserData & fv_data = *m_fv_data;
611
 
 
612
 
        // now we have to check on either side of the result for the 
613
 
        // other vertex
614
 
        
615
 
        vector<BSP_VertexInd>::iterator prev = result - 1;
616
 
        if (prev < face.m_verts.begin()) {      
617
 
                prev = face.m_verts.end() -1;
618
 
        }
619
 
        if (*prev == v2) {
620
 
 
621
 
                // so result <=> v2 and prev <=> v1
622
 
 
623
 
                // create space for new face vertex data
624
 
                
625
 
                int vf_i = fv_data.Size();
626
 
                fv_data.IncSize();
627
 
 
628
 
                int vf_i2 = prev - face.m_verts.begin();
629
 
                int vf_i1 = result - face.m_verts.begin();
630
 
 
631
 
                (*fv_split_func)(
632
 
                        fv_data[int(face.m_fv_data[vf_i1])],
633
 
                        fv_data[int(face.m_fv_data[vf_i2])],
634
 
                        fv_data[vf_i],
635
 
                        epsilon
636
 
                );
637
 
        
638
 
                // insert vertex data index.
639
 
                face.m_fv_data.insert(face.m_fv_data.begin() + vf_i1,vf_i);
640
 
                face.m_verts.insert(result,vi);
641
 
 
642
 
                return;
643
 
        }
644
 
 
645
 
        vector<BSP_VertexInd>::iterator next = result + 1;
646
 
        if (next >= face.m_verts.end()) {       
647
 
                next = face.m_verts.begin();
648
 
        }
649
 
        if (*next == v2) {
650
 
 
651
 
                // so result <=> v1 and next <=> v2
652
 
 
653
 
                int vf_i = fv_data.Size();
654
 
                fv_data.IncSize();
655
 
 
656
 
                int vf_i2 = int(next - face.m_verts.begin());
657
 
                int vf_i1 = int(result - face.m_verts.begin());
658
 
 
659
 
                (*fv_split_func)(
660
 
                        fv_data[int(face.m_fv_data[vf_i1])],
661
 
                        fv_data[int(face.m_fv_data[vf_i2])],
662
 
                        fv_data[vf_i],
663
 
                        epsilon
664
 
                );
665
 
 
666
 
                // insert vertex data index.
667
 
                face.m_fv_data.insert(face.m_fv_data.begin() + vf_i2,vf_i);
668
 
                face.m_verts.insert(next,vi);
669
 
 
670
 
                return;
671
 
        }
672
 
 
673
 
        // if we get here we are in trouble.
674
 
        MT_assert(false);
675
 
        BSP_CSGException e(e_mesh_error);
676
 
        throw(e);
677
 
}
678
 
 
679
 
        void
680
 
BSP_CSGMesh::
681
 
SetBBox(
682
 
        const MT_Vector3 & min,
683
 
        const MT_Vector3 & max
684
 
){
685
 
        m_bbox_min = min;
686
 
        m_bbox_max = max;
687
 
}
688
 
 
689
 
 
690
 
        void
691
 
BSP_CSGMesh::
692
 
BBox(
693
 
        MT_Vector3 &min,
694
 
        MT_Vector3 &max
695
 
) const {
696
 
        min = m_bbox_min;
697
 
        max = m_bbox_max;
698
 
}
699
 
 
700
 
 
701
 
// Update the BBox
702
 
//////////////////
703
 
 
704
 
        void
705
 
BSP_CSGMesh::
706
 
UpdateBBox(
707
 
){
708
 
        // TODO
709
 
};
710
 
 
711
 
        void
712
 
BSP_CSGMesh::
713
 
SC_Classification(
714
 
        BSP_FaceInd f,
715
 
        const MT_Plane3& plane
716
 
){
717
 
        const BSP_MFace & face = FaceSet()[f];
718
 
 
719
 
        vector<BSP_VertexInd>::const_iterator f_verts_it = face.m_verts.begin();
720
 
        vector<BSP_VertexInd>::const_iterator f_verts_end = face.m_verts.end();
721
 
 
722
 
        for (;f_verts_it != f_verts_end; ++f_verts_it) {
723
 
 
724
 
                const BSP_MVertex & vert = VertexSet()[*f_verts_it];
725
 
 
726
 
                MT_Scalar dist = plane.signedDistance(
727
 
                        vert.m_pos
728
 
                );
729
 
 
730
 
                if (fabs(dist) <= BSP_SPLIT_EPSILON ){
731
 
                        MT_assert(BSP_Classification(vert.OpenTag()) == e_classified_on);
732
 
                } else
733
 
                if (dist > BSP_SPLIT_EPSILON) {
734
 
                        MT_assert(BSP_Classification(vert.OpenTag()) == e_classified_out);
735
 
                } else 
736
 
                if (dist < BSP_SPLIT_EPSILON) {
737
 
                        MT_assert(BSP_Classification(vert.OpenTag()) == e_classified_in);
738
 
                }
739
 
        }
740
 
}
741
 
 
742
 
 
743
504
        bool
744
505
BSP_CSGMesh::
745
506
SC_Face(
889
650
        return sum;
890
651
}
891
652
 
892
 
 
893