OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/path.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 { return -p_*n_; }
67  Vec3 GetNormal() const;
68  void SetNormal(const Vec3& n);
69  Real GetP() const;
70  void SetP(Real p) { p_=p; }
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 };
82 
83 class DLLEXPORT Sphere {
84 public:
85  Sphere();
86  Sphere(const Vec3& origin, Real r);
87  Vec3 GetOrigin() const;
88  Real GetRadius() const;
89  void SetOrigin(const Vec3& v);
90  void SetRadius(Real r);
91 
92 private:
93  Vec3 origin_;
94  Real radius_;
95 };
96 
101 class DLLEXPORT CuboidAxis {
102 public:
103  CuboidAxis(): axis_(), half_extent_(0.0)
104  { }
105  CuboidAxis(const Vec3& axis, Real half_extent);
106  const Vec3& GetVector() const { return axis_; }
107  Real GetHalfExtent() const { return half_extent_; }
108  Real GetExtent() const { return 2.0*half_extent_; }
109 private:
110  Vec3 axis_;
111  Real half_extent_;
112 };
113 
115 class DLLEXPORT Cuboid {
116 public:
117  Cuboid();
118  Cuboid(const Vec3& center, const CuboidAxis& a,
119  const CuboidAxis& b, const CuboidAxis& c);
120 
121  Vec3 GetHalfExtents() const
122  {
123  return Vec3(axes_[0].GetHalfExtent(), axes_[1].GetHalfExtent(),
124  axes_[2].GetHalfExtent());
125  }
126 
127  Vec3 GetCenter() const
128  {
129  return center_;
130  }
131  const Vec3& GetVecA() const { return axes_[0].GetVector(); }
132  const Vec3& GetVecB() const { return axes_[1].GetVector(); }
133  const Vec3& GetVecC() const { return axes_[2].GetVector(); }
134 
135  const CuboidAxis& GetAxisA() const { return axes_[0]; }
136  const CuboidAxis& GetAxisB() const { return axes_[1]; }
137  const CuboidAxis& GetAxisC() const { return axes_[2]; }
138 private:
139  Vec3 center_;
140  CuboidAxis axes_[3];
141 };
142 
143 class DLLEXPORT Rotation3
144 {
145 public:
146  Rotation3();
147  Rotation3(Real phi, Real theta, Real psi,
148  const Vec3& origin=Vec3(0.0,0.0,0.0));
149  Rotation3(const Vec3& axis, Real angle,
150  const Vec3& origin=Vec3(0.0,0.0,0.0));
151  Rotation3(const Line3& line, Real angle);
152  Rotation3(const Mat3& rot, const Vec3& origin=Vec3(0.0,0.0,0.0));
153  Rotation3(const Quat& q, const Vec3& origin=Vec3(0.0,0.0,0.0));
154  Vec3 GetOrigin() const;
155  Real GetPhi() const;
156  Real GetTheta() const;
157  Real GetPsi() const;
158  Quat GetQuat() const;
159  Vec3 GetRotationAxis() const;
160  Real GetRotationAngle() const;
161  Mat3 GetRotationMatrix() const;
162  void SetOrigin(const Vec3& o);
163  void SetPhi(Real phi);
164  void SetTheta(Real theta);
165  void SetPsi(Real psi);
166  void SetQuat(const Quat& q);
167  void SetRotationAxis(const Vec3& v);
168  void SetRotationAngle(Real angle);
169  void SetRotationMatrix(const Mat3& rot);
170  Vec3 Apply(const Vec3&) const;
171  bool operator==(const Rotation3& rhs) const;
172 
173 private:
174  Quat generate_from_eulers(Real psi, Real theta, Real phi);
175  Quat generate_from_axis_angle(const Vec3& axis, Real angle);
176  Vec3 find_invariant_vector(Mat3 rot);
177  Vec3 find_orthogonal_vector(const Vec3& xyz);
178  Vec3 find_vector_for_BOD(const Vec3& xyz, const Vec3& uvw);
179  Quat generate_from_matrix(const Mat3& rot);
180  Real reduce_angle(Real ang) const;
181  Quat q_;
182  Vec3 origin_;
183 };
184 
185 typedef std::vector<Rotation3> Rotation3List;
186 
187 DLLEXPORT Rotation3List ImportEulerAngles (const boost::filesystem::path& loc);
188 DLLEXPORT void ExportEulerAngles (const Rotation3List& rot_list,
189  const boost::filesystem::path& loc);
190 
191 } // ns
192 
193 #endif