| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#ifndef AVOGADRO_RENDERING_PRIMITIVE_H |
| 7 |
#define AVOGADRO_RENDERING_PRIMITIVE_H |
| 8 |
|
| 9 |
#include <avogadro/core/vector.h> |
| 10 |
|
| 11 |
namespace Avogadro { |
| 12 |
namespace Rendering { |
| 13 |
|
| 14 |
/** Identifies the type of object a primitive represents. */ |
| 15 |
enum Type |
| 16 |
{ |
| 17 |
InvalidType = -1, |
| 18 |
AtomType, |
| 19 |
BondType |
| 20 |
}; |
| 21 |
|
| 22 |
/** Used to identify the primitive during picking. */ |
| 23 |
struct Identifier |
| 24 |
{ |
| 25 |
Identifier() : molecule(nullptr), type(InvalidType), index(MaxIndex) {} |
| 26 |
|
| 27 |
bool operator==(const Identifier& other) const |
| 28 |
{ |
| 29 |
return molecule == other.molecule && type == other.type && |
| 30 |
index == other.index; |
| 31 |
} |
| 32 |
|
| 33 |
bool operator!=(const Identifier& other) const { return !operator==(other); } |
| 34 |
|
| 35 |
bool isValid() const { return type != InvalidType && molecule != nullptr; } |
| 36 |
|
| 37 |
const void* molecule; |
| 38 |
Type type; |
| 39 |
Index index; |
| 40 |
}; |
| 41 |
|
| 42 |
class Primitive |
| 43 |
{ |
| 44 |
public: |
| 45 |
/** Identifies the type of object a primitive represents. */ |
| 46 |
enum Type |
| 47 |
{ |
| 48 |
Invalid = -1, |
| 49 |
Atom, |
| 50 |
Bond |
| 51 |
}; |
| 52 |
|
| 53 |
/** Used to identify the primitive during picking. */ |
| 54 |
struct Identifier |
| 55 |
{ |
| 56 |
Identifier() : molecule(nullptr), type(Invalid), index(MaxIndex) {} |
| 57 |
|
| 58 |
bool operator==(const Identifier& other) const |
| 59 |
{ |
| 60 |
return molecule == other.molecule && type == other.type && |
| 61 |
index == other.index; |
| 62 |
} |
| 63 |
|
| 64 |
bool operator!=(const Identifier& other) const |
| 65 |
{ |
| 66 |
return !operator==(other); |
| 67 |
} |
| 68 |
|
| 69 |
bool isValid() const { return type != Invalid; } |
| 70 |
|
| 71 |
const void* molecule; |
| 72 |
Type type; |
| 73 |
Index index; |
| 74 |
}; |
| 75 |
|
| 76 |
Primitive(Identifier id, const Vector3ub& color_) |
| 77 |
: m_identifier(id), m_color(color_) |
| 78 |
{ |
| 79 |
} |
| 80 |
|
| 81 |
Identifier identifier() const { return m_identifier; } |
| 82 |
void setIdentifier(Identifier id) { m_identifier = id; } |
| 83 |
|
| 84 |
const Vector3ub& color() const { return m_color; } |
| 85 |
void setColor(const Vector3ub& c) { m_color = c; } |
| 86 |
|
| 87 |
private: |
| 88 |
Identifier m_identifier; |
| 89 |
Vector3ub m_color; |
| 90 |
}; |
| 91 |
|
| 92 |
class Sphere : public Primitive |
| 93 |
{ |
| 94 |
public: |
| 95 |
Sphere(const Vector3f& position_, float radius_, Primitive::Identifier id, |
| 96 |
const Vector3ub& color_) |
| 97 |
: Primitive(id, color_), m_position(position_), m_radius(radius_) |
| 98 |
{ |
| 99 |
} |
| 100 |
|
| 101 |
const Vector3f& position() const { return m_position; } |
| 102 |
void setPosition(const Vector3f& pos) { m_position = pos; } |
| 103 |
|
| 104 |
float radius() const { return m_radius; } |
| 105 |
void setRadius(float r) { m_radius = r; } |
| 106 |
|
| 107 |
private: |
| 108 |
Vector3f m_position; |
| 109 |
float m_radius; |
| 110 |
}; |
| 111 |
|
| 112 |
class Cylinder : public Primitive |
| 113 |
{ |
| 114 |
public: |
| 115 |
/// Direction must be normalized |
| 116 |
Cylinder(const Vector3f& position_, const Vector3f& direction_, float length_, |
| 117 |
float radius_, Primitive::Identifier id, const Vector3ub& color_) |
| 118 |
: Primitive(id, color_), m_position(position_), m_direction(direction_), |
| 119 |
m_length(length_), m_radius(radius_) |
| 120 |
{ |
| 121 |
} |
| 122 |
|
| 123 |
const Vector3f& position() const { return m_position; } |
| 124 |
void setPosition(const Vector3f& pos) { m_position = pos; } |
| 125 |
|
| 126 |
const Vector3f& direction() const { return m_direction; } |
| 127 |
void setDirection(const Vector3f& dir) { m_direction = dir; } |
| 128 |
|
| 129 |
float length() const { return m_length; } |
| 130 |
void setLength(float l) { m_length = l; } |
| 131 |
|
| 132 |
float radius() const { return m_radius; } |
| 133 |
void setRadius(float r) { m_radius = r; } |
| 134 |
|
| 135 |
private: |
| 136 |
Vector3f m_position; |
| 137 |
Vector3f m_direction; |
| 138 |
float m_length; |
| 139 |
float m_radius; |
| 140 |
}; |
| 141 |
|
| 142 |
} // namespace Rendering |
| 143 |
} // namespace Avogadro |
| 144 |
|
| 145 |
#endif // AVOGADRO_RENDERING_PRIMITIVE_H |