| [0b990d] | 1 | //
 | 
|---|
 | 2 | // volume.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 <util/misc/math.h>
 | 
|---|
 | 33 | #include <util/misc/formio.h>
 | 
|---|
 | 34 | #include <util/keyval/keyval.h>
 | 
|---|
 | 35 | #include <math/scmat/vector3.h>
 | 
|---|
 | 36 | #include <math/scmat/local.h>
 | 
|---|
 | 37 | #include <math/isosurf/volume.h>
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | using namespace std;
 | 
|---|
 | 40 | using namespace sc;
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | static ClassDesc Volume_cd(
 | 
|---|
 | 43 |   typeid(Volume),"Volume",1,"public Function",
 | 
|---|
 | 44 |   0, 0, 0);
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 | Volume::Volume():
 | 
|---|
 | 47 |   _interp_acc(1.0e-6)
 | 
|---|
 | 48 | {
 | 
|---|
 | 49 |   set_dimension(new SCDimension(3));
 | 
|---|
 | 50 | }
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 | Volume::Volume(const Ref<KeyVal>&keyval):
 | 
|---|
 | 53 |   Function(keyval)
 | 
|---|
 | 54 | {
 | 
|---|
 | 55 |   set_dimension(new SCDimension(3));
 | 
|---|
 | 56 |   _interp_acc = keyval->doublevalue("interpolation_accuracy");
 | 
|---|
 | 57 |   if (keyval->error() != KeyVal::OK) _interp_acc = 1.0e-6;
 | 
|---|
 | 58 | }
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 | Volume::~Volume()
 | 
|---|
 | 61 | {
 | 
|---|
 | 62 | }
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 | // interpolate using the bisection algorithm
 | 
|---|
 | 65 | void
 | 
|---|
 | 66 | Volume::interpolate(const SCVector3& A,
 | 
|---|
 | 67 |                     const SCVector3& B,
 | 
|---|
 | 68 |                     double val,
 | 
|---|
 | 69 |                     SCVector3& result)
 | 
|---|
 | 70 | {
 | 
|---|
 | 71 |   set_x(A);
 | 
|---|
 | 72 |   double value0 = value() - val;
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 |   set_x(B);
 | 
|---|
 | 75 |   double value1 = value() - val;
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 |   if (value0*value1 > 0.0) {
 | 
|---|
 | 78 |       failure("interpolate(): values at endpoints don't bracket val");
 | 
|---|
 | 79 |     }
 | 
|---|
 | 80 |   else if (value0 == 0.0) {
 | 
|---|
 | 81 |       result = A;
 | 
|---|
 | 82 |       return;
 | 
|---|
 | 83 |     }
 | 
|---|
 | 84 |   else if (value1 == 0.0) {
 | 
|---|
 | 85 |       result = B;
 | 
|---|
 | 86 |       return;
 | 
|---|
 | 87 |     }
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 |   SCVector3 BA = B - A;
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 |   double length = sqrt(BA.dot(BA));
 | 
|---|
 | 92 |   int niter = (int) (log(length/_interp_acc)/M_LN2);
 | 
|---|
 | 93 | 
 | 
|---|
 | 94 |   double f0 = 0.0;
 | 
|---|
 | 95 |   double f1 = 1.0;
 | 
|---|
 | 96 |   double fnext = 0.5;
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 |   for (int i=0; i<niter; i++) {
 | 
|---|
 | 99 |       SCVector3 X = A + fnext*BA;
 | 
|---|
 | 100 |       set_x(X);
 | 
|---|
 | 101 |       double valuenext = value() - val;
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 |       if (valuenext*value0 <= 0.0) {
 | 
|---|
 | 104 |           value1 = valuenext;
 | 
|---|
 | 105 |           f1 = fnext;
 | 
|---|
 | 106 |           fnext = (f0 + fnext)*0.5;
 | 
|---|
 | 107 |         }
 | 
|---|
 | 108 |       else {
 | 
|---|
 | 109 |           value0 = valuenext;
 | 
|---|
 | 110 |           f0 = fnext;
 | 
|---|
 | 111 |           fnext = (fnext + f1)*0.5;
 | 
|---|
 | 112 |         }
 | 
|---|
 | 113 |     }
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 |   result = A + fnext*BA;
 | 
|---|
 | 116 | }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 | void
 | 
|---|
 | 119 | Volume::solve(const SCVector3& start,
 | 
|---|
 | 120 |               const SCVector3& grad,
 | 
|---|
 | 121 |               double val,
 | 
|---|
 | 122 |               SCVector3& result)
 | 
|---|
 | 123 | {
 | 
|---|
 | 124 |   double direction;
 | 
|---|
 | 125 |   set_x(start);
 | 
|---|
 | 126 |   double startvalue = value();
 | 
|---|
 | 127 |   if (startvalue == val) {
 | 
|---|
 | 128 |       result = start;
 | 
|---|
 | 129 |       return;
 | 
|---|
 | 130 |     }
 | 
|---|
 | 131 |   else if (startvalue < val) direction = 1.0;
 | 
|---|
 | 132 |   else direction = -1.0;
 | 
|---|
 | 133 |   int i=0;
 | 
|---|
 | 134 |   SCVector3 next;
 | 
|---|
 | 135 |   double trialvalue;
 | 
|---|
 | 136 |   do {
 | 
|---|
 | 137 |       if (i>10) {
 | 
|---|
 | 138 |           ExEnv::errn() << "Volume::solve: couldn't find end points" << endl;
 | 
|---|
 | 139 |           abort();
 | 
|---|
 | 140 |         }
 | 
|---|
 | 141 |       i++;
 | 
|---|
 | 142 |       next = start + (direction*i)*grad;
 | 
|---|
 | 143 |       set_x(next);
 | 
|---|
 | 144 |       trialvalue = value();
 | 
|---|
 | 145 |     } while ((startvalue-val)*(trialvalue-val)>0.0);
 | 
|---|
 | 146 |   interpolate(start,next,val,result);
 | 
|---|
 | 147 | }
 | 
|---|
 | 148 | 
 | 
|---|
 | 149 | void
 | 
|---|
 | 150 | Volume::failure(const char * msg)
 | 
|---|
 | 151 | {
 | 
|---|
 | 152 |   ExEnv::errn() << scprintf("Volume::failure: \"%s\"\n",msg);
 | 
|---|
 | 153 |   abort();
 | 
|---|
 | 154 | }
 | 
|---|
 | 155 | 
 | 
|---|
 | 156 | void
 | 
|---|
 | 157 | Volume::set_gradient(const SCVector3& g)
 | 
|---|
 | 158 | {
 | 
|---|
 | 159 |   RefSCVector grad(dimension(), matrixkit());
 | 
|---|
 | 160 |   grad.set_element(0, g[0]);
 | 
|---|
 | 161 |   grad.set_element(1, g[1]);
 | 
|---|
 | 162 |   grad.set_element(2, g[2]);
 | 
|---|
 | 163 |   set_gradient(grad);
 | 
|---|
 | 164 | }
 | 
|---|
 | 165 | 
 | 
|---|
 | 166 | void
 | 
|---|
 | 167 | Volume::set_gradient(RefSCVector& g)
 | 
|---|
 | 168 | {
 | 
|---|
 | 169 |   Function::set_gradient(g);
 | 
|---|
 | 170 | }
 | 
|---|
 | 171 | 
 | 
|---|
 | 172 | void
 | 
|---|
 | 173 | Volume::get_gradient(SCVector3& g)
 | 
|---|
 | 174 | {
 | 
|---|
 | 175 |   const RefSCVector v = gradient();
 | 
|---|
 | 176 |   g[0] = v.get_element(0);
 | 
|---|
 | 177 |   g[1] = v.get_element(1);
 | 
|---|
 | 178 |   g[2] = v.get_element(2);
 | 
|---|
 | 179 | }
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 | void
 | 
|---|
 | 182 | Volume::set_x(const SCVector3& x)
 | 
|---|
 | 183 | {
 | 
|---|
 | 184 |   RefSCVector xx(dimension(), matrixkit());
 | 
|---|
 | 185 |   xx.set_element(0, x[0]);
 | 
|---|
 | 186 |   xx.set_element(1, x[1]);
 | 
|---|
 | 187 |   xx.set_element(2, x[2]);
 | 
|---|
 | 188 |   set_x(xx);
 | 
|---|
 | 189 | }
 | 
|---|
 | 190 | 
 | 
|---|
 | 191 | void
 | 
|---|
 | 192 | Volume::set_x(const RefSCVector& x)
 | 
|---|
 | 193 | {
 | 
|---|
 | 194 |   Function::set_x(x);
 | 
|---|
 | 195 | }
 | 
|---|
 | 196 | 
 | 
|---|
 | 197 | void
 | 
|---|
 | 198 | Volume::get_x(SCVector3& x)
 | 
|---|
 | 199 | {
 | 
|---|
 | 200 |   const RefSCVector& v = get_x_no_copy();
 | 
|---|
 | 201 |   x[0] = v.get_element(0);
 | 
|---|
 | 202 |   x[1] = v.get_element(1);
 | 
|---|
 | 203 |   x[2] = v.get_element(2);
 | 
|---|
 | 204 | }
 | 
|---|
 | 205 | 
 | 
|---|
 | 206 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 207 | 
 | 
|---|
 | 208 | // Local Variables:
 | 
|---|
 | 209 | // mode: c++
 | 
|---|
 | 210 | // c-file-style: "CLJ"
 | 
|---|
 | 211 | // End:
 | 
|---|