[aa639ea] | 1 | /** \file Sphere.cc */ |
---|
| 2 | #include <cmath> |
---|
| 3 | #include <cassert> |
---|
| 4 | #include "sphere.h" |
---|
| 5 | #include "myutil.h" |
---|
| 6 | |
---|
| 7 | Sphere::Sphere() |
---|
| 8 | { |
---|
| 9 | r_ = 0; |
---|
| 10 | } |
---|
| 11 | |
---|
| 12 | Sphere::Sphere(double radius) |
---|
| 13 | { |
---|
| 14 | r_ = radius; |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | Sphere::~Sphere() |
---|
| 18 | { |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | void Sphere::SetRadius(double r) |
---|
| 22 | { |
---|
| 23 | r_ = r; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | double Sphere::GetRadius() |
---|
| 27 | { |
---|
| 28 | return r_; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | double Sphere::GetMaxRadius() |
---|
| 32 | { |
---|
| 33 | double maxr = r_; |
---|
| 34 | return maxr; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | ShapeType Sphere::GetShapeType() const |
---|
| 38 | { |
---|
| 39 | return SPHERE; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | double Sphere::GetVolume() |
---|
| 43 | { |
---|
| 44 | double V = 4 * pi / 3 * triple(r_); |
---|
| 45 | return V; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | void Sphere::GetFormFactor(IQ * iq) |
---|
| 49 | { |
---|
| 50 | /** number of I for output, equal to the number of rows of array IQ*/ |
---|
| 51 | int numI = iq->iq_data.dim1(); |
---|
| 52 | double qmin = iq->GetQmin(); |
---|
| 53 | double qmax = iq->GetQmax(); |
---|
| 54 | |
---|
| 55 | assert(numI > 0); |
---|
| 56 | assert(qmin > 0); |
---|
| 57 | assert(qmax > 0); |
---|
| 58 | assert( qmax > qmin ); |
---|
| 59 | |
---|
| 60 | double logMin = log10(qmin); |
---|
| 61 | double z = logMin; |
---|
| 62 | double logMax = log10(qmax); |
---|
| 63 | double delta = (logMax - logMin) / (numI-1); |
---|
| 64 | |
---|
| 65 | for(int i = 0; i < numI; ++i) { |
---|
| 66 | |
---|
| 67 | /** temp for Q*/ |
---|
| 68 | double q = pow(z, 10); |
---|
| 69 | |
---|
| 70 | double bes = 3.0 * (sin(q*r_) - q*r_*cos(q*r_)) / triple(q) / triple(r_); |
---|
| 71 | |
---|
| 72 | /** double f is the temp for I, should be equal to one when q is 0*/ |
---|
| 73 | double f = bes * bes; |
---|
| 74 | |
---|
| 75 | /** IQ[i][0] is Q,Q starts from qmin (non zero),q=0 handle separately IQ[i][1] is IQ*/ |
---|
| 76 | iq->iq_data[i][0]= q; |
---|
| 77 | iq->iq_data[i][1]= f; |
---|
| 78 | |
---|
| 79 | z += delta; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | Point3D Sphere::GetAPoint(double sld) |
---|
| 84 | { |
---|
| 85 | static int max_try = 100; |
---|
| 86 | for (int i = 0; i < max_try; ++i) { |
---|
| 87 | double x = (ran1()-0.5) * 2 * r_; |
---|
| 88 | double y = (ran1()-0.5) * 2 * r_; |
---|
| 89 | double z = (ran1()-0.5) * 2 * r_; |
---|
| 90 | |
---|
| 91 | Point3D apoint(x,y,z,sld); |
---|
| 92 | if (apoint.distanceToPoint(Point3D(0,0,0)) <=r_ ) //dist to origin give sphere shape |
---|
| 93 | return apoint; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | std::cerr << "Max try " |
---|
| 97 | << max_try |
---|
| 98 | << " is reached while generating a point in sphere" << std::endl; |
---|
| 99 | return Point3D(0, 0, 0); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | bool Sphere::IsInside(const Point3D& point) const |
---|
| 103 | { |
---|
| 104 | return point.distanceToPoint(Point3D(center_[0], center_[1], center_[2])) <= r_; |
---|
| 105 | cout << "distance = " << point.distanceToPoint(Point3D(center_[0], center_[1], center_[2])) << endl; |
---|
| 106 | } |
---|