License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / dali / public-api / geometry / mesh-data.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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 // Class header
19 #include <dali/public-api/geometry/mesh-data.h>
20
21 // Internal headers
22 #include <dali/public-api/math/matrix.h>
23 #include <dali/integration-api/debug.h>
24
25 using namespace std;
26
27 namespace Dali
28 {
29
30 namespace // unnamed namespace
31 {
32 static const float kBoundsDefault = 1e10f;
33 } // unnamed namespace
34
35 using Dali::Vector4;
36
37
38 MeshData::MeshData( )
39 : mGeometryType(TRIANGLES),
40   mUseTextureCoords(false),
41   mUseNormals(false),
42   mUseColor(false),
43   mMin( kBoundsDefault,  kBoundsDefault,  kBoundsDefault, 0.0f),
44   mMax(-kBoundsDefault, -kBoundsDefault, -kBoundsDefault, 0.0f)
45 {
46 }
47
48 MeshData::MeshData( const MeshData& meshData )
49 : mVertices( meshData.mVertices ),
50   mFaces( meshData.mFaces ),
51   mGeometryType( meshData.mGeometryType ),
52   mUseTextureCoords( meshData.mUseTextureCoords ),
53   mUseNormals( meshData.mUseNormals ),
54   mUseColor( meshData.mUseColor ),
55   mBones( meshData.mBones ),
56   mMaterial( meshData.mMaterial ),
57   mMin( meshData.mMin ),
58   mMax( meshData.mMax )
59 {
60 }
61
62 MeshData& MeshData::operator=(const MeshData& rhs)
63 {
64   mVertices = rhs.mVertices;
65   mFaces = rhs.mFaces;
66   mGeometryType = rhs.mGeometryType;
67   mUseTextureCoords = rhs.mUseTextureCoords;
68   mUseNormals = rhs.mUseNormals;
69   mUseColor = rhs.mUseColor;
70   mBones = rhs.mBones;
71   mMaterial = rhs.mMaterial;
72   mMin = rhs.mMin;
73   mMax = rhs.mMax;
74
75   return *this;
76 }
77
78 MeshData::~MeshData()
79 {
80 }
81
82 void MeshData::SetData(
83     const VertexContainer& vertices,
84     const FaceIndices&     faceIndices,
85     const BoneContainer&   bones,
86     Material               material )
87 {
88   DALI_ASSERT_ALWAYS( !vertices.empty() && "VertexContainer is empty" );
89   DALI_ASSERT_ALWAYS( !faceIndices.empty() && "FaceIndices is empty" );
90   DALI_ASSERT_ALWAYS( material && "Material handle is empty" );
91
92   mGeometryType = TRIANGLES;
93   mVertices = vertices;
94   mFaces = faceIndices;
95   mMaterial = material;
96   mBones = bones;
97 }
98
99 void MeshData::SetLineData(
100     const VertexContainer& vertices,
101     const FaceIndices&     lineIndices,
102     Material               material )
103 {
104   DALI_ASSERT_ALWAYS( !vertices.empty() && "VertexContainer is empty" );
105   DALI_ASSERT_ALWAYS( !lineIndices.empty() && "FaceIndices is empty" );
106   DALI_ASSERT_ALWAYS( material && "Material handle is empty" );
107
108   mGeometryType = LINES;
109   mVertices = vertices;
110   mFaces = lineIndices;
111   mMaterial = material;
112 }
113
114 void MeshData::SetPointData(
115     const VertexContainer& vertices,
116     Material               material )
117 {
118   DALI_ASSERT_ALWAYS( !vertices.empty() && "VertexContainer is empty" );
119   DALI_ASSERT_ALWAYS( material && "Material handle is empty" );
120
121   mGeometryType = POINTS;
122   mVertices = vertices;
123   mMaterial = material;
124 }
125
126 void MeshData::SetVertices( const VertexContainer& vertices )
127 {
128   DALI_ASSERT_ALWAYS( !vertices.empty() && "VertexContainer is empty" );
129
130   mVertices = vertices;
131 }
132
133 void MeshData::SetFaceIndices( const FaceIndices& faceIndices )
134 {
135   DALI_ASSERT_ALWAYS( !faceIndices.empty() && "FaceIndices is empty" );
136
137   mFaces = faceIndices;
138 }
139
140 void MeshData::AddToBoundingVolume(Vector4& min, Vector4& max, const Dali::Matrix& transform)
141 {
142   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
143
144   for (VertexConstIter iter = mVertices.begin(); iter != mVertices.end(); ++iter)
145   {
146     const Vertex& vertex = *iter;
147     Vector4 point = Vector4(vertex.x, vertex.y, vertex.z, 0.0f);
148
149     mMin = Min(mMin, point);
150     mMax = Max(mMax, point);
151   }
152   // mMin/mMax are accessed through public API, so have chosen to set w to zero
153   // ( They should really be Vector3's )
154   mMin.w=0.0f;
155   mMax.w=0.0f;
156
157   // For the matrix multiplication below to work correctly, the w needs to be 1.
158   Vector4 tmpMin(mMin);
159   Vector4 tmpMax(mMax);
160   tmpMin.w = 1.0f;
161   tmpMax.w = 1.0f;
162
163   min = Min(min, transform * tmpMin);
164   max = Max(max, transform * tmpMax);
165
166   // Ensure the bounding volume out parameters also have w=0. (They should also
167   // be Vector3's )
168   min.w = 0.0f;
169   max.w = 0.0f;
170 }
171
172 MeshData::VertexGeometryType MeshData::GetVertexGeometryType() const
173 {
174   return mGeometryType;
175 }
176
177 size_t MeshData::GetVertexCount() const
178 {
179   return mVertices.size();
180 }
181
182 const MeshData::VertexContainer& MeshData::GetVertices() const
183 {
184   return mVertices;
185 }
186
187 size_t MeshData::GetFaceCount() const
188 {
189   size_t faceCount = 0;
190
191   switch( mGeometryType )
192   {
193     case POINTS:
194       faceCount = mVertices.size();
195       break;
196     case LINES:
197       faceCount = mFaces.size() / 2;
198       break;
199     case TRIANGLES:
200       faceCount = mFaces.size() / 3;
201       break;
202   }
203   return faceCount;
204 }
205
206 const MeshData::FaceIndices& MeshData::GetFaces() const
207 {
208   return mFaces;
209 }
210
211 void MeshData::SetHasTextureCoords(bool hasTexCoords)
212 {
213   mUseTextureCoords = hasTexCoords;
214 }
215
216 bool MeshData::HasTextureCoords() const
217 {
218   return mUseTextureCoords;
219 }
220
221 void MeshData::SetHasNormals(bool hasNormals)
222 {
223   mUseNormals = hasNormals;
224   if( hasNormals )
225   {
226     // Enforce mutual exclusivity
227     mUseColor = false;
228   }
229 }
230
231 bool MeshData::HasNormals() const
232 {
233   return mUseNormals;
234 }
235
236 void MeshData::SetHasColor(bool hasColor)
237 {
238   mUseColor = hasColor;
239   if( hasColor )
240   {
241     // Enforce mutual exclusivity
242     mUseNormals = false;
243   }
244 }
245
246 bool MeshData::HasColor() const
247 {
248   return mUseColor;
249 }
250
251 Material MeshData::GetMaterial() const
252 {
253   return mMaterial;
254 }
255
256 void MeshData::SetMaterial(Material material)
257 {
258   mMaterial = material;
259 }
260
261 size_t MeshData::GetBoneCount() const
262 {
263   return mBones.size();
264 }
265
266 bool MeshData::HasBones() const
267 {
268   return mBones.size() > 0;
269 }
270
271 const BoneContainer& MeshData::GetBones() const
272 {
273   return mBones;
274 }
275
276 const Vector4& MeshData::GetBoundingBoxMin() const
277 {
278   return mMin;
279 }
280
281 void MeshData::SetBoundingBoxMin(const Vector4& bounds)
282 {
283   mMin = bounds;
284 }
285
286 const Vector4& MeshData::GetBoundingBoxMax() const
287 {
288   return mMax;
289 }
290
291
292 void MeshData::SetBoundingBoxMax(const Vector4& bounds)
293 {
294   mMax = bounds;
295 }
296
297 } // namespace Dali
298