| 1 | //
 | 
|---|
| 2 | // edge.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 | #ifdef __GNUC__
 | 
|---|
| 29 | #pragma implementation
 | 
|---|
| 30 | #endif
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #include <math/isosurf/triangle.h>
 | 
|---|
| 33 | #include <math/isosurf/edge.h>
 | 
|---|
| 34 | 
 | 
|---|
| 35 | using namespace std;
 | 
|---|
| 36 | using namespace sc;
 | 
|---|
| 37 | 
 | 
|---|
| 38 | /////////////////////////////////////////////////////////////////////////
 | 
|---|
| 39 | // Edge
 | 
|---|
| 40 | 
 | 
|---|
| 41 | Edge::Edge(const Ref<Vertex> &p1,
 | 
|---|
| 42 |      const Ref<Vertex> &p2,
 | 
|---|
| 43 |      const Ref<Vertex> &p3)
 | 
|---|
| 44 | {
 | 
|---|
| 45 |   _order = 2;
 | 
|---|
| 46 |   _vertices = new Ref<Vertex>[3];
 | 
|---|
| 47 |   _vertices[0]=p1; _vertices[1]=p2; _vertices[2]=p3;
 | 
|---|
| 48 | }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | Edge::Edge(const Ref<Vertex> &p1,
 | 
|---|
| 51 |      const Ref<Vertex> &p2,
 | 
|---|
| 52 |      const Ref<Vertex> &p3,
 | 
|---|
| 53 |      const Ref<Vertex> &p4)
 | 
|---|
| 54 | {
 | 
|---|
| 55 |   _order = 3;
 | 
|---|
| 56 |   _vertices = new Ref<Vertex>[4];
 | 
|---|
| 57 |   _vertices[0]=p1; _vertices[1]=p2; _vertices[2]=p3; _vertices[3]=p4;
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | Edge::~Edge()
 | 
|---|
| 61 | {
 | 
|---|
| 62 |   delete[] _vertices;
 | 
|---|
| 63 | }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | double Edge::straight_length()
 | 
|---|
| 66 | {
 | 
|---|
| 67 |   SCVector3 BA = vertex(1)->point() - vertex(0)->point();
 | 
|---|
| 68 |   return BA.norm();
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | void Edge::add_vertices(std::set<Ref<Vertex> >&set)
 | 
|---|
| 72 | {
 | 
|---|
| 73 |   set.insert(_vertices[0]);
 | 
|---|
| 74 |   set.insert(_vertices[_order]);
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | void
 | 
|---|
| 78 | Edge::set_order(int order, const Ref<Volume>&vol,double isovalue)
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   Ref<Vertex> *newvertices = new Ref<Vertex>[order+1];
 | 
|---|
| 81 |   newvertices[0] = vertex(0);
 | 
|---|
| 82 |   newvertices[order] = vertex(1);
 | 
|---|
| 83 |   delete[] _vertices;
 | 
|---|
| 84 |   _vertices = newvertices;
 | 
|---|
| 85 |   _order = order;
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   SCVector3 pv[2];
 | 
|---|
| 88 |   SCVector3 norm[2];
 | 
|---|
| 89 | 
 | 
|---|
| 90 |   int i;
 | 
|---|
| 91 |   for (i=0; i<2; i++) {
 | 
|---|
| 92 |       pv[i] = vertex(i)->point();
 | 
|---|
| 93 |       norm[i] = vertex(i)->normal();
 | 
|---|
| 94 |     }
 | 
|---|
| 95 | 
 | 
|---|
| 96 |   for (i=1; i<_order; i++) {
 | 
|---|
| 97 |       double I = (1.0*i)/order;
 | 
|---|
| 98 |       double J = (1.0*(_order - i))/order;
 | 
|---|
| 99 |       SCVector3 interpv = I * pv[0] + J * pv[1];
 | 
|---|
| 100 |       SCVector3 start(interpv);
 | 
|---|
| 101 |       SCVector3 interpnorm = I * norm[0] + J * norm[1];
 | 
|---|
| 102 |       SCVector3 newpoint;
 | 
|---|
| 103 |       vol->solve(start,interpnorm,isovalue,newpoint);
 | 
|---|
| 104 |       vol->set_x(newpoint);
 | 
|---|
| 105 |       if (vol->gradient_implemented()) {
 | 
|---|
| 106 |           vol->get_gradient(interpnorm);
 | 
|---|
| 107 |         }
 | 
|---|
| 108 |       interpnorm.normalize();
 | 
|---|
| 109 |       _vertices[i] = new Vertex(newpoint,interpnorm);
 | 
|---|
| 110 |     }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | int
 | 
|---|
| 115 | Edge::interpolate(double r, SCVector3&point, SCVector3&norm)
 | 
|---|
| 116 | {
 | 
|---|
| 117 |   int i;
 | 
|---|
| 118 |   double s = 1.0 - r;
 | 
|---|
| 119 |   double rcoef[Triangle::max_order+1];
 | 
|---|
| 120 |   double scoef[Triangle::max_order+1];
 | 
|---|
| 121 |   double spacing = 1.0/_order;
 | 
|---|
| 122 |   rcoef[0] = 1.0;
 | 
|---|
| 123 |   scoef[0] = 1.0;
 | 
|---|
| 124 |   for (i=1; i<=_order; i++) {
 | 
|---|
| 125 |       rcoef[i] = rcoef[i-1]*(r-(i-1)*spacing)/(i*spacing);
 | 
|---|
| 126 |       scoef[i] = scoef[i-1]*(s-(i-1)*spacing)/(i*spacing);
 | 
|---|
| 127 |     }
 | 
|---|
| 128 |   int has_norm = 1;
 | 
|---|
| 129 |   for (i=0; i<=_order; i++) {
 | 
|---|
| 130 |       if ((has_norm = (has_norm && _vertices[i]->has_normal()))) break;
 | 
|---|
| 131 |     }
 | 
|---|
| 132 |   point = 0.0;
 | 
|---|
| 133 |   norm = 0.0;
 | 
|---|
| 134 |   for (i=0; i<=_order; i++) {
 | 
|---|
| 135 |       int j=_order-i;
 | 
|---|
| 136 |       double coef = rcoef[i]*scoef[j];
 | 
|---|
| 137 |       point += coef * _vertices[i]->point();
 | 
|---|
| 138 |       if (has_norm) norm += coef * _vertices[i]->normal();
 | 
|---|
| 139 |     }
 | 
|---|
| 140 |   if (has_norm) norm.normalize();
 | 
|---|
| 141 |   return has_norm;
 | 
|---|
| 142 | }
 | 
|---|
| 143 | 
 | 
|---|
| 144 | int
 | 
|---|
| 145 | Edge::interpolate(double r, SCVector3&point, SCVector3&norm,
 | 
|---|
| 146 |                   const Ref<Volume> &vol, double isovalue)
 | 
|---|
| 147 | {
 | 
|---|
| 148 |   // first guess
 | 
|---|
| 149 |   int has_norm = interpolate(r,point,norm);
 | 
|---|
| 150 | 
 | 
|---|
| 151 |   if (!has_norm) {
 | 
|---|
| 152 |       ExEnv::errn() << "Edge::interpolate with volume requires norm"
 | 
|---|
| 153 |            << endl;
 | 
|---|
| 154 |       abort();
 | 
|---|
| 155 |     }
 | 
|---|
| 156 | 
 | 
|---|
| 157 |   // refine guess
 | 
|---|
| 158 |   SCVector3 newpoint;
 | 
|---|
| 159 |   vol->solve(point,norm,isovalue,newpoint);
 | 
|---|
| 160 |   // compute the true normal
 | 
|---|
| 161 |   vol->set_x(newpoint);
 | 
|---|
| 162 |   if (vol->gradient_implemented()) {
 | 
|---|
| 163 |       vol->get_gradient(norm);
 | 
|---|
| 164 |       norm.normalize();
 | 
|---|
| 165 |     }
 | 
|---|
| 166 | 
 | 
|---|
| 167 |   return has_norm || vol->gradient_implemented();
 | 
|---|
| 168 | }
 | 
|---|
| 169 | 
 | 
|---|
| 170 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
| 171 | 
 | 
|---|
| 172 | // Local Variables:
 | 
|---|
| 173 | // mode: c++
 | 
|---|
| 174 | // c-file-style: "CLJ"
 | 
|---|
| 175 | // End:
 | 
|---|