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