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 #include "mesh.h"
19
20 #include "mutex.h"
21
22 using std::vector;
23
24 namespace Avogadro {
25 namespace Core {
26
Mesh()27 Mesh::Mesh() : m_stable(true), m_other(0), m_cube(0), m_lock(new Mutex)
28 {
29 m_vertices.reserve(100);
30 m_normals.reserve(100);
31 m_colors.reserve(1);
32 }
33
Mesh(const Mesh & other)34 Mesh::Mesh(const Mesh& other)
35 : m_vertices(other.m_vertices), m_normals(other.m_normals),
36 m_colors(other.m_colors), m_name(other.m_name), m_stable(true),
37 m_isoValue(other.m_isoValue), m_other(other.m_other), m_cube(other.m_cube),
38 m_lock(new Mutex)
39 {
40 }
41
~Mesh()42 Mesh::~Mesh()
43 {
44 delete m_lock;
45 m_lock = 0;
46 }
47
reserve(unsigned int size,bool useColors)48 bool Mesh::reserve(unsigned int size, bool useColors)
49 {
50 m_vertices.reserve(size);
51 m_normals.reserve(size);
52 if (useColors)
53 m_colors.reserve(size);
54 return true;
55 }
56
setStable(bool isStable)57 void Mesh::setStable(bool isStable)
58 {
59 m_stable = isStable;
60 }
61
stable()62 bool Mesh::stable()
63 {
64 return m_stable;
65 }
66
vertices() const67 const Core::Array<Vector3f>& Mesh::vertices() const
68 {
69 return m_vertices;
70 }
71
vertex(int n) const72 const Vector3f* Mesh::vertex(int n) const
73 {
74 return &(m_vertices[n * 3]);
75 }
76
setVertices(const Core::Array<Vector3f> & values)77 bool Mesh::setVertices(const Core::Array<Vector3f>& values)
78 {
79 m_vertices.clear();
80 m_vertices = values;
81 return true;
82 }
83
addVertices(const Core::Array<Vector3f> & values)84 bool Mesh::addVertices(const Core::Array<Vector3f>& values)
85 {
86 if (m_vertices.capacity() < m_vertices.size() + values.size())
87 m_vertices.reserve(m_vertices.capacity() * 2);
88 if (values.size() % 3 == 0) {
89 for (unsigned int i = 0; i < values.size(); ++i)
90 m_vertices.push_back(values.at(i));
91 return true;
92 } else {
93 return false;
94 }
95 }
96
normals() const97 const Core::Array<Vector3f>& Mesh::normals() const
98 {
99 return m_normals;
100 }
101
normal(int n) const102 const Vector3f* Mesh::normal(int n) const
103 {
104 return &(m_normals[n * 3]);
105 }
106
setNormals(const Core::Array<Vector3f> & values)107 bool Mesh::setNormals(const Core::Array<Vector3f>& values)
108 {
109 m_normals.clear();
110 m_normals = values;
111 return true;
112 }
113
addNormals(const Core::Array<Vector3f> & values)114 bool Mesh::addNormals(const Core::Array<Vector3f>& values)
115 {
116 if (m_normals.capacity() < m_normals.size() + values.size())
117 m_normals.reserve(m_normals.capacity() * 2);
118 if (values.size() % 3 == 0) {
119 for (unsigned int i = 0; i < values.size(); ++i)
120 m_normals.push_back(values.at(i));
121 return true;
122 } else {
123 return false;
124 }
125 }
126
colors() const127 const Core::Array<Color3f>& Mesh::colors() const
128 {
129 return m_colors;
130 }
131
color(int n) const132 const Color3f* Mesh::color(int n) const
133 {
134 // If there is only one color return that, otherwise colored by vertex.
135 if (m_colors.size() == 1)
136 return &(m_colors[0]);
137 else
138 return &(m_colors[n * 3]);
139 }
140
setColors(const Core::Array<Color3f> & values)141 bool Mesh::setColors(const Core::Array<Color3f>& values)
142 {
143 m_colors.clear();
144 m_colors = values;
145 return true;
146 }
147
addColors(const Core::Array<Color3f> & values)148 bool Mesh::addColors(const Core::Array<Color3f>& values)
149 {
150 if (m_colors.capacity() < m_colors.size() + values.size())
151 m_colors.reserve(m_colors.capacity() * 2);
152 if (values.size() % 3 == 0) {
153 for (unsigned int i = 0; i < values.size(); ++i)
154 m_colors.push_back(values.at(i));
155 return true;
156 } else {
157 return false;
158 }
159 }
160
valid() const161 bool Mesh::valid() const
162 {
163 if (m_vertices.size() == m_normals.size()) {
164 if (m_colors.size() == 1 || m_colors.size() == m_vertices.size())
165 return true;
166 else
167 return false;
168 } else {
169 return false;
170 }
171 }
172
clear()173 bool Mesh::clear()
174 {
175 m_vertices.clear();
176 m_normals.clear();
177 m_colors.clear();
178 return true;
179 }
180
operator =(const Mesh & other)181 Mesh& Mesh::operator=(const Mesh& other)
182 {
183 m_vertices = other.m_vertices;
184 m_normals = other.m_vertices;
185 m_colors = other.m_colors;
186 m_name = other.m_name;
187 m_isoValue = other.m_isoValue;
188
189 return *this;
190 }
191
192 } // End namespace QtGui
193 } // End namespace Avogadro
194