3es  0.7
3escone.h
1 //
2 // author: Kazys Stepanas
3 //
4 #ifndef _3ESCONE_H_
5 #define _3ESCONE_H_
6 
7 #include "3es-core.h"
8 #include "3esshape.h"
9 
10 namespace tes
11 {
21  class _3es_coreAPI Cone : public Shape
22  {
23  public:
29  static const Vector3f DefaultDir;
30 
31  Cone(uint32_t id = 0u, const V3Arg &point = V3Arg(0, 0, 0), const V3Arg &dir = DefaultDir, float angle = 45.0f / 360.0f * float(M_PI), float length = 1.0f);
39  Cone(uint32_t id, uint16_t category, const V3Arg &point = V3Arg(0, 0, 0), const V3Arg &dir = DefaultDir, float angle = 45.0f / 360.0f * float(M_PI), float length = 1.0f);
40 
44  Cone &setAngle(float angle);
47  float angle() const;
48 
52  Cone &setLength(float length);
55  float length() const;
56 
60  Cone &setPoint(const V3Arg &point);
63  Vector3f point() const;
64 
68  Cone &setDirection(const V3Arg &dir);
74  Vector3f direction() const;
75  };
76 
77 
78  inline Cone::Cone(uint32_t id, const V3Arg &point, const V3Arg &dir, float angle, float length)
79  : Shape(SIdCone, id)
80  {
81  setPosition(point);
82  setDirection(dir);
83  setScale(Vector3f(angle, angle, length));
84  }
85 
86 
87  inline Cone::Cone(uint32_t id, uint16_t category, const V3Arg &point, const V3Arg &dir, float angle, float length)
88  : Shape(SIdCone, id, category)
89  {
90  setPosition(point);
91  setDirection(dir);
92  setLength(length);
93  setAngle(angle);
94  //setScale(Vector3f(angle, angle, length));
95  }
96 
97 
98  inline Cone &Cone::setAngle(float angle)
99  {
100  Vector3f s = scale();
101  s.x = s.y = s.z * std::tan(angle);
102  setScale(s);
103  return *this;
104  }
105 
106 
107  inline float Cone::angle() const
108  {
109  return scale().x;
110  // scale X/Y encode the radius of the cone base.
111  // Convert to angle angle as:
112  // tan(theta) = radius / length
113  // theta = atan(radius / length)
114  const Vector3f s = scale();
115  const float length = s.z;
116  const float radius = s.x;
117  return (length != 0.0f) ? std::atan(radius/ length) : 0.0f;
118  }
119 
120 
121  inline Cone &Cone::setLength(float length)
122  {
123  // Changing the length requires maintaining the angle, so we must adjust the radius to suit.
124  const float angle = this->angle();
125  _data.attributes.scale[2] = length;
126  setAngle(angle);
127  return *this;
128  }
129 
130 
131  inline float Cone::length() const
132  {
133  return scale().z;
134  }
135 
136 
137  inline Cone &Cone::setPoint(const V3Arg &point)
138  {
139  setPosition(point);
140  return *this;
141  }
142 
143 
144  inline Vector3f Cone::point() const
145  {
146  return position();
147  }
148 
149 
150  inline Cone &Cone::setDirection(const V3Arg &dir)
151  {
152  Quaternionf rot;
153  if (dir.v3.dot(DefaultDir) > -0.9998f)
154  {
155  rot = Quaternionf(DefaultDir, dir);
156  }
157  else
158  {
159  rot.setAxisAngle(Vector3f::axisx, float(M_PI));
160  }
161  setRotation(rot);
162  return *this;
163  }
164 
165 
166  inline Vector3f Cone::direction() const
167  {
168  Quaternionf rot = rotation();
169  return rot * DefaultDir;
170  }
171 }
172 
173 #endif // _3ESCONE_H_
A base class for encapsulating a shape which is to be represented remotely.
Definition: 3esshape.h:39
float scale[3]
Object scale.
Definition: 3esmessages.h:358
Cone & setPoint(const V3Arg &point)
Set the position of the cone apex.
Definition: 3escone.h:137
Cone & setLength(float length)
Set the cone length, apex to base.
Definition: 3escone.h:121
Vector3f direction() const
Get the cone direction vector.
Definition: 3escone.h:166
float length() const
Get the cone length, apex to base.
Definition: 3escone.h:131
static const Vector3< T > axisx
The vector (1, 0, 0).
Definition: 3esvector3.h:44
Cone & setAngle(float angle)
Sets the cone angle at the apex (radians).
Definition: 3escone.h:98
Vector3f point() const
Get the position of the cone apex.
Definition: 3escone.h:144
static const Vector3f DefaultDir
Default direction used as a reference orientation for packing the rotation.
Definition: 3escone.h:29
float angle() const
Get the cone angle at the apex (radians).
Definition: 3escone.h:107
Definition: 3esbounds.h:13
Cone & setDirection(const V3Arg &dir)
Set the cone direction vector.
Definition: 3escone.h:150
Vector3< float > Vector3f
Defines a single precision vector.
Definition: 3esvector3.h:14
Quaternion< T > & setAxisAngle(const Vector3< T > &axis, const T &angle)
Sets this quaternion from an axis of rotation and the angle of rotation about that axis (radians)...
Defines a cone shape to display.
Definition: 3escone.h:21
Vector3f v3
Vector 3 value.
Definition: 3esv3arg.h:45
Represents a vector in R3.
Definition: 3esvector3.h:14
A helper structure used to convert from float or double pointers to Vector3f arguments.
Definition: 3esv3arg.h:14
T x
Direct data member access.
Definition: 3esvector3.h:30
Quaternion< float > Quaternionf
Defines a single precision quaternion.
Definition: 3esquaternion.h:198
ObjectAttributes attributes
Initial transformation and colour.
Definition: 3esmessages.h:408
T dot(const Vector3< T > &other) const
Calculates the dot product of this.other.
Definition: 3esvector3.h:422