[f2d6445] | 1 | /** \file Cylinder.cc */ |
---|
| 2 | #include <cmath> |
---|
| 3 | #include <cassert> |
---|
| 4 | #include "cylinder.h" |
---|
| 5 | #include "myutil.h" |
---|
| 6 | |
---|
| 7 | Cylinder::Cylinder() |
---|
| 8 | { |
---|
| 9 | r_ = 0; |
---|
| 10 | } |
---|
| 11 | |
---|
| 12 | Cylinder::Cylinder(double radius,double length) |
---|
| 13 | { |
---|
| 14 | r_ = radius; |
---|
| 15 | l_ = length; |
---|
| 16 | topcenter_ = Point3D(0,length/2,0); |
---|
| 17 | botcenter_ = Point3D(0,-length/2,0); |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | Cylinder::~Cylinder() |
---|
| 21 | { |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | void Cylinder::SetRadius(double r) |
---|
| 25 | { |
---|
| 26 | r_ = r; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | void Cylinder::SetLength(double l) |
---|
| 30 | { |
---|
| 31 | l_ = l; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | double Cylinder::GetRadius() |
---|
| 35 | { |
---|
| 36 | return r_; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | double Cylinder::GetLength() |
---|
| 40 | { |
---|
| 41 | return l_; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | double Cylinder::GetMaxRadius() |
---|
| 45 | { |
---|
| 46 | double maxr = sqrt(4*r_*r_ + l_*l_)/2; |
---|
| 47 | return maxr; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | ShapeType Cylinder::GetShapeType() const |
---|
| 51 | { |
---|
| 52 | return CYLINDER; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | double Cylinder::GetVolume() |
---|
| 56 | { |
---|
| 57 | double V = pi * square(r_) * l_; |
---|
| 58 | return V; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | void Cylinder::GetFormFactor(IQ * iq) |
---|
| 62 | { |
---|
| 63 | /** number of I for output, equal to the number of rows of array IQ*/ |
---|
| 64 | /** to be finished */ |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | Point3D Cylinder::GetAPoint(double sld) |
---|
| 68 | { |
---|
| 69 | /** cylinder long axis is along Y to match vtk actor */ |
---|
| 70 | static int max_try = 100; |
---|
| 71 | for (int i = 0; i < max_try; ++i) { |
---|
| 72 | double x = (ran1()-0.5) * 2 * r_; |
---|
| 73 | double z = (ran1()-0.5) * 2 * r_; |
---|
| 74 | double y = (ran1()-0.5) * l_; |
---|
| 75 | |
---|
| 76 | Point3D apoint(x,y,z,sld); |
---|
| 77 | //check the cross section on xy plane within a sphere at (0,) |
---|
| 78 | if (apoint.distanceToPoint(Point3D(0,y,0)) <= r_ ) |
---|
| 79 | return apoint; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | std::cerr << "Max try " |
---|
| 83 | << max_try |
---|
| 84 | << " is reached while generating a point in cylinder" << std::endl; |
---|
| 85 | return Point3D(0, 0, 0); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | bool Cylinder::IsInside(const Point3D& point) const |
---|
| 89 | { |
---|
| 90 | bool isOutside = false; |
---|
| 91 | double distToLine = point.distanceToLine(GetTopCenter(),GetBotCenter(),&isOutside); |
---|
| 92 | |
---|
| 93 | return (distToLine <= r_ && !isOutside); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | Point3D Cylinder::GetTopCenter() const |
---|
| 97 | { |
---|
| 98 | Point3D new_center(topcenter_); |
---|
| 99 | new_center.Transform(GetOrientation(),GetCenter()); |
---|
| 100 | return new_center; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | Point3D Cylinder::GetBotCenter() const |
---|
| 104 | { |
---|
| 105 | Point3D new_center(botcenter_); |
---|
| 106 | new_center.Transform(GetOrientation(),GetCenter()); |
---|
| 107 | return new_center; |
---|
| 108 | } |
---|