[831] | 1 | // |
---|
| 2 | // ******************************************************************** |
---|
| 3 | // * License and Disclaimer * |
---|
| 4 | // * * |
---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
| 7 | // * conditions of the Geant4 Software License, included in the file * |
---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
| 9 | // * include a list of copyright holders. * |
---|
| 10 | // * * |
---|
| 11 | // * Neither the authors of this software system, nor their employing * |
---|
| 12 | // * institutes,nor the agencies providing financial support for this * |
---|
| 13 | // * work make any representation or warranty, express or implied, * |
---|
| 14 | // * regarding this software system or assume any liability for its * |
---|
| 15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
| 16 | // * for the full disclaimer and the limitation of liability. * |
---|
| 17 | // * * |
---|
| 18 | // * This code implementation is the result of the scientific and * |
---|
| 19 | // * technical work of the GEANT4 collaboration. * |
---|
| 20 | // * By using, copying, modifying or distributing the software (or * |
---|
| 21 | // * any work based on the software) you agree to acknowledge its * |
---|
| 22 | // * use in resulting scientific publications, and indicate your * |
---|
| 23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 24 | // ******************************************************************** |
---|
| 25 | // |
---|
| 26 | // |
---|
[850] | 27 | // $Id: G4KnotVector.cc,v 1.11 2008/03/13 14:18:57 gcosmo Exp $ |
---|
[1228] | 28 | // GEANT4 tag $Name: geant4-09-03 $ |
---|
[831] | 29 | // |
---|
| 30 | // ---------------------------------------------------------------------- |
---|
| 31 | // GEANT 4 class source file |
---|
| 32 | // |
---|
| 33 | // G4KnotVector.cc |
---|
| 34 | // |
---|
| 35 | // ---------------------------------------------------------------------- |
---|
| 36 | |
---|
| 37 | #include "G4KnotVector.hh" |
---|
| 38 | #include "G4GeometryTolerance.hh" |
---|
| 39 | |
---|
| 40 | G4KnotVector::G4KnotVector() |
---|
| 41 | { |
---|
| 42 | knots=(G4double*)0; |
---|
| 43 | kCarTolerance = G4GeometryTolerance::GetInstance()->GetSurfaceTolerance(); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | G4KnotVector::G4KnotVector(G4int sz) |
---|
| 47 | { |
---|
| 48 | k_size=sz; |
---|
| 49 | knots = new G4double[k_size]; |
---|
| 50 | for(G4int a=0;a<k_size;a++) |
---|
| 51 | { knots[a]=0; } |
---|
| 52 | kCarTolerance = G4GeometryTolerance::GetInstance()->GetSurfaceTolerance(); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | G4KnotVector::~G4KnotVector() |
---|
| 57 | { |
---|
| 58 | delete [] knots; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | G4KnotVector::G4KnotVector(const G4KnotVector& orig) |
---|
| 62 | { |
---|
| 63 | delete [] knots; |
---|
| 64 | k_size = orig.k_size; |
---|
| 65 | knots = new G4double[k_size]; |
---|
| 66 | for(register G4int a=0; a < orig.k_size; a++) |
---|
| 67 | { knots[a] = orig.knots[a]; } |
---|
| 68 | kCarTolerance = orig.kCarTolerance; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | G4KnotVector& G4KnotVector::operator=(const G4KnotVector& right) |
---|
| 72 | { |
---|
| 73 | if (&right == this) return *this; |
---|
| 74 | delete [] knots; |
---|
| 75 | k_size = right.k_size; |
---|
| 76 | knots = new G4double[k_size]; |
---|
| 77 | for(register G4int a=0; a < right.k_size; a++) |
---|
| 78 | knots[a] = right.knots[a]; |
---|
| 79 | kCarTolerance = right.kCarTolerance; |
---|
| 80 | |
---|
| 81 | return *this; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | G4int G4KnotVector::GetKnotIndex(G4double k_value, G4int order) const |
---|
| 85 | { |
---|
| 86 | G4int i, knot_index; |
---|
| 87 | G4double knt; |
---|
| 88 | |
---|
| 89 | knt = knots[order - 1]; |
---|
| 90 | if ( k_value < knt ) |
---|
| 91 | { |
---|
| 92 | if (ApxEq( k_value, knt)) |
---|
| 93 | k_value = knt; |
---|
| 94 | else |
---|
| 95 | return -1; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | knt = knots[k_size - order + 1]; |
---|
| 99 | if ( k_value > knt ) |
---|
| 100 | { |
---|
| 101 | if (ApxEq( k_value, knt)) |
---|
| 102 | k_value = knt; |
---|
| 103 | else |
---|
| 104 | return -1; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | if ( k_value == knots[k_size - order + 1] ) |
---|
| 108 | knot_index = k_size - order - 1; |
---|
| 109 | else if ( k_value == knots[ order - 1] ) |
---|
| 110 | knot_index = order - 1; |
---|
| 111 | else |
---|
| 112 | { |
---|
| 113 | knot_index = 0; |
---|
| 114 | |
---|
| 115 | for ( i = 0; i < k_size - 1; i++) |
---|
| 116 | if((knots[i]<k_value) && (k_value <= knots[i+1])) |
---|
| 117 | knot_index = i; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | return knot_index; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | G4KnotVector* G4KnotVector::MultiplyKnotVector(G4int num, |
---|
| 125 | G4double val) |
---|
| 126 | { |
---|
| 127 | G4int n; |
---|
| 128 | G4double* knots_to_add; |
---|
| 129 | |
---|
| 130 | n = CheckKnotVector( val ); |
---|
| 131 | knots_to_add = new G4double[num-n]; |
---|
| 132 | |
---|
| 133 | for (G4int i = 0; i < num - n; i++) |
---|
| 134 | knots_to_add[i] = val; |
---|
| 135 | |
---|
| 136 | G4KnotVector* new_kv = new G4KnotVector(); |
---|
| 137 | new_kv->k_size = num - n + GetSize(); |
---|
| 138 | new_kv->knots = MergeKnotVector(knots_to_add, num-n); |
---|
| 139 | |
---|
| 140 | delete [] knots_to_add; |
---|
| 141 | |
---|
| 142 | return new_kv; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | G4double* G4KnotVector::MergeKnotVector(const G4double* knots_to_add, |
---|
| 147 | G4int add_size ) |
---|
| 148 | { |
---|
| 149 | G4double *newknots; |
---|
| 150 | G4int kv1_ptr = 0, |
---|
| 151 | kv2_ptr = 0, |
---|
| 152 | newptr; |
---|
| 153 | G4int old_size = k_size; |
---|
| 154 | |
---|
| 155 | newknots = new G4double[k_size + add_size]; |
---|
| 156 | |
---|
| 157 | for ( newptr = 0; newptr < k_size+add_size; newptr++) |
---|
| 158 | if ( kv1_ptr >= add_size ) |
---|
| 159 | newknots[newptr] = knots[kv2_ptr++]; |
---|
| 160 | else if ( kv2_ptr >= old_size ) |
---|
| 161 | newknots[newptr] = knots_to_add[kv1_ptr++]; |
---|
| 162 | else if ( knots_to_add[kv1_ptr] < knots[kv2_ptr]) |
---|
| 163 | newknots[newptr] = knots_to_add[kv1_ptr++]; |
---|
| 164 | else |
---|
| 165 | newknots[newptr] = knots[kv2_ptr++]; |
---|
| 166 | |
---|
| 167 | return newknots; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | G4int G4KnotVector::CheckKnotVector(G4double val) const |
---|
| 172 | { |
---|
| 173 | G4int num = 0; |
---|
| 174 | |
---|
| 175 | for ( G4int i = 0; i < k_size; i++) |
---|
| 176 | { |
---|
| 177 | // if ( std::abs(val - knots[i]) < kCarTolerance) |
---|
| 178 | if ( val == knots[i] ) |
---|
| 179 | num++; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | return num; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | void G4KnotVector::ExtractKnotVector( G4KnotVector* kv, |
---|
| 187 | G4int upper, G4int lower) |
---|
| 188 | { |
---|
| 189 | delete[] kv->knots; |
---|
| 190 | kv->k_size = upper-lower; |
---|
| 191 | kv->knots = new G4double[kv->k_size]; |
---|
| 192 | |
---|
| 193 | for ( G4int i = lower; i < upper; i++) |
---|
| 194 | kv->knots[i-lower] = knots[i]; |
---|
| 195 | } |
---|