Cの単一ファイル波関数関数崩壊ライブラリとコマンドラインツール
これは、重複するWFCメソッドをサポートする初期バージョンです。このメソッドは小さな入力画像を取得し、入力画像と局所的に類似したより大きな出力画像を生成します。入力/出力ペアのいくつかの例:

WFCは、手続き上のマップ生成によく使用されますが、このユースケースに限定されません。
ライブラリは非常にパフォーマンスがあり、他の実装では見られない多くの最適化が含まれています。例として、MacBook Air M1(2020)で上記の128x128画像(3x3パターン、反転、回転)の生成には、それぞれ1.35、0.92、0.31、7.7、1.74、および0.67秒がかかりました。これには、画像の読み込み/節約時間が含まれます。
プロジェクトの1つのファイルには、このようなwfc.hを含める必要があります。
#define WFC_IMPLEMENTATION
#include "wfc.h"他のファイルもwfc.h含めて使用できますが、 WFC_IMPLEMENTATIONマクロを定義してはいけません。
または、 make wfc.oを使用して従来の.Oファイルを構築し、通常のヘッダーファイルとしてwfc.h使用することもできます。
使用法:
struct wfc * wfc = wfc_overlapping (
128 , // Output image width in pixels
128 , // Output image height in pixels
input_image , // Input image that will be cut into tiles
3 , // Tile width in pixels
3 , // Tile height in pixels
1 , // Expand input image on the right and bottom
1 , // Add horizontal flips of all tiles
1 , // Add vertical flips of all tiles
1 // Add n*90deg rotations of all tiles
);
wfc_run ( wfc , -1 ); // Run Wave Function Collapse
// -1 means no limit on iterations
struct wfc_image * output_image = wfc_output_image ( wfc );
wfc_destroy ( wfc );
// use output_image->data
// wfc_img_destroy(output_image);デフォルトでは、入力と出力のstruct wfc_imageを使用します。
struct wfc_image {
unsigned char * data ;
int component_cnt ;
int width ;
int height ;
} dataパディングなしでしっかりと詰め込まれています。各ピクセルは、 component_cntコンポーネント(たとえば、RGBA形式の4つのコンポーネント)で構成されています。出力画像には、入力画像と同じ数のコンポーネントがあります。
wfc_run解決策が見つからない場合は0を返します。あなたはそうするようにもう一度試すことができます:
wfc_init ( wfc );
wfc_run ( wfc , -1 );WFCはオプションでSTB_IMAGE.HおよびSTB_IMAGE_WRITE.Hを使用して、画像ファイルを直接動作させるための利便性関数を提供できます。
通常、 wfc.hと同じディレクトリにstb_image.hおよびstb_image_write.hを配置し、プロジェクトファイルの1つに実装を含めます。
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image.h"
#include "stb_image_write.h"さらに、 wfc.hにSTBを使用するよう指示します。
#define WFC_IMPLEMENTATION
#define WFC_USE_STB
#include "wfc.h"使用法:
struct wfc_image * input_image = wfc_img_load ( "input.png" );
struct wfc * wfc = wfc_overlapping (
...
input_image ,
...
);
wfc_run ( wfc , -1 ); // Run Wave Function Collapse
// -1 means no restriction on number of iterations
wfc_export ( wfc , "output.png" );
wfc_img_destroy ( input_image );
wfc_destroy ( wfc );STBを含めることにより有効にされる追加機能:
struct wfc_image * image = wfc_img_load ( "image.png" )
wfc_img_save ( image , "image.png" )
wfc_export ( wfc , "output.png" )
wfc_export_tiles ( wfc , "directory" )
// don't forget to wfc_img_destroy(image) loaded images コマンドラインツールはライブラリを使用し、WFC画像を生成できます。このツールは、stb_image.hおよびstb_image_write.hに依存します。両方のファイルをwfctool.cと同じディレクトリに配置します。
make
./wfc
./wfcを実行して、利用可能なオプションを表示します
基本的な使用法:
./wfc -m overlapping samples/wrinkles.png output.png
WFCを使用してくれてありがとう。バグが見つかった場合は、質問がある場合、またはフィードバックをお知らせください。また、あなたの作品を共有したいなら、それはとても感謝しています。
gmail.comのsamp.krystian