1 /******************************************************************************
2 
3   This source file is part of the Avogadro project.
4 
5   Copyright 2008 Marcus D. Hanwell
6   Copyright 2012 Kitware, Inc.
7 
8   This source code is released under the New BSD License, (the "License").
9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 
16 ******************************************************************************/
17 
18 #ifndef AVOGADRO_CORE_MESH_H
19 #define AVOGADRO_CORE_MESH_H
20 
21 #include "avogadrocore.h"
22 
23 #include "array.h"
24 #include "color3f.h"
25 #include "vector.h"
26 
27 namespace Avogadro {
28 namespace Core {
29 
30 class Molecule;
31 class Mutex;
32 
33 /**
34  * @class Mesh mesh.h <avogadro/core/mesh.h>
35  * @brief Encapsulation of a triangular mesh that makes up a surface.
36  * @author Marcus D. Hanwell
37  *
38  * The Mesh class is a data container that provides a Mesh object. All
39  * meshes should be owned by a Molecule. It should also be removed by the
40  * Molecule that owns it. Meshes encapsulate triangular meshes that can also
41  * have colors associated with each vertex.
42  */
43 
44 class MeshPrivate;
45 class AVOGADROCORE_EXPORT Mesh
46 {
47 public:
48   /**
49    * Constructor.
50    */
51   Mesh();
52 
53   /**
54    * Copy constructor
55    */
56   Mesh(const Mesh& other);
57 
58   /**
59    * Destructor.
60    */
61   ~Mesh();
62 
63   /**
64    * Reserve the expected space for the mesh. This causes all member array
65    * storage to call the reserve function with the number specified.
66    * @param size Expected size of the mesh.
67    * @param colors Should the colors array reserve this space too? Defaults
68    * to false.
69    * @return True on success.
70    */
71   bool reserve(unsigned int size, bool colors = false);
72 
73   /**
74    * This function allows long running calculations to mark the mesh as in
75    * progress.
76    * @param stable Indicate that the Mesh is currently being modified.
77    */
78   void setStable(bool stable);
79 
80   /**
81    * Indicate whether the Mesh is complete or currently being modified. In
82    * general using Mesh values from an unstable Mesh is not advisable.
83    * @return True if the Mesh is complete, false if it is being modified.
84    */
85   bool stable();
86 
87   /**
88    * Set the iso value that was used to generate the Mesh.
89    */
setIsoValue(float value)90   void setIsoValue(float value) { m_isoValue = value; }
91 
92   /**
93    * @return The iso value used to generate the Mesh.
94    */
isoValue()95   float isoValue() const { return m_isoValue; }
96 
97   /**
98    * Set the unique id of the other Mesh if this Mesh is part of a pair.
99    */
setOtherMesh(unsigned int other)100   void setOtherMesh(unsigned int other) { m_other = other; }
101 
102   /**
103    * @return The unique id of the other Mesh if this is part of a pair.
104    */
otherMesh()105   unsigned int otherMesh() const { return m_other; }
106 
107   /**
108    * Set the unique id of the Cube the Mesh was generated from.
109    */
setCube(unsigned int cube_)110   void setCube(unsigned int cube_) { m_cube = cube_; }
111 
112   /**
113    * @return The unique id of the Cube the Mesh was generated from.
114    */
cube()115   unsigned int cube() const { return m_cube; }
116 
117   /**
118    * @return Array containing all of the vertices in a one dimensional array.
119    */
120   const Core::Array<Vector3f>& vertices() const;
121 
122   /**
123    * @return The number of vertices.
124    */
numVertices()125   unsigned int numVertices() const
126   {
127     return static_cast<unsigned int>(m_vertices.size());
128   }
129 
130   /**
131    * @return Pointer to the first vertex of the specified triangle.
132    */
133   const Vector3f* vertex(int n) const;
134 
135   /**
136    * Clear the vertices vector and assign new values.
137    */
138   bool setVertices(const Core::Array<Vector3f>& values);
139 
140   /**
141    * Add one or more vertices, i.e., the array is expected to be of length
142    * 3 x n where n is an integer.
143    */
144   bool addVertices(const Core::Array<Vector3f>& values);
145 
146   /**
147    * @return Array containing all of the normals in a one-dimensional array.
148    */
149   const Core::Array<Vector3f>& normals() const;
150 
151   /**
152    * @return The number of normals.
153    */
numNormals()154   unsigned int numNormals() const
155   {
156     return static_cast<unsigned int>(m_normals.size());
157   }
158 
159   /**
160    * @return Pointer to the first normal of the specified triangle.
161    */
162   const Vector3f* normal(int n) const;
163 
164   /**
165    * Clear the normals array and assign new values.
166    */
167   bool setNormals(const Core::Array<Vector3f>& values);
168 
169   /**
170    * Add one or more normals, i.e., the array is expected to be of length
171    * 3 x n where n is an integer.
172    */
173   bool addNormals(const Core::Array<Vector3f>& values);
174 
175   /**
176    * @return Array containing all of the colors in a one-dimensional array.
177    */
178   const Core::Array<Color3f>& colors() const;
179 
180   /**
181    * @return Pointer to the first color of the specified triangle.
182    */
183   const Color3f* color(int n) const;
184 
185   /**
186    * Clear the colors array and assign new values.
187    */
188   bool setColors(const Core::Array<Color3f>& values);
189 
190   /**
191    * Add one or more normals, i.e., the array is expected to be of length
192    * 3 x n where n is an integer.
193    */
194   bool addColors(const Core::Array<Color3f>& values);
195 
196   /**
197    * Sanity checking function - is the mesh sane?
198    * @return True if the Mesh object is sane and composed of the right number
199    * of elements.
200    */
201   bool valid() const;
202 
203   /**
204    * Clear all mesh data.
205    * @return True on success.
206    */
207   bool clear();
208 
209   /**
210    * Overloaded operator.
211    */
212   Mesh& operator=(const Mesh& other);
213 
214   /**
215    * Set the name of the Mesh.
216    */
setName(const std::string & name_)217   void setName(const std::string& name_) { m_name = name_; }
218 
219   /**
220    * @return The name of the Mesh.
221    */
name()222   std::string name() const { return m_name; }
223 
224   /**
225    * Provides locking.
226    */
lock()227   Mutex* lock() const { return m_lock; }
228 
229   friend class Molecule;
230 
231 private:
232   Core::Array<Vector3f> m_vertices;
233   Core::Array<Vector3f> m_normals;
234   Core::Array<Color3f> m_colors;
235   std::string m_name;
236   bool m_stable;
237   float m_isoValue;
238   unsigned int m_other; // Unique id of the other mesh if this is part of a pair
239   unsigned int m_cube;  // Unique id of the cube this mesh was generated from
240   Mutex* m_lock;
241 };
242 
243 } // End namespace Core
244 } // End namespace Avogadro
245 
246 #endif // AVOGADRO_CORE_MESH_H
247