source: sasview/realSpaceModeling/geoshapespy/libgeoshapespy/Point3D.cc @ bd66af34

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since bd66af34 was f2d6445, checked in by Mathieu Doucet <doucetm@…>, 17 years ago

moving realSpaceModeling to trunk

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[f2d6445]1//Point3D.cpp 
2
3#include <cmath>
4#include <cstdlib>
5#include <stdexcept>
6#include "Point3D.h"
7
8using namespace std;
9
10Point3D::Point3D(double a, double b, double c, double sld)
11{
12  x = a;
13  y = b;
14  z = c;
15  sld_ =  sld;
16}
17
18ostream& operator<< (ostream& os, const Point3D& p)
19{
20  os << "(" << p.getX() << ", " << p.getY() << ", " << p.getZ() << "," << p.getSLD() << ")";
21
22  return os;
23}
24
25double Point3D::distanceToPoint(const Point3D &p) const
26{
27        double dx=x-p.x;
28        double dy=y-p.y;
29        double dz=z-p.z;
30        return sqrt(dx*dx+dy*dy+dz*dz);
31}
32
33double Point3D::distanceToLine(const Point3D &p1, const Point3D &p2, bool * pIsOutside /* 0 */) const
34{
35        double u = ((x-p1.x)*(p2.x-p1.x) + (y-p1.y)*(p2.y-p1.y) + (z-p1.z)*(p2.z-p1.z))/(p1.distanceToPoint(p2)*p1.distanceToPoint(p2));
36       
37        if(pIsOutside != 0) {
38                if ( u < 0 || u > 1)
39                        *pIsOutside=true;
40                else 
41                        *pIsOutside=false;
42        }
43
44        double interX=p1.x+u*(p2.x-p1.x);
45        double interY=p1.y+u*(p2.y-p1.y);
46        double interZ=p1.z+u*(p2.z-p1.z);
47       
48        return sqrt((x-interX)*(x-interX)+(y-interY)*(y-interY)+(z-interZ)*(z-interZ));
49}
50
51// p1 and p2 determine a line, they are two end points
52Point3D Point3D::getInterPoint(const Point3D &p1, const Point3D &p2, bool * pIsOutside /* 0 */) const
53{
54        double u = ((x-p1.x)*(p2.x-p1.x) + (y-p1.y)*(p2.y-p1.y) + (z-p1.z)*(p2.z-p1.z))/(p1.distanceToPoint(p2)*p1.distanceToPoint(p2));
55       
56        if(pIsOutside != 0) {
57
58                if ( u < 0 || u > 1)
59                        *pIsOutside=true;
60                else 
61                        *pIsOutside=false;
62        }
63
64        return Point3D(p1.x+u*(p2.x-p1.x), p1.y+u*(p2.y-p1.y), p1.z+u*(p2.z-p1.z));
65}
66
67void Point3D::set(double x1, double y1, double z1)
68{
69        x = x1;
70        y = y1;
71        z = z1;
72}
73
74double Point3D::norm() const
75{
76        return sqrt(x * x + y * y + z * z);
77}
78
79double Point3D::normalize()
80{
81        double v = norm();
82       
83        if(v != 0) {
84                x /= v;
85                y /= v;
86                z /= v;
87        }
88       
89        return v;
90}
91
92Point3D Point3D::normVector() const
93{
94        Point3D p;     
95        double v = norm();
96       
97        if(v != 0) {
98                p.x = x / v;
99                p.y = y / v;
100                p.z = z / v;
101        }
102
103        return p;
104}
105
106double Point3D::dotProduct(const Point3D &p) const
107{
108        return x * p.x + y * p.y + z * p.z;
109}
110
111Point3D Point3D::minus(const Point3D &p) const
112{
113        return Point3D(x - p.x, y - p.y, z - p.z);
114}
115
116Point3D Point3D::plus(const Point3D &p) const
117{
118        return Point3D(x + p.x, y + p.y, z + p.z);
119}
120
121Point3D& Point3D::operator=(const Point3D &p)
122{
123        x = p.x;
124        y = p.y;
125        z = p.z;
126        sld_ = p.sld_;
127
128        return *this;
129}
130
131void Point3D::scale(double s)
132{
133        x *= s;
134        y *= s;
135        z *= s;
136}
137
138Point3D Point3D::multiplyProduct(const Point3D &p)
139{
140        return Point3D(y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x);
141}
142
143void Point3D::Transform(const vector<double> &orien, const vector<double> &center){
144  if(orien[1]) RotateY(Degree2Radian(orien[1]));
145  if(orien[0]) RotateX(Degree2Radian(orien[0]));
146  if(orien[2]) RotateZ(Degree2Radian(orien[2]));
147
148  Translate(center[0],center[1],center[2]);
149}
150
151void Point3D::RotateX(const double ang_x)
152{
153  double sinA_ = sin(ang_x);
154  double cosA_ = cos(ang_x);
155
156  //x doesn't change;
157  double y_new= y*cosA_ - z*sinA_;
158  double z_new= y*sinA_ + z*cosA_;
159
160  y = y_new;
161  z = z_new;
162}
163
164void Point3D::RotateY(const double ang_y)
165{
166  double sinA_ = sin(ang_y);
167  double cosA_ = cos(ang_y);
168 
169  double x_new = z*sinA_ + x*cosA_;
170  //y doesn't change
171  double z_new = z*cosA_ - x*sinA_;
172
173  x = x_new;
174  z = z_new;
175}
176
177void Point3D::RotateZ(const double ang_z)
178{
179  double sinA_ = sin(ang_z);
180  double cosA_ = cos(ang_z);
181
182  double x_new = x*cosA_ - y*sinA_;
183  double y_new = x*sinA_ + y*cosA_;
184  //z doesn't change
185
186  x = x_new;
187  y = y_new;
188}
189
190void Point3D::Translate(const double trans_x, const double trans_y, const double trans_z)
191{
192  double x_new = x + trans_x;
193  double y_new = y + trans_y;
194  double z_new = z + trans_z;
195
196  x = x_new;
197  y = y_new;
198  z = z_new;
199}
200
201double Point3D::Degree2Radian(const double degree)
202{
203  return degree/180*pi;
204}
205
206void Point3D::TransformMatrix(const vector<double> &rotmatrix, const vector<double> &center)
207{
208  if (rotmatrix.size() != 9 || center.size() != 3)
209    throw std::runtime_error("The size for rotation matrix vector has to be 9.");
210
211  double xold = x;
212  double yold = y;
213  double zold = z;
214
215  x = rotmatrix[0]*xold + rotmatrix[1]*yold + rotmatrix[2]*zold;
216  y = rotmatrix[3]*xold + rotmatrix[4]*yold + rotmatrix[5]*zold;
217  z = rotmatrix[6]*xold + rotmatrix[7]*yold + rotmatrix[8]*zold;
218
219  Translate(center[0],center[1],center[2]);
220}
Note: See TracBrowser for help on using the repository browser.