| [5d30c1] | 1 | //
 | 
|---|
 | 2 | // surfor.cc
 | 
|---|
 | 3 | //
 | 
|---|
 | 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
 | 
|---|
 | 5 | //
 | 
|---|
 | 6 | // Author: Curtis Janssen <cljanss@limitpt.com>
 | 
|---|
 | 7 | // Maintainer: LPS
 | 
|---|
 | 8 | //
 | 
|---|
 | 9 | // This file is part of the SC Toolkit.
 | 
|---|
 | 10 | //
 | 
|---|
 | 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
 | 
|---|
 | 12 | // it under the terms of the GNU Library General Public License as published by
 | 
|---|
 | 13 | // the Free Software Foundation; either version 2, or (at your option)
 | 
|---|
 | 14 | // any later version.
 | 
|---|
 | 15 | //
 | 
|---|
 | 16 | // The SC Toolkit is distributed in the hope that it will be useful,
 | 
|---|
 | 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 19 | // GNU Library General Public License for more details.
 | 
|---|
 | 20 | //
 | 
|---|
 | 21 | // You should have received a copy of the GNU Library General Public License
 | 
|---|
 | 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
 | 
|---|
 | 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 | 
|---|
 | 24 | //
 | 
|---|
 | 25 | // The U.S. Government is granted a limited license as per AL 91-7.
 | 
|---|
 | 26 | //
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | #include <util/misc/formio.h>
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | #include <math/scmat/matrix.h>
 | 
|---|
 | 31 | #include <math/isosurf/surf.h>
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | using namespace std;
 | 
|---|
 | 34 | using namespace sc;
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | void
 | 
|---|
 | 37 | TriangulatedSurface::fix_orientation()
 | 
|---|
 | 38 | {
 | 
|---|
 | 39 |   int i,j;
 | 
|---|
 | 40 |   std::set<Ref<Triangle> >::iterator I;
 | 
|---|
 | 41 |   int nflip = 0;
 | 
|---|
 | 42 |   
 | 
|---|
 | 43 |   int ne = nedge();
 | 
|---|
 | 44 |   int ntri = ntriangle();
 | 
|---|
 | 45 |   Ref<Triangle> *edge_to_triangle0;
 | 
|---|
 | 46 |   Ref<Triangle> *edge_to_triangle1;
 | 
|---|
 | 47 |   edge_to_triangle0 = new Ref<Triangle>[ne];
 | 
|---|
 | 48 |   edge_to_triangle1 = new Ref<Triangle>[ne];
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 |   for (I = _triangles.begin(); I != _triangles.end(); I++) {
 | 
|---|
 | 51 |       Ref<Triangle> tri = *I;
 | 
|---|
 | 52 |       for (j=0; j<3; j++) {
 | 
|---|
 | 53 |           Ref<Edge> e = tri->edge(j);
 | 
|---|
 | 54 |           int e_index = _edge_to_index[e];
 | 
|---|
 | 55 |           if (edge_to_triangle0[e_index].null()) {
 | 
|---|
 | 56 |               edge_to_triangle0[e_index] = tri;
 | 
|---|
 | 57 |             }
 | 
|---|
 | 58 |           else if (edge_to_triangle1[e_index].null()) {
 | 
|---|
 | 59 |               edge_to_triangle1[e_index] = tri;
 | 
|---|
 | 60 |             }
 | 
|---|
 | 61 |           else {
 | 
|---|
 | 62 |               ExEnv::errn() << "TriangulatedSurface::fix_orientation:"
 | 
|---|
 | 63 |                    << " more than two triangles to an edge" << endl;
 | 
|---|
 | 64 |               abort();
 | 
|---|
 | 65 |             }
 | 
|---|
 | 66 |         }
 | 
|---|
 | 67 |     }
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 |   std::set<Ref<Triangle> > unfixed;
 | 
|---|
 | 70 |   std::set<Ref<Triangle> > fixed;
 | 
|---|
 | 71 |   std::set<Ref<Triangle> > finished;
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 |   unfixed.insert(_triangles.begin(), _triangles.end());
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 |   while (unfixed.size()) {
 | 
|---|
 | 76 |       // define unfixed.first()'s orientation to be the fixed orientation
 | 
|---|
 | 77 |       std::set<Ref<Triangle> >::iterator first = unfixed.begin();
 | 
|---|
 | 78 |       fixed.insert(*first);
 | 
|---|
 | 79 |       unfixed.erase(*first);
 | 
|---|
 | 80 |       while (fixed.size()) {
 | 
|---|
 | 81 |           Ref<Triangle> tri = *fixed.begin();
 | 
|---|
 | 82 |           // make all neighbors of tri oriented the same as tri
 | 
|---|
 | 83 |           for (i=0; i<3; i++) {
 | 
|---|
 | 84 |               Ref<Edge> e = tri->edge(i);
 | 
|---|
 | 85 |               int e_index = _edge_to_index[e];
 | 
|---|
 | 86 |               Ref<Triangle> othertri;
 | 
|---|
 | 87 |               if (edge_to_triangle0[e_index] == tri) {
 | 
|---|
 | 88 |                   othertri = edge_to_triangle1[e_index];
 | 
|---|
 | 89 |                 }
 | 
|---|
 | 90 |               else {
 | 
|---|
 | 91 |                   othertri = edge_to_triangle0[e_index];
 | 
|---|
 | 92 |                 }
 | 
|---|
 | 93 |               for (j=0; j<3; j++) {
 | 
|---|
 | 94 |                   if (othertri->edge(j) == e) break;
 | 
|---|
 | 95 |                 }
 | 
|---|
 | 96 |               if (j == 3) {
 | 
|---|
 | 97 |                   ExEnv::errn() << "TriangulatedSurface::fix_orientation: "
 | 
|---|
 | 98 |                        << " edge_to_triangle wrong" << endl;
 | 
|---|
 | 99 |                   abort();
 | 
|---|
 | 100 |                 }
 | 
|---|
 | 101 |               if (tri->orientation(i) == othertri->orientation(j)) {
 | 
|---|
 | 102 |                   if (unfixed.find(othertri) != unfixed.end()) {
 | 
|---|
 | 103 |                       unfixed.erase(othertri);
 | 
|---|
 | 104 |                       fixed.insert(othertri);
 | 
|---|
 | 105 |                       othertri->flip();
 | 
|---|
 | 106 |                       nflip++;
 | 
|---|
 | 107 |                     }
 | 
|---|
 | 108 |                   else {
 | 
|---|
 | 109 |                       ExEnv::errn() << "TriangulatedSurface::fix_orientation:"
 | 
|---|
 | 110 |                            << " tried to flip a fixed triangle" << endl;
 | 
|---|
 | 111 |                       abort();
 | 
|---|
 | 112 |                     }
 | 
|---|
 | 113 |                 }
 | 
|---|
 | 114 |               else if (unfixed.find(othertri) != unfixed.end()) {
 | 
|---|
 | 115 |                   unfixed.erase(othertri);
 | 
|---|
 | 116 |                   fixed.insert(othertri);
 | 
|---|
 | 117 |                 }
 | 
|---|
 | 118 |             }
 | 
|---|
 | 119 |           // by this point all of tri's neighbors have had their
 | 
|---|
 | 120 |           // orientation fixed to match that of tri
 | 
|---|
 | 121 |           fixed.erase(tri);
 | 
|---|
 | 122 |           finished.insert(tri);
 | 
|---|
 | 123 |         }
 | 
|---|
 | 124 |     }
 | 
|---|
 | 125 | 
 | 
|---|
 | 126 |   if (_verbose) {
 | 
|---|
 | 127 |       ExEnv::outn()
 | 
|---|
 | 128 |           << scprintf("%d out of %d triangles were flipped\n", nflip, ntri);
 | 
|---|
 | 129 |     }
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 |   // just in case
 | 
|---|
 | 132 |   for (i=0; i<ne; i++) {
 | 
|---|
 | 133 |       edge_to_triangle0[i] = 0;
 | 
|---|
 | 134 |       edge_to_triangle1[i] = 0;
 | 
|---|
 | 135 |     }
 | 
|---|
 | 136 | 
 | 
|---|
 | 137 |   delete[] edge_to_triangle0;
 | 
|---|
 | 138 |   delete[] edge_to_triangle1;
 | 
|---|
 | 139 | }
 | 
|---|
 | 140 | 
 | 
|---|
 | 141 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 | // Local Variables:
 | 
|---|
 | 144 | // mode: c++
 | 
|---|
 | 145 | // c-file-style: "CLJ"
 | 
|---|
 | 146 | // End:
 | 
|---|