Skip to content

Commit 2c3d94e

Browse files
committed
TexturePackerのリファクタ:ファイル訳
1 parent 86dff67 commit 2c3d94e

File tree

8 files changed

+206
-181
lines changed

8 files changed

+206
-181
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
namespace plateau::texture {
4+
class AtlasContainer {
5+
public:
6+
7+
explicit AtlasContainer(const size_t _gap, const size_t _horizontal_range, const size_t _vertical_range);
8+
9+
size_t getGap() const {
10+
return gap;
11+
}
12+
size_t getRootHorizontalRange() const {
13+
return root_horizontal_range;
14+
}
15+
size_t getHorizontalRange() const {
16+
return horizontal_range;
17+
}
18+
size_t getVerticalRange() const {
19+
return vertical_range;
20+
}
21+
void add(const size_t _length);
22+
23+
private:
24+
size_t gap; // 画像を格納するコンテナの高さ
25+
size_t root_horizontal_range; // コンテナ内で未使用の領域のX座標
26+
size_t horizontal_range; // 最後の画像をパッキングするための領域のX座標
27+
size_t vertical_range; // パッキングの対象となる親の画像のコンテナが配置されている左上のY座標
28+
};
29+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#pragma once
2+
3+
namespace plateau::texture{
4+
class AtlasInfo {
5+
public:
6+
7+
explicit AtlasInfo(const bool valid, const size_t left, const size_t top,
8+
const size_t width, const size_t height,
9+
double u_pos, double v_pos, double u_factor, double v_factor, std::string src_texture_path) :
10+
valid_(valid), left_(left), top_(top), width_(width), height_(height),
11+
u_pos_(u_pos), v_pos_(v_pos), u_factor_(u_factor), v_factor_(v_factor), src_texture_path(std::move(src_texture_path)) {
12+
}
13+
14+
static AtlasInfo empty() {
15+
return AtlasInfo(false, 0, 0, 0, 0, 0, 0, 0, 0, "");
16+
}
17+
18+
size_t getLeft() const {
19+
return left_;
20+
}
21+
size_t getTop() const {
22+
return top_;
23+
}
24+
size_t getWidth() const {
25+
return width_;
26+
}
27+
size_t getHeight() const {
28+
return height_;
29+
}
30+
double getUPos() const {
31+
return u_pos_;
32+
}
33+
double getVPos() const {
34+
return v_pos_;
35+
}
36+
double getUFactor() const {
37+
return u_factor_;
38+
}
39+
double getVFactor() const {
40+
return v_factor_;
41+
}
42+
43+
bool getValid() const;
44+
45+
const std::string& getSrcTexturePath() const {
46+
return src_texture_path;
47+
};
48+
49+
private:
50+
bool valid_; // パッキングが成功したかどうか
51+
size_t left_; // パッキングされた画像の左上のX座標
52+
size_t top_; // パッキングされた画像の左上のY座標
53+
size_t width_; // パッキングされた画像の幅
54+
size_t height_; // パッキングされた画像の高さ
55+
double u_pos_; // uv座標の左上u座標
56+
double v_pos_; // uv座標の左上v座標
57+
double u_factor_; // u座標の倍率
58+
double v_factor_; // v座標の倍率
59+
std::string src_texture_path;
60+
};
61+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma once
2+
3+
#include "texture_image_base.h"
4+
#include "atlas_info.h"
5+
#include "atlas_container.h"
6+
7+
namespace plateau::texture{
8+
class TextureAtlasCanvas {
9+
public:
10+
11+
explicit TextureAtlasCanvas(size_t width, size_t height) :
12+
vertical_range_(0), capacity_(0), coverage_(0),
13+
canvas_(TextureImageBase::createNewTexture(width, height)),
14+
canvas_width_(width), canvas_height_(height) {
15+
}
16+
17+
18+
void setSaveFilePathIfEmpty(const std::string& original_file_path);
19+
const std::string& getSaveFilePath() const;
20+
21+
TextureImageBase& getCanvas() {
22+
return *canvas_;
23+
}
24+
25+
void flush();
26+
27+
/**
28+
* \brief テクスチャ全体に対しての既にパックされた画像の占有率(100%)
29+
*/
30+
double getCoverage() const {
31+
return coverage_;
32+
}
33+
34+
void update(const size_t width, const size_t height, const bool is_new_container, const AtlasInfo& packed_texture_info); // 画像のパッキング成功時の処理、第3引数(TRUE:新規コンテナを作成、FALSE:既存コンテナに追加)
35+
AtlasInfo insert(const size_t width, const size_t height, const std::string& src_texture_path); // 指定された画像領域(width x height)の領域が確保できるか検証、戻り値AtrasInfoの「valid」ブール値(true:成功、false:失敗)で判定可能
36+
bool isTexturePacked(const std::string& src_file_path, AtlasInfo& out_atlas_info);
37+
38+
private:
39+
std::vector<AtlasContainer> container_list_;
40+
size_t canvas_width_;
41+
size_t canvas_height_;
42+
size_t vertical_range_;
43+
size_t capacity_;
44+
double coverage_;
45+
std::unique_ptr<TextureImageBase> canvas_;
46+
std::string save_file_path_;
47+
std::vector<AtlasInfo> packed_textures_info;
48+
};
49+
}

include/plateau/texture/texture_packer.h

Lines changed: 1 addition & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -9,136 +9,10 @@
99
#include <utility>
1010
#include <vector>
1111
#include <filesystem>
12+
#include "texture_atlas_canvas.h"
1213

1314
namespace plateau::texture {
1415

15-
class AtlasInfo {
16-
public:
17-
18-
explicit AtlasInfo(const bool valid, const size_t left, const size_t top,
19-
const size_t width, const size_t height,
20-
double u_pos, double v_pos, double u_factor, double v_factor, std::string src_texture_path) :
21-
valid_(valid), left_(left), top_(top), width_(width), height_(height),
22-
u_pos_(u_pos), v_pos_(v_pos), u_factor_(u_factor), v_factor_(v_factor), src_texture_path(std::move(src_texture_path)) {
23-
}
24-
25-
static AtlasInfo empty() {
26-
return AtlasInfo(false, 0, 0, 0, 0, 0, 0, 0, 0, "");
27-
}
28-
29-
size_t getLeft() const {
30-
return left_;
31-
}
32-
size_t getTop() const {
33-
return top_;
34-
}
35-
size_t getWidth() const {
36-
return width_;
37-
}
38-
size_t getHeight() const {
39-
return height_;
40-
}
41-
double getUPos() const {
42-
return u_pos_;
43-
}
44-
double getVPos() const {
45-
return v_pos_;
46-
}
47-
double getUFactor() const {
48-
return u_factor_;
49-
}
50-
double getVFactor() const {
51-
return v_factor_;
52-
}
53-
54-
bool getValid() const;
55-
56-
const std::string& getSrcTexturePath() const {
57-
return src_texture_path;
58-
};
59-
60-
private:
61-
bool valid_; // パッキングが成功したかどうか
62-
size_t left_; // パッキングされた画像の左上のX座標
63-
size_t top_; // パッキングされた画像の左上のY座標
64-
size_t width_; // パッキングされた画像の幅
65-
size_t height_; // パッキングされた画像の高さ
66-
double u_pos_; // uv座標の左上u座標
67-
double v_pos_; // uv座標の左上v座標
68-
double u_factor_; // u座標の倍率
69-
double v_factor_; // v座標の倍率
70-
std::string src_texture_path;
71-
};
72-
73-
class AtlasContainer {
74-
public:
75-
76-
explicit AtlasContainer(const size_t _gap, const size_t _horizontal_range, const size_t _vertical_range);
77-
78-
size_t getGap() const {
79-
return gap;
80-
}
81-
size_t getRootHorizontalRange() const {
82-
return root_horizontal_range;
83-
}
84-
size_t getHorizontalRange() const {
85-
return horizontal_range;
86-
}
87-
size_t getVerticalRange() const {
88-
return vertical_range;
89-
}
90-
void add(const size_t _length);
91-
92-
private:
93-
size_t gap; // 画像を格納するコンテナの高さ
94-
size_t root_horizontal_range; // コンテナ内で未使用の領域のX座標
95-
size_t horizontal_range; // 最後の画像をパッキングするための領域のX座標
96-
size_t vertical_range; // パッキングの対象となる親の画像のコンテナが配置されている左上のY座標
97-
};
98-
99-
100-
class TextureAtlasCanvas {
101-
public:
102-
103-
explicit TextureAtlasCanvas(size_t width, size_t height) :
104-
vertical_range_(0), capacity_(0), coverage_(0),
105-
canvas_(TextureImageBase::createNewTexture(width, height)),
106-
canvas_width_(width), canvas_height_(height) {
107-
}
108-
109-
110-
void setSaveFilePathIfEmpty(const std::string& original_file_path);
111-
const std::string& getSaveFilePath() const;
112-
113-
TextureImageBase& getCanvas() {
114-
return *canvas_;
115-
}
116-
117-
void flush();
118-
119-
/**
120-
* \brief テクスチャ全体に対しての既にパックされた画像の占有率(100%)
121-
*/
122-
double getCoverage() const {
123-
return coverage_;
124-
}
125-
126-
void update(const size_t width, const size_t height, const bool is_new_container, const AtlasInfo& packed_texture_info); // 画像のパッキング成功時の処理、第3引数(TRUE:新規コンテナを作成、FALSE:既存コンテナに追加)
127-
AtlasInfo insert(const size_t width, const size_t height, const std::string& src_texture_path); // 指定された画像領域(width x height)の領域が確保できるか検証、戻り値AtrasInfoの「valid」ブール値(true:成功、false:失敗)で判定可能
128-
bool isTexturePacked(const std::string& src_file_path, AtlasInfo& out_atlas_info);
129-
130-
private:
131-
std::vector<AtlasContainer> container_list_;
132-
size_t canvas_width_;
133-
size_t canvas_height_;
134-
size_t vertical_range_;
135-
size_t capacity_;
136-
double coverage_;
137-
std::unique_ptr<TextureImageBase> canvas_;
138-
std::string save_file_path_;
139-
std::vector<AtlasInfo> packed_textures_info;
140-
};
141-
14216
/// テクスチャのアトラス化をします。
14317
/// 実装上の注意:
14418
/// TexturePacker にAPIを増やすとき、変更が必要なのは texture_packer.cpp に加えて texture_packer_dummy.cpp もであることに注意してください。

src/texture/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ else()
1010
"png_texture_image.cpp"
1111
"tiff_texture_image.cpp"
1212
"texture_image_base.cpp"
13+
"texture_atlas_canvas.cpp"
14+
"atlas_container.cpp"
1315
)
1416
endif()

src/texture/atlas_container.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <plateau/texture/atlas_container.h>
2+
3+
namespace plateau::texture {
4+
AtlasContainer::AtlasContainer(const size_t _gap, const size_t _horizontal_range, const size_t _vertical_range) {
5+
gap = _gap;
6+
vertical_range = _vertical_range;
7+
root_horizontal_range = _horizontal_range;
8+
horizontal_range = _horizontal_range;
9+
}
10+
11+
void AtlasContainer::add(const size_t _length) {
12+
horizontal_range = root_horizontal_range;
13+
root_horizontal_range = _length + root_horizontal_range;
14+
}
15+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <plateau/texture/texture_atlas_canvas.h>
2+
#include <filesystem>
3+
#include <sstream>
4+
5+
namespace plateau::texture {
6+
namespace fs = std::filesystem;
7+
void TextureAtlasCanvas::setSaveFilePathIfEmpty(const std::string& original_file_path) {
8+
if (!save_file_path_.empty())
9+
return;
10+
11+
auto original_path = std::filesystem::u8path(original_file_path);
12+
const auto original_filename_without_ext = original_path.filename().replace_extension("").u8string();
13+
14+
for (int cnt = 0;; ++cnt) {
15+
std::stringstream ss;
16+
ss << std::setw(6) << std::setfill('0') << cnt;
17+
std::string num = ss.str();
18+
19+
const auto new_filename = std::string("packed_image_").append(original_filename_without_ext).append("_").append(num).append(".png");
20+
const auto& path = original_path.replace_filename(new_filename);
21+
if (!std::filesystem::is_regular_file(path)) {
22+
save_file_path_ = path.u8string();
23+
break;
24+
}
25+
}
26+
}
27+
28+
const std::string& TextureAtlasCanvas::getSaveFilePath() const {
29+
return save_file_path_;
30+
}
31+
32+
void TextureAtlasCanvas::flush() {
33+
bool result = canvas_->save(save_file_path_);
34+
if(!result) {
35+
throw std::runtime_error("failed to write image file.");
36+
}
37+
}
38+
39+
bool TextureAtlasCanvas::isTexturePacked(const std::string& src_file_path, AtlasInfo& out_atlas_info) {
40+
for(const auto& tex_info : packed_textures_info) {
41+
if(tex_info.getSrcTexturePath() == src_file_path) {
42+
out_atlas_info = tex_info;
43+
return true;
44+
}
45+
}
46+
return false;
47+
}
48+
}

0 commit comments

Comments
 (0)