9d03f0d01a1cd239a394096498c4e3ef73c3bd3e
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / navigation-mesh-factory.cpp
1 /*
2  * Copyright (c) 2023 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 // CLASS HEADER
18 #include <dali-scene3d/public-api/loader/navigation-mesh-factory.h>
19
20 // INTERNAL INCLUDES
21 #include <dali-scene3d/internal/algorithm/navigation-mesh-impl.h>
22
23 // EXTERNAL INCLUDES
24 #include <dali/integration-api/debug.h>
25
26 namespace Dali::Scene3D::Loader
27 {
28
29 std::unique_ptr<Algorithm::NavigationMesh> NavigationMeshFactory::CreateFromFile(std::string filename)
30 {
31   std::vector<uint8_t> buffer;
32   auto fin = fopen(filename.c_str(), "rb");
33   if(!fin)
34   {
35     DALI_LOG_ERROR("NavigationMesh: Can't open %s for reading: %s", filename.c_str(), strerror(errno));
36     return nullptr;
37   }
38   else
39   {
40     fseek(fin, 0, SEEK_END);
41     auto size = ftell(fin);
42     fseek(fin, 0, SEEK_SET);
43     buffer.resize(size);
44     auto count = fread(buffer.data(), 1, size, fin);
45     if(!count)
46     {
47       DALI_LOG_ERROR("NavigationMesh: Error reading file: %s\n", filename.c_str());
48       fclose(fin);
49       return nullptr;
50     }
51     fclose(fin);
52
53     return CreateFromBuffer( buffer );
54   }
55 }
56
57 std::unique_ptr<Algorithm::NavigationMesh> NavigationMeshFactory::CreateFromBuffer(const std::vector<uint8_t>& buffer)
58 {
59   auto impl = new Scene3D::Internal::Algorithm::NavigationMesh(buffer);
60   return std::unique_ptr<Algorithm::NavigationMesh>( new Algorithm::NavigationMesh(impl));
61 }
62
63 }