OpenStructure
composite3.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef GEOM_COMPOSITE3_HH
20 #define GEOM_COMPOSITE3_HH
21 
22 #include <iostream>
23 #include <vector>
24 #include <boost/filesystem.hpp>
25 
26 #include "vec3.hh"
27 #include "mat3.hh"
28 #include "quat.hh"
29 
30 
31 /*
32  composite classes in 3D space
33 */
34 
35 namespace geom {
36 
39 public:
40  Line3();
41  Line3(const Vec3& from, const Vec3& to);
42 
43  Vec3 At(Real r) const;
44  Vec3 GetOrigin() const;
45  Vec3 GetDirection() const;
46 private:
47  Vec3 ori_,dir_;
48 };
49 
50 std::ostream& operator<<(std::ostream& s, const Line3& l);
51 
53 public:
54  Plane():n_(Vec3(0,0,1)),p_(0){};
56  Plane(const Vec3& p1, const Vec3& p2, const Vec3& p3);
58  Plane(const Vec3& p, const Vec3& n);
60  Plane(const Line3& l, const Vec3& p);
62  Plane(Real a, Real b, Real c, Real d);
64  Plane(Real x, Real y, Real z);
65 
66  Vec3 GetOrigin() const;
67  void SetOrigin(const Vec3& o);
68  Vec3 GetNormal() const;
69  void SetNormal(const Vec3& n);
70  Real GetP() const;
71  Vec3 At(Real x, Real y) const;
72 
73  enum PLANE_TYPE {
74  YZ=0x1, ZY=0x1, XZ=0x2, ZX=0x2, XY=0x4, YX=0x4
75  };
76 
77 private:
78  // planes are internally specified in HNF
79  Vec3 n_;
80  Real p_;
81  Vec3 o_;
82 
83  void calc_ori();
84 };
85 
86 class DLLEXPORT Sphere {
87 public:
88  Sphere();
89  Sphere(const Vec3& origin, Real r);
90  Vec3 GetOrigin() const;
91  Real GetRadius() const;
92  void SetOrigin(const Vec3& v);
93  void SetRadius(Real r);
94 
95 private:
96  Vec3 origin_;
97  Real radius_;
98 };
99 
104 class DLLEXPORT CuboidAxis {
105 public:
106  CuboidAxis(): axis_(), half_extent_(0.0)
107  { }
108  CuboidAxis(const Vec3& axis, Real half_extent);
109  const Vec3& GetVector() const { return axis_; }
110  Real GetHalfExtent() const { return half_extent_; }
111  Real GetExtent() const { return 2.0*half_extent_; }
112 private:
113  Vec3 axis_;
114  Real half_extent_;
115 };
116 
118 class DLLEXPORT Cuboid {
119 public:
120  Cuboid();
121  Cuboid(const Vec3& center, const CuboidAxis& a,
122  const CuboidAxis& b, const CuboidAxis& c);
123 
124  Vec3 GetHalfExtents() const
125  {
126  return Vec3(axes_[0].GetHalfExtent(), axes_[1].GetHalfExtent(),
127  axes_[2].GetHalfExtent());
128  }
129 
130  Vec3 GetCenter() const
131  {
132  return center_;
133  }
134  const Vec3& GetVecA() const { return axes_[0].GetVector(); }
135  const Vec3& GetVecB() const { return axes_[1].GetVector(); }
136  const Vec3& GetVecC() const { return axes_[2].GetVector(); }
137 
138  const CuboidAxis& GetAxisA() const { return axes_[0]; }
139  const CuboidAxis& GetAxisB() const { return axes_[1]; }
140  const CuboidAxis& GetAxisC() const { return axes_[2]; }
141 private:
142  Vec3 center_;
143  CuboidAxis axes_[3];
144 };
145 
146 class DLLEXPORT Rotation3
147 {
148 public:
149  Rotation3();
150  Rotation3(Real phi, Real theta, Real psi,
151  const Vec3& origin=Vec3(0.0,0.0,0.0));
152  Rotation3(const Vec3& axis, Real angle,
153  const Vec3& origin=Vec3(0.0,0.0,0.0));
154  Rotation3(const Line3& line, Real angle);
155  Rotation3(const Mat3& rot, const Vec3& origin=Vec3(0.0,0.0,0.0));
156  Rotation3(const Quat& q, const Vec3& origin=Vec3(0.0,0.0,0.0));
157  Vec3 GetOrigin() const;
158  Real GetPhi() const;
159  Real GetTheta() const;
160  Real GetPsi() const;
161  Quat GetQuat() const;
162  Vec3 GetRotationAxis() const;
163  Real GetRotationAngle() const;
164  Mat3 GetRotationMatrix() const;
165  void SetOrigin(const Vec3& o);
166  void SetPhi(Real phi);
167  void SetTheta(Real theta);
168  void SetPsi(Real psi);
169  void SetQuat(const Quat& q);
170  void SetRotationAxis(const Vec3& v);
171  void SetRotationAngle(Real angle);
172  void SetRotationMatrix(const Mat3& rot);
173  Vec3 Apply(const Vec3&) const;
174  bool operator==(const Rotation3& rhs) const;
175 
176 private:
177  Quat generate_from_eulers(Real psi, Real theta, Real phi);
178  Quat generate_from_axis_angle(const Vec3& axis, Real angle);
179  Vec3 find_invariant_vector(Mat3 rot);
180  Vec3 find_orthogonal_vector(const Vec3& xyz);
181  Vec3 find_vector_for_BOD(const Vec3& xyz, const Vec3& uvw);
182  Quat generate_from_matrix(const Mat3& rot);
183  Real reduce_angle(Real ang) const;
184  Quat q_;
185  Vec3 origin_;
186 };
187 
188 typedef std::vector<Rotation3> Rotation3List;
189 
190 DLLEXPORT Rotation3List ImportEulerAngles (const boost::filesystem::path& loc);
191 DLLEXPORT void ExportEulerAngles (const Rotation3List& rot_list,
192  const boost::filesystem::path& loc);
193 
194 } // ns
195 
196 #endif